Skip to content

Commit b820146

Browse files
committed
Work
1 parent 35c0c6f commit b820146

28 files changed

+746
-53
lines changed

.clang-format

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ BasedOnStyle: Microsoft
1818
BraceWrapping:
1919
AfterCaseLabel: true
2020
AfterClass: true
21-
AfterControlStatement: true
21+
AfterControlStatement: Always
2222
AfterEnum: true
2323
AfterFunction: true
2424
AfterNamespace: true
@@ -68,7 +68,7 @@ SpaceBeforeParens: ControlStatements
6868
SpaceBeforeRangeBasedForLoopColon: false
6969
SpaceInEmptyParentheses: false
7070
SpacesBeforeTrailingComments: 0
71-
SpacesInAngles: false
71+
SpacesInAngles: Never
7272
SpacesInContainerLiterals: false
7373
SpacesInCStyleCastParentheses: false
7474
SpacesInParentheses: false

.github/workflows/clang-format-check.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ jobs:
77
steps:
88
- uses: actions/checkout@v3
99
- name: Run clang-format style check for C/C++/Protobuf programs.
10-
uses: jidicula/clang-format-action@v4.10.1
10+
uses: jidicula/clang-format-action@v4.11.0
1111
with:
12-
clang-format-version: '15'
12+
clang-format-version: '16'
104 KB
Loading

AimGL/resources/Shaders/Graphics/InfiniteGridFloor.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
out vec4 FragColor;
55

66
float near = 0.01f;
7-
float far = 20.f;
7+
uniform float far = 5.f;
88

99
in vec3 nearPoint;
1010
in vec3 farPoint;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#version 330 core
2+
3+
out vec4 FragColor;
4+
5+
in vec3 FragmentNormal;
6+
in vec3 FragPos;
7+
in vec2 TexCoords;
8+
9+
uniform vec3 lightPos = vec3(3, 3, 3);
10+
uniform vec3 cameraPosition;
11+
uniform vec3 lightColor = vec3(1,1,1);
12+
13+
struct Material {
14+
bool isDiffusePresent;
15+
bool isSpecularPresent;
16+
sampler2D diffuse;
17+
sampler2D specular; // not used yet
18+
};
19+
20+
uniform Material material;
21+
22+
void main()
23+
{
24+
// ambient
25+
float ambientStrength = 0.1;
26+
vec3 ambientColor = vec3(1.0, 1.0, 1.0);
27+
vec3 ambient = ambientStrength * ambientColor;
28+
29+
// diffuse
30+
vec3 norm = normalize(FragmentNormal);
31+
vec3 lightDir = normalize(lightPos - FragPos);
32+
float diff = max(dot(norm, lightDir), 0.0);
33+
vec3 diffuseLight = diff * lightColor;
34+
35+
// specular
36+
float specularStrength = 0.5;
37+
vec3 viewDir = normalize(cameraPosition - FragPos);
38+
vec3 reflectDir = reflect(-lightDir, norm);
39+
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 16);
40+
vec3 specular = specularStrength * spec * lightColor;
41+
42+
// Combine texture color with lighting
43+
vec3 textureColor = vec3(1,1,1);
44+
if(material.isDiffusePresent)
45+
{
46+
textureColor = vec3(texture(material.diffuse, TexCoords));
47+
}
48+
vec3 result = textureColor * (ambient + diffuseLight) + specular;
49+
FragColor = vec4(result, 1.0);
50+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#version 330 core
2+
3+
layout(location = 0) in vec3 position; // Vertex position
4+
layout(location = 1) in vec3 normal; // Norm
5+
layout(location = 2) in vec2 texCoords; // Texture coordinates
6+
7+
out vec3 FragmentNormal;
8+
out vec3 FragPos;
9+
out vec2 TexCoords;
10+
11+
uniform mat4 model = mat4(1.0);
12+
uniform mat4 view;
13+
uniform mat4 projection;
14+
15+
void main()
16+
{
17+
TexCoords = texCoords;
18+
mat4 mvp = projection * view * model;
19+
FragPos = vec3(model * vec4(normal, 1.0));
20+
FragmentNormal = mat3(transpose(inverse(model))) * normal;
21+
22+
gl_Position = mvp * vec4(position, 1.0);
23+
}

AimGL/resources/Shaders/basic.fs

Lines changed: 0 additions & 12 deletions
This file was deleted.

