Skip to content

Commit

Permalink
Fixed bug in jitter generation, added lightmaps to level and editor
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklbell committed Oct 29, 2022
1 parent aa08758 commit a7829be
Show file tree
Hide file tree
Showing 44 changed files with 93 additions and 149 deletions.
Binary file removed data/levels/animation_test.level
Binary file not shown.
Binary file removed data/levels/animation_test2.level
Binary file not shown.
Binary file removed data/levels/cerberus_test.level
Binary file not shown.
Binary file removed data/levels/collider_test.level
Binary file not shown.
Binary file removed data/levels/collision_test.level
Binary file not shown.
Binary file added data/levels/gameplay_test.level
Binary file not shown.
Binary file removed data/levels/lightmap_gazebo.level
Binary file not shown.
Binary file removed data/levels/lightmap_gazebo.level.0.tga
Binary file not shown.
Binary file removed data/levels/lightmap_gazebo.level.2.tga
Binary file not shown.
Binary file modified data/levels/lightmap_test.level
Binary file not shown.
Binary file modified data/levels/lightmap_test.level.0.tga
Binary file not shown.
Binary file modified data/levels/lightmap_test.level.1.tga
Binary file not shown.
Binary file removed data/levels/lightmap_test.level.2.tga
Binary file not shown.
Binary file removed data/levels/lightmap_test2.level
Binary file not shown.
Binary file removed data/levels/lightmap_test2.level.0.tga
Binary file not shown.
Binary file removed data/levels/lightmap_test2.level.1.tga
Binary file not shown.
Binary file removed data/levels/lightmap_test2.level.2.tga
Binary file not shown.
Binary file removed data/levels/lightmap_test2.level.3.tga
Binary file not shown.
Binary file removed data/levels/lightmap_test2.level.4.tga
Binary file not shown.
Binary file removed data/levels/lightmap_test2.level.5.tga
Binary file not shown.
Binary file removed data/levels/lightmap_test2.level.6.tga
Binary file not shown.
Binary file removed data/levels/pillars.level
Binary file not shown.
Binary file removed data/levels/player_test.level
Binary file not shown.
Binary file removed data/levels/test.level
Binary file not shown.
Binary file removed data/levels/test.level.0.tga
Binary file not shown.
Binary file removed data/levels/test_level1.level
Binary file not shown.
Binary file removed data/levels/test_level1.level.35.tga
Binary file not shown.
Binary file removed data/levels/vegetation_test.level
Binary file not shown.
Binary file removed data/levels/water_test.level
Binary file not shown.
Binary file modified data/mesh/character.mesh
Binary file not shown.
7 changes: 4 additions & 3 deletions data/shaders/lib/pbr.gl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ vec3 importanceSampleGGX(vec2 Xi, vec3 normal, vec3 tangent, vec3 bitangent, flo
const float MAX_REFLECTION_LOD = 4.0;

// @note Radiance scaled color
vec3 brdfSchlickGGX(vec3 position, vec3 normal, vec3 albedo, float metallic, float roughness, vec3 light_dir, vec3 light_col, vec3 camera_position, float ao, vec3 GI) {
vec3 brdfSchlickGGX(vec3 position, vec3 normal, vec3 albedo, float metallic, float roughness, vec3 light_dir, vec3 light_col, vec3 camera_position, float ao) {
vec3 view_dir = normalize(camera_position - position);
vec3 halfway_dir = normalize(view_dir + light_dir);
vec3 radiance = light_col;
Expand All @@ -115,6 +115,7 @@ vec3 brdfSchlickGGX(vec3 position, vec3 normal, vec3 albedo, float metallic, flo
kD *= 1.0 - metallic;

vec3 lo = (kD * albedo / PI + specular) * radiance * NdotL;
lo *= ao;
#if SHADOWS
lo *= 1.0 - shadowness(NdotL, position);
#endif
Expand All @@ -128,10 +129,10 @@ vec3 brdfSchlickGGX(vec3 position, vec3 normal, vec3 albedo, float metallic, flo
vec3 env_specular = prefiltered_color * (env_F * env_brdf.x + env_brdf.y);

vec3 irradiance = texture(irradiance_map, normal).rgb;
vec3 env_diffuse = irradiance * albedo * GI;
vec3 env_diffuse = irradiance * albedo;
vec3 ambient = kD * env_diffuse + env_specular;
#else
vec3 ambient = albedo * GI;
vec3 ambient = albedo;
#endif

return ao*(ambient + lo);
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/lib/shadows.gl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ uniform mat4 view;
#define SAMPLES_COUNT 16
#define SAMPLES_COUNT_DIV_2 8
#define INV_SAMPLES_COUNT (1.0f / SAMPLES_COUNT)
#define SHADOW_FILTER_SIZE 100.0
#define SHADOW_FILTER_SIZE 200.0

float calculatePcfShadowness(float NdotL, vec3 position, int layer) {
vec4 shadow_pos = shadow_vps[layer] * vec4(position, 1.0);
Expand Down
5 changes: 4 additions & 1 deletion data/shaders/unified.gl
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,11 @@ void main() {

vec3 GI = texture(ao_map, texcoord).rgb;
float ao = ao_mult*length(GI);
if (ao > 0.0f) {
albedo *= normalize(GI);
}

vec3 hdr_color = brdfSchlickGGX(position, normal, albedo, metallic, roughness, -sun_direction, sun_color, camera_position, ao, texture(ao_map, texcoord).rgb);
vec3 hdr_color = brdfSchlickGGX(position, normal, albedo, metallic, roughness, -sun_direction, sun_color, camera_position, ao);
#endif

out_color = vec4(hdr_color, gl_FrontFacing ? 1.0 : 0.0);
Expand Down
1 change: 1 addition & 0 deletions data/shaders/vegetation.gl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void main()

gl_Position = mvp * vec4(vertex, 1.0);
vs_out.texcoord = in_texcoord;
vs_out.texcoord.y = 1.0 - vs_out.texcoord.y;
vs_out.color = in_color;
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/water.gl
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ void main(){
vec3 albedo = col.xyz;

// Maybe full brdf is unnecessary? test performance
vec3 hdr_color = brdfSchlickGGX(vs_in.position, normal, albedo, 0.1, 0.1f, -sun_direction, sun_color, camera_position, 0.8, vec3(1.0f));
vec3 hdr_color = brdfSchlickGGX(vs_in.position, normal, albedo, 0.0f, 0.0f, -sun_direction, sun_color, camera_position, 1.0);

//hdr_color = mix(deep_color.rgb, hdr_color, 1.0 - smoothstep(15.0, 25.0, scene_z));

Expand Down
2 changes: 1 addition & 1 deletion include/assets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ struct AssetManager {
// @note that this function could cause you to "lose" a texture if the path is the same
Texture* createTexture(const std::string &handle);
bool loadTextureFromAssimp(Texture *&tex, aiMaterial* mat, const aiScene* scene, aiTextureType texture_type, GLint internal_format=GL_RGB16F);
static bool loadTexture(Texture *tex, const std::string &path, GLint internal_format=GL_RGB16F, const bool tile = true);
static bool loadTexture(Texture *tex, const std::string &path, GLint internal_format=GL_RGB16F, const bool tile = false);
static bool loadCubemapTexture(Texture *tex, const std::array<std::string, FACE_NUM_FACES> &paths, GLint internal_format=GL_RGB16F);

Audio* createAudio(const std::string& handle);
Expand Down
1 change: 1 addition & 0 deletions include/entities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct MeshEntity : Entity {
float ao_mult = 1.0;

uint8_t casts_shadow = true;
uint8_t do_lightmap = true;
Texture* lightmap = nullptr;

MeshEntity(Id _id = NULLID) : Entity(_id) {
Expand Down
4 changes: 2 additions & 2 deletions include/texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ struct ImageData {
GLuint create1x1Texture(const unsigned char color[3], const GLint internal_format = GL_RGB);
GLuint create1x1TextureFloat(const glm::fvec3& color, const GLint internal_format = GL_RGB);
bool loadImageData(ImageData *img, const std::string& imagepath, const GLint internal_format=GL_RGBA);
GLuint createGLTextureFromData(ImageData *img, const GLint internal_format=GL_RGBA, const bool tile=true);
GLuint loadImage(const std::string &imagepath, glm::ivec2& resolution, const GLint internal_format=GL_RGBA, const bool tile = true);
GLuint createGLTextureFromData(ImageData *img, const GLint internal_format=GL_RGBA, const bool tile = false);
GLuint loadImage(const std::string &imagepath, glm::ivec2& resolution, const GLint internal_format=GL_RGBA, const bool tile = false);
GLuint loadCubemap(const std::array<std::string, FACE_NUM_FACES> &paths, glm::ivec2& resolution, const GLint internal_format=GL_RGB);

#endif
1 change: 0 additions & 1 deletion src/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,6 @@ static constexpr auto ai_import_flags = aiProcess_JoinIdenticalVertices |
aiProcess_Triangulate |
aiProcess_GenNormals |
aiProcess_CalcTangentSpace |
aiProcess_FlipUVs |
//aiProcess_RemoveComponent (remove colors) |
aiProcess_ImproveCacheLocality |
aiProcess_RemoveRedundantMaterials |
Expand Down
140 changes: 25 additions & 115 deletions src/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,8 @@ void drawEditorGui(EntityManager &entity_manager, AssetManager &asset_manager){
static const std::vector<std::string> image_file_extensions = { ".jpg", ".png", ".bmp", ".tiff", ".tga" };
static bool editing_gizmo_position_offset = false;

auto button_size = ImVec2(ImGui::GetWindowWidth() / 2.0f - pad, 2.0f * pad);
auto button_size = ImVec2(ImGui::GetWindowWidth() - 2.0f * pad, 2.0f * pad);
auto h_button_size = ImVec2(ImGui::GetWindowWidth() / 2.0f - pad, 2.0f * pad);
if (entityInherits(selection.type, MESH_ENTITY)) {
auto m_e = (MeshEntity*)fe;

Expand Down Expand Up @@ -1820,6 +1821,25 @@ void drawEditorGui(EntityManager &entity_manager, AssetManager &asset_manager){
if (metal_chng) e->metal_mult = metal;
}
}

if (selection.ids.size() == 1 && ImGui::CollapsingHeader("Lightmapping")) {
auto m_e = (MeshEntity*)fe;

bool do_lightmap = m_e->do_lightmap;
if(ImGui::Checkbox("Do Lightmap", &do_lightmap)) {
m_e->do_lightmap = do_lightmap;
}

if (m_e->lightmap != nullptr) {
void* tex = (void*)(intptr_t)m_e->lightmap->id;
ImGui::Image(tex, ImVec2(sidebar_w, sidebar_w));

if (ImGui::Button("Clear", button_size)) {
m_e->lightmap = nullptr;
}
}
}

}
if (entityInherits(selection.type, WATER_ENTITY) && selection.ids.size() == 1) {
auto w_e = (WaterEntity*)fe;
Expand Down Expand Up @@ -1906,8 +1926,9 @@ void drawEditorGui(EntityManager &entity_manager, AssetManager &asset_manager){
}

ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetTextLineHeight());
bool half = false;
if(!(entityInherits(selection.type, WATER_ENTITY))){
if (ImGui::Button("Duplicate", button_size)) {
if (ImGui::Button("Duplicate", h_button_size)) {
if (camera.state == Camera::TYPE::TRACKBALL && entityInherits(selection.type, MESH_ENTITY)) {
auto m_e = (MeshEntity*)fe;
camera.target = m_e->position + translation_snap.x;
Expand All @@ -1924,8 +1945,9 @@ void drawEditorGui(EntityManager &entity_manager, AssetManager &asset_manager){
}
}
ImGui::SameLine();
half = true;
}
if(ImGui::Button("Delete", button_size)){
if(ImGui::Button("Delete", half ? h_button_size : button_size)){
for (auto& id : selection.ids) {
entity_manager.deleteEntity(id);
if (id == entity_manager.water) {
Expand All @@ -1934,7 +1956,6 @@ void drawEditorGui(EntityManager &entity_manager, AssetManager &asset_manager){
}
selection.clear();
}
button_size.x *= 2.0;
if (entityInherits(selection.type, MESH_ENTITY)) {
if (ImGui::Button("Change Mesh", button_size)) {
im_file_dialog.SetPwd(exepath + "/data/mesh");
Expand All @@ -1951,118 +1972,7 @@ void drawEditorGui(EntityManager &entity_manager, AssetManager &asset_manager){
}
}

// Information shared with file browser
//static Mesh* s_mesh = nullptr;
//{
// constexpr int true_win_width = 250;
// int win_width = true_win_width - pad;
// auto button_size = ImVec2(win_width, 2*pad);
// auto half_button_size = ImVec2(win_width / 2.f, 2*pad);

// ImGui::SetNextWindowPos(ImVec2(0,0));
// ImGui::SetNextWindowSize(ImVec2(true_win_width, window_height));
// ImGui::SetNextWindowSizeConstraints(ImVec2(true_win_width, window_height), ImVec2(window_width/2.f, window_height));

// ImGui::Begin("Global Properties", NULL, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse);
// //if (ImGui::CollapsingHeader("Camera", ImGuiTreeNodeFlags_DefaultOpen)) {
// // float distance = glm::length(camera.position - camera.target);
// // if (ImGui::SliderFloat("Distance", &distance, 1.f, 100.f)) {
// // camera.position = camera.target + glm::normalize(camera.position - camera.target)*distance;
// // updateCameraView(camera);
// // }
// //}

// if (ImGui::CollapsingHeader("Levels")){
// static char level_name[256] = "";
// if (ImGui::Button("Save Level", half_button_size)){
// im_file_dialog_type = "saveLevel";
// im_file_dialog.SetPwd(exepath+"/data/levels");
// im_file_dialog.SetCurrentTypeFilterIndex(4);
// im_file_dialog.SetTypeFilters({".level"});
// im_file_dialog.Open();
// }
// ImGui::SameLine();
// if (ImGui::Button("Load Level", half_button_size)){
// im_file_dialog_type = "loadLevel";
// im_file_dialog.SetPwd(exepath+"/data/levels");
// im_file_dialog.SetCurrentTypeFilterIndex(4);
// im_file_dialog.SetTypeFilters({ ".level" });
// im_file_dialog.Open();
// }
// if (ImGui::Button("Clear Level", button_size)) {
// entity_manager.clear();
// asset_manager.clear();
// }
// }
// if (ImGui::CollapsingHeader("Meshes", ImGuiTreeNodeFlags_DefaultOpen)){
// auto &mmap = asset_manager.handle_mesh_map;
// if (mmap.size() > 0){
// if(s_mesh == nullptr) s_mesh = &mmap.begin()->second;

// ImGui::SetNextItemWidth(ImGui::GetWindowWidth() - pad);
// if (ImGui::BeginCombo("##asset-combo", s_mesh->handle.c_str())){
// for(auto &a : mmap){
// bool is_selected = (s_mesh == &a.second);
// if (ImGui::Selectable(a.first.c_str(), is_selected))
// s_mesh = &a.second;
// if (is_selected)
// ImGui::SetItemDefaultFocus();
// }
// ImGui::EndCombo();
// }

// if(s_mesh != nullptr) {
// if(ImGui::Button("Add Instance", button_size)){
// auto e = new MeshEntity();
// e->mesh = s_mesh;
// entity_manager.setEntity(entity_manager.getFreeId().i, e);
// }
// if(ImGui::Button("Export Mesh", button_size)){
// im_file_dialog_type = "exportMesh";
// im_file_dialog.SetPwd(exepath+"/data/models");
// im_file_dialog.SetCurrentTypeFilterIndex(1);
// im_file_dialog.SetTypeFilters({ ".mesh" });
// im_file_dialog.Open();
// }
// }
// }
// if(ImGui::Button("Load Mesh", button_size)){
// im_file_dialog_type = "loadMesh";
// im_file_dialog.SetPwd(exepath+"/data/models");
// im_file_dialog.SetCurrentTypeFilterIndex(1);
// im_file_dialog.SetTypeFilters({ ".mesh" });
// im_file_dialog.Open();
// }
// if(ImGui::Button("Load Model (Assimp)", button_size)){
// im_file_dialog_type = "loadModelAssimp";
// im_file_dialog.SetPwd(exepath+"/data/models");
// im_file_dialog.SetCurrentTypeFilterIndex(3);
// im_file_dialog.SetTypeFilters({ ".obj", ".fbx" });
// im_file_dialog.Open();
// }
// }
// if (ImGui::CollapsingHeader("Graphics", ImGuiTreeNodeFlags_DefaultOpen)){
// if (ImGui::Checkbox("Bloom", &graphics::do_bloom)){
// initHdrFbo();
// initBloomFbo();
// }

// ImGui::Text("Sun Color:");
// ImGui::SetNextItemWidth(win_width);
// ImGui::ColorEdit3("", (float*)&sun_color); // Edit 3 floats representing a color
// ImGui::Text("Sun Direction:");
// ImGui::SetNextItemWidth(win_width);
// if(ImGui::InputFloat3("", (float*)&sun_direction)){
// sun_direction = glm::normalize(sun_direction);
// // Casts shadows from sun direction
// updateShadowVP(camera);
// }
// }
// ImGui::End();
//}

drawInfoTextGui();

ImTerminal(entity_manager, asset_manager, do_terminal);

// Handle imfile dialog browser
Expand Down
4 changes: 2 additions & 2 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ void drawEntitiesHdr(const EntityManager& entity_manager, const Texture* skybox,

auto g_model_rot_scl = glm::mat4_cast(m_e->rotation) * glm::mat4x4(m_e->scale);
auto g_model_pos = glm::translate(glm::mat4x4(1.0), m_e->position);
drawMeshMat(*unified_s, m_e->mesh, g_model_rot_scl, g_model_pos, vp, m_e->lightmap);
drawMeshMat(*unified_s, m_e->mesh, g_model_rot_scl, g_model_pos, vp, m_e->do_lightmap ? m_e->lightmap: nullptr);
}

//
Expand Down Expand Up @@ -1461,7 +1461,7 @@ Texture *createJitter3DTexture(AssetManager &asset_manager, int size, int sample
}
}

glTexImage3D(GL_TEXTURE_3D, 0, GL_SIGNED_RGBA_NV, size, size, samples_u * samples_v / 2, 0, GL_RGBA, GL_BYTE, data);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, size, size, samples_u * samples_v / 2, 0, GL_RGBA, GL_BYTE, data);

delete[] data;

Expand Down
28 changes: 27 additions & 1 deletion src/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ static void writeMeshEntity(MeshEntity* e, std::unordered_map<uint64_t, uint64_t
fwrite(&e->ao_mult , sizeof(e->ao_mult ), 1, f);

fwrite(&e->casts_shadow, sizeof(e->casts_shadow), 1, f);

fwrite(&e->do_lightmap, sizeof(e->do_lightmap), 1, f);
uint8_t lightmap_calculated = e->lightmap != nullptr;
fwrite(&lightmap_calculated, sizeof(lightmap_calculated), 1, f);

if (lightmap_calculated) {
lookup = asset_lookup[reinterpret_cast<uint64_t>(e->lightmap)];
fwrite(&lookup, sizeof(lookup), 1, f);
}
}
static void readMeshEntity(MeshEntity* e, const std::unordered_map<uint64_t, void*> index_to_asset, FILE *f) {
fread(&e->position, sizeof(e->position), 1, f);
Expand All @@ -54,6 +63,21 @@ static void readMeshEntity(MeshEntity* e, const std::unordered_map<uint64_t, voi
fread(&e->ao_mult , sizeof(e->ao_mult ), 1, f);

fread(&e->casts_shadow, sizeof(e->casts_shadow), 1, f);

fread(&e->do_lightmap, sizeof(e->do_lightmap), 1, f);
uint8_t lightmap_calculated;
fread(&lightmap_calculated, sizeof(lightmap_calculated), 1, f);

if (lightmap_calculated) {
uint64_t lookup;
fread(&lookup, sizeof(lookup), 1, f);
auto lu = index_to_asset.find(lookup);
if (lu != index_to_asset.end()) {
e->lightmap = (Texture*)lu->second;
} else {
std::cerr << "Unknown lightmap texture index " << lookup << " when reading mesh entity\n";
}
}
}

static void writeAnimatedMeshEntity(AnimatedMeshEntity* e, std::unordered_map<uint64_t, uint64_t> asset_lookup, FILE *f) {
Expand Down Expand Up @@ -192,8 +216,10 @@ void saveLevel(EntityManager & entity_manager, const std::string & level_path, c

if(entityInherits(e->type, MESH_ENTITY)) {
auto me = (MeshEntity*)e;
if(me->mesh != nullptr)
if (me->mesh != nullptr)
used_meshes.emplace(me->mesh);
if(me->lightmap != nullptr)
used_textures.emplace(me->lightmap);
}
if(entityInherits(e->type, ANIMATED_MESH_ENTITY)) {
auto ae = (AnimatedMeshEntity*)e;
Expand Down
Loading

0 comments on commit a7829be

Please sign in to comment.