-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbunny.c
103 lines (83 loc) · 2.3 KB
/
bunny.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
#include "util.h"
#include "bunny.h"
#define WINDOW_NAME "StanfordBunny"
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 640
#define MAX_FOV 179
#define MIN_FOV 1
// model
typedef struct Bunny Bunny;
struct Bunny {
GlisyGeometry geometry;
int faceslen;
};
// forward decl
static void InitializeBunny(Bunny *bunny);
// bunny constructor
void
InitializeBunny(Bunny *bunny) {
GlisyVAOAttribute vPosition = {
.buffer = {
.data = (void *) StanfordBunny.positions,
.type = GL_FLOAT,
.size = sizeof(StanfordBunny.positions),
.usage = GL_STATIC_DRAW,
.offset = 0,
.stride = 0,
.dimension = 3,
}
};
bunny->faceslen = 3 * STANFORD_BUNNY_CELLS_COUNT;
glisyGeometryInit(&bunny->geometry);
glisyGeometryAttr(&bunny->geometry, "vPosition", &vPosition);
glisyGeometryFaces(&bunny->geometry,
GL_UNSIGNED_SHORT,
bunny->faceslen,
(void *) StanfordBunny.cells);
glisyGeometryUpdate(&bunny->geometry);
}
void
DrawBunny(Bunny *bunny) {
glisyGeometryBind(&bunny->geometry, 0);
glisyGeometryDraw(&bunny->geometry, GL_TRIANGLES, 0, bunny->faceslen);
glisyGeometryUnbind(&bunny->geometry);
}
static void
onMouseScroll(GLFWwindow* window, double xoffset, double yoffset) {
Camera *camera = (Camera *) glfwGetWindowUserPointer(window);
camera->fov += yoffset;
camera->fov = min(MAX_FOV, max(camera->fov, MIN_FOV));
UpdateCamera(camera);
}
int
main(void) {
// glfw
GLFWwindow *window;
// glisy
GlisyProgram program;
// objects
Camera camera;
Bunny bunny;
GL_CONTEXT_INIT();
glfwSetScrollCallback(window, onMouseScroll);
program = CreateProgram("bunny.v.glsl", "bunny.f.glsl");
InitializeCamera(&camera, WINDOW_WIDTH, WINDOW_HEIGHT);
InitializeBunny(&bunny);
glfwSetWindowUserPointer(window, &camera);
camera.position = vec3(1, 4, 10);
camera.target = vec3(1, 1, 1);
glisyProgramBind(&program);
GL_RENDER({
const float time = glfwGetTime();
const float angle = time * 22.5f;
const float radians = dtor(angle);
const vec3 rotation = vec3(0, 1, 0);
(void) mat4_rotate(camera.transform,
radians,
rotation);
camera.aspect = width / height;
UpdateCamera(&camera);
DrawBunny(&bunny);
});
return 0;
}