This commit is contained in:
Luca Conte 2024-03-21 13:54:39 +01:00
parent 0789faff80
commit dccf1139df
1 changed files with 69 additions and 6 deletions

View File

@ -3,9 +3,20 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <math.h>
GLuint program;
GLuint vao;
GLfloat hue = 0.0f;
GLfloat hueshift = 0.1f;
GLfloat offsetX = -0.75f;
GLfloat offsetY = 0.4f;
GLfloat speedX = 0.0005f;
GLfloat speedY = -0.0005f;
#define NUM_TRIANGLES 8
void createRect(GLfloat* vertexArray, GLfloat posx, GLfloat posy, GLfloat width, GLfloat height) {
@ -38,9 +49,10 @@ void init(void) {
"#version 330 core\n"
"layout (location = 0) in vec2 aPosition;\n"
"uniform vec3 color;\n"
"uniform vec2 offset;"
"out vec3 vertexColor;\n"
"void main() {\n"
" gl_Position = vec4(aPosition, 0.0, 1.0);\n"
" gl_Position = vec4(aPosition + offset, 0.0, 1.0);\n"
" vertexColor = color;"
"}\n";
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
@ -118,10 +130,10 @@ void init(void) {
// };
GLfloat triangleVertices[48];
createCenterRect(&triangleVertices[0], -0.3f, 0.0f, 0.1f, 1.2f);
createCenterRect(&triangleVertices[12], 0.3f, 0.0f, 0.1f, 1.2f);
createCenterRect(&triangleVertices[24], 0.0f, 0.0f, 0.5f, 0.1f);
createCenterRect(&triangleVertices[36], 0.0f, -0.8f, 0.7f, 0.1f);
createCenterRect(&triangleVertices[0], -0.2f, 0.0f, 0.1f, 1.2f);
createCenterRect(&triangleVertices[12], 0.2f, 0.0f, 0.1f, 1.2f);
createCenterRect(&triangleVertices[24], 0.0f, 0.0f, 0.3f, 0.1f);
createCenterRect(&triangleVertices[36], 0.0f, -0.8f, 0.5f, 0.1f);
GLuint triangleVertexBufferObject;
@ -152,13 +164,64 @@ void init(void) {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
}
void hueToRgb(GLfloat hue, GLfloat* r, GLfloat* g, GLfloat* b) {
GLfloat x = hue / 60;
while (x > 2) x -= 2;
x = x - 1;
x = 1 - fabs(x);
if (hue < 60) {
*r = 1.0f;
*g = x;
*b = 0;
} else if (hue < 120) {
*r = x;
*g = 1.0f;
*b = 0;
} else if (hue < 180) {
*r = 0;
*g = 1.0f;
*b = x;
} else if (hue < 240) {
*r = 0;
*g = x;
*b = 1.0f;
} else if (hue < 300) {
*r = x;
*g = 0;
*b = 1.0f;
} else {
*r = 1.0f;
*g = 0;
*b = x;
}
}
void draw(void) {
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glBindVertexArray(vao);
hue += hueshift;
if (hue > 360) hue -= 360;
offsetX += speedX;
offsetY += speedY;
if (offsetX >= 0.75f || offsetX <= -0.75f) speedX *= -1;
if (offsetY >= 0.4f || offsetY <=-0.15f) speedY *= -1;
GLfloat r;
GLfloat g;
GLfloat b;
hueToRgb(hue, &r, &g, &b);
// SET COLOR
glUniform3f(glGetUniformLocation(program, "color"), 0.7719f, 0.2105f, 0.0175f);
// glUniform3f(glGetUniformLocation(program, "color"), 0.7719f, 0.2105f, 0.0175f);
glUniform3f(glGetUniformLocation(program, "color"), r, g, b);
glUniform2f(glGetUniformLocation(program, "offset"), offsetX, offsetY);
glDrawArrays(GL_TRIANGLES, 0, NUM_TRIANGLES * 3);