[SOLVED]Won't render quad no matter what I do in OpenGL3+,what am I doing wrong?

Problem is happening again.

been following new set of tutorials I discovered by ThinMatrix on Youtube https://www.youtube.com/playlist?list=PLRIWtICgwaX0u7Rf9zkZhLoLuZVfUksDP and all indications are it will be a good set. However I am having a problem that keeps recurring. No errors, no squiggly lines, nothing. It just doesn’t display anything.

The focus of the tutorials is to use OpenGL3+ VAOs and VBOs and don’t use the fixed pipeline of older OpenGL versions. Exactly what I am trying to do, but I can’t make it work. I just get a blank red window. It works just fine if I remove Indexing, but fails when I use it again.


        ContextAttribs attribs = new ContextAttribs(3,2);
        attribs.withForwardCompatible(true);
        attribs.withProfileCore(true);
        
        try {
            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
            Display.create(new PixelFormat(), attribs);
            Display.setTitle("ThinMatrix Tutorials");
        } catch (LWJGLException ex){
            ex.printStackTrace();
        }
        
        glViewport(0,0, WIDTH, HEIGHT);
        glClearColor(1,0,0,1);
        
        // method of my own making that has been verified
        int shaderProgram = initializeShaders();        
        
        float[] vertices = {        // floats for Indexed Quad
            -0.5f, +0.5f, 0f, // 0
            -0.5f, -0.5f, 0f, // 1
            +0.5f, -0.5f, 0f, // 2
            +0.5f, +0.5f, 0f};// 3
        
//        float[] vertices = {      // floats for Non-Indexed Quad
//            -0.5f, +0.5f, 0f, // 0
//            -0.5f, -0.5f, 0f, // 1
//            +0.5f, +0.5f, 0f, // 3
//            +0.5f, +0.5f, 0f, // 3
//            -0.5f, -0.5f, 0f, // 1
//            +0.5f, -0.5f, 0f};// 2
        
        int[] indices = {
                    0,1,3,
                    3,1,2};
                    
        int vertexCount = indices.length;
        
        int vaoID = glGenVertexArrays();
        glBindVertexArray(vaoID);
        
        int vboID = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboID);
        FloatBuffer vBuffer = BufferUtils.createFloatBuffer(vertices.length);
        vBuffer.put(vertices);
        vBuffer.flip();
        glBufferData(GL_ARRAY_BUFFER, vBuffer, GL_STATIC_DRAW);
        glVertexAttribPointer(attributeNumber, 3, GL_FLOAT, false, 0, 0);
        glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind
        
        int iboID = glGenBuffers();
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID);
        IntBuffer iBuffer = BufferUtils.createIntBuffer(indices.length);
        iBuffer.put(indices);
        iBuffer.flip();
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, iBuffer, GL_STATIC_DRAW);
        //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // Unbind
        
        glBindVertexArray(0);

        while(!Display.isCloseRequested()){
            // Clear Screen        
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            glUseProgram(shaderProgram);
            
            glBindVertexArray(vaoID);
            glEnableVertexAttribArray(0);
            
            glDrawElements(GL_TRIANGLES, vertexCount, GL_UNSIGNED_INT, 0);
            //glDrawArrays(GL_TRIANGLES, 0, vertexCount);
            
            glDisableVertexAttribArray(0);
            glBindVertexArray(0);
            
            glUseProgram(0);
            
            Display.update();
            Display.sync(FPS_CAP);
        }
        
        Display.destroy();
        System.exit(0);

Vertex Shader:


#version 400 core

in vec3 position;

out vec3 color;

void main() {

    gl_Position = vec4(position, 1.0);
    color = vec3(position.x+0.5, 1.0, position.z+0.5);
}

Fragment Shader:


#version 400 core

in vec3 color;

out vec4 final_color;

void main() {
    final_color = vec4(color, 1.0);
}

I know this is after you requested to remove it, but if you just kept it there and then posted the solution to your own problem, you might save some people grief with their own issue.

Relevant xkcd

The xkcd basically says if your problem happens to be a rare one and you solve it, you should post the solution for the benefit of others. Even if you think it’s a super simple problem, give it the benefit of the doubt and post the answer anyway.

normally I would, but it was a simple typo.

UPDATE: Aparently the typo wasn’t my only problem - it keeps happening, so taking your advice, I re-posted it.

+1 for the comic link

Protip. Check for GL Errors. You should be calling glGetError() every now and again otherwise if there is an error it will completely pass you by.

My advice before you go looking for bugs is to stick this at the end of your initialization code and at the end of each loop. See what that turns up with.


int err = glGetError();
if(err != GL_NO_ERROR) {
    throw new RuntimeException("GL Error: " + err);
}

try without culling [icode]GL11.glDisable(GL11.GL_CULL_FACE)[/icode]

Added this to a method, placed it after every command. No errors.

Added this. No effect.

mhm. how about a [icode]glViewport()[/icode] ?

