顶点阵列GLFW崩溃C 程序

Vertex Arrays GLFW crashes c++ program

本文关键字:程序 崩溃 GLFW 阵列 顶点      更新时间:2023-10-16

我正在遵循GLFW上Internet的一个教程以制作一个简单的2D游戏。

我的程序被困在一个我必须创建一个顶点阵列以存储我的四边形顶点并将其绘制在屏幕上的点。但是我的程序崩溃了,在我生成以下异常

的情况下,我的程序崩溃了

glfwtest.exe中的0x00000000 inthanded Exception:0xc0000005:访问 违规。

这是我的gamewindow.h文件

#include <iostream>
#include <GLglew.h>
#include <GLglfw.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经验的人可以提供一些帮助

在glwopenwindow之后呼叫glewInit ()