Skip to content

Commit

Permalink
Began work on water collider calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklbell committed Aug 19, 2022
1 parent 25020d6 commit d8b72da
Show file tree
Hide file tree
Showing 16 changed files with 439 additions and 196 deletions.
Binary file modified data/levels/pillars.level
Binary file not shown.
Binary file modified data/levels/water_test.level
Binary file not shown.
Binary file modified data/mesh/pillar1.mesh
Binary file not shown.
18 changes: 18 additions & 0 deletions data/shaders/depth_only.gl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifdef COMPILING_VS
layout (location = 0) in vec3 vertex;

uniform mat4 mvp;

void main()
{
gl_Position = mvp * vec4(vertex, 1.0);
}
#endif

#ifdef COMPILING_FS


void main()
{
}
#endif
5 changes: 5 additions & 0 deletions data/shaders/unified.gl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ uniform vec3 camera_position;
uniform vec3 sun_direction;
uniform vec3 sun_color;


void main() {
// obtain TBN normal from normal map in range [0,1]
vec3 normal = texture(normal_map, texcoord).rgb;
Expand Down Expand Up @@ -98,6 +99,10 @@ void main() {
#endif

#ifdef PBR
//vec3 l = normalize(position - camera_position);
//vec3 r = reflect(l, normalize(normal));

//vec3 reflect = vec4(texture(skybox, r).rgb, 1.0);
float roughness = texture(roughness_map, texcoord).r;
float metallic = texture(metallic_map, texcoord).r;
float ao = texture(ao_map, texcoord).r + 0.04;
Expand Down
20 changes: 20 additions & 0 deletions data/shaders/white.gl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifdef COMPILING_VS
layout (location = 0) in vec3 vertex;

uniform mat4 mvp;

void main()
{
gl_Position = mvp * vec4(vertex, 1.0);
}
#endif

#ifdef COMPILING_FS

out vec4 out_color;

void main()
{
out_color = vec4(1.0);
}
#endif
6 changes: 6 additions & 0 deletions include/editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ namespace editor {
extern Id sel_e;
extern AssetManager editor_assets;
extern glm::vec3 translation_snap;

struct Selection {
Id id = NULLID;
bool is_water = false;
};
extern Selection selection;
}

#endif
6 changes: 6 additions & 0 deletions include/entities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ struct EntityManager {
std::stack<int> delete_entity_stack;
int id_counter = 0;

WaterEntity *water = nullptr;

~EntityManager(){
clear();
}
Expand All @@ -107,6 +109,10 @@ struct EntityManager {
memset(versions, 0, sizeof(versions));
free_entity_stack = {};
delete_entity_stack = {};
if(water != nullptr) {
free(water);
water = nullptr;
}
id_counter = 0;
}
inline Entity *getEntity(Id id){
Expand Down
11 changes: 9 additions & 2 deletions include/graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class EntityManager;
struct Mesh;
struct Texture;
struct WaterEntity;

extern int window_width;
extern int window_height;
Expand Down Expand Up @@ -56,6 +57,9 @@ 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 clearFramebuffer(const glm::vec4 &color);
void bindHdr();
void drawSkybox(const Texture* skybox, const Camera &camera);
Expand All @@ -75,11 +79,14 @@ namespace graphics {
extern GLuint hdr_buffers[2];
extern const char *shadow_macro;
extern const char * shadow_invocation_macro;
extern GLuint shadow_fbo;
extern GLuint shadow_buffer;
extern GLuint shadow_buffer, shadow_fbo;

extern GLuint water_collider_fbo, water_collider, water_collider_depth;

extern Mesh quad;
extern Mesh cube;
extern Mesh grid;

extern Texture * simplex_gradient;
extern Texture * simplex_value;
}
Expand Down
8 changes: 8 additions & 0 deletions include/shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace shader {
POST_SHADER,
DEBUG_SHADER,
SKYBOX_SHADER,
WHITE_SHADER,
DEPTH_ONLY_SHADER,
NUM_SHADER_TYPES,
};
extern GLuint null_program;
Expand Down Expand Up @@ -62,6 +64,12 @@ namespace shader {
extern struct SkyboxUniforms {
GLuint view, projection;
} skybox_uniforms;

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

void loadShader(std::string path, shader::TYPE type);
Expand Down
71 changes: 44 additions & 27 deletions src/controls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ void initEditorControls(){
delta_mouse_position = glm::dvec2(0,0);
}

using namespace editor;

void handleEditorControls(Camera &camera, EntityManager &entity_manager, float dt) {
// Stores the previous state of input, updated at end of function
static int c_key_state = GLFW_RELEASE;
Expand All @@ -75,24 +73,37 @@ void handleEditorControls(Camera &camera, EntityManager &entity_manager, float d
glfwGetCursorPos(window, &mouse_position.x, &mouse_position.y);
delta_mouse_position = mouse_position - delta_mouse_position;

Entity *sel_e;
if(editor::selection.is_water) {
sel_e = entity_manager.water;
} else {
sel_e = entity_manager.getEntity(editor::selection.id);
}
if(backtick_key_state == GLFW_RELEASE && glfwGetKey(window, GLFW_KEY_GRAVE_ACCENT) == GLFW_PRESS) {
editor::do_terminal = !editor::do_terminal;
}
if(!io.WantCaptureKeyboard){
if(sel_e.i != -1 && glfwGetKey(window, GLFW_KEY_DELETE) == GLFW_PRESS){
entity_manager.deleteEntity(editor::sel_e);
sel_e = NULLID;
if(sel_e != nullptr && glfwGetKey(window, GLFW_KEY_DELETE) == GLFW_PRESS){
if(sel_e->type == WATER_ENTITY) {
free(entity_manager.water);
entity_manager.water = nullptr;
} else {
entity_manager.deleteEntity(sel_e->id);
}
sel_e = nullptr;
editor::selection = editor::Selection();
}
if(sel_e.i != -1 && glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS && c_key_state == GLFW_RELEASE && glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS){
copy_id = sel_e;
if(sel_e != nullptr && sel_e->type != WATER_ENTITY && glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS && c_key_state == GLFW_RELEASE && glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS){
copy_id = sel_e->id;
}
if(copy_id.i != -1 && ctrl_v_state == GLFW_RELEASE && glfwGetKey(window, GLFW_KEY_V) == GLFW_PRESS && glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS && entity_manager.getEntity(copy_id) != nullptr){
auto e = entity_manager.duplicateEntity(copy_id);
copy_id = e->id;
if((e->type & MESH_ENTITY)){
if(e->type == MESH_ENTITY){
((MeshEntity*)e)->position.x += editor::translation_snap.x;
if(camera.state == Camera::TYPE::TRACKBALL){
sel_e = e->id;
editor::selection.id = copy_id;
editor::selection.is_water = true;
camera.target = ((MeshEntity*)e)->position;
updateCameraView(camera);
}
Expand All @@ -111,14 +122,15 @@ void handleEditorControls(Camera &camera, EntityManager &entity_manager, float d
mouse_position = glm::dvec2(window_width/2, window_height/2);
delta_mouse_position = glm::dvec2(0,0);

sel_e = NULLID;
sel_e = nullptr;
editor::selection = editor::Selection();
} else if(camera.state == Camera::TYPE::SHOOTER) {
camera.state = Camera::TYPE::TRACKBALL;

glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);

if(sel_e.i != -1){
auto s_m_e = (MeshEntity*)entity_manager.getEntity(sel_e);
if(sel_e != nullptr){
auto s_m_e = reinterpret_cast<MeshEntity*>(sel_e);
if(s_m_e != nullptr && s_m_e->type == EntityType::MESH_ENTITY){
camera.target = s_m_e->position;
updateCameraView(camera);
Expand All @@ -131,35 +143,38 @@ void handleEditorControls(Camera &camera, EntityManager &entity_manager, float d

if(camera.state == Camera::TYPE::TRACKBALL){
if (right_mouse_click_release) {
sel_e = NULLID;
sel_e = nullptr;
editor::selection = editor::Selection();
}
if (left_mouse_click_release && (glfwGetTime() - mouse_left_press_time) < 0.2) {
glm::vec3 out_origin;
glm::vec3 out_direction;
screenPosToWorldRay(mouse_position, camera.view, camera.projection, out_origin, out_direction);

sel_e = NULLID;
sel_e = nullptr;
editor::selection = editor::Selection();

float min_collision_distance = std::numeric_limits<float>::max();
for(int i = 0; i < ENTITY_COUNT; ++i){
auto m_e = (MeshEntity*)entity_manager.entities[i];
if(m_e == nullptr) continue;
// Collision with bounded plane
if(m_e->type & EntityType::WATER_ENTITY){
auto w_e = (WaterEntity*)m_e;
float t;
if(!lineIntersectsPlane(w_e->position, glm::vec3(0,1,0), out_origin, out_direction, t)) continue;

// Collision with bounded plane
if(entity_manager.water != nullptr){
auto w_e = entity_manager.water;
float t;
if(lineIntersectsPlane(w_e->position, glm::vec3(0,1,0), out_origin, out_direction, t)){
auto p = glm::abs((out_origin + out_direction*t) - w_e->position);
if(p.x <= w_e->scale[0][0] && p.z <= w_e->scale[2][2]) {
auto collision_distance = glm::length((out_origin + out_direction*t) - camera.position);
if(collision_distance < min_collision_distance){
min_collision_distance = collision_distance;
sel_e = w_e->id;
sel_e = w_e;
camera.target = w_e->position;
}
}
continue;
}
if(m_e->type != EntityType::MESH_ENTITY || m_e->mesh == nullptr) continue;
}
for(int i = 0; i < ENTITY_COUNT; ++i){
auto m_e = (MeshEntity*)entity_manager.entities[i];
if(m_e == nullptr || m_e->type != EntityType::MESH_ENTITY || m_e->mesh == nullptr) continue;

const auto &mesh = m_e->mesh;
const auto trans = createModelMatrix(m_e->position, m_e->rotation, m_e->scale);
Expand All @@ -177,13 +192,15 @@ void handleEditorControls(Camera &camera, EntityManager &entity_manager, float d
auto collision_distance = glm::length((out_origin + out_direction*(float)t) - camera.position);
if(collision_distance < min_collision_distance){
min_collision_distance = collision_distance;
sel_e = m_e->id;
sel_e = m_e;
camera.target = m_e->position;
}
}
}
}
if(sel_e.i != -1){
if(sel_e != nullptr){
editor::selection.id = sel_e->id;
editor::selection.is_water = sel_e->type == WATER_ENTITY;
updateCameraView(camera);
updateShadowVP(camera);
}
Expand Down
Loading

0 comments on commit d8b72da

Please sign in to comment.