-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmodel.c
273 lines (232 loc) · 5.37 KB
/
model.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
#include <stddef.h>
#include <math.h>
#include <GL/gl.h>
#include "matrix.h"
#include "program.h"
#include "util.h"
struct point {
float x;
float y;
float z;
} __attribute__((packed));
struct color {
float r;
float g;
float b;
} __attribute__((packed));
// Each vertex has position, normal and color:
struct vertex {
struct point pos;
struct point normal;
struct color color;
} __attribute__((packed));
// Each triangle has three vertices:
struct triangle {
struct vertex vert[3];
} __attribute__((packed));
// Each corner point has a position and a color:
struct corner {
struct point pos;
struct color color;
} __attribute__((packed));
// Each face has a single normal, four corner points,
// and two triangles:
struct face {
struct corner corner[4];
struct point normal;
struct triangle tri[2];
} __attribute__((packed));
// Each cube has six faces:
struct cube {
struct face face[6];
} __attribute__((packed));
static GLuint vao, vbo;
static float matrix[16] = { 0 };
// Mouse movement:
static struct {
int x;
int y;
} pan;
// Cube rotation axis:
static struct point rot = {
.x = 0.0f,
.y = 1.0f,
.z = 0.0f,
};
// Return the cross product of two vectors:
static void
cross (struct point *result, const struct point *a, const struct point *b)
{
result->x = a->y * b->z - a->z * b->y;
result->y = a->z * b->x - a->x * b->z;
result->z = a->x * b->y - a->y * b->x;
}
// Initialize the model:
void
model_init (void)
{
// Define our cube:
struct cube cube =
{ .face[0].corner =
{ { 0, 1, 0 }
, { 1, 0, 0 }
, { 0, 0, 0 }
, { 1, 1, 0 }
}
, .face[1].corner =
{ { 0, 0, 0 }
, { 1, 0, 1 }
, { 0, 0, 1 }
, { 1, 0, 0 }
}
, .face[2].corner =
{ { 1, 0, 0 }
, { 1, 1, 1 }
, { 1, 0, 1 }
, { 1, 1, 0 }
}
, .face[3].corner =
{ { 1, 1, 0 }
, { 0, 1, 1 }
, { 1, 1, 1 }
, { 0, 1, 0 }
}
, .face[4].corner =
{ { 0, 1, 0 }
, { 0, 0, 1 }
, { 0, 1, 1 }
, { 0, 0, 0 }
}
, .face[5].corner =
{ { 0, 1, 1 }
, { 1, 0, 1 }
, { 1, 1, 1 }
, { 0, 0, 1 }
}
} ;
// Generate colors for each corner based on its position:
FOREACH (cube.face, face) {
FOREACH (face->corner, corner) {
corner->color.r = corner->pos.x * 0.8f + 0.1f;
corner->color.g = corner->pos.y * 0.8f + 0.1f;
corner->color.b = corner->pos.z * 0.8f + 0.1f;
}
}
// Center cube on the origin by translating corner points:
FOREACH (cube.face, face) {
FOREACH (face->corner, corner) {
corner->pos.x -= 0.5f;
corner->pos.y -= 0.5f;
corner->pos.z -= 0.5f;
}
}
// Face normals are cross product of two ribs:
FOREACH (cube.face, face) {
// First rib is (corner 3 - corner 0):
struct point a = {
.x = face->corner[3].pos.x - face->corner[0].pos.x,
.y = face->corner[3].pos.y - face->corner[0].pos.y,
.z = face->corner[3].pos.z - face->corner[0].pos.z,
};
// Second rib is (corner 2 - corner 0):
struct point b = {
.x = face->corner[2].pos.x - face->corner[0].pos.x,
.y = face->corner[2].pos.y - face->corner[0].pos.y,
.z = face->corner[2].pos.z - face->corner[0].pos.z,
};
// Face normal is cross product of these two ribs:
cross(&face->normal, &a, &b);
}
// Create two triangles for each face:
FOREACH (cube.face, face) {
// Corners to compose triangles of, chosen in
// such a way that both triangles rotate CCW:
int index[2][3] = { { 0, 2, 1 }, { 1, 3, 0 } };
for (int t = 0; t < 2; t++) {
for (int v = 0; v < 3; v++) {
int c = index[t][v];
struct corner *corner = &face->corner[c];
struct vertex *vertex = &face->tri[t].vert[v];
vertex->pos = corner->pos;
vertex->normal = face->normal;
vertex->color = corner->color;
}
}
}
// Copy vertices into separate array for drawing:
struct vertex vertex[6 * 2 * 3];
struct vertex *cur = vertex;
FOREACH (cube.face, face) {
FOREACH (face->tri, tri) {
for (int v = 0; v < 3; v++) {
*cur++ = tri->vert[v];
}
}
}
// Generate empty buffer:
glGenBuffers(1, &vbo);
// Generate empty vertex array object:
glGenVertexArrays(1, &vao);
// Set as current vertex array:
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// Add vertex, color and normal data to buffers:
struct {
enum LocCube loc;
const void *ptr;
}
map[] = {
{ .loc = LOC_CUBE_VERTEX
, .ptr = (void *) offsetof(struct vertex, pos)
} ,
{ .loc = LOC_CUBE_VCOLOR
, .ptr = (void *) offsetof(struct vertex, color)
} ,
{ .loc = LOC_CUBE_NORMAL
, .ptr = (void *) offsetof(struct vertex, normal)
} ,
};
FOREACH (map, m) {
GLint loc = program_cube_loc(m->loc);
glEnableVertexAttribArray(loc);
glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, sizeof(struct vertex), m->ptr);
}
// Upload vertex data:
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
}
void
model_draw (void)
{
static float angle = 0.0f;
// Rotate slightly:
angle += 0.01f;
// Setup rotation matrix:
mat_rotate(matrix, rot.x, rot.y, rot.z, angle);
// Use our own shaders:
program_cube_use();
// Don't clip against background:
glClear(GL_DEPTH_BUFFER_BIT);
// Draw all the triangles in the buffer:
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 12 * 3);
}
const float *
model_matrix (void)
{
return matrix;
}
void
model_pan_start (int x, int y)
{
pan.x = x;
pan.y = y;
}
void
model_pan_move (int x, int y)
{
int dx = pan.x - x;
int dy = pan.y - y;
// Rotation vector is perpendicular to (dx, dy):
rot.x = dy;
rot.y = -dx;
}