A cube is not rendering

Hi all,
I’ve decided to pick some OpenGL that I haven’t touched for quite a lot.
I was just trying to draw a cube, but all I get is a black screen… I
know the question is extremely silly, but I can’t find where’s the issue here

I have some structs defined as:


struct Program{
    GLuint programID;
    GLuint vertexPositionAttribute;
    GLuint textureCoordAttribute;
    GLuint vertexNormalAttribute;
    GLuint pMatrixUniform;
    GLuint mvMatrixUniform;
    GLuint nMatrixUniform;
    GLuint samplerUniform;
    GLuint textureUniform;
    GLuint ambientColorUniform;

} typedef Program;

struct MeshBuffers{
    GLuint positionBuffer;
    GLuint indexBuffer;
    GLuint normalBuffer;
    GLuint textCoordBuffer;
    GLuint textureID;
    GLint vertexNumber;
    GLint indexNumber;
} typedef MeshBuffers;

Then I build up a cube using those file:

Cube.h



#include "structs.h"

static GLfloat cube_positions[] = {
            // Front face
            -1.0, -1.0,  1.0,
             1.0, -1.0,  1.0,
             1.0,  1.0,  1.0,
            -1.0,  1.0,  1.0,

            // Back face
            -1.0, -1.0, -1.0,
            -1.0,  1.0, -1.0,
             1.0,  1.0, -1.0,
             1.0, -1.0, -1.0,

            // Top face
            -1.0,  1.0, -1.0,
            -1.0,  1.0,  1.0,
             1.0,  1.0,  1.0,
             1.0,  1.0, -1.0,

            // Bottom face
            -1.0, -1.0, -1.0,
             1.0, -1.0, -1.0,
             1.0, -1.0,  1.0,
            -1.0, -1.0,  1.0,

            // Right face
             1.0, -1.0, -1.0,
             1.0,  1.0, -1.0,
             1.0,  1.0,  1.0,
             1.0, -1.0,  1.0,

            // Left face
            -1.0, -1.0, -1.0,
            -1.0, -1.0,  1.0,
            -1.0,  1.0,  1.0,
            -1.0,  1.0, -1.0,   
    };

static GLfloat cube_normals[] = {
            // Front face
             0.0,  0.0,  1.0,
             0.0,  0.0,  1.0,
             0.0,  0.0,  1.0,
             0.0,  0.0,  1.0,

            // Back face
             0.0,  0.0, -1.0,
             0.0,  0.0, -1.0,
             0.0,  0.0, -1.0,
             0.0,  0.0, -1.0,

            // Top face
             0.0,  1.0,  0.0,
             0.0,  1.0,  0.0,
             0.0,  1.0,  0.0,
             0.0,  1.0,  0.0,

            // Bottom face
             0.0, -1.0,  0.0,
             0.0, -1.0,  0.0,
             0.0, -1.0,  0.0,
             0.0, -1.0,  0.0,

            // Right face
             1.0,  0.0,  0.0,
             1.0,  0.0,  0.0,
             1.0,  0.0,  0.0,
             1.0,  0.0,  0.0,

            // Left face
            -1.0,  0.0,  0.0,
            -1.0,  0.0,  0.0,
            -1.0,  0.0,  0.0,
            -1.0,  0.0,  0.0
    };

static GLfloat cube_texture_uvs[] = {
            // Front face
            0.0, 0.0,
            1.0, 0.0,
            1.0, 1.0,
            0.0, 1.0,

            // Back face
            1.0, 0.0,
            1.0, 1.0,
            0.0, 1.0,
            0.0, 0.0,

            // Top face
            0.0, 1.0,
            0.0, 0.0,
            1.0, 0.0,
            1.0, 1.0,

            // Bottom face
            1.0, 1.0,
            0.0, 1.0,
            0.0, 0.0,
            1.0, 0.0,

            // Right face
            1.0, 0.0,
            1.0, 1.0,
            0.0, 1.0,
            0.0, 0.0,

            // Left face
            0.0, 0.0,
            1.0, 0.0,
            1.0, 1.0,
            0.0, 1.0

    };
    
static GLfloat cube_indices[]={
            0, 1, 2,      0, 2, 3,    // Front face
            4, 5, 6,      4, 6, 7,    // Back face
            8, 9, 10,     8, 10, 11,  // Top face
            12, 13, 14,   12, 14, 15, // Bottom face
            16, 17, 18,   16, 18, 19, // Right face
            20, 21, 22,   20, 22, 23  // Left face

};

