我有一个用于测试的小应用程序,它正在做一些后期处理效果。我的目标是使用双PBO读取像素,然后将像素渲染到全屏纹理四边形,并在片段着色器中修改/添加我的效果。和这里类似。应用程序使用OSG,但我使用openGL调用来做全屏纹理。
我的问题是,当我使用glTexSubImage2D将像素复制到纹理每帧时,我得到一个无效值openGL错误。我已经用gDEBugger验证了这是生成错误的调用。第一次发生错误时,指针地址与所有后续帧中的地址不同。另外,通过设置glTexSubImage2d的宽度和高度参数为0,就不会产生错误。所以我真的不知道发生了什么。
相关代码如下:
MyCallbackClass()
{
pImgData = (unsigned char*) malloc(1024*512*3);
setupTextureAndShaders();
}
void setupTextureAndShaders()
{
glGenTextures(1, &screenTexture);
glBindTexture(GL_TEXTURE_2D, screenTexture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
memset(pImgData, 0, 1024*512*3);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, 1024, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, pImgData);
glBindTexture(GL_TEXTURE_2D, 0);
}
//callback called every frame
virtual void operator () (osg::RenderInfo& renderInfo) const
{
// buffer and context stuff unchanged from the sample program
if(!shaderInit) // only executed once
{
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
const GLchar* vs_source = shaderLoadFile("vert.glsl");
glShaderSource(vs, 1, &vs_source, NULL);
glCompileShader(vs);
checkShader(&vs);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar* fs_source = shaderLoadFile("frag.glsl");
glShaderSource(fs, 1, &fs_source, NULL);
glCompileShader(fs);
checkShader(&fs);
prog = glCreateProgram();
glAttachShader(prog, vs);
glAttachShader(prog, fs);
glLinkProgram(prog);
texLoc = glGetUniformLocation(prog, "screenTex");
shaderInit = true;
}
read(); // does the double pbo operations to read the image data into pImgData
glUseProgram(prog);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, screenTexture);
glUniform1i(texLoc, 0);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1024, 512, GL_RGB, GL_UNSIGNED_BYTE, pImgData);
glBegin(GL_QUADS);
glTexCoord2f(0, 1);
glVertex2f(-1,-1);
glTexCoord2f(0, 0);
glVertex2f(-1,1);
glTexCoord2f(1,0);
glVertex2f(1,1);
glTexCoord2f(1,1);
glVertex2f(1,-1);
glEnd();
glDisable(GL_TEXTURE_2D);
glMatrixMode (GL_MODELVIEW);
glPopMatrix();
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glMatrixMode (GL_MODELVIEW);
glUseProgram(0);
}
我查看了read()调用中的状态变化,但真正的问题是我的setupTexturesAndShaders()函数处于不同的上下文中。我相信这导致glTexSubImage2D被调用,而没有在相同的上下文中首先调用glTexImage2D,这将使任何大小的图像超出范围。谢谢你让我走上正确的道路