Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions assets/shaders/circle_renderer.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#if __VERSION__ >= 330
in vec3 localPos;
out vec4 FragColor;
#else
varying vec3 localPos;
#endif

uniform vec4 color;

float circleAlpha(vec3 lPos)
{
const float RADIUS = 1.0;
const float MULTIPLIER = 2.1; // Quad is -0.5 to 0.5, scale to radius 1.0

vec3 samplePos = lPos * MULTIPLIER;

float dist = length(samplePos);
float edgeWidth = fwidth(dist);
float alpha = 1.0 - smoothstep(RADIUS - edgeWidth, RADIUS + edgeWidth, dist);

return alpha;
}

void main()
{
float alpha = circleAlpha(localPos);

#if __VERSION__ >= 330
FragColor = vec4(color.rgb, color.a * alpha);
#else
gl_FragColor = vec4(color.rgb, color.a * alpha);
#endif
}
27 changes: 27 additions & 0 deletions assets/shaders/circle_renderer.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if __VERSION__ >= 330
layout (location = 0) in vec3 aPos;
out vec3 localPos;
#else
attribute vec3 aPos;
varying vec3 localPos;
#endif

uniform mat4 model;
#if GAMECOE_HAS_UBO
layout(std140) uniform CameraMatrices
{
mat4 projection;
mat4 view;
vec3 cameraPosition;
};
#else
uniform mat4 view;
uniform mat4 projection;
uniform vec3 cameraPosition;
#endif

void main()
{
localPos = aPos;
gl_Position = projection * view * model * vec4(aPos, 1.0);
}
4 changes: 2 additions & 2 deletions assets/shaders/shape_renderer.frag
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
uniform vec4 color;

#if __VERSION__ >= 330
out vec4 FragColor;
#endif

uniform vec4 color;

void main()
{
#if __VERSION__ >= 330
Expand Down
3 changes: 2 additions & 1 deletion assets/shaders/shape_renderer.vert
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ layout (location = 0) in vec3 aPos;
#else
attribute vec3 aPos;
#endif
// texture, etc... in the future

uniform mat4 model;
#if GAMECOE_HAS_UBO
layout(std140) uniform CameraMatrices
{
mat4 projection;
mat4 view;
vec3 cameraPosition;
};
#else
uniform mat4 view;
uniform mat4 projection;
uniform vec3 cameraPosition;
#endif

void main()
Expand Down
73 changes: 73 additions & 0 deletions assets/shaders/sphere_renderer.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#if __VERSION__ >= 330
in vec3 worldPos;
out vec4 FragColor;
#else
varying vec3 worldPos;
#endif

uniform mat4 model;
uniform vec4 color;
#if GAMECOE_HAS_UBO
layout(std140) uniform CameraMatrices
{
mat4 projection;
mat4 view;
vec3 cameraPosition;
};
#else
uniform mat4 view;
uniform mat4 projection;
uniform vec3 cameraPosition;
#endif
uniform vec3 sphereCenter;
uniform vec3 sphereScale;
uniform mat3 sphereRotation;

vec3 sphereIntersection(vec3 rayOrigin, vec3 wPos)
{
const float RADIUS = 1.0;
const float BOX_RADIUS = 0.5;

mat3 sphereRotationT = transpose(sphereRotation);

vec3 effectiveScale = sphereScale * BOX_RADIUS;
vec3 rayDirection = vec3(sphereRotationT * normalize(wPos - rayOrigin)) / effectiveScale;
vec3 sphereCenterToRayOrigin = (sphereRotationT * vec3(rayOrigin - sphereCenter)) / effectiveScale;

float a = dot(rayDirection, rayDirection);
float b = 2.0 * dot(sphereCenterToRayOrigin, rayDirection);
float c = dot(sphereCenterToRayOrigin, sphereCenterToRayOrigin) - (RADIUS * RADIUS);

float discriminant = b * b - 4.0 * a * c;

if (discriminant < 0.0)
{
return vec3(0.0);
}

float t1 = (-b + sqrt(discriminant)) / (2.0 * a);
float t2 = (-b - sqrt(discriminant)) / (2.0 * a);
float t = t2 > 0.0 ? t2 : t1;
vec3 intersection = rayOrigin + (sphereRotation * (vec3(t * rayDirection) * effectiveScale));

return intersection;
}

void main()
{
vec3 spherePoint = sphereIntersection(cameraPosition, worldPos);
if (spherePoint == vec3(0.0))
{
discard;
}

vec4 shadingPos = projection * view * model * vec4(spherePoint, 1.0);
shadingPos /= shadingPos.w;
gl_FragDepth = shadingPos.z; // need version gating?

#if __VERSION__ >= 330
FragColor = color;
#else
gl_FragColor = color;
#endif
}
28 changes: 28 additions & 0 deletions assets/shaders/sphere_renderer.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#if __VERSION__ >= 330
layout (location = 0) in vec3 aPos;
out vec3 worldPos;
#else
attribute vec3 aPos;
varying vec3 worldPos;
#endif

uniform mat4 model;
#if GAMECOE_HAS_UBO
layout(std140) uniform CameraMatrices
{
mat4 projection;
mat4 view;
vec3 cameraPosition;
};
#else
uniform mat4 view;
uniform mat4 projection;
uniform vec3 cameraPosition;
#endif

void main()
{
vec4 wPos = model * vec4(aPos, 1.0);
worldPos = vec3(wPos);
gl_Position = projection * view * wPos;
}
13 changes: 9 additions & 4 deletions include/gamecoe/entity/renderer/shape_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@ namespace gamecoe
Triangle,
Rectangle,
Box,
Circle,
Sphere,
// TODO: Support more primitive shapes
};

