简体   繁体   English

如果我缓冲的字节多于所需的字节数,那么opengl中的缓冲区数据问题只会绘制

[英]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 . 以下是代码main.cpp着色器的粘贴箱。 It uses devIL, glload and glfw. 它使用devIL,glload和glfw。 Runs on windows and linux. 在Windows和Linux上运行。 any png named pic.png will load. 任何名为pic.png的png都会加载。

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. 有9 * 3 = 27个花车。 Therefore 108 bytes. 因此108个字节。 if I buffer 108 bytes it will screw up the texture coords. 如果我缓冲108个字节,它将搞砸纹理坐标。 If I buffer 116 bytes, (2 floats more) It renders fine. 如果我缓冲116个字节,(2浮动更多)它渲染得很好。

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? 怎么会发生这种情况?

glVertexAttribPointer第二个参数是组件数,对于纹理坐标,它是2和3表示正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM