使用OpenGL渲染QImage [英] Render QImage with OpenGL
问题描述
与我的其他问题相关,我认为更核心的问题是的,你如何使用OpenGL渲染一个 QImage
?
Related to my other question, I think the more central question would be, how you render a QImage
with OpenGL?
我认为代码看起来像这样,我不确定我还需要什么,除了 convertToGLFormat(img)
。
I think the code has to look something like this, but I'm not sure what else I need, besides maybe convertToGLFormat(img)
.
glEnable(GL_TEXTURE_2D);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glGenTextures(1, &offscreenBufferTexture);
glBindTexture(GL_TEXTURE_2D, offscreenBufferTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imgGL.width(),
imgGL.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE,
imgGL.bits());
glBegin(GL_QUADS);
glTexCoord2d(0,0); glVertex2d(0, 0);
glTexCoord2d(1,0); glVertex2d(windowSize.width(), 0);
glTexCoord2d(1,1); glVertex2d(windowSize.width(), windowSize.height());
glTexCoord2d(0,1); glVertex2d(0, windowSize.height());
glEnd();
glDisable(GL_TEXTURE_2D);
此外,我认为你可以使用 glDrawPixels code>(虽然你不应该),像
Also, I thought quick and dirty you could use glDrawPixels()
(although you shouldn't), like
glDrawPixels(imgGL.width(), imgGL.height(), GL_RGBA,
GL_UNSIGNED_BYTE, imgGL.bits());
但是无论哪种方式,我看不到图像,只是一个白色方形的大小。我缺少这里的东西。
But either way I can't see the image, just a white square with the size of it. I'm missing something here.
推荐答案
我设法绘制一个 QImage
使用OpenGL从图片源,但不是 SoOffscreenRenderer
,如在这里。
I managed to draw a QImage
with OpenGL from a picture source, but not from the SoOffscreenRenderer
as described here.
void init(){
loadTexture2("kitten.png", backgroundimage);
}
QImage loadTexture2(char *filename, GLuint &textureID){
glEnable(GL_TEXTURE_2D); // Enable texturing
glGenTextures(1, &textureID); // Obtain an id for the texture
glBindTexture(GL_TEXTURE_2D, textureID); // Set as the current texture
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
QImage im(filename);
QImage tex = QGLWidget::convertToGLFormat(im);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glDisable(GL_TEXTURE_2D);
return tex;
}
void renderSceneGL2(){
glClearColor(0.4f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, backgroundimage);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(-1, -1, -1);
glTexCoord2f(1,0); glVertex3f(1, -1, -1);
glTexCoord2f(1,1); glVertex3f(1, 1, -1);
glTexCoord2f(0,1); glVertex3f(-1, 1, -1);
glEnd();
glDisable(GL_TEXTURE_2D);
}
也 QGLWidget :: convertToGLFormat
不定期抛出随机访问冲突
错误。
Also QGLWidget::convertToGLFormat
throws random Access violation
errors from time to time.
这篇关于使用OpenGL渲染QImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!