Frank Lioty's picture

Texture mapping a .png into a rectangle and paint geometries

Hi guy,
I'm really new with OpenGL and I have some problems with texture mapping. I need to paint a .png (with transparency) and some lines and squares. I tried with TexLib but it render (correctly) just the texture with no geometries. So I'm trying to myself. This is my code:

    private void Form1_Load(object sender, EventArgs e) {
      bitmap = new Bitmap(@"c:\tiles\bitmask\pap.png");
      GL.Enable(EnableCap.Texture2D);
      GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
      //naming e binding
      GL.GenTextures(1, out texture);
      GL.BindTexture(TextureTarget.Texture2D, texture);
      BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
      GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
          OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
      bitmap.UnlockBits(data);
 
      GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
      GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
      GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
                OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
 
    }

here the paint one:

    private void editor_Paint(object sender, PaintEventArgs e) {
      if (!loaded) return;
      GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
      GL.MatrixMode(MatrixMode.Modelview);
      GL.LoadIdentity();
 
      //hotspots
      for (int i = 0; i < arrays.Length; i++ ) {
        for (int j = 0; j < arrays[i].Count; j++) {
          //bitmask
          GL.BindTexture(TextureTarget.Texture2D, texture);
          GL.Begin(BeginMode.Quads);
          GL.TexCoord2(0, 0); GL.Vertex2(arrays[i][j].X, arrays[i][j].Y);
          GL.TexCoord2(1, 0); GL.Vertex2(arrays[i][j].X + arrays[i][j].Width, arrays[i][j].Y);
          GL.TexCoord2(1, 1); GL.Vertex2(arrays[i][j].X + arrays[i][j].Width, arrays[i][j].Y + arrays[i][j].Height);
          GL.TexCoord2(0, 1); GL.Vertex2(arrays[i][j].X, arrays[i][j].Y + arrays[i][j].Height);
          GL.End();
          //handler
          GL.Color3(colors[i]);
          GL.Begin(BeginMode.Quads);
          GL.Vertex2(arrays[i][j].Center.X - 4, arrays[i][j].Center.Y - 4);
          GL.Vertex2(arrays[i][j].Center.X + 4, arrays[i][j].Center.Y - 4);
          GL.Vertex2(arrays[i][j].Center.X + 4, arrays[i][j].Center.Y + 4);
          GL.Vertex2(arrays[i][j].Center.X - 4, arrays[i][j].Center.Y + 4);
          GL.End();
        }
      }

but it paints a black box (the .png) and all the geometries are not in their color but also in black. Any suggest?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mOfl's picture

The geometry is drawn, but in solid black color? Maybe try calling GL.Enable(EnableCap.Texture2D) somewhere at the beginning.

Frank Lioty's picture

it's there, the second instruction. Damn I'm going crazy!

Frank Lioty's picture

if I try to only paint my .png it works (with no transparency). But if I try to draw a red line, my .png become red, his content is visible but red-tinted:

In order:

- my .png
- a red filled square
- a white not-filled square