More Related Content
Similar to Robot Motion Source code
#includefloat angle, move, scene, roadmove,turn, on=1; int i, st.docx#includefloat angle, move, scene, roadmove,turn, on=1; int i, st.docxajoy21
Similar to Robot Motion Source code (20)
Robot Motion Source code
- 1. #include <iostream>
using namespace std;
#include <gl/glut.h>
int X = 0, Y = 1, Z = 0, wire = 0, ObjectLbl, choice = 0;
GLfloat A = 1, a = 0., b = 0., c = 0., x = 0., y = 0., z = 0., spin = 0., inspin = 0.,
turn = 0., x1 = .0, xx1 = 0, yy1 = .2, z1 = .2, h = 2, w = 2, xmove = 0, ymove = 0;
//notice y1 is a reserved name, so I used yy1 instead
//GLfloat mat_ambient[] = { .0, 1, .0, 1.0 };
//GLfloat mat_specular[] = { .5, .5, 0, 1.0 };
//GLfloat mat_shininess[] = { 200. }; //this needs specular (work together)
GLfloat light0_position[] = { 8., 8., -5., .0 };
GLfloat mat_diffuse[] = {.8,.9,.0};
GLfloat mat_diffuse1[] = { .8, .0, .0 };
GLfloat mat_diffuse2[] = { .1, .0, .0 };
GLfloat mat_diffuse3[] = { .0, .0, .8 };
GLfloat mat_diffuse4[] = { .0, .8, .0 };
void init(void)
{
glClearColor(.0, .6, .9, .5); //background
//glShadeModel(GL_SMOOTH); //default: SMOOTH
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
//glLightfv(GL_LIGHT0, GL_SPECULAR, mat_ambient);
//glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
cout << "nUse:n '?' 'x' 'y' 'z' 'a' 'b' 'c' 'w' 's' 'S' ' ' keys for
animationn" << endl;
glEnable(GL_DEPTH_TEST);
}
void land()
{
glPushMatrix();
glTranslatef(0, -.7, 0);
glScalef(2, 1, 1);
//glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse);
glColor3f(0,2,0); glutSolidCube(1);
glPopMatrix();
}
void mario()
{
glPushMatrix();
glTranslatef(-.95 + x, 0 + y, 0 + z);
glScalef(.05,.1,.05);
glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse2); glutSolidSphere(.5, 10, 10);
glTranslatef(-.1,-.9,0);
glScalef(1,2,1);
glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse1); glutSolidCube(.5);
glTranslatef(-.18, -.4, 0);
glScalef(.15,.3,.1);
glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse3); glutSolidCube(1);
glTranslatef(2.5, 0, 0);
glutSolidCube(1);
glTranslatef(-1.2, 1.5, 0);
glScalef(10, .2, .1);
- 2. glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse1); glutSolidCube(1);
glPopMatrix();
}
void bricks()
{
glPushMatrix();
glTranslatef(-.5, .4, 0);
for (int i = 0; i < 5; i++)
{
glColor3f(.5, .1, .1); glutSolidCube(.15);
glTranslatef(.155, 0, 0);
}
glPopMatrix();
}
void mushroom()
{
glPushMatrix();
glTranslatef(-.19, .12 + a, 0);
//glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse4);
glTranslatef(0,.25,0);
glScalef(.15,.15,.15);
glColor3f(.8, .8, .8); glutSolidCube(.5);
glTranslatef(0, .25, 0);
glScalef(1.5, 1.5, 1.5);
glColor3f(0,.5,0); glutSolidSphere(.25, 100, 100);
glPopMatrix();
}
void arm()
{
glPushMatrix();
glTranslatef(-.408, -.2, 0);
glRotated(spin, x1, 0, 0); glTranslatef(0,-.2,0);
glScalef(.2, .8, .2);
glColor3f(0, 0, 0); if (choice == 0){ glutSolidCube(.5); }
if (choice == 1){ glutWireCube(.5); }
glTranslatef(0, -.38, 0);
glScalef(1.3, .5, 1.3);
glColor3f(0, 0, 1); if (choice == 0){ glutSolidCube(.5); }
if (choice == 1){ glutWireCube(.5); }
glTranslatef(0, 0, 0);
glPopMatrix();
}
void arm2()
{
glPushMatrix();
glTranslatef(.408, -.2, 0);
glRotated(spin*-1, x1, 0, 0); glTranslatef(0, -.2, 0);
glScalef(.2, .8, .2);
glColor3f(0, 0, 0); if (choice == 0){ glutSolidCube(.5); }
if (choice == 1){ glutWireCube(.5); }
glTranslatef(0, -.38, 0);
glScalef(1.3, .5, 1.3);
glColor3f(0, 0, 1); if (choice == 0){ glutSolidCube(.5); }
if (choice == 1){ glutWireCube(.5); }
glPopMatrix();
- 3. }
void leg()
{
glPushMatrix();
glTranslatef(0, -.6, 0);
glRotatef(spin*-1, x1, 0, 0); glTranslatef(-.21, -.2, 0);
glScalef(.15, .9, .15);
glColor3f(0, 0, 0); if (choice == 0){ glutSolidCube(.5); }
if (choice == 1){ glutWireCube(.5); }
glTranslatef(0, -.43, 0);
glScalef(1.3, .7, 1.3);
glColor3f(0, 0, 1); if (choice == 0){ glutSolidCube(.5); }
if (choice == 1){ glutWireCube(.5); }
glTranslatef(0, 0, 0);
glPopMatrix();
}
void leg2()
{
glPushMatrix();
glTranslatef(.0, -.6, 0);
glRotatef(spin, x1, 0, 0); glTranslatef(.21, -.2, 0);
glScalef(.15, .9, .15);
glColor3f(0, 0, 0); if (choice == 0){ glutSolidCube(.5); }
if (choice == 1){ glutWireCube(.5); }
glTranslatef(0, -.43, 0);
glScalef(1.3, .7, 1.3);
glColor3f(0, 0, 1); if (choice == 0){ glutSolidCube(.5); }
if (choice == 1){ glutWireCube(.5); }
glPopMatrix();
}
void head()
{
glScalef(.7, .6, .7);
glColor3f(0, 0, 0); if (choice == 0){ glutSolidCube(.2); }
if (choice == 1){ glutWireCube(.2); }
}
void torso()
{
glTranslatef(0 + xmove, 0 + ymove, 0);
glScalef(.5, .3, .5);
glColor3f(0, 0, 1); if (choice == 0){ glutSolidCube(.5); }
if (choice == 1){ glutWireCube(.5); }
glRotatef(turn=90, xx1, 1, 0);
glTranslatef(0, -.65, 0);
glScalef(1, 4, 1);
glColor3f(1, 0, 0); if (choice == 0){ glutSolidCube(.25); }
if (choice == 1){ glutWireCube(.25); }
glTranslatef(0, .28, 0);
head();
}
void robot()
{
glPushMatrix();
glTranslatef(-.95 + x, 0. + y, 0 + z);
glScalef(.25,.28,.25);
torso();
arm();
arm2();
- 4. leg();
leg2();
glPopMatrix();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
land();
robot();
bricks();
mushroom();
glutSwapBuffers();
}
void movemushroom()
{
a += .0001; if (a > .18) a = .001;
glutPostRedisplay();
}
void spinDisplay()
{
x += .001;
spin += A; if (spin > 100 || spin < -100){ A = A*-1; }
//inspin -= A; if (inspin>100 || inspin < -180){ A = A*-1 }
if (x > .5 && x < .8)
{
y += .001;
}
if (x > .8 && x < 1.1)
{
movemushroom();
y -= .001;
}
if (x > 1.7)
{
x = 0.; y = 0.;
}
glutPostRedisplay();
}
void rotatebody()
{
turn += 1; if (turn>360)turn = 0;
glutPostRedisplay();
}
//void keyboard(unsigned char key, int xx, int yy)
//{
// switch (key)
// {
// case '?': cout << "nUse:n '?' 'x' 'y' 'z' 'a' 'b' 'c' 'w' 's' 'S' ' ' keys
for animationn" << endl; break;
// case 'x': X = 1; Y = Z = 0; break;
// case 'y': Y = 1; X = Z = 0; break;
- 5. // case 'z': Z = 1; X = Y = 0; break;
// case 'a': a += .01; break;
// case 'b': b += .01; break;
// case 'c': c += .01; break;
// case 'A': a -= .01; break;
// case 'B': b -= .01; break;
// case 'C': c -= .01; break;
//
// case 'h': x1 += .1; break;
// case 'v': yy1 += .1; break;
// case 'd': z1 += .1; break;
// case 'H': x1 -= .1; break;
// case 'V': yy1 -= .1; break;
// case 'D': z1 -= .1; break;
//
// case ' ': spin = x = y = z = .0; break;
// case 'w': wire = ++wire % 2; break;
// case 's': glShadeModel(GL_SMOOTH); break;
// case 'S': glShadeModel(GL_FLAT); break;
// }
// glutPostRedisplay();
//}
void keyboard(unsigned char key, int xx, int yy)
{
//glutIdleFunc();
switch (key)
{
case 'x': xmove += .001; break;
case 'y': ymove += .001; break;
case 'z': z += 1.; break;
case 'X': xmove -= .001; break;
case 'Y': ymove -= .001; break;
case 'Z': z -= 1.; break;
case 'l': yy1 += .2; rotatebody(); break;
case 'r': yy1 -= .2; rotatebody(); break;
case 'u': xx1 += .2, yy1 = 0; rotatebody(); break;
case 'd': xx1 -= .2, yy1 = 0; rotatebody(); break;
case 'w': choice = 1; break;
case 's': choice = 0; break;
}
glutPostRedisplay();
}
void mouse(int button, int state, int xx, int yy)
{
glutIdleFunc(spinDisplay);
switch (button)
{
case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { x1 = 1; } break;
case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN){ x = 0; glutIdleFunc(NULL); }
break;
}
glutPostRedisplay();
}
- 6. void reshape(int w, int h)
{
glViewport(0., 0., 1500., 800.);
glLoadIdentity();
gluPerspective(80., (GLfloat)w / (GLfloat)h, .1, 190.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); //important: last line must be glLoadIdentity() for coloring
with openGL
// glTranslatef (.0, .0, -4.); //do not translate at all if glLoadIdentity() is
used
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1200, 700); glutInitWindowPosition(0, 0);
glutCreateWindow("Brian's GLUT Window");
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glutMainLoop();
}