-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
662 additions
and
49 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#version 330 core | ||
|
||
out vec4 FragColor; | ||
|
||
in vec3 FragmentNormal; | ||
in vec3 FragPos; | ||
in vec2 TexCoords; | ||
|
||
uniform vec3 lightPos = vec3(3, 3, 3); | ||
uniform vec3 cameraPosition; | ||
uniform vec3 lightColor = vec3(1,1,1); | ||
|
||
struct Material { | ||
bool isDiffusePresent; | ||
bool isSpecularPresent; | ||
sampler2D diffuse; | ||
sampler2D specular; // not used yet | ||
}; | ||
|
||
uniform Material material; | ||
|
||
void main() | ||
{ | ||
// ambient | ||
float ambientStrength = 0.1; | ||
vec3 ambientColor = vec3(1.0, 1.0, 1.0); | ||
vec3 ambient = ambientStrength * ambientColor; | ||
|
||
// diffuse | ||
vec3 norm = normalize(FragmentNormal); | ||
vec3 lightDir = normalize(lightPos - FragPos); | ||
float diff = max(dot(norm, lightDir), 0.0); | ||
vec3 diffuseLight = diff * lightColor; | ||
|
||
// specular | ||
float specularStrength = 0.5; | ||
vec3 viewDir = normalize(cameraPosition - FragPos); | ||
vec3 reflectDir = reflect(-lightDir, norm); | ||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 16); | ||
vec3 specular = specularStrength * spec * lightColor; | ||
|
||
// Combine texture color with lighting | ||
vec3 textureColor = vec3(1,1,1); | ||
if(material.isDiffusePresent) | ||
{ | ||
textureColor = vec3(texture(material.diffuse, TexCoords)); | ||
} | ||
vec3 result = textureColor * (ambient + diffuseLight) + specular; | ||
FragColor = vec4(result, 1.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#version 330 core | ||
|
||
layout(location = 0) in vec3 position; // Vertex position | ||
layout(location = 1) in vec3 normal; // Norm | ||
layout(location = 2) in vec2 texCoords; // Texture coordinates | ||
|
||
out vec3 FragmentNormal; | ||
out vec3 FragPos; | ||
out vec2 TexCoords; | ||
|
||
uniform mat4 model = mat4(1.0); | ||
uniform mat4 view; | ||
uniform mat4 projection; | ||
|
||
void main() | ||
{ | ||
TexCoords = texCoords; | ||
mat4 mvp = projection * view * model; | ||
FragPos = vec3(model * vec4(normal, 1.0)); | ||
FragmentNormal = mat3(transpose(inverse(model))) * normal; | ||
|
||
gl_Position = mvp * vec4(position, 1.0); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "Mesh.h" | ||
#include "pch.h" | ||
|
||
Mesh::Mesh(std::vector<Vertex>&& vertices, std::vector<unsigned>&& indices, | ||
std::vector<Texture>&& textures) | ||
: vertices(std::move(vertices)), | ||
indices(std::move(indices)), | ||
textures(std::move(textures)) | ||
{ | ||
mVBO.setBuffer(this->vertices); | ||
mEBO.setBuffer(this->indices); | ||
|
||
mVAO.bind(); | ||
mVBO.bind(); | ||
mBufferLayout.push<float>(3); | ||
mBufferLayout.push<float>(3); | ||
mBufferLayout.push<float>(2); | ||
mVAO.setBuffer(mVBO, mBufferLayout); | ||
mVAO.unbind(); | ||
} | ||
|
||
void Mesh::setTextureToShaderUniform(const Shader& shader, const Texture& texture) const | ||
{ | ||
switch (texture.type()) | ||
{ | ||
case Texture::Type::Diffuse: // | ||
shader.setUniform("material.diffuse", 0); | ||
shader.setUniform("material.isDiffusePresent", true); | ||
break; | ||
case Texture::Type::Specular: // | ||
shader.setUniform("material.specular", 1); | ||
shader.setUniform("material.isSpecularPresent", true); | ||
break; | ||
default: spdlog::warn( | ||
"Trying to draw texture of unsupported type inside Mesh. TextureType used: {}", | ||
toString(texture.type())); | ||
} | ||
} | ||
|
||
void Mesh::setDefaultValuesToMaterialUniforms(const Shader& shader) const | ||
{ | ||
shader.setUniform("material.isDiffusePresent", false); | ||
shader.setUniform("material.isSpecularPresent", false); | ||
} | ||
|
||
void Mesh::draw(const Renderer& target, const Camera& camera, const Shader& shader) const | ||
{ | ||
shader.bind(); | ||
setDefaultValuesToMaterialUniforms(shader); | ||
for (auto i = 0; i < textures.size(); ++i) | ||
{ | ||
auto& texture = textures[i]; | ||
setTextureToShaderUniform(shader, texture); | ||
texture.bind(i); | ||
} | ||
target.draw3D(mVAO, mEBO, shader, camera); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#pragma once | ||
|
||
#include "Renderer/Graphics/3D/Core/Vertex.h" | ||
|
||
#include <Renderer/Renderer.h> | ||
#include <Renderer/Core/Shader.h> | ||
#include <Renderer/Core/VertexArray.h> | ||
#include <Renderer/Core/Buffers/BufferLayout.h> | ||
#include <Renderer/Core/Buffers/IndexBuffer.h> | ||
#include <Renderer/Graphics/Texture.h> | ||
|
||
/** | ||
* \brief Basic representation of shape of 3d object | ||
*/ | ||
class Mesh | ||
{ | ||
public: | ||
/** | ||
* \brief Constructor of Mesh class | ||
* \param vertices The vertices of which the shape consists | ||
* \param indices Indexes describing the order in which the vertices are drawn | ||
* \param textures Textures with which the mesh should be drawn | ||
*/ | ||
Mesh(std::vector<Vertex>&& vertices, std::vector<unsigned>&& indices, | ||
std::vector<Texture>&& textures); | ||
|
||
/** | ||
* \brief Draws a mesh for a given target | ||
* \param target The target to which the mesh is drawn | ||
* \param camera A camera in 3D space that looks at this object | ||
* \param shader Shader that should be used to draw the mesh | ||
*/ | ||
void draw(const Renderer& target, const Camera& camera, const Shader& shader) const; | ||
|
||
private: | ||
/** | ||
* \brief Sets the specified texture to the shader | ||
* \param shader Shader to which the texture should be bind | ||
* \param texture Texture that should be bind to shader | ||
*/ | ||
void setTextureToShaderUniform(const Shader& shader, const Texture& texture) const; | ||
|
||
/** | ||
* \brief Sets default values to object material in shader | ||
* \param shader Shader to which default values should be set | ||
*/ | ||
void setDefaultValuesToMaterialUniforms(const Shader& shader) const; | ||
|
||
private: | ||
std::vector<Vertex> vertices; | ||
std::vector<unsigned int> indices; | ||
std::vector<Texture> textures; // TODO: resource manager should be used there | ||
|
||
VertexArray mVAO; | ||
VertexBuffer mVBO; | ||
IndexBuffer mEBO; | ||
BufferLayout mBufferLayout; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
|
||
struct Vertex | ||
{ | ||
glm::vec3 position; | ||
glm::vec3 normal; | ||
glm::vec2 textureCoordinates; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "Model.h" | ||
#include "pch.h" | ||
|
||
Model::Model(const std::string& objFilePath, const std::vector<TextureType>& texturesToLoad) | ||
: mShader{{ShaderType::VertexShader, "resources/Shaders/Graphics/Model/ModelTextured.vs"}, | ||
{ShaderType::FragmentShader, "resources/Shaders/Graphics/Model/ModelTextured.fs"}} | ||
, mObjLoader(objFilePath, {ObjLoader::Flags::ForceCenterAtOrigin}) | ||
{ | ||
// TODO: Storing texture this way is not the best idea as the texture | ||
// TODO: may be duplicated as many times as this object is loaded! :<< | ||
std::vector<Texture> textures; | ||
textures.reserve(textures.size()); | ||
for (const auto& textureToLoad: texturesToLoad) | ||
{ | ||
const auto& [texturePath, textureType] = textureToLoad; | ||
textures.emplace_back(texturePath, textureType); | ||
} | ||
mMesh = std::make_unique<Mesh>(std::move(mObjLoader.vertices()), | ||
std::move(mObjLoader.indices()), std::move(textures)); | ||
setPosition({0, 0, 0}); | ||
} | ||
|
||
void Model::draw(const Renderer& target, const Camera& camera) const | ||
{ | ||
mMesh->draw(target, camera, mShader); | ||
} | ||
|
||
void Model::setPosition(const glm::vec3& newPosition, Origin origin) | ||
{ | ||
mPosition = newPosition; | ||
switch (origin) | ||
{ | ||
case Origin::Center: break; | ||
case Origin::LeftBottom: mPosition += mObjLoader.dimensions() / 2.f; | ||
break; | ||
case Origin::CenterBottom: mPosition.y += mObjLoader.dimensions().y / 2.f; | ||
break; | ||
} | ||
updateModel(); | ||
} | ||
|
||
void Model::updateModel() | ||
{ | ||
glm::mat4 model = glm::mat4(1.0f); | ||
model = glm::translate(model, glm::vec3(mPosition)); | ||
// TODO: Add rotation and scale to the model | ||
//model = glm::rotate(model, glm::radians(mRotation.first), mRotation.second); | ||
//model = glm::scale(model, glm::vec3(mDimensionsNormalized * mScale / 2.f, 1.0f)); | ||
mShader.bind(); | ||
mShader.setUniform("model", model); | ||
mShader.unbind(); | ||
} |
Oops, something went wrong.