MeshBuffers getCube();


Cube.cpp


...
struct MeshBuffers getCube(){

    MeshBuffers cubeBuffers;
    glGenBuffers(1,&(cubeBuffers.positionBuffer));
    glBindBuffer(GL_ARRAY_BUFFER,cubeBuffers.positionBuffer);
    glBufferData(GL_ARRAY_BUFFER,sizeof(cube_positions), cube_positions,GL_STATIC_DRAW);
    cubeBuffers.vertexNumber=24;


    glGenBuffers(1,&(cubeBuffers.normalBuffer));
    glBindBuffer(GL_ARRAY_BUFFER,cubeBuffers.normalBuffer);
    glBufferData(GL_ARRAY_BUFFER,sizeof(cube_normals), cube_normals,GL_STATIC_DRAW);

    glGenBuffers(1,&(cubeBuffers.textCoordBuffer));
    glBindBuffer(GL_ARRAY_BUFFER,cubeBuffers.textCoordBuffer);
    glBufferData(GL_ARRAY_BUFFER,sizeof(cube_texture_uvs), cube_texture_uvs,GL_STATIC_DRAW);

    glGenBuffers(1,&(cubeBuffers.indexBuffer));
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,cubeBuffers.indexBuffer);
    glBufferData(GL_ARRAY_BUFFER,sizeof(cube_indices), cube_indices,GL_STATIC_DRAW);
    cubeBuffers.indexNumber=36;

    return cubeBuffers;
}

...


And finally my main file:




int main( void ){
    MeshBuffers cube = getCube();
    while(1){
        drawLoop();
    }

}


void drawLoop(){
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glUseProgram(programID);
    if(glfwGetKey('W')==GLFW_PRESS)
        z-=0.05f;
    if(glfwGetKey('S')==GLFW_PRESS)
        z+=0.05f;



    pMatrix=glm::perspective(45.0f, 1024.0f/768.0f, 0.1f, 10000.0f);
    glUniform3f(program.ambientColorUniform, 0.8f,0.8f,0.8f);

    mvMatrix = glm::mat4(1.0);
    mvMatrix=glm::translate(mvMatrix,glm::vec3(0.0f,0.0f,z));

    for(int i=0; i<meshes.size(); ++i){
        if(useTexture){
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D,meshes.at(i).textureID);
            glUniform1i(program.samplerUniform,0);
            glBindBuffer(GL_ARRAY_BUFFER,meshes.at(i).textCoordBuffer);
            glVertexAttribPointer(
                program.textureCoordAttribute,
                2,
                GL_FLOAT,
                GL_FALSE,
                0,
                (void*)0
                );
        }
        
        glBindBuffer(GL_ARRAY_BUFFER,meshes.at(i).normalBuffer);
        glVertexAttribPointer(
            program.vertexNormalAttribute,
            3,
            GL_FLOAT,
            GL_FALSE,
            0,
            (void*)0
            );

        glBindBuffer(GL_ARRAY_BUFFER,meshes.at(i).positionBuffer);
        glVertexAttribPointer(
            program.vertexPositionAttribute,
            3,
            GL_FLOAT,
            GL_FALSE,
            0,
            (void*)0
            );


        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,meshes.at(i).indexBuffer);
        setMatrixUniform();
        glDrawElements(GL_TRIANGLE_STRIP,meshes.at(i).indexNumber, GL_UNSIGNED_SHORT,0);

    }
            glfwSwapBuffers();
            glFlush();

}

...

void initShaders(){
    programID = LoadShaders( "vs.vs", "fs.fs" );
    glUseProgram(programID);
    program.vertexPositionAttribute=glGetAttribLocation(programID, "aVertexPosition");
    glEnableVertexAttribArray(program.vertexPositionAttribute);
    program.vertexNormalAttribute=glGetAttribLocation(programID, "aVertexNormal");
    glEnableVertexAttribArray(program.vertexNormalAttribute);
    program.vertexNormalAttribute=1;
    glEnableVertexAttribArray(program.vertexNormalAttribute);

    program.textureCoordAttribute=glGetAttribLocation(programID, "aTextureCoord");
    glEnableVertexAttribArray(program.textureCoordAttribute);

    program.nMatrixUniform=glGetUniformLocation(programID, "uNMatrix");
    program.pMatrixUniform=glGetUniformLocation(programID, "uPMatrix");
    program.mvMatrixUniform=glGetUniformLocation(programID, "uMVMatrix");

    program.ambientColorUniform=glGetUniformLocation(programID, "uAmbientColor");
    program.samplerUniform=glGetUniformLocation(programID, "uSampler");
    program.textureUniform=glGetUniformLocation(programID, "uText");
}

