PDA

View Full Version : Glut/GL Camera or view point please help



Slong
11-08-2009, 04:39 PM
im writing a program thats the bouncing ball and i need the camera to track the ball as it moves, the ball bounces from the left to the riht of the screen but i cant figure out what matrixmode i need to use to have the camera follow the ball as it moves? i was thinkging perspective but i really have no idea how to get the camera to do this, PLEASE HELP i can post my full code if needed just let me know :) thanks!

AdrianPi
11-08-2009, 06:26 PM
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, aspect, znear, zfar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(posx, posy, posz, ballx, bally, ballz, upx, upy, upz);

where:
"fov" is the field of view in degrees, i.e, 60
"aspect" is the aspect ratio of the viewport, i.e, 640/480 = 1.3333
"znear" is the near clipping plane, ie 1
"zfar" is the far clipping plane, ie 1000
"posx", "posy", "posz" is the position of the camera
"ballx", "bally", "ballz" is the ball position
"upx", "upy", "upz" is the "up" vector (normally 0,1,0 or 0,0,1, depending of what means "up" on your world)

Slong
11-08-2009, 06:39 PM
wow man thats great, once i get time ill implement it and see what i can get working, i assume this goes in the display function that gets called by glDisplayFunc(display) in main correct?

Slong
11-08-2009, 06:50 PM
i tried it but when i run the program, it just shows the blue background, it appears not to draw the ball or any of the other objects(ground and mountains) i can post full code if you like w/e makes it easier

Edit: prior to adding in what you suggested everything worked fine,just cant get the camera to work.

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//gluPerspective(60,1,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(400,400,0,Xpos,Ypos,0,0,1,0);
mountains();
ground();
ball();
glutSwapBuffers();
glFlush();
}

AdrianPi
11-08-2009, 07:17 PM
Are you doing 3D or 2D?

Slong
11-08-2009, 08:00 PM
i am doing 3d

AdrianPi
11-08-2009, 08:15 PM
Then it should work. Post your code, I don't know how to help you otherwise.

Slong
11-08-2009, 08:40 PM
there ya go man, thats everything... it looked like it should myself but idk

#include<stdlib.h>
#include<GL/glut.h>
#include <math.h>

GLsizei ww = 800, wh = 800;
GLfloat Xpos = 75.0, Ypos = 650.0;
GLfloat addToY = -4.;
int timesBounced = 1;
const GLfloat YMAX = 650.0;
const float SQUISHY = 0.3f;
GLfloat Xvel = 85., Yvel = 25.;
GLfloat Xmin = -20.0, Xmax = 20.0;
GLfloat Ymin = -30.8, Ymax = 40.0;

GLfloat light0_pos[] ={1.,2.,3.,1.};
GLfloat diffuse0[]={0.,0,0,1};
GLfloat ambient0[]={.5,.5,.5,1};
GLfloat specular0[]={1,1,1,1};


void mountains();
void ground();
void display();
void reshape(GLsizei w, GLsizei h);
void init();
void ball();
void timer(int n);


int main()
{
int n = 10;
glutInitWindowSize(ww,wh);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("Bouncing Ball");
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutTimerFunc(Xvel,timer,n);
glutMainLoop();
}



void mountains()
{
GLfloat ambient[]={.5,.5,.0,1};
GLfloat diffuse[]={0.3,.5,.0,1};
GLfloat specular[]={0.3,.5,0,1};
//Material
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuse) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specula r);

//First Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(75.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,200,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Second Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(200.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,400,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Third Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(350.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,450,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Fourth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(500.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,350,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Fifth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(600.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,425,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Six Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(750.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,500,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Seven Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(850.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,400,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Eighth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(925.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,315,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Ninth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(1050.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,425,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Tenth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(1175.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,225,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Eleventh Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(1300.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,375,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Twelveth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(1400.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,300,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Thirteenth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(1500.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,450,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Fourteenth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(1625.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,250,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Eleventh Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(1710.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,375,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();
glFlush();

}

void ball()
{
GLfloat ambient[]={1.0,.0,.0,1};
GLfloat diffuse[]={1,0,.0,1};
GLfloat specular[]={1,0,0,1};
//Material
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuse) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specula r);

glPushMatrix();
glLoadIdentity();
glTranslatef(Xpos,Ypos,0.);
//if (Ypos<SQUISHY)
//glScalef(1.0f,1.0f-SQUISHY+Ypos,1.0f);
glRotatef(-90,1.,0.,0.);
glColor3f(1.0,0.,.0);
glutSolidSphere( 25,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();
glFlush();
}


