-
Notifications
You must be signed in to change notification settings - Fork 2
/
PROG4.cpp
103 lines (93 loc) · 2.22 KB
/
PROG4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<GL/glut.h>
using namespace std;
float pts[8][3] = {{-1,-1,-1},{-1,1,-1},{1,1,-1},{1,-1,-1},{-1,-1,1},{-1,1,1},{1,1,1},{1,-1,1}};
float theta[] ={0,0,0};
int axis = 2;
float viewer[]={5,0,0};
void myInit()
{
glMatrixMode(GL_PROJECTION);
glFrustum(-2,2,-2,2,2,10);
glMatrixMode(GL_MODELVIEW);
}
void draw_polygon(int a, int b, int c, int d)
{
glBegin(GL_QUADS);
glVertex3fv(pts[a]);
glVertex3fv(pts[b]);
glVertex3fv(pts[c]);
glVertex3fv(pts[d]);
glEnd();
}
void draw_cube(float pts[8][3])
{
glColor3f(0,0,1);
draw_polygon(0,1,2,3); //front face
glColor3f(0,1,0);
draw_polygon(4,5,6,7); //behind face
glColor3f(1,0,0);
draw_polygon(0,1,5,4); //left face
glColor3f(0,0,0);
draw_polygon(3,2,6,7); //right face
glColor3f(0,1,1);
draw_polygon(0,4,7,3); //bottom face
glColor3f(1,0,1);
draw_polygon(1,5,6,2); //top face
}
void myDisplay()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2],0,0,0,0,1,0);
glRotatef(theta[2],0,0,1);
glRotatef(theta[1],0,1,0);
glRotatef(theta[0],1,0,0);
draw_cube(pts);
glFlush();
glutSwapBuffers();
}
void spincube()
{
theta[axis] = theta[axis]+4;
if(theta[axis]>360)
theta[axis]=0;
glutPostRedisplay();
}
void mouse(int btn , int state , int x , int y)
{
if((btn==GLUT_LEFT_BUTTON)&&(state==GLUT_DOWN))
axis=0;
if((btn==GLUT_RIGHT_BUTTON)&&(state==GLUT_DOWN))
axis=2;
if((btn==GLUT_MIDDLE_BUTTON)&&(state==GLUT_DOWN))
axis=1;
spincube();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='X') viewer[0]+=1;
if(key=='x') viewer[0]-=1;
if(key=='Y') viewer[1]+=1;
if(key=='y') viewer[1]-=1;
if(key=='Z') viewer[2]+=1;
if(key=='z') viewer[2]-=1;
glutPostRedisplay();
}
int main (int argc, char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode( GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowPosition(50,50);
glutInitWindowSize(500,500);
glutCreateWindow("Positioning of Camera");
myInit();
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(myDisplay);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}