commit a9d199ef032c4fc563c1191850e710de89406553
parent e53186814b7da75e6f1fed69983e2fe7d60df850
Author: Matsuda Kenji <info@mtkn.jp>
Date: Mon, 26 Jun 2023 18:38:29 +0900
add face
Diffstat:
5 files changed, 47 insertions(+), 36 deletions(-)
diff --git a/img/awesomeface.jpg b/img/awesomeface.jpg
Binary files differ.
diff --git a/img/awesomeface.png b/img/awesomeface.png
Binary files differ.
diff --git a/main.c b/main.c
@@ -12,6 +12,7 @@
#include "stb_image.h"
int rotating = 0;
+float alpha = 0.2;
void
framebuffer_size_callback(GLFWwindow *window, int width, int height)
@@ -33,6 +34,10 @@ processInput(GLFWwindow *window)
} else if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_RELEASE &&
spacePressing == 1) {
spacePressing = 0;
+ } else if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
+ alpha += 0.01;
+ } else if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
+ alpha -= 0.01;
}
}
@@ -42,19 +47,15 @@ main(void)
float theta = 0;
float delta = 0.01;
float vertices[] = {
- // positions, colors, texture coords
- 0.0, 0.8, 0.0, 1.0, 0.0, 0.0, 0.5, 1.0,
- 0.4*sqrtf(3), -0.4, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
- -0.4*sqrtf(3), -0.4, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
+ // positions, colors, texture coords
+ 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0,
+ -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
+ -0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
+ 0.5, -0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0,
};
- float texCoords[] = {
- 0.0, 0.0,
- 1.0, 0.0,
- 0.5, 1.0,
- };
-
unsigned int indices[] = {
0, 1, 2,
+ 0, 2, 3,
};
// initialize glfw
@@ -111,27 +112,34 @@ main(void)
GL_STATIC_DRAW);
// setup texture
- unsigned int texture;
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
- GL_LINEAR_MIPMAP_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- int width, height, nrChannels;
- char *texfile = "img/wall.jpg";
- unsigned char *data = stbi_load(texfile, &width, &height,
- &nrChannels, 0);
- if (data == NULL) {
- fprintf(stderr, "can't load texture: %s\n", texfile);
- exit(1);
+ int nTex = 2;
+ unsigned int textures[nTex];
+ char *texfiles[nTex];
+ texfiles[0] = "img/wall.jpg";
+ texfiles[1] = "img/awesomeface.jpg";
+ glGenTextures(nTex, textures);
+ for (int i = 0; i < nTex; i++) {
+ glActiveTexture(GL_TEXTURE0 + i);
+ glBindTexture(GL_TEXTURE_2D, textures[i]);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
+ GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
+ GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
+ GL_LINEAR_MIPMAP_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ int width, height;
+ unsigned char *data = stbi_load(texfiles[i], &width, &height,
+ NULL, 0);
+ if (data == NULL) {
+ fprintf(stderr, "can't load texture: %s\n", texfiles[i]);
+ exit(1);
+ }
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
+ GL_UNSIGNED_BYTE, data);
+ glGenerateMipmap(GL_TEXTURE_2D);
+ stbi_image_free(data);
}
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
- GL_UNSIGNED_BYTE, data);
- glGenerateMipmap(GL_TEXTURE_2D);
- stbi_image_free(data);
-
while (!glfwWindowShouldClose(window)) {
processInput(window);
@@ -142,7 +150,10 @@ main(void)
theta += delta;
}
ShaderSetFloat(shader, "theta", theta);
- glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
+ glUniform1i(glGetUniformLocation(shader->ID, "texture0"), 0);
+ glUniform1i(glGetUniformLocation(shader->ID, "texture1"), 1);
+ ShaderSetFloat(shader, "alpha", alpha);
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwPollEvents();
glfwSwapBuffers(window);
diff --git a/shaders/fragment.sl b/shaders/fragment.sl
@@ -1,11 +1,13 @@
#version 330 core
-in vec3 ourColor;
in vec2 texCoord;
-uniform sampler2D ourTexture;
+uniform sampler2D texture0;
+uniform sampler2D texture1;
+uniform float alpha;
out vec4 FragColor;
void
main()
{
- FragColor = texture(ourTexture, texCoord) * vec4(ourColor, 1.0);
+ FragColor = mix(texture(texture0, texCoord),
+ texture(texture1, 2*(vec2(1, 1)-texCoord)), alpha);
}
diff --git a/shaders/vertex.sl b/shaders/vertex.sl
@@ -2,7 +2,6 @@
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
-out vec3 ourColor;
out vec2 texCoord;
uniform float theta;
@@ -12,6 +11,5 @@ main()
vec2 pxy = aPos.xy;
mat2 rot = mat2(cos(theta), -sin(theta), sin(theta), cos(theta));
gl_Position = vec4(pxy*rot, aPos.z, 1.0);
- ourColor = aColor;
texCoord = aTexCoord;
}