PDA

View Full Version : Object seems to "disappear"



workgameproper
02-25-2008, 02:01 PM
I am working on designing a 3d football player (very basic). When I rotate/move my player around the screen, it seems to disappear when I try to move it towards the top of the window. I am posting my code, feel free to take it / compile it and please help me figure out what is wrong. I am guessing it has something to do with the camera situation. Thanks in advance:


#include <iostream>
#include <GL/glut.h>
#include "math.h"
#define PI 3.14159265

GLsizei ww, hh;

int rotateX = 0;
int rotateY = 0;
double transX = 0;
float playerX = -1.0; // x-pos (top left)
float playerY = 1.5; // y-pos (top left)
float playerZ = 0; // z-pos (top left)
float deg = 0.0; // angle of player (degrees)
float midX = playerX + 2.0; // midpoint of player (x-coord)
float midY = playerY - 1.5; // midpoint of player (y-coord)
float midZ = playerZ + 1; // midpoint of player (z-coord)
float scaleX = 1.0; // scale x
float scaleY = 1.0; // scale y
float scaleZ = 1.0; // scale z

GLUquadricObj *myHead;

void init()
{
// Set clear color to sky blue
glClearColor(0.0, 0.45, 1.0, 1.0);

glMatrixMode(GL_PROJECTION);
glShadeModel(GL_SMOOTH); //Use smooth shading
glLoadIdentity();
glOrtho(-5.0,5.0,-5.0,5.0,-5.0,5.0);

// Sphere will be used for player head
myHead = gluNewQuadric();
gluQuadricDrawStyle(myHead, GLU_FILL);
}

void reshape(GLsizei w, GLsizei h)
{
// Adjust the clipping box
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

if (w <= h)
glOrtho(-5, 5, -5 * (GLfloat) h/
(GLfloat) w, 5 * (GLfloat) h / (GLfloat) w, -5, 5);
else
glOrtho(-5 * (GLfloat) w / (GLfloat) h,
5 * (GLfloat) w / (GLfloat) h, -5, 5, -5, 5);

glMatrixMode(GL_MODELVIEW);

// Adjust viewport

glViewport(0,0,w,h);

// Set global size that will be used by drawing routine

ww = w;
hh = h;
}

void player()
{
glPushMatrix();

glTranslatef(midX, midY, midZ); // Translate back
glScalef(scaleX, scaleY, scaleZ); // Perform operation
glTranslatef(-midX, -midY, -midZ); // Translate to origin

// Rotate about x-axis
glTranslatef(midX, midY, midZ);
glRotatef(rotateX, 1.0, 0.0, 0.0);
glTranslatef(-midX, -midY, -midZ);

// Rotate about y-axis
glTranslatef(midX, midY, midZ);
glRotatef(rotateY, 0, 1, 0);
glTranslatef(-midX, -midY, -midZ);

glTranslatef(midX, midY, midZ);
glTranslatef(transX, 0, 0);
glTranslatef(-midX, -midY, -midZ);


// Back of body
glBegin(GL_POLYGON);
glColor3f(1.0,0.34,0.0);
glVertex3f(0.5,1.25,0);
glColor3f(0.7,0.04,0.0);
glVertex3f(0.5,1.25,1.45);
glColor3f(1.0,0.34,0.0);
glVertex3f(0.5,-0.85,1.45);
glColor3f(0.7,0.04,0.0);
glVertex3f(0.5,-0.85,0);
glEnd();

// Front of body
glBegin(GL_POLYGON);
glColor3f(1.0,0.34,0.0);
glVertex3f(1.2,1.25,0);
glColor3f(0.7,0.04,0.0);
glVertex3f(1.2,1.25,1.45);
glColor3f(1.0,0.34,0.0);
glVertex3f(1.2,-0.85,1.45);
glColor3f(0.7,0.04,0.0);
glVertex3f(1.2,-0.85,0);
glEnd();

// Right side of body
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,1.0);
glVertex3f(1.2,1.25,0);
glColor3f(0.0,0.0,0.5);
glVertex3f(0.5,1.25,0);
glColor3f(0.0,0.0,1.0);
glVertex3f(0.5,-0.85,0);
glColor3f(0.0,0.0,0.5);
glVertex3f(1.2,-0.85,0);
glEnd();