AimGL/resources/Shaders/basic.vs

Lines changed: 0 additions & 14 deletions
This file was deleted.

AimGL/src/CMakeLists_Sources.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ set(PROJECT_SOURCES
1111
Renderer/Core/OpenglUtils.cpp
1212
Renderer/Graphics/Texture.cpp
1313
Renderer/Graphics/2D/Sprite2D.cpp
14-
Renderer/Graphics/3D/Sprite3D.cpp
1514
Renderer/Graphics/2D/Rectangle2D.cpp
15+
Renderer/Graphics/3D/Sprite3D.cpp
16+
Renderer/Graphics/3D/Core/Mesh.cpp
17+
Renderer/Graphics/3D/Model.cpp
1618
Renderer/Renderer.cpp
19+
Resources/ObjLoader.cpp
1720
States/State.cpp
1821
States/StateStack.cpp
1922
States/CustomStates/LogoState.cpp

AimGL/src/Game.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Game::Game()
7272
mAppStack.saveState<GameState>(State_ID::GameState, *mGameWindow);
7373

7474
// Initial state of the statestack is TitleState
75-
mAppStack.push(State_ID::LogoState);
75+
mAppStack.push(State_ID::GameState);
7676
}
7777

7878
void Game::run()

AimGL/src/Renderer/Core/Buffers/IndexBuffer.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ class IndexBuffer : public Buffer
3636
*/
3737
void setBuffer(const unsigned int* data, unsigned count);
3838

