Skip to content

Commit

Permalink
Added level and editor camera, frustrum drawing and playing
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklbell committed Aug 24, 2022
1 parent d8b72da commit e4d4b10
Show file tree
Hide file tree
Showing 18 changed files with 772 additions and 345 deletions.
Binary file modified data/levels/pillars.level
Binary file not shown.
Binary file removed data/levels/test.level
Binary file not shown.
Binary file modified data/levels/water_test.level
Binary file not shown.
48 changes: 48 additions & 0 deletions data/shaders/distance_blur.gl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#if COMPILING_VS

layout (location = 0) in vec3 in_vertex;
layout (location = 1) in vec2 in_texcoord;

out vec2 texcoord;

void main(){
gl_Position = vec4(in_vertex,1.0);
texcoord = in_texcoord;
}

#endif

#if COMPILING_FS
out vec4 out_color;

in vec2 texcoord;

uniform sampler2D image;

uniform bool horizontal;

void main(){
const float weight[5] = float[] (1.f/2.f, 1.f/3.f, 1.f/4.f, 1.f/5.f, 1.f/6.f);
//vec2 tex_offset = 1.0 / textureSize(image, 0); // gets size of single texel
vec2 tex_offset = vec2(0.0003);
vec3 result = texture(image, texcoord).rgb * weight[0]; // current fragment's contribution
if(horizontal)
{
for(int i = 1; i < 5; ++i)
{
result += texture(image, texcoord + vec2(tex_offset.x * i, 0.0)).rgb * weight[i];
result += texture(image, texcoord - vec2(tex_offset.x * i, 0.0)).rgb * weight[i];
}
}
else
{
for(int i = 1; i < 5; ++i)
{
result += texture(image, texcoord + vec2(0.0, tex_offset.y * i)).rgb * weight[i];
result += texture(image, texcoord - vec2(0.0, tex_offset.y * i)).rgb * weight[i];
}
}
out_color = vec4(min(result, vec3(1.0)), 1.0);
}

#endif
25 changes: 17 additions & 8 deletions data/shaders/water.gl
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ uniform float time;

