Added Texture

This commit is contained in:
Grayson Riffe (Laptop) 2021-08-27 17:41:45 -05:00
parent 4f1ea8327a
commit 933d3f1fe9
13 changed files with 84 additions and 18 deletions

View File

@ -216,6 +216,7 @@
<ClCompile Include="src\Renderer\IndexBuffer.cpp" /> <ClCompile Include="src\Renderer\IndexBuffer.cpp" />
<ClCompile Include="src\Renderer\Renderer.cpp" /> <ClCompile Include="src\Renderer\Renderer.cpp" />
<ClCompile Include="src\Renderer\Shader.cpp" /> <ClCompile Include="src\Renderer\Shader.cpp" />
<ClCompile Include="src\Renderer\Texture.cpp" />
<ClCompile Include="src\Renderer\VertexArray.cpp" /> <ClCompile Include="src\Renderer\VertexArray.cpp" />
<ClCompile Include="src\Renderer\VertexBuffer.cpp" /> <ClCompile Include="src\Renderer\VertexBuffer.cpp" />
<ClCompile Include="src\Utility.cpp" /> <ClCompile Include="src\Utility.cpp" />
@ -234,6 +235,7 @@
<ClInclude Include="src\include\NothinFancy.h" /> <ClInclude Include="src\include\NothinFancy.h" />
<ClInclude Include="src\include\Renderer.h" /> <ClInclude Include="src\include\Renderer.h" />
<ClInclude Include="src\include\Shader.h" /> <ClInclude Include="src\include\Shader.h" />
<ClInclude Include="src\include\Texture.h" />
<ClInclude Include="src\include\Utility.h" /> <ClInclude Include="src\include\Utility.h" />
<ClInclude Include="src\include\VertexArray.h" /> <ClInclude Include="src\include\VertexArray.h" />
<ClInclude Include="src\include\VertexBuffer.h" /> <ClInclude Include="src\include\VertexBuffer.h" />

View File

@ -51,6 +51,9 @@
<ClCompile Include="src\Renderer\Drawable\Entity.cpp"> <ClCompile Include="src\Renderer\Drawable\Entity.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\Renderer\Texture.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\include\Config.h"> <ClInclude Include="src\include\Config.h">
@ -101,6 +104,9 @@
<ClInclude Include="src\include\Entity.h"> <ClInclude Include="src\include\Entity.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\include\Texture.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Natvis Include="NatvisFile.natvis" /> <Natvis Include="NatvisFile.natvis" />

View File

@ -2,8 +2,10 @@
out vec4 color; out vec4 color;
uniform sampler2D tex;
in vec2 texCoord; in vec2 texCoord;
void main() { void main() {
color = vec4(1.0, 0.0, 1.0, 1.0); color = texture(tex, texCoord);
} }

View File

@ -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 = 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) { void Entity::setPosition(float x, float y, float z) {

View File

@ -6,11 +6,19 @@ namespace nf {
Model::Model() { 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 = new VertexArray;
m_vao->addBuffer(vertexBufferData, vertexBufferSize); m_vao->addBuffer(vertexBufferData, vertexBufferSize);
m_vao->push<float>(3); m_vao->push<float>(2);
//TODO: Change this to 3
m_vao->finishBufferLayout(); m_vao->finishBufferLayout();
if (textureCoordinatesBufferData && textureCoordinatesBufferSize && textureName) {
m_vao->addBuffer(textureCoordinatesBufferData, textureCoordinatesBufferSize);
m_vao->push<float>(2);
m_vao->finishBufferLayout();
m_texture = new Texture;
m_texture->create(textureName);
}
m_ib = new IndexBuffer(indexBufferData, indexBufferCount); m_ib = new IndexBuffer(indexBufferData, indexBufferCount);
} }
@ -18,10 +26,12 @@ namespace nf {
if (m_vao == nullptr) if (m_vao == nullptr)
Error("Tried to bind uninitialized model!"); Error("Tried to bind uninitialized model!");
m_vao->bind(); m_vao->bind();
if (m_texture)
m_texture->bind();
m_ib->bind(); m_ib->bind();
} }
Model::~Model() { Model::~Model() {
delete m_texture;
} }
} }

View File

@ -11,7 +11,7 @@ namespace nf {
} }
void IndexBuffer::bind() const { void IndexBuffer::bind() const {
glBindBuffer(GL_ARRAY_BUFFER, m_id); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_id);
} }
unsigned int IndexBuffer::getCount() { unsigned int IndexBuffer::getCount() {

View File

@ -44,10 +44,7 @@ namespace nf {
} }
void Shader::bind() { void Shader::bind() {
if (m_id != Shader::current) { glUseProgram(m_id);
glUseProgram(m_id);
Shader::current = m_id;
}
} }
void Shader::setUniform(const char* name, glm::mat4& data) { void Shader::setUniform(const char* name, glm::mat4& data) {
@ -66,6 +63,4 @@ namespace nf {
Shader::~Shader() { Shader::~Shader() {
glDeleteProgram(m_id); glDeleteProgram(m_id);
} }
unsigned int Shader::current;
} }

View File

@ -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);
}
}

View File

@ -11,7 +11,7 @@ namespace nf {
public: public:
Entity(); 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 //TODO: Do this using loaded assets somehow
void setPosition(float x, float y, float z); void setPosition(float x, float y, float z);
void setRotation(float x, float y, float z); void setRotation(float x, float y, float z);

View File

@ -5,17 +5,18 @@
#endif #endif
#include "Drawable.h" #include "Drawable.h"
#include "Texture.h"
namespace nf { namespace nf {
class Model : public Drawable { class Model : public Drawable {
public: public:
Model(); 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; void bind() override;
~Model(); ~Model();
private: private:
Texture* m_texture;
}; };
} }

View File

@ -22,7 +22,7 @@ namespace nf {
public: public:
Entity(); 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 //TODO: Do this using loaded assets somehow
void setPosition(float x, float y, float z); void setPosition(float x, float y, float z);
void setRotation(float x, float y, float z); void setRotation(float x, float y, float z);

View File

@ -12,7 +12,6 @@ namespace nf {
void bind(); void bind();
void setUniform(const char* name, glm::mat4& data); void setUniform(const char* name, glm::mat4& data);
static unsigned int current;
~Shader(); ~Shader();
private: private:

View File

@ -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;
};
}