commit 79beae0de5eedfb18f346f5da5e732b6d19912c1
parent 944c21151c565baef62ca1101b5b9d60d3479f6e
Author: Matsuda Kenji <info@mtkn.jp>
Date: Mon, 26 Jun 2023 07:59:10 +0900
rotate using uniform
Diffstat:
4 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/main.c b/main.c
@@ -20,7 +20,6 @@ unsigned int indices[] = {
1, 2, 3,
};
-
void
framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
@@ -44,7 +43,7 @@ main(void)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// create glfw window
- GLFWwindow *window = glfwCreateWindow(800, 600, "opengl", NULL, NULL);
+ GLFWwindow *window = glfwCreateWindow(600, 600, "opengl", NULL, NULL);
if (window == NULL) {
fprintf(stderr, "main: failed to open window\n");
glfwTerminate();
@@ -86,6 +85,9 @@ main(void)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
GL_STATIC_DRAW);
+
+ float theta = 0;
+ float delta = 0.01;
while (!glfwWindowShouldClose(window)) {
processInput(window);
@@ -93,6 +95,8 @@ main(void)
glClearColor(1.0, 1.0, 0.8, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
+ theta += delta;
+ ShaderSetFloat(shader, "theta", theta);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwPollEvents();
diff --git a/main.h b/main.h
@@ -13,3 +13,5 @@ char *readfile(const char *file);
Shader *ShaderInit(const char *vs, const char *fs);
void ShaderUse(Shader *s);
void ShaderDelete(Shader *s);
+void ShaderSetFloat(Shader *s, const char *name, float value);
+
diff --git a/shader.c b/shader.c
@@ -134,3 +134,8 @@ ShaderDelete(Shader *s)
glDeleteProgram(s->ID);
}
+void
+ShaderSetFloat(Shader *s, const char *name, float value)
+{
+ glUniform1f(glGetUniformLocation(s->ID, name), value);
+}
diff --git a/shaders/vertex.sl b/shaders/vertex.sl
@@ -1,11 +1,14 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
+uniform float theta;
out vec3 ourColor;
void
main()
{
- gl_Position = vec4(aPos, 1.0);
+ vec2 pxy = aPos.xy;
+ mat2 rot = mat2(sin(theta), -cos(theta), cos(theta), sin(theta));
+ gl_Position = vec4(pxy * rot, aPos.z, 1.0);
ourColor = aColor;
}