分解代码时的奇怪的OpenGL问题 [英] Weird OpenGL issue when factoring out code

查看:101
本文介绍了分解代码时的奇怪的OpenGL问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些代码创建一个缓冲区并在其中放入一些顶点:

  GLuint vertexbuffer; 
glGenBuffers(1,& vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER,vertexbuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(g_vertex_buffer_data),g_vertex_buffer_data,GL_STATIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ARRAY_BUFFER,0);
glEnableVertexAttribArray(0);

我还将其绑定到着色器属性:

  glBindAttribLocation(programID,0,pos); 

最后,绘制:

  glBindBuffer(GL_ARRAY_BUFFER,vertexbuffer); 
glDrawArrays(GL_TRIANGLES,0,3);

当然,还有其他代码,但所有这些东西都运行良好但是,我试图把这个东西在一个结构体,没有任何东西会显示(这里是其中的一种方法):

$(

) b
$ b

  void loadVerts(GLfloat verts [],int indices)
{
GLuint vertexbuffer;
glGenBuffers(1,& vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER,vertexbuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(verts),verts,GL_STATIC_DRAW);
glVertexAttribPointer(indice,indices,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ARRAY_BUFFER,0);
glEnableVertexAttribArray(indice);
indice ++;
buffers.push_back(vertexbuffer);
}



我已经四次检查这个代码,我也跟踪它确保它将匹配上面的代码每当它的调用。我的绘制调用几乎和我原来的一样:

  void draw()
{
glBindBuffer GL_ARRAY_BUFFER,buffers.at(0));
glDrawArrays(GL_TRIANGLES,0,3);
}



我也试过把这个类作为一个类,的代码。 buffers indice 只是一些vars来跟踪缓冲区和属性索引。 buffers std :: vector< GLuint> FWIW。

解决方案

主要问题在这里:

  void loadVerts(GLfloat verts [ ,int indices)
{
...
glBufferData(GL_ARRAY_BUFFER,sizeof(verts),verts,GL_STATIC_DRAW);

verts 参数的类型是指向 GLfloat 。您的函数签名等效于:

  void loadVerts(GLfloat * verts,int indices)

所以 sizeof(verts),用作<$ c的第二个参数$ c> glBufferData()在32位架构上为4,在64位架构上为8。



传递大小作为此函数的附加参数,并将该值用作 glBufferData()的第二个参数。



这些语句看起来有点混乱:

  glVertexAttribPointer(indice,indices,GL_FLOAT,GL_FALSE,0,0); 
glEnableVertexAttribArray(indice);

我不知道是否有一个真正的问题,但你有两个变量名称非常相似使用方式截然不同。 indice 需要是顶点着色器中属性的位置,而 indices 需要是组件的数量属性。


So I have some code that creates a buffer and puts some vertices in it:

GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(0);

I also bind it to a shader attribute:

glBindAttribLocation(programID, 0, "pos");

And, finally, draw it:

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glDrawArrays(GL_TRIANGLES, 0, 3);

Of course, there is other code, but all of this stuff runs fine (displays a red triangle on the screen)

However, the instant I try to factor this stuff out in a struct, nothing will display (here is one of the methods):

void loadVerts(GLfloat verts[], int indices)
    {
        GLuint vertexbuffer;
        glGenBuffers(1, &vertexbuffer);
        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
        glVertexAttribPointer(indice, indices, GL_FLOAT, GL_FALSE, 0, 0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glEnableVertexAttribArray(indice);
        indice++;
        buffers.push_back(vertexbuffer);
    }

I've quadruple checked this code, and I've also traced it to make sure it would match the code above whenever its called. My draw call is almost the same as my original:

void draw()
    {
        glBindBuffer(GL_ARRAY_BUFFER, buffers.at(0));
        glDrawArrays(GL_TRIANGLES, 0, 3);
    }

I've also tried making this a class, and adding/changing many parts of the code. buffers and indice are just some vars to keep track of buffers and attribute indexes. buffers is an std::vector<GLuint> FWIW.

解决方案

The main problem is here:

void loadVerts(GLfloat verts[], int indices)
{
    ...
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

The type of the verts argument is a pointer to GLfloat. Your function signature is equivalent to:

void loadVerts(GLfloat* verts, int indices)

So sizeof(verts), which is used as the second argument to glBufferData(), is 4 on a 32-bit architecture, 8 on a 64-bit architecture.

You will need to pass the size as an additional argument to this function, and use that value as the second argument to glBufferData().

These statements also look somewhat confusing:

glVertexAttribPointer(indice, indices, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(indice);

I can't tell if there's a real problem, but you have two variables with very similar names that are used very differently. indice needs to be the location of the attribute in your vertex shader, while indices needs to be the number of components in the attribute.

这篇关于分解代码时的奇怪的OpenGL问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