From the code you have shown, I can see two possible problems. First of all, your shaders use #version 400 core, which was added with OpenGL 4.0 and you are requesting an OpenGL 3.2 context. There’s no guarantee that the shaders will work with that version and I suggest that you either change to using #version 140 (GLSL for OpenGL 3.2) or request an OpenGL 4.0 context. The other possible problem is you call glEnableVertexAttribArray(0) when there’s no guarantee that your position attribute ended up at location 0. You have a variable attributeNumber that appears to hold the attribute’s location, so you should change the call to glEnableVertexAttribArray(attributeNumber).

Ok, checked both items:

  1. Shader version - changed it to 140 and it won’t compile - changed it back to 400 - no errors

Vertex Shader wasn't able to be compiled properly
src\Shaders\shader.vert
Exception in thread "main" java.lang.RuntimeException: 0(1) : er
	at Shaders.OpenGL_Shaders.initializeShaders(OpenGL_Shaders.java:73)
	at Application.Application.main(Application.java:50)
Java Result: 1

  1. the attributeNumber in question is passed in by a line that passes in the number 0 - I appologize, this is one drawback with copy and pasting from multiple functions in an attempt to get linear coherency.

I copy/pasted your code into my IDE, and everything worked exactly as expected. I can’t reproduce your problem; the quad shows up and is colored the way your code colors it. I also changed the [icode]#version 400 core[/icode] to [icode]#version 140[/icode] and the shaders still compiled successfully. I’m guessing the problem is either your shader loading code isn’t right, or when you pasted this version you accidentally fixed it.

Hmm, that’s odd… Maybe this part of the code isn’t the issue. Here’s my shader setup code:


        shaderProgram = glCreateProgram();
        vertShaderID = glCreateShader(GL_VERTEX_SHADER);
        fragShaderID = glCreateShader(GL_FRAGMENT_SHADER);

        vertShaderSource = setupShader(VERTEX_SHADER_LOCATION);
        fragShaderSource = setupShader(FRAGMENT_SHADER_LOCATION);
        
        
        // Compile vertex shader
        glShaderSource(vertShaderID, vertShaderSource);
        glCompileShader(vertShaderID);
        if(glGetShaderi(vertShaderID, GL_COMPILE_STATUS) == GL_FALSE){
            System.err.println("Vertex Shader wasn't able to be compiled properly");
            System.out.println(VERTEX_SHADER_LOCATION);
            throw new RuntimeException(glGetShaderInfoLog(vertShaderID, 10)); // 10 is "maximum length" and a guess
            //<editor-fold defaultstate="collapsed" desc="Dispose of Screen and Exit">
        }

        // Compile fragment shader
        glShaderSource(fragShaderID, fragShaderSource);
        glCompileShader(fragShaderID);
        if(glGetShaderi(fragShaderID, GL_COMPILE_STATUS) == GL_FALSE){
            System.err.println("Fragment Shader wasn't able to be compiled properly");
            System.out.println(FRAGMENT_SHADER_LOCATION);
            throw new RuntimeException(glGetShaderInfoLog(fragShaderID, 10)); // 10 is "maximum length" and a guess
        }
        
        // Link shaders to shader Program?
        glAttachShader(shaderProgram, vertShaderID);
        glAttachShader(shaderProgram, fragShaderID);
        // Link shader program to main program?
        
        
        glBindAttribLocation(shaderProgram, 0, "position");
//        glBindAttribLocation(shaderProgram, 1, "in_Colour");
//        glBindFragDataLocation(shaderProgram, 0, "fragColor");  // optional - shader is simple for now
        
        
        glLinkProgram(shaderProgram);
        glValidateProgram(shaderProgram);
        int status = glGetProgrami(shaderProgram, GL_LINK_STATUS);
        if (status != GL_TRUE) {
            throw new RuntimeException(glGetProgramInfoLog(shaderProgram,10));
        }


Only other thing I can think of is maybe something in how I have the IDE set up isn’t set right. I’ll clear it and re-set it up fresh, see if that makes a difference too.

I believe that you have to bind the index buffer before rendering the quad. e.g:

while(!Display.isCloseRequested()){
            // Clear Screen        
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            glUseProgram(shaderProgram);
            
            glBindVertexArray(vaoID);
            glEnableVertexAttribArray(0);
+           glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID);
            glDrawElements(GL_TRIANGLES, vertexCount, GL_UNSIGNED_INT, 0);
            //glDrawArrays(GL_TRIANGLES, 0, vertexCount);
            
            glDisableVertexAttribArray(0);
            glBindVertexArray(0);
            
            glUseProgram(0);
            
            Display.update();
            Display.sync(FPS_CAP);
        }

SOLVED IT!!!

When you drawArrays, your vertexCount = numberOfFloats/3 due to there being 3 floats per vertex (x,y,z)
When you drawElements, your vertexCount = numberOfInts due to there being 1 int per vertex offset

I still had the /3 on the end of vertexCount calculation.

Removed that, works like a charm!