summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c42
1 files changed, 35 insertions, 7 deletions
diff --git a/src/main.c b/src/main.c
index 4dfe6b6..b46095d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,4 +1,5 @@
#include "shader.h"
+#include "texture.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
@@ -41,10 +42,10 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
static const float verts[] = {
- 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, // top right
- 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right
- -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom left
- -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f // top left
+ 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, // top right
+ 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
+ -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
+ -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
static const GLuint indices[] = {
0, 1, 3, // first triangle
@@ -62,11 +63,13 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices) / 2, indices, GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
+ glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
+ glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
+ glEnableVertexAttribArray(2);
Shader shader;
if (shader_init(&shader,
@@ -77,15 +80,37 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
return 1;
}
+ Texture texture;
+ if (texture_init(&texture, "res/wall.jpg")) {
+ fprintf(stderr, "texture_init failed\n");
+ return 1;
+ }
+
+ Texture texture1;
+ if (texture_init(&texture1, "res/awesomeface.jpg")) {
+ fprintf(stderr, "texture_init failed\n");
+ return 1;
+ }
+
const GLint vertex_color_location =
shader_uniform_location(&shader, "ourColor");
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, GL_LINEAR);
+
while (!glfwWindowShouldClose(window)) {
shader_use(&shader);
const int r = 0;//rand();
glUniform4f(vertex_color_location,
!(r&1), !(r&2), !(r&4), !(r&8));
+ glUniform1i(shader_uniform_location(&shader, "texture0"), 0);
+ glUniform1i(shader_uniform_location(&shader, "texture1"), 1);
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, texture.id);
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, texture1.id);
+
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
@@ -93,6 +118,9 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
glfwPollEvents();
}
+ texture_deinit(&texture1);
+ texture_deinit(&texture);
+ shader_deinit(&shader);
return 0;
}