PDA

View Full Version : gluPerspective problem

Ehsan Nekooee
08-14-2009, 10:50 AM
gluPerspective doesn't work in this code, Just shows a white Blank screen, If I change it to glOrtho and it will work.
Any suggestions why perspective doesn't work?

here is the code:

#include "stdafx.h"
#include "glut.h"
#include "math.h"
#include <stdio.h>
const double Xmin = 0.0, Xmax = 800.0;
const double Ymin = 0.0, Ymax = 600.0;

/*---------------- Resize Function ---------------*/

void resize(int w, int h)
{

double scale, center;
double windowXmin, windowXmax, windowYmin, windowYmax;

glViewport( 0, 0, w, h ); // View port uses whole window

w = (w==0) ? 1 : w;
h = (h==0) ? 1 : h;
if ( (Xmax-Xmin)/w < (Ymax-Ymin)/h ) {
scale = ((Ymax-Ymin)/h)/((Xmax-Xmin)/w);
center = (Xmax+Xmin)/2;
windowXmin = center - (center-Xmin)*scale;
windowXmax = center + (Xmax-center)*scale;
windowYmin = Ymin;
windowYmax = Ymax;
}
else {
scale = ((Xmax-Xmin)/w)/((Ymax-Ymin)/h);
center = (Ymax+Ymin)/2;
windowYmin = center - (center-Ymin)*scale;
windowYmax = center + (Ymax-center)*scale;
windowXmin = Xmin;
windowXmax = Xmax;
}

glMatrixMode( GL_PROJECTION );
//glOrtho( windowXmin, windowXmax, windowYmin, windowYmax, -1200, 1200 );
gluPerspective(60.0f,(float)w/(float)h,0,600);
glMatrixMode(GL_MODELVIEW);
}

/*--------------------- show function -------------------*/

void show (){

float v[3][3];
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1,1,1,1);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);

//glDepthFunc(GL_ALWAYS);
glColor4f(1,0,1,0.5);

// Front

glVertex3f(200,500,0);
glColor4f(0,0,1,1);
glVertex3f(200,200,0);
glColor4f(1,1,0,1);
glVertex3f(500,200,0);
glColor4f(0,1,1,1);
glVertex3f(500,500,0);
glEnd();

glColor4f(0,1,0,1);
// Top
glVertex3f(200,500,300);
glColor4f(0.4,0.7,0.3,1);
glVertex3f(200,500,0);
glColor4f(1,0.6,0,1);
glVertex3f(500,500,0);
glColor4f(0.8,0.1,0.4,1);
glVertex3f(500,500,300);
glEnd();

// Right
glColor4f(0,0,1,1);
glVertex3f(500,500,0);
glColor4f(0.6,0.7,0.6,1);
glVertex3f(500,200,0);
glColor4f(0.2,0.5,0.1,1);
glVertex3f(500,200,300);
glColor4f(0,0.5,0.7,1);
glVertex3f(500,500,300);
glEnd();
// glDepthFunc(GL_LEQUAL);
// Left
glColor4f(0.4,1,1,1);
glVertex3f(200,500,300);
glColor4f(0.4,1,0.7,1);
glVertex3f(200,200,300);
glColor4f(0.2,1,0.8,1);
glVertex3f(200,200,0);
glColor4f(1,0,1,1);
glVertex3f(200,500,0);
glEnd();
// glDepthFunc(GL_LEQUAL);

// Back
glColor4f(1,0,0,1);
glVertex3f(200,500,300);
// glColor4f(0.7,0,1,1);
glVertex3f(200,200,300);
// glColor4f(0.4,1,1,1);
glVertex3f(500,200,300);
// glColor4f(1,0.2,1,1);
glVertex3f(500,500,300);
glEnd();
//glDepthFunc(GL_LEQUAL);
// Bottem
glColor4f(0.5,0,0.5,1);
glVertex3f(200,200,0);
glColor4f(1,1,0,1);
glVertex3f(200,200,300);
glColor4f(0,1,0,1);
glVertex3f(500,200,300);
glColor4f(0,1,0.7,1);
glVertex3f(500,200,0);
glEnd();
glPopMatrix();
glutSwapBuffers();
glFlush();
}

