0

I'm trying to render in opengl using shaders, the background colour generates and no error messages appear but the object I'm trying to render doesn't show. I've been trying all day to solve this but have come to nothing. I'm reading vertices and indices in from a .txt file but that doesn't seem to be the problem as all the numbers are exactly what they should be. Here is my init:

void init() {
    readFile("pendulum.txt", pVert, pIndices, pCols);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable( GL_DEPTH_TEST );

    glClearColor(0.5,0.5,0.5,1.0);

    program = InitShader( "aVertexShader64.glsl", "aFragShader63.glsl" );
    glUseProgram( program );

    modelView = glGetUniformLocation( program, "model_view" );
    projection = glGetUniformLocation( program, "projection" );


    glGenVertexArrays( 1, &vao );
    glBindVertexArray( vao );

    // Create and initialize two buffer objects
    glGenBuffers( 2, buffers);

    //one buffer for the vertexPositions and colours
    glBindBuffer( GL_ARRAY_BUFFER, buffers[0]);
    glBufferData( GL_ARRAY_BUFFER, numVertexPositionBytes + numVertexColourBytes,NULL, GL_STATIC_DRAW );
    glBufferSubData( GL_ARRAY_BUFFER, 0, numVertexPositionBytes, vertexPositions );
    glBufferSubData( GL_ARRAY_BUFFER, numVertexPositionBytes, numVertexColourBytes, vertexColours);

    //one buffer for the indices
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
    glBufferData( GL_ELEMENT_ARRAY_BUFFER, numVertexIndexBytes,vertexIndices, GL_STATIC_DRAW );

    // set up vertex arrays
    GLuint vPosition = glGetAttribLocation( program, "vPosition" );
    glEnableVertexAttribArray( vPosition );
    glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );

    GLuint vColor = glGetAttribLocation( program, "vColor" );
    glEnableVertexAttribArray( vColor );
    glVertexAttribPointer( vColor, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVertexPositionBytes) );
}

Main:

int main( int argc, char **argv ){
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow( "pendulum" );

    glewInit();
    init();
    //initialiseBoxCoordinates();
    //glutTimerFunc(1,timer,0);
    glutReshapeFunc(reshape);
    glutDisplayFunc( display );
    glutKeyboardFunc( keyboard );
    glutIdleFunc( idle );

    glutMainLoop();

    return 0;
}

And Display:

void display( void ) {
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    pStack.pushMatrix();
    pStack.loadIdentity();
    pStack.rotated(theta,0.0,1.0,0.0);
    pStack.translated(0.0,0.0,-1.0);

    for(int i = 0; i<NumPVerts; i++){
        pStack.transformd(&pVert[i*4],&pVertActual[i*4]);
    }

    glBindVertexArray(lineVao);
    glDrawArrays(GL_LINES,0, lineVertSize/3);

    glBindVertexArray(pVao);

    glBindBuffer(GL_ARRAY_BUFFER, pBuffers[0]);
    glBufferSubData(GL_ARRAY_BUFFER, 0, numPVertexBytes, pVertActual);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pBuffers[1]);

    glDrawElements(GL_TRIANGLES, NumPIndices, GL_UNSIGNED_BYTE, 0);

    pStack.popMatrix();

    //switch buffers
    glutSwapBuffers();

}

After the matrix transformations the vertices are all where they should be and I have used the same matrix class before with no problems so I don't think it's that. I think it has something to do with the shaders but I'm still new to opengl and am really not sure. Here are the vertex shader:

in  vec4 vPosition;
in  vec4 vColor;
out vec4 color;

uniform mat4 model_view;
uniform mat4 projection;

void main()  {
    gl_Position = projection*model_view*vPosition/vPosition.w;
    color = vColor;
} 

And the fragment shader:

in  vec4 color;
out vec4 fColor;

void main() {
    fColor = color;
} 
AivanF.
  • 1,134
  • 2
  • 23
  • 52
user2755996
  • 115
  • 2
  • 8

1 Answers1

0

As far as i can see, your shaders are kinda fine. But you don't use #version tag there, which could lead to some problems, like shader compilation errors.

I don't understand why you have the division vPosition/vPosition.w in your vertex shader. This is still done automatically by opengl pipeline, you don't need to do this.

There are some rucial bits in your sources left out. For example, i don't know what InitShader(...) does, and i don't see any calls to glUniform(..) (i suppose, they are hidden in pStack object?). And i don't see initialization code of some important variables like numVertexPositionBytes, or what BUFFER_OFFSET(numVertexPositionBytes) is supposed even to mean.

AivanF.
  • 1,134
  • 2
  • 23
  • 52
Pavel Beliy
  • 494
  • 1
  • 3
  • 14
  • 1
    And what yre you doing with pStack.transformd(&pVert[i*4],&pVertActual[i*4])? Transforming your vertex data on the CPU? Why? Aren't your matrix uniforms enough? – Pavel Beliy Oct 22 '13 at 12:03
  • I pulled the vPosition/vPosition.w out of the shader, everything has been initialised to the correct values, I've checked all my initialiser counts multiple times. I wasn't aware that I had to call glUniform, I'm still very new to this and don't really have an awesome grasp on what I'm doing. I believe BUFFER_OFFSET is the number used to notify where vertecies stop and colours begin in a buffer. Also pStack is of a matrix stack class, I can post the code for that as well if you need – user2755996 Oct 22 '13 at 12:24