checkeboard pattern

This commit is contained in:
Luca Conte 2025-03-27 15:55:41 +01:00
parent ddfe9bfc3c
commit 0c7aae80c7
3 changed files with 15 additions and 51 deletions

View File

@ -129,14 +129,14 @@ void init(void) {
*/
GLfloat vertices[] = {
// X // Y // vertValue
-0.75f, -0.1f, 0.0f,
-0.75f, 0.1f, 0.0f,
0.75f, 0.1f, 1.0f,
// X // Y // tX // tY
-1.0f, -1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
-0.75f, -0.1f, 0.0f,
0.75f, -0.1f, 1.0f,
0.75f, 0.1f, 1.0f
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f
};
DEBUG("Creating vertext buffer");
@ -158,12 +158,12 @@ void init(void) {
2, // number of values to read
GL_FLOAT, // type of value
GL_FALSE, // if values are normalised
3 * sizeof(GLfloat), // stride - distance between vertices
4 * sizeof(GLfloat), // stride - distance between vertices
0 // start offset
);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
@ -190,15 +190,7 @@ void draw(void) {
glBindVertexArray(vertexArrayObject);
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);
}
glDrawArrays(GL_TRIANGLES, 0, 6);
}

View File

@ -1,31 +1,8 @@
#version 330 core
in float fragValue;
flat in int pattern;
in vec2 fragValue;
void main() {
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);
}
gl_FragColor = vec4(vec3(step(0, sin(fragValue.x * 3.14 * 4) * sin(fragValue.y * 3.14 * 4))), 1.0);
}

View File

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