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 f9d4408
Show file tree
Hide file tree
Showing 28 changed files with 736 additions and 45 deletions.
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ BasedOnStyle: Microsoft
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: true
Expand Down Expand Up @@ -68,7 +68,7 @@ SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 0
SpacesInAngles: false
SpacesInAngles: Never
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/clang-format-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Run clang-format style check for C/C++/Protobuf programs.
uses: jidicula/clang-format-action@v4.10.1
uses: jidicula/clang-format-action@27cc05370cf2e7984be2446da7a2f927fa4f19b7
with:
clang-format-version: '15'
clang-format-version: '17'
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
58 changes: 58 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Core/Mesh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#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);
}
57 changes: 57 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Core/Mesh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

#include "Renderer/Graphics/3D/Core/Vertex.h"
#include <Renderer/Core/Buffers/BufferLayout.h>
#include <Renderer/Core/Buffers/IndexBuffer.h>
#include <Renderer/Core/Shader.h>
#include <Renderer/Core/VertexArray.h>
#include <Renderer/Graphics/Texture.h>
#include <Renderer/Renderer.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;
};
11 changes: 11 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Core/Vertex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

/**
* \brief Structure representing one vertex in 3d space
*/
struct Vertex
{
glm::vec3 position;
glm::vec3 normal;
glm::vec2 textureCoordinates;
};
Loading

0 comments on commit f9d4408

Please sign in to comment.