Romich41's picture

Сolor of the pixel

Hi, With bibltoteki OpenTK draw a map of the city. I need to know the color of the pixel under the cursor (RGB) and displayed in Label each channel of RGB?


Comments

Comment viewing options

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

I think the Picking Example supplied with OpenTK does exactly what you need.
Specifically, you want the Byte4 struct and the GL.ReadPixels() call on line 255.

Romich41's picture

? - I do not know what to write and how to allocate RGB
Color foncolor = GL.ReadPixels(e.X, e.Y,glControl1.Width,glControl1.Height, ? , ?);

Hortus Longus's picture

Have a look at the function reference and you will see:

Parameters:
    	x 	Specify the window coordinates of the first pixel that is read from the frame buffer. This location is the lower left corner of a rectangular block of pixels.
    	width 	Specify the dimensions of the pixel rectangle. width and height of one correspond to a single pixel.
    	format 	Specifies the format of the pixel data. The following symbolic values are accepted: GL_COLOR_INDEX, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT, GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_BGR, GL_RGBA, GL_BGRA, GL_LUMINANCE, and GL_LUMINANCE_ALPHA.
    	type 	Specifies the data type of the pixel data. Must be one of GL_UNSIGNED_BYTE, GL_BYTE, GL_BITMAP, GL_UNSIGNED_SHORT, GL_SHORT, GL_UNSIGNED_INT, GL_INT, GL_FLOAT, GL_UNSIGNED_BYTE_3_3_2, GL_UNSIGNED_BYTE_2_3_3_REV, GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_5_6_5_REV, GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_UNSIGNED_SHORT_5_5_5_1, GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_UNSIGNED_INT_8_8_8_8, GL_UNSIGNED_INT_8_8_8_8_REV, GL_UNSIGNED_INT_10_10_10_2, or GL_UNSIGNED_INT_2_10_10_10_REV.
    	data 	Returns the pixel data.
the Fiddler's picture

To read a single pixel:

[StructLayout(LayoutKind.Sequential, Pack=1)]
struct Rgba
{
    public byte R, G, B, A;
}
 
Rgba pixel = new Rgba();
GL.ReadPixels(e.X, e.Y, 1, 1,
    OpenTK.Graphics.OpenGL.PixelFormat.Rgba, // or Bgra, needs testing
    OpenTK.Graphics.OpenGL.PixelType.Byte, 
    ref pixel);

If you need to read more than a single pixel, allocate an array large enough to hold your pixels. If you need to read pixels in a different format (e.g. float instead of byte), change the definition of the Rgba struct and specify the correct PixelType.