SlideShare a Scribd company logo
1 of 5
#include <iostream>
using namespace std;
#include <gl/glut.h>
int X = 0, Y = 1, Z = 0, wire = 0, ObjectLbl, choice=0;
GLfloat a = 0., b = 0., c = 0., x = 0., y = 0., z = 0., spin = 0., x1 = .2, yy1 = .2, z1
= .2, h = 2, w = 2;
//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[] = { 2.0, 0.0, 0.0, 1.0 };
GLfloat mat_diffuse1[] = { 1.0, 0.5, 0.0, 1.0 };
GLfloat mat_diffuse2[] = { 1.5, 0.0, 2.0, 1.0 };
GLfloat mat_diffuse3[] = { 0.3, 0.2, 0.8, 0.0 };
GLfloat mat_diffuse4[] = { 2.0, 0.0, 0.0, 2.0 };
GLfloat mat_diffuse5[] = { 1.5, 0.0, 2.0, 0.0 };
GLfloat mat_diffuse6[] = { 0.0, 2.5, 0.0, 1.0 };
GLfloat mat_diffuse7[] = { 0.0, 2.0, 2.0, 0.0 };
GLfloat mat_diffuse8[] = { 2.0, 2.0, 2.0, 2.0 };
GLfloat mat_diffuse9[] = { 0.0, 0.0, 1.0, 0.0 };
void init(void)
{
glClearColor(.5, .5, .5, .5); //background
glShadeModel(GL_FLAT); //default: SMOOTH
//glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
//glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
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 display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//1
glPushMatrix();
glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse5);
if (choice == 1){ glTranslatef(-.8 + a, .5 + b, 0 + c); }
else{glTranslatef(-.8, .5, 0);}
if (choice == 1 || choice == 0)glRotatef(spin, X, Y, Z);
if (choice == 1 || choice == 0){ glScalef(x1, yy1, z1); }
else{ glScalef(.2, .2, .2); }
glutWireCube(.7);
glPopMatrix();
//2
glPushMatrix();
glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse6);
if (choice == 2){ glTranslatef(-.5 + a, .5 + b, 0 + c); }
else{ glTranslatef(-.5, .5, 0); }
if (choice == 2 || choice == 0)glRotatef(spin, X, Y, Z);
if (choice == 2 || choice == 0){ glScalef(x1, yy1, z1); }
else{ glScalef(.2, .2, .2); }
glutWireTeapot(.3);
glPopMatrix();
//3
glPushMatrix();
glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse7);
if (choice == 3){ glTranslatef(-.2 + a, .5 + b, 0 + c); }
else{ glTranslatef(-.2, .5, 0); }
if (choice == 3 || choice == 0)glRotatef(spin, X, Y, Z);
if (choice == 3 || choice == 0){ glScalef(x1, yy1, z1); }
else{ glScalef(.2, .2, .2); }
glutWireCube(.7);
glPopMatrix();
//4
glPushMatrix();
glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse8);
if (choice == 4){ glTranslatef(.1 + a, .5 + b, 0 + c); }
else{ glTranslatef(.1, .5, 0); }
if (choice == 4 || choice == 0)glRotatef(spin, X, Y, Z);
if (choice == 4 || choice == 0){ glScalef(x1, yy1, z1); }
else{ glScalef(.2, .2, .2); }
glutWireTeapot(.3);
glPopMatrix();
//5
glPushMatrix();
glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse9);
if (choice == 5){ glTranslatef(.4 + a, .5 + b, 0 + c); }
else{ glTranslatef(.4, .5, 0); }
if (choice == 5 || choice == 0)glRotatef(spin, X, Y, Z);
if (choice == 5 || choice == 0){ glScalef(x1, yy1, z1); }
else{ glScalef(.2, .2, .2); }
glutWireCube(.7);
glPopMatrix();
//6
glPushMatrix();
glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse);
if (choice == 6){ glTranslatef(-.8 + a, .0 + b, 0 + c); }
else{ glTranslatef(-.8, .0, 0); }
if (choice == 6 || choice == 0)glRotatef(spin, X, Y, Z);
if (choice == 6 || choice == 0){ glScalef(x1, yy1, z1); }
else{ glScalef(.2, .2, .2); }
if (wire)glutSolidSphere(.5, 16, 8);
else glutWireSphere(.5, 16, 8);
glPopMatrix();
//7
glPushMatrix();
glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse1);
if (choice == 7){ glTranslatef(-.5 + a, .0 + b, 0 + c); }
else{ glTranslatef(-.5, .0, 0); }
if (choice == 7 || choice == 0)glRotatef(spin, X, Y, Z);
if (choice == 7 || choice == 0){ glScalef(x1, yy1, z1); }
else{ glScalef(.2, .2, .2); }
if (wire)glutSolidCube(.7);
else glutWireCube(.7);
glPopMatrix();
//8
glPushMatrix();
glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse2);
if (choice == 8){ glTranslatef(-.2 + a, .0 + b, 0 + c); }
else{ glTranslatef(-.2, .0, 0); }
if (choice == 8 || choice == 0)glRotatef(spin, X, Y, Z);
if (choice == 8 || choice == 0){ glScalef(x1, yy1, z1); }
else{ glScalef(.2, .2, .2); }
if (wire)glutSolidSphere(.5, 16, 8);
else glutWireSphere(.5, 16, 8);
glPopMatrix();
//9
glPushMatrix();
glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse3);
if (choice == 3){ glTranslatef(.1 + a, .0 + b, 0 + c); }
else{ glTranslatef(.1, .0, 0); }
if (choice == 9 || choice == 0)glRotatef(spin, X, Y, Z);
if (choice == 9 || choice == 0){ glScalef(x1, yy1, z1); }
else{ glScalef(.2, .2, .2); }
if (wire)glutSolidCube(.7);
else glutWireCube(.7);
glPopMatrix();
//10
glPushMatrix();
glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse4);
if (choice == 3){ glTranslatef(.4 + a, .0 + b, 0 + c); }
else{ glTranslatef(.4, .0, 0); }
if (choice == 10 || choice == 0)glRotatef(spin, X, Y, Z);
if (choice == 10 || choice == 0){ glScalef(x1, yy1, z1); }
else{ glScalef(.2, .2, .2); }
if (wire)glutSolidSphere(.5, 16, 8);
else glutWireSphere(.5, 16, 8);
glPopMatrix();
glutSwapBuffers();
}
void spinDisplay()
{
spin += 1; if (spin>360.)spin = 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();
}
int ObjectSpin(int x, int y)
{
if (x >= 70 && x <= 230 && y >= 54 && y <= 140){
ObjectLbl = 1; }
if (x >= 273 && x <= 491 && y >= 67 && y <= 140){
ObjectLbl = 2; }
if (x >= 520 && x <= 680 && y >= 54 && y <= 140){
ObjectLbl = 3; }
if (x >= 723 && x <= 941 && y >= 67 && y <= 140){
ObjectLbl = 4; }
if (x >= 970 && x <= 1120 && y >= 54 && y <= 140){
ObjectLbl = 5; }
if (x >= 37 && x <= 260 && y >= 242 && y <= 360){
ObjectLbl = 6; }
if (x >= 296 && x <= 453 && y >= 256 && y <= 344){
ObjectLbl = 7; }
if (x >= 487 && x <= 711 && y >= 242 && y <= 360){
ObjectLbl = 8; }
if (x >= 746 && x <= 903 && y >= 256 && y <= 344){
ObjectLbl = 9; }
if (x >= 937 && x <= 1162 && y >= 242 && y <= 360){
ObjectLbl = 10; }
return ObjectLbl;
}
void mouse(int button, int state, int xx, int yy)
{
glutIdleFunc(spinDisplay);
switch (button)
{
case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { choice = ObjectSpin(xx,yy); X =
1; Y =1, Z = -1; } break;
case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN){ choice = 0; }break;
//glutIdleFunc(NULL);
}
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();
}
//////////////////////////////////////////// END OF PROGRAM
///////////////////////////////////

