我的程序的目标是使用VBO显示旋转的简单三角形。 程序在glDrawArrays调用时失败(分段错误)。 这是我的c ++代码的一部分:
#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <gl/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#define OFFSET_BUFFER(offset) ((char*)NULL + (offset))
GLfloat angle = 0.0f;
const static int WIDTH = 500;
const static int HEIGHT = 500;
struct Vertex
{
float x, y, z;
float nx, ny, nz;
float s0, t0;
};
Vertex vertices[3] =
{
1.000000f, -1.000000f, -1.000000f,
0.000000f, -1.000000f, 0.000000f,
0.626059f, 0.734295f,
1.000000f, -1.000000f, 1.000000f,
0.000000f, -1.000000f, 0.000000f,
0.626059f, 0.512602f,
-1.000000f, -1.000000f, 1.000000f,
0.000000f, -1.000000f, 0.000000f,
0.404365f, 0.512602f,
};
[...]
static GLuint applyVBO(void)
{
GLuint vboID;
glGenBuffers(1, &vboID);
glBindBuffer(GL_VERTEX_ARRAY, vboID);
glBufferData(GL_VERTEX_ARRAY, 3 * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_VERTEX_ARRAY, NULL);
return (vboID);
}
int main(int ac, char **av)
{
bool continuer = true;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("VBO tests",NULL);
SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL);
if (glewInit() != GLEW_OK)
return (-1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//glEnable(GL_NORMALIZE);
//Initialize projection
glViewport(0, 0, WIDTH, HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)WIDTH/(float)HEIGHT, 1.0f, 100.0f);
gluLookAt(0.0f, 0.0f, 8.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
//Initialize VBO
GLuint vboID = applyVBO();
//Initialize uv-mapping
GLuint texID = applyTexture("Box.bmp");
//Initialize camera
//Main loop
while (continuer)
{
eventListener(&event, &continuer);
glClearDepth(1.0f);
glClearColor(0.13f, 0.12f, 0.13f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glRotatef(angle, 1.0f, 1.0f, 1.0f);
glBindBuffer(GL_VERTEX_ARRAY, vboID);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), OFFSET_BUFFER(0));
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, sizeof(Vertex), OFFSET_BUFFER(12));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, texID);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), OFFSET_BUFFER(24));
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindTexture(GL_TEXTURE_2D, NULL);
glBindBuffer(GL_VERTEX_ARRAY, NULL);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
angle += 0.50f;
glPopMatrix();
glFlush();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return (0);
}
有人可以帮助我吗?
答案 0 :(得分:1)
问题来自于GL_VERTEX_ARRAY标志不是好的标志。它们必须由GL_ARRAY_BUFFER替换。