Help in readpixel function

Mar 31, 2016 at 9:26 AM
I am trying to create a SharpGL application that is able to select a triangle that is rendered onscreen. I am following the code written in "FBORenderContextProvider.cs". I could not quite find any documentation or code description on how it works.

Specifically, i am trying to use the readpixel function. But it seems like the code uses window based functionality.

Could anyone assist me? Thanks!
Mar 31, 2016 at 8:30 PM
Edited Mar 31, 2016 at 8:33 PM
I'm not 100% sure this is what you want, but I have used this with SharpGL for testing if I have mouse clicked close to a colour (can be change to on a colour).

First add an MouseDown event for the SharpGL window from the properties window of the SharpGLForm.
Then use MousePosition to find mouse location (C# Control.MousePosition Property)
And use Graphics.FromImage. to obtain pixel colour.
private void openGLControl_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
            if (IsPixelBlack(MousePosition))
            {
                 MessageBox.Show("black pixel");
            }
}


public bool IsPixelBlack(Point position)
{
            //Checks within 2 by 2 area (change to (1,1) for single pixel)
           using (Bitmap bitmap = new Bitmap(2, 2))
           {
               using (Graphics graphics = Graphics.FromImage(bitmap))
               {
                   try
                   {
                       graphics.CopyFromScreen(position, new Point(0, 0), new Size(2, 2));
                   }
                   catch
                   {
                       return true;
                   }
               }


               if (RGB__Not_Black(bitmap.GetPixel(0, 0)))
                   return false;
               if (RGB__Not_Black(bitmap.GetPixel(0, 1)))
                   return false;
               if (RGB__Not_Black(bitmap.GetPixel(1, 0)))
                   return false;
               if (RGB__Not_Black(bitmap.GetPixel(1, 1)))
                   return false;

              // return bitmap.GetPixel(0, 0);
           }

           return true;
}


private bool RGB__Not_Black(System.Drawing.Color c)
{
           if ((c.R == 0) && (c.G == 0) && (c.B == 0))
               return false; //Black
             else
                   return true; //Not black
}
Apr 1, 2016 at 4:31 AM
I'm actually trying to use shaders to generate fragment colors based on their primitive ID, writing to a 2D texture, afterwhich "readpixel" is used to get the data back using a mouse function.

To generate the buffers
        uint[] ids = new uint[1];
        gl.GenFramebuffersEXT(1, ids);
        frameBufferID = ids[0];
        gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, frameBufferID);

        gl.GenTextures(1, ids);
        colourRenderBufferID = ids[0];
        gl.BindTexture(OpenGL.GL_TEXTURE_2D, colourRenderBufferID);
        gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA32F, width, height, 0, OpenGL.GL_RGBA32F, OpenGL.GL_FLOAT, null);
        gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, colourRenderBufferID,  0);

        gl.ReadBuffer(OpenGL.GL_NONE); 
        gl.DrawBuffer(OpenGL.GL_COLOR_ATTACHMENT0_EXT); 

To read in the pixel value
        gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, frameBufferID);
        gl.ReadBuffer(OpenGL.GL_COLOR_ATTACHMENT0_EXT);


        byte[] pixelInfo = new byte[30]; // some random large space

        gl.ReadPixels(x, y, 1, 1, OpenGL.GL_RGB32F, OpenGL.GL_UNSIGNED_BYTE, pixelInfo);

        gl.ReadBuffer(OpenGL.GL_NONE);
        gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, 0);

The code works when I am using it on C++, but it fails to read in this implementation on SharpGL. I will be grateful if anyone can point me to a working code that uses this method to get the pixel information.
Jan 23 at 12:45 PM
What AORD is showing you is exactly what you need. He shows how you read from an image buffer using the Graphics From Image methods.

What you are showing in your "C++" code has nothing to do with your question except this line...

gl.ReadPixels(x, y, 1, 1, OpenGL.GL_RGB32F, OpenGL.GL_UNSIGNED_BYTE, pixelInfo);

And you didn't show the code to that method. So its hard to know what is going on to be able to help you without knowing what that method is doing. The problem is most likely in your ReadPixels method. C++ and C# do not always read the same way. So if all you did was copy and paste, it won't work.

So, bottom line, examine your ReadPixels method in your C++ closer and you'll see your answer. Without posting the code, there is no real way to answer this question.