void ground()
{
GLfloat ambient[]={.0,1.,0,1};
GLfloat diffuse[]={0.0,1.,0,1};
GLfloat specular[]={0.0,1.,0,1};
//Material
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuse) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specula r);

glPushMatrix();
glColor3f(.0,1.,0.);
glBegin(GL_POLYGON);
glVertex2f(3000,300);
glVertex2f(-3000,300);
glVertex2f(3000,-300);
glVertex2f(-3000,-300);
glEnd();
glPopMatrix();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//gluPerspective(90,1,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(400,400,0,Xpos,Ypos,0,0,1,0);
mountains();
ground();
ball();
glutSwapBuffers();
glFlush();
}

void init()
{
glClearColor (0.0, 0.7, 1.0, 1.0);
//glColor3f(.5,.5,0.);
glMatrixMode(GL_PROJECTION); //set the clipping space
glLoadIdentity();
glOrtho(-2.,2.0,-2.,2.,-2.,2.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);

//Lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
//glEnable(GL_COLOR_MATERIAL);
glLightfv(GL_LIGHT0,GL_POSITION,light0_pos);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambient0);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse0);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular0);

}

void reshape(GLsizei w, GLsizei h)
{
/* adjust clipping box */

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -300.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

/* adjust viewport and clear */

glViewport(0,0,w,h);
glClearColor (0.0, 0.7, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();

/* set global size for use by drawing routine */

ww = w;
wh = h;

}

//Timer to control the bounce of the ball
void timer(int n)
{
Xpos+=1;
if(Ypos < 300)
{
timesBounced++;
addToY = 3.5;
}
else
if(Ypos > YMAX/timesBounced + 300)
addToY = -3.5;
Ypos += addToY;

if (Ypos<SQUISHY)
glScalef(1.0f,1.0f-SQUISHY+Ypos,1.0f);

glutPostRedisplay();
glutTimerFunc(Xvel/n, timer, n);


}

AdrianPi
11-08-2009, 08:59 PM
Why are you doing

glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);

for every primitive and calling glutReshapeFunc(reshape) by yourself?

AdrianPi
11-08-2009, 09:06 PM
Remove that and do

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90,1,1,1000);
glMatrixMode(GL_MODELVIEW);
gluLookAt(400,400,0,Xpos,Ypos,0,0,1,0);

only on the reshape function, replacing

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -300.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Not that I see, this is bad:

gluLookAt(400,400,0,Xpos,Ypos,0,0,1,0);

you set the position to 400,400,0
and "up" to 0,0,1

For what I see the mountains and the ball lie on the x,y plane, so "up" is 0,0,1, that's ok

try

gluLookAt(400,-400,200, Xpos,Ypos,0, 0,1,0);

to see what happens...

Slong
11-08-2009, 09:22 PM
Alright i made alot of changes, but im still getting just the blue screen... i tried taking out the

glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);

on each but that didnt fix anything, the reason i did it i have no clue but it worked : / any other ideas? here is the updated code

#include<stdlib.h>
#include<GL/glut.h>
#include <math.h>

GLsizei ww = 800, wh = 800;
GLfloat Xpos = 75.0, Ypos = 650.0;
GLfloat addToY = -4.;
int timesBounced = 1;
const GLfloat YMAX = 650.0;
const float SQUISHY = 0.3f;
GLfloat Xvel = 85., Yvel = 25.;
GLfloat Xmin = -20.0, Xmax = 20.0;
GLfloat Ymin = -30.8, Ymax = 40.0;

GLfloat light0_pos[] ={1.,2.,3.,1.};
GLfloat diffuse0[]={0.,0,0,1};
GLfloat ambient0[]={.5,.5,.5,1};
GLfloat specular0[]={1,1,1,1};


void mountains();
void ground();
void display();
void reshape(GLsizei w, GLsizei h);
void init();
void ball();
void timer(int n);


int main()
{
int n = 10;
glutInitWindowSize(ww,wh);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("Bouncing Ball");
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutTimerFunc(Xvel,timer,n);
glutMainLoop();
}



void mountains()
{
GLfloat ambient[]={.5,.5,.0,1};
GLfloat diffuse[]={0.3,.5,.0,1};
GLfloat specular[]={0.3,.5,0,1};
//Material
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuse) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specula r);

//First Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(75.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,200,25,25);
//glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
//glMatrixMode(GL_MODELVIEW);
//glutReshapeFunc(reshape);
glPopMatrix();

