PDA

View Full Version : glMatrixMode() is causing my program to crash



Ygyaxthe3rd
05-01-2013, 05:17 PM
I am trying to create a program that loads an image into memory and doesn't crash, but it seems that whenever I load an image the OpenGL context breaks. This program is crashing at the glMatrixMode() (line 62). If I comment out lines 62 to 67 then the program runs without crashing. Here is my code:



1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <memory>
4 #include <glload/gl_all.h>
5 #include <glload/gll.hpp>
6 #include <glimg/glimg.h>
7 #include <GL/glfw.h>
8 #include <glimg/TextureGenerator.h>
9 using namespace glimg;
10
11 int main()
12 {
13 int width, height;
14 int frame = 0;
15 bool running = true;
16
17 glfwInit();
18
19 if( !glfwOpenWindow( 1600, 900, 0, 0, 0, 0, 0, 0, GLFW_FULLSCREEN ) )
20 {
21 glfwTerminate();
22 return 0;
23 }
24
25 glfwSetWindowTitle("GLFW Application");
26
27 //////////////////////////////////////////////////////////////////////////////////
28 if(glload::LoadFunctions() == glload::LS_LOAD_FAILED)
29 {
30 running = false;
31 return 0;
32 }
33
34 GLuint theTexture = 0;
35
36 try
37 {
38 std::auto_ptr<glimg::ImageSet> pImgSet(glimg::loaders::stb::LoadFromFile("TestImage.png"));
39 theTexture = glimg::CreateTexture(pImgSet.get(), 0);
40 }
41 catch(glimg::loaders::stb::StbLoaderException &e)
42 {
43 printf("Image file loading failed.");
44 }
45 catch(glimg::TextureGenerationException &e)
46 {
47 printf("Texture creation failed.");
48 }
49 /////////////////////////////////////////////////////////////////////////////////
50
51 while(running)
52 {
53 frame++;
54
55 glfwGetWindowSize( &width, &height );
56 height = height > 0 ? height : 1;
57
58 glViewport( 0, 0, width, height );
59
60 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
61 glClear( GL_COLOR_BUFFER_BIT );
62 glMatrixMode(GL_PROJECTION);
63 glLoadIdentity();
64 glOrtho(0.0,width,height,0.0,-1.0,1.0);
65 glMatrixMode(GL_MODELVIEW);
66 glLoadIdentity();
67 glfwSwapBuffers();
68
69 // exit if ESC was pressed or window was closed
70 running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam( GLFW_OPENED);
71 }
72
73 glfwTerminate();
74
75 return 0;
76 }

The Little Body
05-01-2013, 06:07 PM
Have you test the values of width and height after the line 55 ?

Note that your 1600x900 window size don't seem me to be a very standard definition
(you have a 1600x1200 screen or something like this ?)

I have tested to compile your code, but have a problem with glload and glimg #includes :(


g++ crash.c -lGL -lglut -lGLU -o crash
crash.c:4:27: erreur fatale: glload/gl_all.h : Aucun fichier ou dossier de ce type
compilation terminée.
make: *** [crash] Erreur 1


and if I comment alls glload #include lines this is the glimg header that is not find :(


g++ crash.c -lGL -lglut -lGLU -o crash
crash.c:6:25: erreur fatale: glimg/glimg.h : Aucun fichier ou dossier de ce type
compilation terminée.
make: *** [crash] Erreur 1

Ygyaxthe3rd
05-01-2013, 06:59 PM
My default screen resolution is 1600x900, so I don't think that is the problem. The program will build and run, but once it reaches the glMatrixMode() line it crashes. The image always loads successfully and the screen fills with the color black but then it crashes.

