public Skybox(RenderContext rc) { // load texture imgData = rc.LoadImage("Assets/skyboxOberflächenfarbe.jpg"); _iTex = rc.CreateTexture(imgData); _rc = rc; _syboxMesh = MeshReader.LoadMesh(@"Assets/skybox.obj.model"); }
public Weapon(DynamicWorld world, Game game) { _click = true; _world = world; _sphereCollider = _world.AddSphereShape(1); _game = game; _srTomato = _game.SceneLoader.LoadTomato(); Magazin = 10; imgData = game.RC.LoadImage("Assets/TomateOberflächenfarbe.jpg"); RC = game.RC; }
/// <summary> /// Creates a new Bitmap-Object from an image file, /// locks the bits in the memory and makes them available /// for furher action (e.g. creating a texture). /// Method must be called before creating a texture to get the necessary /// ImageData struct. /// </summary> /// <param name="filename">Path to the image file you would like to use as texture.</param> /// <returns>An ImageData object with all necessary information for the texture-binding process.</returns> public ImageData LoadImage(String filename) { var bmp = new Bitmap(filename); //Flip y-axis, otherwise texture would be upside down bmp.RotateFlip(RotateFlipType.RotateNoneFlipY); BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); int strideAbs = (bmpData.Stride < 0) ? -bmpData.Stride : bmpData.Stride; int bytes = (strideAbs)*bmp.Height; var ret = new ImageData { PixelData = new byte[bytes], Height = bmpData.Height, Width = bmpData.Width, Stride = bmpData.Stride }; Marshal.Copy(bmpData.Scan0, ret.PixelData, 0, bytes); bmp.UnlockBits(bmpData); return ret; }
/// <summary> /// Creates a new Texture and binds it to the shader. /// </summary> /// <param name="img">A given ImageData object, containing all necessary information for the upload to the graphics card.</param> /// <returns>An ITexture that can be used for texturing in the shader. In this implementation, the handle is an integer-value which is necessary for OpenTK.</returns> public ITexture CreateTexture(ImageData img) { int id = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, id); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, img.Width, img.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, img.PixelData); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) TextureMinFilter.Linear); ITexture texID = new Texture {handle = id}; return texID; }
/// <summary> /// Creates a new Image with a specified size and color. /// </summary> /// <param name="width">The width of the image.</param> /// <param name="height">The height of the image.</param> /// <param name="bgColor">The color of the image. Value must be JS compatible.</param> /// <returns>An ImageData struct containing all necessary information for further processing.</returns> public ImageData CreateImage(int width, int height, String bgColor) { var bmp = new Bitmap(width, height); Graphics gfx = Graphics.FromImage(bmp); Color color = Color.FromName(bgColor); gfx.Clear(color); bmp.RotateFlip(RotateFlipType.RotateNoneFlipY); BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); int strideAbs = (bmpData.Stride < 0) ? -bmpData.Stride : bmpData.Stride; int bytes = (strideAbs)*bmp.Height; var ret = new ImageData { PixelData = new byte[bytes], Height = bmpData.Height, Width = bmpData.Width, Stride = bmpData.Stride }; Marshal.Copy(bmpData.Scan0, ret.PixelData, 0, bytes); bmp.UnlockBits(bmpData); return ret; }
/// <summary> /// Maps a specified text with on an image. /// </summary> /// <param name="imgData">The ImageData struct with the PixelData from the image.</param> /// <param name="fontName">The name of the text-font.</param> /// <param name="fontSize">The size of the text-font.</param> /// <param name="text">The text that sould be mapped on the iamge.</param> /// <param name="textColor">The color of the text-font.</param> /// <param name="startPosX">The horizontal start-position of the text on the image.</param> /// <param name="startPosY">The vertical start-position of the text on the image.</param> /// <returns>An ImageData struct containing all necessary information for further processing</returns> public ImageData TextOnImage(ImageData imgData, String fontName, float fontSize, String text, String textColor, float startPosX, float startPosY) { var imgDataNew = imgData; GCHandle arrayHandle = GCHandle.Alloc(imgDataNew.PixelData, GCHandleType.Pinned); var bmp = new Bitmap(imgDataNew.Width, imgDataNew.Height, imgDataNew.Stride, PixelFormat.Format32bppArgb, arrayHandle.AddrOfPinnedObject()); // Flip before writing text on bmp bmp.RotateFlip(RotateFlipType.RotateNoneFlipY); Color color = Color.FromName(textColor); var font = new System.Drawing.Font(fontName, fontSize, FontStyle.Regular, GraphicsUnit.World); Graphics gfx = Graphics.FromImage(bmp); gfx.TextRenderingHint = TextRenderingHint.AntiAlias; gfx.DrawString(text, font, new SolidBrush(color), startPosX, startPosY); // Flip after writing text on bmp bmp.RotateFlip(RotateFlipType.RotateNoneFlipY); BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); int strideAbs = (bmpData.Stride < 0) ? -bmpData.Stride : bmpData.Stride; int bytes = (strideAbs)*bmp.Height; imgDataNew.PixelData = new byte[bytes]; imgDataNew.Height = bmpData.Height; imgDataNew.Width = bmpData.Width; imgDataNew.Stride = bmpData.Stride; Marshal.Copy(bmpData.Scan0, imgDataNew.PixelData, 0, bytes); bmp.UnlockBits(bmpData); return imgDataNew; }
/// <summary> /// Creates a new texture and binds it to the shader. /// </summary> /// <remarks> /// Method should be called after LoadImage method to process /// the BitmapData an make them available for the shader. /// </remarks> /// <param name="imgData">An ImageData struct, containing necessary information for the upload to the graphics card.</param> /// <returns> /// An <see cref="ITexture"/> that can be used for texturing in the shader. /// </returns> public ITexture CreateTexture(ImageData imgData) { return _rci.CreateTexture(imgData); }
/// <summary> /// Maps a specified text with on an image. /// </summary> /// <param name="imgData">The ImageData struct with the PixelData from the image.</param> /// <param name="fontName">The name of the text-font.</param> /// <param name="fontSize">The size of the text-font.</param> /// <param name="text">The text that sould be mapped on the iamge.</param> /// <param name="textColor">The color of the text-font.</param> /// <param name="startPosX">The horizontal start-position of the text on the image.</param> /// <param name="startPosY">The vertical start-position of the text on the image.</param> /// <returns>An ImageData struct containing all necessary information for further processing</returns> public ImageData TextOnImage(ImageData imgData, String fontName, float fontSize, String text, String textColor, float startPosX, float startPosY) { return _rci.TextOnImage(imgData, fontName, fontSize, text, textColor, startPosX, startPosY); }