-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,309 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
out/ | ||
.vshistory/ | ||
.vshistory/ | ||
CMakeSettings.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#version 330 core | ||
// THANK YOU https://asliceofrendering.com/scene%20helper/2020/01/05/InfiniteGrid/!!! | ||
|
||
out vec4 FragColor; | ||
|
||
float near = 0.01f; | ||
float far = 20.f; | ||
|
||
in vec3 nearPoint; | ||
in vec3 farPoint; | ||
in mat4 fragView; | ||
in mat4 fragProjection; | ||
|
||
vec4 grid(vec3 fragPos3D, float scale) { | ||
vec2 coord = fragPos3D.xz * scale; | ||
vec2 derivative = fwidth(coord); | ||
vec2 grid = abs(fract(coord - 0.5) - 0.5) / derivative; | ||
float line = min(grid.x, grid.y); | ||
float minimumz = min(derivative.y, 1); | ||
float minimumx = min(derivative.x, 1); | ||
vec4 color = vec4(0.3, 0.3, 0.3, 0.0 + min(line, 1.0)); | ||
return color; | ||
} | ||
|
||
float computeDepth(vec3 position) { | ||
vec4 clip_space_position = fragProjection * fragView * vec4 (position.xyz, 1.0); | ||
return 0.5 + 0.5 * (clip_space_position.z / clip_space_position.w); | ||
} | ||
|
||
float computeLinearDepth(vec3 pos) { | ||
vec4 clip_space_pos = fragProjection * fragView * vec4(pos.xyz, 1.0); | ||
float clip_space_depth = (clip_space_pos.z / clip_space_pos.w) * 2.0 - 1.0; // put back between -1 and 1 | ||
float linearDepth = (2.0 * near * far) / (far + near - clip_space_depth * (far - near)); // get linear value between 0.01 and 100 | ||
return linearDepth / far; // normalize | ||
} | ||
|
||
void main() | ||
{ | ||
float t = -nearPoint.y / (farPoint.y - nearPoint.y); | ||
vec3 fragPos3D = nearPoint + t * (farPoint - nearPoint); | ||
gl_FragDepth = computeDepth(fragPos3D); | ||
|
||
float linearDepth = computeLinearDepth(fragPos3D); | ||
float fading = max(0, (0.5 - linearDepth)); | ||
|
||
FragColor = (grid(fragPos3D, 10) + grid(fragPos3D, 1))* float(t > 0); // adding multiple resolution for the grid | ||
FragColor.a *= fading; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#version 330 | ||
// THANK YOU https://asliceofrendering.com/scene%20helper/2020/01/05/InfiniteGrid/!!! | ||
|
||
uniform mat4 view; | ||
uniform mat4 projection; | ||
|
||
out vec3 nearPoint; | ||
out vec3 farPoint; | ||
out mat4 fragView; | ||
out mat4 fragProjection; | ||
|
||
vec3 UnprojectPoint(float x, float y, float z, mat4 view, mat4 projection) { | ||
mat4 viewInv = inverse(view); | ||
mat4 projInv = inverse(projection); | ||
vec4 unprojectedPoint = viewInv * projInv * vec4(x, y, z, 1.0); | ||
return unprojectedPoint.xyz / unprojectedPoint.w; | ||
} | ||
|
||
|
||
vec3 gridPlane[6] = vec3[]( | ||
vec3(1, 1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0), | ||
vec3(-1, -1, 0), vec3(1, 1, 0), vec3(1, -1, 0) | ||
); | ||
|
||
void main() { | ||
vec3 p = gridPlane[gl_VertexID].xyz; | ||
nearPoint = UnprojectPoint(p.x, p.y, 0.0, view, projection).xyz; // unprojecting on the near plane | ||
farPoint = UnprojectPoint(p.x, p.y, 1.0, view, projection).xyz; // unprojecting on the far plane | ||
gl_Position = vec4(p, 1.0); // using directly the clipped coordinates | ||
fragView = view; | ||
fragProjection = projection; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#version 330 core | ||
|
||
out vec4 FragColor; | ||
|
||
uniform vec4 color = vec4(1,0,0, 1.f); | ||
|
||
void main() | ||
{ | ||
FragColor = color; | ||
if(FragColor.a == 0.0) | ||
discard; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#version 330 core | ||
|
||
layout(location = 0) in vec2 position; // Vertex position | ||
|
||
uniform mat4 model; | ||
uniform mat4 projection = mat4(1.0); // For 2D it is identity | ||
uniform mat4 windowOrthoProjection; | ||
|
||
void main() | ||
{ | ||
mat4 mvp = windowOrthoProjection * model; | ||
gl_Position = mvp * vec4(position, 0.0, 1.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = 1.f; | ||
|
||
void main() | ||
{ | ||
color = texture(spriteTexture, TexCoords); | ||
color.a = min(color.a, opacity); | ||
if(color.a == 0.0) | ||
discard; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#version 330 core | ||
|
||
layout(location = 0) in vec3 position; // Vertex position | ||
layout(location = 1) in vec2 texCoords; // Texture coordinates | ||
|
||
out vec2 TexCoords; | ||
|
||
uniform mat4 model; | ||
uniform mat4 view; | ||
uniform mat4 projection; | ||
|
||
void main() | ||
{ | ||
TexCoords = texCoords; | ||
mat4 mvp = projection * view * model; | ||
gl_Position = mvp * vec4(position, 1.0); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#include "Rectangle2D.h" | ||
#include "pch.h" | ||
|
||
Rectangle2D::Rectangle2D(const glm::vec2 size, const glm::vec4& color) | ||
: mShader{{ShaderType::VertexShader, "resources/Shaders/Graphics/Rectangle2D.vs"}, | ||
{ShaderType::FragmentShader, "resources/Shaders/Graphics/Rectangle2D.fs"}} | ||
, mColor(color) | ||
, mPosition(0.0f) | ||
, mRotation(0.f) | ||
, mDimensions(size) | ||
{ | ||
|
||
initializeBuffers(); | ||
setPosition({0.f, 0.f}, Origin::Center); | ||
|
||
updateColor(); | ||
} | ||
|
||
void Rectangle2D::initializeBuffers() | ||
{ | ||
float vertices[] = { | ||
// positions | ||
1.f, 1.f, // top right | ||
1.f, -1.f,// bottom right | ||
-1.f, -1.f,// bottom left | ||
-1.f, 1.f // top left | ||
}; | ||
|
||
unsigned int indices[] = { | ||
0, 1, 3,// first Triangle | ||
1, 2, 3 // second Triangle | ||
}; | ||
|
||
mVBO.setBuffer(vertices, sizeof(vertices)); | ||
mEBO.setBuffer(indices, sizeof(indices)); | ||
|
||
mVAO.bind(); | ||
mVBO.bind(); | ||
mBufferLayout.push<float>(2); | ||
mVAO.setBuffer(mVBO, mBufferLayout); | ||
mVAO.unbind(); | ||
} | ||
|
||
void Rectangle2D::draw(const Renderer3D& target) const | ||
{ | ||
target.draw(mVAO, mEBO, mShader); | ||
} | ||
|
||
void Rectangle2D::updateColor() | ||
{ | ||
mShader.bind(); | ||
mShader.setUniform("color", mColor); | ||
mShader.unbind(); | ||
} | ||
|
||
void Rectangle2D::updateModel() | ||
{ | ||
glm::mat4 model = glm::mat4(1.0f); | ||
model = glm::translate(model, glm::vec3(mPosition)); | ||
|
||
// Negative mRotation makes it rotate clockwise for positive angles | ||
model = glm::rotate(model, glm::radians(-mRotation), glm::vec3(0.0f, 0.0f, 1.0f)); | ||
|
||
// Dividing dimensions by two is needed as vertices are from -1 to 1 | ||
model = glm::scale(model, glm::vec3(mDimensions.x / 2.f, mDimensions.y / 2.f, 1.0f)); | ||
mShader.bind(); | ||
mShader.setUniform("model", model); | ||
mShader.unbind(); | ||
} | ||
|
||
void Rectangle2D::setPosition(const glm::vec2& newPosition, Origin origin) | ||
{ | ||
switch (origin) | ||
{ | ||
case Origin::Center: mPosition = glm::vec3(newPosition, 0.f); break; | ||
case Origin::LeftBottom: | ||
mPosition = glm::vec3(newPosition, 0.f); | ||
mPosition += glm::vec3{mDimensions.x / 2.f, mDimensions.y / 2.f, 0.f}; | ||
break; | ||
} | ||
updateModel(); | ||
} | ||
|
||
void Rectangle2D::setColor(glm::vec4 color) | ||
{ | ||
mColor = color; | ||
updateColor(); | ||
} | ||
|
||
void Rectangle2D::setRotation(float angle) | ||
{ | ||
mRotation = angle; | ||
updateModel(); | ||
} | ||
|
||
void Rectangle2D::setOpacity(float opacity) | ||
{ | ||
mColor.a = opacity; | ||
updateColor(); | ||
} | ||
|
||
void Rectangle2D::setWidth(float width) | ||
{ | ||
mDimensions.x = width; | ||
updateModel(); | ||
} | ||
|
||
void Rectangle2D::setHeight(float height) | ||
{ | ||
mDimensions.y = height; | ||
updateModel(); | ||
} | ||
|
||
void Rectangle2D::showDebugImGui(std::string name) | ||
{ | ||
name = "[Rectangle2D] " + name; | ||
ImGui::Begin(name.c_str()); | ||
ImGui::SliderFloat4("Color", &mColor[0], 0.0f, 1.0f); | ||
ImGui::SliderFloat2("Position", &mPosition[0], -1500.f, 1500.f); | ||
ImGui::SliderFloat("Rotation", &mRotation, 0.0f, 360.f); | ||
ImGui::End(); | ||
updateModel(); | ||
updateColor(); | ||
} | ||
|
||
float Rectangle2D::opacity() | ||
{ | ||
return mColor.a; | ||
} |
Oops, something went wrong.