<p>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 <em>asynchronous</em>, 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 <a href="https://www.khronos.org/opengl/wiki/Synchronization" rel="nofollow noreferrer">OpenGL wiki</a> has more about synchronization.)</p>

<p>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.</p>

CC BY-SA 3.0
2017-09-17T10:21:43.073Z
My problem solved by disabling texture2d before drawing colored triangles. Also I did use GL.Finish() after reading pixels.
Younes Jafari
CC BY-SA 3.0
2017-09-17T09:46:18.060Z
2017-09-17T09:46:18.060Z
Dan Hulme
<p>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:</p>

<pre><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();
</code></pre>

<p>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!</p>

<p>How can I fix this problem?</p>

<opengl><rendering><vertex-buffer-object>
How to read vertex color before rendering texture?
Younes Jafari
# How to read vertex color before rendering texture?

(Asked by: username_0 on 2017-09-13)

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?

**Tags:** opengl, rendering, vertex-buffer-object


---

## 1 answers

### Answer by username_1 on 2017-09-17. Score: 1

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](https://www.khronos.org/opengl/wiki/Synchronization) 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.

> **Comments:**
> **username_0**: My problem solved by disabling texture2d before drawing colored triangles. Also I did use GL.Finish() after reading pixels.  

---

