我有一个名为 renderLoop 的方法,它将处理我所有的绘图。当我运行程序时,我得到了这个异常。这个异常究竟是什么以及如何解决它?
当我从此类外部的函数回调(framebuffer_size_callback,调整窗口大小)调用 glViewport() 时,我也会遇到相同的异常。
#include "../Headers/glfwSetup.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
}
GlfwSetup::GlfwSetup()
{
LogStart("Constructor : glfwSetup");
Check(glfwInit(), "glfwInit() : [ GL_TRUE ]", "glfw Init() : [ GL_FALSE ]"); // init the glfw library
//manages function pointers for OpenGL so we want to initialize GLAD before we call any OpenGL functions
Check(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress), "GLAD Initialized", "Failed To Initialize GLAD");
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); Log("glfw WindowHints : [ MAJOR : 3 ]");
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); Log("glfw WindowHints : [ MINOR : 3 ]");
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); Log("glfw WindowHints : [ RESIZABLE : [ GL_TRUE ] ]");
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); Log("glfw WindowHints : [ CORE_PROFILE ]\n");
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); //Log("glfw WindowHints : [ FORWARD_COMPAT ]"); // this is for mac users
//create the window
this->m_window = glfwCreateWindow(1024, (1024 / 16 * 9), "Learn OpenGL", NULL, NULL);
CompareToNULL(this->m_window, "Window Created\n", "Failed to create a window\n", glfwTerminate());
// make our window the current context
glfwMakeContextCurrent(this->m_window); LogSet("Context");
// outputs openGL related errors to the console
glfwSetErrorCallback(&LogGlfwError); LogSet("Error Callback");
//this handles resizing of the window
glfwSetFramebufferSizeCallback(this->m_window, framebuffer_size_callback);
LogEnd("[ Constructor : glfwSetup ]");
}
GlfwSetup::~GlfwSetup()
{
LogStart("[ DeConstructor : glfwSetup ]");
glfwTerminate(); Log("glfw [ TERMINATED ]");
LogEnd("[ DeConstructor : glfwSetup ]");
cin.get();
}
void GlfwSetup::renderLoop()
{
LogStart("Render Loop");
while (!glfwWindowShouldClose(this->m_window))
{
//handle input every frame
processInput(this->m_window);
glClearColor(0.2f, 0.3f, 0.6f, 1.0f); // set the color
glClear(GL_COLOR_BUFFER_BIT); // clear the screen with the color set with glClearColor(r, g, b, a)
glfwSwapBuffers(this->m_window);
glfwPollEvents();
}
LogEnd("Render Loop");
}
Run Code Online (Sandbox Code Playgroud)
澄清此检查和日志功能是什么:
#pragma once
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include<iostream>
using namespace std;
#define Log(msg) cout << msg << endl;
#define LogSet(msg) cout << msg << " [ SET ]\n" << endl;
#define LogStart(msg) cout << msg << " [ START ]\n" << endl;
#define LogEnd(msg) cout << msg << " [ END ]\n" << endl;
#define LogError(msg) cout << "[ ERROR ]" << msg << emdl << endl;
#define Check(x, msg, err) if(x) { Log(msg << endl); } else { Log("Error - " << err << endl); }
#define Compare(x, y, msg, err) if(x == y) { Log(msg); } else { Log("Error - " << err); }
#define CompareToNULL(x, msg, err, ter) if(x == nullptr) { Log("Error - " << err); ter; } else { Log(msg); }
static void LogGlfwError(int id, const char* description)
{
cout << "[ GLFW ERROR ] : " << description << " !!!" << endl << endl;
}
Run Code Online (Sandbox Code Playgroud)
您gladLoadGLLoader 在拥有 GL 上下文之前正在调用。您已初始化 GLFW,但在您实际创建 之前GLFWwindow,您还没有 GL 上下文。因此,glfwGetProcAddress不会为您提供上下文的有效函数指针。glfwMakeContextCurrent打电话后移动它,你应该很甜蜜。
另一件事:GLFW 是一个 C 库,需要 C 回调/约定。您应该将这些回调包装在一个extern "C" { ... }块中。
| 归档时间: |
|
| 查看次数: |
2170 次 |
| 最近记录: |