The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
OpenGL 3 program shows only clear color
blm768


Joined: 22 May 2012
Posts: 12
I've been working on an SDL2/OpenGL 3 project in the D programming language, and I've run into a really nasty bug. All of my code seems to be set up properly and runs without error messages, but no primitives are displayed on the screen.

Here's my setup code:
Code:

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, I direct the bindings to load the OpenGL 3.2 symbols, which it does correctly.

Since my code is all nice and object-oriented, I won't post a full listing; it would take up way too much space. Instead, I'll post a commented version of the log from GLIntercept:
Code:

    //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)


Here are my shaders:

Vertex shader:
Code:

#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:
Code:

#version 330

out vec4 fragColor;

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


According to the OpenGL debugging software I used, the primitives are being processed, but they never show up on the screen. Something does seem to be strange with my projection matrix, but even hard-coding the gl_Position output to (0, 0, 0, 1), which should be right in the middle of the viewing frustum in NDC, and setting the mode to GL_POINTS does nothing.

I must have something wrong, but I just can't tell what. If someone can spot it, I'd really appreciate it.
blm768


Joined: 22 May 2012
Posts: 12
Figured it out; I was setting my viewport as 0 by 0. I knew I should have initialized those variables...