-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
358 lines (289 loc) · 11.1 KB
/
main.cpp
File metadata and controls
358 lines (289 loc) · 11.1 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <math.h>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
void drawSemiCircle(GLfloat x, GLfloat y, GLfloat radius, GLint numberOfSides);
void drawCircle(GLfloat x, GLfloat y, GLfloat radius, GLint numberOfSides);
void drawCircleFill(float cx, float cy, float r, int num_segments);
int main(void)
{
GLFWwindow* window;
// Initialize the library
if (!glfwInit())
{
return -1;
}
// Create a windowed mode window and its OpenGL context
window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Our 2-Dimensional House", NULL, NULL); // "Polygon" is title of window
if (!window)
{
glfwTerminate();
return -1;
}
// Make the window's context current
glfwMakeContextCurrent(window);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glClearColor(1.0, 1.0, 1.0, 1.0);
glViewport(0.0f, 0.0f, SCREEN_WIDTH, SCREEN_HEIGHT); // specifies the part of the window to which OpenGL will draw (in pixels), convert from normalised to pixels
glMatrixMode(GL_PROJECTION); // projection matrix defines the properties of the camera that views the objects in the world coordinate frame. Here you typically set the zoom factor, aspect ratio and the near and far clipping planes
glLoadIdentity(); // replace the current matrix with the identity matrix and starts us a fresh because matrix transforms such as glOrpho and glRotate cumulate, basically puts us at (0, 0, 0)
glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 1); // essentially set coordinate system
glMatrixMode(GL_MODELVIEW); // (default matrix mode) modelview matrix defines how your objects are transformed (meaning translation, rotation and scaling) in your world
glLoadIdentity(); // same as above comment
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
GLfloat polygonVertices[] =
{
200, 240, 0, // 1
160, 240, 0, // 2
160, 300, 0, // 3
480, 300, 0,// 4
480, 240, 0,// 5
440, 240, 0,// 6
440, 120, 0,// 7
200, 120, 0,// 8
};
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glLineWidth(1);
GLfloat door[] =
{
280, 120, 0,
280, 220, 0, // X, Y , Z Format
360, 220, 0,// Follows on from above point (1) in CLOCK-WISE MANNER
360, 120, 0,
};
GLfloat window1[] =
{
220, 180, 0,
220, 220, 0, // X, Y , Z Format
260, 220, 0,// Follows on from above point (1) in CLOCK-WISE MANNER
260, 180, 0,
};
GLfloat window2[] =
{
380, 180, 0,
380, 220, 0, // X, Y , Z Format
420, 220, 0,// Follows on from above point (1) in CLOCK-WISE MANNER
420, 180, 0,
};
GLfloat line1_left[] =
{
240, 180, 0,
240, 220, 0
};
GLfloat line2_left[] =
{
220, 200, 0,
260, 200, 0
};
GLfloat line1_right[] =
{
400, 180, 0,
400, 220, 0
};
GLfloat line2_right[] =
{
380, 200, 0,
420, 200, 0
};
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // polygon drawing mode (GL_POINT, GL_LINE, GL_FILL)
// Loop until the user closes the window
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
// render OpenGL here
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glEnableClientState(GL_VERTEX_ARRAY);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.8, 0.0, 0.6);//purple
glVertexPointer(3, GL_FLOAT, 0, polygonVertices);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawArrays(GL_POLYGON, 0, 8); // Number of points in polygon
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDisableClientState(GL_VERTEX_ARRAY);
glColor3f(0.2, 0.2, 0.2);//blackish
// render OpenGL here
glEnableClientState(GL_VERTEX_ARRAY);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glVertexPointer(3, GL_FLOAT, 0, door);
glDrawArrays(GL_POLYGON, 0, 4);// Number of points in polygon
glDisableClientState(GL_VERTEX_ARRAY);
// render OpenGL here
glEnableClientState(GL_VERTEX_ARRAY);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glVertexPointer(3, GL_FLOAT, 0, window1);
glDrawArrays(GL_POLYGON, 0, 4);// Number of points in polygon
glDisableClientState(GL_VERTEX_ARRAY);
// render OpenGL here
glEnableClientState(GL_VERTEX_ARRAY);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glVertexPointer(3, GL_FLOAT, 0, window2);
glDrawArrays(GL_POLYGON, 0, 4);// Number of points in polygon
glDisableClientState(GL_VERTEX_ARRAY);
// Render OpenGL here
glEnable(GL_LINE_SMOOTH);
// glEnable( GL_LINE_STIPPLE );
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glPushAttrib(GL_LINE_BIT);
glLineWidth(3);
glLineStipple(1, 0x00FF);
glEnableClientState(GL_VERTEX_ARRAY);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glVertexPointer(3, GL_FLOAT, 0, line1_left); // name of line
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);
glPopAttrib();
glDisable(GL_LINE_STIPPLE);
glDisable(GL_LINE_SMOOTH);
// Render OpenGL here
glEnable(GL_LINE_SMOOTH);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// glEnable( GL_LINE_STIPPLE );
glPushAttrib(GL_LINE_BIT);
glLineWidth(3);
glLineStipple(1, 0x00FF);
glEnableClientState(GL_VERTEX_ARRAY);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glVertexPointer(3, GL_FLOAT, 0, line2_left); // name of line
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);
glPopAttrib();
glDisable(GL_LINE_STIPPLE);
glDisable(GL_LINE_SMOOTH);
// Render OpenGL here
glEnable(GL_LINE_SMOOTH);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// glEnable( GL_LINE_STIPPLE );
glPushAttrib(GL_LINE_BIT);
glLineWidth(3);
glLineStipple(1, 0x00FF);
glEnableClientState(GL_VERTEX_ARRAY);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glVertexPointer(3, GL_FLOAT, 0, line1_right); // name of line
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);
glPopAttrib();
glDisable(GL_LINE_STIPPLE);
glDisable(GL_LINE_SMOOTH);
// Render OpenGL here
glEnable(GL_LINE_SMOOTH);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// glEnable( GL_LINE_STIPPLE );
glPushAttrib(GL_LINE_BIT);
glLineWidth(3);
glLineStipple(1, 0x00FF);
glEnableClientState(GL_VERTEX_ARRAY);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glVertexPointer(3, GL_FLOAT, 0, line2_right); // name of line
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);
glPopAttrib();
glDisable(GL_LINE_STIPPLE);
glDisable(GL_LINE_SMOOTH);
// render OpenGL here - the semicircle on top
glColor3f(0.8, 0.0, 0.6); //purple
drawSemiCircle(320, 300, 120, 250); // movement on x axis, movement on y axis , size,
// render OpenGL here - door knob
glColor3f(0.8, 0.0, 0.6); //purple
glPolygonMode(GL_BACK, GL_FILL);
drawCircleFill(350, 170, 5, 250); // movement on x axis, movement on y axis , radius, sides
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glLineWidth(5);
drawCircle(500, 400, 40, 360); // movement on x axis, movement on y axis , radius, sides
glColor3f(0.8f, 0.6f, 0.2f); //Golden
glLineWidth(5);
drawCircleFill(500, 400, 40, 360); // movement on x axis, movement on y axis , radius, sides
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(1.0f, 1.0f, 1.0f); //white
drawCircleFill(525, 410, 40, 360); // movement on x axis, movement on y axis , radius, sides
glColor3f(0.3, 0.3, 0.3);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// Swap front and back buffers
glfwSwapBuffers(window);
glColor3f(0.3, 0.3, 0.3);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glLineWidth(1);
// Poll for and process events
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void drawSemiCircle(float cx, float cy, float r, int num_segments)
{
float theta = 3.1415926 / float(num_segments);
float tangetial_factor = tanf(theta);//calculate the tangential factor
float radial_factor = cosf(theta);//calculate the radial factor
float x = r;//we start at angle = 0
float y = 0;
glBegin(GL_LINE_LOOP);
for (int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x + cx, y + cy);//output vertex
//calculate the tangential vector
//remember, the radial vector is (x, y)
//to get the tangential vector we flip those coordinates and negate one of them
float tx = -y;
float ty = x;
//add the tangential vector
x += tx * tangetial_factor;
y += ty * tangetial_factor;
//correct using the radial factor
x *= radial_factor;
y *= radial_factor;
}
glEnd();
}
void drawCircle(float cx, float cy, float r, int num_segments)
{
float theta = 3.1415926 * 2 / float(num_segments);
float tangetial_factor = tanf(theta);//calculate the tangential factor
float radial_factor = cosf(theta);//calculate the radial factor
float x = r;//we start at angle = 0
float y = 0;
glLineWidth(2);
glBegin(GL_LINE_LOOP);
for (int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x + cx, y + cy);//output vertex
//calculate the tangential vector
//remember, the radial vector is (x, y)
//to get the tangential vector we flip those coordinates and negate one of them
float tx = -y;
float ty = x;
//add the tangential vector
x += tx * tangetial_factor;
y += ty * tangetial_factor;
//correct using the radial factor
x *= radial_factor;
y *= radial_factor;
}
glEnd();
}
void drawCircleFill(float cx, float cy, float r, int num_segments)
{
float theta = 3.1415926 * 2 / float(num_segments);
float tangetial_factor = tanf(theta);//calculate the tangential factor
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
float radial_factor = cosf(theta);//calculate the radial factor
float x = r;//we start at angle = 0
float y = 0;
//glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
for (int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x + cx, y + cy);//output vertex
//calculate the tangential vector
//remember, the radial vector is (x, y)
//to get the tangential vector we flip those coordinates and negate one of them
float tx = -y;
float ty = x;
//add the tangential vector
x += tx * tangetial_factor;
y += ty * tangetial_factor;
//correct using the radial factor
x *= radial_factor;
y *= radial_factor;
}
glEnd();
}