Ok I have done alota reading over the past few days and am starting to understand the differences between gluOrtho & gluPerspective.
I have an app I’ve written, and I hope you guys can run it and see what I’m doing wrong. (it’ll be something small).
The first version uses glOrtho ( and a 3D Cube ) & the second uses gluPerspective ( & a 2D Square ) on rotation they both distort the objects into weird shapes.
I’m thinking its something to do with my depth clipping not being set up correctly… In both cases it doesn’t draw the point far away so it makes a weird shape…
Would really really appreciate this as I’m almost there finally lol!
Code 1:
#include <iostream>
#include <time.h>
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
//projection makes things look smaller as they are further away, ortho doesn't
using namespace std;
void initGeneral(void) {
}
void initGL(void) {
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-400, 400, 300, -300, 0, 10); //lrbt nf
//gluPerspective(45.0f,(GLfloat)800/(GLfloat)600,0.1f,100.0f); //FOV Y, aspect, Znear, Zfar
//only objects with a z value of -0.1 to -100 will be drawn. and an object of 1 unit wide at 0.1 will be the full size of the screen
//gluLookAt(-100.0,0.0,-5.0 ,0.0,0.0,0.0 ,0.0, 1.0, 0.0);
glMatrixMode( GL_MODELVIEW ); // Set the view matrix ...
glLoadIdentity();
}
void reshape(int width, int height ) {
glViewport(0, 0, width, height); // Largest possible square
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-400, 400, 300, -300, 0, 10); //lrbt nf
//gluPerspective(45.0f,(GLfloat)800/(GLfloat)600,0.1f,100.0f); //FOV Y, aspect, Znear, Zfar
glMatrixMode( GL_MODELVIEW ); // Set the view matrix ...
glLoadIdentity();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//draw functions. z -1 is closer than z-50, and 50 set in gluPerspective will draw anythiung as far away as -50
//itll also only draw [censored] from -1 to -50 so stuff with 0 wont be drawn
//gluLookAt(0,0, 100, 0.0,0.0,0.0, 0,1,0); //Is Determined by glOrtho
//if eyeZ : set to -, then it goes on the other side of the scene, so [censored] looks flipped...
//the further eyeZ set to + the further away from scene camera is
glPushMatrix();
//glTranslatef(0.0f,0.0f,2.0f); // Move Left 1.5 Units And Into The Screen 6.0
glRotatef(rotateY,1.0f,1.0f,0.0f);
glBegin(GL_QUADS);
glColor3f (0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, -1.0);
glVertex3f(100.0, 0.0, -1.0);
glVertex3f(100.0, 100.0, -1.0);
glVertex3f(0.0, 100.0, -1.0);
glEnd();
glPopMatrix();
rotateY +=0.6f;
rotateX +=0.6f;
//glFlush();
glutSwapBuffers ( );
}
void animate(void) {
/*
currentSystemTime = clock();
timeAnimationStep = ((double)(currentSystemTime - previousSystemTime))/((double) CLOCKS_PER_SEC);
previousSystemTime=currentSystemTime;
if (timeAnimationStep>0.002){
timeGame = ((double)(currentSystemTime - startSystemTime))/((double) CLOCKS_PER_SEC);
if (timeGame>maxTime) endGame();
}
*/
//glutPostRedisplay();
}
void handleKeyPress(unsigned char key, int x, int y) {
}
void handleMouseClick(int button, int state, int x, int y) {
if (state == GLUT_DOWN){ // only do event handling if mouse button pressed (i.e. not at release)
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(400, 200);
glutCreateWindow("SpaceWalker");
//glutMouseFunc(handleMouseClick); // Set function to handle mouse clicks
//glutKeyboardFunc(handleKeyPress); // Set function to handle pressing of key
initGL();
initGeneral();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(display);
glutMainLoop();
}
code 2:
#include <iostream>
#include <time.h>
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
//projection makes things look smaller as they are further away, ortho doesn't
using namespace std;
void initGeneral(void) {
}
void initGL(void) {
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-400, 400, 300, -300, 0, 10); //lrbt nf
gluPerspective(45.0f,(GLfloat)800/(GLfloat)600,0.1f,100.0f); //FOV Y, aspect, Znear, Zfar
//only objects with a z value of -0.1 to -100 will be drawn. and an object of 1 unit wide at 0.1 will be the full size of the screen
//gluLookAt(-100.0,0.0,-5.0 ,0.0,0.0,0.0 ,0.0, 1.0, 0.0);
glMatrixMode( GL_MODELVIEW ); // Set the view matrix ...
glLoadIdentity();
}
void reshape(int width, int height ) {
glViewport(0, 0, width, height); // Largest possible square
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-400, 400, 300, -300, 0, 10); //lrbt nf
gluPerspective(45.0f,(GLfloat)800/(GLfloat)600,0.1f,100.0f); //FOV Y, aspect, Znear, Zfar
glMatrixMode( GL_MODELVIEW ); // Set the view matrix ...
glLoadIdentity();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//draw functions. z -1 is closer than z-50, and 50 set in gluPerspective will draw anythiung as far away as -50
//itll also only draw [censored] from -1 to -50 so stuff with 0 wont be drawn
//gluLookAt(0,0, 100, 0.0,0.0,0.0, 0,1,0); //Is Determined by glOrtho
//if eyeZ : set to -, then it goes on the other side of the scene, so [censored] looks flipped...
//the further eyeZ set to + the further away from scene camera is
glPushMatrix();
glTranslatef(0.0f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glRotatef(rotateY,1.0f,1.0f,0.0f); // Rotate The Triangle On The Y axis
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f( 1.0f,-1.0f, 1.0f); // Right Of Triangle (Front)
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Right)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f( 1.0f,-1.0f, 1.0f); // Left Of Triangle (Right)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f( 1.0f,-1.0f, -1.0f); // Right Of Triangle (Right)
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f( 1.0f,-1.0f, -1.0f); // Left Of Triangle (Back)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(-1.0f,-1.0f, -1.0f); // Right Of Triangle (Back)
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(-1.0f,-1.0f,-1.0f); // Left Of Triangle (Left)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(-1.0f,-1.0f, 1.0f); // Right Of Triangle (Left)
glEnd();
glPopMatrix();
//glFlush();
glutSwapBuffers ( );
}
void animate(void) {
/*
currentSystemTime = clock();
timeAnimationStep = ((double)(currentSystemTime - previousSystemTime))/((double) CLOCKS_PER_SEC);
previousSystemTime=currentSystemTime;
if (timeAnimationStep>0.002){
timeGame = ((double)(currentSystemTime - startSystemTime))/((double) CLOCKS_PER_SEC);
if (timeGame>maxTime) endGame();
}
*/
//glutPostRedisplay();
}
void handleKeyPress(unsigned char key, int x, int y) {
}
void handleMouseClick(int button, int state, int x, int y) {
if (state == GLUT_DOWN){ // only do event handling if mouse button pressed (i.e. not at release)
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(400, 200);
glutCreateWindow("SpaceWalker");
//glutMouseFunc(handleMouseClick); // Set function to handle mouse clicks
//glutKeyboardFunc(handleKeyPress); // Set function to handle pressing of key
initGL();
initGeneral();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(display);
glutMainLoop();
}