| 158 | } |
| 159 | |
| 160 | void opengl_embed::draw_rectangle(const double x1, const double x2, |
| 161 | const double y1, const double y2, |
| 162 | const std::array<float, 4> &color) { |
| 163 | // Create and bind vertex array object |
| 164 | unsigned int VAO; |
| 165 | glGenVertexArrays(1, &VAO); |
| 166 | glBindVertexArray(VAO); |
| 167 | |
| 168 | // Copy vertex data into the buffer's memory |
| 169 | std::vector<float> vertices = { |
| 170 | // x, y, z, r, g, b |
| 171 | static_cast<float>(x2), static_cast<float>(y2), // top right |
| 172 | static_cast<float>(x2), static_cast<float>(y1), // bottom right |
| 173 | static_cast<float>(x1), static_cast<float>(y1), // bottom left |
| 174 | static_cast<float>(x1), static_cast<float>(y2) // top left |
| 175 | }; |
| 176 | |
| 177 | // Create and bind vertex buffer object |
| 178 | unsigned int VBO; |
| 179 | glGenBuffers(1, &VBO); |
| 180 | glBindBuffer(GL_ARRAY_BUFFER, VBO); |
| 181 | glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_DYNAMIC_DRAW); |
| 182 | |
| 183 | std::vector<unsigned int> indices = { // note that we start from 0! |
| 184 | 0, 1, 3, // first triangle |
| 185 | 1, 2, 3 // second triangle |
| 186 | }; |
| 187 | |
| 188 | // Element buffer |
| 189 | unsigned int EBO; |
| 190 | glGenBuffers(1, &EBO); |
| 191 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); |
| 192 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_DYNAMIC_DRAW); |
| 193 | |
| 194 | // Set the vertex attributes pointers |
| 195 | int vertex_attribute_location = 0; |
| 196 | size_t stride = 2 * sizeof(float); |
| 197 | glVertexAttribPointer(vertex_attribute_location, 2, GL_FLOAT, GL_FALSE, static_cast<GLsizei>(stride), (void *)0); |
| 198 | glEnableVertexAttribArray(0); |
| 199 | |
| 200 | // Set the color attribute pointers |
| 201 | // int color_attribute_location = 1; |
| 202 | // glVertexAttribPointer(color_attribute_location, 3, GL_FLOAT, GL_FALSE, stride, (void *)(3*sizeof(float))); |
| 203 | // glEnableVertexAttribArray(1); |
| 204 | |
| 205 | // Activate our shader program |
| 206 | glUseProgram(draw_2d_single_color_shader_program_); |
| 207 | |
| 208 | // Set window size |
| 209 | int windowHeightLocation = glGetUniformLocation(draw_2d_single_color_shader_program_, "windowHeight"); |
| 210 | if (windowHeightLocation == -1) { |
| 211 | throw std::runtime_error("can't find uniform location"); |
| 212 | } |
| 213 | glUniform1f(windowHeightLocation, static_cast<float>(height())); |
| 214 | |
| 215 | int windowWidthLocation = glGetUniformLocation(draw_2d_single_color_shader_program_, "windowWidth"); |
| 216 | if (windowWidthLocation == -1) { |
| 217 | throw std::runtime_error("can't find uniform location"); |
no test coverage detected