gpt4 book ai didi

c++ - glClearColor 和 glClear 上的 GLFW 异常

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

enter image description here

我有一个名为 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");
}

澄清这个检查和日志功能是什么:

#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;
}

最佳答案

您在 gladLoadGLLoader 拥有 GL 上下文之前调用。您已经初始化了 GLFW,但是在您实际创建 GLFWwindow 之前,您没有 GL 上下文。因此,glfwGetProcAddress 不会为上下文提供有效的函数指针。在你的 glfwMakeContextCurrent 调用之后移动它,你应该很甜蜜。

另一件事:GLFW 是一个 C 库,需要 C 回调/约定。您应该将这些回调包装在 extern "C"{ ... } block 中。

关于c++ - glClearColor 和 glClear 上的 GLFW 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45118349/

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