// Left side of body
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,1.0);
glVertex3f(1.2,1.25,1.45);
glColor3f(0.0,0.0,0.5);
glVertex3f(0.5,1.25,1.45);
glColor3f(0.0,0.0,1.0);
glVertex3f(0.5,-0.85,1.45);
glColor3f(0.0,0.0,0.5);
glVertex3f(1.2,-0.85,1.45);
glEnd();

// Top of body
glBegin(GL_POLYGON);
glVertex3f(0.5,1.25,0.0);
glVertex3f(0.5,1.25,1.45);
glVertex3f(1.2,1.25,1.45);
glVertex3f(1.2,1.25,0.0);
glEnd();

// Player Head
glColor3f(1.0,1.0,1.0);
glTranslatef(0.9,2.0,0.75);
gluSphere(myHead, 0.65, 100, 100);
glTranslatef(-0.9,-2.0,-0.75);

// Left arm (top)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,0.0);
glVertex3f(0.7,0.8,0.0);
glVertex3f(0.7,0.8,2.75);
glVertex3f(1.0,0.8,2.75);
glVertex3f(1.0,0.8,0.0);
glEnd();

// Left arm (bottom)
glBegin(GL_POLYGON);
glVertex3f(0.7,0.65,0.0);
glVertex3f(0.7,0.65,2.75);
glVertex3f(1.0,0.65,2.75);
glVertex3f(1.0,0.65,0.0);
glEnd();

// Left arm (side)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,0.0);
glVertex3f(0.7,0.8,0);
glVertex3f(0.7,0.8,2.75);
glVertex3f(0.7,0.65,2.75);
glVertex3f(0.7,0.65,0);
glEnd();

// Left arm (side)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,0.0);
glVertex3f(1.0,0.8,0);
glVertex3f(1.0,0.8,2.75);
glVertex3f(1.0,0.65,2.75);
glVertex3f(1.0,0.65,0);
glEnd();

// Left arm (end)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,0.0);
glVertex3f(1.0,0.8,2.75);
glVertex3f(0.7,0.8,2.75);
glVertex3f(0.7,0.65,2.75);
glVertex3f(1.0,0.65,2.75);
glEnd();

// Right arm (top)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,0.0);
glVertex3f(0.7,0.8,0.0);
glVertex3f(0.7,0.8,-1.3);
glVertex3f(1.0,0.8,-1.3);
glVertex3f(1.0,0.8,0.0);
glEnd();

// Right arm (bottom)
glBegin(GL_POLYGON);
glVertex3f(0.7,0.65,0.0);
glVertex3f(0.7,0.65,-1.3);
glVertex3f(1.0,0.65,-1.3);
glVertex3f(1.0,0.65,0.0);
glEnd();

// Right arm (side)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,0.0);
glVertex3f(0.7,0.8,0);
glVertex3f(0.7,0.8,-1.3);
glVertex3f(0.7,0.65,-1.3);
glVertex3f(0.7,0.65,0);
glEnd();

// Right arm (side)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,0.0);
glVertex3f(1.0,0.8,0);
glVertex3f(1.0,0.8,-1.3);
glVertex3f(1.0,0.65,-1.3);
glVertex3f(1.0,0.65,0);
glEnd();

// Right arm (end)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,0.0);
glVertex3f(1.0,0.8,-1.3);
glVertex3f(0.7,0.8,-1.3);
glVertex3f(0.7,0.65,-1.3);
glVertex3f(1.0,0.65,-1.3);
glEnd();

// Eyes on face
glColor3f(0.0,0.0,0.0);
glTranslatef(0.35,2.3,0.5);
gluSphere(myHead, 0.1, 100, 100);
glTranslatef(0.0,0.0,0.5);
gluSphere(myHead, 0.1, 100, 100);

// Left leg (left-side)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,1.0);
glVertex3f(0.65,-3.1,0.25);
glVertex3f(0.25,-3.1,0.25);
glVertex3f(0.25,-5.0,0.25);
glVertex3f(0.65,-5.0,0.25);
glEnd();

// Left leg (right-side)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,1.0);
glVertex3f(0.65,-3.1,-0.1);
glVertex3f(0.25,-3.1,-0.1);
glVertex3f(0.25,-5.0,-0.1);
glVertex3f(0.65,-5.0,-0.1);
glEnd();

