【发布时间】:2020-12-21 01:06:58
【问题描述】:
我正在使用 ImGUI 为图像创建颜色选择器 OpenGL 应用程序。我设法通过将图像加载到glTexImage2D 并使用ImGUI::Image() 来加载图像。
现在我想实现一个方法,它可以在鼠标左键单击的情况下确定像素的颜色。
这是我加载纹理,然后将其分配给帧缓冲区的方法:
bool LoadTextureFromFile(const char *filename, GLuint *out_texture, int *out_width, int *out_height,ImVec2 mousePosition ) {
// Reading the image into a GL_TEXTURE_2D
int image_width = 0;
int image_height = 0;
unsigned char *image_data = stbi_load(filename, &image_width, &image_height, NULL, 4);
if (image_data == NULL)
return false;
GLuint image_texture;
glGenTextures(1, &image_texture);
glBindTexture(GL_TEXTURE_2D, image_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_image_free(image_data);
glBindTexture(GL_TEXTURE_2D, 0);
*out_texture = image_texture;
*out_width = image_width;
*out_height = image_height;
// Assigning texture to Frame Buffer
unsigned int fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, image_texture, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, image_texture, 0);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE){
std::cout<< "Frame buffer is done."<< std::endl;
}
return true;
}
不幸的是,上面的代码导致了一个完全空白的屏幕。我想,在设置帧缓冲区时我错过了一些东西。
这是方法,我想使用鼠标坐标对帧缓冲区纹理进行采样:
void readPixelFromImage(ImVec2 mousePosition) {
unsigned char pixels[4];
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels(GLint(mousePosition.x), GLint(mousePosition.y), 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
std::cout << "r: " << static_cast<int>(pixels[0]) << '\n';
std::cout << "g: " << static_cast<int>(pixels[1]) << '\n';
std::cout << "b: " << static_cast<int>(pixels[2]) << '\n';
std::cout << "a: " << static_cast<int>(pixels[3]) << '\n' << std::endl;
}
感谢任何帮助!
【问题讨论】:
-
当心鼠标坐标指的是与 OpenGL 坐标不同的窗口坐标...如果
ys是y视图大小,您很可能应该反转y位置,然后使用 @987654329 @ ...如果您在窗口中有面板和其他东西,您也需要考虑这一点...您也可以直接从纹理中使用pick textels ...在图像渲染和从中选择glReadPixels也很重要同一个线程,...因为您很可能会在某些情况下选择您的图像当时可能是空白的。 -
见OpenGL 3D-raypicking with high poly meshes有
glReadPixel直接帧缓冲的例子(没有PBO/FBO的附件)直接选择纹理你可以使用glGetTexImage
标签: opengl framebuffer imgui