SlideShare a Scribd company logo
1 of 6
#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);
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();
}
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();
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;
// 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();
}
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();
}

More Related Content

What's hot

ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contr...
ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contr...ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contr...
ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contr...Cyber Security Alliance
 
10CSL67 CG LAB PROGRAM 8
10CSL67 CG LAB PROGRAM 810CSL67 CG LAB PROGRAM 8
10CSL67 CG LAB PROGRAM 8Vanishree Arun
 
10CSL67 CG LAB PROGRAM 10
10CSL67 CG LAB PROGRAM 1010CSL67 CG LAB PROGRAM 10
10CSL67 CG LAB PROGRAM 10Vanishree Arun
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignmentashikul akash
 
Kotlin killed Java stars
Kotlin killed Java starsKotlin killed Java stars
Kotlin killed Java starsMatteo Bonifazi
 
CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)
CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)
CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)jon_bell
 
[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3Seok-joon Yun
 
Quinto Punto Parte B
Quinto Punto Parte BQuinto Punto Parte B
Quinto Punto Parte Bgustavo206
 
Computer graphics programs in c++
Computer graphics programs in c++Computer graphics programs in c++
Computer graphics programs in c++Ankit Kumar
 
Wap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmWap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmKapil Pandit
 
11103500 61599 sagar (1)
11103500 61599 sagar (1)11103500 61599 sagar (1)
11103500 61599 sagar (1)Rounit Roy
 
โปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานโปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานknang
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicalsManoj Chauhan
 
Datastructures asignment
Datastructures asignmentDatastructures asignment
Datastructures asignmentsreekanth3dce
 

What's hot (20)

Primer Punto
Primer PuntoPrimer Punto
Primer Punto
 
Ssaw08 0624
Ssaw08 0624Ssaw08 0624
Ssaw08 0624
 
Tercer Punto
Tercer PuntoTercer Punto
Tercer Punto
 
ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contr...
ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contr...ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contr...
ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contr...
 
10CSL67 CG LAB PROGRAM 8
10CSL67 CG LAB PROGRAM 810CSL67 CG LAB PROGRAM 8
10CSL67 CG LAB PROGRAM 8
 
10CSL67 CG LAB PROGRAM 10
10CSL67 CG LAB PROGRAM 1010CSL67 CG LAB PROGRAM 10
10CSL67 CG LAB PROGRAM 10
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignment
 
Kotlin killed Java stars
Kotlin killed Java starsKotlin killed Java stars
Kotlin killed Java stars
 
CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)
CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)
CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)
 
