私はVBOを使い始めたばかりです(SDL / glewを使用)。立方体の簡単な例を使用して開始しようとしていますが、実際には現時点では立方体の1つの面だけですが、何も表示できません。
私の頂点構造は次のように定義されています。
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))
しかし、何も表示できません。同じテクスチャなどを即時モードで立方体に適用すると、問題なく表示されます。
奇妙なことに、時々非常に奇妙なものが表示されますが、それは毎回異なります-それで、おそらくそれはある種の初期化エラーですか?