class ShapeRenderer : public Renderer
{
static std::atomic<std::uint32_t> s_counter;
static std::optional<Shader> s_shader;
#if GAMECOE_HAS_UBO
static std::uint32_t s_cameraUniformBlockIndex;
#endif
static std::optional<Shader> s_shapeShader;
static std::optional<Shader> s_circleShader;
static std::optional<Shader> s_sphereShader;

Shape m_shape;
Color m_color;
const VertexArray &m_vertexArray;

std::optional<gamecoe::Shader> &shapeToShader(gamecoe::Shape shape) const;

ShapeRenderer(GameObject &owner, Shape shape, const Color &color, std::int8_t layer = 0);

public:
Expand All @@ -55,6 +58,8 @@ namespace gamecoe
static std::unique_ptr<ShapeRenderer> triangle(GameObject &owner, const Color &color, std::int8_t layer = 0);
static std::unique_ptr<ShapeRenderer> rectangle(GameObject &owner, const Color &color, std::int8_t layer = 0);
static std::unique_ptr<ShapeRenderer> box(GameObject &owner, const Color &color, std::int8_t layer = 0);
static std::unique_ptr<ShapeRenderer> circle(GameObject &owner, const Color &color, std::int8_t layer = 0);
static std::unique_ptr<ShapeRenderer> sphere(GameObject &owner, const Color &color, std::int8_t layer = 0);
};

} // namespace gamecoe
5 changes: 5 additions & 0 deletions src/gamecoe/core/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ namespace gamecoe
#if GAMECOE_USE_OPENGL
if(!gladLoadGL(glfwGetProcAddress))
detail::throwError("Game::Game(): Failed to initialize glad");

glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
#endif

m_internalScene.emplace(*this, "gamecoe::Internal");
Expand Down
1 change: 1 addition & 0 deletions src/gamecoe/core/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace gamecoe
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
#endif
glfwWindowHint(GLFW_SAMPLES, 4); // which opengl version? work only on opengl?

GLFWwindow *current = glfwGetCurrentContext();
GLFWwindow *window = glfwCreateWindow(m_width, m_height, m_title.c_str(), nullptr, current);
Expand Down
2 changes: 2 additions & 0 deletions src/gamecoe/entity/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ namespace gamecoe
{
glm::mat4 m_projection;
glm::mat4 m_view;
glm::vec3 m_cameraPosition;
} data;
data.m_projection = projectionMatrix();
data.m_view = viewMatrix();
data.m_cameraPosition = m_owner.transform().position();

m_uniformBuffer->uploadData(&data, sizeof(data));
#endif
Expand Down
Loading