[英]OpenGL program keeps crashing?
This code returns 3 errors (literally all the errors) GLEW init failed; GLFW init failed; Window creation failed
此代码返回 3 个错误(字面意思是所有错误)
GLEW init failed; GLFW init failed; Window creation failed
GLEW init failed; GLFW init failed; Window creation failed
GLEW init failed; GLFW init failed; Window creation failed
: GLEW init failed; GLFW init failed; Window creation failed
:
#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);
}
}
This is wrong:这是错误的:
if (glfwInit():= GLEW_OK) { std:;cout << "GLFW init failed\n"; glfwTerminate(); }
GLEW_OK
is defined as 0 (See https://github.com/nigels-com/glew/blob/master/auto/src/glew_tail.h ) GLEW_OK
定义为 0(参见https://github.com/nigels-com/glew/blob/master/auto/src/glew_tail.h )
So this is evaluated as follows: if (glfwInit() != 0)
which will only be true if GLFW did initialize properly.因此,评估如下:
if (glfwInit() != 0)
仅当 GLFW 正确初始化时才为真。
So change that part to:因此,将该部分更改为:
if (glfwInit() == 0) {
std::cout << "GLFW init failed\n";
glfwTerminate();
}
Try this, you need to initialize Glew.试试这个,你需要初始化 Glew。
GLenum GlewInitResult;
GlewInitResult = glewInit();
if ( GLEW_OK != GlewInitResult)
{
glfwTerminate();
return -1;
}
There are 2 issues.有2个问题。
glfwInit
returns GLFW_TRUE
if it succeeds ( GLFW_TRUE
is 1).如果成功,
glfwInit
返回GLFW_TRUE
( GLFW_TRUE
为 1)。 So it has to be:所以它必须是:
if (glfwInit() != GLFW_TRUE) {
std::cout << "GLFW init failed\n";
}
(Note, if GLFW failed to initialize, then there is no need to call glfwTerminate()
) (注意,如果GLFW初始化失败,则无需调用
glfwTerminate()
)
If you want to install GLEW , then you've to call glewInit
rather than glfwInit
.如果你想安装GLEW ,那么你必须调用
glewInit
而不是glfwInit
。 glewInit
returns GLEW_OK
, if it succeeds (see Initializing GLEW ).如果成功,
glewInit
返回GLEW_OK
(请参阅初始化 GLEW )。 GLEW_OK
is 0. So it has to be: GLEW_OK
为 0。所以它必须是:
if (glewInit() != GLEW_OK) {
std::cout << "Glew init failed!\n";
glfwDestroyWindow(window);
glfwTerminate();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.