My code is breaking when I use the glMatrixMode( GL_PROJECTION ) command, and I think this is because the context is breaking when I try to load a picture using glimg. Here is my code:
#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/TextureGeneratorExceptions.h>
int main()
{
int width, height;
int frame = 0;
bool running = true;
glfwInit();
if( !glfwOpenWindow( 512, 512, 0, 0, 0, 0, 0, 0, GLFW_WINDOW ) )
{
glfwTerminate();
return 0;
}
glfwSetWindowTitle("GLFW Application");
//////////////////////////////////////////////////////////////////////////////////
if(glload::LoadFunctions() == glload::LS_LOAD_FAILED)
{
printf("glload failure.
");
running = false;
return 0;
}
GLuint theTexture = 0;
try{
std::auto_ptr<glimg::ImageSet> pImgSet(glimg::loaders::stb::LoadFromFile("test.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.
");
}
printf("Texture creation successful.
");
/////////////////////////////////////////////////////////////////////////////////
while(running)
{
frame++;
glfwGetWindowSize( &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();
gluPerspective( 65.0f, (GLfloat)width/(GLfloat)height, 1.0f, 100.0f );
// Draw some rotating garbage
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt(0.0f, -10.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f );
//glTranslatef( 1.0f, 1.0f, 0.0f );
glRotatef(frame, 0.25f, 1.0f, 0.75f);
glBegin( GL_TRIANGLES );
glColor3f(0.1f, 0.0f, 0.0f );
glVertex3f(0.0f, 3.0f, -4.0f);
glColor3f(0.0f, 1.0f, 0.0f );
glVertex3f(3.0f, -2.0f, -4.0f);
glColor3f(0.0f, 0.0f, 1.0f );
glVertex3f(-3.0f, -2.0f, -4.0f);
glEnd();
glBegin( GL_TRIANGLES );
glColor3f(0.0f, 0.1f, 0.0f );
glVertex3f(0.0f, 3.0f, -3.0f);
glColor3f(0.0f, 0.0f, 1.0f );
glVertex3f(3.0f, -2.0f, -2.0f);
glColor3f(1.0f, 0.0f, 0.0f );
glVertex3f(-3.0f, -2.0f, 2.0f);
glEnd();
glfwSwapBuffers();
// exit if ESC was pressed or window was closed
running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam( GLFW_OPENED);
}
glfwTerminate();
return 0;
}
If I comment out glMatrixMode( GL_PROJECTION ) to the last glEnd(), the program runs, but I am trying to get the program to load an image as well as display the triangle demo.