.....


Where my shaders are:

vs.vs


   #version 330 core


    attribute vec3 aVertexPosition;
    attribute vec3 aVertexNormal;
    attribute vec2 aTextureCoord;

    uniform mat4 uMVMatrix;
    uniform mat4 uPMatrix;
    uniform mat3 uNMatrix;

    varying vec2 vTextureCoord;
    varying vec3 vTransformedNormal;
    varying vec4 vPosition;


    void main(void) {
        vPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
        gl_Position = uPMatrix * vPosition;
        vTextureCoord = aTextureCoord;
        vTransformedNormal = uNMatrix * aVertexNormal;
    }

fs.fs


#version 330 core

    precision mediump float;

    // These are unused for now.
    varying vec2 vTextureCoord;
    varying vec3 vTransformedNormal; 
    varying vec4 vPosition;
    uniform vec3 uAmbientColor; 
    uniform sampler2D uSampler;


    void main(void) {
        vec4 fragmentColor;
        fragmentColor = vec4(0.9, 0.4, 0.4, 1.0);
        gl_FragColor = vec4(fragmentColor.rgb* uAmbientColor, fragmentColor.a);
    }

I’m sorry to ask such a vague and stupid question, but I’m kind of clueless.
I’m also sorry that the code here is quite a lot, but it should be fast readable as it is pretty standard I guess

[QUOTE=cifacia;1257367]Hi all,
I’ve decided to pick some OpenGL that I haven’t touched for quite a lot.
I was just trying to draw a cube, but all I get is a black screen… I
know the question is extremely silly, but I can’t find where’s the issue here

I have some structs defined as:


struct Program{
    GLuint programID;
    GLuint vertexPositionAttribute;
    GLuint textureCoordAttribute;
    GLuint vertexNormalAttribute;
    GLuint pMatrixUniform;
    GLuint mvMatrixUniform;
    GLuint nMatrixUniform;
    GLuint samplerUniform;
    GLuint textureUniform;
    GLuint ambientColorUniform;

} typedef Program;

struct MeshBuffers{
    GLuint positionBuffer;
    GLuint indexBuffer;
    GLuint normalBuffer;
    GLuint textCoordBuffer;
    GLuint textureID;
    GLint vertexNumber;
    GLint indexNumber;
} typedef MeshBuffers;

Then I build up a cube using those file:

Cube.h



#include "structs.h"

static GLfloat cube_positions[] = {
            // Front face
            -1.0, -1.0,  1.0,
             1.0, -1.0,  1.0,
             1.0,  1.0,  1.0,
            -1.0,  1.0,  1.0,

            // Back face
            -1.0, -1.0, -1.0,
            -1.0,  1.0, -1.0,
             1.0,  1.0, -1.0,
             1.0, -1.0, -1.0,

            // Top face
            -1.0,  1.0, -1.0,
            -1.0,  1.0,  1.0,
             1.0,  1.0,  1.0,
             1.0,  1.0, -1.0,

            // Bottom face
            -1.0, -1.0, -1.0,
             1.0, -1.0, -1.0,
             1.0, -1.0,  1.0,
            -1.0, -1.0,  1.0,

            // Right face
             1.0, -1.0, -1.0,
             1.0,  1.0, -1.0,
             1.0,  1.0,  1.0,
             1.0, -1.0,  1.0,

            // Left face
            -1.0, -1.0, -1.0,
            -1.0, -1.0,  1.0,
            -1.0,  1.0,  1.0,
            -1.0,  1.0, -1.0,   
    };

static GLfloat cube_normals[] = {
            // Front face
             0.0,  0.0,  1.0,
             0.0,  0.0,  1.0,
             0.0,  0.0,  1.0,
             0.0,  0.0,  1.0,

            // Back face
             0.0,  0.0, -1.0,
             0.0,  0.0, -1.0,
             0.0,  0.0, -1.0,
             0.0,  0.0, -1.0,

            // Top face
             0.0,  1.0,  0.0,
             0.0,  1.0,  0.0,
             0.0,  1.0,  0.0,
             0.0,  1.0,  0.0,

            // Bottom face
             0.0, -1.0,  0.0,
             0.0, -1.0,  0.0,
             0.0, -1.0,  0.0,
             0.0, -1.0,  0.0,

            // Right face
             1.0,  0.0,  0.0,
             1.0,  0.0,  0.0,
             1.0,  0.0,  0.0,
             1.0,  0.0,  0.0,

            // Left face
            -1.0,  0.0,  0.0,
            -1.0,  0.0,  0.0,
            -1.0,  0.0,  0.0,
            -1.0,  0.0,  0.0
    };