More Related Content

What's hot

D3 svg & angular
D3 svg & angularD3 svg & angular
D3 svg & angular500Tech
 
The Ring programming language version 1.10 book - Part 64 of 212
The Ring programming language version 1.10 book - Part 64 of 212The Ring programming language version 1.10 book - Part 64 of 212
The Ring programming language version 1.10 book - Part 64 of 212Mahmoud Samir Fayed
 
X2 T05 06 Partial Fractions
X2 T05 06 Partial FractionsX2 T05 06 Partial Fractions
X2 T05 06 Partial FractionsNigel Simmons
 
第13回数学カフェ「素数!!」二次会 LT資料「乱数!!」
第13回数学カフェ「素数!!」二次会 LT資料「乱数!!」第13回数学カフェ「素数!!」二次会 LT資料「乱数!!」
第13回数学カフェ「素数!!」二次会 LT資料「乱数!!」Ken'ichi Matsui
 
The Ring programming language version 1.5.4 book - Part 59 of 185
The Ring programming language version 1.5.4 book - Part 59 of 185The Ring programming language version 1.5.4 book - Part 59 of 185
The Ring programming language version 1.5.4 book - Part 59 of 185Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189Mahmoud Samir Fayed
 
Tabela completa de derivadas e integrais
Tabela completa de derivadas e integraisTabela completa de derivadas e integrais
Tabela completa de derivadas e integraisDiego Rodrigues Vaz
 
