gpt4 book ai didi

c++ - OpenGL 程序不断崩溃?

转载 作者:行者123 更新时间:2023-11-30 04:42:42 26 4
gpt4 key购买 nike

此代码返回 3 个错误(实际上是所有错误)GLEW 初始化失败; GLFW 初始化失败;窗口创建失败:

#include "GL/glew.h"
#include "GLFW/glfw3.h"

#include <iostream>


//Window dimensions
const GLint SCREEN_WIDTH = 800, SCREEN_HEIGHT = 600;


int main() {
//Initialize GLFW
if (glfwInit() != GLEW_OK) {
std::cout << "GLFW init failed\n";
glfwTerminate();
}


//Setup GLFW window properties
//OpenGL version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //Large version
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //Small version
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //Detects any old OpenGL code, this will throw an error
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); //Allows forward compatibility (between differnt OS)


//Creating window
GLFWwindow* window;
window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "OpenGL Test Window", NULL, NULL);
if (window == NULL) {
std::cout << "GLFW window creation failed!\n";
glfwTerminate();
}

//Get buffer size information
int BUFFER_WIDTH, BUFFER_HEIGHT;
glfwGetFramebufferSize(window, &BUFFER_WIDTH, &BUFFER_HEIGHT);

//Set context for GLEW to use (can change between which window)
glfwMakeContextCurrent(window);

//Allow modern extension features
glewExperimental = GL_TRUE;


if (glfwInit() != GLEW_OK) {
std::cout << "Glew init failed!\n";
glfwDestroyWindow(window);
glfwTerminate();
}


//Setup viewport size
glViewport(0, 0, BUFFER_WIDTH, BUFFER_HEIGHT);



//Main game loop
while (!glfwWindowShouldClose(window)) {
//Get + Handle user input events
glfwPollEvents();

//Clear window
glClearColor(1.f, 0.f, 0.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);

glfwSwapBuffers(window);
}
}

最佳答案

试试这个,你需要初始化 Glew。

GLenum GlewInitResult;
GlewInitResult = glewInit();
if ( GLEW_OK != GlewInitResult)
{
glfwTerminate();
return -1;
}

关于c++ - OpenGL 程序不断崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58635821/

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