glReadPixel return 故障结果
glReadPixel return fault result
我想通过frame pick的方式在OpenGl上做3D-picking。这样我就需要用glReadPixel
来获取当前屏幕像素点的一些信息,所以我按照下面的方式测试,但是得到了错误的结果。
首先,我使用回调 glfwSetCursorPosCallback(window, mouse_callback)
和 mouse_callback(GLFWwindow* window, double xpos, double ypos)
获取当前鼠标位置(屏幕位置)和像素的颜色(当我更改鼠标位置时),然后使用 std::cout
打印它。
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
uint32_t color;
glm::vec4 c;
GLfloat stencil;
glReadPixels(xpos, ypos, 1, 1,GL_RGBA, GL_UNSIGNED_BYTE, &color);
c.r = (color & 0xFF) / 255.0F;
c.g = ((color >> 8) & 0xFF) / 255.0F;
c.b = ((color >> 16) & 0xFF) / 255.0F;
c.a = ((color >> 24) & 0xFF) / 255.0F;
std::cout << "color[0]" << c.r<< std::endl;
std::cout << "color[1]" << c.g << std::endl;
std::cout << "color[2]" << c.b << std::endl;
std::cout << "color[3]" << c.a << std::endl;
}
但问题是当我渲染下面的场景,并将鼠标放在这个场景的不同部分时,结果似乎是错误的。从左上区域到右下区域,RBG 应该是 (1.0,0.0,0.0) , (0.6,0.0,0.4) , (0.4,0.0,0.6) , (0.2,0.0,0.8) , (0.0 ,0.0,1.0).图片如下.
the return result is (0.4,0.0,0.6) but the right result shoud be (1.0,0.0,0.0)
the return result is (0.0,0.0,0.1) but the right result shoud be (0.4,0.0,0.6)
the return result is (0.8,0.0,0.2) but the right result shoud be (0.4,0.0,0.6)
我试过用简单的例子(画一个三角形)来测试glReadPixel
,结果好像是对的。我不知道为什么在使用我的场景时会出错。谁能给我一些建议?或者如何做frame pick picking?谢谢你的帮助!!
OpenGL 使用原点 (0, 0) 为 window 的 bottom-left 且 +Y 向上的坐标。读取时必须转换为OpenGL的坐标系,因为光标事件使用(0, 0)作为window的top-left,+Y向下
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
int width, height;
glfwGetWindowSize(window, &width, &height);
int ixpos = (int)xpos;
int iypos = height - 1 - (int)ypos;
uint32_t color;
glReadPixels(ixpos, iypos, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &color);
...
}
我想通过frame pick的方式在OpenGl上做3D-picking。这样我就需要用glReadPixel
来获取当前屏幕像素点的一些信息,所以我按照下面的方式测试,但是得到了错误的结果。
首先,我使用回调 glfwSetCursorPosCallback(window, mouse_callback)
和 mouse_callback(GLFWwindow* window, double xpos, double ypos)
获取当前鼠标位置(屏幕位置)和像素的颜色(当我更改鼠标位置时),然后使用 std::cout
打印它。
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
uint32_t color;
glm::vec4 c;
GLfloat stencil;
glReadPixels(xpos, ypos, 1, 1,GL_RGBA, GL_UNSIGNED_BYTE, &color);
c.r = (color & 0xFF) / 255.0F;
c.g = ((color >> 8) & 0xFF) / 255.0F;
c.b = ((color >> 16) & 0xFF) / 255.0F;
c.a = ((color >> 24) & 0xFF) / 255.0F;
std::cout << "color[0]" << c.r<< std::endl;
std::cout << "color[1]" << c.g << std::endl;
std::cout << "color[2]" << c.b << std::endl;
std::cout << "color[3]" << c.a << std::endl;
}
但问题是当我渲染下面的场景,并将鼠标放在这个场景的不同部分时,结果似乎是错误的。从左上区域到右下区域,RBG 应该是 (1.0,0.0,0.0) , (0.6,0.0,0.4) , (0.4,0.0,0.6) , (0.2,0.0,0.8) , (0.0 ,0.0,1.0).图片如下.
the return result is (0.4,0.0,0.6) but the right result shoud be (1.0,0.0,0.0)
the return result is (0.0,0.0,0.1) but the right result shoud be (0.4,0.0,0.6)
the return result is (0.8,0.0,0.2) but the right result shoud be (0.4,0.0,0.6)
我试过用简单的例子(画一个三角形)来测试glReadPixel
,结果好像是对的。我不知道为什么在使用我的场景时会出错。谁能给我一些建议?或者如何做frame pick picking?谢谢你的帮助!!
OpenGL 使用原点 (0, 0) 为 window 的 bottom-left 且 +Y 向上的坐标。读取时必须转换为OpenGL的坐标系,因为光标事件使用(0, 0)作为window的top-left,+Y向下
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
int width, height;
glfwGetWindowSize(window, &width, &height);
int ixpos = (int)xpos;
int iypos = height - 1 - (int)ypos;
uint32_t color;
glReadPixels(ixpos, iypos, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &color);
...
}