当我调用 glBindBuffer 时,它会产生访问冲突。我确保 glewInit 返回 GLEW_OK 并且传递的参数是有效的。所以我对这个问题没有任何想法。你能帮助我吗?大概,这个功能需要我的集成显卡支持吗?我在 Windows Vista 上使用 Visual C++ 2008 express 做到了这一点。这是代码:
#include <stdio.h>
#include <assert.h>
#define GLEW_STATIC
#include <GL/glew.h>
#ifdef __APPLE__
#include <glut/glut.h> // OS X version of GLUT
#include <GL/glxew.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#include <GL/wglew.h>
#endif
void ChangeSize(int w, int h) {
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, w, h);
}
void SetupRC() {
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
gluOrtho2D(0.0, 300.0, 0.0, 250.0);
}
void RenderScene(void) {
glPolygonMode(GL_FRONT, GL_LINE);
glPolygonMode(GL_BACK, GL_LINE);
GLfloat points[] = {
20.0f, 10.0f, 1.0f,
200.0f, 20.0f, 1.0f,
50.0f, 30.0f, 1.0f,
125.0f, 100.0f, 1.0f
};
GLfloat colors[] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
printf("glew version: %s", (char*)glewGetString( GLEW_VERSION ));
//GLuint arrayIdentifier[2];
// it will crash
//glGenBuffers(2, arrayIdentifier);
//instead, even if i dont call glGenBuffers, the program will crash when it comes to glBindBuffer
//GLuint arrayIdentifier[2] = {2, 3};
glBindBuffer(GL_ARRAY_BUFFER, arrayIdentifier[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), (GLvoid*)points, GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, (GLubyte*)NULL + 0);
glBindBuffer(GL_ARRAY_BUFFER, arrayIdentifier[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), NULL, GL_STATIC_DRAW);
GLfloat* bufColor = (GLfloat*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
for (int i = 0; i < sizeof(colors) / sizeof(*colors); ++i){
bufColor[i] = colors[i];
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glColorPointer(3, GL_FLOAT, 0, (void*)bufColor);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBegin(GL_POLYGON);
glArrayElement(0);
glArrayElement(1);
glArrayElement(2);
glEnd();
glBegin(GL_POLYGON);
glArrayElement(2);
glArrayElement(1);
glArrayElement(3);
glEnd();
glFlush();
}
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitWindowSize(800, 600);
glutCreateWindow("Triangle");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return 1;
}
SetupRC();
glutMainLoop();
return 0;
}