Program compiles but there is no display

Hi Guys,

Sorry for the long post.

I have spent most of the week-end trying to figure out where I am going wrong. The program compiles without any problems, yet I just cannot get the cube to display.

I am still very new to openGL and am trying out transformations. Perhaps I have implemented it wrongly?

If anyone can make some suggestions, id be very grateful :slight_smile:


#include "ShapeGenerator.h"

//----------------------------------------------------------------------------
GLuint program;

void
init( void )
{
    glEnable(GL_DEPTH_TEST);

    // Create a vertex array object
    GLuint vao;
    // use glGenVertexArray to find an unused name for the buffer
    glGenVertexArrays( 1, &vao );
    // The first time the function glBindVertexArray is executed for a given name, the object is created
    glBindVertexArray( vao );

    shapeData shape = ShapeGenerator::makeCube();

    // Create and initialize a buffer object
    GLuint vertexBufferID;
    // we use glGenBuffers to give us an unused identifier for our buffer object that is put into the variable buffer.
    glGenBuffers( 1, &vertexBufferID );
    // The type GL_ARRAY_BUFFER indicates that the data in the buffer will be vertex attribute data rather than
    // one of the other storage types that we will encounter later.
    glBindBuffer( GL_ARRAY_BUFFER, vertexBufferID );

    // with glBufferData, we allocate sufficient memory on the GPU for our data and provide a pointer to the array holding the data
    glBufferData( GL_ARRAY_BUFFER, shape.vertexBufferSize(), shape.vertices, GL_STATIC_DRAW );

    // Load shaders and use the resulting shader program
    program = InitShader( "vshader.glsl", "fshader.glsl" );
    glUseProgram( program );

    // Initialize the vertex position attribute from the vertex shader
    // The function glGetAttribLocation returns the index of an attribute variable, such as
    // the vertex location attribute vPosition in our vertex shader.
    GLuint loc = glGetAttribLocation( program, "vPosition" );

    // this enables the vertex attributes that are in the shaders
    glEnableVertexAttribArray( loc );

    // describe the form of the data in the vertex array
    // In glVertexAttribPointer, the second and third parameters specify that the array points to a 4D array of type GL_FLOAT
    // The fourth parameter says that we do not want the data normalized to be the range (0.0, 1.0), whereas the fifth if 0, that the values in the array are contiguous/tightly packed
    // The last parameter is the address in the buffer where the data begin.
    glVertexAttribPointer( loc, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 8, BUFFER_OFFSET(0) );
    glEnableVertexAttribArray( 1 );
    glVertexAttribPointer( 1, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 8, (char*)(sizeof(float) * 4) );

    GLuint indexArrayBufferID;
    glGenBuffers(1, &indexArrayBufferID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexArrayBufferID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, shape.indexBufferSize(), shape.indices, GL_STATIC_DRAW);

    shape.cleanup();
}

//----------------------------------------------------------------------------

void
display( void )
{
    glClearColor(0.0f / 255.0f, 0.0f / 255.0f, 0.0f / 255.0f, 1.0f);
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );  // clear the window

    mat4 modelTransformMatrix = Translate(vec3(0.0, 0.0, -3.0));
    mat4 projectionMatrix = Perspective(60.0f, ((float)700/700), 0.1f, 10.0f);

    GLint modelTransformMatrixUniformLocation = glGetUniformLocation(program, "modelTransformMatrix");
    GLint projectionMatrixUniformLocation = glGetUniformLocation(program, "projectionTransformMatrix");

    glUniformMatrix4fv(modelTransformMatrixUniformLocation, 1, GL_FALSE, &modelTransformMatrix[0][0]);
    glUniformMatrix4fv(projectionMatrixUniformLocation, 1, GL_FALSE, &projectionMatrix[0][0]);

    // glDrawArrays(GL_QUADS, 0, 8);
    glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0 );    // draw the points
    glutSwapBuffers();
}

//----------------------------------------------------------------------------

void reshape (int w, int h)
{
    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
}

//----------------------------------------------------------------------------

void
keyboard( unsigned char key, int x, int y )
{

    switch ( key ) {

    case 033:
        exit( EXIT_SUCCESS );
        break;
    }
}

