OpenGL 3程序黑屏
Posted
技术标签:
【中文标题】OpenGL 3程序黑屏【英文标题】:OpenGL 3 program blank screen 【发布时间】:2012-07-11 14:40:49 【问题描述】:我一直在将我的一些旧代码转换为 OpenGL 3,并且大部分代码都能正常工作。所有的 VAO、VBO 等都工作正常,正如 OpenGL 调试程序所验证的那样,我的着色器编译得很好,但屏幕上除了我的清晰颜色之外什么都没有出现。我认为我所有的矩阵都应该没问题,尽管我不确定投影矩阵是否应该将负 Z 坐标转换为正坐标;我尝试过的每个代码示例都会产生这样的矩阵。这是我的着色器:
顶点着色器:
#version 330
uniform mat4 modelview;
uniform mat4 projection;
in vec4 position;
in vec4 normal;
in vec4 texCoord;
void main()
gl_Position = projection * modelview * position;
片段着色器:
#version 330
out vec4 fragColor;
void main()
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
我的代码很复杂,所以我将发布一个经过修改的注释版本的 glIntercept 调用日志,其中应该包含所有相关调用:
//Create shaders
glCreateShader(GL_VERTEX_SHADER)=1
glShaderSource(1,1,vertShaderText,vertShaderTextLen)
glCompileShader(1)
glGetShaderiv(1,GL_COMPILE_STATUS,buf)
glCreateShader(GL_FRAGMENT_SHADER)=2
glShaderSource(2,1,fragShaderText,fragShaderTextLen)
glCompileShader(2)
glGetShaderiv(2,GL_COMPILE_STATUS,buf)
glCreateProgram()=3
glAttachShader(3,1)
glAttachShader(3,2)
glLinkProgram(3)
glGetProgramiv(3,GL_LINK_STATUS,buf)
//Find uniform/attribute locations
//Extra glUseProgram() calls are for safety.
glGetUniformLocation(3,"modelview")=0
glUseProgram(3)
glGetUniformLocation(3,"projection")=1
glUseProgram(3)
glGetAttribLocation(3,"position")=0
glUseProgram(3)
glGetAttribLocation(3,"normal")=-1
glUseProgram(3)
glGetAttribLocation(3,"texCoord")=-1
//Create a texture; probably irrelevant to this code
glGenTextures(1,texId)
glBindTexture(GL_TEXTURE_2D,1)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,256,256,0,GL_RGBA,GL_UNSIGNED_BYTE,texData)
glGenTextures(1,01DD275C)
//Create VAO, VBO
glGenVertexArrays(1,vaoId)
glBindVertexArray(1)
glGenBuffers(1,bufId)
glBindBuffer(GL_ARRAY_BUFFER,1)
//Redundant call; actual data set later
glBufferData(GL_ARRAY_BUFFER,0,00000000,GL_STATIC_DRAW)
glGenBuffers(1,indexBufId)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,4)
glBufferData(GL_ELEMENT_ARRAY_BUFFER,0,00000000,GL_STATIC_DRAW)
//Set up buffer data
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,4)
glBufferData(GL_ELEMENT_ARRAY_BUFFER,96,indexData,GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER,1)
glBufferData(GL_ARRAY_BUFFER, 48,01DC3D00,GL_STATIC_DRAW)
//Start drawing
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT)
glUseProgram(3)
glUseProgram(3)
//Modelview
glUniformMatrix4fv(modelviewLocation,1,false,[1.000000,0.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,-6.000000,1.000000])
glUseProgram(3)
//Projection
glUniformMatrix4fv(projectionLocation,1,false,[0.200000,0.000000,0.000000,0.000000,0.000000,0.150000,0.000000,0.000000,0.000000,0.000000,-1.000200,-1.000000,0.000000,0.000000,-0.200020,0.000000])
//Bind VAO, VBOs, and attribute positions
glBindVertexArray(1)
glBindBuffer(GL_ARRAY_BUFFER,1)
glVertexAttribPointer(positionAttributeLocation,3,GL_FLOAT,false,12,00000000)
glEnableVertexAttribArray(positionAttributeLocation)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,4)
glDrawElements(GL_TRIANGLES,24,GL_UNSIGNED_INT,00000000) GLSL=3 Textures[ (0,2) ]
glDisableVertexAttribArray(0)
我有一架飞机正对着镜头,但它根本没有出现。我所看到的只是清晰的颜色。剔除和深度测试已关闭。
【问题讨论】:
我又做了一些调试工作;使用 AMD 的 GPUPerfStudio,我可以验证 glDrawElements 是否正确发送了图元,但它们从未到达屏幕。我不确定是什么阻止了他们到达它;我已尽力验证所有矩阵,甚至尝试使用单位投影矩阵;似乎没有任何效果。 据我所见,片段着色器只会显示红色像素。这是你想要的吗? 【参考方案1】:在将制服传递给着色器时,我以下列方式使用了 glGetUniformLocation():
myUniformLocation = glGetUniformLocation(program, "myUniform")
glUniform1f(myUniformLocation, variable_value)
【讨论】:
这基本上是我使用的形式;当我编辑调用跟踪时,我已经停止用描述性变量名称替换跟踪的值。我可能应该解决这个问题。【参考方案2】:您的日志没有显示任何glEnableVertexAttribArray
呼叫。你忘了吗?
【讨论】:
第一帧好像没有调用,之后的每一帧都调用了。我已经解决了;仍然没有运气。【参考方案3】:事实证明,我的问题出在我的初始化代码中。我有未初始化的变量被传递给 glViewport,给了我一个 0x0 的视口。
【讨论】:
以上是关于OpenGL 3程序黑屏的主要内容,如果未能解决你的问题,请参考以下文章
我的OpenGL学习进阶之旅当你运行OpenGL程序的时候,程序并不绘制任何内容,并且白屏和黑屏的时候怎么排查?