繁体   English   中英

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

[英]OpenGL C++ glfw 3 glew Error 1282 on glGenVertexArrays

我正在尝试用 C++ 编写我的第一个游戏引擎(我已经在 java 中完成了它)我创建了一个基本的网格类,它包含一个用于 vao/顶点数组的 GLuint 整数和一个数组(目前缓冲区/vbos 的大小仅为 2),当我尝试在网格类中调用我的构造函数时,我调用函数 glGenVertexArrays(1, &vaoId); 程序崩溃,在visual studio上出现一个框说

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

网格.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);
    }

网格.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

主.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;
  }

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

在 OpenGL 上下文由glfwMakeContextCurrent变为当前上下文后,必须通过glewInit初始化GLEW库。
请参阅初始化 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 constex 成为当前之前调用任何 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 van 中元素的数量可由std::vector::size获取,指向其内容的指针可由std::vector::data
在您的情况下,每个顶点坐标由 3 个分量( xyz )组成,因此坐标数为vertices.size() / 3
glBufferData的第二个参数必须glBufferData字节为单位的缓冲区大小,即vertices.size() * sizeof(float)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM