VBO 不会显示(带有 glew 的 sdl)
VBO won't display (sdl with glew)
我刚刚开始使用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() 是一个 simaple 宏,定义为:
// A helper macro to get a position
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
但我只是无法显示任何内容 - 在即时模式下将相同的纹理等应用于立方体显示正常。
奇怪的是,偶尔会显示一些非常奇怪的东西,每次都不同 - 所以也许是某种初始化错误?
将&m_vertices
和&m_indeces
更改为m_vertices
,并在调用glBufferSubData()
中m_indeces
有效!
您的尺寸都与评论不匹配,我怀疑您正在阅读 VBO 的末尾。
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- SDL 窗口不会弹出
- SDL 操纵杆按钮按下/C++
- GLEW/GLUT:调用init并创建一个窗口后,取消初始化并重新初始化?
- VSCode IntelliSense无法识别SDL框架的SDL_image扩展库
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- 为什么需要检查 GLAD 或 GLEW 是否正确初始化?
- 现代OpenGL和GLEW Libraray的编译错误
- SDL 映像:无法打开映像,仅显示错误消息
- 如何在 3D OpenGL 场景上绘制 SDL 2D 矩形?
- 使用 CMake 和 Visual Studio 2019 编译 GLEW?
- 三角形未绘制.C++/GLEW/SDL.
- OpenGL + SDL + glew 在成功初始化后不会绘制任何内容
- 在VIsual studio 2010 express中使用GLEW,SDL和OpenGL的未解析外部符号
- 使用GLEW 1.10.0和SDL 2.0时出现问题
- 编译GLEW/SDL程序时出错
- 使用 SDL 和 glew 时 Qt Creator 中未定义的引用错误
- 用代码块在C++中编译Glew和SDL
- VBO 不会显示(带有 glew 的 sdl)