Михаил Матросов, Повседневный С++: boost и STL
Михаил Матросов, Повседневный С++: boost и STLМихаил Матросов, Повседневный С++: boost и STL
Михаил Матросов, Повседневный С++: boost и STLSergey Platonov
 
統計的学習の基礎 4章 前半
統計的学習の基礎 4章 前半統計的学習の基礎 4章 前半
統計的学習の基礎 4章 前半Ken'ichi Matsui
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignmentashikul akash
 
Unification and Refactoring of Clones
Unification and Refactoring of ClonesUnification and Refactoring of Clones
Unification and Refactoring of ClonesNikolaos Tsantalis
 
Cg my own programs
Cg my own programsCg my own programs
Cg my own programsAmit Kapoor
 
The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212Mahmoud Samir Fayed
 

What's hot (20)

D3 svg & angular
D3 svg & angularD3 svg & angular
D3 svg & angular
 
Scrollytelling
ScrollytellingScrollytelling
Scrollytelling
 
The Ring programming language version 1.10 book - Part 64 of 212
The Ring programming language version 1.10 book - Part 64 of 212The Ring programming language version 1.10 book - Part 64 of 212
The Ring programming language version 1.10 book - Part 64 of 212
 
X2 T05 06 Partial Fractions
X2 T05 06 Partial FractionsX2 T05 06 Partial Fractions
X2 T05 06 Partial Fractions
 
第13回数学カフェ「素数!!」二次会 LT資料「乱数!!」
第13回数学カフェ「素数!!」二次会 LT資料「乱数!!」第13回数学カフェ「素数!!」二次会 LT資料「乱数!!」
第13回数学カフェ「素数!!」二次会 LT資料「乱数!!」
 
The Ring programming language version 1.5.4 book - Part 59 of 185
The Ring programming language version 1.5.4 book - Part 59 of 185The Ring programming language version 1.5.4 book - Part 59 of 185
The Ring programming language version 1.5.4 book - Part 59 of 185
 
ES6(ES2015) is beautiful
ES6(ES2015) is beautifulES6(ES2015) is beautiful
ES6(ES2015) is beautiful
 
The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189
 
Tabela completa de derivadas e integrais
Tabela completa de derivadas e integraisTabela completa de derivadas e integrais
Tabela completa de derivadas e integrais
 
Ejercicio 211 del libro de baldor
Ejercicio 211 del libro de baldorEjercicio 211 del libro de baldor
Ejercicio 211 del libro de baldor
 
Proga 0622
Proga 0622Proga 0622
Proga 0622
 
Михаил Матросов, Повседневный С++: boost и STL
Михаил Матросов, Повседневный С++: boost и STLМихаил Матросов, Повседневный С++: boost и STL
Михаил Матросов, Повседневный С++: boost и STL
 
統計的学習の基礎 4章 前半
統計的学習の基礎 4章 前半統計的学習の基礎 4章 前半
統計的学習の基礎 4章 前半
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignment
 
