-
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
21 changed files
with
985 additions
and
11 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 = 10.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.2, 0.2, 0.2, 1.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
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
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,109 @@ | ||
#include "Sprite3D.h" | ||
#include "pch.h" | ||
#include "World/Camera.h" | ||
|
||
Sprite3D::Sprite3D(const Texture& texture) | ||
: mTexture(texture) | ||
, mShader{{ShaderType::VertexShader, "resources/Shaders/Graphics/Sprite3D.vs"}, | ||
{ShaderType::FragmentShader, "resources/Shaders/Graphics/Sprite3D.fs"}} | ||
, mPosition(0.0f) | ||
, mScale(1.0f) | ||
, mRotation(0.f) | ||
{ | ||
|
||
initializeBuffers(); | ||
//setScale(1); | ||
setPosition({0.f, 0.f, 0.f}); | ||
} | ||
|
||
void Sprite3D::initializeBuffers() | ||
{ | ||
float vertices[] = { | ||
// positions // texture coords | ||
1.f, 1.f, 0.0f, 1.0f, 1.0f,// top right | ||
1.f, -1.f, 0.0f, 1.0f, 0.0f,// bottom right | ||
-1.f, -1.f, 0.0f, 0.0f, 0.0f,// bottom left | ||
-1.f, 1.f, 0.0f, 0.0f, 1.0f // 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>(3); | ||
mBufferLayout.push<float>(2); | ||
mVAO.setBuffer(mVBO, mBufferLayout); | ||
mVAO.unbind(); | ||
} | ||
|
||
void Sprite3D::draw(const Renderer3D& target, const Camera& camera) const | ||
{ | ||
// Prepare transformations | ||
mShader.bind(); | ||
mTexture.bind(0); | ||
target.draw(mVAO, mEBO, mShader, camera); | ||
} | ||
|
||
void Sprite3D::updateModel() | ||
{ | ||
glm::mat4 model = glm::mat4(1.0f); | ||
model = glm::translate(model, glm::vec3(mPosition)); | ||
//model = glm::rotate(model, glm::radians(mRotation), glm::vec3(0.0f, 0.0f, 1.0f)); | ||
//model = glm::scale(model, glm::vec3(mScale, 1.0f)); | ||
mShader.bind(); | ||
mShader.setUniform("model", model); | ||
mShader.unbind(); | ||
} | ||
|
||
void Sprite3D::setPosition(const glm::vec3& newPosition, Origin origin) | ||
{ | ||
switch (origin) | ||
{ | ||
case Origin::Center: mPosition = newPosition; break; | ||
case Origin::LeftBottom: | ||
mPosition = newPosition; | ||
mPosition += glm::vec3{1, 1, 0}; | ||
//mPosition += glm::vec3{mTexture.width(), mTexture.height(), 0.f}; | ||
break; | ||
} | ||
updateModel(); | ||
} | ||
|
||
void Sprite3D::setScale(float newScale) | ||
{ | ||
mScale = {mTexture.width(), mTexture.height()}; | ||
mScale *= newScale; | ||
} | ||
|
||
void Sprite3D::setRotation(float angle) | ||
{ | ||
mRotation = angle; | ||
} | ||
|
||
void Sprite3D::setOpacity(float opacity) | ||
{ | ||
mOpacity = opacity; | ||
mShader.bind(); | ||
mShader.setUniform("opacity", mOpacity); | ||
mShader.unbind(); | ||
} | ||
|
||
void Sprite3D::setWidth(float width) | ||
{ | ||
mScale = {width, width / mTexture.aspectRatio()}; | ||
mScale /= 2.f; | ||
updateModel(); | ||
} | ||
|
||
void Sprite3D::setHeight(float height) | ||
{ | ||
mScale = {height * mTexture.aspectRatio(), height}; | ||
mScale /= 2.f; | ||
updateModel(); | ||
} |
Oops, something went wrong.