-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
executable file
·225 lines (178 loc) · 6.3 KB
/
Copy pathmain.c
File metadata and controls
executable file
·225 lines (178 loc) · 6.3 KB
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
#include "include/common.h"
#include "include/glad.h"
#include <cglm/cglm.h>
#include <GLFW/glfw3.h>
#define SCR_WIDTH 800
#define SCR_HEIGHT 600
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void process_input(GLFWwindow *window);
extern uint32_t create_shader_program(char const *vertex_path, char const *frag_path);
extern uint32_t generate_and_bind_texture(void);
extern void load_texture(char *filename, uint32_t id, char *uniform_name);
vec3 camera_pos = {0.0f, 0.0f, 3.0f}; vec3 camera_front = {0.0f, 0.0f, -1.0f}; vec3 camera_up = {0.0f, 1.0f, 0.0f};
float delta_time = 0.0f, last_frame = 0.0f;
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Renderer", NULL, NULL);
if (window == NULL) {
printf("Failed to create GLFW window\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
printf("Failed to initialize GLAD\n");
return -1;
}
uint32_t id = create_shader_program("shaders/vertex_source.glsl", "shaders/frag_source.glsl");
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
vec3 cube_positions[] = {
{ 0.0f, 0.0f, 0.0f},
{ 2.0f, 5.0f, -15.0f},
{-1.5f, -2.2f, -2.5f},
{-3.8f, -2.0f, -12.3f},
{ 2.4f, -0.4f, -3.5f},
{-1.7f, 3.0f, -7.5f},
{ 1.3f, -2.0f, -2.5f},
{ 1.5f, 2.0f, -2.5f},
{ 1.5f, 0.2f, -1.5f},
{-1.3f, 1.0f, -1.5f}
};
uint32_t VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3* sizeof(float)));
glEnableVertexAttribArray(1);
/* texture part */
uint32_t texture1 = generate_and_bind_texture();
load_texture("resources/textures/container.jpg", id, "texture1");
/* enable z-buffer */
glEnable(GL_DEPTH_TEST);
/* projection matrix */
mat4 projection = {
{ 1.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 1.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 1.0f },
};
glm_perspective(glm_rad(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f, projection);
glUniformMatrix4fv(glGetUniformLocation(id, "projection"), 1, GL_FALSE, (float *)projection);
/* main loop */
while (!glfwWindowShouldClose(window)) {
float current_frame = glfwGetTime();
delta_time = current_frame - last_frame;
last_frame = current_frame;
process_input(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glUseProgram(id);
/* setup camera */
mat4 view;
vec3 camera_sum;
glm_vec3_add(camera_pos, camera_front, camera_sum);
glm_lookat(camera_pos, camera_sum, camera_up, view);
glUniformMatrix4fv(glGetUniformLocation(id, "view"), 1, GL_FALSE, (float *)view);
glBindVertexArray(VAO);
/* spawn multiple cubes */
for(unsigned int i = 0; i < 10; i++) {
/* make multiple cubes */
mat4 model = {
{ 1.0f, 0.0f, 0.0f, 0.0f},
{ 0.0f, 1.0f, 0.0f, 0.0f},
{ 0.0f, 0.0f, 1.0f, 0.0f},
{ 0.0f, 0.0f, 0.0f, 1.0f}
};
glm_translate(model, cube_positions[i]);
float angle = 20.0f * i;
glm_rotate(model, glm_rad(angle), (vec3){1.0f, 0.3f, 0.5f});
glUniformMatrix4fv(glGetUniformLocation(id, "model"), 1, GL_FALSE, (float *)model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
}
void process_input(GLFWwindow *window)
{
float camera_speed = 2.5f * delta_time;
vec3 camera_cross, camera_mul;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, 1);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
glm_vec3_scale(camera_front, camera_speed, camera_mul);
glm_vec3_add(camera_pos, camera_mul, camera_pos);
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
glm_vec3_scale(camera_front, camera_speed, camera_mul);
glm_vec3_sub(camera_pos, camera_mul, camera_pos);
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
glm_cross(camera_front, camera_up, camera_cross);
glm_normalize(camera_cross);
glm_vec3_scale(camera_cross, camera_speed, camera_cross);
glm_vec3_sub(camera_pos, camera_cross, camera_pos);
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
glm_cross(camera_front, camera_up, camera_cross);
glm_normalize(camera_cross);
glm_vec3_scale(camera_cross, camera_speed, camera_cross);
glm_vec3_add(camera_pos, camera_cross, camera_pos);
}
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}