Hi everyone, let me brief what im trying to do:
I have a cube that will move when i press keys in X Y Z direction. The cube is transparent. Im trying to put a ball inside the cube and let it bounce around in the cube and also when i rotate the cube with the keys. But it seems like ive done somehting wrong for sure. Ive been trying to get it done for two days but im getting nowhere. Please help me if you can.
Thanks in advance.
Below is my code
#include <freeglut.h>
#include <math.h>
double xdir=0;
double ydir=0;
double zdir=0;
double rot=0;;
GLfloat X = 0.0f;
GLfloat Y = 0.0f;
GLfloat Z = 0.0f;
GLfloat rotX = 0.0f;
GLfloat rotY = 0.0f;
GLfloat rotZ = 0.0f;
GLfloat rotLx = 0.0f;
GLfloat rotLy = 0.0f;
GLfloat rotLz = 0.0f;
GLfloat zNear = 0.10;
GLfloat zFar = 100.;
GLfloat Xball=0.0f;
GLfloat Yball=0.0f;
GLfloat Zball=0.0f;
GLfloat border=12;
void glDisplayLines(void);
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColor3f(0.2, 0.8, 0.2);
glTranslatef(Xball,Yball,Zball);
glutSolidSphere(0.7,180,180);
glPopMatrix();
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColor3f(0.8, 0.2, 0.2);
glRotatef(X,1,0,0);
glRotatef(Y,0,1,0);
glRotatef(Z,0,0,1);
glutWireCube(border);
Xball=Xball+xdir *0.05;
if(Xball>= 5-0.7)
xdir=-1;
//If ball touch left-most, change position to right
else if(Xball<0.7)
xdir=1;
Yball = Yball+ydir *0.05;
if (Yball >= 5-0.7)
ydir = -1;
else if (Yball <0.7)
ydir = 1;
Zball = Zball+zdir *0.05;
if(Zball>= 5-0.7)
zdir=-1;
else if(Zball<0.7)
zdir=1;
rot = rot+.5;
if (rot >= 360)
rot = 0;
glLoadIdentity();
glTranslatef(Xball,Yball,Zball);
glRotatef(rot, 0.,0.,0.);
glColor3f(0.2, 0.8, 0.2);
glutSolidSphere(0.7,180,180);glDisable(GL_LINE_STIPPLE); // Disable the line stipple
glPopMatrix(); // Don’t forget to pop the MatrixglFlush();
glutSwapBuffers();
glutPostRedisplay();
}
// This function is called whenever the window size is changed
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h); // Set the viewport
glMatrixMode (GL_PROJECTION); // Set the Matrix mode
glLoadIdentity ();
gluPerspective(75, (GLfloat) w /(GLfloat) h , zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (rotLx, rotLy, 15.0 + rotLz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
// called on special key pressed
void specialKey(int key, int x, int y) {// The keys below are using the gluLookAt() function for navigation
// Check which key is pressed
switch(key) {
case GLUT_KEY_LEFT : // Rotate on x axis
X -= 1.0f;
break;
case GLUT_KEY_RIGHT : // Rotate on x axis (opposite)
X += 1.0f;
break;
case GLUT_KEY_UP : // Rotate on y axis
Y += 1.0f;
break;
case GLUT_KEY_DOWN : // Rotate on y axis (opposite)
Y -= 1.0f;
break;
case GLUT_KEY_PAGE_UP: // Roatae on z axis
Z -= 1.0f;
break;
case GLUT_KEY_PAGE_DOWN:// Roatae on z axis (opposite)
Z += 1.0f;
break;
}
glutPostRedisplay(); // Redraw the scene
}
// Main entry point of the program
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); // Setup display mode to double buffer and RGB color
glutInitWindowSize (600,600); // Set the screen size
glutCreateWindow(“Box”);
init ();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
//glutKeyboardFunc(keyboard); // set window’s key callback
glutSpecialFunc(specialKey); // set window’s to specialKey callback
glutMainLoop();
return 0;
}