Sign in to follow this  
NathanRidley

OpenGL Having a lot of trouble getting OpenGL to work

Recommended Posts

NathanRidley    1092

In an effort to not lock out OSX/Linux users from things I hope to create, I'm learning OpenGL, using C++ on Windows 8.1. I'm compiling with CL (MSVC++2013 update 3) from the command line, and using VS2013 for debugging. Unfortunately I am having problems even getting to a stage where I can put a few test vertices on screen.

 

My main problem has been that I keep getting the error "invalid enumerant" when I try to create a vertex shader like so:

auto id = glCreateShader(GL_VERTEX_SHADER);

My initialization code looks like this:

GLFWwindow *window;
if (!glfwInit())
	return 0;

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

window = glfwCreateWindow(width, height, "My App", 0, 0);
if (!window)
{
	glfwTerminate();
	return 0;
}

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
int error = glewInit();

Here's a log dump from my console:

[Graphics] Vendor: NVIDIA Corporation
[Graphics] Renderer: GeForce GTX 670/PCIe/SSE2
[Graphics] Version: 4.1.0 NVIDIA 347.09
[Graphics] GLSL Version: 4.10 NVIDIA via Cg compiler
[Shaders] Compiling shader file: vs-default.glsl
[Shaders] Unable to create shader: invalid enumerant
  • I have the latest driver from NVIDIA
  • My video card supports up to version 4.5, but I have also tried setting 3.2, 3.3, 4.0, 4.1 and 4.4.
  • I'm using GLFW3 and GLEW.
  • I have glewExperimental = GL_TRUE right before glewInit().
  • I have tried setting glewExperimental = GL_FALSE, to no avail

The opengl32.dll in my system32 folder has the following properties:
 
2015-01-19_2233.png
 
I found some old thread from 2011 without much in the way of help, but one person said that GLEW wasn't great with core profile stuff, though that's around three years out of date. Nevertheless, I tried using the glbinding project, but I was unable to get it to work with glfw, as glfw defines __gl_h_, which causes glbinding to break on compile, complaining that it's not compatible with __gl_h_, which I find strange given that the glbinding readme file cites glfw as one of the possible opengl libraries that it can be used with.
 
Anyway, I'm stuck. Roadblocked before I even got a chance to start doing anything with OpenGL. Any help would be appreciated.

Edited by axefrog

Share this post


Link to post
Share on other sites
NathanRidley    1092

Update:

 

After adding some extra logging code, I've discovered that the error is actually originating here:

glewExperimental = GL_TRUE;
glewInit();
err = glGetError();
if(err != GL_NO_ERROR)
{
    logger("Graphics") << "Unable to initialize glew: " << gluErrorString(err) << endlog;
    return 0;
} 

I still don't understand why it's happening though.

Edited by axefrog

Share this post


Link to post
Share on other sites
Ashaman73    13715

glGetError() gets the last error, this must not be necessary the error state of the last command. In your case the error could be a result of a previously executed OGL command. Best to add the error-checking once before analysing certain code part like this:

// start here, if an error occurres here, something went wrong before
err=glGetError();
if(err!=GL_NO_ERROR) ... 


glCommand1();
..
glCommandN():

// check point, if an error occurres here, glComman1 to glCommandN could be the reason.
err=glGetError();
if(err!=GL_NO_ERROR) ... 

Share this post


Link to post
Share on other sites
NathanRidley    1092

@Ashaman - thanks, I discovered this also in the last few minutes, which has helped me solve the problem... so without further ado...

 

SOLVED!

 

Either of these two lines, whether in isolation, or used together, will cause glewInit to fail. I have no idea why.

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

Share this post


Link to post
Share on other sites
mhagain    13430

Instead of using glGetError to check whether GLEW initialization succeeded or failed, you should use the documented and supported way, i.e check the return value from glewInit.  This should return GLEW_OK if GLEW initialized successfully.

 

So, this is basically broken:

glewInit();
err = glGetError();
if(err != GL_NO_ERROR)

And this is the right way:

if (glewInit () != GLEW_OK)

And this is even better again if you want to find out exactly what went wrong:

GLenum err = glewInit();
if (GLEW_OK != err)
{
  /* Problem: glewInit failed, something is seriously wrong. */
  fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  ...
}

