-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
154 lines (125 loc) · 4.46 KB
/
main.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
#include "glad/glad.h"
#include <stdio.h>
#include "window/window.h"
#include "rendering/shaders/shaders.h"
#include "rendering/vbo/vbo.h"
#include "camera/camera.h"
#define WIREFRAME 0x00
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
double timeI, timeF, delta_time;
const char *vertexShaderSource = "#version 330 core\n"
"\n"
"layout (location = 0) in vec3 aPos;\n"
"\n"
"uniform mat4 proj;\n"
"uniform mat4 view;\n"
"\n"
"void main() {\n"
" gl_Position = proj * view * vec4(aPos, 1.0);\n"
"}\n\0";
const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main() {\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
int main()
{
GLFWwindow* window = create_main_window();
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
fprintf(stderr, "Failed to initialize GLAD.\n");
return -1;
}
// Create and compile shaders
Shader vertex_shader = {glCreateShader(GL_VERTEX_SHADER), vertexShaderSource};
compile_shader(vertex_shader);
Shader fragment_shader = {glCreateShader(GL_FRAGMENT_SHADER), fragmentShaderSource};
compile_shader(fragment_shader);
// link shaders
ShaderProgram shaderProgram = { glCreateProgram() };
attach_shader(shaderProgram, vertex_shader);
attach_shader(shaderProgram, fragment_shader);
link_shader_program(shaderProgram);
destroy_shader(vertex_shader);
destroy_shader(fragment_shader);
// Vertices needed to make a cube
float vertices[] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
};
int attributes[] = {
3
};
VAO vao = {0x00};
glGenVertexArrays(1, &vao.id);
VBO vbo = {0x00, vertices, sizeof(vertices), (sizeof(attributes) / sizeof(int)), attributes};
create_vbo(vbo, vao);
// Set the background color
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
#if WIREFRAME
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
#endif
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// Get the time this frame started (used to calculate delta_time at the end)
timeI = glfwGetTime();
// Update Camera
update_camera(shaderProgram, delta_time);
// Clear frame
glClear(GL_COLOR_BUFFER_BIT);
// Render frame
glUseProgram(shaderProgram.program_id);
glBindVertexArray(vao.id);
glDrawArrays(GL_TRIANGLES, 0, sizeof(vertices) / sizeof(float));
glBindVertexArray(0);
// Display frame
glfwSwapBuffers(window);
// Read window events that happened during this frame
glfwPollEvents();
// Calculate delta_time
timeF = glfwGetTime();
delta_time = timeF - timeI;
}
glDeleteVertexArrays(1, &vao.id);
glDeleteBuffers(1, &vbo.id);
glDeleteProgram(shaderProgram.program_id);
glfwTerminate();
return 0;
}