summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2020-05-21 10:16:59 -0500
committersanine-a <sanine.not@pm.me>2020-05-21 10:16:59 -0500
commit8b7e8a1a3e77b4e3b56ea9025af8b254faa16c13 (patch)
tree3f39bb785d5f1df1f026589a6cc1aa2749e30e6d
parent9b66b322b600ab0db66cb6f3ff5a7c4196b9193f (diff)
add textured cube primitive
-rw-r--r--demo.c42
-rw-r--r--demo.fs7
-rw-r--r--demo.vs7
-rw-r--r--include/primitives.h18
-rw-r--r--include/texture.h7
-rw-r--r--src/primitives.c73
-rw-r--r--src/texture.c7
7 files changed, 119 insertions, 42 deletions
diff --git a/demo.c b/demo.c
index 2213dc7..e2ef74a 100644
--- a/demo.c
+++ b/demo.c
@@ -132,10 +132,8 @@ void draw() {
honey_shader_set_matrix_4fv(shader, "model", (float*) model);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, container.texture_id);
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, happy_face.texture_id);
+ honey_texture_use(container, 0);
+ honey_texture_use(happy_face, 1);
honey_mesh_draw(cube, shader);
@@ -164,43 +162,13 @@ int main() {
return 1;
}
- /* create triangle */
- /*float vertices[] = {
- /* positions colors tex coords /
- -0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 0.0,
- 0.5, -0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0,
- -0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0,
- 0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0,
- -0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0,
- 0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0,
- -0.5, 0.5, -0.5, 0.0, 0.0, 1.0, 0.0, 1.0,
- 0.5, 0.5, -0.5, 1.0, 1.0, 1.0, 1.0, 1.0 };
-
- unsigned int indices[] = { 0, 1, 2,
- 1, 2, 3,
- 4, 5, 6,
- 5, 6, 7,
- 0, 2, 4,
- 2, 4, 6,
- 1, 3, 5,
- 3, 5, 7,
- 2, 3, 6,
- 3, 6, 7,
- 0, 1, 4,
- 1, 4, 5 };
-
- unsigned int attribute_sizes[] = { 3, 3, 2 }; /* position, color, texture coordinate /
- enum honey_mesh_result result = honey_mesh_new(&cube,
- vertices, 8, 3, attribute_sizes,
- indices,
- sizeof(indices)/sizeof(unsigned int)); */
- if (honey_mesh_new_cube(&cube, 1, 1, 1) != MESH_OK) {
+ if (honey_mesh_new_textured_cube(&cube, 1, 1, 1) != MESH_OK) {
fprintf(stderr, "Failed to load cube\n");
return 1;
}
- /*honey_shader_set_int(shader, "boxTexture", 0);
- honey_shader_set_int(shader, "happyTexture", 1);*/
+ honey_shader_set_int(shader, "box_texture", 0);
+ honey_shader_set_int(shader, "happy_texture", 1);
glm_mat4_identity(model);
glm_rotate_x(model, glm_rad(-55), model);
diff --git a/demo.fs b/demo.fs
index e43d761..84ed0fd 100644
--- a/demo.fs
+++ b/demo.fs
@@ -1,10 +1,13 @@
#version 330 core
-in vec4 color;
+in vec2 texture_coordinate;
+
+uniform sampler2D box_texture;
+uniform sampler2D happy_texture;
out vec4 FragColor;
void main()
{
- FragColor = color;
+ FragColor = mix(texture(box_texture, texture_coordinate), texture(happy_texture, vec2(1,1)-texture_coordinate), 0.2);
}
diff --git a/demo.vs b/demo.vs
index 2c62f90..33a6956 100644
--- a/demo.vs
+++ b/demo.vs
@@ -1,7 +1,8 @@
#version 330 core
layout (location = 0) in vec3 position;
+layout (location = 1) in vec2 texCoord;
-out vec4 color;
+out vec2 texture_coordinate;
uniform mat4 model;
uniform mat4 view;
@@ -10,5 +11,5 @@ uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(position.xyz, 1.0);
- color = vec4(0, 1, 1, 1);
-} \ No newline at end of file
+ texture_coordinate = texCoord;
+} \ No newline at end of file
diff --git a/include/primitives.h b/include/primitives.h
index bfa944d..a7e4ccb 100644
--- a/include/primitives.h
+++ b/include/primitives.h
@@ -11,6 +11,8 @@
/** @brief Create a cube.
*
+ * This function creates a cube with vertex positions in attribute 0.
+ *
* @param[out] mesh Pointer to the destination mesh
* @param[in] width Desired width of the cube (x-axis)
* @param[in] height Desired height of the cube (y-axis)
@@ -22,5 +24,21 @@ enum honey_mesh_result honey_mesh_new_cube(honey_mesh* mesh,
float width,
float height,
float depth);
+/** @brief Create a textured cube.
+ *
+ * This function creates a cube with vertex positions in attribute 0,
+ * and texture coordinates in attribute 1.
+ *
+ * @param[out] mesh Pointer to the destination mesh
+ * @param[in] width Desired width of the cube (x-axis)
+ * @param[in] height Desired height of the cube (y-axis)
+ * @param[in] depth Desired depth of the cube (z-axis)
+ *
+ * @return Success or failure code
+ */
+enum honey_mesh_result honey_mesh_new_textured_cube(honey_mesh* mesh,
+ float width,
+ float height,
+ float depth);
#endif
diff --git a/include/texture.h b/include/texture.h
index e773e62..4caeebf 100644
--- a/include/texture.h
+++ b/include/texture.h
@@ -32,4 +32,11 @@ enum honey_texture_result honey_texture_new(honey_texture* texture,
char* texture_path,
bool alpha_channel);
+/** @brief Load a texture into a texture unit.
+ *
+ * @param[in] texture The texture to use
+ * @param[in] texture_unit The texture unit to put the texture in
+ */
+void honey_texture_use(honey_texture texture, int texture_unit);
+
#endif
diff --git a/src/primitives.c b/src/primitives.c
index 7cbea07..00cfdb9 100644
--- a/src/primitives.c
+++ b/src/primitives.c
@@ -45,4 +45,77 @@ enum honey_mesh_result honey_mesh_new_cube(honey_mesh* mesh,
return result;
}
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+enum honey_mesh_result honey_mesh_new_textured_cube(honey_mesh* mesh,
+ float width,
+ float height,
+ float depth) {
+ float x0 = 0;
+ float y0 = 0;
+ float z0 = 0;
+
+ float x1 = width;
+ float y1 = height;
+ float z1 = depth;
+
+ float vertices[] = {
+ /* position tex coord */
+ /* back face */
+ x0, y0, z0, 0, 0,
+ x1, y0, z0, 1, 0,
+ x0, y1, z0, 0, 1,
+ x1, y1, z0, 1, 1,
+
+ /* front face */
+ x0, y0, z1, 0, 0,
+ x1, y0, z1, 1, 0,
+ x0, y1, z1, 0, 1,
+ x1, y1, z1, 1, 1,
+
+ /* left face */
+ x0, y0, z0, 0, 0,
+ x0, y1, z0, 1, 0,
+ x0, y0, z1, 0, 1,
+ x0, y1, z1, 1, 1,
+
+ /* right face */
+ x1, y0, z0, 0, 0,
+ x1, y1, z0, 1, 0,
+ x1, y0, z1, 0, 1,
+ x1, y1, z1, 1, 1,
+
+ /* bottom face */
+ x0, y0, z0, 0, 0,
+ x1, y0, z0, 1, 0,
+ x0, y0, z1, 0, 1,
+ x1, y0, z1, 1, 1,
+
+ /* top face */
+ x0, y1, z0, 0, 0,
+ x1, y1, z0, 1, 0,
+ x0, y1, z1, 0, 1,
+ x1, y1, z1, 1, 1 };
+
+ unsigned int indices[] = {
+ 0, 1, 2,
+ 1, 2, 3,
+ 4, 5, 6,
+ 5, 6, 7,
+ 8, 9, 10,
+ 9, 10, 11,
+ 12, 13, 14,
+ 13, 14, 15,
+ 16, 17, 18,
+ 17, 18, 19,
+ 20, 21, 22,
+ 21, 22, 23 };
+
+ unsigned int attrib_sizes[] = { 3, 2 };
+
+ enum honey_mesh_result result;
+ result = honey_mesh_new(mesh, vertices, 24,
+ 2, attrib_sizes,
+ indices, 36);
+ return result;
+}
diff --git a/src/texture.c b/src/texture.c
index b041b4f..c1a5843 100644
--- a/src/texture.c
+++ b/src/texture.c
@@ -36,3 +36,10 @@ enum honey_texture_result honey_texture_new(honey_texture* texture,
return TEXTURE_OK;
}
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
+void honey_texture_use(honey_texture texture, int texture_unit) {
+ glActiveTexture(GL_TEXTURE0 + texture_unit);
+ glBindTexture(GL_TEXTURE_2D, texture.texture_id);
+}