summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2020-05-20 21:29:09 -0500
committersanine-a <sanine.not@pm.me>2020-05-20 21:29:09 -0500
commitc6ec37cd355b313083af5be3435162224020fe5e (patch)
tree9a6f069812e62e97052fd6fff6bd23ac1472c756
parent040ba6826237eb124aaa4576fc302e2e07dd40c5 (diff)
add honey_texture
-rw-r--r--.gitignore1
-rw-r--r--CMakeLists.txt2
-rw-r--r--demo.c39
-rw-r--r--demo.fs2
-rwxr-xr-xhoney_engine_demobin447408 -> 0 bytes
-rw-r--r--include/honey.h1
-rw-r--r--include/texture.h32
-rw-r--r--src/texture.c38
8 files changed, 80 insertions, 35 deletions
diff --git a/.gitignore b/.gitignore
index 4339f88..1f9012f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,4 @@ Makefile
cmake_install.cmake
doc/html
*#
+honey_engine_demo
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9ae826f..8bf34bb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -9,7 +9,7 @@ add_executable(honey_engine_demo demo.c)
set(CMAKE_C_FLAGS "-g")
-add_library(honey src/honey_setup.c src/shader.c src/mesh.c)
+add_library(honey src/honey_setup.c src/texture.c src/shader.c src/mesh.c)
add_library(glad src/glad.c)
add_library(stb_image src/stb_image.c)
diff --git a/demo.c b/demo.c
index 26e54e0..08c11fb 100644
--- a/demo.c
+++ b/demo.c
@@ -105,43 +105,16 @@ int main() {
honey_set_mouse_move_callback(window, mouseCallback);
/* load box texture */
- unsigned int boxTex;
- glGenTextures(1, &boxTex);
- glBindTexture(GL_TEXTURE_2D, boxTex);
- 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);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-
- int imgWidth, imgHeight, imgChannels;
- unsigned char* imageData = stbi_load("container.jpg", &imgWidth, &imgHeight, &imgChannels, 0);
- if (imageData == NULL) {
- fprintf(stderr, "ERROR: failed to load 'container.jpg'\n");
+ honey_texture box;
+ if (honey_texture_new(&box, "container.jpg", false) != TEXTURE_OK) {
return 1;
}
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imgWidth, imgHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
- glGenerateMipmap(GL_TEXTURE_2D);
- stbi_image_free(imageData);
-
/* load happy face texture */
- unsigned int happyTex;
- glGenTextures(1, &happyTex);
- glBindTexture(GL_TEXTURE_2D, happyTex);
- 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);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-
- imageData = stbi_load("happy.png", &imgWidth, &imgHeight, &imgChannels, 0);
- if (imageData == NULL) {
- fprintf(stderr, "ERROR: failed to load 'happy.png'\n");
+ honey_texture happy_face;
+ if (honey_texture_new(&happy_face, "happy.png", true) != TEXTURE_OK) {
return 1;
}
-
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imgWidth, imgHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
- glGenerateMipmap(GL_TEXTURE_2D);
- stbi_image_free(imageData);
honey_shader shader;
if (honey_shader_load(&shader, "demo.vs", "demo.fs") != SHADER_OK) {
@@ -237,9 +210,9 @@ int main() {
honey_shader_set_matrix_4fv(shader, "model", (float*) model);
glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, boxTex);
+ glBindTexture(GL_TEXTURE_2D, box.texture_id);
glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, happyTex);
+ glBindTexture(GL_TEXTURE_2D, happy_face.texture_id);
honey_mesh_draw(cube, shader);
diff --git a/demo.fs b/demo.fs
index ebdf3f8..ddfc235 100644
--- a/demo.fs
+++ b/demo.fs
@@ -10,5 +10,5 @@ uniform sampler2D happyTexture;
void main()
{
- FragColor = vec4(outColor.xyz, 1.0); //mix(texture(boxTexture, outTexCoord), texture(happyTexture, outTexCoord), 0.2);
+ FragColor = mix(texture(boxTexture, outTexCoord), texture(happyTexture, outTexCoord), 0.1);
}
diff --git a/honey_engine_demo b/honey_engine_demo
deleted file mode 100755
index 6e32448..0000000
--- a/honey_engine_demo
+++ /dev/null
Binary files differ
diff --git a/include/honey.h b/include/honey.h
index 251881e..9b048c0 100644
--- a/include/honey.h
+++ b/include/honey.h
@@ -4,6 +4,7 @@
#include "common.h"
#include "mesh.h"
#include "shader.h"
+#include "texture.h"
typedef GLFWwindow* honey_window;
diff --git a/include/texture.h b/include/texture.h
new file mode 100644
index 0000000..ee367ba
--- /dev/null
+++ b/include/texture.h
@@ -0,0 +1,32 @@
+#ifndef HONEY_TEXTURE_H
+#define HONEY_TEXTURE_H
+
+/** @file Defines the honey_texture struct and associated functions. */
+
+#include "common.h"
+
+enum honey_texture_result {
+ TEXTURE_OK,
+ TEXTURE_FAILED,
+ N_TEXTURE_RESULTS };
+
+typedef struct {
+ unsigned int texture_id;
+ int width;
+ int height;
+ int channels;
+} honey_texture;
+
+/** @brief Load a texture from disk.
+ *
+ * @param[out] texture Pointer to the destination texture
+ * @param[in] texture_path Path to the location of the texture
+ * @param[in] alpha_channel Set to true if the target image contains an alpha channel
+ *
+ * @return Success or failure type
+ */
+enum honey_texture_result honey_texture_new(honey_texture* texture,
+ char* texture_path,
+ bool alpha_channel);
+
+#endif
diff --git a/src/texture.c b/src/texture.c
new file mode 100644
index 0000000..b041b4f
--- /dev/null
+++ b/src/texture.c
@@ -0,0 +1,38 @@
+#include "include/texture.h"
+
+enum honey_texture_result honey_texture_new(honey_texture* texture,
+ char* texture_path,
+ bool alpha_channel) {
+ unsigned int texture_id;
+ glGenTextures(1, &texture_id);
+ glBindTexture(GL_TEXTURE_2D, texture_id);
+
+ 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);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+ int width, height, channels;
+ unsigned char* image_data = stbi_load(texture_path, &width, &height, &channels, 0);
+ if (image_data == NULL) {
+ fprintf(stderr, "ERROR: failed to load '%s'\n", texture_path);
+ return TEXTURE_FAILED;
+ }
+
+ if (alpha_channel) {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
+ }
+ else {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image_data);
+ }
+
+ glGenerateMipmap(GL_TEXTURE_2D);
+ stbi_image_free(image_data);
+
+ (*texture).texture_id = texture_id;
+ (*texture).width = width;
+ (*texture).height = height;
+ (*texture).channels = channels;
+
+ return TEXTURE_OK;
+}