VBO 不会显示(sdl with glew)

发布于 2024-11-07 11:19:46 字数 3825 浏览 4 评论 0原文

我刚刚开始使用 VBO(使用 SDL/glew)。我试图从使用立方体的简单示例开始 - 实际上目前只是立方体的一个面 - 但我无法得到任何东西来显示

我的顶点结构定义如下:

struct Vertex
{
float x, y, z;          //Vertex coords
float tx, ty;           //Texture coords
float nx, ny, nz;       //Normal coords
};

然后生成立方体所以:

        Vertex temp;

        //NOTE: Perspective is from looking at the cube from the outside
   //Just trying to display one face for the moment to simplify
        //Back face ------------------------------------------------------------------------------------------
        temp.x = 0.f; temp.y = 0.f; temp.z = 0.f;       //Bottom Right  - 0
        temp.nx = 0.f; temp.ny = 0.f; temp.nz = 1.f; // This stays the same for the rest of the face
        temp.tx = 1.f; temp.ty = 0.f;
        m_vertices.push_back(temp);

        temp.x = 0.f; temp.y = m_fHeight; temp.z = 0.f;         //Top Right - 1
        temp.tx = 1.f; temp.ty = 1.f;
        m_vertices.push_back(temp);

        temp.x = m_fWidth; temp.y = m_fHeight; temp.z = 0.f;    //Top Left      - 2
        temp.tx = 0.f; temp.ty = 1.f;
        m_vertices.push_back(temp);

        temp.x = m_fWidth; temp.y = 0.f; temp.z = 0.f;  //Bottom Left - 3
        temp.tx = 0.f; temp.ty = 0.f;
        m_vertices.push_back(temp);

        m_indeces.push_back(0); m_indeces.push_back(1); m_indeces.push_back(2);
        m_indeces.push_back(2); m_indeces.push_back(3); m_indeces.push_back(0);


        //Generate the vertex buffer
        glGenBuffers(1, &m_vertexBufferID);
        //Bind the vertex buffer                                                                                                
        glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferID);
        //Fill the vertex buffer - size is 24*sizeof(Vertex) bcs 6 faces with 4 corners
        glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 4, NULL, GL_STATIC_DRAW);
        glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Vertex) * 4, &m_vertices); // Actually upload the data

        //Set up the pointers
        glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(12));
        glNormalPointer(GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(20));
        glVertexPointer(3, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(0));


        //Generate the index buffer
        glGenBuffers(1, &m_indexBufferID);                                      
        //Bind the index buffer                                                                                         
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBufferID);
        //Fill the index buffer- size is 36*sizeof(uint) bcs 6 traingle coords in 1 face * 6 faces
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * 6, NULL, GL_STATIC_DRAW);
        glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(GLubyte) * 6, &m_indeces); // Actually upload the data

然后将其发送到单独的函数中进行渲染:

glBindBuffer(GL_ARRAY_BUFFER, vertexID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexID);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);

// Resetup the pointers. 
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(12));
glNormalPointer(GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(20));
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(0));

//Draw the indexed elements
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));

glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

并且 BUFFER_OFFSET() 是一个简单的宏,定义为:

// A helper macro to get a position
#define BUFFER_OFFSET(i) ((char *)NULL + (i))

但我无法显示任何内容 - 在立即模式下将相同的纹理等应用到立方体显示得很好。

奇怪的是,偶尔会显示一些非常奇怪的东西,每次都不一样 - 所以也许是某种初始化错误?

I'm just starting out using VBOs (using SDL/glew). I'm trying to start out using the simple example of a cube - actually at the moment just the one face of a cube - but I can't get anything to display

my vertex structure is defined as follows:

struct Vertex
{
float x, y, z;          //Vertex coords
float tx, ty;           //Texture coords
float nx, ny, nz;       //Normal coords
};

The cube is then generated like so:

        Vertex temp;

        //NOTE: Perspective is from looking at the cube from the outside
   //Just trying to display one face for the moment to simplify
        //Back face ------------------------------------------------------------------------------------------
        temp.x = 0.f; temp.y = 0.f; temp.z = 0.f;       //Bottom Right  - 0
        temp.nx = 0.f; temp.ny = 0.f; temp.nz = 1.f; // This stays the same for the rest of the face
        temp.tx = 1.f; temp.ty = 0.f;
        m_vertices.push_back(temp);

        temp.x = 0.f; temp.y = m_fHeight; temp.z = 0.f;         //Top Right - 1
        temp.tx = 1.f; temp.ty = 1.f;
        m_vertices.push_back(temp);

        temp.x = m_fWidth; temp.y = m_fHeight; temp.z = 0.f;    //Top Left      - 2
        temp.tx = 0.f; temp.ty = 1.f;
        m_vertices.push_back(temp);

        temp.x = m_fWidth; temp.y = 0.f; temp.z = 0.f;  //Bottom Left - 3
        temp.tx = 0.f; temp.ty = 0.f;
        m_vertices.push_back(temp);

        m_indeces.push_back(0); m_indeces.push_back(1); m_indeces.push_back(2);
        m_indeces.push_back(2); m_indeces.push_back(3); m_indeces.push_back(0);


        //Generate the vertex buffer
        glGenBuffers(1, &m_vertexBufferID);
        //Bind the vertex buffer                                                                                                
        glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferID);
        //Fill the vertex buffer - size is 24*sizeof(Vertex) bcs 6 faces with 4 corners
        glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 4, NULL, GL_STATIC_DRAW);
        glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Vertex) * 4, &m_vertices); // Actually upload the data

        //Set up the pointers
        glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(12));
        glNormalPointer(GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(20));
        glVertexPointer(3, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(0));


        //Generate the index buffer
        glGenBuffers(1, &m_indexBufferID);                                      
        //Bind the index buffer                                                                                         
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBufferID);
        //Fill the index buffer- size is 36*sizeof(uint) bcs 6 traingle coords in 1 face * 6 faces
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * 6, NULL, GL_STATIC_DRAW);
        glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(GLubyte) * 6, &m_indeces); // Actually upload the data

which is then sent to render in a seperate function:

glBindBuffer(GL_ARRAY_BUFFER, vertexID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexID);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);

// Resetup the pointers. 
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(12));
glNormalPointer(GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(20));
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(0));

//Draw the indexed elements
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));

glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

and BUFFER_OFFSET() is a simaple macro defined as:

// A helper macro to get a position
#define BUFFER_OFFSET(i) ((char *)NULL + (i))

But I just can't get anything to display - applying the same texture etc. to a cube in immediate mode shows up fine.

The weird thing is that occasionaly something very strange will get displayed, which is different every time - so maybe it's some sort of initialization error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风夜微凉 2024-11-14 11:19:46

在调用 glBufferSubData() 时将 &m_vertices&m_indeces 更改为 m_verticesm_indeces成功了!

Changing &m_vertices and &m_indeces to m_vertices and m_indeces in the call to glBufferSubData() worked!

终遇你 2024-11-14 11:19:46

你的尺寸都不符合评论,我怀疑你正在读 VBO 的结尾。

None of your sizes match the comments, I suspect you're reading off the end of the VBO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文