-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.cpp
133 lines (105 loc) · 3.01 KB
/
shapes.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
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
#include "shapes.h"
Rectangle::Rectangle() : Rectangle(0.0f, 0.0f, black)
{
offset = glm::vec2(0.0f);
width = 0.0f;
height = 0.0f;
color = glm::vec3(0.0f);
transparency = 0.0f;
shader = Shader("vertexShader_shapes.vert", "fragmentShader_shapes.frag");
}
Rectangle::Rectangle(float x, float y, float width, float height,
glm::vec3 color)
{
offset = glm::vec2(x, y);
this->width = width;
this->height = height;
this->color = color;
transparency = 0.0f;
shader = Shader("vertexShader_shapes.vert", "fragmentShader_shapes.frag");
setup();
}
Rectangle::Rectangle(float x, float y, float width, float height,
glm::vec3 color, float transparency)
{
offset = glm::vec2(x, y);
this->width = squareSize;
this->height = squareSize;
this->color = color;
this->transparency = transparency;
shader = Shader("vertexShader_shapes.vert", "fragmentShader_shapes.frag");
setup();
}
Rectangle::Rectangle(float x, float y, glm::vec3 color)
{
offset = glm::vec2(x, y);
this->width = squareSize;
this->height = squareSize;
this->color = color;
transparency = 0.0f;
shader = Shader("vertexShader_shapes.vert", "fragmentShader_shapes.frag");
setup();
}
Rectangle::Rectangle(float x, float y, glm::vec3 color, float transparency)
{
offset = glm::vec2(x, y);
this->width = squareSize;
this->height = squareSize;
this->color = color;
this->transparency = transparency;
shader = Shader("vertexShader_shapes.vert", "fragmentShader_shapes.frag");
setup();
}
Rectangle::Rectangle(Position pos, glm::vec3 color, float transparency)
{
offset = posToCoord(pos);
this->width = squareSize;
this->height = squareSize;
this->color = color;
this->transparency = transparency;
shader = Shader("vertexShader_shapes.vert", "fragmentShader_shapes.frag");
setup();
}
void Rectangle::setup()
{
float x = offset.x;
float y = offset.y;
float vertices[8] = {
// bottom left
(2.0f / SCR_WIDTH) * x - 1.0f, (2.0f / 768.0f) * y - 1.0f,
// top left
(2.0f / SCR_HEIGHT) * x - 1.0f, (2.0f / 768.0f) * (y + height) - 1.0f,
// bottom right
(2.0f / SCR_WIDTH) * (x + width) - 1.0f, (2.0f / 768.0f) * y - 1.0f,
// top right
(2.0f / SCR_WIDTH) * (x + width) - 1.0f,
(2.0f / SCR_HEIGHT) * (y + height) - 1.0f
};
unsigned int indices[6] = {
0, 1, 2, // first triangle
1, 2, 3 // second triangle
};
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
GL_STATIC_DRAW);
// positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Rectangle::draw()
{
shader.use();
shader.set3f("color", color);
shader.set1f("transparency", transparency);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}