-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.c
202 lines (153 loc) · 3.47 KB
/
render.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
#include "render.h"
#include "game.h"
#include "math.h"
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#endif
float camera_angle = 0.0f;
static void drawcoord();
static void rendertile(struct tile *tile)
{
glPushMatrix();
glMultMatrixf(tile->transform);
glBegin(GL_QUADS);
glColor3ub(0, 109, 150);
glVertex3f(-1.0, 0.0, 1.0);
glVertex3f(1.0, 0.0, 1.0);
glVertex3f(1.0, 0.0, -1.0);
glVertex3f(-1.0, 0.0, -1.0);
glColor3ub(0, 213, 190);
if(tile->west == -1)
{
glVertex3f(-1.0, 0.1, 1.0);
glVertex3f(-1.0, 0.1, -1.0);
glVertex3f(-1.0, 0.4, -1.0);
glVertex3f(-1.0, 0.4, 1.0);
}
if(tile->north == -1)
{
glVertex3f(-1.0, 0.1, -1.0);
glVertex3f(1.0, 0.1, -1.0);
glVertex3f(1.0, 0.4, -1.0);
glVertex3f(-1.0, 0.4, -1.0);
}
if(tile->east == -1)
{
glVertex3f(1.0, 0.1, 1.0);
glVertex3f(1.0, 0.1, -1.0);
glVertex3f(1.0, 0.4, -1.0);
glVertex3f(1.0, 0.4, 1.0);
}
if(tile->south == -1)
{
glVertex3f(-1.0, 0.1, 1.0);
glVertex3f(1.0, 0.1, 1.0);
glVertex3f(1.0, 0.4, 1.0);
glVertex3f(-1.0, 0.4, 1.0);
}
glEnd();
glPopMatrix();
}
static void renderplayer()
{
glBegin(GL_QUADS);
glColor3ub(40, 0, 130);
#define V(x,y,z) \
glVertex3f(x ? -0.3 : 0.3, y ? 0.0 : 0.5, z ? -0.3 : 0.3);
V(0,0,1) V(1,0,1) V(1,1,1) V(0,1,1)
V(1,0,1) V(1,0,0) V(1,1,0) V(1,1,1)
V(1,0,0) V(0,0,0) V(0,1,0) V(1,1,0)
V(0,0,0) V(0,0,1) V(0,1,1) V(0,1,0)
V(0,0,1) V(1,0,1) V(1,0,0) V(0,0,0)
V(0,1,1) V(1,1,1) V(1,1,0) V(0,1,0)
#undef V
glEnd();
}
static void rendercheckpoint(GLubyte r, GLubyte g, GLubyte b)
{
glBegin(GL_TRIANGLE_STRIP);
int i;
float a;
for(i=0; i<=16; i++)
{
a = (i / 16.0) * (2.0 * M_PI);
glColor4ub(r, g, b, 63);
glVertex3f(cos(a) * 0.5, 0.0, sin(a) * 0.5);
glColor4ub(0, 0, 0, 0);
glVertex3f(cos(a) * 0.5, 5.0, sin(a) * 0.5);
}
glEnd();
}
static void drawcoord()
{
glDepthRange(0, 0);
glBegin(GL_LINES);
glColor3ub(255, 0, 0);
glVertex3i(0, 0, 0);
glVertex3i(1, 0, 0);
glColor3ub(0, 255, 0);
glVertex3i(0, 0, 0);
glVertex3i(0, 1, 0);
glColor3ub(0, 0, 255);
glVertex3i(0, 0, 0);
glVertex3i(0, 0, 1);
glEnd();
glDepthRange(0, 1);
}
void renderinit()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
extern int window_w, window_h;
gluPerspective(60.0, window_w/(float)window_h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
extern struct tile *tiles;
extern int tiles_count;
extern int current_tile;
extern int start_tile, goal_tile;
extern float player_position[2];
extern float player_pan;
glLoadIdentity();
glTranslatef(-0.05, -1.0, -3.0);
glRotatef(camera_angle, 0.0, 1.0, 0.0);
renderplayer();
glRotatef(-player_pan * (180.0 / M_PI), 0.0, 1.0, 0.0);
glTranslatef(-player_position[0], 0.0, -player_position[1]);
glPushMatrix();
float ls[16];
inv_orthonormal_m4_m4(ls, tiles[current_tile].transform);
glMultMatrixf(ls);
int i;
for(i=0; i<tiles_count; i++)
{
rendertile(&tiles[i]);
}
glEnable(GL_BLEND);
glDepthMask(0);
//start
glPushMatrix();
glMultMatrixf(tiles[start_tile].transform);
rendercheckpoint(255, 186, 0);
glPopMatrix();
//goal
glPushMatrix();
glMultMatrixf(tiles[goal_tile].transform);
rendercheckpoint(0, 255, 0);
glPopMatrix();
glDisable(GL_BLEND);
glDepthMask(1);
glPopMatrix();
}