[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3
 
Quinto Punto Parte B
Quinto Punto Parte BQuinto Punto Parte B
Quinto Punto Parte B
 
Computer graphics programs in c++
Computer graphics programs in c++Computer graphics programs in c++
Computer graphics programs in c++
 
Wap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmWap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithm
 
11103500 61599 sagar (1)
11103500 61599 sagar (1)11103500 61599 sagar (1)
11103500 61599 sagar (1)
 
Promise is a Promise
Promise is a PromisePromise is a Promise
Promise is a Promise
 
Fred
FredFred
Fred
 
โปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานโปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐาน
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicals
 
Funcion matematica
Funcion matematicaFuncion matematica
Funcion matematica
 
Datastructures asignment
Datastructures asignmentDatastructures asignment
Datastructures asignment
 

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.docx#includefloat angle, move, scene, roadmove,turn, on=1; int i, st.docx
#includefloat angle, move, scene, roadmove,turn, on=1; int i, st.docxajoy21
 
Tutorial Open GL (Listing Code)
Tutorial Open GL (Listing Code)Tutorial Open GL (Listing Code)
Tutorial Open GL (Listing Code)Aila Gema Safitri
 
Ass day3 1_bd flag
Ass day3 1_bd flagAss day3 1_bd flag
Ass day3 1_bd flagRobi Parvez
 
Computer Graphics and Multimedia lab report
Computer Graphics and Multimedia lab reportComputer Graphics and Multimedia lab report
Computer Graphics and Multimedia lab reportBijoy679
 
The Ring programming language version 1.7 book - Part 60 of 196
The Ring programming language version 1.7 book - Part 60 of 196The Ring programming language version 1.7 book - Part 60 of 196
The Ring programming language version 1.7 book - Part 60 of 196Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 57 of 189
The Ring programming language version 1.6 book - Part 57 of 189The Ring programming language version 1.6 book - Part 57 of 189
The Ring programming language version 1.6 book - Part 57 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 54 of 181
The Ring programming language version 1.5.2 book - Part 54 of 181The Ring programming language version 1.5.2 book - Part 54 of 181
The Ring programming language version 1.5.2 book - Part 54 of 181Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 66 of 212
The Ring programming language version 1.10 book - Part 66 of 212The Ring programming language version 1.10 book - Part 66 of 212
The Ring programming language version 1.10 book - Part 66 of 212Mahmoud Samir Fayed
 
square.cpp Open
 square.cpp Open square.cpp Open
square.cpp OpenMikeEly930
 
Programa de objetos 3 d wire
Programa de objetos 3 d wirePrograma de objetos 3 d wire
Programa de objetos 3 d wireRené Domínguez
 
The Ring programming language version 1.5.4 book - Part 54 of 185
The Ring programming language version 1.5.4 book - Part 54 of 185The Ring programming language version 1.5.4 book - Part 54 of 185
The Ring programming language version 1.5.4 book - Part 54 of 185Mahmoud Samir Fayed
 
animation.#include windows.h#include GLglut.h#inclu.pdf
animation.#include windows.h#include GLglut.h#inclu.pdfanimation.#include windows.h#include GLglut.h#inclu.pdf
animation.#include windows.h#include GLglut.h#inclu.pdfambritgames
 
The Ring programming language version 1.5.1 book - Part 53 of 180
The Ring programming language version 1.5.1 book - Part 53 of 180The Ring programming language version 1.5.1 book - Part 53 of 180
The Ring programming language version 1.5.1 book - Part 53 of 180Mahmoud Samir Fayed
 
Disney Effects: Building web/mobile castle in OpenGL 2D & 3D
Disney Effects: Building web/mobile castle in OpenGL 2D & 3DDisney Effects: Building web/mobile castle in OpenGL 2D & 3D
Disney Effects: Building web/mobile castle in OpenGL 2D & 3DSVWB
 
Ass day2 2_rotating my name (robi)
Ass day2 2_rotating my name (robi)Ass day2 2_rotating my name (robi)
Ass day2 2_rotating my name (robi)Robi Parvez
 

Similar to Robot Motion Source code (20)

#includefloat angle, move, scene, roadmove,turn, on=1; int i, st.docx
#includefloat angle, move, scene, roadmove,turn, on=1; int i, st.docx#includefloat angle, move, scene, roadmove,turn, on=1; int i, st.docx
#includefloat angle, move, scene, roadmove,turn, on=1; int i, st.docx
 
Tutorial Open GL (Listing Code)
Tutorial Open GL (Listing Code)Tutorial Open GL (Listing Code)
Tutorial Open GL (Listing Code)
 
Practica
PracticaPractica
Practica
 
Transformasi 2 dmensi
Transformasi 2 dmensiTransformasi 2 dmensi
Transformasi 2 dmensi
 
Ass day3 1_bd flag
Ass day3 1_bd flagAss day3 1_bd flag
Ass day3 1_bd flag
 
Computer Graphics and Multimedia lab report
Computer Graphics and Multimedia lab reportComputer Graphics and Multimedia lab report
Computer Graphics and Multimedia lab report
 
The Ring programming language version 1.7 book - Part 60 of 196
The Ring programming language version 1.7 book - Part 60 of 196The Ring programming language version 1.7 book - Part 60 of 196
The Ring programming language version 1.7 book - Part 60 of 196
 
The Ring programming language version 1.6 book - Part 57 of 189
The Ring programming language version 1.6 book - Part 57 of 189The Ring programming language version 1.6 book - Part 57 of 189
The Ring programming language version 1.6 book - Part 57 of 189
 
The Ring programming language version 1.5.2 book - Part 54 of 181
The Ring programming language version 1.5.2 book - Part 54 of 181The Ring programming language version 1.5.2 book - Part 54 of 181
The Ring programming language version 1.5.2 book - Part 54 of 181
 
The Ring programming language version 1.10 book - Part 66 of 212
The Ring programming language version 1.10 book - Part 66 of 212The Ring programming language version 1.10 book - Part 66 of 212
The Ring programming language version 1.10 book - Part 66 of 212
 
square.cpp Open
 square.cpp Open square.cpp Open
square.cpp Open
 
Robot by gulnaz
Robot by gulnazRobot by gulnaz
Robot by gulnaz
 
Robot by gulnaz
Robot by gulnazRobot by gulnaz
Robot by gulnaz
 
Programa de objetos 3 d wire
Programa de objetos 3 d wirePrograma de objetos 3 d wire
Programa de objetos 3 d wire
 
The Ring programming language version 1.5.4 book - Part 54 of 185
The Ring programming language version 1.5.4 book - Part 54 of 185The Ring programming language version 1.5.4 book - Part 54 of 185
The Ring programming language version 1.5.4 book - Part 54 of 185
 
animation.#include windows.h#include GLglut.h#inclu.pdf
animation.#include windows.h#include GLglut.h#inclu.pdfanimation.#include windows.h#include GLglut.h#inclu.pdf
animation.#include windows.h#include GLglut.h#inclu.pdf
 
The Ring programming language version 1.5.1 book - Part 53 of 180
The Ring programming language version 1.5.1 book - Part 53 of 180The Ring programming language version 1.5.1 book - Part 53 of 180
The Ring programming language version 1.5.1 book - Part 53 of 180
 
Komputer Grafik
Komputer GrafikKomputer Grafik
Komputer Grafik
 
Disney Effects: Building web/mobile castle in OpenGL 2D & 3D
Disney Effects: Building web/mobile castle in OpenGL 2D & 3DDisney Effects: Building web/mobile castle in OpenGL 2D & 3D
Disney Effects: Building web/mobile castle in OpenGL 2D & 3D
 
Ass day2 2_rotating my name (robi)
Ass day2 2_rotating my name (robi)Ass day2 2_rotating my name (robi)
Ass day2 2_rotating my name (robi)
 

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