-
Notifications
You must be signed in to change notification settings - Fork 12
/
ex06_multiple_attributes.cpp
78 lines (58 loc) · 2.35 KB
/
ex06_multiple_attributes.cpp
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
#include <application.hpp>
#include <shader.hpp>
class AttributesApplication : public our::Application {
our::ShaderProgram program;
GLuint vertex_array = 0;
GLuint position_vbo = 0, color_vbo = 0;
our::WindowConfiguration getWindowConfiguration() override {
return { "Multiple Attributes", {1280, 720}, false };
}
void onInitialize() override {
program.create();
program.attach("assets/shaders/ex06_multiple_attributes/multiple_attributes.vert", GL_VERTEX_SHADER);
program.attach("assets/shaders/ex04_varyings/varying_color.frag", GL_FRAGMENT_SHADER);
program.link();
glGenVertexArrays(1, &vertex_array);
glBindVertexArray(vertex_array);
glGenBuffers(1, &position_vbo);
glBindBuffer(GL_ARRAY_BUFFER, position_vbo);
glm::vec3 positions[] = {
{-0.5, -0.5, 0.0},
{ 0.5, -0.5, 0.0},
{ 0.0, 0.5, 0.0}
};
glBufferData(GL_ARRAY_BUFFER, 3*3*sizeof(float), positions, GL_STATIC_DRAW);
// Setting attribute for (0) the postion.
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, (void*)0);
glEnableVertexAttribArray(0);
glGenBuffers(1, &color_vbo);
glBindBuffer(GL_ARRAY_BUFFER, color_vbo);
glm::vec<4, glm::uint8, glm::defaultp> colors[] = {
{255, 0, 0, 255},
{ 0, 255, 0, 255},
{ 0, 0, 255, 255}
};
glBufferData(GL_ARRAY_BUFFER, 3*4*sizeof(glm::uint8), colors, GL_STATIC_DRAW);
// Setting attribute for (1) the color, same as before replicating the process changing the size to 4*sizeof(glm::uint8)
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, true, 0, (void*)0);
glEnableVertexAttribArray(1);
glBindVertexArray(0);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
void onDraw(double deltaTime) override {
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glBindVertexArray(vertex_array);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
}
void onDestroy() override {
program.destroy();
glDeleteVertexArrays(1, &vertex_array);
glDeleteBuffers(1, &position_vbo);
glDeleteBuffers(1, &color_vbo);
}
};
int main(int argc, char** argv) {
return AttributesApplication().run();
}