HermanssoN's picture

Vertex Buffer Object and texture


I made a small program that renders a 2D quad with a texture.
I did this using glBegin() and glEnd();

Now I decided to use a VBO, I never used VBO's and decided it was time to learn.

To start with I implemented the my VBO just like the sample program that comes with the SDK, eg;

public void DrawQuad2D(ref Texture texture)
            GL.BindBuffer(BufferTarget.ArrayBuffer, m_quad.VBO);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, m_quad.IBO);
            GL.VertexPointer(3, VertexPointerType.Float, BlittableValueType.StrideOf(m_quad.Vertices), new IntPtr(0));
            GL.ColorPointer(4, ColorPointerType.UnsignedByte, BlittableValueType.StrideOf(m_quad.Vertices), new IntPtr(12));
            GL.DrawElements(BeginMode.Triangles, m_quad.IndexCount, DrawElementsType.UnsignedShort, IntPtr.Zero);

I started with out using the texure, just like the sample. But there was a problem. The color was wrong, turned to grey.
I still send the texture to the function, and that caused the problem.

When I load the texture I use this:

 Bitmap bitmap = new Bitmap(file);
            OpenTK.Graphics.OpenGL.GL.GenTextures(1, out TextureIndex);
            OpenTK.Graphics.OpenGL.GL.BindTexture(OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, TextureIndex);
            Width = bitmap.Width;
            Height = bitmap.Height;
            BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
            ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            OpenTK.Graphics.OpenGL.GL.TexImage2D(OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, 0, OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgba, data.Width, data.Height, 0,
            OpenTK.Graphics.OpenGL.PixelFormat.Bgra, OpenTK.Graphics.OpenGL.PixelType.UnsignedByte, data.Scan0);
            OpenTK.Graphics.OpenGL.GL.TexParameter(OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, OpenTK.Graphics.OpenGL.TextureParameterName.TextureMinFilter, (int)OpenTK.Graphics.OpenGL.TextureMinFilter.Linear);
            OpenTK.Graphics.OpenGL.GL.TexParameter(OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, OpenTK.Graphics.OpenGL.TextureParameterName.TextureMagFilter, (int)OpenTK.Graphics.TextureMagFilter.Linear);

this line caused the problem:
OpenTK.Graphics.OpenGL.GL.TexParameter(OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, OpenTK.Graphics.OpenGL.TextureParameterName.TextureMinFilter, (int)OpenTK.Graphics.OpenGL.TextureMinFilter.Linear);
Comment this and the rendering is correct,
My question is why???


Comment viewing options

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

The most common issue is using LinearMipmapNearest or LinearMipmapLinear without generating a complete mipmap chain (e.g. though GL.GenerateMipmap()). However, you are specifying a Linear filter which doesn't need a mipmap chain, so this shouldn't be an issue.

Try calling GL.GetError() right after that line to see if any OpenGL errors appear. (You can also compile a debug version of OpenTK.dll, which will report any such errors immediately - very useful for debugging!)

Finally, it is more efficient to specify all TexParameters before using GL.TexImage2D. Some drivers may destroy and recreate the texture when you change specific parameters.

HermanssoN's picture

Thank you for your input.

Gl.GetError(); returns no errors.

The thing that I don't understand is why the texture stuff interferes.
Sure I create the texture, but I don't use it??

Do i have to unbind the texture after i created it or something?

the Fiddler's picture

If you have texturing enabled, you won't be able to use vertex colors. Try calling GL.Disable(EnableCap.Texture2d) before drawing your VBO.

HermanssoN's picture

Ahh, that explains a lot.
Thank you very much :)