static GLfloat cube_texture_uvs[] = {
            // Front face
            0.0, 0.0,
            1.0, 0.0,
            1.0, 1.0,
            0.0, 1.0,

            // Back face
            1.0, 0.0,
            1.0, 1.0,
            0.0, 1.0,
            0.0, 0.0,

            // Top face
            0.0, 1.0,
            0.0, 0.0,
            1.0, 0.0,
            1.0, 1.0,

            // Bottom face
            1.0, 1.0,
            0.0, 1.0,
            0.0, 0.0,
            1.0, 0.0,

            // Right face
            1.0, 0.0,
            1.0, 1.0,
            0.0, 1.0,
            0.0, 0.0,

            // Left face
            0.0, 0.0,
            1.0, 0.0,
            1.0, 1.0,
            0.0, 1.0

    };
    
static GLfloat cube_indices[]={
            0, 1, 2,      0, 2, 3,    // Front face
            4, 5, 6,      4, 6, 7,    // Back face
            8, 9, 10,     8, 10, 11,  // Top face
            12, 13, 14,   12, 14, 15, // Bottom face
            16, 17, 18,   16, 18, 19, // Right face
            20, 21, 22,   20, 22, 23  // Left face

};

MeshBuffers getCube();


Cube.cpp


...
struct MeshBuffers getCube(){

    MeshBuffers cubeBuffers;
    glGenBuffers(1,&(cubeBuffers.positionBuffer));
    glBindBuffer(GL_ARRAY_BUFFER,cubeBuffers.positionBuffer);
    glBufferData(GL_ARRAY_BUFFER,sizeof(cube_positions), cube_positions,GL_STATIC_DRAW);
    cubeBuffers.vertexNumber=24;


    glGenBuffers(1,&(cubeBuffers.normalBuffer));
    glBindBuffer(GL_ARRAY_BUFFER,cubeBuffers.normalBuffer);
    glBufferData(GL_ARRAY_BUFFER,sizeof(cube_normals), cube_normals,GL_STATIC_DRAW);

    glGenBuffers(1,&(cubeBuffers.textCoordBuffer));
    glBindBuffer(GL_ARRAY_BUFFER,cubeBuffers.textCoordBuffer);
    glBufferData(GL_ARRAY_BUFFER,sizeof(cube_texture_uvs), cube_texture_uvs,GL_STATIC_DRAW);

    glGenBuffers(1,&(cubeBuffers.indexBuffer));
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,cubeBuffers.indexBuffer);
    glBufferData(GL_ARRAY_BUFFER,sizeof(cube_indices), cube_indices,GL_STATIC_DRAW);
    cubeBuffers.indexNumber=36;

    return cubeBuffers;
}

...


And finally my main file:




int main( void ){
    MeshBuffers cube = getCube();
    while(1){
        drawLoop();
    }

}


void drawLoop(){
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glUseProgram(programID);
    if(glfwGetKey('W')==GLFW_PRESS)
        z-=0.05f;
    if(glfwGetKey('S')==GLFW_PRESS)
        z+=0.05f;



    pMatrix=glm::perspective(45.0f, 1024.0f/768.0f, 0.1f, 10000.0f);
    glUniform3f(program.ambientColorUniform, 0.8f,0.8f,0.8f);

    mvMatrix = glm::mat4(1.0);
    mvMatrix=glm::translate(mvMatrix,glm::vec3(0.0f,0.0f,z));

    for(int i=0; i<meshes.size(); ++i){
        if(useTexture){
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D,meshes.at(i).textureID);
            glUniform1i(program.samplerUniform,0);
            glBindBuffer(GL_ARRAY_BUFFER,meshes.at(i).textCoordBuffer);
            glVertexAttribPointer(
                program.textureCoordAttribute,
                2,
                GL_FLOAT,
                GL_FALSE,
                0,
                (void*)0
                );
        }
        
        glBindBuffer(GL_ARRAY_BUFFER,meshes.at(i).normalBuffer);
        glVertexAttribPointer(
            program.vertexNormalAttribute,
            3,
            GL_FLOAT,
            GL_FALSE,
            0,
            (void*)0
            );

        glBindBuffer(GL_ARRAY_BUFFER,meshes.at(i).positionBuffer);
        glVertexAttribPointer(
            program.vertexPositionAttribute,
            3,
            GL_FLOAT,
            GL_FALSE,
            0,
            (void*)0
            );


        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,meshes.at(i).indexBuffer);
        setMatrixUniform();
        glDrawElements(GL_TRIANGLE_STRIP,meshes.at(i).indexNumber, GL_UNSIGNED_SHORT,0);

    }
            glfwSwapBuffers();
            glFlush();

}

