1

enter image description here

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;
}
Shago
  • 101
  • 3
  • 9
  • Possible duplicate of https://stackoverflow.com/questions/14009726/access-violation-reading-location-0x000000000-in-a-simple-opengl-app – Batın Evirgen Jul 15 '17 at 12:46
  • 4
    Please post all the relevant code in text form. Do you have a valid context at that point? – BDL Jul 15 '17 at 12:46
  • 1
    @Stradivarius: I don't think it's a duplicate of this. `glClearColor` was introduced in OpenGL 1. Windows should support 1.2 without extension loading. – BDL Jul 15 '17 at 12:48
  • I think he didn't initialize his OpenGL Loader Library and loader library can not find memory location of that function. – Batın Evirgen Jul 15 '17 at 12:52
  • 1
    @Stradivarius: As I said in my last comment: You only need a OpenGL loading library for functions > OpenGL 1.2. The default ABI supports OpenGL 1.2 without using any loading library. – BDL Jul 15 '17 at 12:59
  • Yes i do have a window and a context at this point I Will Post the code – Shago Jul 15 '17 at 13:06
  • 1
    @BDL: That depends on the loading library you use. Some loading libraries load everything dynamically, so if you include their header instead of the system-provided ``, your `glClearColor` ends being a pointer that has to be initialized. Based on the access violation the OP gets, it's almost surely the case. – Yakov Galka Jul 15 '17 at 13:23
  • I use GLAD if that is what you mean @ybungalobill – Shago Jul 15 '17 at 13:31

2 Answers2

6

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.

Brett Hale
  • 21,653
  • 2
  • 61
  • 90
0

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.

Shago
  • 101
  • 3
  • 9