-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
344 lines (280 loc) · 9.58 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
// Defining compiler directives to identify with OS is Apple. This is because Apple has its own subset
// functions for glGenVertexArrays, glBindVertexArray, glDeleteVertexArrays.
#ifdef __APPLE__
#define glGenVertexArrays glGenVertexArraysAPPLE
#define glBindVertexArray glBindVertexArrayAPPLE
#define glDeleteVertexArrays glDeleteVertexArraysAPPLE
#endif
// GL Error handing functions plus compiler directives
#define assert(x) if(!(x)) return -1;
#define GLCall(x) GLClearError();\
x;\
assert(GLLogCall(#x, __FILE__, __LINE__))\
static void GLClearError()
{
while (glGetError() != GL_NO_ERROR);
}
static bool GLLogCall(const char *function, const char *file, const int line)
{
// The breakdown of this compared to tutorial is
// quite different. Within the tutorial Cherno init and assigns GLenum error
// within the while statement clause. This causes all sorts of problems
// for the IDE, compiler etc.
int error;
while ((error = glGetError()) != GL_NO_ERROR)
{
printf("OpenGL Error code: %d\n", error);
printf("Error occurred at function: %s\n", function);
printf("File: %s\n", file);
printf("Line: %d\n\n", line);
return false;
}
return true;
}
// GLFW Error handing
void glfwErrorCallback(int error, const char description[]) {
printf("\nGLFW Error: %s\n\n", description);
}
// Shader parsing and struct. parseShader would be considered a public function.
struct ShaderSource
{
char VertexShader[512];
char FragmentShader[512];
};
struct ShaderSource parseShader(const char filepath[])
{
// instantiate a temp version of our struct
struct ShaderSource tmp;
// Open our target file for parsing. This is the shader source
FILE *fp = fopen(filepath, "r");
// Establish a buffer array for the file stream
const char buffer[512];
// unsigned char ptr array that has the same size array as the buffer and
// struct variables. This is probably not needed... Checking in on
// that.
unsigned char (*src)[512];
// Determiners... These values determine when parsing if source code pertains to
// a vertex or a fragment shader.
char divider[] = "#shader";
char version[] = "#version";
char v[] = "vertex";
char f[] = "fragment";
while(fgets(buffer, 512, fp))
{
if (strstr(buffer, divider))
{
if (strstr(buffer, v))
{
src = &tmp.VertexShader;
}
else if (strstr(buffer, f))
{
src = &tmp.FragmentShader;
}
}
else if (strstr(buffer, version))
{
strcpy(src, buffer);
}
else
{
strcat(src, buffer);
}
}
// Lets close that file as we don't need it.
fclose(fp);
// Return our parsed shader sources.
return tmp;
}
// Shader compiler. Embedded function, used within createShader
static unsigned int compileShader(unsigned int type, const char source[])
{
unsigned int id = glCreateShader(type);
glShaderSource(id, 1, &source, NULL);
glCompileShader(id);
// Syntax error checking needing for shader files
int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
int len;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &len);
char message[len];
glGetShaderInfoLog(id, len, &len, message);
printf("%s\n", message);
printf("FILE: %s\n", (type == GL_VERTEX_SHADER ? "vertex" : "fragment"));
glDeleteShader(id);
return 0;
}
return id;
};
// Public function createShader. Call to create shaders and attach shaders to program
unsigned int createShader(const char vertexShader[], const char fragmentShader[])
{
unsigned int program = glCreateProgram();
unsigned int vs = compileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = compileShader(GL_FRAGMENT_SHADER, fragmentShader);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
return program;
};
// Rendering functions for abstractions and easier implimentation...
// 1. Generating vertex buffer and quick binding and unbinding functions
// vertexBuffer function generates a buffer, binds it and assigns it data
// On successful pass return 0, otherwise GLCall will return -1 with corresponding
// error code.
int vertexBuffer(unsigned int buffer, const void* data, unsigned int size)
{
GLCall(glGenBuffers(1, &buffer));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, buffer));
GLCall(glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW));
return 0;
}
int vertexBufferBind(unsigned int buffer)
{
GLCall(glBindBuffer(GL_ARRAY_BUFFER, buffer));
return 0;
}
// Unbind vertext buffer.
int vertexBufferUnbind()
{
GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0));
return 0;
}
// 2. Index buffer abstracted functions.... Here we only bind the index buffer
// and bind the corresponding data to the ib object. We do not gen a buffer
// for the ibo, because this causes a program crash with no error returned.
// This function must be called after glGenBuffers(GLsizei n, GLuint* buffers)
int indexBuffer(unsigned int ibo, const unsigned int* data, unsigned int count)
{
GLCall(glGenBuffers(1, &ibo));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo));
GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, count, data, GL_STATIC_DRAW));
return 0;
}
int indexBufferBind(unsigned int ibo)
{
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo));
return 0;
}
int indexBufferUnbind()
{
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
return 0;
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
// Due to version limitation of Mac OSX openGL version
// GLFW_OPENGL_ANY_PROFILE is currently the only profile that is usable
// Otehrwise you'll received... GLFW Error: Context profiles are only defined for OpenGL version 3.2 and above
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
glfwSetErrorCallback(glfwErrorCallback);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
// GLEW implementation
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
return -1;
}
// Let's print the GLEW Version
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
// Let's print the target processor (your computer's integrated or external GPU) OpenGL Version
fprintf(stdout, "Status: Using OpenGL %s\n", glGetString(GL_VERSION));
fprintf(stdout, "GLSL Version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
// Define data Buffer for drawing
float positions[] = {
-0.5f, -0.5f,
0.5f, -0.5f,
0.5f, 0.5f,
-0.5f, 0.5f
};
unsigned int indices[] = {
0, 1, 2,
2, 3, 0
};
// Vertex array elements
unsigned int vao;
GLCall(glGenVertexArrays(1, &vao));
GLCall(glBindVertexArray(vao));
// Binding for vertex Buffer
unsigned int buffer;
vertexBuffer(buffer, positions, 4 * 2 * sizeof(float));
GLCall(glEnableVertexAttribArray(0));
GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0));
// Binding for index buffer
unsigned int ibo;
assert(sizeof(unsigned int) == sizeof(GLuint));
indexBuffer(ibo, indices, 6 * sizeof(unsigned int));
char filepath[] = "shaders/basic.shader";
struct ShaderSource Source = parseShader(filepath);
unsigned int shader = createShader(Source.VertexShader, Source.FragmentShader);
glUseProgram(shader);
int location = glGetUniformLocation(shader, "u_Color");
glUniform4f(location, 1.0, 0.0, 0.0, 1.0);
float r = 0.0f;
float increment = 0.05f;
// Unbind all buffers and shader programs
glUseProgram(0);
glBindVertexArray(0);
vertexBufferUnbind();
indexBufferUnbind();
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
// Bind shader program
glUseProgram(shader);
// Animation of colour
glUniform4f(location, r, 0.0, 0.0, 1.0);
// // Bind vertex array
GLCall(glBindVertexArray(vao));
// Index buffer binding
indexBufferBind(ibo);
// Draw pull or function..
GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, &indices));
// Colour animation.
if (r > 1.0f)
increment = -0.05f;
else if (r < 0.0f)
increment = 0.05f;
r += increment;
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glDeleteProgram(shader);
glDeleteBuffers(1, &buffer);
glDeleteBuffers(1, &ibo);
glDeleteVertexArrays(1, &vao);
glfwTerminate();
return 0;
}