diff --git a/Engine/Engine.vcxproj b/Engine/Engine.vcxproj index 329be66..4c38a9f 100644 --- a/Engine/Engine.vcxproj +++ b/Engine/Engine.vcxproj @@ -166,17 +166,18 @@ - + + - + diff --git a/Engine/engine/texture.h b/Engine/engine/texture.h new file mode 100644 index 0000000..b57d609 --- /dev/null +++ b/Engine/engine/texture.h @@ -0,0 +1,47 @@ +#pragma once + +#include + +#include +#include + +class Texture +{ +public: + unsigned int ID; + int width, height, nrChannels; + + Texture(const char* texturePath, bool flip = false) + { + // Create the Texture and set our ID + glGenTextures(1, &ID); + glBindTexture(GL_TEXTURE_2D, ID); + + // set the texture wrapping/filtering options (on the currently bound texture object) + 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); + + // Grab the texture from our path + if (flip) + stbi_set_flip_vertically_on_load(true); + + unsigned char *data = stbi_load(texturePath, &width, &height, &nrChannels, 0); + if (data) + { + GLenum format = (nrChannels == 4) ? GL_RGBA : GL_RGB; + glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); + glGenerateMipmap(GL_TEXTURE_2D); + } + else + { + std::cout << "Failed to load texture" << std::endl; + } + + if (flip) + stbi_set_flip_vertically_on_load(false); + + stbi_image_free(data); + } +}; diff --git a/Engine/main.cpp b/Engine/main.cpp index 78accff..ed27647 100644 --- a/Engine/main.cpp +++ b/Engine/main.cpp @@ -5,6 +5,7 @@ #include #include +#include const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; @@ -71,7 +72,7 @@ int main(int argc, char* argv[]) glfwSetFramebufferSizeCallback(window, window_resize_callback); // Load Shader - Shader shaderTest("shaders/basicVertex.vs", "shaders/basicFragment.fs"); + Shader shaderTest("shaders/basicVertex.vert", "shaders/basicFragment.frag"); float vertices[] = { 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 @@ -112,56 +113,12 @@ int main(int argc, char* argv[]) glEnableVertexAttribArray(2); - unsigned int texture1, texture2; - glGenTextures(1, &texture1); - glBindTexture(GL_TEXTURE_2D, texture1); - // set the texture wrapping/filtering options (on the currently bound texture object) - 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); - // load and generate the texture - int width, height, nrChannels; - unsigned char *data = stbi_load("placeholders/wall.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); - - // texture 2 - // --------- - glGenTextures(1, &texture2); - glBindTexture(GL_TEXTURE_2D, texture2); - // set the texture wrapping parameters - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - // set texture filtering parameters - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - // load image, create texture and generate mipmaps - stbi_set_flip_vertically_on_load(true); - data = stbi_load("placeholders/awesomeface.png", &width, &height, &nrChannels, 0); - if (data) - { - // note that the awesomeface.png has transparency and thus an alpha channel, so make sure to tell OpenGL the data type is of GL_RGBA - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - glGenerateMipmap(GL_TEXTURE_2D); - } - else - { - std::cout << "Failed to load texture" << std::endl; - } - stbi_image_free(data); + Texture texture1("placeholders/wall.jpg"); + Texture texture2("placeholders/awesomeface.png", true); shaderTest.Use(); // don't forget to activate/use the shader before setting uniforms! - shaderTest.setInt("ourTexture", 0); - shaderTest.setInt("decal", 1); + shaderTest.setInt("ourTexture", texture1.ID); + shaderTest.setInt("decal", texture2.ID); // Render Loop while (!glfwWindowShouldClose(window)) @@ -176,9 +133,9 @@ int main(int argc, char* argv[]) shaderTest.Use(); glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture1); + glBindTexture(GL_TEXTURE_2D, texture1.ID); glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, texture2); + glBindTexture(GL_TEXTURE_2D, texture2.ID); // Draw our triangle glBindVertexArray(VAO); diff --git a/Engine/shaders/basicFragment.fs b/Engine/shaders/basicFragment.frag similarity index 100% rename from Engine/shaders/basicFragment.fs rename to Engine/shaders/basicFragment.frag diff --git a/Engine/shaders/basicVertex.vs b/Engine/shaders/basicVertex.vert similarity index 100% rename from Engine/shaders/basicVertex.vs rename to Engine/shaders/basicVertex.vert