The Little Body
05-01-2013, 07:03 PM
I confirm, this crash too on my linux box with a 800x600 definition :(
(I have only commented all glload/glimg lines for to can compile the code)

Ygyaxthe3rd
05-01-2013, 07:15 PM
I can't check the screen size right now unfortunately, but I have another version of this program I made using a a smaller window that wasn't fullscreen, and that didn't work for the same reasons as this program. Also, the header file thing is probably because I have the headers for glload and glimg in my compiler's include folder.

The Little Body
05-01-2013, 08:27 PM
This version don't crash my linux box :)



#include <stdlib.h>
#include <stdio.h>
#include <memory>
// #include <glload/gl_all.h>
// #include <glload/gll.hpp>
// #include <glimg/glimg.h>
#include <GL/glfw.h>
// #include <glimg/TextureGenerator.h>
// using namespace glimg;

bool running = false;

void GLFWCALL My_Key_Callback(int key, int action)
{
if ( action == GLFW_PRESS )
{
printf("Key %c (%d) pressed \n", key, key);
if( key == GLFW_KEY_ESC )
running = false;
}
}


int main()
{
int width, height;
int frame = 0;

glfwInit();

if( glfwOpenWindow( 800, 600, 0, 0, 0, 0, 0, 0, GLFW_WINDOW ) != GL_TRUE )
{
printf("glfwOpenWindow() fails :( \n");
glfwTerminate();
return 0;
}
else
running = true;

glfwSetWindowTitle("GLFW Application");
glfwSetKeyCallback(My_Key_Callback);

//////////////////////////////////////////////////////////////////////////////////
/*
if(glload::LoadFunctions() == glload::LS_LOAD_FAILED)
{
running = false;
return 0;
}

GLuint theTexture = 0;

try
{
std::auto_ptr<glimg::ImageSet> pImgSet(glimg::loaders::stb::LoadFromFile("TestImage.png"));
theTexture = glimg::CreateTexture(pImgSet.get(), 0);
}
catch(glimg::loaders::stb::StbLoaderException &e)
{
printf("Image file loading failed.");
}
catch(glimg::TextureGenerationException &e)
{
printf("Texture creation failed.");
}
*/
/////////////////////////////////////////////////////////////////////////////////

while(running == true)
{
frame++;

glfwGetWindowSize( &width, &height );
// printf("window size = %d x %d \n", width, height);
// height = height > 0 ? height : 1;

glViewport( 0, 0, width, height );

glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,width,height,0.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glfwSwapBuffers();

glfwPollEvents();

}

printf("Quit \n");
glfwTerminate();

return 0;
}


This use a GLFW_WINDOW mode (not GLFW_FULLSCREEN) and I have modified the polling of keys press using glfwSetKeyCallback() and glfwPollEvents() because this don't work with glfwGetKey()

=> this is perhaps because of the glfwPollEvents() missing call that this crash ???
(this don't crash with the glfwGetKey() method this only don't intercept keys pressed)



Why use you GLFW instead GLUT ?
(most cross-platform tutorials use GLUT)

The Little Body
05-01-2013, 09:09 PM
Or this perhaps don't crash thank to the use of glfwSetKeyCallback() before the loop ???

Ygyaxthe3rd
05-02-2013, 03:03 PM
*bump*

Your code worked, but it doesn't do what I want it to do. I am trying to create a program that loads and image into memory, then displays the opengl context until you press the escape key. In your version you commented out all of the code that does the image loading, and when I uncommented it and the header file #includes I got the same errors I posted about above. I believe the problem has to do with the opengl context breaking after I load the image, but I cant seem to fix it. Is there any way to recreate the context after I load the image, or is there something else that is causing this program to crash?

The Little Body
05-02-2013, 03:23 PM
Perhaps to make the window creation **after** the image loading ?

cf. move the bloc that initialize the GL context just after the image lmage loading instead before as now :



// make the image creation here at the beginning instead to make it at the end

////////////////////////////////////////////////////////////////////////////////// 28 if(glload::LoadFunctions() == glload::LS_LOAD_FAILED) 29 { 30 running = false; 31 return 0; 32 } 33 34 GLuint theTexture = 0; 35 36 try 37 { 38 std::auto_ptr<glimg::ImageSet> pImgSet(glimg::loaders::stb::LoadFromFile("TestImage.png")); 39 theTexture = glimg::CreateTexture(pImgSet.get(), 0); 40 } 41 catch(glimg::loaders::stb::StbLoaderException &e) 42 { 43 printf("Image file loading failed."); 44 } 45 catch(glimg::TextureGenerationException &e) 46 { 47 printf("Texture creation failed."); 48 } 49 /////////////////////////////////////////////////////////////////////////////////


glfwInit(); if( glfwOpenWindow( 800, 600, 0, 0, 0, 0, 0, 0, GLFW_WINDOW ) != GL_TRUE ) { printf("glfwOpenWindow() fails :( \n"); glfwTerminate(); return 0; } else running = true; glfwSetWindowTitle("GLFW Application"); glfwSetKeyCallback(My_Key_Callback);

// make the image creation is now make at the top of the bloc, not here at the end

Ygyaxthe3rd
05-02-2013, 03:40 PM
I tried moving the image loading and texture creation before the context creation, but it seems that both need to have the context in order to work. The program crashed when it tried loading the image.

The Little Body
05-02-2013, 05:57 PM
How can I install the glload and glimg library ???

Because without them, I can't test the entire code with them ...

The Little Body
05-02-2013, 06:00 PM
How can I install glload and glimg libraries/headers ????

Because without them, I can't test the code with them ...