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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
#include "gl_graphics.h"
#define ATTR_COLOR 0x1 // 0001
#define ATTR_TEX 0x2 // 0010
// ============== Loading functions are not subject to change =================
void LoadUniformInt(u32 SP, const char *Uniform, i32 Val)
{
glUniform1i(glGetUniformLocation(SP, Uniform), Val);
}
void LoadUniformFloat(u32 SP, const char *Uniform, f32 Val)
{
glUniform1f(glGetUniformLocation(SP, Uniform), Val);
}
void LoadUniformVec3(u32 SP, const char *Uniform, Vec3 Val)
{
glUniform3f(glGetUniformLocation(SP, Uniform), Val.x, Val.y, Val.z);
}
void LoadUniformMat4(u32 SP, const char *Uniform, const f32 *Val)
{
glUniformMatrix4fv(glGetUniformLocation(SP, Uniform), 1, GL_TRUE, Val);
}
u32 CreateVertexShader(const char *VertexShaderSource)
{
u32 VertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(VertexShader, 1, &VertexShaderSource, NULL);
glCompileShader(VertexShader);
i32 Success;
char InfoLog[512];
glGetShaderiv(VertexShader, GL_COMPILE_STATUS, &Success);
if (!Success)
{
glGetShaderInfoLog(VertexShader, 512, NULL, InfoLog);
// todo(talha): error logging
// vertex shader compilation failed
printf("================================\n");
printf("vertex shader compilation failed:\n%s\n", InfoLog);
}
return VertexShader;
}
u32 CreateFragmentShader(const char *FragmentShaderSource)
{
u32 FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(FragmentShader, 1, &FragmentShaderSource, NULL);
glCompileShader(FragmentShader);
i32 Success;
char InfoLog[512];
glGetShaderiv(FragmentShader, GL_COMPILE_STATUS, &Success);
if (!Success)
{
glGetShaderInfoLog(FragmentShader, 512, NULL, InfoLog);
// todo(talha): error logging
// fragment shader compilation failed
printf("================================\n");
printf("fragment shader compilation failed:\n%s\n", InfoLog);
}
return FragmentShader;
}
u32 CreateShaderProgram(unsigned int VertexShader, unsigned int FragmentShader)
{
u32 ShaderProgram = glCreateProgram();
glAttachShader(ShaderProgram, VertexShader);
glAttachShader(ShaderProgram, FragmentShader);
glLinkProgram(ShaderProgram);
i32 Success;
char InfoLog[512];
glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &Success);
if (!Success)
{
glGetProgramInfoLog(ShaderProgram, 512, NULL, InfoLog);
// todo(talha): error logging
// program linking failed
printf("================================\n");
printf("program linking failed:\n%s\n", InfoLog);
}
glDeleteShader(VertexShader);
glDeleteShader(FragmentShader);
return ShaderProgram;
}
// ---------------------------------------------------------------------------
u32 CreateTriangle(f32 vertices[], i32 sz)
{
u32 VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// bind to setup triangle attributes
u32 VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sz, vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(f32), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(f32), (void*)(3*sizeof(f32)));
glEnableVertexAttribArray(1);
// unbind post setup
glBindVertexArray(0);
return VAO;
}
void DrawTriangle(u32 VAO)
{
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
}
u32 CreateRectangle(f32 vertices[], i32 v_sz, u32 indices[], i32 i_sz)
{
u32 VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// bind to setup triangle attributes
u32 VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, v_sz, vertices, GL_STATIC_DRAW);
u32 EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, i_sz, indices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(f32), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(f32), (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
// unbind post setup
glBindVertexArray(0);
return VAO;
}
BufferO CreateRectangleTextured(f32 vertices[], i32 v_sz, u32 indices[], i32 i_sz, u32 attr)
{
/*
* This will setup options and buffer objects to create a rectangle based on a texture
* The actual assigning of a texture will happen outside of this function
* Call BindGlTexture/GenAndBindGlTexture for assigning textures
*/
u32 VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// bind to setup triangle attributes
u32 VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, v_sz, vertices, GL_STATIC_DRAW);
u32 EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, i_sz, indices, GL_STATIC_DRAW);
// default is 3 for position
u32 stride = 3;
i32 color_offset = 3;
i32 tex_offset = 3;
if (attr & ATTR_COLOR)
{
stride += 3;
tex_offset += 3;
}
if (attr & ATTR_TEX)
{
stride += 2;
}
u8 attr_pos = 1;
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride*sizeof(f32), (void*)0);
glEnableVertexAttribArray(0);
if (attr & ATTR_COLOR)
{
// color attribute
glVertexAttribPointer(attr_pos, 3, GL_FLOAT, GL_FALSE, stride*sizeof(f32), (void*)(color_offset*sizeof(f32)));
glEnableVertexAttribArray(attr_pos);
attr_pos += 1;
}
if (attr & ATTR_TEX)
{
// texture attribute
glVertexAttribPointer(attr_pos, 2, GL_FLOAT, GL_FALSE, stride*sizeof(f32), (void*)(tex_offset*sizeof(f32)));
glEnableVertexAttribArray(attr_pos);
}
// unbind post setup
glBindVertexArray(0);
BufferO BO = {0};
BO.VAO=VAO;
BO.VBO=VBO;
BO.EBO=EBO;
return BO;
}
void MakeRectangleTextured(BufferO *bo, f32 vertices[], i32 v_sz, u32 indices[], i32 i_sz, u32 attr)
{
/*
* This will setup options and buffer objects to create a rectangle based on a texture
* The actual assigning of a texture will happen outside of this function
*/
glGenVertexArrays(1, &bo->VAO);
glBindVertexArray(bo->VAO);
// bind to setup triangle attributes
glGenBuffers(1, &bo->VBO);
glBindBuffer(GL_ARRAY_BUFFER, bo->VBO);
glBufferData(GL_ARRAY_BUFFER, v_sz, vertices, GL_STATIC_DRAW);
glGenBuffers(1, &bo->EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bo->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, i_sz, indices, GL_STATIC_DRAW);
// default is 3 for position
u32 stride = 3;
i32 color_offset = 3;
i32 tex_offset = 3;
if (attr & ATTR_COLOR)
{
stride += 3;
tex_offset += 3;
}
if (attr & ATTR_TEX)
{
stride += 2;
}
u8 attr_pos = 1;
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride*sizeof(f32), (void*)0);
glEnableVertexAttribArray(0);
if (attr & ATTR_COLOR)
{
// color attribute
glVertexAttribPointer(attr_pos, 3, GL_FLOAT, GL_FALSE, stride*sizeof(f32), (void*)(color_offset*sizeof(f32)));
glEnableVertexAttribArray(attr_pos);
attr_pos += 1;
}
if (attr & ATTR_TEX)
{
// texture attribute
glVertexAttribPointer(attr_pos, 2, GL_FLOAT, GL_FALSE, stride*sizeof(f32), (void*)(tex_offset*sizeof(f32)));
glEnableVertexAttribArray(attr_pos);
}
// unbind post setup
glBindVertexArray(0);
}
void DrawRectangle(u32 VAO)
{
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
void DrawRectangleTextured(u32 VAO, u32 TexO)
{
glBindTexture(GL_TEXTURE_2D, TexO);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
}
BufferO CreateCube(f32 vertices[], i32 v_sz)
{
u32 VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// bind to setup triangle attributes
u32 VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, v_sz, vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(f32), (void*)0);
glEnableVertexAttribArray(0);
// normal attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(f32), (void*)(3*sizeof(f32)));
glEnableVertexAttribArray(1);
// unbind post setup
glBindVertexArray(0);
BufferO BO = {0};
BO.VAO=VAO;
BO.VBO=VBO;
return BO;
}
BufferO CreateCubeTextured(f32 vertices[], i32 v_sz)
{
/*
* This will setup options and buffer objects to create a rectangle based on a texture
* The actual assigning of a texture will happen outside of this function
*/
u32 VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// bind to setup triangle attributes
u32 VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, v_sz, vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8*sizeof(f32), (void*)0);
glEnableVertexAttribArray(0);
// normal attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8*sizeof(f32), (void*)(3*sizeof(f32)));
glEnableVertexAttribArray(1);
// texture attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8*sizeof(f32), (void*)(6*sizeof(f32)));
glEnableVertexAttribArray(2);
// unbind post setup
glBindVertexArray(0);
BufferO BO = {0};
BO.VAO=VAO;
BO.VBO=VBO;
return BO;
}
void DrawCube(u32 VAO)
{
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
GLint GetColorAttrib(u32 nrChannels)
{
GLint color_attrib;
switch (nrChannels) {
case 1:
{
color_attrib = GL_RED;
} break;
case 3:
{
color_attrib = GL_RGB;
} break;
case 4:
{
color_attrib = GL_RGBA;
} break;
default:
{
assert(1 == 0);
break;
}
}
return color_attrib;
}
/*
* Defines texture loaded from a Texture2D object into opengl.
* Texture wrapping and filtering options are defined. Should they need be altered
* define a new function or change them inside. It depends on the art style, but
* I think it should remain the same for everything
*
* Parameters:
* 1. Texture2D Tex: Contains information about the loaded texture
* 2. u32 VAO: Contains the VertexArrayObject index for the VAO to attach the texture to
* 3. u32 *TexO: Pointer to a TextureObject. This needs to be adtivated by glActiveTexture
* in order to define texture properties
*/
void GenAndBindGlTexture(Texture2D Tex, u32 VAO, u32 *TexO)
{
glBindVertexArray(VAO);
glGenTextures(1, TexO);
glBindTexture(GL_TEXTURE_2D, *TexO);
// defining texture wrapping options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GLint color_attrib = GetColorAttrib(Tex.nrChannels);
glTexImage2D(GL_TEXTURE_2D, 0, color_attrib, Tex.width, Tex.height, 0, color_attrib, GL_UNSIGNED_BYTE, Tex.data);
glGenerateMipmap(GL_TEXTURE_2D);
glBindVertexArray(0);
}
/*
* Similar to GenAndBindGlTexture but it only binds a texture object
* must be passed a generated tex_obj
*/
void BindGlTexture(Texture2D Tex, u32 VAO, u32 TexO)
{
glBindVertexArray(VAO);
glBindTexture(GL_TEXTURE_2D, TexO);
// defining texture wrapping options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GLint color_attrib = GetColorAttrib(Tex.nrChannels);
glTexImage2D(GL_TEXTURE_2D, 0, color_attrib, Tex.width, Tex.height, 0, color_attrib, GL_UNSIGNED_BYTE, Tex.data);
glGenerateMipmap(GL_TEXTURE_2D);
glBindVertexArray(0);
}
|