generate random colored squares

This commit is contained in:
Robert 2020-09-02 12:32:00 +02:00
parent 2fae908a55
commit 48c2cd6386
5 changed files with 20 additions and 9 deletions

View file

@ -4,13 +4,15 @@
#include <string> #include <string>
#include <fstream> #include <fstream>
#define frand() ((float)rand() / (float)RAND_MAX)
PlotWindow::PlotWindow(int w, int h, int id, std::string title) : PlotWindow::PlotWindow(int w, int h, int id, std::string title) :
window(nullptr), id(id), window(nullptr), id(id),
vertices{ vertices{
-0.5f, 0.5f, 0.0f, -0.5f, 0.5f, 0.0f, frand(), frand(), frand(),
0.5f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f, frand(), frand(), frand(),
0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, frand(), frand(), frand(),
-0.5f, -0.5f, 0.0f -0.5f, -0.5f, 0.0f, frand(), frand(), frand()
}, },
indices{ indices{
0, 1, 3, 0, 1, 3,
@ -111,8 +113,10 @@ PlotWindow::PlotWindow(int w, int h, int id, std::string title) :
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, (3 + 3) * sizeof(float), (void*)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, (3 + 3) * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);

View file

@ -34,6 +34,6 @@ private:
ShaderProgram shader; ShaderProgram shader;
BufferObject VAO, VBO, EBO; BufferObject VAO, VBO, EBO;
float vertices[4 * 3]; float vertices[4 * 3 * 4];
unsigned int indices[2 * 3]; unsigned int indices[2 * 3];
}; };

View file

@ -9,6 +9,8 @@
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
srand(time(0));
int result = glfwInit(); int result = glfwInit();
if (result != GL_TRUE) if (result != GL_TRUE)
{ {

View file

@ -1,8 +1,9 @@
#version 460 core #version 460 core
out vec4 oColor; in vec4 vertexColor;
out vec4 fragColor;
void main() void main()
{ {
oColor = vec4(0.6f, 0.2f, 0.9f, 1.0f); fragColor = vertexColor;
} }

View file

@ -1,8 +1,12 @@
#version 460 core #version 460 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aCol;
out vec4 vertexColor;
void main() void main()
{ {
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); vertexColor = vec4(aCol, 1.0f);
gl_Position = vec4(aPos, 1.0f);
} }