gpt4 book ai didi

c++ - 调用 glGenBuffers 时 openGL 崩溃

转载 作者:行者123 更新时间:2023-11-28 02:01:51 25 4
gpt4 key购买 nike

我正在通过 http://learnopengl.com/ 学习 OpenGL但是,当我尝试在教程的“hello triangle”部分绑定(bind)缓冲区时,我崩溃了。我以前有 C++ 编码经验,但我无法弄清楚哪里出了问题。

我的代码:

#include <iostream>

#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/glfw3.h>
#include <GL/gl.h>

using namespace std;



const GLuint WIDTH = 800, HEIGHT = 600;

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
int initializeWindow(GLFWwindow* window);
int main() {

GLFWwindow* window;

cout << "Creating Triangles" << endl;
GLfloat triangles[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f
};

cout << "Initialising GLFW" << endl;
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

cout << "Initialising Window..." << endl;
window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", NULL, NULL);

cout << "Setting Callback Functions..." << endl;
glfwSetKeyCallback(window, key_callback);

cout << "Binding Buffers" << endl;
GLuint VBO;
glGenBuffers(1, &VBO); // <--- Crashing here
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangles), triangles, GL_STATIC_DRAW);

if (initializeWindow(window) == -1) {
return -1;
}

while(!glfwWindowShouldClose(window)) {
glfwPollEvents();

glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glfwSwapBuffers(window);
}

cout << "Terminating..." << endl;
glfwTerminate();
return 0;
}

int initializeWindow(GLFWwindow* window) {

if (window == NULL) {
cout << "Failed to create GLFW window... exiting" << endl;
glfwTerminate();
return -1;
}

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
if(glewInit() != GLEW_OK) {
cout << "Failed to initialize GLEW... exiting" << endl;
return -1;
}

int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);

return 0;
}

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
}

我遇到的只是一般性的“无响应”崩溃,控制台中没有任何错误。非常感谢任何帮助,谢谢。

最佳答案

您需要在窗口创建之后和对 gl 函数的任何调用之前调用 initializeWindow。这是激活当前窗口和初始化 glew 所必需的(这些操作在 initializewindow 中完成)

关于c++ - 调用 glGenBuffers 时 openGL 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39135830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com