...

void initShaders(){
    programID = LoadShaders( "vs.vs", "fs.fs" );
    glUseProgram(programID);
    program.vertexPositionAttribute=glGetAttribLocation(programID, "aVertexPosition");
    glEnableVertexAttribArray(program.vertexPositionAttribute);
    program.vertexNormalAttribute=glGetAttribLocation(programID, "aVertexNormal");
    glEnableVertexAttribArray(program.vertexNormalAttribute);
    program.vertexNormalAttribute=1;
    glEnableVertexAttribArray(program.vertexNormalAttribute);

    program.textureCoordAttribute=glGetAttribLocation(programID, "aTextureCoord");
    glEnableVertexAttribArray(program.textureCoordAttribute);

    program.nMatrixUniform=glGetUniformLocation(programID, "uNMatrix");
    program.pMatrixUniform=glGetUniformLocation(programID, "uPMatrix");
    program.mvMatrixUniform=glGetUniformLocation(programID, "uMVMatrix");

    program.ambientColorUniform=glGetUniformLocation(programID, "uAmbientColor");
    program.samplerUniform=glGetUniformLocation(programID, "uSampler");
    program.textureUniform=glGetUniformLocation(programID, "uText");
}

.....


Where my shaders are:

vs.vs


   #version 330 core


    attribute vec3 aVertexPosition;
    attribute vec3 aVertexNormal;
    attribute vec2 aTextureCoord;

    uniform mat4 uMVMatrix;
    uniform mat4 uPMatrix;
    uniform mat3 uNMatrix;

    varying vec2 vTextureCoord;
    varying vec3 vTransformedNormal;
    varying vec4 vPosition;


    void main(void) {
        vPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
        gl_Position = uPMatrix * vPosition;
        vTextureCoord = aTextureCoord;
        vTransformedNormal = uNMatrix * aVertexNormal;
    }

fs.fs


#version 330 core

    precision mediump float;

    // These are unused for now.
    varying vec2 vTextureCoord;
    varying vec3 vTransformedNormal; 
    varying vec4 vPosition;
    uniform vec3 uAmbientColor; 
    uniform sampler2D uSampler;


    void main(void) {
        vec4 fragmentColor;
        fragmentColor = vec4(0.9, 0.4, 0.4, 1.0);
        gl_FragColor = vec4(fragmentColor.rgb* uAmbientColor, fragmentColor.a);
    }

I’m sorry to ask such a vague and stupid question, but I’m kind of clueless.
I’m also sorry that the code here is quite a lot, but it should be fast readable as it is pretty standard I guess[/QUOTE]

I’m not able to EDIT my post, but however I’m pretty sure now that the error is in the Cube/relative structures and not in the rendering part.

Can you give me a hint ? :frowning:

If you’re clueless about your own code, what makes you think others have a better idea of what your code is supposed to do? People aren’t here to debug your code. They may possibly answer specific questions about OpenGL, but it seems you don’t have any specific questions to ask.

I haven’t looked at your code, but it’s obvious that this IBO is initialized incorrectly:

[QUOTE=Jesse;1257419]If you’re clueless about your own code, what makes you think others have a better idea of what your code is supposed to do? People aren’t here to debug your code. They may possibly answer specific questions about OpenGL, but it seems you don’t have any specific questions to ask.

I haven’t looked at your code, but it’s obvious that this IBO is initialized incorrectly:[/QUOTE]

Well I’m clueless where the error is, not what the code does. I’m sorry for haven’t made myself clear. My question was (according to the second post) what’s wrong with my structures that lead to a black screen instead of the cube rendered ?
Anyway, I fixed that earlier and I’m sorry I haven’t post it here, but even with that change the thing is still not working.