PDA

View Full Version : Very new to openGL



Vishan S Gill
07-01-2008, 02:33 AM
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 Matrix

glFlush();
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;
}

Zengar
07-01-2008, 05:12 AM
Can you explain what is going wrong?

Vishan S Gill
07-01-2008, 11:05 PM
The ball is moving but not going on the -X axis at all, it stops at 0 and then go back to +X side, i want the ball to move all over within the cube and even when i turn the cube. I hope you get what i mean? I want to detect when it touches the cube walls and bounce back! thats all im trying to do.
Thanks

dletozeun
07-02-2008, 12:13 AM
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;


here, Xball varies only between 0.7 and 4.3

Vishan S Gill
07-02-2008, 11:26 PM
ok i got it all working now, thanks for the tip! :)
another thing i want to know is if my cube size is 12 and ive a ball of radius 0.7 in the middle, how can i make it bounce around the cube ? thanks

dletozeun
07-03-2008, 12:19 AM
It is not an opengl problem but an algorithm problem...
You may have to check if the ball hit the cube by testing if a point of its surface is close to the cube boundaries, it is very simple!