Andrew
Andrew

Reputation: 1784

Issue with buffer data in opengl = only draws if I buffer more bytes than needed

Here are the paste bins for the code main.cpp and the shaders. It uses devIL, glload and glfw. Runs on windows and linux. any png named pic.png will load.

I buffer my data in a fairly normal way. Just a simple triangle.

glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
//vX     vY    vZ   vW       nX   nY   nZ     U    V        
float bufferDataThree[9*3] = {  
-1.0f, -1.0f, 0.0f,1.0f,    0.0f,1.0f,0.0f,  0.0f,0.0f,
1.0f, -1.0f, 0.0f,1.0f,    0.0f,1.0f,0.0f,  0.0f,1.0f,
1.0f,  1.0f, 0.0f,1.0f,    0.0f,1.0f,0.0f,  1.0f,1.0f};
//TOTAL 4 + 3 + 2 = 9;  
glBufferData(GL_ARRAY_BUFFER, (9*3)*4, bufferDataThree, GL_STATIC_DRAW); //Doesnt Work
//glBufferData(GL_ARRAY_BUFFER, (10*3)*4, bufferDataThree, GL_STATIC_DRAW); //Works

There is 9*3 = 27 floats. Therefore 108 bytes. if I buffer 108 bytes it will screw up the texture coords. If I buffer 116 bytes, (2 floats more) It renders fine.

My display method.

void display()
{
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgram(program);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D,tbo);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, (4 + 3 + 2)*sizeof(float), 0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, (4 + 3 + 2)*sizeof(float),(void*) (4*sizeof(float)));
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, (4 + 3 + 2)*sizeof(float),(void*) ((4+3)*sizeof(float)));

    glDrawArrays(GL_TRIANGLES,0,3);
    glDisableVertexAttribArray(0);
    glUseProgram(0);

    glfwSwapBuffers();
}

How can this be happening?

Upvotes: 1

Views: 144

Answers (1)

yngccc
yngccc

Reputation: 5684

second argument to glVertexAttribPointer is number of components, for texture coord it is 2 and 3 for normal.

Upvotes: 3

Related Questions