PDA

View Full Version : Clipping problem



Joyman
11-17-2007, 12:40 PM
Hi folks, I was wondering if one of you with more experience than me would be able to spot the error I am making. The goal of this project is just to animate a ball bouncing. I cannot figure out why but the polygon on which it is above is being drawn in front of the ball when the camera is above and behind the ball when the camera is below (the opposite of what should be happening). Here is my code, any help is greatly appreciated:


/************************************************** **************/
/* OpenGL Bouncing Ball Porgam */
/* Chris Robichaud */
/* University of Massachusetts Lowell */
/* 91.427 Computer Graphics I */
/************************************************** **************/
/*
/* Camera controls:
/* Use w,a,s,d to move the camera around the environment
/* 'q' and 'e' can be used to raise or lower the y value of the camera (though it will still look at the center of the scene
/*
/* Spacebar will pause the ball
/*
/************************************************** **************/

#include "stdafx.h"
#include <gl/glut.h>
#include <math.h>


#define pi 3.14159265


struct Coord3d{
GLfloat x, y, z;
};

/*** Objects ***/
Coord3d camera, center, ball;
GLfloat ball_radius = 1;

/*** Lighting variables ***/
GLfloat lmodel_ambient[] = {0.2, 0.2, 0.2, 1.0};

GLfloat light0_ambient[] = {0.2, 0.2, 0.2, 1.0};
GLfloat light0_diffuse[] = {0.8, 0.8, 0.8, 1.0};
GLfloat light0_specular[] = {0.5, 1, 0.5, 1.0};
GLfloat light0_position[] = {-5, 5, -5, 1.0};

GLfloat ground_ambient[] = {0.3, 0.3, 1, 1};
GLfloat ground_specular[] = {0.3, 1, 0.3, 1};
GLfloat ground_shininess[] = {1};

GLfloat ball_ambient[] = {0.6, 0.3, 0.3, 1};
GLfloat ball_diffuse[] = {1, 1, 1, 1};
GLfloat ball_specular[] = {1, 1, 1, 1};
GLfloat ball_shininess[] = {50};

/*** Misc ***/
GLfloat camera_zoom = 1;
GLfloat camera_rotation_deg = 0;
float time = 0;
int window_size[2] = {800, 600};

bool pause = false;
int camera_view = 0;



void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

//glFrustum(-10, 10, -10, 10, 10, 10); // Defines frustum for the clipping plane
gluPerspective(70, // Field-of-view angle
(GLdouble)window_size[0] / (GLdouble)window_size[1], // aspect ratio
-15, 15); // Near and far clipping-planes

gluLookAt(camera.x, camera.y, camera.z, // Eye coordinates
center.x, center.y, center.z, // Center of scene
0, 1, 0); // Up vector


glRotatef(camera_rotation_deg, 0, 1, 0);
glScalef(camera_zoom, camera_zoom, camera_zoom);

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

glLightfv(GL_LIGHT0, GL_AMBIENT_AND_DIFFUSE, light0_ambient);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);

glPushMatrix();
glRotatef(180, 0, 0, 1);

//glColor3f(0, 0, 1);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, ground_ambient);
glMaterialfv(GL_FRONT, GL_SPECULAR, ground_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, ground_shininess);
glNormal3f(0, 1, 0);
glBegin(GL_QUADS);
glVertex3f(10, 0, 10);
glVertex3f(-10, 0, 10);
glVertex3f(-10, 0, -10);
glVertex3f(10, 0, -10);
glEnd();

glPopMatrix();
glTranslatef(ball.x, ball.y, ball.z);

//glColor3f(1, 0, 0);
glMaterialfv(GL_FRONT, GL_AMBIENT, ball_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, ball_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, ball_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, ball_shininess);
glutSolidSphere(ball_radius, 100, 100);

glutSwapBuffers();

}



void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 'w':
case 'W':
camera_zoom *= 1.1;
break;
case 's':
case 'S':
camera_zoom /= 1.1;
break;
case 'a':
case 'A':
camera_rotation_deg -= 2;
break;
case 'd':
case 'D':
camera_rotation_deg += 2;
break;
case 'q':
case 'Q':
camera.y++;
break;
case 'e':
case 'E':
camera.y--;
break;
case ' ':
pause = !pause;
break;
case 9: // Tab key
camera_view++;

if(camera_view == 1)
{
center.x = ball.x;
center.y = ball.y;
center.z = ball.z;
}
else
{
center.x = 0;
center.y = 0;
center.z = 0;

camera_view = 0;
}

break;
}

printf("key = %c, %d\n", key, key);
glutPostRedisplay();
}



GLfloat getBallHeight(GLfloat t)
{
GLfloat sin_t, max_height = 11;

sin_t = sin(t * pi);

return(max_height * sin_t + ball_radius);
}



void idle()
{

// Calculate position of ball
if(!pause)
{
ball.y = getBallHeight(time);

time += 0.01;
while(time > 1)
time = time - 1;
}

glutPostRedisplay();
}



int main(int argc, char* argv[])
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); // | GLUT_RGBA);
glutInitWindowSize(window_size[0], window_size[1]);
glutInitWindowPosition(400, 400);
glutCreateWindow("Chris Robichaud: OpenGL Bouncing Ball");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);

/*******************************/

camera.x = 0;
camera.y = 10;
camera.z = -50;

center.x = 0;
center.y = 0;
center.z = 0;

ball.x = 0;
ball.y = 5;
ball.z = 0;

glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

//glShadeModel(GL_SMOOTH);

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightfv(GL_LIGHT0, GL_AMBIENT_AND_DIFFUSE, light0_ambient);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);

glClearColor(0, 0, 0, 0);

/*******************************/

glutMainLoop();

return 0;
}

Humus
11-17-2007, 03:59 PM
Near and far plane must be positive. You've put the near plane at -15. Note that this is not world space Z, but in view space. The camera is exactly at 0, and -15 would be behind you, which obviously doesn't make any sense.