//----------------------------------------------------------------------------

void demo_menu(int id)
{
    switch(id)
    {
        case 1:
            exit(0);
            break;
    }
    glutPostRedisplay();
}

int
main( int argc, char **argv )
{
    int w = 700, h = 700;
    glutInit( &argc, argv );
    // glEnable(GL_DEPTH_TEST);
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE ); // use GLUT_DOUBLE to add in double buffering. Remember parameters are logically or'ed. GLUT_DEPTH is used for hidden surface removal.
    glutInitWindowSize( w, h );
    glutInitWindowPosition(100, 100);

    glutCreateWindow( "Assignment 01 - Student Number: 33816115" );
    glewExperimental = GL_TRUE;
    glewInit();

    init();

    glutDisplayFunc( display );
    glutKeyboardFunc( keyboard );
    glutReshapeFunc( reshape );

    glutCreateMenu(demo_menu);
    glutAddMenuEntry("quit", 1);
    glutAttachMenu(GLUT_RIGHT_BUTTON);

    glutMainLoop();
    return 0;
}

vshader


#version 330

in layout(location = 0) vec4 vPosition;
in layout(location = 1) vec4 vColor;
out vec4 color;
uniform mat4 modelTransformMatrix;
uniform mat4 projectionMatrix;

void
main()
{
    color = vColor;
    vec4 v = vPosition;
    vec4 newPosition = modelTransformMatrix * v;
    vec4 projectedPosition = projectionMatrix * newPosition;
    gl_Position = projectedPosition;
}

fshader


#version 330

in  vec4  color;
out vec4  fColor;

void
main()
{
    fColor = color;
}


#ifndef SHAPEDATA_H_INCLUDED
#define SHAPEDATA_H_INCLUDED
#include "Angel.h"

struct shapeData
{
    //shapeDate(): vertices(0), numVertices(0), indices(0), numIndices(0) {}

    vec4 *vertices;
    GLuint numVertices;
    GLushort* indices;
    GLuint numIndices;

    GLsizeiptr vertexBufferSize() const
    {
        return numVertices * sizeof(vec4);
    }

    GLsizeiptr indexBufferSize() const
    {
        return numIndices * sizeof(GLushort);
    }

    void cleanup()
    {
        delete [] vertices;
        delete [] indices;
        numVertices = numIndices = 0;
    }
};

#endif // SHAPEDATA_H_INCLUDED


#ifndef SHAPEGENERATOR_H
#define SHAPEGENERATOR_H
#include "ShapeData.h"

class ShapeGenerator
{
    public:
        static shapeData makeTriangle();
        static shapeData makeCube();
    protected:
    private:
};

#endif // SHAPEGENERATOR_H


#include "ShapeGenerator.h"

shapeData ShapeGenerator::makeTriangle()
{
    shapeData ret;

    vec4 thisTri[] = {
        vec4(  0.0,  1.0, 0.0, 1.0f ), // vertex 0
        vec4(  0.0,  0.0, 1.0, 1.0f ), // vertex 0 color
        vec4( -1.0, -1.0, 0.0, 1.0f ), // vertex 1
        vec4(  0.0,  0.0, 1.0, 1.0f ), // vertex 1 color
        vec4(  1.0, -1.0, 0.0, 1.0f ), // vertex 2
        vec4(  0.0,  0.0, 1.0, 1.0f ), // vertex 2 color0
    };

    ret.numVertices = sizeof(thisTri) / sizeof(*thisTri);
    ret.vertices = new vec4[ret.numVertices];
    memcpy(ret.vertices, thisTri, sizeof(thisTri));

    GLushort indices[] = {0, 1, 2};
    ret.numIndices = sizeof(indices) / sizeof(*indices);
    ret.indices = new GLushort[ret.numIndices];
    memcpy(ret.indices, indices, sizeof(indices));

    return ret;
}

