c++ - Program crash when calling OpenGL functions -


i'm trying setup game engine project. visual studio project setup have 'engine' project separate 'game' project. engine project being compiled dll game project use. i've downloaded , setup glfw , glew start using opengl. problem when ever hit first opengl function program crashes. know has glewinit though glew initializing (no console errors). in engine project, have window class where, upon window construction, glew should 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() called)

#include "window.h" #include <cstdio>  namespace blazegraphics {      //needed define outside of window class (not sure 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 initialize glfw!");                 return;             };              m_window = glfwcreatewindow(m_width, m_height, m_title, null, null);             if (!m_window)             {                 log("failed initialize glfw window!");                 glfwterminate();                 return;             };              glfwmakecontextcurrent(m_window);             glfwsetwindowsizecallback(m_window, windowresize);         }          //intializegl         {             //this needs after 2 functions above (makecontextcurrent , setwindowresizecallback) or else glew not initialize             **if (glewinit() != glew_ok)             {                 log("failed 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 bool because glfwwindowshouldclose returns nonzero number or 0     bool window::closed() const     {         //made equal 1 take away warning involving converting int bool         return glfwwindowshouldclose(m_window) == 1;     }      //not part of window class defined above     void windowresize(glfwwindow* window, int width, int height)     {         glviewport(0, 0, width, height);     }  } 

here main.cpp file found within game project have opengl functionality in global functions (just 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 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 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 move glewinit() code here in main.cpp:

int main() {      blazegraphics::window window(1280, 720, "mygame");      if (glewinit() != glew_ok)             {                 log("failed initialize glew!");             }      initializeglbuffers();     installshaders();      while (!window.closed())     {         window.clear();          gldrawelements(gl_triangles, 3, gl_unsigned_short, 0);          window.update();     };      return 0; } 

then program compiles fine. why trying initialize glew within engine.dll cause program crash? help.

glew works defining function pointer global variable each opengl function. let's @ glbindbuffer example:

#define glew_fun_export glewapi  typedef void (glapientry * pfnglbindbufferproc) (glenum target, gluint buffer); glew_fun_export pfnglbindbufferproc __glewbindbuffer; 

so have __glewbindbuffer function pointer, set correct address opengl implementation glewinit.

to able write glbindbuffer, glew defines pre-processor macros mapping gl functions function pointer variables:

#define glbindbuffer glew_get_fun(__glewbindbuffer); 

why trying initialize glew within engine.dll cause program crash?

because engine.dll , main application each have separate set of of these global variables. have export __glew* variables engine dll able access results of glewinit call in engine.dll.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -