-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
356 lines (276 loc) · 6.75 KB
/
main.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
350
351
352
353
354
355
356
#include <stdio.h>
#include <stdlib.h>
#include "gl.h"
#include "camera.h"
#include "texture.h"
#include "font.h"
#include "t2cap.h"
#define WIDTH 1280
#define HEIGHT 720
T2CAP_GROUND *t2ground;
Camera camera;
Texture ground;
Texture side;
Texture font;
Texture skybox_top;
Texture skybox_bottom;
Texture skybox_front;
Texture skybox_back;
Texture skybox_left;
Texture skybox_right;
long last_time = 0;
long last_draw = 0;
int frames = 0;
void draw_ground(float x, float z, int8_t h)
{
// store current matrix
glPushMatrix();
// set the height
float y = 0.5;
glTranslatef(x, (float)h / 2, z);
// bind ground texture
glBindTexture(GL_TEXTURE_2D, ground.texture);
// set clockwise for drawing
glFrontFace(GL_CW);
// top face
glBegin(GL_TRIANGLE_STRIP);
glNormal3d(0, 1, 0);
glTexCoord2f(0, 0);
glVertex3f(0, y, 0);
glTexCoord2f(0, 1);
glVertex3f(0, y, -1);
glTexCoord2f(1, 0);
glVertex3f(1, y, 0);
glTexCoord2f(1, 1);
glVertex3f(1, y, -1);
glEnd();
// bind side texture
glBindTexture(GL_TEXTURE_2D, side.texture);
for (int i = 0; i < abs(h); i++)
{
// translate into position
if (i != 0) glTranslatef(0, -y, 0);
// front face
glBegin(GL_TRIANGLE_STRIP);
glNormal3d(0, 0, 1);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0, 0, 0);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0, y, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1, 0, 0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1, y, 0);
glEnd();
// back face
glBegin(GL_TRIANGLE_STRIP);
glNormal3d(0, 0, -1);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(0, 0, -1);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(0, y, -1);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(1, 0, -1);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1, y, -1);
glEnd();
// left face
glBegin(GL_TRIANGLE_STRIP);
glNormal3d(-1, 0, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(0, 0, 0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(0, y, 0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0, 0, -1);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0, y, -1);
glEnd();
// right face
glBegin(GL_TRIANGLE_STRIP);
glNormal3d(1, 0, 0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(1, 0, 0);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1, y, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1, 0, -1);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1, y, -1);
glEnd();
}
// pop the matrix back
glPopMatrix();
}
void hud()
{
// disable lighting
glDisable(GL_LIGHTING);
// set to the projecion mode
glMatrixMode(GL_PROJECTION);
// store the matrix in the stack
glPushMatrix();
// load identity
glLoadIdentity();
// set the ortho to the screen
glOrtho(0, WIDTH, HEIGHT, 0, -1, 1);
// set to model view matrix
glMatrixMode(GL_MODELVIEW);
// push the current matrix on the stack
glPushMatrix();
// load identity
glLoadIdentity();
// bind the font texture
glBindTexture(GL_TEXTURE_2D, font.texture);
// draw string on the screen
//draw_string("Hello, world.", 10, 10);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0, 0, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1, 0, 0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1, 1, 0);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0, 1, 0);
glEnd();
// switch to projection matrix
glMatrixMode(GL_PROJECTION);
// pop off matrix to grab last from stack
glPopMatrix();
// switch to model view matrix
glMatrixMode(GL_MODELVIEW);
// pop off matrix to grab last from stack
glPopMatrix();
// re-enable lighting
glEnable(GL_LIGHTING);
}
void draw()
{
// clear buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// render the HUD
hud();
// draw the camera
long cur_time = glutGet(GLUT_ELAPSED_TIME);
camera_draw(camera, cur_time - last_draw);
last_draw = cur_time;
// set light position shit
GLfloat lpos[] = { 56.0f, 30.0f, 56.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
// draw the ground
for (int x = 0; x < 56; x++)
for (int z = 0; z < 0x39; z++)
draw_ground(x, z, t2ground[x].column[z]);
// swap buffers
glutSwapBuffers();
}
void keys(unsigned char key, int x, int y)
{
if (key == 27) // escape
exit(0);
// update camera input
camera_input(camera, key, true);
}
void ukey(unsigned char key, int x, int y)
{
// update camera input
camera_input(camera, key, false);
}
void skey(int key, int x, int y)
{
// update camera input
camera_input(camera, key, true);
}
void uskey(int key, int x, int y)
{
// update camera input
camera_input(camera, key, false);
}
void resize(int w, int h)
{
// just in case height is 0 fix it
if(h == 0) h = 1;
// get the ratio from width and height
float ratio = 1.0 * w / h;
// switch to projection
glMatrixMode(GL_PROJECTION);
// reset matrix
glLoadIdentity();
// set the viewport
glViewport(0, 0, w, h);
// set perspective
gluPerspective(45, ratio, 1, 1000);
// go back to modelview
glMatrixMode(GL_MODELVIEW);
}
void idle()
{
long cur_time = glutGet(GLUT_ELAPSED_TIME);
frames++;
if (cur_time - last_time >= 1000)
{
printf("FPS: %i\n", frames);
last_time = cur_time;
frames = 0;
}
glutPostRedisplay();
}
void init(int argc, char *argv[])
{
// test load CAP
if (argc > 1) t2ground = load_cap(argv[1]);
else t2ground = load_cap("parks/grd.PRK");
// set camera
camera_new(camera, 32.0f, 10.0f, 32.0f);
// load the textures
load_texture(ground, "textures/ground.png");
load_texture(side, "textures/side.png");
load_texture(font, "textures/font.png");
}
int main(int argc, char *argv[])
{
// initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("THUG2 Park Editor");
// register callbacks
glutDisplayFunc(draw);
glutReshapeFunc(resize);
glutIdleFunc(idle);
glutKeyboardFunc(keys);
glutKeyboardUpFunc(ukey);
glutSpecialFunc(skey);
glutSpecialUpFunc(uskey);
// enable stuff
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// set properties and values
glutIgnoreKeyRepeat(1);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glShadeModel(GL_SMOOTH);
glCullFace(GL_BACK);
// set lighting properties
GLfloat ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glMaterialf(GL_LIGHT0, GL_SHININESS, 20.0f);
glMaterialfv(GL_LIGHT0, GL_EMISSION, specular);
// initialize things
init(argc, argv);
// set clear color to black
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// main GLUT loop
glutMainLoop();
return 1;
}