glGenBuffers生成访问冲突

时间:2013-07-14 10:35:03

标签: c++ c opengl

当我调用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;
}

0 个答案:

没有答案