-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdining-room.cpp
More file actions
201 lines (182 loc) · 4.51 KB
/
dining-room.cpp
File metadata and controls
201 lines (182 loc) · 4.51 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <GL/glu.h>
#include "Camera.h"
#include "Floor.h"
#include "Room.h"
#include "Shelf.h"
#include "Chair.h"
#include "Table.h"
#include "Fan.h"
#include "Teapot.h"
#include "Objects.h"
#include "Toys.h"
using namespace std;
Floor f;
Room room;
Shelf shelf;
Chair chair;
Table table;
Fan fan;
Teapot teapot;
Objects objects;
Toys toy;
Camera camera({5, 5, 5}, {0, 0, 0});
GLboolean isDragging = false;
GLint dragX, dragY;
GLfloat rotationAngle = 0.0f;
/**
* @brief Method to define the keyboard keys for camera movement
* @param key keyboard key for a particular movement
*/
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'w':
camera.translate(Camera::Direction::FORWARD);
break;
case 's':
camera.translate(Camera::Direction::BACKWARD);
break;
case 'a':
camera.translate(Camera::Direction::LEFT);
break;
case 'd':
camera.translate(Camera::Direction::RIGHT);
break;
case 'r':
camera.reset();
break;
case '[': // ACW roll
camera.rotate(-5);
break;
case ']': // CW roll
camera.rotate(5);
break;
case 'q':
glutLeaveMainLoop();
break;
default:
break;
}
glutPostRedisplay();
}
/**
* @brief Method to define the arrow key motion for camera movement
* @param key keyboard arrow key for vertical motion
*/
void special(int key, int x, int y) {
switch (key) {
case GLUT_KEY_UP:
camera.translate(Camera::Direction::UP);
break;
case GLUT_KEY_DOWN:
camera.translate(Camera::Direction::DOWN);
break;
default:
break;
}
glutPostRedisplay();
}
/**
* @brief Method to define the movement of camera by mouse button
* @param button mouse button for movement of camera
* @param state to show the status of scene whether it is dragged or not
*/
void mouseButton(int button, int state, int x, int y) {
switch (button) {
case GLUT_LEFT_BUTTON: {
if (state == GLUT_DOWN) {
isDragging = true;
dragX = x;
dragY = y;
} else {
isDragging = false;
}
break;
}
case 3:
camera.zoom(1);
break;
case 4:
camera.zoom(-1);
break;
default:
break;
}
glutPostRedisplay();
}
/**
* @brief Method to define the movement of camera by mouse if it is dragged
*/
void mouseMotion(int x, int y) {
if (isDragging) {
camera.rotate(x - dragX, y - dragY);
dragX = x;
dragY = y;
glutPostRedisplay();
}
}
/**
* Method to define the lighting for the scene
*/
void light()
{
GLfloat light1_diffuse[] = {1.0, 1.0, 1.0, 1.0}; /* bright white */
GLfloat light1_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light1_ambient[] = {0.8, 0.8, 0.8, 1.0}; /* soft white */
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);
}
void animate() {
fan.RotateFan();
glutPostRedisplay();
}
/**
* Method to render the scene
*/
void render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0*camera.getZoom(),1.0,1.0,64.0);
glMatrixMode(GL_MODELVIEW);
light();
glLoadIdentity();
camera.lookAt();
f.DrawFloor();
room.DrawRoom();
shelf.DrawShelf();
chair.DrawChair();
table.DrawTable();
objects.DrawObjects();
toy.DrawBall();
teapot.DrawTeapot();
fan.DrawFan();
glutSwapBuffers();
}
/**
* Method to initialise the glut window
*/
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitWindowSize(800, 800);
glutInitWindowPosition(-400, -400);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Dining Room");
light();
glutDisplayFunc(render);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutMouseFunc(mouseButton);
glutMotionFunc(mouseMotion);
glutIdleFunc(animate);
glutMainLoop();
return 0;
}