//Second Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(200.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,400,25,25);
//glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
//glMatrixMode(GL_MODELVIEW);
//glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Third Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(350.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,450,25,25);
//glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
//glMatrixMode(GL_MODELVIEW);
//glutReshapeFunc(reshape);
glPopMatrix();

//Fourth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(500.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,350,25,25);
//glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
//glMatrixMode(GL_MODELVIEW);
//glutReshapeFunc(reshape);
glPopMatrix();

//Fifth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(600.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,425,25,25);
//glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
//glMatrixMode(GL_MODELVIEW);
//glutReshapeFunc(reshape);
glPopMatrix();

//Six Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(750.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,500,25,25);
//glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
//glMatrixMode(GL_MODELVIEW);
//glutReshapeFunc(reshape);
glPopMatrix();

//Seven Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(850.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,400,25,25);
//glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
//glMatrixMode(GL_MODELVIEW);
//glutReshapeFunc(reshape);
glPopMatrix();

//Eighth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(925.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,315,25,25);
//glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
//glMatrixMode(GL_MODELVIEW);
// glutReshapeFunc(reshape);
glPopMatrix();

//Ninth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(1050.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,425,25,25);
//glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
//glMatrixMode(GL_MODELVIEW);
// glutReshapeFunc(reshape);
glEnd();
glPopMatrix();

//Tenth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(1175.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,225,25,25);
//glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
//glMatrixMode(GL_MODELVIEW);
//glutReshapeFunc(reshape);
glPopMatrix();

//Eleventh Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(1300.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,375,25,25);
//glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
//glMatrixMode(GL_MODELVIEW);
//glutReshapeFunc(reshape);
glPopMatrix();

//Twelveth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(1400.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,300,25,25);
//glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
//glMatrixMode(GL_MODELVIEW);
//glutReshapeFunc(reshape);
glPopMatrix();

//Thirteenth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(1500.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,450,25,25);
//glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
//glMatrixMode(GL_MODELVIEW);
//glutReshapeFunc(reshape);
glPopMatrix();

//Fourteenth Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(1625.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,250,25,25);
//glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
//glMatrixMode(GL_MODELVIEW);
// glutReshapeFunc(reshape);
glPopMatrix();

//Eleventh Mountain
glPushMatrix();
glLoadIdentity();
glTranslatef(1710.,300.,0.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,375,25,25);
//glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
//glMatrixMode(GL_MODELVIEW);
//glutReshapeFunc(reshape);
glPopMatrix();
glFlush();

}

void ball()
{
GLfloat ambient[]={1.0,.0,.0,1};
GLfloat diffuse[]={1,0,.0,1};
GLfloat specular[]={1,0,0,1};
//Material
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuse) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specula r);

glPushMatrix();
glLoadIdentity();
glTranslatef(Xpos,Ypos,0.);
//if (Ypos<SQUISHY)
//glScalef(1.0f,1.0f-SQUISHY+Ypos,1.0f);
glRotatef(-90,1.,0.,0.);
glColor3f(1.0,0.,.0);
glutSolidSphere( 25,25,25);
glOrtho(0.,(GLdouble)ww,0.,(GLdouble)wh, -300.,300.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);
glEnd();
glPopMatrix();
glFlush();
}


void ground()
{
GLfloat ambient[]={.0,1.,0,1};
GLfloat diffuse[]={0.0,1.,0,1};
GLfloat specular[]={0.0,1.,0,1};
//Material
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuse) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specula r);

glPushMatrix();
glColor3f(.0,1.,0.);
glBegin(GL_POLYGON);
glVertex2f(3000,300);
glVertex2f(-3000,300);
glVertex2f(3000,-300);
glVertex2f(-3000,-300);
glEnd();
glPopMatrix();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90,1,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(400,400,0,Xpos,Ypos,0,0,1,0);
mountains();
ground();
ball();
glutSwapBuffers();
glFlush();
}

void init()
{
glClearColor (0.0, 0.7, 1.0, 1.0);
//glColor3f(.5,.5,0.);
glMatrixMode(GL_PROJECTION); //set the clipping space
glLoadIdentity();
glOrtho(-2.,2.0,-2.,2.,-2.,2.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);

//Lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
//glEnable(GL_COLOR_MATERIAL);
glLightfv(GL_LIGHT0,GL_POSITION,light0_pos);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambient0);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse0);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular0);

}

