PDA

View Full Version : GLEW and GLFW once again



Ckef01
05-18-2012, 06:35 AM
Hey,

I'm relatively new to OpenGL, I'm still experimenting with different techniques etc. I got a window up and running with GLFW and GLEW, soon I started rendering cubes, camera movement and shaders in OpenGL 3.0+. I used the GLEW binaries so far, but for some reason it just failed to work at some point. So I downloaded the source of GLEW, imported the visual studio project into Code::Blocks, and build the libglew_static.a. Of course I made sure I had the newest version of everything (glew 1.7 and glfw 2.7.5).

The window setup works all fine, no problem on GLFW's part, but as soon as I call glewInit() it throws a GL_INVALID_ENUM error, and the function returns GLEW_ERROR_NO_GL_VERSION. If I don't check for errors and just continue as if nothing happened, it seems to render fine on some machines, and on others it just shows a black screen.

Including gl headers:


// OpenGL
#define GLFW_NO_GLU
#define GLEW_STATIC

#include "GL/glew.h"
#include "GL/glfw.h"


Also I'm pretty sure I linked everything correctly. I build the 'engine' in a .dll, I linked libglew_static.a, libglfw.a, opengl32, kernel32, in that exact order. The actual test project which uses the engine only links to the engine .dll, and of course it searches in it's include directory for header files. It doesn't show any errors when compiling, it simply throws them when I run the program.

The actual setup:



//-----------------------------------------------------------------------
Console::Console() :
_open(false),
_frameCap(0),
_prevTime(0.0),
_frameTime(0.0),

_major(3),
_minor(0),

_width(800),
_height(600),
_visCursor(false),
_scene(0)
{
// Initialize GLFW
*this << "Initializing...\n";
if(glfwInit())
{
int maj, min, rev;
glfwGetVersion(&maj, &min, &rev);
*this << "GLFW Version " << maj << "." << min << "." << rev << " loaded.\n";
}
else *this << "Unable to load GLFW.\n";
}

............

//-----------------------------------------------------------------------
bool Console::openWindow(const int &width, const int &height, const char *title, bool resizeable)
{
//-----------------------------------------------------------------------
// Already open
if(_open) return false;

// Set properties
glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, _major);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, _minor);
glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, !resizeable);

// Set resolution
_width = width > 0 ? width : _width;
_height = height > 0 ? height : _height;

// Open window and set title
if(!glfwOpenWindow(_width, _height, 0,0,0,0, 32,0, GLFW_WINDOW)) return false;
glfwSetWindowTitle(title);

// Resize
glfwSetWindowSizeCallback(Utils::resize);

//-----------------------------------------------------------------------
// Get OpenGL Version
int major = glfwGetWindowParam(GLFW_OPENGL_VERSION_MAJOR);
int minor = glfwGetWindowParam(GLFW_OPENGL_VERSION_MINOR);
if(major < 3)
{
// Cannot go below 3.0
*this << "OpenGL Version 3.0 is not supported.\n";

closeWindow();
return false;
}
*this << "OpenGL Version " << major << "." << minor << " loaded.\n";

// Initialize GLEW, Normally I check for glewInit() != GLEW_OK
if(glewInit() == GLEW_ERROR_NO_GL_VERSION);
{
*this << "Unable to load GLEW.\n";

//closeWindow(); commented this for testing purposes
//return false;
}
*this << "GLEW Version " << glewGetString(GLEW_VERSION) << " loaded.\n";

//-----------------------------------------------------------------------
// Initialize OpenGL
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

// Culling
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);

// Depth
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LEQUAL);
glDepthRange(0.0f, 1.0f);
glClearDepth(1.0);

// Set Open Flag
return _open = true;
}


I 'replaced' std::cout << by overloading the << operator, so the console outputs:



Initializing...
GLFW Version 2.7.5 loaded.
OpenGL Version 3.0 loaded.
Unable to load GLEW.
GLEW Version 1.7.0 loaded. < shows that because I temporarily disabled the close-at-error thingy


Again, when I start rendering using this setup code it renders fine, on some machines, but of course thats not what I'd prefer :P I scouted the internet for solutions but none worked so far.

Edit: btw, I'm on windows 7 if that matters

Thanks for any help
-Ckef

Ckef01
05-18-2012, 12:15 PM
Ok, so I decided to manually load extensions, the error is gone, but it still doesn't display on all machines, while it did before. It may just be the machine, as the driver got updated on the one that doesn't work anymore, though updating a driver to make it not-work is kind of strange...

Including gl headers and defining extensions:


// WIN32 extensions
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# define glGetProcAddress(name) wglGetProcAddress((LPCSTR)name)
#endif

// Window setup
#define GLFW_NO_GLU
#include "GL/glfw.h"
#include "GL/glext.h"

