-
Notifications
You must be signed in to change notification settings - Fork 2
/
renderer.c
246 lines (191 loc) · 6.36 KB
/
renderer.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
//
// heligun: 3D flight sim shooter
// Copyright (c) 2016 Joseph Kuziel
//
// This software is MIT licensed.
//
#include "renderer.h"
#include "sdlext.h"
#include "glext.h"
#include "game.h"
#include "mathext.h"
#include <stdio.h>
static GLuint g_terrain_diffuse_texture;
static GLuint g_terrain_normal_texture;
static GLuint g_cockpit_diffuse_texture;
static GLuint g_terrain_shader;
static GLuint g_cockpit_shader;
static GLint g_terrain_position_loc;
static GLint g_terrain_rotation_loc;
static GLint g_cockpit_offset_loc;
static GLint g_cockpit_rotation_loc;
static GLint g_cockpit_brightness_loc;
static GLint g_sunangle_loc;
static GLuint g_terrain_vbo;
int initRenderer() {
// Culling
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
// Depth
glDisable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS);
// Blend
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Other
glDisable(GL_STENCIL_TEST);
// Create OpenGL shader program
g_terrain_shader = glextCreateShaderProgram(
"res/terrain.vert"
, "res/terrain.frag"
);
if(g_terrain_shader == 0) {
printf("Could not create terrain shader program\n");
return 1;
}
g_cockpit_shader = glextCreateShaderProgram(
"res/cockpit.vert"
, "res/cockpit.frag"
);
if(g_cockpit_shader == 0) {
printf("Could not create cockpit shader program\n");
return 1;
}
// Geometry
glBindAttribLocation(g_terrain_shader, 0, "a_position");
glBindAttribLocation(g_cockpit_shader, 0, "a_position");
GLfloat terrain_vertex_data[] = {
-1.f, 1.0f,
1.0f, 1.0f,
-1.f, -1.f,
1.0f, -1.f,
};
g_terrain_vbo = glextCreateVBO(terrain_vertex_data, 4, 2, GL_STATIC_DRAW);
if(g_terrain_vbo == 0) {
return 1;
}
// Load textures
g_terrain_diffuse_texture = glextLoadTexture("res/map0d.png", 1, 0);
g_terrain_normal_texture = glextLoadTexture("res/map0n.png", 1, 0);
g_cockpit_diffuse_texture = glextLoadTexture("res/helicockpit.png", 0, 1);
if(g_cockpit_diffuse_texture == 0) {
return 1;
}
glextBindTextureToUniform(
g_terrain_shader
, g_terrain_diffuse_texture
, 0
, "u_diffusemap"
);
glextBindTextureToUniform(
g_terrain_shader
, g_terrain_normal_texture
, 1
, "u_normalmap"
);
if( glextBindTextureToUniform(
g_cockpit_shader
, g_cockpit_diffuse_texture
, 0
, "u_diffusemap"
) == 1) {
return 1;
}
// Program attribute locations
g_sunangle_loc = glGetUniformLocation(g_terrain_shader, "u_sunangle");
if(g_sunangle_loc == -1) {
printf("Could not find uniform location: u_sunangle\n");
return 1;
}
g_terrain_position_loc = glGetUniformLocation(g_terrain_shader,"u_viewpos");
if(g_terrain_position_loc == -1) {
printf("Could not find uniform location: u_viewpos\n");
return 1;
}
g_terrain_rotation_loc = glGetUniformLocation(g_terrain_shader,"u_viewrot");
if(g_terrain_rotation_loc == -1) {
printf("Could not find uniform location: u_viewrot\n");
return 1;
}
g_cockpit_offset_loc = glGetUniformLocation(g_cockpit_shader, "u_offset");
if(g_cockpit_offset_loc == -1) {
printf("Could not find uniform location: u_offset");
return 1;
}
g_cockpit_rotation_loc =glGetUniformLocation(g_cockpit_shader,"u_rotation");
if(g_cockpit_rotation_loc == -1) {
printf("Could not find uniform location: u_rotation");
return 1;
}
g_cockpit_brightness_loc
= glGetUniformLocation(g_cockpit_shader,"u_brightness");
if(g_cockpit_brightness_loc == -1) {
printf("Could not find uniform location: u_brightness\n");
return 1;
}
return 0;
}
void setRendererSize(int width, int height) {
glViewport(0, 0, width, height);
glUseProgram(g_terrain_shader);
GLint terrain_screensize_loc =
glGetUniformLocation(
g_terrain_shader
, "u_screensize"
);
if(terrain_screensize_loc != -1) {
float renderSize[] = {(float)width, (float)height};
glUniform2fv(terrain_screensize_loc, 1, renderSize);
glextCheckError();
} else {
printf("Could not find uniform location: u_screensize\n");
}
glUseProgram(g_cockpit_shader);
GLint cockpit_screensize_loc =
glGetUniformLocation(
g_cockpit_shader
, "u_screensize"
);
if(cockpit_screensize_loc != -1) {
float renderSize[] = {(float)width, (float)height};
glUniform2fv(cockpit_screensize_loc, 1, renderSize);
glextCheckError();
} else {
printf("Could not find uniform location: u_screensize\n");
}
}
void render() {
glEnableVertexAttribArray(0);
// Render terrain //////////////////
glUseProgram(g_terrain_shader);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, g_terrain_diffuse_texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, g_terrain_normal_texture);
float sunAngle[] = { getSunAngle() };
glUniform1fv(g_sunangle_loc, 1, sunAngle);
Helicopter player = getPlayer();
float terrain_position_vec[] = { player.px1, player.py1, player.pz1 };
glUniform3fv(g_terrain_position_loc, 1, terrain_position_vec);
float terrain_rotation_vec[] = { player.rx1, player.ry1, player.rz1 };
glUniform3fv(g_terrain_rotation_loc, 1, terrain_rotation_vec);
glBindBuffer(GL_ARRAY_BUFFER, g_terrain_vbo);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Render cockpit //////////////////
glUseProgram(g_cockpit_shader);
float cockpit_offset_vec[] = { -player.rz2 * 0.05f, -player.rx2 * 0.05f };
glUniform2fv(g_cockpit_offset_loc, 1, cockpit_offset_vec);
float cockpit_rotation[] = { -player.ry2 * 0.05f };
glUniform1fv(g_cockpit_rotation_loc, 1, cockpit_rotation);
float cockpit_brightness[] = {
SCALE(sin(player.rz1) * cos(getSunAngle()), -1.0f, 1.0f, 0.6f, 1.0f)
};
glUniform1fv(g_cockpit_brightness_loc, 1, cockpit_brightness);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, g_cockpit_diffuse_texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableVertexAttribArray(0);
}