Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
Notiooo committed Nov 19, 2023
1 parent 35c0c6f commit 70602e7
Show file tree
Hide file tree
Showing 22 changed files with 356 additions and 4 deletions.
22 changes: 22 additions & 0 deletions AimGL/resources/Models/tree/tree.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Blender 3.4.1 MTL File: 'None'
# www.blender.org

newmtl Bark
Ns 256.000031
Ka 1.000000 1.000000 1.000000
Kd 0.207595 0.138513 0.055181
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 1

newmtl Tree
Ns 256.000031
Ka 1.000000 1.000000 1.000000
Kd 0.256861 0.440506 0.110769
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 1
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
34 changes: 34 additions & 0 deletions AimGL/resources/Shaders/basic3d.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#version 330 core

out vec4 FragColor;

in vec3 FragmentNormal;
in vec3 FragPos;

uniform vec3 lightPos = vec3(3, 3, 3);
uniform vec3 cameraPosition;
uniform vec3 lightColor = vec3(1,1,1);
uniform vec3 objectColor = vec3(0.3,0.3,0.3);

void main()
{
// ambient
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;

// diffuse
vec3 norm = normalize(FragmentNormal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = 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), 32);
vec3 specular = specularStrength * spec * lightColor;

vec3 result = (ambient + diffuse + specular) * objectColor;
FragColor = vec4(result, 1.0);
}
21 changes: 21 additions & 0 deletions AimGL/resources/Shaders/basic3d.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#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;

uniform mat4 model = mat4(1.0);
uniform mat4 view;
uniform mat4 projection;

void main()
{
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);
}
13 changes: 13 additions & 0 deletions AimGL/resources/Shaders/veryBasic3d.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#version 330 core

out vec4 FragColor;

uniform vec3 lightPos = vec3(3, 3, 3);
uniform vec3 cameraPosition;
uniform vec3 lightColor = vec3(1,1,1);
uniform vec3 objectColor = vec3(0.3,0.3,0.3);

void main()
{
FragColor = vec4(0.5,0.5,0.5, 1.0);
}
15 changes: 15 additions & 0 deletions AimGL/resources/Shaders/veryBasic3d.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#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

uniform mat4 model = mat4(1.0);
uniform mat4 view;
uniform mat4 projection;

void main()
{
mat4 mvp = projection * view * model;
gl_Position = mvp * vec4(position, 1.0);
}
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
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
2 changes: 2 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Core/Mesh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "Mesh.h"
#include "pch.h"
47 changes: 47 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Core/Mesh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#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>

class Mesh
{
public:
Mesh(const std::vector<Vertex>& vertices, const std::vector<unsigned>& indices,
const std::vector<Texture>& textures)
: vertices(vertices),
indices(indices),
textures(textures)
{
mVBO.setBuffer(vertices.data(), vertices.size());
mEBO.setBuffer(indices.data(), indices.size());

mVAO.bind();
mVBO.bind();
mBufferLayout.push<float>(3);
mBufferLayout.push<float>(3);
mBufferLayout.push<float>(2);
mVAO.setBuffer(mVBO, mBufferLayout);
mVAO.unbind();
}

void draw(const Renderer& target, const Camera& camera, const Shader& shader) const
{
target.draw3D(mVAO, mEBO, shader, camera);
}

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;
};
2 changes: 2 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Model.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "Model.h"
#include "pch.h"
27 changes: 27 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include "Core/Mesh.h"

#include <Renderer/Renderer.h>
#include <Resources/ObjLoader.h>

