Revision: 34732
Updated Code
at October 27, 2010 21:01 by autofasurer
Updated Code
#include <GLUT/glut.h>
#include <iostream>
using namespace std;
//initialising variables
float x_positie = 0.0, x_doel = 0.0, x_doel2, x_kracht = 0.02, x_versnelling = -0.001, weerstand = 0.99, doeldist = 0.0;
float y_positie = 0.0, y_doel = 0.0, y_doel2, y_kracht = 0.02, y_versnelling = -0.001, rot = 0.0;
float z_positie = 0.0, z_doel = 0.0, z_doel2, z_kracht = 0.02, z_versnelling = -0.001, t = 0.0;
//initialising states
void init()
{
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
}
//reshaping the viewport and projection
void reshape(int width, int height)
{
glViewport(0, 0, 1024, 768);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.,(GLfloat) width/(GLfloat) height,0.1, 800.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0,0.0,200.0, 0.0, 0.0, 0.0, 0,1,0);
}
//reading the keyboard input
void key(unsigned char key, int x, int y){
switch (key){
case 32:
x_doel2 = (rand()%160-80);
y_doel2 = (rand()%160-80);
z_doel2 = (rand()%160-80);
doeldist = sqrt (pow((x_doel-x_doel2),2.0) + pow((y_doel-y_doel2),2.0) + pow((z_doel-z_doel2),2.0));
break;
}
}
//drawing and displaying
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glRotatef(0.5,0,1,0);
glPushMatrix();
x_positie += x_kracht ;
y_positie += y_kracht ;
z_positie += z_kracht ;
x_versnelling = (x_doel2 - x_positie)*0.0003;
y_versnelling = (y_doel2 - y_positie)*0.0003;
z_versnelling = (z_doel2 - z_positie)*0.0003;
x_kracht += x_versnelling;
y_kracht += y_versnelling;
z_kracht += z_versnelling;
x_kracht *= weerstand;
y_kracht *= weerstand;
z_kracht *= weerstand;
glBegin(GL_LINES);
glVertex3f(x_doel,y_doel,z_doel);
glVertex3f(x_positie,y_positie,z_positie);
glEnd();
glTranslatef(x_positie, y_positie, z_positie);
glColor4f(0.6, 0.0, 0.0, 1.0);
glutSolidSphere(2.0,9,9);
glPopMatrix();
glPushMatrix();
glTranslatef(x_positie+sin(t)*9,y_positie+cos(t)*9,z_positie);
glutSolidSphere(2.0,9,9);
if (t > 6.2829){
t = 0;
}
t += 0.02;
glPopMatrix();
glPushMatrix();
glTranslatef(x_doel, y_doel, z_doel);
glColor4f(0.3,0.3,0.3,0.3);
glutSolidSphere(5.0,15,15);
glPopMatrix();
glutSwapBuffers();
}
//main loop with initialising of window and glut, assigning functions
int main(int argc, char** argv)
{
srand(time(NULL));
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutGameModeString("1024x768:32");
glutEnterGameMode();
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutMainLoop();
return 0;
}
Revision: 34731
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at October 27, 2010 20:58 by autofasurer
Initial Code
#include <GLUT/glut.h>
#include <iostream>
using namespace std;
//initialising variables
float x_positie = 0.0, x_doel = 0.0, x_doel2, x_kracht = 0.02, x_versnelling = -0.001, weerstand = 0.99, doeldist = 0.0;
float y_positie = 0.0, y_doel = 0.0, y_doel2, y_kracht = 0.02, y_versnelling = -0.001, rot = 0.0;
float z_positie = 0.0, z_doel = 0.0, z_doel2, z_kracht = 0.02, z_versnelling = -0.001, t = 0.0;
//initialising states
void init()
{
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
}
//reshaping the viewport and projection
void reshape(int width, int height)
{
glViewport(0, 0, 1024, 768);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.,(GLfloat) width/(GLfloat) height,0.1, 800.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0,0.0,200.0, 0.0, 0.0, 0.0, 0,1,0);
}
//reading the keyboard input
void key(unsigned char key, int x, int y){
switch (key){
case 32:
x_doel2 = (rand()%160-80);
y_doel2 = (rand()%160-80);
z_doel2 = (rand()%160-80);
doeldist = sqrt (pow((x_doel-x_doel2),2.0) + pow((y_doel-y_doel2),2.0) + pow((z_doel-z_doel2),2.0));
break;
}
}
//drawing and displaying
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glRotatef(0.5,0,1,0);
glPushMatrix();
x_positie += x_kracht ; //kracht werkt in op x, y, z positie
y_positie += y_kracht ;
z_positie += z_kracht ;
x_versnelling = (x_doel2 - x_positie)*0.0003;
y_versnelling = (y_doel2 - y_positie)*0.0003;
z_versnelling = (z_doel2 - z_positie)*0.0003;
x_kracht += x_versnelling;
y_kracht += y_versnelling;
z_kracht += z_versnelling;
x_kracht *= weerstand;
y_kracht *= weerstand;
z_kracht *= weerstand;
//x_positie *= weerstand;
//y_positie *= weerstand;
//z_positie *= weerstand;
glBegin(GL_LINES);
glVertex3f(x_doel,y_doel,z_doel);
glVertex3f(x_positie,y_positie,z_positie);
glEnd();
glTranslatef(x_positie, y_positie, z_positie);
glColor4f(0.6, 0.0, 0.0, 1.0);
glutSolidSphere(2.0,9,9);
glPopMatrix();
glPushMatrix();
glTranslatef(x_positie+sin(t)*9,y_positie+cos(t)*9,z_positie);
glutSolidSphere(2.0,9,9);
if (t > 6.2829){
t = 0;
}
t += 0.02;
glPopMatrix();
glPushMatrix();
glTranslatef(x_doel, y_doel, z_doel);
glColor4f(0.3,0.3,0.3,0.3);
glutSolidSphere(5.0,15,15);
glPopMatrix();
glutSwapBuffers();
}
//main loop with initialising of window and glut, assigning functions
int main(int argc, char** argv)
{
srand(time(NULL));
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutGameModeString("1024x768:32");
glutEnterGameMode();
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutMainLoop();
return 0;
}
Initial URL
www.autofasurer.net/wp
Initial Description
Little test to control separate objects in OpenGL using pushMatrix() and popMatrix()
Initial Title
Elastic Spheres
Initial Tags
Initial Language
C++