Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Opengl VBO赢得';t显示(带glew的sdl)_Opengl_Sdl_Vbo_Glew - Fatal编程技术网

Opengl VBO赢得';t显示(带glew的sdl)

Opengl VBO赢得';t显示(带glew的sdl),opengl,sdl,vbo,glew,Opengl,Sdl,Vbo,Glew,我刚开始使用VBOs(使用SDL/glew)。我试着从一个简单的立方体开始——实际上目前只是立方体的一个面——但我无法显示任何东西 我的顶点结构定义如下: struct Vertex { float x, y, z; //Vertex coords float tx, ty; //Texture coords float nx, ny, nz; //Normal coords }; 然后生成多维数据集,如下所示: Vertex

我刚开始使用VBOs(使用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()是一个simaple宏,定义如下:

// A helper macro to get a position
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
但我就是无法显示任何东西——在即时模式下对立方体应用相同的纹理等,效果很好


奇怪的是,有时会显示一些非常奇怪的东西,每次都不一样-所以可能是某种初始化错误?

您的大小都与注释不匹配,我怀疑您正在读取VBO的结尾。

Huzzah!!在调用
glBufferSubData()
的过程中,将
&m_顶点
&m_索引
更改为
m_顶点
m_索引
,成功了


谢谢你

“index”的复数形式是“index”或“index”。glGetError()告诉你什么?如果VBOs不为你工作,那么
glDraweElements
是什么?顺便说一句,
offsetof
(这是一个标准提供的宏)将比
BUFFER\u OFFSET
好得多。glGetError()似乎返回了GL\u NO\u ERRORHi,很抱歉,我显然忘了添加注释-我认为大小是正确的,因为我目前只尝试渲染一个面,应该用4个顶点结构来描述(注释中说24,因为对于完整的立方体x6面),索引的数量也是一样的