PDA

View Full Version : OpenGL 3 program blank screen



blm768
07-12-2012, 10:44 PM
I've been converting some of my old code to OpenGL 3, and I've gotten most of it working. All the VAOs, VBOs, etc. are working fine, as verified by an OpenGL debugging program, and my shaders compile just fine, but nothing appears on the screen other than my clear color. I think all my matrices should be fine, although I'm not sure if the projection matrix is supposed to convert negative Z-coordinates to positive ones; every code sample I've tried produces such a matrix. Here are my shaders:

Vertex shader:


#version 330

uniform mat4 modelview;
uniform mat4 projection;

in vec4 position;
in vec4 normal;
in vec4 texCoord;

void main() {
gl_Position = projection * modelview * position;
}

Fragment shader:


#version 330

out vec4 fragColor;

void main() {
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

My code is complex, so I'll just post a modified, commented version of the glIntercept call log, which should have all the relevant calls:


//Create shaders
glCreateShader(GL_VERTEX_SHADER)=1
glShaderSource(1,1,vertShaderText,vertShaderTextLe n)
glCompileShader(1)
glGetShaderiv(1,GL_COMPILE_STATUS,buf)
glCreateShader(GL_FRAGMENT_SHADER)=2
glShaderSource(2,1,fragShaderText,fragShaderTextLe n)
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_FILTE R,GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,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.000 000,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.150 000,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(positionAttributeLocatio n)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,4)
glDrawElements(GL_TRIANGLES,24,GL_UNSIGNED_INT,000 00000) GLSL=3 Textures[ (0,2) ]
glDisableVertexAttribArray(0)

I've got a plane facing directly into the camera, but it doesn't show up at all. All I see is the clear color. Culling and depth test are off.

Any ideas what could be wrong?

Dark Photon
07-13-2012, 05:10 AM
I've been converting some of my old code to OpenGL 3...but nothing appears on the screen other than my clear color. I think all my matrices should be fine, although I'm not sure if the projection matrix is supposed to convert negative Z-coordinates to positive ones
You're using a perspective projection matrix, so for points inside the view frustum it should (with the perspective divide added) map EYE-SPACE negative Z values (i.e. in front of the eye) between -near and -far to -1..1 NDC Z values. See picture here:

* http://www.songho.ca/opengl/gl_projectionmatrix.html

Try running EYE-SPACE (0,0,-(near+far)/2,1) through your projection matrix, do the perspective divide, and make sure you get something in -1..1 (NDC).

blm768
07-13-2012, 07:01 AM
You're using a perspective projection matrix, so for points inside the view frustum it should (with the perspective divide added) map EYE-SPACE negative Z values (i.e. in front of the eye) between -near and -far to -1..1 NDC Z values.

That's definitely part of the problem. However, when I try an orthographic projection by scaling the Z coordinate down by a factor of 12 (my plane is 6 units away), the NDC Z-coordinate of the plane is -0.5, but it still doesn't show up.

blm768
07-16-2012, 07:45 PM
Sorry for the bump, but I can't figure out how to edit my last post.

I'm wondering if the issue might be with my initialization code. Here's what I have for that (it's in the D programming language, so it's a little different from C++ but should be readable):


if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
throw new Error("Unable to initialize SDL:" ~ to!string(SDL_GetError()));
}

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, glMajorVersion);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, glMinorVersion);

//Make sure we get an accelerated renderer.
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

window = SDL_CreateWindow(null, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
width, height, SDL_WINDOW_OPENGL);
if(!window) {
throw new Error("Unable to create SDL window: " ~ to!string(SDL_GetError()));
}

context = SDL_GL_CreateContext(window);
if(!context) {
throw new Error("Unable to create OpenGL " ~ to!string(glMajorVersion) ~
"." ~ to!string(glMinorVersion) ~ " or higher context. Please try updating your graphics drivers.");
}

glClearColor(0.0, 1.0, 0.0, 1.0);
glClearDepth(1.0);

//For testing
glDisable(GL_DEPTH_TEST);

glViewport(0, 0, width, height);


After that point, I instruct the bindings to load the OpenGL 3.2 symbols. The symbols appear to load just fine.
Might I be missing something in my setup?