// CWE extensions
extern PFNGLATTACHSHADERPROC glAttachShader;
extern PFNGLBINDBUFFERPROC glBindBuffer;
extern PFNGLBUFFERDATAPROC glBufferData;
extern PFNGLBUFFERSUBDATAPROC glBufferSubData;
extern PFNGLCOMPILESHADERPROC glCompileShader;
extern PFNGLCREATEPROGRAMPROC glCreateProgram;
extern PFNGLCREATESHADERPROC glCreateShader;
extern PFNGLDELETEBUFFERSPROC glDeleteBuffers;
extern PFNGLDELETEPROGRAMPROC glDeleteProgram;
extern PFNGLDELETESHADERPROC glDeleteShader;
extern PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray;
extern PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray;
extern PFNGLGENBUFFERSPROC glGenBuffers;
extern PFNGLGETATTRIBLOCATIONPROC glGetAttribLocation;
extern PFNGLGETBUFFERSUBDATAPROC glGetBufferSubData;
extern PFNGLGETPROGRAMIVPROC glGetProgramiv;
extern PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
extern PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
extern PFNGLGETSHADERIVPROC glGetShaderiv;
extern PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
extern PFNGLLINKPROGRAMPROC glLinkProgram;
extern PFNGLMAPBUFFERRANGEPROC glMapBufferRange;
extern PFNGLSHADERSOURCEPROC glShaderSource;
extern PFNGLUNIFORM1IPROC glUniform1i;
extern PFNGLUNIFORM3FPROC glUniform3f;
extern PFNGLUNIFORMMATRIX4FVPROC glUniformMatrix4fv;
extern PFNGLUNMAPBUFFERPROC glUnmapBuffer;
extern PFNGLUSEPROGRAMPROC glUseProgram;
extern PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer;

// CWE functions
bool CWE_INIT_EXTENSIONS();


The .cpp file for it:


// CWE (CWorx Engine) Extensions & Functions
//-----------------------------------------------------------------------
PFNGLATTACHSHADERPROC glAttachShader = 0;
PFNGLBINDBUFFERPROC glBindBuffer = 0;
PFNGLBUFFERDATAPROC glBufferData = 0;
PFNGLBUFFERSUBDATAPROC glBufferSubData = 0;
PFNGLCOMPILESHADERPROC glCompileShader = 0;
PFNGLCREATEPROGRAMPROC glCreateProgram = 0;
PFNGLCREATESHADERPROC glCreateShader = 0;
PFNGLDELETEBUFFERSPROC glDeleteBuffers = 0;
PFNGLDELETEPROGRAMPROC glDeleteProgram = 0;
PFNGLDELETESHADERPROC glDeleteShader = 0;
PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray = 0;
PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray = 0;
PFNGLGENBUFFERSPROC glGenBuffers = 0;
PFNGLGETATTRIBLOCATIONPROC glGetAttribLocation = 0;
PFNGLGETBUFFERSUBDATAPROC glGetBufferSubData = 0;
PFNGLGETPROGRAMIVPROC glGetProgramiv = 0;
PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog = 0;
PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog = 0;
PFNGLGETSHADERIVPROC glGetShaderiv = 0;
PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation = 0;
PFNGLLINKPROGRAMPROC glLinkProgram = 0;
PFNGLMAPBUFFERRANGEPROC glMapBufferRange = 0;
PFNGLSHADERSOURCEPROC glShaderSource = 0;
PFNGLUNIFORM1IPROC glUniform1i = 0;
PFNGLUNIFORM3FPROC glUniform3f = 0;
PFNGLUNIFORMMATRIX4FVPROC glUniformMatrix4fv = 0;
PFNGLUNMAPBUFFERPROC glUnmapBuffer = 0;
PFNGLUSEPROGRAMPROC glUseProgram = 0;
PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer = 0;

