summaryrefslogtreecommitdiff
path: root/main.c
blob: 0ebccf8f76ac5fbbca62549d4223f87317787410 (plain) (blame)
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
#define GLFW_INCLUDE_NONE
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <GL/gl.h>
#include <stdio.h>

// struct and function declaration
// ----------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
// ----------------------------------------------------------------------------
// END

int main(void) {
    int w_width = 1280;
    int w_height = 820;
    int shader_success;
    unsigned int VBO, VAO, EBO;
    // fragment shader
    unsigned int vertex_shader;
    unsigned int fragment_shader;
    unsigned int shader_program;

    char info_Log[512];
    // vertex shader source
    const char* vertex_shader_source =
	"#version 460 core\n"
	"layout (location = 0) in vec3 aPos;\n"
	"\n"
	"void main() {\n"
	    "gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
	"}\n\0";
    // Fragment shader
    // ---------------
    const char* fragment_shader_source =
	"#version 460 core\n"
	"out vec4 FragColor;\n"
	"\n"
	"void main() {\n"
	    "FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
	"}\n\0";

    // Triangle shenanigans and now two of them!
    // -----------------------------------------
    float verticies[] = {
	-0.5f, -0.5f, 0.0f, 	// bottom left
	 0.5f, -0.5f, 0.0f, 	// bottom right
	-0.5f,  0.5f, 0.0f,  	// top left
	 0.5f,  0.5f, 0.0f  	// top right
    };
    unsigned int indices[] = {
	0, 1, 2,	// first triangle
	2, 3, 1		// second triangle
    };


    // GLFW settings init and which version of opengl we should use
    // and which subset of opengl profiles
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    
    

    // glfw window creation
    GLFWwindow* window = glfwCreateWindow(w_width, w_height, "Test", NULL, NULL);
    if (window == NULL) {
	printf("Failed to create GLFW window\n");
	glfwTerminate();
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
	return -1;
    }
    // init window
    glfwMakeContextCurrent(window);

    // init GLAD
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
	printf("Failed to initialize GLAD\n");
	return -1;
    }

    // opengl viewport
    framebuffer_size_callback(window, w_width, w_height);


    // build and compile our shader program
    // ------------------------------------

    // vertex_shader
    vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
    glCompileShader(vertex_shader);

    // check for compilation error
    glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &shader_success);
    if (!shader_success) {
	glGetShaderInfoLog(vertex_shader, 512, NULL, info_Log);
	printf("ERROR SHADER VERTEX COMPILATION FAILED: %s\n", info_Log);
    }

    // fragment_shader
    fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
    glCompileShader(fragment_shader);

    // check for compilation error
    glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &shader_success);
    if (!shader_success) {
	glGetShaderInfoLog(fragment_shader, 512, NULL, info_Log);
	printf("ERROR SHADER FRAGMENT COMPILATION FAILED: %s\n", info_Log);
    }

    // create shader program
    // ---------------------
    shader_program = glCreateProgram();
    // attach to shader program id
    glAttachShader(shader_program, vertex_shader);
    glAttachShader(shader_program, fragment_shader);
    glLinkProgram(shader_program);

    // check for linking erros
    glGetProgramiv(shader_program, GL_LINK_STATUS, &shader_success);
    if (!shader_success) {
	glGetProgramInfoLog(shader_program, 512, NULL, info_Log);
	printf("ERROR WHILE LINKIN THE SHADERS TO THE PROGRAM: %s\n", info_Log);
    }

    // delete now unneeded shaders
    glDeleteShader(vertex_shader);
    glDeleteShader(fragment_shader);

    // set up vertex data (and buffers) and configure vertex attributes
    // ----------------------------------------------------------------
    
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);

    // bind the vertex array object first, then bind and set vertex buffers, and then configure vertex attributes.
    glBindVertexArray(VAO);

    // 0. copy our vertices array in a buffer for OpenGL to use
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verticies), verticies, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    // 1. then set the vertex attributes pointer
    // location of array, size of vertex attribute (vec3), type of data(float), normialition?, stride (space between data in array), offset of where the position data begins in the buffer
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);


    // uncomment this call to draw in wireframe polygons.
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    // -------------------------------------------------------------------------
    // render loop
    // -------------------------------------------------------------------------
    while (!glfwWindowShouldClose(window)) {
	// input
	// -----
	processInput(window);

	// rendering commands here
	// -----------------------
	static GLclampf c = 0.0f;
	glClearColor(c, c, c, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT);
	
	c += 1.0f / 256.0f;
	if (c >= 1.0f) c = 0.0f;

	// render triangle
	glUseProgram(shader_program);
	glBindVertexArray(VAO);
	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

	// check and call events and swap the buffers
	// ------------------------------------------
	glfwSwapBuffers(window);
	glfwPollEvents();
    }

    // de-allocate all resources
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glDeleteBuffers(1, &EBO);
    glDeleteProgram(shader_program);

    // Terminate program
    // -----------------
    glfwTerminate();
    return 0;
}

// END render loop
// -----------------------------------------------------------------------------

// Functions
// -----------------------------------------------------------------------------
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
    glViewport(0, 0, width, height);
}

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow* window) {
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, 1);
    if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) glfwSetWindowShouldClose(window, 1);
}