GLFW exception on glClearColor and glClear

∥☆過路亽.° 提交于 2019-12-14 02:23:49

问题


I have a method called renderLoop that is going to take care of all my drawing. When i run the program i get this exception. What exactly is this exception and how to fix it ?

I also get the same exception when i call glViewport() from a function callback (framebuffer_size_callback, resizing the window) outside this class.

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

To clarify what this check and log functions are :

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

回答1:


You are calling gladLoadGLLoader before you have a GL context. You've initialized GLFW, but until you actually create a GLFWwindow, you don't have a GL context. Therefore, glfwGetProcAddress is not going to provide you with valid function pointers for a context. Move it after your glfwMakeContextCurrent call, and you should be sweet.

One other thing: GLFW is a C library and expects C callbacks / conventions. You should be wrapping those callbacks in an extern "C" { ... } block.




回答2:


I'm using GLAD but it seems that it does not work. So i just removed the glad includes and included GL/gl.h and it worked correctly.



来源:https://stackoverflow.com/questions/45118349/glfw-exception-on-glclearcolor-and-glclear

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!