顶点数组GLFW崩溃了c ++程序

时间:2014-03-08 07:28:46

标签: c++ arrays opengl glfw vertex-buffer

我正在关注glfw上互联网上的一个教程,制作一个简单的2D游戏。

我的程序停留在一个点,我必须创建一个顶点数组来存储我的四边形顶点并在屏幕上绘制它们。但我的程序崩溃在我生成BUF的行,并抛出以下异常

  

glfwtest.exe中0x00000000处的未处理异常:0xC0000005:Access   冲突。

这是我的GameWindow.h文件

#include <iostream>
#include <GL\glew.h>
#include <GL\glfw.h>

class GameWindow{

private:
    bool _running;
    GLfloat _height;
    GLfloat _width;
    GLuint _vertexBufferID;

public:
    void setRunning(bool nRunning);
    bool getRunning();
    GameWindow(bool running);
    void Render();
    void Update();
};

以下是我的GameWindow.cpp代码

#include "GameWindow.h"

typedef struct{
    GLfloat positionCoordinates[3];
}vertexData;

#define Square_Size 100

vertexData vertices[] = {
    {0.0f,0.0f,0.0f},
    {Square_Size,0.0f,0.0f},
    {Square_Size,Square_Size,0.0f},
    {0.0f,Square_Size,0.0f}
 };

void GameWindow::setRunning(bool nRunning){
    _running = nRunning;
}

bool GameWindow::getRunning(){
    return _running;
}  

GameWindow::GameWindow(bool running){
    _running = running;
    _height = 800;
    _width = 800*16/9;
    _vertexBufferID = 0;
    glClearColor(1.0,1.0,1.0,1.0);  //Set the initial screen to White
    glViewport(0.0f,0.0f,_width, _height);
    glMatrixMode(GL_PROJECTION);   
    gluOrtho2D(0,_width,0,_height);
    glMatrixMode(GL_MODELVIEW);

    //generate and make your vertex array ready for usage
    glGenBuffers(1,&_vertexBufferID);
    glBindBuffer(GL_ARRAY_BUFFER,_vertexBufferID);
    glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);

    //tells opengl to enable vertex array. Normally for other states we use glEnable
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3,GL_FLOAT,sizeof(vertexData),(GLvoid*)offsetof(vertexData,positionCoordinates));
}

void GameWindow::Render(){
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0f,0.0f,0.0f);
     glDrawArrays(GL_QUADS,0,4);
     glfwSwapBuffers();
}

void GameWindow::Update(){

}

最后这是我的主要

#include "GameWindow.h"

GameWindow *gameWindow;

int main(int argc,char** argv){

    glfwInit();
    glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE,GL_TRUE);
    glfwOpenWindow(640,480,8,8,8,8,0,0,GLFW_WINDOW);
    gameWindow = new GameWindow(true);

    while(gameWindow->getRunning()){
        gameWindow->Render();
        gameWindow->Update();
        gameWindow->setRunning(glfwGetWindowParam(GLFW_OPENED));
    }

    delete gameWindow;
    glfwTerminate();
}

此外,我的程序无法识别glGenBuffers(..)说它未定义,我不得不添加glew库来解决问题,尽管在教程中从未添加过glew库。我认为这个问题与glew有关?但我无法解决它!希望有glfw经验的人能提供一些帮助

1 个答案:

答案 0 :(得分:0)

在glwOpenWindow之后调用glewInit ()