From ce250f34671b25a7bc974c932a34a88e5ec330c0 Mon Sep 17 00:00:00 2001 From: Lukian Date: Wed, 29 Jan 2025 15:54:35 +0100 Subject: [PATCH] code improvement --- shaders/shader.fs | 3 +-- shaders/shader.vs | 5 +---- src/main.cpp | 29 +++++++++++++---------------- src/texture.cpp | 1 + 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/shaders/shader.fs b/shaders/shader.fs index a081fad..796e31b 100644 --- a/shaders/shader.fs +++ b/shaders/shader.fs @@ -1,7 +1,6 @@ #version 330 core out vec4 FragColor; - -in vec3 ourColor; + in vec2 TexCoord; uniform sampler2D ourTexture; diff --git a/shaders/shader.vs b/shaders/shader.vs index 311ec24..7c728aa 100644 --- a/shaders/shader.vs +++ b/shaders/shader.vs @@ -1,14 +1,11 @@ #version 330 core layout (location = 0) in vec3 aPos; -layout (location = 1) in vec3 aColor; -layout (location = 2) in vec2 aTexCoord; +layout (location = 1) in vec2 aTexCoord; -out vec3 ourColor; out vec2 TexCoord; void main() { gl_Position = vec4(aPos, 1.0); - ourColor = aColor; TexCoord = aTexCoord; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 210c38f..fcd0901 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,9 @@ #include #include #include +#include +#include +#include void framebuffer_size_callback(GLFWwindow* window, int width, int height); void processInput(GLFWwindow *window); @@ -35,11 +38,10 @@ int main() glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); float vertices[] = { - // positions // colors // texture coords - 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top right - 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // bottom right - -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, // bottom left - -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f // top left + 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, + -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, + -0.5f, 0.5f, 0.0f, 0.0f, 1.0f }; unsigned int indices[] = { @@ -47,31 +49,26 @@ int main() 2, 3, 0 }; + unsigned int VAO; + glGenVertexArrays(1, &VAO); + glBindVertexArray(VAO); + unsigned int VBO; glGenBuffers(1, &VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - unsigned int VAO; - glGenVertexArrays(1, &VAO); - glBindVertexArray(VAO); - unsigned int EBO; glGenBuffers(1, &EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3* sizeof(float))); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * 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 ourShader("shaders/shader.vs", "shaders/shader.fs"); Texture sataaAndagii("textures/saataa_andagii.png"); Texture ohMyGah("textures/oh_my_gah.png"); diff --git a/src/texture.cpp b/src/texture.cpp index b0ead54..8cbfa48 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -16,6 +16,7 @@ Texture::Texture(const char* texturePath) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // load and generate the texture int width, height, nrChannels; + stbi_set_flip_vertically_on_load(true); unsigned char *data = stbi_load(texturePath, &width, &height, &nrChannels, 0); if (data) {