void reshape(GLsizei w, GLsizei h)
{
/* adjust clipping box */
/*
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -300.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); */

//new stuff
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90,1,1,1000);
glMatrixMode(GL_MODELVIEW);
gluLookAt(400,-400,200,Xpos,Ypos,0,0,1,0);

/* adjust viewport and clear */

glViewport(0,0,w,h);
glClearColor (0.0, 0.7, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();

/* set global size for use by drawing routine */

ww = w;
wh = h;

}

//Timer to control the bounce of the ball
void timer(int n)
{
Xpos+=1;
if(Ypos < 300)
{
timesBounced++;
addToY = 3.5;
}
else
if(Ypos > YMAX/timesBounced + 300)
addToY = -3.5;
Ypos += addToY;

if (Ypos<SQUISHY)
glScalef(1.0f,1.0f-SQUISHY+Ypos,1.0f);

glutPostRedisplay();
glutTimerFunc(Xvel/n, timer, n);


}

Slong
11-08-2009, 09:33 PM
alright just made alot of changes shorting up the code a bit, mainly on the primitive part... it seems in my reshape is wher its deciding what is drawn or shown to be drawn... if you comment out the old code at its start and uncomment the one labeled new stuff then you'll see just a blue screen..

AdrianPi
11-09-2009, 06:43 AM
Fixed. You where working in 2D, or 2D and 1/4. ;)

I removed what I told you to remove for each mountain... and also changed some other things. Look for // <--- comments

#include<stdlib.h>
#include<GL/glut.h>
#include <math.h>


GLsizei ww = 800, wh = 800;
GLfloat Xpos = 75.0, Ypos = 650.0;
GLfloat addToY = -4.;
int timesBounced = 1;
const GLfloat YMAX = 650.0;
const float SQUISHY = 0.3f;
GLfloat Xvel = 85., Yvel = 25.;
GLfloat Xmin = -20.0, Xmax = 20.0;
GLfloat Ymin = -30.8, Ymax = 40.0;

GLfloat light0_pos[] ={1.,2.,3.,1.};
GLfloat diffuse0[]={0.,0,0,1};
GLfloat ambient0[]={.5,.5,.5,1};
GLfloat specular0[]={1,1,1,1};


void mountains();
void ground();
void display();
void reshape(GLsizei w, GLsizei h);
void init();
void ball();
void timer(int n);


int main()
{
int n = 10;
glutInitWindowSize(ww,wh);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("Bouncing Ball");
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutTimerFunc(Xvel,timer,n);
glutMainLoop();
}



void mountains()
{
GLfloat ambient[]={.5,.5,.0,1};
GLfloat diffuse[]={0.3,.5,.0,1};
GLfloat specular[]={0.3,.5,0,1};
//Material
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuse) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specula r);

//First Mountain
glPushMatrix();
glTranslatef(75.,300.,-300.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,200,25,25);
glPopMatrix();

//Second Mountain
glPushMatrix();
glTranslatef(200.,300.,-300.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,400,25,25);
glPopMatrix();

//Third Mountain
glPushMatrix();
glTranslatef(350.,300.,-300.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,450,25,25);
glPopMatrix();

//Fourth Mountain
glPushMatrix();
glTranslatef(500.,300.,-300.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,350,25,25);
glPopMatrix();

//Fifth Mountain
glPushMatrix();
glTranslatef(600.,300.,-300.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,425,25,25);
glPopMatrix();

//Six Mountain
glPushMatrix();
glTranslatef(750.,300.,-300.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,500,25,25);
glPopMatrix();

//Seven Mountain
glPushMatrix();
glTranslatef(850.,300.,-300.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,400,25,25);
glPopMatrix();

//Eighth Mountain
glPushMatrix();
glTranslatef(925.,300.,-300.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,315,25,25);
glPopMatrix();

//Ninth Mountain
glPushMatrix();
glTranslatef(1050.,300.,-300.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,425,25,25);
glPopMatrix();

//Tenth Mountain
glPushMatrix();
glTranslatef(1175.,300.,-300.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,225,25,25);
glPopMatrix();

//Eleventh Mountain
glPushMatrix();
glTranslatef(1300.,300.,-300.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,375,25,25);
glPopMatrix();

//Twelveth Mountain
glPushMatrix();
glTranslatef(1400.,300.,-300.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,300,25,25);
glPopMatrix();

//Thirteenth Mountain
glPushMatrix();
glTranslatef(1500.,300.,-300.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,450,25,25);
glPopMatrix();

//Fourteenth Mountain
glPushMatrix();
glTranslatef(1625.,300.,-300.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,250,25,25);
glPopMatrix();