Share this post


Link to post
Share on other sites
Sponji    2503

You should check the return value from glewInit instead of using glGetError. Also, if I remember correctly, there's a bug(?) in glew so that its initialization function uses glGetString or something and it causes a silly error because it's not allowed in the context. You can just ignore that by calling simply glGetError once after glewInit.

 

Do something like this:

glewExperimental = GL_TRUE;

GLenum error = glewInit();
glGetError(); // ignore the silly error

if(error != GLEW_OK) {
	// glew failed
}

Edit: That GL_INVALID_ENUM thing is mentioned here: https://www.opengl.org/wiki/OpenGL_Loading_Library#GLEW_.28OpenGL_Extension_Wrangler.29

Edited by Sponji

Share this post


Link to post
Share on other sites
NathanRidley    1092

Ok, it really seems like GLEW is doing something weird internally, because there no GL error exists before glewInit() is called, and glewInit() itself doesn't report an error, but after calling it, there's an error.

 

Here's an excerpt from my code that I used to test this:

log << "Setting window as current context" << endlog;
glfwMakeContextCurrent(window);

// inserted the following to be sure no errors were waiting to be read
auto err = glGetError();
if(err != GL_NO_ERROR)
{
	log << "Unable to make context current: " << gluErrorString(err) << endlog;
	return 0;
}

log << "Initializing GLEW" << endlog;
glewExperimental = GL_TRUE;
int glewError = glewInit();
if(glewError != GLEW_OK)
{
	log << "Failed to initialize glew: " << glewGetErrorString(glewError) << endlog;
	glfwTerminate();
	return 0;
}
err = glGetError();
if(err != GL_NO_ERROR)
{
	log << "Unable to initialize glew: " << gluErrorString(err) << endlog;
	glfwTerminate();
	return 0;
}

Here's the output:

[Graphics] Initializing GLFW
[Graphics] Creating window (1280x768)
[Graphics] Setting window as current context
[Graphics] Initializing GLEW
[Graphics] Unable to initialize glew: invalid enumerant

Quite confused about this, and while I can of course hack around the existence of the error, it bothers me...

Share this post


Link to post
Share on other sites
BitMaster    8651
It's not unlikely that GLEW encounters OpenGL errors during its initialization which do not influence its own outcome. The obvious way would be to check the return value of glewInit (that call must succeed) and then clear the error state by calling glGetError until GL_NO_ERROR is returned (note that glGetError can always return error codes in a row because each call reports the state of a single error flag, as per the second paragraph of the documentation).

Unless 3rd party libraries document that functionality explicitly you should not assume they will leave the OpenGL state in a specific way. Edited by BitMaster

Share this post


Link to post
Share on other sites
Xycaleth    2391

I believe there's a long-standing bug in GLEW where it uses glGetString(GL_EXTENSIONS) to get a list of all the extensions. This has been deprecated since OpenGL 3.1, and (I believe) is removed from the core context completely. This is what's producing the GL error.

Edited by Xycaleth

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
    • By Picpenguin
      Hi
      I'm new to learning OpenGL and still learning C. I'm using SDL2, glew, OpenGL 3.3, linmath and stb_image.
      I started following through learnopengl.com and got through it until I had to load models. The problem is, it uses Assimp for loading models. Assimp is C++ and uses things I don't want in my program (boost for example) and C support doesn't seem that good.
      Things like glVertexAttribPointer and shaders are still confusing to me, but I have to start somewhere right?
      I can't seem to find any good loading/rendering tutorials or source code that is simple to use and easy to understand.
      I have tried this for over a week by myself, searching for solutions but so far no luck. With tinyobjloader-c and project that uses it, FantasyGolfSimulator, I was able to actually load the model with plain color (always the same color no matter what I do) on screen and move it around, but cannot figure out how to use textures or use its multiple textures with it.
      I don't ask much: I just want to load models with textures in them, maybe have lights affect them (directional spotlight etc). Also, some models have multiple parts and multiple textures in them, how can I handle those?
      Are there solutions anywhere?
      Thank you for your time. Sorry if this is a bit confusing, English isn't my native language
  • Popular Now