Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
Notiooo committed Nov 10, 2023
1 parent ec47ce8 commit 3f1c26b
Show file tree
Hide file tree
Showing 27 changed files with 619 additions and 107 deletions.
27 changes: 27 additions & 0 deletions AimGL/CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": ""
},
{
"name": "x64-Release",
"generator": "Ninja",
"configurationType": "RelWithDebInfo",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "msvc_x64_x64" ],
"variables": []
}
]
}
15 changes: 15 additions & 0 deletions AimGL/resources/Shaders/Graphics/Sprite.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#version 330 core

in vec2 TexCoords;
out vec4 color;

uniform sampler2D spriteTexture;
uniform float opacity = 0.f;

void main()
{
color = texture(spriteTexture, TexCoords);
color.a = min(color.a, opacity);
if(color.a == 0.0)
discard;
}
17 changes: 17 additions & 0 deletions AimGL/resources/Shaders/Graphics/Sprite.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#version 330 core

layout(location = 0) in vec2 position; // Vertex position
layout(location = 1) in vec2 texCoords; // Texture coordinates

out vec2 TexCoords;

uniform mat4 model;
uniform mat4 projection = mat4(1.0); // For 2D it is identity
uniform mat4 windowProjection;

void main()
{
TexCoords = texCoords;
mat4 mvp = windowProjection * model;
gl_Position = mvp * vec4(position, 0.0, 1.0);
}
9 changes: 7 additions & 2 deletions AimGL/resources/Shaders/basic.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#version 330 core
out vec4 FragColor;

in vec3 ourColor;
in vec2 TexCoord;

uniform sampler2D ourTexture;

void main()
{
FragColor = vec4(0.5,0.5,0.1,1);
}
FragColor = texture(ourTexture, TexCoord);
}
9 changes: 8 additions & 1 deletion AimGL/resources/Shaders/basic.vs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;

out vec3 ourColor;
out vec2 TexCoord;

void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = aTexCoord;
}
Binary file added AimGL/resources/Textures/dead-inside.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 10 additions & 3 deletions AimGL/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ target_precompile_headers(AimGLSrc PUBLIC pch.h)
set(CUSTOM_INCLUDES_DIR ${CMAKE_CURRENT_BINARY_DIR}/custom_includes)
file(MAKE_DIRECTORY ${CUSTOM_INCLUDES_DIR})

configure_file(
${FETCHCONTENT_BASE_DIR}/minitrace-src/minitrace.h
set(CUSTOM_INCLUDES
${FETCHCONTENT_BASE_DIR}/minitrace-src/minitrace.h
${FETCHCONTENT_BASE_DIR}/stb-src/stb_image.h
)

foreach(HEADER ${CUSTOM_INCLUDES})
configure_file(
${HEADER}
${CUSTOM_INCLUDES_DIR}
COPYONLY
)
)
endforeach()

target_include_directories(AimGLSrc PUBLIC
${FETCHCONTENT_BASE_DIR}/glew-src/include
Expand Down
5 changes: 4 additions & 1 deletion AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ set(PROJECT_SOURCES
Renderer3D/VertexArray.cpp
Renderer3D/VertexBuffer.cpp
Renderer3D/OpenglUtils.cpp
Renderer3D/Texture.cpp
Renderer3D/Sprite2D.cpp
States/State.cpp
States/StateStack.cpp
States/CustomStates/SampleState.cpp
States/CustomStates/LogoState.cpp
States/CustomStates/ExitGameState.cpp
)
13 changes: 9 additions & 4 deletions AimGL/src/Game.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Game.h"
#include "States/CustomStates/SampleState.h"
#include "States/CustomStates/ExitGameState.h"
#include "States/CustomStates/LogoState.h"
#include "constants.h"
#include "pch.h"

Expand Down Expand Up @@ -59,11 +60,15 @@ Game::Game()
throw std::runtime_error("Failed to initialize GLEW");
}

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// Setup all application-flow states
mAppStack.saveState<SampleState>(State_ID::SampleState, *mGameWindow);
mAppStack.saveState<LogoState>(State_ID::LogoState, *mGameWindow);
mAppStack.saveState<ExitGameState>(State_ID::ExitGameState);

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