shapeData ShapeGenerator::makeCube()
{
    shapeData ret;

    vec4 stackVerts[] = {
        vec4(-1.0f, +1.0f, +1.0f, 0.0), // 0
        vec4(+1.0f, +0.0f, +0.0f, 0.0), // Colour
        vec4(+1.0f, +1.0f, +1.0f, 0.0), // 1
        vec4(+0.0f, +1.0f, +0.0f, 0.0), // Colour
        vec4(+1.0f, +1.0f, -1.0f, 0.0), // 2
        vec4(+0.0f, +0.0f, +1.0f, 0.0), // Colour
        vec4(-1.0f, +1.0f, -1.0f, 0.0), // 3
        vec4(+1.0f, +1.0f, +1.0f, 0.0), // Colour
        vec4(-1.0f, +1.0f, -1.0f, 0.0), // 4
        vec4(+1.0f, +0.0f, +1.0f, 0.0), // Colour
        vec4(+1.0f, +1.0f, -1.0f, 0.0), // 5
        vec4(+0.0f, +0.5f, +0.2f, 0.0), // Colour
        vec4(+1.0f, -1.0f, -1.0f, 0.0), // 6
        vec4(+0.8f, +0.6f, +0.4f, 0.0), // Colour
        vec4(-1.0f, -1.0f, -1.0f, 0.0), // 7
        vec4(+0.3f, +1.0f, +0.5f, 0.0), // Colour
        vec4(+1.0f, +1.0f, -1.0f, 0.0), // 8
        vec4(+0.2f, +0.5f, +0.2f, 0.0), // Colour
        vec4(+1.0f, +1.0f, +1.0f, 0.0), // 9
        vec4(+0.9f, +0.3f, +0.7f, 0.0), // Colour
        vec4(+1.0f, -1.0f, +1.0f, 0.0), // 10
        vec4(+0.3f, +0.7f, +0.5f, 0.0), // Colour
        vec4(+1.0f, -1.0f, -1.0f, 0.0), // 11
        vec4(+0.5f, +0.7f, +0.5f, 0.0), // Colour
        vec4(-1.0f, +1.0f, +1.0f, 0.0), // 12
        vec4(+0.7f, +0.8f, +0.2f, 0.0), // Colour
        vec4(-1.0f, +1.0f, -1.0f, 0.0), // 13
        vec4(+0.5f, +0.7f, +0.3f, 0.0), // Colour
        vec4(-1.0f, -1.0f, -1.0f, 0.0), // 14
        vec4(+0.4f, +0.7f, +0.7f, 0.0), // Colour
        vec4(-1.0f, -1.0f, +1.0f, 0.0), // 15
        vec4(+0.2f, +0.5f, +1.0f, 0.0), // Colour
        vec4(+1.0f, +1.0f, +1.0f, 0.0), // 16
        vec4(+0.6f, +1.0f, +0.7f, 0.0), // Colour
        vec4(-1.0f, +1.0f, +1.0f, 0.0), // 17
        vec4(+0.6f, +0.4f, +0.8f, 0.0), // Colour
        vec4(-1.0f, -1.0f, +1.0f, 0.0), // 18
        vec4(+0.2f, +0.8f, +0.7f, 0.0), // Colour
        vec4(+1.0f, -1.0f, +1.0f, 0.0), // 19
        vec4(+0.2f, +0.7f, +1.0f, 0.0), // Colour
        vec4(+1.0f, -1.0f, -1.0f, 0.0), // 20
        vec4(+0.8f, +0.3f, +0.7f, 0.0), // Colour
        vec4(-1.0f, -1.0f, -1.0f, 0.0), // 21
        vec4(+0.8f, +0.9f, +0.5f, 0.0), // Colour
        vec4(-1.0f, -1.0f, +1.0f, 0.0), // 22
        vec4(+0.5f, +0.8f, +0.5f, 0.0), // Colour
        vec4(+1.0f, -1.0f, +1.0f, 0.0), // 23
        vec4(+0.9f, +1.0f, +0.2f, 0.0), // Colour
        };

        ret.numVertices = sizeof(stackVerts) / sizeof(*stackVerts);
        ret.vertices = new vec4[ret.numVertices];
        memcpy(ret.vertices, stackVerts, sizeof(stackVerts));

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

        ret.numIndices = sizeof(stackIndices) / sizeof(*stackIndices);
        ret.indices = new GLushort[ret.numIndices];
        memcpy(ret.indices, stackIndices, sizeof(stackIndices));
        return ret;
}