//Eleventh Mountain
glPushMatrix();
glTranslatef(1710.,300.,-300.);
glRotatef(-90,1.,0.,0.);
glNormal3f(0,0,0);
glColor3f(.5,.5,0.);
glutSolidCone( 100,375,25,25);
glPopMatrix();
glFlush();

}

void ball()
{
GLfloat ambient[]={1.0,.0,.0,1};
GLfloat diffuse[]={1,0,.0,1};
GLfloat specular[]={1,0,0,1};
//Material
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuse) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specula r);

glPushMatrix();
//glLoadIdentity();
glTranslatef(Xpos,Ypos,0.);
//if (Ypos<SQUISHY)
//glScalef(1.0f,1.0f-SQUISHY+Ypos,1.0f);
glRotatef(-90,1.,0.,0.);
glColor3f(1.0,0.,.0);
glutSolidSphere( 25,25,25);
glPopMatrix();
glFlush();
}


void ground()
{
GLfloat ambient[]={.0,1.,0,1};
GLfloat diffuse[]={0.0,1.,0,1};
GLfloat specular[]={0.0,1.,0,1};
//Material
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuse) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specula r);

//glPushMatrix();
glColor3f(.0,1.,0.);
glBegin(GL_POLYGON);
glVertex3f(3000,300,3000); // <--- fixed coordinates
glVertex3f(-3000,300,3000);
glVertex3f(-3000,300,-3000);
glVertex3f(3000,300,-3000);
glEnd();
//glPopMatrix();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90,1,1,10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(800,350,500,Xpos,Ypos,0,0,1,0); // <--- now it works
mountains();
ground();
ball();
glutSwapBuffers();
glFlush();
}

void init()
{
glClearColor (0.0, 0.7, 1.0, 1.0);
//glColor3f(.5,.5,0.);
glMatrixMode(GL_PROJECTION); //set the clipping space
glLoadIdentity();
glOrtho(-2.,2.0,-2.,2.,-2.,2.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);

//Lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST); // <--- enabled zbuffer
//glEnable(GL_COLOR_MATERIAL);
glLightfv(GL_LIGHT0,GL_POSITION,light0_pos);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambient0);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse0);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular0);

}

void reshape(GLsizei w, GLsizei h)
{
/* adjust clipping box */

//glMatrixMode(GL_PROJECTION); <--- now done in display
//glLoadIdentity();
//glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -300.0, 300.0);
//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();

/* adjust viewport and clear */

glViewport(0,0,w,h);
glClearColor (0.0, 0.7, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();

/* set global size for use by drawing routine */

ww = w;
wh = h;

}

//Timer to control the bounce of the ball
void timer(int n)
{
Xpos+=1;
if(Ypos < 300+25) // <--- added ball radius
{
timesBounced++;
addToY = 3.5;
}
else
if(Ypos > YMAX/timesBounced + 300+25) // <--- added ball radius
addToY = -3.5;
Ypos += addToY;

if (Ypos<SQUISHY)
glScalef(1.0f,1.0f-SQUISHY+Ypos,1.0f);

glutPostRedisplay();
glutTimerFunc(Xvel/n, timer, n);


}

Slong
11-09-2009, 08:51 AM
alright sweet bro! but im what would i have to do to stop the ball from bouncing so much at the end? like after the 7 bounce or so it just looks like its vibrating the screen the bounces are so small.. i thought maybe setting a bounce counter and after like 8 bounces or whenever the height of the ball reaches a certain point just set the balls position to the point where the last bounce occurred? also as you can see i been trying to get the ball to squish when it hits the ground, decreasing by 1/2 each bounce/hit... what ya think is wrong with that part? your a freakin life savor btw!

Slong
11-09-2009, 08:55 AM
also how can i get the camera to move with the ball? like this view is following it as it moves but the camera itself isnt moving, my goal was to get the camera to move with the ball following it as it goes up and down till the point it stops, basically the background just "scrolls" behind the ball as it moves but the camera is "fixed" on the ball moving with it. if you help on these 2 things id greatly appreciate it

EDIT: maybe this will help, i got this off google but dont truely understand what how it works : /

Now suppose that the camera is fixed to the moving object. The arguments to gluLookAt() are constant and the moving object is rendered without transformation. We move the origin to (-X,-Y) before rendering the scenery:

gluLookAt(...); // fixed arguments
draw_car();
transform(...); // move origin to (-X,-Y)
draw_scenery();

Slong
11-09-2009, 08:59 PM
anyone have a idea?