-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.c
351 lines (327 loc) · 10.7 KB
/
interface.c
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
/*
Nama file : interface.c
Dibuat oleh : Ali Qornan Jaisyurrahman
Tanggal : 22 januari 2018 18:42 WIB
Fungsi : ADT yang berfungsi untuk mengimplementasikan fungsi yang ada di library freeglut untuk
membuat suatu bangun yang diinginkan.
Lisensi : https://github.com/qornanali/PendopoTonyAgung-OpenGL-C/blob/master/LICENSE
*/
#include "interface.h"
#include <math.h>
#include <stdio.h>
/* Method prosedur untuk menampilkan bangun ruang kubus */
void drawCube(Coord coord, GLfloat radius, Color color[], GLuint textureId, BOOL bindings[], GLfloat repeat){
drawPrism4(coord, radius, radius, radius, color, textureId, bindings, repeat);
}
/* Method prosedur untuk menampilkan bangun ruang prisma dengan alas segi empat */
void drawPrism4(Coord coord, GLfloat length, GLfloat width, GLfloat height,
Color color[], GLuint textureId, BOOL bindings[], GLfloat repeat){
if(textureId != 0){
glBindTexture(GL_TEXTURE_2D, textureId);
}
glPushMatrix();
glBegin(GL_POLYGON);
glColor3f(color[0].r, color[0].g, color[0].b);
if(bindings[0]){
glTexCoord2f(repeat, repeat);
}
glVertex3f(coord.x, coord.y, coord.z);
if(bindings[0]){
glTexCoord2f(0.0f, repeat);
}
glVertex3f(coord.x + length, coord.y, coord.z);
if(bindings[0]){
glTexCoord2f(repeat, 0.0f);
}
glVertex3f(coord.x + length, coord.y + height, coord.z);
if(bindings[0]){
glTexCoord2f(0.0f, 0.0f);
}
glVertex3f(coord.x, coord.y + height, coord.z);
glEnd();
glBegin(GL_POLYGON);
glColor3f(color[1].r, color[1].g, color[1].b);
if(bindings[1]){
glTexCoord2f(repeat, repeat);
}
glVertex3f(coord.x, coord.y, coord.z);
if(bindings[1]){
glTexCoord2f(0.0f, repeat);
}
glVertex3f(coord.x, coord.y, coord.z + width);
if(bindings[1]){
glTexCoord2f(repeat, 0.0f);
}
glVertex3f(coord.x, coord.y + height, coord.z + width);
if(bindings[1]){
glTexCoord2f(0.0f, 0.0f);
}
glVertex3f(coord.x, coord.y + height, coord.z);
glEnd();
glBegin(GL_POLYGON);
glColor3f(color[2].r, color[2].g, color[2].b);
if(bindings[2]){
glTexCoord2f(repeat, repeat);
}
glVertex3f(coord.x + length, coord.y, coord.z);
if(bindings[2]){
glTexCoord2f(0.0f, repeat);
}
glVertex3f(coord.x + length, coord.y, coord.z + width);
if(bindings[2]){
glTexCoord2f(repeat, 0.0f);
}
glVertex3f(coord.x + length, coord.y + height, coord.z + width);
if(bindings[2]){
glTexCoord2f(0.0f, 0.0f);
}
glVertex3f(coord.x + length, coord.y + height, coord.z);
glEnd();
glBegin(GL_POLYGON);
glColor3f(color[3].r, color[3].g, color[3].b);
if(bindings[3]){
glTexCoord2f(repeat, repeat);
}
glVertex3f(coord.x, coord.y, coord.z);
if(bindings[3]){
glTexCoord2f(repeat, 0.0f);
}
glVertex3f(coord.x + length, coord.y, coord.z);
if(bindings[3]){
glTexCoord2f(0.0f, repeat);
}
glVertex3f(coord.x + length, coord.y, coord.z + width);
if(bindings[3]){
glTexCoord2f(0.0f, 0.0f);
}
glVertex3f(coord.x, coord.y, coord.z + width);
glEnd();
glBegin(GL_POLYGON);
glColor3f(color[4].r, color[4].g, color[4].b);
if(bindings[4]){
glTexCoord2f(repeat, repeat);
}
glVertex3f(coord.x, coord.y + height, coord.z);
if(bindings[4]){
glTexCoord2f(repeat, 0.0f);
}
glVertex3f(coord.x + length, coord.y + height, coord.z);
if(bindings[4]){
glTexCoord2f(0.0f, repeat);
}
glVertex3f(coord.x + length, coord.y + height, coord.z + width);
if(bindings[4]){
glTexCoord2f(0.0f, 0.0f);
}
glVertex3f(coord.x, coord.y + height, coord.z + width);
glEnd();
glBegin(GL_POLYGON);
glColor3f(color[5].r, color[5].g, color[5].b);
if(bindings[5]){
glTexCoord2f(repeat, repeat);
}
glVertex3f(coord.x, coord.y, coord.z + width);
if(bindings[5]){
glTexCoord2f(0.0f, repeat);
}
glVertex3f(coord.x + length, coord.y, coord.z + width);
if(bindings[5]){
glTexCoord2f(repeat, 0.0f);
}
glVertex3f(coord.x + length, coord.y + height, coord.z + width);
if(bindings[5]){
glTexCoord2f(0.0f, 0.0f);
}
glVertex3f(coord.x, coord.y + height, coord.z + width);
glEnd();
glPopMatrix();
}
/* Method prosedur untuk menampilkan bangun ruang prisma dengan alas trapezium */
void drawPrismTrapezoid(Coord coord, GLfloat lengtha, GLfloat lengthb, GLfloat width, GLfloat height,
Color color[], GLuint textureId, BOOL bindings[], GLfloat repeat){
if(textureId != 0){
glBindTexture(GL_TEXTURE_2D, textureId);
}
glPushMatrix();
glBegin(GL_POLYGON);
glColor3f(color[0].r, color[0].g, color[0].b);
if(bindings[0]){
glTexCoord2f(repeat, 0.0f);
}
glVertex3f(coord.x, coord.y, coord.z);
if(bindings[0]){
glTexCoord2f(repeat, repeat);
}
glVertex3f(coord.x + lengthb, coord.y, coord.z);
if(bindings[0]){
glTexCoord2f(0.0f, repeat);
}
glVertex3f(coord.x + lengtha + ((lengthb - lengtha)/2), coord.y + height, coord.z);
if(bindings[0]){
glTexCoord2f(0.0f, 0.0f);
}
glVertex3f(coord.x + ((lengthb - lengtha)/2), coord.y + height, coord.z);
glEnd();
glBegin(GL_POLYGON);
glColor3f(color[1].r, color[1].g, color[1].b);
if(bindings[1]){
glTexCoord2f(repeat, 0.0f);
}
glVertex3f(coord.x + lengthb, coord.y, coord.z);
if(bindings[1]){
glTexCoord2f(repeat, repeat);
}
glVertex3f(coord.x + lengtha + ((lengthb - lengtha)/2), coord.y + height, coord.z);
if(bindings[1]){
glTexCoord2f(0.0f, repeat);
}
glVertex3f(coord.x + lengtha + ((lengthb - lengtha)/2), coord.y + height, coord.z + width);
if(bindings[1]){
glTexCoord2f(0.0f, 0.0f);
}
glVertex3f(coord.x + lengthb, coord.y, coord.z + width);
glEnd();
glBegin(GL_POLYGON);
glColor3f(color[2].r, color[2].g, color[2].b);
if(bindings[2]){
glTexCoord2f(repeat, 0.0f);
}
glVertex3f(coord.x + ((lengthb - lengtha)/2), coord.y + height, coord.z);
if(bindings[2]){
glTexCoord2f(repeat, repeat);
}
glVertex3f(coord.x + ((lengthb - lengtha)/2), coord.y + height, coord.z+ width);
if(bindings[2]){
glTexCoord2f(0.0f, repeat);
}
glVertex3f(coord.x + lengtha + ((lengthb - lengtha)/2), coord.y + height, coord.z);
if(bindings[2]){
glTexCoord2f(0.0f, 0.0f);
}
glVertex3f(coord.x + lengtha + ((lengthb - lengtha)/2), coord.y + height, coord.z + width);
glEnd();
glBegin(GL_POLYGON);
glColor3f(color[3].r, color[3].g, color[3].b);
if(bindings[3]){
glTexCoord2f(repeat, 0.0f);
}
glVertex3f(coord.x, coord.y, coord.z);
if(bindings[3]){
glTexCoord2f(repeat, repeat);
}
glVertex3f(coord.x + ((lengthb - lengtha)/2), coord.y + height, coord.z);
if(bindings[3]){
glTexCoord2f(0.0f, repeat);
}
glVertex3f(coord.x + ((lengthb - lengtha)/2), coord.y + height, coord.z + width);
if(bindings[3]){
glTexCoord2f(0.0f, 0.0f);
}
glVertex3f(coord.x, coord.y, coord.z + width);
glEnd();
glBegin(GL_POLYGON);
glColor3f(color[4].r, color[4].g, color[4].b);
if(bindings[4]){
glTexCoord2f(repeat, 0.0f);
}
glVertex3f(coord.x, coord.y, coord.z);
if(bindings[4]){
glTexCoord2f(repeat, repeat);
}
glVertex3f(coord.x + lengthb, coord.y, coord.z);
if(bindings[4]){
glTexCoord2f(0.0f, repeat);
}
glVertex3f(coord.x + lengthb, coord.y, coord.z + width);
if(bindings[4]){
glTexCoord2f(0.0f, 0.0f);
}
glVertex3f(coord.x, coord.y, coord.z + width);
glEnd();
glBegin(GL_POLYGON);
glColor3f(color[5].r, color[5].g, color[5].b);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(coord.x, coord.y, coord.z + width);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(coord.x + lengthb, coord.y, coord.z + width);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(coord.x + lengtha + ((lengthb - lengtha)/2), coord.y + height, coord.z + width);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(coord.x + ((lengthb - lengtha)/2), coord.y + height, coord.z + width);
glEnd();
glPopMatrix();
}
/* Method prosedur untuk menampilkan garis kartesius 3D */
void drawCartesius(Coord coord, GLfloat radius){
glPushMatrix();
//x
glBegin(GL_LINES);
glColor3f(coord.x + radius, coord.y, coord.z);
glVertex3f(coord.x + radius, coord.y, coord.z);
glVertex3f(coord.x - radius, coord.y, coord.z);
glEnd();
//y
glBegin(GL_LINES);
glColor3f(coord.x, coord.y + radius, coord.z);
glVertex3f(coord.x, coord.y + radius, coord.z);
glVertex3f(coord.x, coord.y - radius, coord.z);
glEnd();
//z
glBegin(GL_LINES);
glColor3f(coord.x, coord.y, coord.z + radius);
glVertex3f(coord.x, coord.y, coord.z + radius);
glVertex3f(coord.x, coord.y, coord.z - radius);
glEnd();
glPopMatrix();
}
/* Method prosedur untuk menampilkan bangun ruang balok */
void drawBox(GLfloat width, GLfloat height, GLfloat depth, GLuint textFront, GLuint textBack, GLuint textRight, GLuint textLeft, GLuint textTop, GLuint textBottom, GLfloat textKoor)
{
width = width*0.5;
depth = depth*0.5;
height = height*0.5;
GLfloat face[6][4][3]= {
{{-width,-height, -depth},{width, -height, -depth},{width, height, -depth},{-width, height, -depth}}, //front
{{-width,-height, depth},{width, -height, depth},{width, height, depth},{-width, height, depth}}, //back
{{width, -height, -depth},{width, -height, depth},{width, height, depth},{width, height, -depth}}, //right
{{-width, -height, depth},{-width, -height, -depth},{-width, height, -depth},{-width, height, depth}}, //left
{{width, height, depth},{-width, height, depth},{-width, height, -depth},{width, height, -depth}}, //top
{{-width, -height, depth},{width, -height, depth},{width, -height, -depth},{-width, -height, -depth}} //bottom
};
int i;
for(i=0;i<6;i++){
switch (i){
case 0 :
glBindTexture(GL_TEXTURE_2D, textFront);
break;
case 1 :
glBindTexture(GL_TEXTURE_2D, textBack);
break;
case 2 :
glBindTexture(GL_TEXTURE_2D, textRight);
break;
case 3 :
glBindTexture(GL_TEXTURE_2D, textLeft);
break;
case 4 :
glBindTexture(GL_TEXTURE_2D, textTop);
break;
case 5 :
glBindTexture(GL_TEXTURE_2D, textBottom);
break;
}
glPushMatrix();
glBegin(GL_POLYGON);
glColor3f(1,1,1);
glTexCoord2f(0,textKoor);
glVertex3f(face[i][0][0], face[i][0][1], face[i][0][2]);
glTexCoord2f(textKoor,textKoor);
glVertex3f(face[i][1][0], face[i][1][1], face[i][1][2]);
glTexCoord2f(textKoor,0);
glVertex3f(face[i][2][0], face[i][2][1], face[i][2][2]);
glTexCoord2f(0,0);
glVertex3f(face[i][3][0], face[i][3][1], face[i][3][2]);
glEnd();
glPopMatrix();
}
}