Unification and Refactoring of Clones
Unification and Refactoring of ClonesUnification and Refactoring of Clones
Unification and Refactoring of Clones
 
Cg my own programs
Cg my own programsCg my own programs
Cg my own programs
 
The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212
 
Ocr code
Ocr codeOcr code
Ocr code
 
Apuntes clojure
Apuntes clojureApuntes clojure
Apuntes clojure
 
Python speleology
Python speleologyPython speleology
Python speleology
 

Similar to openGl example

#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
 
10CSL67 CG LAB PROGRAM 8
10CSL67 CG LAB PROGRAM 810CSL67 CG LAB PROGRAM 8
10CSL67 CG LAB PROGRAM 8Vanishree Arun
 
CG OpenGL Shadows + Light + Texture -course 10
CG OpenGL Shadows + Light + Texture -course 10CG OpenGL Shadows + Light + Texture -course 10
CG OpenGL Shadows + Light + Texture -course 10fungfung Chen
 
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
 
The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 60 of 185
The Ring programming language version 1.5.4 book - Part 60 of 185The Ring programming language version 1.5.4 book - Part 60 of 185
The Ring programming language version 1.5.4 book - Part 60 of 185Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202Mahmoud Samir Fayed
 
10CSL67 CG LAB PROGRAM 3
10CSL67 CG LAB PROGRAM 310CSL67 CG LAB PROGRAM 3
10CSL67 CG LAB PROGRAM 3Vanishree Arun
 
The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210Mahmoud Samir Fayed
 
Apply physics laws to the circles- When a circle hits one of the sides.pdf
Apply physics laws to the circles- When a circle hits one of the sides.pdfApply physics laws to the circles- When a circle hits one of the sides.pdf
Apply physics laws to the circles- When a circle hits one of the sides.pdfEricG3KColemanf
 
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graphkinan keshkeh
 
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
 
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
 
Ass day2 1_checkerboard...copy in cpp
Ass day2 1_checkerboard...copy in cppAss day2 1_checkerboard...copy in cpp
Ass day2 1_checkerboard...copy in cppRobi Parvez
 
Computer Graphics and Multimedia lab report
Computer Graphics and Multimedia lab reportComputer Graphics and Multimedia lab report
Computer Graphics and Multimedia lab reportBijoy679
 
Ass day3 2_olympic logos
Ass day3 2_olympic logosAss day3 2_olympic logos
Ass day3 2_olympic logosRobi Parvez
 

Similar to openGl example (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)
 
tetris
tetristetris
tetris
 
Transformasi 2 dmensi
Transformasi 2 dmensiTransformasi 2 dmensi
Transformasi 2 dmensi
 
Ssaw08 0624
Ssaw08 0624Ssaw08 0624
Ssaw08 0624
 
10CSL67 CG LAB PROGRAM 8
10CSL67 CG LAB PROGRAM 810CSL67 CG LAB PROGRAM 8
10CSL67 CG LAB PROGRAM 8
 
CG OpenGL Shadows + Light + Texture -course 10
CG OpenGL Shadows + Light + Texture -course 10CG OpenGL Shadows + Light + Texture -course 10
CG OpenGL Shadows + Light + Texture -course 10
 
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
 
The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184
 
The Ring programming language version 1.5.4 book - Part 60 of 185
The Ring programming language version 1.5.4 book - Part 60 of 185The Ring programming language version 1.5.4 book - Part 60 of 185
The Ring programming language version 1.5.4 book - Part 60 of 185
 
The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202
 
10CSL67 CG LAB PROGRAM 3
10CSL67 CG LAB PROGRAM 310CSL67 CG LAB PROGRAM 3
10CSL67 CG LAB PROGRAM 3
 
The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210
 
Apply physics laws to the circles- When a circle hits one of the sides.pdf
Apply physics laws to the circles- When a circle hits one of the sides.pdfApply physics laws to the circles- When a circle hits one of the sides.pdf
Apply physics laws to the circles- When a circle hits one of the sides.pdf
 
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph
 
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
 
Programa de objetos 3 d wire
Programa de objetos 3 d wirePrograma de objetos 3 d wire
Programa de objetos 3 d wire
 
