-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpad.c
220 lines (171 loc) · 4.05 KB
/
pad.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
#include <stdlib.h>
#include <stdio.h>
#include <GL/glew.h>
#include "pad.h"
#include "linmath.h"
#include "constants.h"
#include "shader.h"
#include "model.h"
#include "mesh.h"
#include "objects.h"
#include "utils.h"
#include "texture.h"
static const GLfloat PAD_THROTTLE = 0.3f;
static const GLfloat MAX_SPEED = 1.7f;
static const GLfloat PAD_FRICTION = 0.19f;
static const GLfloat SCALE_FACTOR = 0.4;
static const GLfloat MAX_SPRING = 0.4;
static const GLfloat SPRING_FORCE = 0.1;
static int mesh_ref_count = 0;
static struct mesh *mesh = NULL;
static int texture_ref_count = 0;
static GLuint texture = 0;
static struct mesh *
pad_mesh(void)
{
if (mesh != NULL) {
++mesh_ref_count;
return mesh;
}
mesh = mesh_load("resource/pad.obj");
return mesh;
}
static void
pad_mesh_free(void)
{
if (mesh == NULL)
return;
--mesh_ref_count;
if (mesh_ref_count == 0) {
mesh_free(mesh);
mesh = NULL;
}
}
static GLuint
pad_texture(void)
{
if (texture_ref_count == 0) {
texture = load_texture_from_png(
"resource/pad_texture.png", NULL, NULL);
}
++texture_ref_count;
return texture;
}
static void
pad_texture_free(void)
{
if (texture_ref_count == 0)
return;
--texture_ref_count;
if (texture_ref_count == 0) {
glDeleteTextures(1, &texture);
}
}
struct pad *
pad_new(void)
{
struct pad *p = malloc(sizeof *p);
p->x = 0.0;
p->speed = 0.0;
p->spring_strength = 0.0;
p->angle = -90.0;
p->mesh = pad_mesh();
p->texture = pad_texture();
return p;
}
void
pad_free(struct pad *p)
{
free(p);
pad_mesh_free();
pad_texture_free();
}
void
pad_draw(void *object, GLuint shader_program)
{
struct pad *pad = (struct pad *) object;
shader_set_uniform_m4(shader_program, "model", pad->model_matrix);
shader_set_uniform_m4(shader_program, "normalModel", pad->normal_matrix);
shader_set_uniform_i(shader_program, "objectType", OBJECT_PAD);
glBindTexture(GL_TEXTURE_2D, pad->texture);
glBindVertexArray(pad->mesh->vao);
glDrawArrays(GL_TRIANGLES, 0, pad->mesh->vertex_count);
glBindVertexArray(0);
}
static void
pad_deaccel(struct pad *p)
{
int signal;
if (fuzzy_compare(p->speed, 0.0))
return;
signal = (p->speed > 0) ? 1 : -1;
p->speed -= signal*PAD_FRICTION;
if (fabs(p->speed) <= 0.1f) {
p->speed = 0.0f;
return;
}
}
static void
recalculate_matrices(struct pad *p)
{
mat4x4_identity(p->model_matrix);
mat4x4_translate_in_place(p->model_matrix, p->x, -p->spring_strength, 0.0);
mat4x4_scale_aniso(p->model_matrix, p->model_matrix, SCALE_FACTOR, SCALE_FACTOR, SCALE_FACTOR);
mat4x4_invert(p->normal_matrix, p->model_matrix);
mat4x4_transpose(p->normal_matrix, p->normal_matrix);
}
static void
update_bounding_box(struct pad *p)
{
mat4x4 m;
mat4x4_identity(m);
mat4x4_translate_in_place(m, p->x, 0.0, 0.0);
mat4x4_scale_aniso(m, m, SCALE_FACTOR, SCALE_FACTOR, SCALE_FACTOR);
p->box = p->mesh->bounding_box;
mat4x4_mul_vec3(p->box.min, m, p->box.min);
mat4x4_mul_vec3(p->box.max, m, p->box.max);
}
void
pad_update(void *object)
{
int signal;
struct pad *p = (struct pad *) object;
p->x += p->speed;
if (fuzzy_compare(p->spring_strength, 0.0))
p->spring_strength = 0.0;
else
p->spring_strength -= SPRING_FORCE;
signal = (p->speed > 0) ? 1 : -1;
if (fabs(p->x) > WORLD_BOUNDARY_X) {
p->x = signal * WORLD_BOUNDARY_X;
p->speed = -p->speed * 1.5;
}
pad_deaccel(p);
recalculate_matrices(p);
update_bounding_box(p);
}
void
pad_throttle_left(struct pad *p)
{
p->speed -= PAD_THROTTLE;
if (p->speed < -MAX_SPEED)
p->speed = -MAX_SPEED;
}
void
pad_throttle_right(struct pad *p)
{
p->speed += PAD_THROTTLE;
if (p->speed > MAX_SPEED)
p->speed = MAX_SPEED;
}
void
pad_rotate_x(struct pad *p)
{
p->angle += 10;
p->angle = p->angle % 360;
}
void
pad_bump(struct pad *p)
{
p->spring_strength = MAX_SPRING;
}