From 933d3f1fe95de7f069767fbac432eb84796b6793 Mon Sep 17 00:00:00 2001 From: "Grayson Riffe (Laptop)" Date: Fri, 27 Aug 2021 17:41:45 -0500 Subject: [PATCH] Added Texture --- NothinFancy/NothinFancy.vcxproj | 2 ++ NothinFancy/NothinFancy.vcxproj.filters | 6 ++++ NothinFancy/res/defaultShader/fragment.shader | 4 ++- NothinFancy/src/Renderer/Drawable/Entity.cpp | 4 +-- NothinFancy/src/Renderer/Drawable/Model.cpp | 16 +++++++-- NothinFancy/src/Renderer/IndexBuffer.cpp | 2 +- NothinFancy/src/Renderer/Shader.cpp | 7 +--- NothinFancy/src/Renderer/Texture.cpp | 36 +++++++++++++++++++ NothinFancy/src/include/Entity.h | 2 +- NothinFancy/src/include/Model.h | 5 +-- NothinFancy/src/include/NothinFancy.h | 2 +- NothinFancy/src/include/Shader.h | 1 - NothinFancy/src/include/Texture.h | 15 ++++++++ 13 files changed, 84 insertions(+), 18 deletions(-) create mode 100644 NothinFancy/src/Renderer/Texture.cpp create mode 100644 NothinFancy/src/include/Texture.h diff --git a/NothinFancy/NothinFancy.vcxproj b/NothinFancy/NothinFancy.vcxproj index 182acac..ff1b237 100644 --- a/NothinFancy/NothinFancy.vcxproj +++ b/NothinFancy/NothinFancy.vcxproj @@ -216,6 +216,7 @@ + @@ -234,6 +235,7 @@ + diff --git a/NothinFancy/NothinFancy.vcxproj.filters b/NothinFancy/NothinFancy.vcxproj.filters index e1365df..dcf212a 100644 --- a/NothinFancy/NothinFancy.vcxproj.filters +++ b/NothinFancy/NothinFancy.vcxproj.filters @@ -51,6 +51,9 @@ Source Files + + Source Files + @@ -101,6 +104,9 @@ Header Files + + Header Files + diff --git a/NothinFancy/res/defaultShader/fragment.shader b/NothinFancy/res/defaultShader/fragment.shader index b2875d5..adb17ff 100644 --- a/NothinFancy/res/defaultShader/fragment.shader +++ b/NothinFancy/res/defaultShader/fragment.shader @@ -2,8 +2,10 @@ out vec4 color; +uniform sampler2D tex; + in vec2 texCoord; void main() { - color = vec4(1.0, 0.0, 1.0, 1.0); + color = texture(tex, texCoord); } diff --git a/NothinFancy/src/Renderer/Drawable/Entity.cpp b/NothinFancy/src/Renderer/Drawable/Entity.cpp index 3df5cef..c3237a2 100644 --- a/NothinFancy/src/Renderer/Drawable/Entity.cpp +++ b/NothinFancy/src/Renderer/Drawable/Entity.cpp @@ -10,9 +10,9 @@ namespace nf { } - void Entity::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount) { + void Entity::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData, size_t textureCoordinatesBufferSize, const char* textureName) { m_model = new Model; - m_model->create(vertexBufferData, vertexBufferSize, indexBufferData, indexBufferCount); + m_model->create(vertexBufferData, vertexBufferSize, indexBufferData, indexBufferCount, textureCoordinatesBufferData, textureCoordinatesBufferSize, textureName); } void Entity::setPosition(float x, float y, float z) { diff --git a/NothinFancy/src/Renderer/Drawable/Model.cpp b/NothinFancy/src/Renderer/Drawable/Model.cpp index 9958fb4..065fdfe 100644 --- a/NothinFancy/src/Renderer/Drawable/Model.cpp +++ b/NothinFancy/src/Renderer/Drawable/Model.cpp @@ -6,11 +6,19 @@ namespace nf { Model::Model() { } - void Model::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount) { + void Model::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData, size_t textureCoordinatesBufferSize, const char* textureName) { m_vao = new VertexArray; m_vao->addBuffer(vertexBufferData, vertexBufferSize); - m_vao->push(3); + m_vao->push(2); + //TODO: Change this to 3 m_vao->finishBufferLayout(); + if (textureCoordinatesBufferData && textureCoordinatesBufferSize && textureName) { + m_vao->addBuffer(textureCoordinatesBufferData, textureCoordinatesBufferSize); + m_vao->push(2); + m_vao->finishBufferLayout(); + m_texture = new Texture; + m_texture->create(textureName); + } m_ib = new IndexBuffer(indexBufferData, indexBufferCount); } @@ -18,10 +26,12 @@ namespace nf { if (m_vao == nullptr) Error("Tried to bind uninitialized model!"); m_vao->bind(); + if (m_texture) + m_texture->bind(); m_ib->bind(); } Model::~Model() { - + delete m_texture; } } \ No newline at end of file diff --git a/NothinFancy/src/Renderer/IndexBuffer.cpp b/NothinFancy/src/Renderer/IndexBuffer.cpp index 589132e..9a277df 100644 --- a/NothinFancy/src/Renderer/IndexBuffer.cpp +++ b/NothinFancy/src/Renderer/IndexBuffer.cpp @@ -11,7 +11,7 @@ namespace nf { } void IndexBuffer::bind() const { - glBindBuffer(GL_ARRAY_BUFFER, m_id); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_id); } unsigned int IndexBuffer::getCount() { diff --git a/NothinFancy/src/Renderer/Shader.cpp b/NothinFancy/src/Renderer/Shader.cpp index 9c9f921..0068483 100644 --- a/NothinFancy/src/Renderer/Shader.cpp +++ b/NothinFancy/src/Renderer/Shader.cpp @@ -44,10 +44,7 @@ namespace nf { } void Shader::bind() { - if (m_id != Shader::current) { - glUseProgram(m_id); - Shader::current = m_id; - } + glUseProgram(m_id); } void Shader::setUniform(const char* name, glm::mat4& data) { @@ -66,6 +63,4 @@ namespace nf { Shader::~Shader() { glDeleteProgram(m_id); } - - unsigned int Shader::current; } \ No newline at end of file diff --git a/NothinFancy/src/Renderer/Texture.cpp b/NothinFancy/src/Renderer/Texture.cpp new file mode 100644 index 0000000..3c28c01 --- /dev/null +++ b/NothinFancy/src/Renderer/Texture.cpp @@ -0,0 +1,36 @@ +#include "Texture.h" + +#include "GL/glew.h" +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" + +#include "Utility.h" + +namespace nf { + Texture::Texture() { + glGenTextures(1, &m_id); + } + + void Texture::create(const char* textureName) { + int width, height, nChannels; + stbi_set_flip_vertically_on_load(true); + unsigned char* texture = stbi_load(textureName, &width, &height, &nChannels, 0); + //TODO: Load from memory + if (!texture) + Error("Texture failed to load from memory!"); + glBindTexture(GL_TEXTURE_2D, m_id); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture); + //glGenerateMipmap(GL_TEXTURE_2D); + stbi_image_free(texture); + } + + void Texture::bind() { + glBindTexture(GL_TEXTURE_2D, m_id); + } + + Texture::~Texture() { + glDeleteTextures(1, &m_id); + } +} \ No newline at end of file diff --git a/NothinFancy/src/include/Entity.h b/NothinFancy/src/include/Entity.h index 3bf2ddd..62508a7 100644 --- a/NothinFancy/src/include/Entity.h +++ b/NothinFancy/src/include/Entity.h @@ -11,7 +11,7 @@ namespace nf { public: Entity(); - void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount); + void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData = nullptr, size_t textureCoordinatesBufferSize = 0, const char* textureName = nullptr); //TODO: Do this using loaded assets somehow void setPosition(float x, float y, float z); void setRotation(float x, float y, float z); diff --git a/NothinFancy/src/include/Model.h b/NothinFancy/src/include/Model.h index 5295236..517a823 100644 --- a/NothinFancy/src/include/Model.h +++ b/NothinFancy/src/include/Model.h @@ -5,17 +5,18 @@ #endif #include "Drawable.h" +#include "Texture.h" namespace nf { class Model : public Drawable { public: Model(); - void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount); + void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinates = nullptr, size_t textureCoordinatesBufferSize = 0, const char* textureName = nullptr); void bind() override; ~Model(); private: - + Texture* m_texture; }; } \ No newline at end of file diff --git a/NothinFancy/src/include/NothinFancy.h b/NothinFancy/src/include/NothinFancy.h index 4bf6a8b..b791908 100644 --- a/NothinFancy/src/include/NothinFancy.h +++ b/NothinFancy/src/include/NothinFancy.h @@ -22,7 +22,7 @@ namespace nf { public: Entity(); - void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount); + void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData = nullptr, size_t textureCoordinatesBufferSize = 0, const char* textureName = nullptr); //TODO: Do this using loaded assets somehow void setPosition(float x, float y, float z); void setRotation(float x, float y, float z); diff --git a/NothinFancy/src/include/Shader.h b/NothinFancy/src/include/Shader.h index 5b93d7b..fd3522d 100644 --- a/NothinFancy/src/include/Shader.h +++ b/NothinFancy/src/include/Shader.h @@ -12,7 +12,6 @@ namespace nf { void bind(); void setUniform(const char* name, glm::mat4& data); - static unsigned int current; ~Shader(); private: diff --git a/NothinFancy/src/include/Texture.h b/NothinFancy/src/include/Texture.h new file mode 100644 index 0000000..c7d3af2 --- /dev/null +++ b/NothinFancy/src/include/Texture.h @@ -0,0 +1,15 @@ +#pragma once + +namespace nf { + class Texture { + public: + Texture(); + + void create(const char* textureName); + void bind(); + + ~Texture(); + private: + unsigned int m_id; + }; +} \ No newline at end of file