// Left leg (front)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,1.0);
glVertex3f(0.25,-3.1,-0.1);
glVertex3f(0.25,-3.1,0.25);
glVertex3f(0.25,-5.0,0.25);
glVertex3f(0.25,-5.0,-0.1);
glEnd();

// Left leg (back)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,1.0);
glVertex3f(0.65,-3.1,-0.1);
glVertex3f(0.65,-3.1,0.25);
glVertex3f(0.65,-5.0,0.25);
glVertex3f(0.65,-5.0,-0.1);
glEnd();

// Right leg (left-side)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,1.0);
glVertex3f(0.65,-3.1,-0.45);
glVertex3f(0.25,-3.1,-0.45);
glVertex3f(0.25,-5.0,-0.45);
glVertex3f(0.65,-5.0,-0.45);
glEnd();

// Right leg (right-side)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,1.0);
glVertex3f(0.65,-3.1,-0.8);
glVertex3f(0.25,-3.1,-0.8);
glVertex3f(0.25,-5.0,-0.8);
glVertex3f(0.65,-5.0,-0.8);
glEnd();

// Right leg (front)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,1.0);
glVertex3f(0.25,-3.1,-0.8);
glVertex3f(0.25,-3.1,-0.45);
glVertex3f(0.25,-5.0,-0.45);
glVertex3f(0.25,-5.0,-0.8);
glEnd();

// Right leg (back)
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,1.0);
glVertex3f(0.65,-3.1,-0.8);
glVertex3f(0.65,-3.1,-0.45);
glVertex3f(0.65,-5.0,-0.45);
glVertex3f(0.65,-5.0,-0.8);
glEnd();

glPopMatrix();
}

void keyboardFunc(int key, int x, int y)
{
int alt;

switch(key)
{
// Keyboard arrow 'up'
case GLUT_KEY_UP :
transX -= 0.2;
glutPostRedisplay();
break;

// Keyboard arrow 'down'
case GLUT_KEY_DOWN :
transX += 0.2;
glutPostRedisplay();
break;

// Keyboard arrow 'right'
case GLUT_KEY_RIGHT :
rotateY-=1;
rotateY%=360;
glutPostRedisplay();
break;

// Keyboard arrow 'left'
case GLUT_KEY_LEFT :
rotateY+=1;
rotateY%=360;
glutPostRedisplay();
break;

case GLUT_KEY_F4 :
alt = glutGetModifiers();
// Enable Alt+F4 to close program
if (alt == (GLUT_ACTIVE_ALT))
{
exit(0);
}
break;
}
}

void mouse(int button, int state, int x, int y)
{
// Left mouse button pressed down
if(button == GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN)
{
scaleX-=.1;
scaleY-=.1;
scaleZ-=.1;
}

// Right mouse button pressed down
if(button == GLUT_RIGHT_BUTTON &amp;&amp; state == GLUT_DOWN)
{
scaleX+=.1;
scaleY+=.1;
scaleZ+=.1;
}
}

void display()
{
// Clear matrices for color and depth
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

// Draw player object
player();



glutSwapBuffers();
glFlush();
}

int main(int argc, char** argv)
{
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("3d football player");

init();
glutDisplayFunc(display);


// Reshape if user changes window size
glutReshapeFunc(reshape);

// Enable hidden surface removal
glEnable(GL_DEPTH_TEST);

// Handle keyboard and mouse actions
glutSpecialFunc(keyboardFunc);
glutMouseFunc(mouse);

// Ehen no events are to be handled
glutIdleFunc(display);

glutMainLoop();
}

-NiCo-
02-25-2008, 02:08 PM
You are right. It has got something to do with the camera setup. The last two arguments to you glOrtho calls define the near and far plane of your camera. Everything in front of the near plane and everything behind the far plane gets clipped.

N.

workgameproper
02-25-2008, 02:16 PM
I have tried changing the last 2 arguments in glOrtho to larger numbers in order to place the back plane farther back. But that does not have any effect on the program. The object still disappears when moving to the upper portion of the window.

-NiCo-
02-25-2008, 03:01 PM
Did you make the adjustments to all three glOrtho calls?

N.

workgameproper
02-25-2008, 03:09 PM
Thanks that did the trick Nico!