OpenGL C ++ glfw 3在glGenVertexArrays上出现错误1282

时间:2019-03-14 13:34:02

标签: c++ opengl glfw glew

我正在尝试用C ++编写我的第一个游戏引擎(我已经用Java来完成了) 我创建了一个基本的网格类,它为vao / Vertex数组保存一个GLuint整数,为Buffers / vbos保存一个数组(目前大小为2), 当我尝试在Mesh类中调用构造函数时,我调用了函数 glGenVertexArrays(1,&vaoId);程序崩溃,在Visual Studio上出现一个框,显示

  

在0x00000000上执行路径期间违反了访问权限

Mesh.cpp:

#include "Mesh.h"

Mesh::Mesh(GLuint vaoid, int verticeslength) : vaoId(vaoid), 
verticesLength(verticeslength) {}

Mesh::Mesh(float vertices[]) {
    this->verticesLength = sizeof(vertices) / sizeof(float); // set the length of the vertices
    glGenVertexArrays(1, &vaoId); // create VAO
    glBindVertexArray(vaoId); // bind VAO
    glGenBuffers(1, &vboIds[0]); // allocate memory to VBO
    glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]); // bind vbo
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) / sizeof(float), 
    vertices, GL_STATIC_DRAW); // store data in vbo
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); // store vbo in vao
  }

  Mesh::~Mesh() {
      glDisableVertexAttribArray(0); // disable the position vbo
      glDeleteBuffers(2, vboIds); // delete the vbos
      glDeleteVertexArrays(1, &vaoId); // delete the vbos
      delete &vaoId;
      delete &vboIds;
   }

   GLuint Mesh::getVaoId() { return vaoId; }

   int Mesh::getVerticesLength() { return verticesLength; }

   void Mesh::render() {
        glBindVertexArray(vaoId);
        glEnableVertexAttribArray(0);
        glDrawArrays(GL_TRIANGLES, 0, verticesLength);
        glDisableVertexAttribArray(0);
        glBindVertexArray(0);
    }

Mesh.h:

#ifndef Mesh_H
#define Mesh_H

#include <GL/glew.h>

 class Mesh {
 private:
     int verticesLength;
     GLuint vboIds[2]; // 0 = position, 1 = textureCoords
     GLuint vaoId;
 public:
     Mesh(GLuint vaoId, int verticesLength);
     Mesh(float vertices[]);
     ~Mesh();
     int getVerticesLength();
     GLuint getVaoId();
     void render();
 };

 #endif Mesh

Main.cpp:

 #include <iostream>
 #include "Mesh.h"
 #include <GLFW/glfw3.h>
 #include "GlfwUtils.h"
 #include "InputManager.h"

 #define WIDTH 800
 #define HEIGHT 600

 bool initializeGLFW();

 int main() {
     if (!initializeGLFW()) return EXIT_FAILURE;
     GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Scope Engine", 
     NULL, NULL);
     glfwMakeContextCurrent(window);
     if (!window) {
        std::cout << "Window creation failed" << std::endl;
        return EXIT_FAILURE; 
     }
     glfwSetKeyCallback(window, InputManager::key_callback);

     float vertices[] = {
     -0.5f, 0.5f, 0,
     -0.5f, -0.5f, 0,
      0.5f, -0.5f, 0,
      0.5f, -0.5f, 0,
      0.5f, 0.5f, 0,
      -0.5f, 0.5f, 0
      }; 

      Mesh* mesh = new Mesh(vertices); // gotta initalize the mesh!


     while (!glfwWindowShouldClose(window)) {
          mesh->render();
          std::cout << "Game Loop!" << std::endl;
          GlfwUtils::UpdateDisplay(window);
     }

     delete mesh;
     glfwDestroyWindow(window);
     return EXIT_SUCCESS;
 }

 bool initializeGLFW() {
     glewExperimental = GL_TRUE;
     if (!glewInit()) {
         std::cout << "Couldn't initalize OpenGL" << std::endl;
         return false;
     }
     GLenum error = glGetError();
     if (error != GL_NO_ERROR) { std::cout << "OpenGL error: " << error << std::endl; }
     if (!glfwInit()) {
         std::cout << "Couldn't initalize GLFW" << std::endl;
         return false;
     }
     glfwSetErrorCallback(GlfwUtils::error_callBack);
     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
     return true;
  }

这与驱动程序,链接器有关还是我的代码出错了?

1 个答案:

答案 0 :(得分:2)

GLEW成为OpenGL上下文之后,glfwMakeContextCurrent库必须由glewInit初始化。
参见Initializing GLEW

首先将OpenGL上下文设为当前,然后初始化GLEW:

glfwMakeContextCurrent(window);
if (!window) {
    std::cout << "Window creation failed" << std::endl;
    return EXIT_FAILURE; 
}

glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
    std::cout << "Couldn't initalize OpenGL" << std::endl;
    return false;
}

在OpenGL常量成为当前版本之前调用任何OpenGL指令都没有意义。
GLenum error = glGetError();中删除initializeGLFW


在构造函数Mesh::Mesh(float vertices[])中,sizeof(vertices)不是数组的大小(这不是java)。它是指向数组的指针的大小,在64位系统上为8。

使用std::vector

#include <vector>
std::vector<float> vertices{
    -0.5f, 0.5f, 0,
    -0.5f, -0.5f, 0,
     0.5f, -0.5f, 0,
     0.5f, -0.5f, 0,
     0.5f, 0.5f, 0,
    -0.5f, 0.5f, 0
}; 
Mesh *mesh = new Mesh(vertices);
class Mesh {
private:
    int noOfVertices;
    // [...]

public:
    Mesh::Mesh(const std::vector<float> &vertices);
    // [...]
};

Mesh::Mesh(const std::vector<float> &vertices) {

    // [...]

    noOfVertices = (int)vertices.size() / 3;
    glBufferData(GL_ARRAY_BUFFER, 
        vertices.size()*sizeof(float), vertices.data(), GL_STATIC_DRAW);
}

std::vector中元素的数量可以通过std::vector::size来获取,指向内容的指针可以通过std::vector::data来获取。
在您的情况下,每个顶点坐标由3个分量( x y z )组成,因此坐标数为{{1} }。
glBufferData的第二个参数必须是缓冲区的大小(以字节为单位),即vertices.size() / 3