vec3 gwaves(in vec3 v) {
const int wn = 2;
const vec2 d[wn] = {normalize(vec2(0.2, 0.4)), normalize(vec2(0.3, 0.2))};
const float a[wn] = {0.08, 0.03};
const float q[wn] = {0.7, 0.3};
const float p[wn] = {0.7, 0.6};
const float w[wn] = {20.0, 10.0};
const vec2 d[wn] = {normalize(vec2(0.2, 0.4)), normalize(vec2(0.3, 0.2))};
const float a[wn] = {0.08, 0.03};
const float q[wn] = {0.7, 0.3};
const float p[wn] = {0.7, 0.6};
const float w[wn] = {20.0, 10.0};

vec3 res = v;
for(int i = 0; i < wn; i++){
Expand All @@ -36,7 +36,8 @@ vec3 gwaves(in vec3 v) {
}

void main(){
vec3 vertex = gwaves(in_vertex);
// vec3 vertex = gwaves(in_vertex);
vec3 vertex = in_vertex;
vec4 v = vec4(vertex, 1.0);
gl_Position = mvp * v;
vs_out.position = (model * v).xyz;
Expand Down Expand Up @@ -175,6 +176,7 @@ layout (location = 1) out vec4 out_bloom_color;

uniform sampler2D simplex_gradient;
uniform sampler2D simplex_value;
uniform sampler2D collider;

#load lib/shadows.gl

Expand Down Expand Up @@ -257,11 +259,18 @@ void main(){
float scene_d = gl_FragCoord.z;

vec4 alpha = texture(screen_map, screencoord);
if (scene_d >= screen_d) {
if (scene_d > screen_d) {
out_color = alpha;

gl_FragDepth = screen_d;
} else {
float collision = texture(collider, vs_in.texcoord.xy).r;
// collision = sqrt(collision);
// collision = max(
// clamp(sin(exp(-(2.0-collision))*0.5f*t + 5.0*t)*2.f*collision, 0.5, 1.0f) - 0.5,
// clamp(1.1-exp(-collision*collision), 0.5, 1.0) - 0.5
// )*2.0f;

vec3 n1 = normalnoise(10.0*vs_in.texcoord + t);

float screen_z = depthToDist(screen_d);
Expand All @@ -277,7 +286,7 @@ void main(){

vec4 col = mix(mat, alpha, min(rad*0.7 + 0.08, 0.8));

col = mix(col, foam_color, foam_dif + foam_dif*noise(18.0*vs_in.texcoord + (-1.0+screen_z - scene_z)*t*0.1 + t));
col = mix(col, foam_color, collision);

vec3 albedo = col.xyz;
vec3 normal = dgwaves(vs_in.vertex);
Expand Down
62 changes: 61 additions & 1 deletion data/shaders/white.gl
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,76 @@ uniform mat4 mvp;

void main()
{
// transforms vertex into grid space
gl_Position = mvp * vec4(vertex, 1.0);
}
#endif

#ifdef COMPILING_GS

layout (triangles) in;
layout (triangle_strip, max_vertices = 4) out;

uniform float height;

vec2 getIntersectionWithPlane(vec4 p1, vec4 p2) {
float t = abs(p1.y / (p2.y - p1.y));
vec2 i = p1.xz + (p2.xz - p1.xz)*t;
i = -i;
i.x = (1.0 - 2.0*i.x)/2.0f - 0.5f;
return i;
}

#define THICKNESS 0.001

void makeLine(vec2 p1, vec2 p2) {
vec2 d = normalize(p2 - p1);
vec2 n = vec2(-d.y, d.x)*THICKNESS;
gl_Position = vec4(p1, 0.0, 1.0);
EmitVertex();
gl_Position = vec4(p1 + n, 0.0, 1.0);
EmitVertex();
gl_Position = vec4(p2 + n, 0.0, 1.0);
EmitVertex();
gl_Position = vec4(p2, 0.0, 1.0);
EmitVertex();
EndPrimitive();
}

void main() {
bool is_below0 = gl_in[0].gl_Position.y <= 0;
bool is_below1 = gl_in[1].gl_Position.y <= 0;
bool is_below2 = gl_in[2].gl_Position.y <= 0;
// Triangle passes through plane
if(is_below0 != is_below1 || is_below0 != is_below2) {
int i = 0;
vec2 points[2];
if(is_below0 != is_below1) {
points[i] = getIntersectionWithPlane(gl_in[0].gl_Position, gl_in[1].gl_Position);
i++;
}
if (is_below1 != is_below2) {
points[i] = getIntersectionWithPlane(gl_in[1].gl_Position, gl_in[2].gl_Position);
i++;
}
if (is_below2 != is_below0) {
points[i] = getIntersectionWithPlane(gl_in[2].gl_Position, gl_in[0].gl_Position);
i++;
}

makeLine(points[0], points[1]);
}
}

#endif

#ifdef COMPILING_FS

out vec4 out_color;

void main()
{
out_color = vec4(1.0);
out_color = vec4(gl_FragCoord.xy, 0.0, 1.0);
out_color.xy /= 1000.0f;
}
#endif
3 changes: 2 additions & 1 deletion include/controls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace controls {
extern glm::dvec2 mouse_position;
extern glm::dvec2 delta_mouse_position;
}
void handleEditorControls(Camera &camera, EntityManager &entity_manager, float dt);
void handleEditorControls(Camera& editor_camera, Camera& level_camera, EntityManager& entity_manager, float dt);
void handleGameControls();
void initEditorControls();

#endif
7 changes: 5 additions & 2 deletions include/editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ enum class TransformType : unsigned int{

void initEditorGui(AssetManager &asset_manager);

void ImTerminal(EntityManager &entity_manager, AssetManager &asset_manager, bool is_active);
void ImTerminal(EntityManager &entity_manager, AssetManager &asset_manager, bool is_active, Camera &level_camera, Camera &editor_camera);

void drawEditorGui(Camera &camera, EntityManager &entity_manager, AssetManager &asset_manager);
void drawEditorGui(Camera& editor_camera, Camera& level_camera, EntityManager& entity_manager, AssetManager& asset_manager);
void drawGameGui(Camera& editor_camera, Camera& level_camera, EntityManager& entity_manager, AssetManager& asset_manager);

bool editorTranslationGizmo(glm::vec3 &pos, glm::quat &rot, glm::mat3 &scl, Camera &camera, const glm::vec3 &snap, bool do_snap);
bool editorRotationGizmo(glm::vec3 &pos, glm::quat &rot, glm::mat3 &scl, const Camera &camera, float rot_snap, bool do_snap);
Expand All @@ -40,6 +41,7 @@ bool editTransform(Camera &camera, glm::vec3 &pos, glm::quat &rot, glm::mat3 &sc
void drawEditor3DArrow(const glm::vec3 &position, const glm::vec3 &direction, const Camera &camera, const glm::vec4 &color, const glm::vec3 &scale, bool shaded=true, bool block=false);
void drawEditor3DRing(const glm::vec3 &position, const glm::vec3 &direction, const Camera &camera, const glm::vec4 &color, const glm::vec3 &scale, bool shaded=true);
void drawMeshCube(const glm::vec3 &pos, const glm::quat &rot, const glm::mat3x3 &scl, const Camera &camera);
void drawFrustrum(Camera& drawn_camera, const Camera& camera);
void drawMeshWireframe(const Mesh &mesh, const glm::vec3 &pos, const glm::quat &rot, const glm::mat3x3 &scl, const Camera &camera, bool flash);
void drawWaterDebug(WaterEntity* w_e, const Camera &camera, bool flash);

Expand All @@ -57,6 +59,7 @@ namespace editor {
extern ImGui::FileBrowser im_file_dialog;
extern bool draw_debug_wireframe;
extern bool transform_active;
extern bool use_level_camera, draw_level_camera;
extern Id sel_e;
extern AssetManager editor_assets;
extern glm::vec3 translation_snap;
Expand Down
1 change: 1 addition & 0 deletions include/globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern bool draw_bloom;
extern AssetManager global_assets;
extern std::string GL_version, GL_vendor, GL_renderer;
extern std::string level_path;
extern bool playing;

struct ThreadPool;
extern ThreadPool *global_thread_pool;
Expand Down
14 changes: 10 additions & 4 deletions include/graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ struct Camera {
enum TYPE {
TRACKBALL = 0,
SHOOTER = 1,
STATIC = 2,
} state;

const float near_plane = 1.0f, far_plane = 100.0f;
const glm::vec3 up = glm::vec3(0,1,0);
float near_plane = 1.0f, far_plane = 100.0f;
float fov = glm::radians(45.0f);
glm::vec3 up = glm::vec3(0,1,0);
glm::vec3 forward;
glm::vec3 right;
glm::vec3 position;
glm::vec3 target;
glm::mat4 view;
Expand All @@ -52,13 +56,15 @@ void updateCameraProjection(Camera &camera);
void initGraphicsPrimitives(AssetManager &asset_manager);
void drawQuad();
void drawCube();
void drawLineCube();

void updateShadowVP(const Camera &camera);
void initShadowFbo();
void bindDrawShadowMap(const EntityManager &entity_manager, const Camera &camera);

void initWaterColliderFbo();
void bindDrawWaterColliderMap(const EntityManager &entity_manager, const Camera &camera, WaterEntity *water);
void bindDrawWaterColliderMap(const EntityManager &entity_manager, WaterEntity *water);
void blurWaterFbo();

void clearFramebuffer(const glm::vec4 &color);
void bindHdr();
Expand All @@ -81,7 +87,7 @@ namespace graphics {
extern const char * shadow_invocation_macro;
extern GLuint shadow_buffer, shadow_fbo;

extern GLuint water_collider_fbo, water_collider, water_collider_depth;
extern GLuint water_collider_fbos[2], water_collider_buffers[2];

extern Mesh quad;
extern Mesh cube;
Expand Down
14 changes: 12 additions & 2 deletions include/shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace shader {
UNIFIED_SHADER,
WATER_SHADER,
GAUSSIAN_BLUR_SHADER,
DISTANCE_BLUR_SHADER,
POST_SHADER,
DEBUG_SHADER,
SKYBOX_SHADER,
Expand Down Expand Up @@ -50,6 +51,11 @@ namespace shader {
GLuint horizontal;
} gaussian_blur_uniforms;

extern GLuint distance_blur_program;
extern struct DistanceBlurUniforms {
GLuint horizontal;
} distance_blur_uniforms;

extern GLuint debug_program;
extern struct DebugUniforms {
GLuint mvp, model, sun_direction, time, flashing, shaded, color, color_flash_to;
Expand All @@ -66,10 +72,14 @@ namespace shader {
} skybox_uniforms;

extern GLuint depth_only_program;
extern GLuint white_program;
extern struct MvpUniforms {
GLuint mvp;
} depth_only_uniforms, white_uniforms;
} depth_only_uniforms;

extern GLuint white_program;
extern struct WhiteUniforms {
GLuint mvp, height;
} white_uniforms;
}

void loadShader(std::string path, shader::TYPE type);
Expand Down
5 changes: 3 additions & 2 deletions include/utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

#include "graphics.hpp"

void saveLevel(EntityManager& entity_manager, const std::string& level_path);
bool loadLevel(EntityManager &entity_manager, AssetManager &asset_manager, const std::string &level_path);
void saveLevel(EntityManager& entity_manager, const std::string& level_path, const Camera &camera);
bool loadLevel(EntityManager &entity_manager, AssetManager &asset_manager, const std::string &level_path, Camera& camera);

void checkGLError(std::string identifier="");

glm::mat4x4 createModelMatrix(const glm::vec3 &pos, const glm::quat &rot, const glm::mat3x3 &scl);
glm::mat4x4 createModelMatrix(const glm::vec3& pos, const glm::mat3x3& rot, const glm::mat3x3& scl);

void screenPosToWorldRay(glm::ivec2 mouse_position, glm::mat4 view, glm::mat4 projection, glm::vec3 &out_origin, glm::vec3 &out_direction);

Expand Down
Loading

0 comments on commit e4d4b10

Please sign in to comment.