39+
40+
/**
41+
* \brief Sets the data from vector and binds the buffer
42+
* \tparam T Type of vector
43+
* \param vector Vector having linearly arranged data of type T in memory
44+
*/
45+
template<typename T>
46+
void setBuffer(const std::vector<T>& vector);
47+
3948
/**
4049
* Returns the number of elements in the index buffer
4150
* @return Size of the index buffer
@@ -45,3 +54,9 @@ class IndexBuffer : public Buffer
4554
private:
4655
unsigned int mCount;
4756
};
57+
58+
template<typename T>
59+
void IndexBuffer::setBuffer(const std::vector<T>& vector)
60+
{
61+
setBuffer(vector.data(), vector.size());
62+
}

AimGL/src/Renderer/Core/Buffers/VertexBuffer.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,5 @@ VertexBuffer::VertexBuffer(const std::vector<T>& vector)
5959
template<typename T>
6060
void VertexBuffer::setBuffer(const std::vector<T>& vector)
6161
{
62-
bind();
63-
GLCall(glBindBuffer(GL_ARRAY_BUFFER, mBufferId));
64-
GLCall(glBufferData(GL_ARRAY_BUFFER, vector.size(), vector.data(), GL_STATIC_DRAW));
62+
setBuffer(vector.data(), vector.size() * sizeof(vector[0]));
6563
}

AimGL/src/Renderer/Core/Shader.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ void Shader::setUniform(const std::string& name, glm::vec4 vec) const
3838
GLCall(glUniform4fv(getUniformLocation(name), 1, glm::value_ptr(vec)));
3939
}
4040

41+
void Shader::setUniform(const std::string& name, glm::vec3 vec) const
42+
{
43+
GLCall(glUniform3fv(getUniformLocation(name), 1, glm::value_ptr(vec)));
44+
}
45+
4146
void Shader::setUniform(const std::string& name, float f1, float f2, float f3, float f4) const
4247
{
4348
GLCall(glUniform4f(getUniformLocation(name), f1, f2, f3, f4));

AimGL/src/Renderer/Core/Shader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class Shader
6565

6666
void setUniform(const std::string& name, glm::mat4 mat) const;
6767
void setUniform(const std::string& name, glm::vec4 vec) const;
68+
void setUniform(const std::string& name, glm::vec3 vec) const;
6869
void setUniform(const std::string& name, float f1, float f2, float f3, float f4) const;
6970
void setUniform(const std::string& name, float f1, float f2, float f3) const;
7071
void setUniform(const std::string& name, float f1, float f2) const;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "Mesh.h"
2+
#include "pch.h"
3+
4+
Mesh::Mesh(std::vector<Vertex>&& vertices, std::vector<unsigned>&& indices,
5+
std::vector<Texture>&& textures)
6+
: vertices(std::move(vertices))
7+
, indices(std::move(indices))
8+
, textures(std::move(textures))
9+
{
10+
mVBO.setBuffer(this->vertices);
11+
mEBO.setBuffer(this->indices);
12+
13+
mVAO.bind();
14+
mVBO.bind();
15+
mBufferLayout.push<float>(3);
16+
mBufferLayout.push<float>(3);
17+
mBufferLayout.push<float>(2);
18+
mVAO.setBuffer(mVBO, mBufferLayout);
19+
mVAO.unbind();
20+
}
21+
22+
void Mesh::setTextureToShaderUniform(const Shader& shader, const Texture& texture) const
23+
{
24+
switch (texture.type())
25+
{
26+
case Texture::Type::Diffuse:
27+
shader.setUniform("material.diffuse", 0);
28+
shader.setUniform("material.isDiffusePresent", true);
29+
break;
30+
case Texture::Type::Specular:
31+
shader.setUniform("material.specular", 1);
32+
shader.setUniform("material.isSpecularPresent", true);
33+
break;
34+
default:
35+
spdlog::warn(
36+
"Trying to draw texture of unsupported type inside Mesh. TextureType used: {}",
37+
toString(texture.type()));
38+
}
39+
}
40+
41+
void Mesh::setDefaultValuesToMaterialUniforms(const Shader& shader) const
42+
{
43+
shader.setUniform("material.isDiffusePresent", false);
44+
shader.setUniform("material.isSpecularPresent", false);
45+
}
46+
47+
void Mesh::draw(const Renderer& target, const Camera& camera, const Shader& shader) const
48+
{
49+
shader.bind();
50+
setDefaultValuesToMaterialUniforms(shader);
51+
for (auto i = 0; i < textures.size(); ++i)
52+
{
53+
auto& texture = textures[i];
54+
setTextureToShaderUniform(shader, texture);
55+
texture.bind(i);
56+
}
57+
target.draw3D(mVAO, mEBO, shader, camera);
58+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#pragma once
2+
3+
#include "Renderer/Graphics/3D/Core/Vertex.h"
4+
#include <Renderer/Renderer.h>
5+
#include <Renderer/Core/Shader.h>
6+
#include <Renderer/Core/VertexArray.h>
7+
#include <Renderer/Core/Buffers/BufferLayout.h>
8+
#include <Renderer/Core/Buffers/IndexBuffer.h>
9+
#include <Renderer/Graphics/Texture.h>
10+
11+
/**
12+
* \brief Basic representation of shape of 3d object
13+
*/
14+
class Mesh
15+
{
16+
public:
17+
/**
18+
* \brief Constructor of Mesh class
19+
* \param vertices The vertices of which the shape consists
20+
* \param indices Indexes describing the order in which the vertices are drawn
21+
* \param textures Textures with which the mesh should be drawn
22+
*/
23+
Mesh(std::vector<Vertex>&& vertices, std::vector<unsigned>&& indices,
24+
std::vector<Texture>&& textures);
25+
26+
/**
27+
* \brief Draws a mesh for a given target
28+
* \param target The target to which the mesh is drawn
29+
* \param camera A camera in 3D space that looks at this object
30+
* \param shader Shader that should be used to draw the mesh
31+
*/
32+
void draw(const Renderer& target, const Camera& camera, const Shader& shader) const;
33+
34+
private:
35+
/**
36+
* \brief Sets the specified texture to the shader
37+
* \param shader Shader to which the texture should be bind
38+
* \param texture Texture that should be bind to shader
39+
*/
40+
void setTextureToShaderUniform(const Shader& shader, const Texture& texture) const;
41+
42+
/**
43+
* \brief Sets default values to object material in shader
44+
* \param shader Shader to which default values should be set
45+
*/
46+
void setDefaultValuesToMaterialUniforms(const Shader& shader) const;
47+
48+
private:
49+
std::vector<Vertex> vertices;
50+
std::vector<unsigned int> indices;
51+
std::vector<Texture> textures;// TODO: resource manager should be used there
52+
53+
VertexArray mVAO;
54+
VertexBuffer mVBO;
55+
IndexBuffer mEBO;
56+
BufferLayout mBufferLayout;
57+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
/**
4+
* \brief Structure representing one vertex in 3d space
5+
*/
6+
struct Vertex
7+
{
8+
glm::vec3 position;
9+
glm::vec3 normal;
10+
glm::vec2 textureCoordinates;
11+
};

0 commit comments

Comments
 (0)