Jason
Jason

Reputation: 2288

Program crash when calling OpenGL functions

I'm trying to setup a game engine project. My visual studio project is setup so that I have an 'engine' project separate from my 'game' project. Then engine project is being compiled to a dll for the game project to use. I've already downloaded and setup glfw and glew to start using openGL. My problem is when ever I hit my first openGL function the program crashes. I know this has something to do with glewinit even though glew IS initializing successfully (no console errors). In my engine project, I have a window class where, upon window construction, glew should be setup:

Window.h

#pragma once
#include "GL\glew.h"
#include "GLFW\glfw3.h"

#if (_DEBUG)
#define LOG(x) printf(x)
#else
#define LOG(x)
#endif

namespace BlazeGraphics
{

    class __declspec(dllexport) Window
    {
    public:
        Window(short width, short height, const char* title);
        ~Window();

        void Update();
        void Clear() const;
        bool Closed() const; 

    private:
        int m_height;
        int m_width;
        const char* m_title;
        GLFWwindow* m_window;

    private:
        Window(const Window& copy) {}
        void operator=(const Window& copy) {}
    };

}

Window.cpp (where glewinit() is called)

#include "Window.h"
#include <cstdio>

namespace BlazeGraphics
{

    //Needed to define outside of the window class (not sure exactly why yet)
    void WindowResize(GLFWwindow* window, int width, int height);

    Window::Window(short width, short height, const char* title) :
        m_width(width),
        m_height(height),
        m_title(title)
    {
        //InitializeWindow
        {
            if (!glfwInit())
            {
                LOG("Failed to initialize glfw!");
                return;
            };

            m_window = glfwCreateWindow(m_width, m_height, m_title, NULL, NULL);
            if (!m_window)
            {
                LOG("Failed to initialize glfw window!");
                glfwTerminate();
                return;
            };

            glfwMakeContextCurrent(m_window);
            glfwSetWindowSizeCallback(m_window, WindowResize);
        }

        //IntializeGl
        {
            //This needs to be after two functions above (makecontextcurrent and setwindowresizecallback) or else glew will not initialize
            **if (glewInit() != GLEW_OK)
            {
                LOG("Failed to initialize glew!");
            }**
        }
    }


    Window::~Window()
    {
        glfwTerminate();
    }

    void Window::Update()
    {
        glfwPollEvents();
        glfwSwapBuffers(m_window);
    }

    void Window::Clear() const
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }

    //Returns a bool because glfwwindowShouldClose returns a nonzero number or zero
    bool Window::Closed() const
    {
        //Made it equal to 1 to take away warning involving converting an int to bool
        return glfwWindowShouldClose(m_window) == 1;
    }

    //Not part of window class so defined above
    void WindowResize(GLFWwindow* window, int width, int height)
    {
        glViewport(0, 0, width, height);
    }

}

Here is my main.cpp file which is found within my game project where I currently have my openGL functionality in global functions (just for now):

main.cpp

#include <iostream>
#include <array>
#include <fstream>
#include "GL\glew.h"
#include "GLFW\glfw3.h"
#include "../Engine/Source/Graphics/Window.h"

void initializeGLBuffers()
{
    GLfloat triangle[] =
    {
        +0.0f, +0.1f, -0.0f,
        0.0f, 1.0f, 0.0f,

        -0.1f, -0.1f, 0.0f, //1
        0.0f, 1.0f, 0.0f,

        +0.1f, -0.1f, 0.0f, //2
        0.0f, 1.0f, 0.0f,
    };

    GLuint bufferID;
    glGenBuffers(1, &bufferID);
    glBindBuffer(GL_ARRAY_BUFFER, bufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, (sizeof(GLfloat)) * 6, nullptr);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, (sizeof(GLfloat)) * 6, (char*)((sizeof(GLfloat)) * 3));

    GLushort indices[] =
    {
        0,1,2
    };

    GLuint indexBufferID;
    glGenBuffers(1, &indexBufferID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
};


void installShaders()
{
    //Create Shader
    GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

    //Add source or text file to shader object
    std::string temp = readShaderCode("VertexShaderCode.glsl");
    const GLchar* adapter[1];

    adapter[0] = temp.c_str();
    glShaderSource(vertexShaderID, 1, adapter, 0);
    temp = readShaderCode("FragmentShaderCode.glsl").c_str();
    adapter[0] = temp.c_str();
    glShaderSource(FragmentShaderID, 1, adapter, 0);

    //Compile Shadaer
    glCompileShader(vertexShaderID);
    glCompileShader(FragmentShaderID);

    if (!checkShaderStatus(vertexShaderID) || !checkShaderStatus(FragmentShaderID))
        return;

    //Create Program
    GLuint programID = glCreateProgram();
    glAttachShader(programID, vertexShaderID);
    glAttachShader(programID, FragmentShaderID);

    //Link Program
    glLinkProgram(programID);

    if (!checkProgramStatus(programID))
    {
        std::cout << "Failed to link program";
        return;
    }

    //Use program
    glUseProgram(programID);
}

int main()
{

    BlazeGraphics::Window window(1280, 720, "MyGame");

    initializeGLBuffers();
    installShaders();

    while (!window.Closed())
    {
        window.Clear();

        glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);

        window.Update();
    };

    return 0;
}

Now if I were to move the glewinit() code here in my main.cpp:

int main()
{

    BlazeGraphics::Window window(1280, 720, "MyGame");

    if (glewInit() != GLEW_OK)
            {
                LOG("Failed to initialize glew!");
            }

    initializeGLBuffers();
    installShaders();

    while (!window.Closed())
    {
        window.Clear();

        glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);

        window.Update();
    };

    return 0;
}

then my program compiles fine. Why does trying to initialize glew within engine.dll cause a program crash? Thanks for any help.

Upvotes: 0

Views: 1883

Answers (1)

derhass
derhass

Reputation: 45362

GLEW works by defining a function pointer as global variable for each OpenGL function. Let's look at glBindBuffer as an example:

#define GLEW_FUN_EXPORT GLEWAPI

typedef void (GLAPIENTRY * PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);
GLEW_FUN_EXPORT PFNGLBINDBUFFERPROC __glewBindBuffer;

So we just have a __glewBindBuffer function pointer, which will be set to the correct address from your OpenGL implementation by glewInit.

To actually be able to write glBindBuffer, GLEW simply defines pre-processor macros mapping the GL functions to those function pointer variables:

#define glBindBuffer GLEW_GET_FUN(__glewBindBuffer);

Why does trying to initialize glew within engine.dll cause a program crash?

Because your engine.dll and your main application each have a separate set of all of these global variables. You would have to export all the __glew* variables from your engine DLL to be able to get access to the results of your glewInit call in engine.dll.

Upvotes: 3

Related Questions