This commit is contained in:
Luca Conte 2025-03-11 11:29:59 +01:00
parent 4cc7e98998
commit ddfe9bfc3c
3 changed files with 57 additions and 97 deletions

View File

@ -129,81 +129,14 @@ void init(void) {
*/
GLfloat vertices[] = {
// X // Y
// X // Y // vertValue
-0.75f, -0.1f, 0.0f,
-0.75f, 0.1f, 0.0f,
0.75f, 0.1f, 1.0f,
// left vertical bar
-0.35f, -0.6f,
-0.35f, 0.6f,
-0.2f, 0.6f,
-0.35f, -0.6f,
-0.2f, 0.6f,
-0.2f, -0.6f,
// right vertical bar
0.35f, -0.6f,
0.35f, 0.6f,
0.2f, 0.6f,
0.35f, -0.6f,
0.2f, 0.6f,
0.2f, -0.6f,
// middle bar
-0.2f, -0.1f,
-0.2f, 0.1f,
0.2f, 0.1f,
-0.2f, -0.1f,
0.2f, 0.1f,
0.2f, -0.1f,
// bottom bar
-0.35f, -0.7f,
-0.35f, -0.9f,
0.35f, -0.9f,
-0.35f, -0.7f,
0.35f, -0.9f,
0.35f, -0.7f
};
GLfloat colors[] = {
// R // G // B
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.3f,
1.0f, 0.3f, 0.0f,
1.0f, 0.3f, 0.3f,
1.0f, 0.3f, 0.6f,
1.0f, 0.6f, 0.3f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.3f,
0.3f, 1.0f, 0.0f,
0.3f, 1.0f, 0.3f,
0.3f, 1.0f, 0.6f,
0.6f, 1.0f, 0.3f,
0.3f, 0.3f, 1.0f,
0.3f, 0.6f, 1.0f,
0.6f, 0.3f, 1.0f,
0.3f, 0.3f, 1.0f,
0.3f, 0.6f, 1.0f,
0.6f, 0.3f, 1.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.1f,
1.0f, 1.0f, 0.2f,
1.0f, 1.0f, 0.7f,
1.0f, 1.0f, 0.8f,
1.0f, 1.0f, 0.9f
-0.75f, -0.1f, 0.0f,
0.75f, -0.1f, 1.0f,
0.75f, 0.1f, 1.0f
};
DEBUG("Creating vertext buffer");
@ -213,13 +146,6 @@ void init(void) {
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
DEBUG("Creating colour buffer");
GLuint colorBuffer;
glGenBuffers(1, &colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
DEBUG("Creating vertex array object");
// create vertex array object
glGenVertexArrays(1, &vertexArrayObject);
@ -232,17 +158,14 @@ void init(void) {
2, // number of values to read
GL_FLOAT, // type of value
GL_FALSE, // if values are normalised
2 * sizeof(GLfloat), // stride - distance between vertices
3 * sizeof(GLfloat), // stride - distance between vertices
0 // start offset
);
glEnableVertexAttribArray(0);
// vertex color data
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);
glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
@ -265,11 +188,18 @@ void draw(void) {
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
GLuint positionLocation = glGetUniformLocation(program, "uPosition");
glUniform2f(positionLocation, cos(currentHue * 2 * M_PI) * 0.2f, sin(currentHue * 2 * M_PI) * 0.2f);
glBindVertexArray(vertexArrayObject);
glDrawArrays(GL_TRIANGLES, 0, 48);
GLuint yPositionLocation = glGetUniformLocation(program, "uYPosition");
GLuint patternLocation = glGetUniformLocation(program, "uPattern");
for (int i = 0; i < 5; i++) {
glUniform1f(yPositionLocation, i / 4.0f * -1.4f + 0.7f);
glUniform1i(patternLocation, i);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {

View File

@ -1,5 +1,31 @@
#version 330 core
in vec3 vertexColor;
in float fragValue;
flat in int pattern;
void main() {
gl_FragColor = vec4(vertexColor, 1.0);
vec4 fragColor1 = vec4(1.0, 0.0, 0.0, 1.0);
vec4 fragColor2 = vec4(0.0, 1.0, 0.0, 1.0);
switch (pattern) {
case 0:
gl_FragColor = mix(fragColor1, fragColor2, fragValue);
break;
case 1:
gl_FragColor = mix(fragColor1, fragColor2, step(0.5, fragValue));
break;
case 2:
gl_FragColor = mix(fragColor1, fragColor2, smoothstep(0.4, 0.6, fragValue));
break;
case 3:
gl_FragColor = mix(fragColor1, fragColor2, sin(fragValue * 4 * 3.14159) * 0.5 + 0.5);
break;
case 4:
gl_FragColor = mix(fragColor1, fragColor2, step(0.5, sin(fragValue * 4 * 3.14159) * 0.5 + 0.5));
break;
default:
gl_FragColor = vec4(0.0);
}
}

View File

@ -1,11 +1,15 @@
#version 330 core
layout (location = 0) in vec2 aPosition;
layout (location = 1) in vec3 aColor;
uniform vec2 uPosition;
layout (location = 1) in float vertValue;
out vec3 vertexColor;
uniform float uYPosition;
uniform int uPattern;
out float fragValue;
flat out int pattern;
void main() {
vertexColor = aColor;
gl_Position = vec4(uPosition + aPosition, 0.0, 1.0);
pattern = uPattern;
fragValue = vertValue;
gl_Position = vec4(aPosition.x, aPosition.y + uYPosition, 0.0, 1.0);
}