Ass day2 1_checkerboard...copy in cpp
Ass day2 1_checkerboard...copy in cppAss day2 1_checkerboard...copy in cpp
Ass day2 1_checkerboard...copy in cpp
 
Computer Graphics and Multimedia lab report
Computer Graphics and Multimedia lab reportComputer Graphics and Multimedia lab report
Computer Graphics and Multimedia lab report
 
Ass day3 2_olympic logos
Ass day3 2_olympic logosAss day3 2_olympic logos
Ass day3 2_olympic logos
 

openGl example

  • 1. #include <iostream> using namespace std; #include <gl/glut.h> int X = 0, Y = 1, Z = 0, wire = 0, ObjectLbl, choice=0; GLfloat a = 0., b = 0., c = 0., x = 0., y = 0., z = 0., spin = 0., x1 = .2, yy1 = .2, z1 = .2, h = 2, w = 2; //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[] = { 2.0, 0.0, 0.0, 1.0 }; GLfloat mat_diffuse1[] = { 1.0, 0.5, 0.0, 1.0 }; GLfloat mat_diffuse2[] = { 1.5, 0.0, 2.0, 1.0 }; GLfloat mat_diffuse3[] = { 0.3, 0.2, 0.8, 0.0 }; GLfloat mat_diffuse4[] = { 2.0, 0.0, 0.0, 2.0 }; GLfloat mat_diffuse5[] = { 1.5, 0.0, 2.0, 0.0 }; GLfloat mat_diffuse6[] = { 0.0, 2.5, 0.0, 1.0 }; GLfloat mat_diffuse7[] = { 0.0, 2.0, 2.0, 0.0 }; GLfloat mat_diffuse8[] = { 2.0, 2.0, 2.0, 2.0 }; GLfloat mat_diffuse9[] = { 0.0, 0.0, 1.0, 0.0 }; void init(void) { glClearColor(.5, .5, .5, .5); //background glShadeModel(GL_FLAT); //default: SMOOTH //glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient); //glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess); 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 display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //1 glPushMatrix(); glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse5); if (choice == 1){ glTranslatef(-.8 + a, .5 + b, 0 + c); } else{glTranslatef(-.8, .5, 0);} if (choice == 1 || choice == 0)glRotatef(spin, X, Y, Z); if (choice == 1 || choice == 0){ glScalef(x1, yy1, z1); } else{ glScalef(.2, .2, .2); }
  • 2. glutWireCube(.7); glPopMatrix(); //2 glPushMatrix(); glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse6); if (choice == 2){ glTranslatef(-.5 + a, .5 + b, 0 + c); } else{ glTranslatef(-.5, .5, 0); } if (choice == 2 || choice == 0)glRotatef(spin, X, Y, Z); if (choice == 2 || choice == 0){ glScalef(x1, yy1, z1); } else{ glScalef(.2, .2, .2); } glutWireTeapot(.3); glPopMatrix(); //3 glPushMatrix(); glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse7); if (choice == 3){ glTranslatef(-.2 + a, .5 + b, 0 + c); } else{ glTranslatef(-.2, .5, 0); } if (choice == 3 || choice == 0)glRotatef(spin, X, Y, Z); if (choice == 3 || choice == 0){ glScalef(x1, yy1, z1); } else{ glScalef(.2, .2, .2); } glutWireCube(.7); glPopMatrix(); //4 glPushMatrix(); glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse8); if (choice == 4){ glTranslatef(.1 + a, .5 + b, 0 + c); } else{ glTranslatef(.1, .5, 0); } if (choice == 4 || choice == 0)glRotatef(spin, X, Y, Z); if (choice == 4 || choice == 0){ glScalef(x1, yy1, z1); } else{ glScalef(.2, .2, .2); } glutWireTeapot(.3); glPopMatrix(); //5 glPushMatrix(); glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse9); if (choice == 5){ glTranslatef(.4 + a, .5 + b, 0 + c); } else{ glTranslatef(.4, .5, 0); } if (choice == 5 || choice == 0)glRotatef(spin, X, Y, Z); if (choice == 5 || choice == 0){ glScalef(x1, yy1, z1); } else{ glScalef(.2, .2, .2); } glutWireCube(.7); glPopMatrix(); //6 glPushMatrix(); glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse); if (choice == 6){ glTranslatef(-.8 + a, .0 + b, 0 + c); } else{ glTranslatef(-.8, .0, 0); } if (choice == 6 || choice == 0)glRotatef(spin, X, Y, Z);
  • 3. if (choice == 6 || choice == 0){ glScalef(x1, yy1, z1); } else{ glScalef(.2, .2, .2); } if (wire)glutSolidSphere(.5, 16, 8); else glutWireSphere(.5, 16, 8); glPopMatrix(); //7 glPushMatrix(); glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse1); if (choice == 7){ glTranslatef(-.5 + a, .0 + b, 0 + c); } else{ glTranslatef(-.5, .0, 0); } if (choice == 7 || choice == 0)glRotatef(spin, X, Y, Z); if (choice == 7 || choice == 0){ glScalef(x1, yy1, z1); } else{ glScalef(.2, .2, .2); } if (wire)glutSolidCube(.7); else glutWireCube(.7); glPopMatrix(); //8 glPushMatrix(); glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse2); if (choice == 8){ glTranslatef(-.2 + a, .0 + b, 0 + c); } else{ glTranslatef(-.2, .0, 0); } if (choice == 8 || choice == 0)glRotatef(spin, X, Y, Z); if (choice == 8 || choice == 0){ glScalef(x1, yy1, z1); } else{ glScalef(.2, .2, .2); } if (wire)glutSolidSphere(.5, 16, 8); else glutWireSphere(.5, 16, 8); glPopMatrix(); //9 glPushMatrix(); glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse3); if (choice == 3){ glTranslatef(.1 + a, .0 + b, 0 + c); } else{ glTranslatef(.1, .0, 0); } if (choice == 9 || choice == 0)glRotatef(spin, X, Y, Z); if (choice == 9 || choice == 0){ glScalef(x1, yy1, z1); } else{ glScalef(.2, .2, .2); } if (wire)glutSolidCube(.7); else glutWireCube(.7); glPopMatrix(); //10 glPushMatrix(); glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_diffuse4); if (choice == 3){ glTranslatef(.4 + a, .0 + b, 0 + c); } else{ glTranslatef(.4, .0, 0); } if (choice == 10 || choice == 0)glRotatef(spin, X, Y, Z); if (choice == 10 || choice == 0){ glScalef(x1, yy1, z1); } else{ glScalef(.2, .2, .2); } if (wire)glutSolidSphere(.5, 16, 8); else glutWireSphere(.5, 16, 8); glPopMatrix();
  • 4. glutSwapBuffers(); } void spinDisplay() { spin += 1; if (spin>360.)spin = 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(); } int ObjectSpin(int x, int y) { if (x >= 70 && x <= 230 && y >= 54 && y <= 140){ ObjectLbl = 1; } if (x >= 273 && x <= 491 && y >= 67 && y <= 140){ ObjectLbl = 2; } if (x >= 520 && x <= 680 && y >= 54 && y <= 140){ ObjectLbl = 3; } if (x >= 723 && x <= 941 && y >= 67 && y <= 140){ ObjectLbl = 4; } if (x >= 970 && x <= 1120 && y >= 54 && y <= 140){ ObjectLbl = 5; } if (x >= 37 && x <= 260 && y >= 242 && y <= 360){ ObjectLbl = 6; } if (x >= 296 && x <= 453 && y >= 256 && y <= 344){
  • 5. ObjectLbl = 7; } if (x >= 487 && x <= 711 && y >= 242 && y <= 360){ ObjectLbl = 8; } if (x >= 746 && x <= 903 && y >= 256 && y <= 344){ ObjectLbl = 9; } if (x >= 937 && x <= 1162 && y >= 242 && y <= 360){ ObjectLbl = 10; } return ObjectLbl; } void mouse(int button, int state, int xx, int yy) { glutIdleFunc(spinDisplay); switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { choice = ObjectSpin(xx,yy); X = 1; Y =1, Z = -1; } break; case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN){ choice = 0; }break; //glutIdleFunc(NULL); } 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(); } //////////////////////////////////////////// END OF PROGRAM ///////////////////////////////////