glClearColor和glClear上的GLFW异常

时间:2017-07-15 12:45:16

标签: c++ opengl glfw

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

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:0)

我正在使用GLAD,但它似乎不起作用。 所以我刚刚删除了高兴的包含并包含GL / gl.h并且它正常工作。