0
$\begingroup$

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?

$\endgroup$

1 Answer 1

1
$\begingroup$

For this kind of functionality, you shouldn't try to use the same framebuffer for the vertex colours and for the final image. Don't forget that GL is asynchronous, so alternating CPU reads and GPU writes to the same framebuffer seriously limit your achievable performance and make it much harder for you to get the concurrency right. (The OpenGL wiki has more about synchronization.)

Instead, you should create an FBO that you only use for your vertex colours render. Draw the vertex colours into that and use it for mouse picking. Only use the 'real' framebuffer (the one that gets drawn) for the final image.

$\endgroup$
1
  • $\begingroup$ My problem solved by disabling texture2d before drawing colored triangles. Also I did use GL.Finish() after reading pixels. $\endgroup$ Commented Sep 17, 2017 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.