openglc++;GLGEnVertexArray上的glfw 3 glew错误1282 我试图用C++编写我的第一个游戏引擎(我已经用java THO完成了它) 我创建了一个基本网格类,它为vao/顶点数组保存一个整数GLuint,为缓冲区/VBO保存一个数组(目前大小仅为2), 当我试图在mesh类中调用构造函数时,我调用了函数 glGenVertexArrays(1,&vaoId);程序崩溃了,在VisualStudio上出现了一个框,上面写着
在0x00000000上执行路径期间违反了访问权限 Mesh.cpp:openglc++;GLGEnVertexArray上的glfw 3 glew错误1282 我试图用C++编写我的第一个游戏引擎(我已经用java THO完成了它) 我创建了一个基本网格类,它为vao/顶点数组保存一个整数GLuint,为缓冲区/VBO保存一个数组(目前大小仅为2), 当我试图在mesh类中调用构造函数时,我调用了函数 glGenVertexArrays(1,&vaoId);程序崩溃了,在VisualStudio上出现了一个框,上面写着,c++,opengl,glfw,glew,C++,Opengl,Glfw,Glew,在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); //
#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
\ifndef网格
#定义网格
#包括
类网格{
私人:
int垂直长度;
GLuint vboIds[2];//0=位置,1=纹理词
胶膜;
公众:
网格(GLuint vaoId,int verticesLength);
网格(浮动顶点[]);
~Mesh();
int getVerticesLength();
GLuint getVaoId();
void render();
};
#endif网格
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;
}
#包括
#包括“Mesh.h”
#包括
#包括“GlfwUtils.h”
#包括“InputManager.h”
#定义宽度800
#定义高度600
bool初始化eglfw();
int main(){
如果(!initializeGLFW())返回EXIT_失败;
GLFWwindow*window=glfwCreateWindow(宽度、高度,“范围引擎”,
空,空);
glfwMakeContextCurrent(窗口);
如果(!窗口){
std::cout在OpenGL上下文成为当前上下文后,必须通过glewInit
初始化库。
看
首先将OpenGL上下文设置为当前,然后初始化GLEW:
glfwMakeContextCurrent(窗口);
如果(!窗口){
std::可能不相关,但是,Mesh
构造函数中的(sizeof(顶点)
可能没有您认为的功能。非常感谢Rabbi76,它现在可以工作了!您应该对照GLEW\u OK
(=0)检查glewInit
的返回值。当前,当glew高兴时,代码返回false,这与您想要的正好相反。