| 1 | #include <GL/gl3w.h> |
| 2 |
|
| 3 | #include "GLRenderObject.hpp" |
| 4 | #include "GLRenderObjectRainbowTriangle.hpp" |
| 5 | #include "GLProgram.hpp" |
| 6 |
|
| 7 |
|
| 8 | GLRenderObjectRainbowTriangle::GLRenderObjectRainbowTriangle(): |
| 9 | GLRenderObject::GLRenderObject("RainbowTriangle", 1) // use GLProgram "RainbowTriangle", render w/ priority 1 |
| 10 | { |
| 11 |
|
| 12 | } |
| 13 |
|
| 14 | void GLRenderObjectRainbowTriangle::Generate() |
| 15 | { |
| 16 | float vertices[] = { |
| 17 | // For visualization: each row is a vertex. |
| 18 | // Each vertex has position [x, y, z] and color [r, g, b] |
| 19 | -0.5f, -0.5f, 1.0f, 1.0, 0.0, 0.0, // red color for this vertex |
| 20 | 0.5f, -0.5f, 1.0f, 0.0, 1.0, 0.0, // green color |
| 21 | 0.0f, 0.5f, 1.0f, 0.0, 0.0, 1.0, // blue color for our top vertex |
| 22 | }; |
| 23 |
|
| 24 | glGenVertexArrays(1, &m_VAO); |
| 25 | glBindVertexArray(m_VAO); |
| 26 |
|
| 27 | unsigned int VBO; |
| 28 | glGenBuffers(1, &VBO); |
| 29 |
|
| 30 | glBindBuffer(GL_ARRAY_BUFFER, VBO); |
| 31 |
|
| 32 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); |
| 33 |
|
| 34 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) 0); |
| 35 | glEnableVertexAttribArray(0); |
| 36 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); |
| 37 | glEnableVertexAttribArray(1); |
| 38 | } |
| 39 |
|
| 40 | void GLRenderObjectRainbowTriangle::Render() |
| 41 | { |
| 42 | if (!m_renderEnabled) { return; } |
| 43 |
|
| 44 | if (!m_geometryGenerated) |
| 45 | { |
| 46 | Generate(); |
| 47 | m_geometryGenerated = true; |
| 48 | } |
| 49 | GLTargetSetup(); // generic GLRenderObject.GLTargetSetup(), calls glUseProgram() and glUniformX() |
| 50 |
|
| 51 | glBindVertexArray(m_VAO); |
| 52 | glDrawArrays(GL_TRIANGLES, 0, 3); |
| 53 | } |
| 54 |
|