//-----------------------------------------------------------------------
bool CWE_INIT_EXTENSIONS()
{
bool success = true;

success = (glAttachShader = (PFNGLATTACHSHADERPROC)glGetProcAddress("glAttachShader")) != 0 && success;
success = (glBindBuffer = (PFNGLBINDBUFFERPROC)glGetProcAddress("glBindBuffer")) != 0 && success;
success = (glBufferData = (PFNGLBUFFERDATAPROC)glGetProcAddress("glBufferData")) != 0 && success;
success = (glBufferSubData = (PFNGLBUFFERSUBDATAPROC)glGetProcAddress("glBufferSubData")) != 0 && success;
success = (glCompileShader = (PFNGLCOMPILESHADERPROC)glGetProcAddress("glCompileShader")) != 0 && success;
success = (glCreateProgram = (PFNGLCREATEPROGRAMPROC)glGetProcAddress("glCreateProgram")) != 0 && success;
success = (glCreateShader = (PFNGLCREATESHADERPROC)glGetProcAddress("glCreateShader")) != 0 && success;
success = (glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)glGetProcAddress("glDeleteBuffers")) != 0 && success;
success = (glDeleteProgram = (PFNGLDELETEPROGRAMPROC)glGetProcAddress("glDeleteProgram")) != 0 && success;
success = (glDeleteShader = (PFNGLDELETESHADERPROC)glGetProcAddress("glDeleteShader")) != 0 && success;
success = (glDisableVertexAttribArray = (PFNGLDISABLEVERTEXATTRIBARRAYPROC)glGetProcAddres s("glDisableVertexAttribArray"))!= 0 && success;
success = (glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)glGetProcAddress ("glEnableVertexAttribArray")) != 0 && success;
success = (glGenBuffers = (PFNGLGENBUFFERSPROC)glGetProcAddress("glGenBuffers")) != 0 && success;
success = (glGetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC)glGetProcAddress("glGetAttribLocation")) != 0 && success;
success = (glGetBufferSubData = (PFNGLGETBUFFERSUBDATAPROC)glGetProcAddress("glGetBufferSubData")) != 0 && success;
success = (glGetProgramiv = (PFNGLGETPROGRAMIVPROC)glGetProcAddress("glGetProgramiv")) != 0 && success;
success = (glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)glGetProcAddress("glGetProgramInfoLog")) != 0 && success;
success = (glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)glGetProcAddress("glGetShaderInfoLog")) != 0 && success;
success = (glGetShaderiv = (PFNGLGETSHADERIVPROC)glGetProcAddress("glGetShaderiv")) != 0 && success;
success = (glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)glGetProcAddress("glGetUniformLocation")) != 0 && success;
success = (glLinkProgram = (PFNGLLINKPROGRAMPROC)glGetProcAddress("glLinkProgram")) != 0 && success;
success = (glMapBufferRange = (PFNGLMAPBUFFERRANGEPROC)glGetProcAddress("glMapBufferRange")) != 0 && success;
success = (glShaderSource = (PFNGLSHADERSOURCEPROC)glGetProcAddress("glShaderSource")) != 0 && success;
success = (glUniform1i = (PFNGLUNIFORM1IPROC)glGetProcAddress("glUniform1i")) != 0 && success;
success = (glUniform3f = (PFNGLUNIFORM3FPROC)glGetProcAddress("glUniform3f")) != 0 && success;
success = (glUniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC)glGetProcAddress("glUniformMatrix4fv")) != 0 && success;
success = (glUnmapBuffer = (PFNGLUNMAPBUFFERPROC)glGetProcAddress("glUnmapBuffer")) != 0 && success;
success = (glUseProgram = (PFNGLUSEPROGRAMPROC)glGetProcAddress("glUseProgram")) != 0 && success;
success = (glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)glGetProcAddress("glVertexAttribPointer")) != 0 && success;

return success;
}


And the initialization code:


//-----------------------------------------------------------------------
Console::Console() :
_open(false),
_frameCap(0),
_prevTime(0.0),
_frameTime(0.0),

_major(3),
_minor(0),

_width(800),
_height(600),
_visCursor(false),
_scene(0)
{
// Initialize GLFW
*this << "Initializing...\n";
if(glfwInit())
{
int maj, min, rev;
glfwGetVersion(&maj, &min, &rev);
*this << "GLFW Version " << maj << "." << min << "." << rev << " loaded.\n";
}
else *this << "Unable to load GLFW.\n";
}

......................

//-----------------------------------------------------------------------
bool Console::openWindow(const int &width, const int &height, const char *title, bool resizeable)
{
//-----------------------------------------------------------------------
// Already open
if(_open) return false;

// Set properties
glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, _major);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, _minor);
glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, !resizeable);

// Set resolution
_width = width > 0 ? width : _width;
_height = height > 0 ? height : _height;

// Open window and set title
if(!glfwOpenWindow(_width, _height, 0,0,0,0, 32,0, GLFW_WINDOW)) return false;
glfwSetWindowTitle(title);

// Resize
glfwSetWindowSizeCallback(CWE_RESIZE);

//-----------------------------------------------------------------------
// Get OpenGL Version
int major = glfwGetWindowParam(GLFW_OPENGL_VERSION_MAJOR);
int minor = glfwGetWindowParam(GLFW_OPENGL_VERSION_MINOR);
if(major < 3)
{
// Cannot go below 3.0
*this << "OpenGL Version 3.0 is not supported.\n";

closeWindow();
return false;
}
*this << "OpenGL Version " << major << "." << minor << " loaded.\n";

// Load OpenGL Extensions
if(!CWE_INIT_EXTENSIONS())
{
*this << "Unable to load OpenGL Extensions.\n";

closeWindow();
return false;
}
*this << "OpenGL Extensions loaded.";

//-----------------------------------------------------------------------
// Initialize OpenGL
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

// Culling
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);

// Depth
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LEQUAL);
glDepthRange(0.0f, 1.0f);
glClearDepth(1.0);

// Set Open Flag
return _open = true;
}