Skip to content

Commit

Permalink
Implement rudimentary halftone effect (only OpenGL for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
justindriggers committed Aug 23, 2023
1 parent 90655d3 commit ee20228
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
2 changes: 1 addition & 1 deletion linguine/src/scenes/TutorialScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class TutorialScene : public Scene {
physicalState->currentPosition = transform->position;

auto fixture = cameraEntity->add<CameraFixture>();
fixture->height = 20.0f;
fixture->height = 10.0f;
fixture->camera = renderer.createCamera();
fixture->camera->clearColor = glm::vec3(0.04f, 0.05f, 0.37f);
}
Expand Down
2 changes: 1 addition & 1 deletion linguine/src/systems/CameraFollowSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void CameraFollowSystem::fixedUpdate(float fixedDeltaTime) {

findEntities<CameraFixture, PhysicalState>()->each([playerState](const Entity& cameraEntity) {
auto cameraState = cameraEntity.get<PhysicalState>();
cameraState->currentPosition += ((playerState->currentPosition - cameraState->currentPosition) - glm::vec2(0.0f, 3.0f)) * 0.1f;
cameraState->currentPosition += ((playerState->currentPosition - cameraState->currentPosition) - glm::vec2(0.0f, 1.0f)) * 0.1f;
});
});
}
Expand Down
65 changes: 63 additions & 2 deletions renderers/opengl/src/postprocessing/GammaCorrection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,76 @@ GammaCorrection::GammaCorrection(GLuint sourceTexture)
precision highp float;
const float dotSize = 8.0;
in vec2 uv;
uniform sampler2D source;
out vec4 outColor;
vec2 grid(vec2 px, float size) {
return px - mod(px, size);
}
vec2 rot(vec2 p, float deg) {
float c = cos(deg);
float s = sin(deg);
mat2 m = mat2(c, s, s, -c);
return m*p;
}
vec3 halftone(ivec2 resolution, vec2 coord, float rotation,
vec2 offset, vec3 components, vec3 color) {
vec2 gridEdge = grid(rot(coord, rotation), dotSize);
vec2 center = rot(gridEdge + 0.5 * dotSize, rotation);
vec4 value = texture(source, center / vec2(resolution));
value = vec4(pow(value.rgb, vec3(1.0 / 2.2)), value.a);
vec3 layer = value.rgb * components;
float distance = length(coord - center + offset);
float brightness = max(layer.r, max(layer.g, layer.b));
if (distance < dotSize * brightness * 0.5) {
color += layer;
}
return color;
}
void main() {
vec4 value = texture(source, uv);
outColor = vec4(pow(value.rgb, vec3(1.0 / 2.2)), value.a);
ivec2 resolution = textureSize(source, 0);
vec2 coord = uv * vec2(resolution);
float offset = 1.0;
vec3 color = vec3(0.0);
color = halftone(resolution, coord, 15.0, vec2(-offset, offset), vec3(0.0, 1.0, 1.0), color);
color = halftone(resolution, coord, 75.0, vec2(offset, offset), vec3(1.0, 0.0, 1.0), color);
color = halftone(resolution, coord, 0.0, vec2(-offset, -offset), vec3(1.0, 1.0, 0.0), color);
bool foreground = length(color) > 0.0;
float rotation = 45.0;
vec2 gridEdge = grid(rot(coord, rotation), dotSize);
vec2 center = rot(gridEdge + 0.5 * dotSize, rotation);
vec4 value = texture(source, center / vec2(resolution));
value = vec4(pow(value.rgb, vec3(1.0 / 2.2)), value.a);
float kDistance = length(center - coord);
float brightness = max(value.r, max(value.g, value.b));
if (kDistance < dotSize * (1.0 - brightness) * 0.5) {
color = value.rgb;
foreground = true;
}
if (!foreground)
color = vec3(1.0);
outColor = vec4(color, value.a);
}
)";

Expand Down

0 comments on commit ee20228

Please sign in to comment.