PDA

View Full Version : Losing Image On Resize



Tweener
11-21-2002, 10:23 PM
Hey all,
I am creating a very simple scene description language to make life a litte easier. I am reading my data like: objects, light, etc. in from a file. Also I am only using single buffering. My problem is when I resize my window, I lose the image. Do I have to write a reshape function or I am missing some simple command. I have provided my code for reference. Thanks in advance.


#include <windows.h>
#include <gl/Gl.h>
#include <gl/Glu.h>
#include <Gl/Glut.h>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>


using namespace std;

const char *datName = "data.txt";
//ifstream in(datName);
ifstream in;


void renderSDL(ifstream &in)
{
GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f};
GLfloat mat_diffuse[] = { 1.6f, 0.0f, 0.0f, 1.0f};
GLfloat mat_specular[] = {1.0f,1.0f,1.0f,1.0f};
GLfloat mat_shininess[] = {80.0f};

glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); //set initial ambience
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); //set intial diffusion color
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); //set initial specular settings
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); //set initial shininess for objects

GLfloat lightIntenisity[] = { 0.9f, 0.9f, 0.9f, 1.0f};
GLfloat light_position[] = {1.5f, 3.0f, 1.0f, 0.0f};

glLightfv(GL_LIGHT1, GL_POSITION, light_position);
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightIntenisity );

glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glEnable(GL_DEPTH_TEST);

glEnable(GL_LIGHTING); // Turn on lighting
glEnable(GL_LIGHT1); // Turn on light 1

string cmd;

in>>cmd;

while(!in.eof())
{

if(cmd == "ROTATE"){
GLdouble angle, ux, uy, uz;
in>>angle>>ux>>uy>>uz;
glRotatef(angle, ux, uy, uz);
}

else if(cmd == "TRANSLATE"){
GLdouble dx, dy, dz;
in>>dx>>dy>>dz;
glTranslatef(dx, dy, dz);
}

else if(cmd == "SCALE"){
GLdouble sx, sy, sz;
in>>sx>>sy>>sz;
glScalef(sx, sy, sz);
}

else if(cmd == "PLANE"){
GLdouble thickness;
in>>thickness;
glTranslated(0.5,0.5 * thickness, 0.5);
glScaled(5.0, thickness, 5.0);
glutSolidCube(1.0);
}

else if(cmd == "PUSH")
glPushMatrix();

else if(cmd == "POP")
glPopMatrix();

else if(cmd == "CUBE"){
GLdouble size;
in>>size;
glutSolidCube(size);
}

else if(cmd == "TEAPOT"){
GLdouble size;
in>>size;
glutSolidTeapot(size);
}

else if(cmd == "SPHERE"){
GLdouble radius;
in>>radius;
glutSolidSphere(radius, 30, 30);
}


else if(cmd == "TORUS"){
GLdouble innerRadius, outerRadius;
in>>innerRadius>>outerRadius;
glutSolidTorus(innerRadius, outerRadius, 30, 30);
}


else if(cmd == "CONE"){
GLdouble radius, height;
in>>radius>>height;
glutSolidCone(radius, height, 30 , 30);
}


else if(cmd == "ICOSAHEDRON"){
glutSolidIcosahedron();
}

else if(cmd == "OCTAHEDRON"){
glutSolidOctahedron();
}

else if(cmd == "TETRAHEDRON"){
glutSolidTetrahedron();
}

else if(cmd == "DODECAHEDRON"){
glutSolidDodecahedron();
}

else if(cmd == "IDENTITYAFFINE"){
glLoadIdentity();
}

else if(cmd == "!"){
string comment;
getline(in, comment);
}


else
cout << "ERROR KEYWORD "<<cmd<<" NOT DEFINED" << endl;

in >> cmd;
}//end-while loop

glFlush();

}


void myDisplay(void)
{
GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f};
GLfloat mat_diffuse[] = { .6f, 0.6f, 0.6f, 1.0f};
GLfloat mat_specular[] = {1.0f,1.0f,1.0f,1.0f};
GLfloat mat_shininess[] = {80.0f};

glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); //set initial ambience
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); //set intial diffusion color
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); //set initial specular settings
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); //set initial shininess for objects

GLfloat lightIntenisity[] = { 0.9f, 0.9f, 0.9f, 1.0f};

GLfloat light_position[] = {.5f, 1.0f, 1.0f, 0.0f};

glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightIntenisity );


glMatrixMode(GL_PROJECTION);
glLoadIdentity();

double winHt = 1.0;
glOrtho(-winHt*64/48.0, winHt*64/48.0, -winHt, winHt, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(2.3, 1.3, 2, 0, 0.3, 0, 0.0, 1.0, 0.0); //View of scene, "eye"
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

renderSDL(in);

glFlush();

}

void keyboard (unsigned char key, int x, int y)
{
switch (key)
{
case 'q':
case 'Q':
exit(0);
break;

default:
break;
}
}


int main(int argc, char **argv)
{

in.open(datName);

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(840, 680);
glutInitWindowPosition(20, 20);
glutCreateWindow("SDL Static Scene");

glutDisplayFunc(myDisplay);

glutKeyboardFunc(keyboard);

glutMainLoop();

return 0;

}

Gavin
11-22-2002, 12:20 AM
Yes make a reshape func.....

nexusone
11-22-2002, 04:16 AM
Yes, you need to handle a resize, also you should look at some openGL programming examples.

nehe.gamedev.net