Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
Notiooo committed Nov 29, 2023
1 parent 35c0c6f commit f91d3b0
Show file tree
Hide file tree
Showing 28 changed files with 662 additions and 49 deletions.
Binary file added AimGL/resources/Models/tree/tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AimGL/resources/Models/tree/tree_bark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AimGL/resources/Models/tree/tree_combined.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion AimGL/resources/Shaders/Graphics/InfiniteGridFloor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
out vec4 FragColor;

float near = 0.01f;
float far = 20.f;
uniform float far = 5.f;

in vec3 nearPoint;
in vec3 farPoint;
Expand Down
50 changes: 50 additions & 0 deletions AimGL/resources/Shaders/Graphics/Model/ModelTextured.fs
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);
}
23 changes: 23 additions & 0 deletions AimGL/resources/Shaders/Graphics/Model/ModelTextured.vs
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);
}
12 changes: 0 additions & 12 deletions AimGL/resources/Shaders/basic.fs

This file was deleted.

14 changes: 0 additions & 14 deletions AimGL/resources/Shaders/basic.vs

This file was deleted.

5 changes: 4 additions & 1 deletion AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ set(PROJECT_SOURCES
Renderer/Core/OpenglUtils.cpp
Renderer/Graphics/Texture.cpp
Renderer/Graphics/2D/Sprite2D.cpp
Renderer/Graphics/3D/Sprite3D.cpp
Renderer/Graphics/2D/Rectangle2D.cpp
Renderer/Graphics/3D/Sprite3D.cpp
Renderer/Graphics/3D/Core/Mesh.cpp
Renderer/Graphics/3D/Model.cpp
Renderer/Renderer.cpp
Resources/ObjLoader.cpp
States/State.cpp
States/StateStack.cpp
States/CustomStates/LogoState.cpp
Expand Down
2 changes: 1 addition & 1 deletion AimGL/src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Game::Game()
mAppStack.saveState<GameState>(State_ID::GameState, *mGameWindow);

// Initial state of the statestack is TitleState
mAppStack.push(State_ID::LogoState);
mAppStack.push(State_ID::GameState);
}

void Game::run()
Expand Down
15 changes: 15 additions & 0 deletions AimGL/src/Renderer/Core/Buffers/IndexBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class IndexBuffer : public Buffer
*/
void setBuffer(const unsigned int* data, unsigned count);


/**
* \brief Sets the data from vector and binds the buffer
* \tparam T Type of vector
* \param vector Vector having linearly arranged data of type T in memory
*/
template<typename T>
void setBuffer(const std::vector<T>& vector);

/**
* Returns the number of elements in the index buffer
* @return Size of the index buffer
Expand All @@ -45,3 +54,9 @@ class IndexBuffer : public Buffer
private:
unsigned int mCount;
};

template<typename T>
void IndexBuffer::setBuffer(const std::vector<T>& vector)
{
setBuffer(vector.data(), vector.size());
}
4 changes: 1 addition & 3 deletions AimGL/src/Renderer/Core/Buffers/VertexBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,5 @@ VertexBuffer::VertexBuffer(const std::vector<T>& vector)
template<typename T>
void VertexBuffer::setBuffer(const std::vector<T>& vector)
{
bind();
GLCall(glBindBuffer(GL_ARRAY_BUFFER, mBufferId));
GLCall(glBufferData(GL_ARRAY_BUFFER, vector.size(), vector.data(), GL_STATIC_DRAW));
setBuffer(vector.data(), vector.size() * sizeof(vector[0]));
}
5 changes: 5 additions & 0 deletions AimGL/src/Renderer/Core/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ void Shader::setUniform(const std::string& name, glm::vec4 vec) const
GLCall(glUniform4fv(getUniformLocation(name), 1, glm::value_ptr(vec)));
}

void Shader::setUniform(const std::string& name, glm::vec3 vec) const
{
GLCall(glUniform3fv(getUniformLocation(name), 1, glm::value_ptr(vec)));
}

void Shader::setUniform(const std::string& name, float f1, float f2, float f3, float f4) const
{
GLCall(glUniform4f(getUniformLocation(name), f1, f2, f3, f4));
Expand Down
1 change: 1 addition & 0 deletions AimGL/src/Renderer/Core/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Shader

void setUniform(const std::string& name, glm::mat4 mat) const;
void setUniform(const std::string& name, glm::vec4 vec) const;
void setUniform(const std::string& name, glm::vec3 vec) const;
void setUniform(const std::string& name, float f1, float f2, float f3, float f4) const;
void setUniform(const std::string& name, float f1, float f2, float f3) const;
void setUniform(const std::string& name, float f1, float f2) const;
Expand Down
57 changes: 57 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Core/Mesh.cpp
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);
}
58 changes: 58 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Core/Mesh.h
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;
};
8 changes: 8 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Core/Vertex.h
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;
};
52 changes: 52 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Model.cpp
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();
}
Loading

0 comments on commit f91d3b0

Please sign in to comment.