OpenGL 3程序空白屏幕

时间:2012-07-11 14:40:50

标签: opengl opengl-3

我一直将我的一些旧代码转换为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)

我有一架直接面向相机的飞机,但它根本没有出现。我所看到的只是清晰的颜色。剔除和深度测试都已关闭。

3 个答案:

答案 0 :(得分:0)

将制服传递给着色器时,我使用glGetUniformLocation(),方法如下:

myUniformLocation = glGetUniformLocation(program, "myUniform")
glUniform1f(myUniformLocation, variable_value)

答案 1 :(得分:0)

您的日志未显示任何glEnableVertexAttribArray来电。你忘记了吗?

答案 2 :(得分:0)

事实证明,我的问题出在我的初始化代码中。我有未初始化的变量传递给glViewport,给我一个0x0视口。