如何读取opengl中像素的颜色 [英] How do I read the color of a pixel in opengl
本文介绍了如何读取opengl中像素的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试读取像素的颜色。但由于某种原因它不起作用
这里的代码:
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;
输出:
data [0] = 0xcc
数据[1] = 0xcc
数据[2] = 0xcc
我尝试过: < br $> b $ b
我无法弄清楚出了什么问题...
OUTPUT :
data[0] = 0xcc
data[1] = 0xcc
data[2] = 0xcc
What I have tried:
I can't figure out what's wrong...
推荐答案
我认为第二个参数是错误的。这是函数的原型:
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 );
前两个参数是开始阅读的位置。第二个参数y也应该是0,但肯定不是winHeight,因为这将是一个无效的值。 y参数最多可以是winHeight-1。
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.
感谢您的快速响应,
我启用了十六进制。我禁用它并且它有效
Thanks for the quick response,
I had the hexadecimal enabled. I disabled it and it worked
这篇关于如何读取opengl中像素的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文