forked from yahiaetman/OpenGL-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex05_attributes.cpp
77 lines (60 loc) · 2.88 KB
/
ex05_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
#include <application.hpp>
#include <shader.hpp>
#include <iostream>
class AttributesApplication : public our::Application {
our::ShaderProgram program;
GLuint vertex_array = 0, vertex_buffer = 0;
our::WindowConfiguration getWindowConfiguration() override {
return { "Attributes", {1280, 720}, false };
}
void onInitialize() override {
program.create();
program.attach("assets/shaders/ex05_attributes/attribute_position.vert", GL_VERTEX_SHADER);
program.attach("assets/shaders/ex02_shader_introduction/red.frag", GL_FRAGMENT_SHADER);
program.link();
glGenVertexArrays(1, &vertex_array);
glBindVertexArray(vertex_array);
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
//#define USE_GLM_VEC3
#ifndef USE_GLM_VEC3
float positions[] = {
-0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
0.0, 0.5, 0.0
};
#else
glm::vec3 positions[] = {
{-0.5, -0.5, 0.0},
{ 0.5, -0.5, 0.0},
{ 0.0, 0.5, 0.0}
};
#endif
glBufferData(GL_ARRAY_BUFFER, 3*3*sizeof(float), positions, GL_STATIC_DRAW); // function specifically targeted to copy user-defined data into the currently bound buffer.
// Its first argument is the type of the buffer we want to copy data into:
// the vertex buffer object currently bound to the GL_ARRAY_BUFFER target.
// The second argument specifies the size of the data (in bytes) we want to pass to the buffer;
// a simple sizeof of the vertex data suffices. The third parameter is the actual data we want to send.
GLuint position_attribute_location = glGetAttribLocation(program, "position");
std::cout << "Position Attribute Location: " << position_attribute_location << std:: endl;
glVertexAttribPointer(position_attribute_location, 3, GL_FLOAT, false, 0, (void*)0);
glEnableVertexAttribArray(position_attribute_location);
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, &vertex_buffer);
}
};
int main(int argc, char** argv) {
return AttributesApplication().run();
}