我正在尝试用C ++编写我的第一个游戏引擎(我已经在java中完成了它)我创建了一个基本的网格类,它包含了vao / Vertex数组的GLuint整数和一个数组(目前只有2的大小为Buffers / vbos,当我尝试在网格类中调用我的构造函数时,我调用函数glGenVertexArrays(1,&vaoId);程序崩溃,在视觉工作室一个盒子出现说
在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;
}
它是否与驱动程序,链接器或我的代码中的错误有关?
GLEW库必须由glewInit
初始化,在OpenGL上下文由glfwMakeContextCurrent
成为当前。
见Initializing GLEW。
首先使OpenGL上下文成为当前,然后是init 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
中的元素数量可以通过std::vector::size
获得,std::vector::data
可以获得指向内容的指针。
在您的情况下,每个顶点坐标由3个分量(x,y和z)组成,因此坐标数为vertices.size() / 3
。
glBufferData
的第二个参数必须是缓冲区的大小,以字节为单位,即vertices.size() * sizeof(float)
。