1

So I've been building a pretty simple piece of code that renders .OBJ files and lets the user look around using VBOs and the most simple GLSL shaders immaginable, but when run the window is just a static image (looks like the object seen from 0,0,0). Without the shader program it still renders normally, just without color. Any idea what I colud be doing wrong?

VBO & shader stuff:

//Create buffer for vertex data (x,y,z)
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vector3f), &vertices[0], GL_STATIC_DRAW);
//Create buffer for color data (r,g,b)
glGenBuffers(1, &colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, temp.size() * sizeof(vector3f), &temp[0], GL_STATIC_DRAW);

//Create and compile shaders
GLuint vertShader = 0, fragShader = 0, program = 0;
createShader("vertex.glsl", GL_VERTEX_SHADER, vertShader);
createShader("fragment.glsl", GL_FRAGMENT_SHADER, fragShader);
program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glBindAttribLocation(program, vertexAttribIndex, "vertex_position");
glBindAttribLocation(program, colorAttribIndex, "vertex_colour");
glLinkProgram(program);
printShaderInfoLog(vertShader);
printProgramInfoLog(program);

//Set attribute pointers for GLSL
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(vertexAttribIndex, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(vertexAttribIndex);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glVertexAttribPointer(colorAttribIndex, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(colorAttribIndex);

Render loop:

while (!glfwWindowShouldClose(window)) {
    glClearColor(0.0F, 0.0F, 0.0F, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glUseProgram(program);
    glViewport(0, 0, width, height);
    gluPerspective(70.0, width / height, 1.0, 30.0);
    glMatrixMode(GL_MODELVIEW);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glDrawArrays(GL_TRIANGLES, 0, vertBufferSize);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    glfwSwapBuffers(window);
    glfwPollEvents();
}

Vertex shader:

#version 420

layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec3 vertex_colour;

out vec3 colour;
out vec4 gl_Position;

void main () {
  colour = vertex_colour;
  gl_Position = vec4 (vertex_position, 1.0);
}

Fragment shader:

#version 420

in vec3 colour;
out vec4 colorOut;

void main () {
  colorOut = vec4(colour, 1.0);
}

Sorry if I've missed out anything obvious, I've been fighting this for hours.

  • 1
    What exactly is the problem? That you can't look around? – user253751 May 26 '16 at 23:35
  • It would be useful if you can post some screenshots as well – notadam May 26 '16 at 23:38
  • What's your OpenGL context version ? If you use an OpenGL 4.20 core profile, you need to use VAO (Vertex Array Object) for your VBOs. Also, I don't see vertexAttribIndex and colorAttribIndex are initialize. And the call to glBindAttribLocation seems redundant with the manual layout binding in the shader itself. – 246tNt May 27 '16 at 06:01
  • Yes, the problem is that it doesn't seem to render past the first frame (so you can't look around). – CheeseMonkey May 27 '16 at 07:43
  • I had a VAO as well but it didn't seem to make any difference whether It was included or commented out. I'll try adding vertexAttribIndex + colorAttribIndex, if that fails then I'll post screenshots. thx for the help so far – CheeseMonkey May 27 '16 at 07:46
  • sorry, moment of confusion there, vertexAttribIndex and colorAttribIndex are initialised at the top of the file as 0 and 1 respectively (both integers) – CheeseMonkey May 27 '16 at 07:49
  • What is in the GLSL compilation and linking logs? What gfx card/driver/OS? see [complete GL+GLSL+VAO/VBO C++ example](http://stackoverflow.com/a/31913542/2521214) so you got something working for comparison – Spektre May 27 '16 at 08:09
  • Also, you don't need glEnableClientState / glDisableClientState those are deprecated and useless for your case. And you have operations on the fixed pipeline matrices ( glMatrixMode & such ) but you use vertex shaders, all of this is not taken into account at all. – 246tNt May 27 '16 at 12:10

0 Answers0