Click here to Skip to main content
14,028,549 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I am trying to read the color of a pixel. but for some reason it doesn't work

HERE'S THE CODE :
ModelClassObject.UseShader(SelectionRender.GetShaderProgram());
			glUniformMatrix4fv(SelectionRender.GetUnifromModelID(), 1, GL_FALSE, glm::value_ptr(Model));
			glUniformMatrix4fv(SelectionRender.GetUnifromProjectionID(), 1, GL_FALSE, glm::value_ptr(Projection));
			glUniformMatrix4fv(SelectionRender.GetUniformView(), 1, GL_FALSE, glm::value_ptr(Camera.CalculateViewMatrix()));
			ModelClassObject.DrawTriangles();
			glFlush();
			glFinish();
			glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
			unsigned char data[3];
			glReadPixels(0, winHeight, winWidth, winHeight, GL_RGB, GL_UNSIGNED_BYTE, data);
			std::cout 
				<< "  R  "
				<< data[0]
				<< "  G  "
				<< data[1]
				<< "  B  "
				<< data[2] 
				<< std::endl;

OUTPUT :
data[0] = 0xcc
data[1] = 0xcc
data[2] = 0xcc

What I have tried:

I can't figure out what's wrong...
Posted
Updated 25-Feb-19 21:56pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

I think the second parameter is wrong. Here is the prototype of the function:
void glReadPixels( GLint x, 
       GLint y, 
       GLsizei width, 
       GLsizei height, 
       GLenum format, 
       GLenum type, 
       GLvoid* data );
The first two parameters are the location to begin reading. The second parameter, y, should probably be 0 also but certainly not winHeight because that would be an invalid value. The y parameter can be winHeight-1 at most.
   
Comments
Member 14131869 25-Feb-19 13:04pm
   
//LastX and LastY is the mouse position
glReadPixels(LastX, winHeight - 1 - LastY, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data);

but still it doesn't return valid values.
Rick York 25-Feb-19 13:41pm
   
Note that you are using GL_RGBA so you need four bytes to hold the data for one pixel with that argument.

I tried this in my little app I call GLplayground with this code :Hide   Copy Code
void COpenGLView::OnLButtonDown( UINT nFlags, CPoint point )
{
    trace( _T( "OnLButtonDown at %d,%d - size is %d,%d\n" ),
        point.x, point.y, m_WinSize.cx, m_WinSize.cy );
    int ycoord = m_WinSize.cy - 1 - point.y;
    UCHAR data[4] = { 0 };
    glReadPixels( point.x, ycoord, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data );
    traceDump( data, 4 );
    __super::OnLButtonDown( nFlags, point );
}
and it worked and displayed values that appear to be correct. I recommend trying something similar. FWIW, this is an MFC app on windows. There are other sample apps at this site you can try this with if you want to. This code should be usable in any mouse click handler function in another framework except you will probably need to replace the trace and traceDump calls with something usable for you. The traceDump function just display hex output so a printf with a format string of %02X for each byte of the pixel will work the same.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Thanks for the quick response,
I had the hexadecimal enabled. I disabled it and it worked
   
Comments
Richard Deeming 26-Feb-19 11:05am
   
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution and post a comment.

DO NOT post your comment in the "Add your solution here" box.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190419.4 | Last Updated 26 Feb 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100