int main(int argc, char *argv[])
{
glutInit(&amp;argc,argv);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE |GLUT_DEPTH );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 1024, 768 );
glutCreateWindow("My Window");
glutReshapeFunc( resize );
glutDisplayFunc(show);
glutTimerFunc(0,Timer,0);
glutMainLoop();
return 0;
}

08-14-2009, 10:57 AM
Try to use
gluPerspective(60.0f,(float)w/(float)h,0.1,600);

gluPerspective(60.0f,(float)w/(float)h,0,600);

Ehsan Nekooee
08-14-2009, 11:04 AM
Well, Thanx for your concern.
I changed it but still nothing happening. still nothing shown on the screen.

I am really confused what's happening to my code?

08-14-2009, 11:30 AM
Your model is ver, very, very big ; use this code:

glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//!!glClearColor(1,1,1,1);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glTranslatef( -5.0f, -5.0f, -50.0f );
// glRotatef( fXrot, 1.0f, 0.0f, 0.0f );
//glRotatef( fYrot, 0.0f, 1.0f, 0.0f );
// glRotatef( fZrot, 0.0f, 0.0f, 1.0f );

//glBindTexture( GL_TEXTURE_2D, g_textureID );

glColor4f(1,0,1,0.5);

// Front

glVertex3f(2,5,0);
glColor4f(0,0,1,1);
glVertex3f(2,2,0);
glColor4f(1,1,0,1);
glVertex3f(5,2,0);
glColor4f(0,1,1,1);
glVertex3f(5,5,0);
glEnd();

glColor4f(0,1,0,1);
// Top
glVertex3f(2,5,3);
glColor4f(0.4,0.7,0.3,1);
glVertex3f(2,5,0);
glColor4f(1,0.6,0,1);
glVertex3f(5,5,0);
glColor4f(0.8,0.1,0.4,1);
glVertex3f(5,5,3);
glEnd();

// Right
glColor4f(0,0,1,1);
glVertex3f(5,5,0);
glColor4f(0.6,0.7,0.6,1);
glVertex3f(5,2,0);
glColor4f(0.2,0.5,0.1,1);
glVertex3f(5,2,3);
glColor4f(0,0.5,0.7,1);
glVertex3f(5,5,3);
glEnd();
// glDepthFunc(GL_LEQUAL);
// Left
glColor4f(0.4,1,1,1);
glVertex3f(2,5,3);
glColor4f(0.4,1,0.7,1);
glVertex3f(2,2,3);
glColor4f(0.2,1,0.8,1);
glVertex3f(2,2,0);
glColor4f(1,0,1,1);
glVertex3f(2,5,0);
glEnd();
// glDepthFunc(GL_LEQUAL);

// Back
glColor4f(1,0,0,1);
glVertex3f(2,5,3);
// glColor4f(0.7,0,1,1);
glVertex3f(2,2,3);
// glColor4f(0.4,1,1,1);
glVertex3f(5,2,3);
// glColor4f(1,0.2,1,1);
glVertex3f(5,5,3);
glEnd();
//glDepthFunc(GL_LEQUAL);
// Bottem
glColor4f(0.5,0,0.5,1);
glVertex3f(2,2,0);
glColor4f(1,1,0,1);
glVertex3f(2,2,3);
glColor4f(0,1,0,1);
glVertex3f(5,2,3);
glColor4f(0,1,0.7,1);
glVertex3f(5,2,0);
glEnd();

And my other code too, I think it won't work with yours :)

With rotation, you can see your model ; how did you create that ? Looks nice ^^

Ehsan Nekooee
08-14-2009, 12:32 PM

I don't know how could I use those big scales !

It might sound stupid but I placed a big Dictionary book in front of myself which has about 1000 pages! I looked at it and assumed what might be the edges! Of course these numbers are not the real dictionary dimensions but Really helped me ;)

08-14-2009, 12:39 PM
You could create your models with blender.

Then you can load it in your programm. ^^

I've written a little engine (+exporter), which loads models,textures and animations ;)

Ehsan Nekooee
08-14-2009, 12:50 PM
Can you tell me in a few words what is exactly an engine and how could I build my own?