PDA

View Full Version : Display window opens then exits



nick2price
11-23-2008, 05:21 PM
I get confused knowing what functions to use in my main. I am running a program through my main class, but when i run it, ther program opens and then exits straight away. This is my main class code


#include "stdafx.h"
#include <GL/glut.h>
#include <windows.h>
#include <stdio.h> //for file access
#include <gl\gl.h>
#include "skybox.h"
#include "glbmp.h"

int screen_width=800;
int screen_height=600;

skybox aSkyBox;

GLvoid ReSizeGLScene(GLsizei width, GLsizei height) {

if (height<=0) { height=1; }

glViewport(0,0,width,height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int InitGL(GLvoid) {

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH); //Try GL_FLAT to see the difference
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //Background colour
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST); //Enables depth testing
glDepthFunc(GL_LEQUAL); //Specifies how Depth testing is done
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //Affects quality of colour and texture interpolation
glFrontFace (GL_CCW); //Specifies that the front of a face is defined
//in a Counter Clockwise manner
glCullFace(GL_BACK); //Specifies that the back of faces are not shown
//so OpenGL does not need to draw things twice
glEnable(GL_CULL_FACE); //Enables culling of faces


aSkyBox.Init(); //Initalize the Skybox


return TRUE;
}

int DrawGLScene(GLvoid) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

//this is where stuff is drawn
aSkyBox.draw();

return TRUE;
}

int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screen_width,screen_height);
glutInitWindowPosition(0,0);
glutCreateWindow("Coursework 2");

DrawGLScene();
glutMainLoop();

return(0);
}


As i say, i am a bit confused as to what function i need to use in my main, whether it needs to be somthing like
glutDisplayFunc(DrawGLScene)
Any help in telling me why my program just exits would be great
cheers

James A.
11-24-2008, 01:58 AM
If you want it to stop exiting straight away you need to set up the rendering function in glut properly:

change your rendering function to:

void DrawGLScene(GLvoid) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

//this is where stuff is drawn
aSkyBox.draw();
}

and change you main function to


int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screen_width,screen_height);
glutInitWindowPosition(0,0);
glutCreateWindow("Coursework 2");
glutDisplayFunc(DrawGLScene);
glutMainLoop();

return(0);
}

I think that should stop it exiting straight away:

here is a little tutorial on using GLUT: http://www.lighthouse3d.com/opengl/glut/