class Model
{
public:
explicit Model(const std::string objFilePath)
: mShader{{ShaderType::VertexShader, "resources/Shaders/veryBasic3d.vs"},
{ShaderType::FragmentShader, "resources/Shaders/veryBasic3d.fs"}}
, mObjLoader(objFilePath)
, mMesh(mObjLoader.vertices(), mObjLoader.indices(), std::vector<Texture>{})
{
}

void draw(const Renderer& target, const Camera& camera) const
{
mMesh.draw(target, camera, mShader);
}

private:
Shader mShader;
ObjLoader mObjLoader;
Mesh mMesh;
};
1 change: 1 addition & 0 deletions AimGL/src/Renderer/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void Renderer::draw3D(const VertexArray& va, const IndexBuffer& ib, const Shader
const auto projection = camera.projection();
shader.setUniform("view", view);
shader.setUniform("projection", projection);
shader.setUniform("cameraPosition", camera.cameraPosition());
GLCall(glDrawElements(toOpenGl(drawMode), ib.size(), GL_UNSIGNED_INT, nullptr));

#ifdef _DEBUG
Expand Down
2 changes: 2 additions & 0 deletions AimGL/src/Resources/ObjLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "ObjLoader.h"
#include "pch.h"
126 changes: 126 additions & 0 deletions AimGL/src/Resources/ObjLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#pragma once
#include <fstream>
#include <Renderer/Graphics/3D/Core/Vertex.h>


class ObjLoader
{
public:
ObjLoader(const std::string& objFilePath)
{
loadObject(objFilePath);
}

[[nodiscard]] const std::vector<Vertex>& vertices() const
{
return mVertices;
}
[[nodiscard]] const std::vector<unsigned int>& indices() const
{
return mIndices;
}

private:
std::vector<Vertex> mVertices;
std::vector<glm::vec3> mPositions;
std::vector<glm::vec3> mNormals;
std::vector<glm::vec2> mTextureCoordinates;
std::vector<unsigned int> mIndices;

void loadObject(const std::string& objFilePath)
{
std::ifstream file(objFilePath);
std::string line;
while (std::getline(file, line))
{
parseLine(line);
}

/*
assert(mPositions.size() == mNormals.size() == mTextureCoordinates.size());
for (auto i = 0; i < mPositions.size(); ++i)
{
mVertices.push_back(Vertex(mPositions[i], mNormals[i], mTextureCoordinates[i]));
}
mPositions.clear();
mNormals.clear();
mTextureCoordinates.clear();
*/
}

void parseVertex(std::istringstream& ss)
{
glm::vec3 position;
ss >> position.x >> position.y >> position.z;
mPositions.push_back(position);
}

void parseNormal(std::istringstream& ss)
{
glm::vec3 normal;
ss >> normal.x >> normal.y >> normal.z;
mNormals.push_back(normal);
}

void parseTextureCoordinate(std::istringstream& ss)
{
glm::vec2 textureCoordinate;
ss >> textureCoordinate.x >> textureCoordinate.y;
mTextureCoordinates.push_back(textureCoordinate);
}

std::tuple<int, int, int> parseIndices(const std::string& vertex)
{
std::istringstream vertexStream(vertex);
std::string index;
std::vector<int> indices;

while (std::getline(vertexStream, index, '/'))
{
if (!index.empty())
{
indices.push_back(std::stoi(index) - 1);
}
}

return {indices[0], indices[1], indices[2]};
}

void parseFace(std::istringstream& ss)
{
std::string vertex;
while (ss >> vertex)
{
auto [positionIndex, textureIndex, normalIndex] = parseIndices(vertex);
Vertex v{mPositions[positionIndex],
mNormals[normalIndex], mTextureCoordinates[textureIndex]};
mVertices.push_back(v);
mIndices.push_back(mIndices.size());
}
}

void parseLine(const std::string& line)
{
std::istringstream ss(line);
std::string prefix;
ss >> prefix;

if (prefix == "v")
{
parseVertex(ss);
}
else if (prefix == "vn")
{
parseNormal(ss);
}
else if (prefix == "vt")
{
parseTextureCoordinate(ss);
}
else if (prefix == "f")
{
parseFace(ss);
}
}
};
Loading

0 comments on commit 70602e7

Please sign in to comment.