I'm using VBO to upload vertex array, colors and then texture coordinates! I did implement mouse pick with color coding technique. It works fine but when I render texture after ReadPixels, it returns wrong color! I don't know why this happens! Here is my render code:
GL.PushClientAttrib(ClientAttribMask.ClientVertexArrayBit);
GL.ShadeModel(ShadingModel.Flat);
Debug.Assert(_vbo.VertexBufferId != 0);
Debug.Assert(_vbo.ElementBufferId != 0);
// vertex colors
if (_vbo.ColorBufferId != 0)
{
GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo.ColorBufferId);
GL.ColorPointer(4, ColorPointerType.UnsignedByte, sizeof(int), IntPtr.Zero);
GL.EnableClientState(ArrayCap.ColorArray);
}
// vertex position
GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo.VertexBufferId);
GL.VertexPointer(3, VertexPointerType.Float, Vector3.SizeInBytes, IntPtr.Zero);
GL.EnableClientState(ArrayCap.VertexArray);
// primitives
GL.BindBuffer(BufferTarget.ElementArrayBuffer, _vbo.ElementBufferId);
GL.DrawElements(OpenTK.Graphics.OpenGL.PrimitiveType.Triangles, _vbo.NumIndices * 3, _vbo.Is32BitIndices ? DrawElementsType.UnsignedInt : DrawElementsType.UnsignedShort, IntPtr.Zero);
// Read pixel under mouse cursor
_selectedPolyIndex = uint.MaxValue;
Byte4 Pixel = new Byte4();
GL.ReadPixels(mouse_x, height - mouse_y, 1, 1, PixelFormat.Rgba, PixelType.UnsignedByte, ref Pixel);
_selectedPolyIndex = Pixel.ToUInt();
// Restore the state
GL.PopClientAttrib();
GL.PushClientAttrib(ClientAttribMask.ClientVertexArrayBit);
// normals
if (flags.HasFlag(RenderFlags.Shaded))
{
if (_vbo.NormalBufferId != 0)
{
GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo.NormalBufferId);
GL.NormalPointer(NormalPointerType.Float, Vector3.SizeInBytes, IntPtr.Zero);
GL.EnableClientState(ArrayCap.NormalArray);
}
}
// UV coordinates
if (flags.HasFlag(RenderFlags.Textured))
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); // this line must move out to line 160
if (_vbo.TexCoordBufferId != 0)
{
GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo.TexCoordBufferId);
GL.TexCoordPointer(2, TexCoordPointerType.Float, 8, IntPtr.Zero);
GL.EnableClientState(ArrayCap.TextureCoordArray);
}
}
// vertex position
GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo.VertexBufferId);
GL.VertexPointer(3, VertexPointerType.Float, Vector3.SizeInBytes, IntPtr.Zero);
GL.EnableClientState(ArrayCap.VertexArray);
// primitives
GL.BindBuffer(BufferTarget.ElementArrayBuffer, _vbo.ElementBufferId);
GL.DrawElements(OpenTK.Graphics.OpenGL.PrimitiveType.Triangles, _vbo.NumIndices * 3, _vbo.Is32BitIndices ? DrawElementsType.UnsignedInt : DrawElementsType.UnsignedShort, IntPtr.Zero);
// Restore the state
GL.PopClientAttrib();
When texture rendering is enabled OpenGL reads wrong pixel color under mouse cursor (I want vertex color but it reads texture color), even I call readpixels before texture rendering!
How can I fix this problem?