StMark's picture

Now with textures

I have problems in displaying my production data as a display-list with textures, so I started
first with combining 2 OpenGL samples of OpenTK: The display lists sample and the texture mapping.
And it works. I'm still searching for the reason why in production it fails, I'll blog the reason
that nobody else will stumble over the same problem.

So first the OnLoad:

            GL.ClearColor(Color.MidnightBlue);
 
            GL.Enable(EnableCap.Texture2D);
 
            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
 
            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);
 
            int first_list = GL.GenLists(num_lists);
            lists[0] = first_list;
            GL.NewList(first_list, ListMode.Compile);
 
            GL.Begin(BeginMode.Quads);
 
            GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(-0.6f, -0.4f);
            GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(0.6f, -0.4f);
            GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(0.6f, 0.4f);
            GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(-0.6f, 0.4f);
 
            GL.End();
 
            GL.EndList();

and the OnRenderFrame

            GL.Clear(ClearBufferMask.ColorBufferBit);
 
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
 
            GL.CallLists(num_lists, ListNameType.Int, lists);

Comments

Comment viewing options

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

For the production environmont I switch between meshes with material or texture. For texture I have also light, but ambient is 1, whilst for material there is no ambient light (0). What is also important, that you switch to default material when you show a mesh with textures otherwise the last material will be shown. So I used a function to reset it by setting all values to 1.

 
       public static Color4 _colorDefaultAmbient = new Color4(1f, 1f, 1f, 1f); // OpenGL default ambient color
        public static Color4 _colorDefaultDiffuse = new Color4(1f, 1f, 1f, 1f); // OpenGL default diffuse color
        public static Color4 _colorDefaultSpecular = new Color4(1f, 1f, 1f, 1f);    // OpenGL default specular color
 
        private void SetDefaultMaterial()
        {
            GL.Material(
                MaterialFace.FrontAndBack,
                MaterialParameter.Ambient,
                _colorDefaultAmbient);
 
            GL.Material(
                MaterialFace.FrontAndBack,
                MaterialParameter.Diffuse,
                _colorDefaultDiffuse);
 
            GL.Material(
                MaterialFace.FrontAndBack,
                MaterialParameter.Specular,
                _colorDefaultSpecular);
        }