diff --git a/Makefile b/Makefile index 753818e..cf5638c 100644 --- a/Makefile +++ b/Makefile @@ -18,3 +18,6 @@ run: build clean: $(RM) $(P) + +.PHONY: all +all: build \ No newline at end of file diff --git a/awesomeface.png b/awesomeface.png new file mode 100644 index 0000000..9840caf Binary files /dev/null and b/awesomeface.png differ diff --git a/container.jpg b/container.jpg new file mode 100644 index 0000000..d07bee4 Binary files /dev/null and b/container.jpg differ diff --git a/main.c b/main.c index 410ed55..87952c3 100644 --- a/main.c +++ b/main.c @@ -6,6 +6,10 @@ #include #include "lib.h" +#define STB_IMAGE_IMPLEMENTATION +#define STBI_FAILURE_USERMSG +#include "stb_image.h" + int SCREEN_WIDTH = 1024; int SCREEN_HEIGHT = 768; @@ -46,62 +50,84 @@ int main(void) { GLuint shaderProgram = compileShaderProgram("shaders/main.vert", "shaders/main.frag", NULL); - // float vertices[] = { - // 0.5f, 0.5f, 0.0f, // top right - // 0.5f, -0.5f, 0.0f, // bottom right - // -0.5f, -0.5f, 0.0f, // bottom left - // -0.5f, 0.5f, 0.0f // top left - // }; - // unsigned int indices[] = { // note that we start from 0! - // 0, 1, 3, // first triangle - // 1, 2, 3 // second triangle - // }; + stbi_set_flip_vertically_on_load(true); + GLuint texture1, texture2; + glGenTextures(1, &texture1); + glBindTexture(GL_TEXTURE_2D, texture1); + 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 w1, h1, c1; + unsigned char* container = stbi_load("container.jpg", &w1, &h1, &c1, 0); + if (container == NULL) { + fprintf(stderr, "Failed to load image\n"); + exit(-1); + } + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w1, h1, 0, GL_RGB, GL_UNSIGNED_BYTE, container); + glGenerateMipmap(GL_TEXTURE_2D); - // float vertices[] = { - // -0.5f, -0.5f, 0.0f, - // 0.5f, -0.5f, 0.0f, - // 0.0f, 0.5f, 0.0f - // }; + glGenTextures(1, &texture2); + glBindTexture(GL_TEXTURE_2D, texture2); + 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 w2, h2, c2; + unsigned char* smiley = stbi_load("awesomeface.png", &w2, &h2, &c2, 0); + if (smiley == NULL) { + fprintf(stderr, "Failed to load image\n"); + exit(-1); + } + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w2, h2, 0, GL_RGBA, GL_UNSIGNED_BYTE, smiley); + glGenerateMipmap(GL_TEXTURE_2D); + + stbi_image_free(container); + stbi_image_free(smiley); float v1[] = { - -1.0f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, - 0.0f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, - -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, + // Position // Color // UV + 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, + -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, + -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f }; - float v2[] = { - 0.0f, -0.5f, 0.0f, - 1.0f, -0.5f, 0.0f, - 0.5f, 0.5f, 0.0f + unsigned int indices[] = { + 0, 1, 3, // first triangle + 1, 2, 3 // second triangle }; GLuint vaos[2]; GLuint vbos[2]; - unsigned int VBO1, VBO2; - // unsigned int EBO; + unsigned int EBO; glGenVertexArrays(2, vaos); - // glGenBuffers(1, &EBO); + glGenBuffers(1, &EBO); glGenBuffers(2, vbos); glBindVertexArray(vaos[0]); glBindBuffer(GL_ARRAY_BUFFER, vbos[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(v1), v1, GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); - glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float))); - glEnableVertexAttribArray(1); - glBindVertexArray(vaos[1]); - glBindBuffer(GL_ARRAY_BUFFER, vbos[1]); - glBufferData(GL_ARRAY_BUFFER, sizeof(v2), v2, GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + 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); - // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - // glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); + 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); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); + glUseProgram(shaderProgram); + int t1Loc = glGetUniformLocation(shaderProgram, "t1"); + glUniform1i(t1Loc, 0); + int t2Loc = glGetUniformLocation(shaderProgram, "t2"); + glUniform1i(t2Loc, 1); + while (!glfwWindowShouldClose(window)) { processInput(window); @@ -116,11 +142,16 @@ int main(void) { int offsetLoc = glGetUniformLocation(shaderProgram, "offset"); glUniform1f(offsetLoc, 0.5f); - glBindVertexArray(vaos[0]); - glDrawArrays(GL_TRIANGLES, 0, 3); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture1); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, texture2); - glBindVertexArray(vaos[1]); - glDrawArrays(GL_TRIANGLES, 0, 3); + glBindVertexArray(vaos[0]); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + + // glBindVertexArray(vaos[1]); + // glDrawArrays(GL_TRIANGLES, 0, 3); // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); @@ -130,8 +161,7 @@ int main(void) { } glDeleteVertexArrays(2, vaos); - glDeleteBuffers(1, &VBO1); - glDeleteBuffers(1, &VBO2); + glDeleteBuffers(2, vbos); glDeleteProgram(shaderProgram); glfwTerminate(); return 0; diff --git a/shaders/main.frag b/shaders/main.frag index c076e98..c629f6f 100644 --- a/shaders/main.frag +++ b/shaders/main.frag @@ -1,11 +1,13 @@ #version 330 core -in vec3 vertexPos; -in vec3 vertexColor; +in vec3 vPos; +in vec3 vCol; +in vec2 tCoord; out vec4 FragColor; -// uniform vec4 ourColor; +uniform sampler2D t1; +uniform sampler2D t2; void main() { - FragColor = vec4(vertexPos, 1); + FragColor = mix(texture(t1, tCoord), texture(t2, tCoord), 0.2); } diff --git a/shaders/main.vert b/shaders/main.vert index b23dbf2..8e7512e 100644 --- a/shaders/main.vert +++ b/shaders/main.vert @@ -1,12 +1,16 @@ #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; +layout (location = 2) in vec2 aTexCoord; -out vec3 vertexPos; -out vec3 vertexColor; +out vec3 vPos; +out vec3 vCol; +out vec2 tCoord; uniform float offset; void main() { gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); - vertexPos = vec3(abs(aPos.x), abs(aPos.y), abs(aPos.z)); + vPos = aPos; + vCol = aColor; + tCoord = aTexCoord; }