diff --git a/headers/shapes.h b/headers/shapes.h index b1d414f..9a110e6 100644 --- a/headers/shapes.h +++ b/headers/shapes.h @@ -18,6 +18,14 @@ unsigned int sqr_indices[] = { 1, 2, 3 /*br triangle*/ }; +float sqr_tex_vertices[] = { + /*positions*colors*texture coords*/ + 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.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, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f /*top left*/ +}; + const char *basicVertexShaderSource = "#version 330 core\n" "layout (location = 0) in vec3 aPos;\n" @@ -55,11 +63,12 @@ const char *rgbFragmentShaderSource = "#version 330 core\n" " FragColor = vec4(color, 1.0);\n" "}\0"; -const char *rgbPulseFragmentShaderSource = "#version 330 core\n" - "out vec4 FragColor;\n" - "in vec3 color;\n" - "uniform float u_time;" - "void main()\n" - "{\n" - " FragColor = vec4(color,0.0)*(sin(u_time)/2.0+0.5);\n" - "}\0"; +const char *rgbPulseFragmentShaderSource = + "#version 330 core\n" + "out vec4 FragColor;\n" + "in vec3 color;\n" + "uniform float u_time;" + "void main()\n" + "{\n" + " FragColor = vec4(color,0.0)*(sin(u_time)/2.0+0.5);\n" + "}\0"; diff --git a/shaders/TexFrag.glsl b/shaders/TexFrag.glsl new file mode 100644 index 0000000..206c686 --- /dev/null +++ b/shaders/TexFrag.glsl @@ -0,0 +1,12 @@ +#version 330 core +out vec4 FragColor; + +in vec3 ourColor; +in vec2 TexCoord; + +uniform sampler2D tex0; + +void main() +{ + FragColor = texture(Tex0, TexCoord) * vec4(ourColor, 1.0); +} diff --git a/shaders/TexVert.glsl b/shaders/TexVert.glsl new file mode 100644 index 0000000..22041cd --- /dev/null +++ b/shaders/TexVert.glsl @@ -0,0 +1,14 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aColor; +layout (location = 2) in vec2 aTexCoord; + +out vec3 ourColor; +out vec2 TexCoord; + +void main() +{ + gl_Position = vec4(aPos, 1.0); + ourColor = aColor; + TexCoord = aTexCoord; +} diff --git a/src/main.cpp b/src/main.cpp index ca0914b..ff7d785 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,15 +2,15 @@ #include #include #include -#include #include +#include +#include #define UNUSED(x) (void)(x) void processInput(GLFWwindow *window); void framebuffer_size_callback(GLFWwindow *window, int width, int height); - int main(void) { int tmp = 0; @@ -29,27 +29,55 @@ int main(void) { glViewport(0, 0, 800, 600); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); - unsigned int VBO,VAO,EBO; + unsigned int VBO, VAO, EBO; glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); glGenBuffers(1, &VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(tri_rgb_vertices), tri_rgb_vertices, + glBufferData(GL_ARRAY_BUFFER, sizeof(sqr_tex_vertices), sqr_tex_vertices, GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0); + 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); + glGenBuffers(1, &EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sqr_indices), sqr_indices, GL_STATIC_DRAW); - */ + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sqr_indices), sqr_indices, + GL_STATIC_DRAW); - Shader* shaderProgram = new Shader("./shaders/rgbPulseVert.glsl","./shaders/rgbPulseFrag.glsl"); + Shader *shaderProgram = + new Shader("./shaders/TexVert.glsl", "./shaders/TexFrag.glsl"); + + 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); + + stbi_set_flip_vertically_on_load(true); + int width, height, nrChannels; + unsigned char *data = + stbi_load("./texture/container.jpg", &width, &height, &nrChannels, 0); + if (data) { + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, + GL_UNSIGNED_BYTE, data); + glGenerateMipmap(GL_TEXTURE_2D); + } else { + std::cout << "Failed to load texture" << std::endl; + } + stbi_image_free(data); while (!glfwWindowShouldClose(window)) { processInput(window); @@ -58,17 +86,18 @@ int main(void) { glClear(GL_COLOR_BUFFER_BIT); shaderProgram->use(); - shaderProgram->setFloat("u_time",glfwGetTime()); + shaderProgram->setInt("Tex0",0); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture); glBindVertexArray(VAO); - //glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); - glDrawArrays(GL_TRIANGLES, 0, 3); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glfwSwapBuffers(window); glfwPollEvents(); } - delete(shaderProgram); + delete (shaderProgram); glfwTerminate(); return 0; } diff --git a/texture/brick.jpg b/texture/brick.jpg new file mode 100644 index 0000000..4963198 Binary files /dev/null and b/texture/brick.jpg differ diff --git a/texture/container.jpg b/texture/container.jpg new file mode 100644 index 0000000..d07bee4 Binary files /dev/null and b/texture/container.jpg differ