void Game::run()
Expand Down Expand Up @@ -215,7 +220,7 @@ void Game::update(const sf::Time& deltaTime)
void Game::render()
{
MTR_SCOPE("Game", "Game::render");
glClearColor(0.43f, 0.69f, 1.0f, 1.0f);
glClearColor(0.f, 0.f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// draw the application
Expand Down
11 changes: 11 additions & 0 deletions AimGL/src/Renderer3D/Renderer3D.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
#include "Renderer3D.h"
#include "pch.h"

Renderer3D::Renderer3D(sf::Window& window)
: mWindow(window)
{
}

void Renderer3D::draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader,
const DrawMode& drawMode) const
{
// mWindow.setActive(true);
shader.bind();
va.bind();
ib.bind();
float aspectRatio = static_cast<float>(mWindow.getSize().x) / mWindow.getSize().y;

glm::mat4 windowProjection = glm::ortho(0.f, static_cast<float>(mWindow.getSize().x), 0.f,
static_cast<float>(mWindow.getSize().y));
shader.setUniform("windowProjection", windowProjection);
GLCall(glDrawElements(toOpenGl(drawMode), ib.size(), GL_UNSIGNED_INT, nullptr));

#ifdef _DEBUG
Expand Down
3 changes: 3 additions & 0 deletions AimGL/src/Renderer3D/Renderer3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Renderer3D
Quads
};

Renderer3D(sf::Window& window);

/**
* Draws the data given in VertexArray, IndexBuffer to the screen using the interpretation given
* in Shader.
Expand All @@ -29,4 +31,5 @@ class Renderer3D

private:
unsigned toOpenGl(const DrawMode& drawMode) const;
sf::Window& mWindow;
};
23 changes: 14 additions & 9 deletions AimGL/src/Renderer3D/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,52 @@ void Shader::unbind() const
GLCall(glUseProgram(0));
}

void Shader::setUniform(const std::string& name, float f1, float f2, float f3, float f4)
void Shader::setUniform(const std::string& name, glm::mat4 mat) const
{
GLCall(glUniformMatrix4fv(getUniformLocation(name), 1, GL_FALSE, glm::value_ptr(mat[0])));
}

void Shader::setUniform(const std::string& name, float f1, float f2, float f3, float f4) const
{
GLCall(glUniform4f(getUniformLocation(name), f1, f2, f3, f4));
}

void Shader::setUniform(const std::string& name, float f1, float f2, float f3)
void Shader::setUniform(const std::string& name, float f1, float f2, float f3) const
{
GLCall(glUniform3f(getUniformLocation(name), f1, f2, f3));
}

void Shader::setUniform(const std::string& name, float f1, float f2)
void Shader::setUniform(const std::string& name, float f1, float f2) const
{
GLCall(glUniform2f(getUniformLocation(name), f1, f2));
}

void Shader::setUniform(const std::string& name, float f1)
void Shader::setUniform(const std::string& name, float f1) const
{
GLCall(glUniform1f(getUniformLocation(name), f1));
}

void Shader::setUniform(const std::string& name, int i1, int i2, int i3, int i4)
void Shader::setUniform(const std::string& name, int i1, int i2, int i3, int i4) const
{
GLCall(glUniform4i(getUniformLocation(name), i1, i2, i3, i4));
}

void Shader::setUniform(const std::string& name, int i1, int i2, int i3)
void Shader::setUniform(const std::string& name, int i1, int i2, int i3) const
{
GLCall(glUniform3i(getUniformLocation(name), i1, i2, i3));
}

void Shader::setUniform(const std::string& name, int i1, int i2)
void Shader::setUniform(const std::string& name, int i1, int i2) const
{
GLCall(glUniform2i(getUniformLocation(name), i1, i2));
}

void Shader::setUniform(const std::string& name, int i1)
void Shader::setUniform(const std::string& name, int i1) const
{
GLCall(glUniform1i(getUniformLocation(name), i1));
}

unsigned Shader::getUniformLocation(const std::string& name)
unsigned Shader::getUniformLocation(const std::string& name) const
{
if (mUniformLocationCache.find(name) != mUniformLocationCache.end())
{
Expand Down
21 changes: 11 additions & 10 deletions AimGL/src/Renderer3D/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ class Shader
*/
void unbind() const;

void setUniform(const std::string& name, float f1, float f2, float f3, float f4);
void setUniform(const std::string& name, float f1, float f2, float f3);
void setUniform(const std::string& name, float f1, float f2);
void setUniform(const std::string& name, float f1);
void setUniform(const std::string& name, int i1, int i2, int i3, int i4);
void setUniform(const std::string& name, int i1, int i2, int i3);
void setUniform(const std::string& name, int i1, int i2);
void setUniform(const std::string& name, int i1);
void setUniform(const std::string& name, glm::mat4 mat) 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;
void setUniform(const std::string& name, float f1) const;
void setUniform(const std::string& name, int i1, int i2, int i3, int i4) const;
void setUniform(const std::string& name, int i1, int i2, int i3) const;
void setUniform(const std::string& name, int i1, int i2) const;
void setUniform(const std::string& name, int i1) const;


private:
Expand Down Expand Up @@ -113,7 +114,7 @@ class Shader
* \param name Name of uniform
* \return Location, or -1 in case of failure
*/
unsigned getUniformLocation(const std::string& name);
unsigned getUniformLocation(const std::string& name) const;

/**
* \brief Creates a shader based on shader types and source code
Expand All @@ -138,7 +139,7 @@ class Shader

private:
unsigned int mRendererId;
std::unordered_map<std::string, int> mUniformLocationCache;
mutable std::unordered_map<std::string, int> mUniformLocationCache;
};

/**
Expand Down
Loading

0 comments on commit 3f1c26b

Please sign in to comment.