diff --git a/assets/models/kettle.json b/assets/models/kettle.json index 9f16256..3b1c7f7 100644 --- a/assets/models/kettle.json +++ b/assets/models/kettle.json @@ -1,4 +1,7 @@ { "mesh_file": "kettle.fbx", - "primitive_type": 2147483649 + "primitive_type": 2147483649, + "modifiers": [ + "gamma_correct_alpha" + ] } \ No newline at end of file diff --git a/assets/models/plane.json b/assets/models/plane.json index f658854..f24387e 100644 --- a/assets/models/plane.json +++ b/assets/models/plane.json @@ -1,4 +1,7 @@ { "mesh_file": "plane.fbx", - "primitive_type": 2147483649 + "primitive_type": 2147483649, + "modifiers": [ + "gamma_correct_alpha" + ] } \ No newline at end of file diff --git a/assets/models/plant.fbx b/assets/models/plant.fbx index 6c7fd36..a1bc8ef 100644 Binary files a/assets/models/plant.fbx and b/assets/models/plant.fbx differ diff --git a/assets/models/plant.json b/assets/models/plant.json index 3a8cf17..fc9771c 100644 --- a/assets/models/plant.json +++ b/assets/models/plant.json @@ -1,4 +1,8 @@ { "mesh_file": "plant.fbx", - "primitive_type": 2147483650 + "primitive_type": 2147483650, + "modifiers": [ + "vegetation", + "gamma_correct_alpha" + ] } \ No newline at end of file diff --git a/assets/models/testmap.json b/assets/models/testmap.json index b846c2b..d892059 100644 --- a/assets/models/testmap.json +++ b/assets/models/testmap.json @@ -1,4 +1,7 @@ { "mesh_file": "testmap.fbx", - "primitive_type": 2147483649 + "primitive_type": 2147483649, + "modifiers": [ + "gamma_correct_alpha" + ] } \ No newline at end of file diff --git a/src/renderer/ps2gl_renderers/vertex_color_renderer.cc b/src/renderer/ps2gl_renderers/vertex_color_renderer.cc index 97cd204..097929c 100644 --- a/src/renderer/ps2gl_renderers/vertex_color_renderer.cc +++ b/src/renderer/ps2gl_renderers/vertex_color_renderer.cc @@ -79,8 +79,8 @@ void CVertexColorRenderer::InitContext(GLenum primType, tU32 rcChanges, bool use packet.Pad96(); - packet.OpenUnpack(Vifs::UnpackModes::s_32, kTime, Packet::kSingleBuff); - packet += 0.f; + packet.OpenUnpack(Vifs::UnpackModes::s_32, kIsVegetation, Packet::kSingleBuff); + packet += (s32)0; packet.CloseUnpack(1); packet.Mscal(0); diff --git a/src/renderer/ps2gl_renderers/vertex_color_vegetation_renderer.cc b/src/renderer/ps2gl_renderers/vertex_color_vegetation_renderer.cc index ab5203c..47e54c5 100644 --- a/src/renderer/ps2gl_renderers/vertex_color_vegetation_renderer.cc +++ b/src/renderer/ps2gl_renderers/vertex_color_vegetation_renderer.cc @@ -53,6 +53,11 @@ CVertexColorVegetationRenderer* CVertexColorVegetationRenderer::Register() return renderer; } +static float get_veg_p(float offset) +{ + return sinf(Engine::get_game_time() + offset); +} + void CVertexColorVegetationRenderer::InitContext(GLenum primType, tU32 rcChanges, bool userRcChanged) { primType = GL_TRIANGLE_STRIP; @@ -79,10 +84,19 @@ void CVertexColorVegetationRenderer::InitContext(GLenum primType, tU32 rcChanges packet.Pad96(); - packet.OpenUnpack(Vifs::UnpackModes::s_32, kTime, Packet::kSingleBuff); - packet += (float)(fmodf(Engine::get_game_time(), (M_PI * 2)) - M_PI); + packet.OpenUnpack(Vifs::UnpackModes::s_32, kIsVegetation, Packet::kSingleBuff); + packet += (s32)1; packet.CloseUnpack(1); + packet.OpenUnpack(Vifs::UnpackModes::s_32, kVegetationParams, Packet::kSingleBuff); + packet += get_veg_p(0.f); + packet += get_veg_p(M_PI / 6.f); + packet += get_veg_p((2.f * M_PI) / 6.f); + packet += get_veg_p((3.f * M_PI) / 6.f); + packet += get_veg_p((4.f * M_PI) / 6.f); + packet += get_veg_p((5.f * M_PI) / 6.f); + packet.CloseUnpack(6); + packet.Mscal(0); packet.Flushe(); diff --git a/tools/ps2-mesh-converter/include/model_modifiers.hpp b/tools/ps2-mesh-converter/include/model_modifiers.hpp index 2bff203..f0a7b43 100644 --- a/tools/ps2-mesh-converter/include/model_modifiers.hpp +++ b/tools/ps2-mesh-converter/include/model_modifiers.hpp @@ -3,4 +3,4 @@ #include #include "types.hpp" -void apply_gamma_correction(std::vector& meshes); \ No newline at end of file +bool apply_modification(const std::string& mod, Mesh& mesh); \ No newline at end of file diff --git a/tools/ps2-mesh-converter/include/types.hpp b/tools/ps2-mesh-converter/include/types.hpp index cf20279..62dbe94 100644 --- a/tools/ps2-mesh-converter/include/types.hpp +++ b/tools/ps2-mesh-converter/include/types.hpp @@ -17,6 +17,8 @@ struct Mesh uint32_t primitive_type; std::vector vertices; std::vector indices; + + std::vector modifiers; }; static bool isMeshValid(const Mesh& mesh) diff --git a/tools/ps2-mesh-converter/src/json_importer.cpp b/tools/ps2-mesh-converter/src/json_importer.cpp index d90a2c4..276b3c4 100644 --- a/tools/ps2-mesh-converter/src/json_importer.cpp +++ b/tools/ps2-mesh-converter/src/json_importer.cpp @@ -49,5 +49,14 @@ bool parseJson(std::string_view path, std::vector& meshes) meshes[0].primitive_type = obj["primitive_type"].asUInt(); } + meshes[0].modifiers.clear(); + if (obj.isMember("modifiers")) + { + for (auto val : obj["modifiers"]) + { + meshes[0].modifiers.push_back(val.asString()); + } + } + return true; } \ No newline at end of file diff --git a/tools/ps2-mesh-converter/src/main.cpp b/tools/ps2-mesh-converter/src/main.cpp index 11fe64a..2b58730 100644 --- a/tools/ps2-mesh-converter/src/main.cpp +++ b/tools/ps2-mesh-converter/src/main.cpp @@ -132,13 +132,13 @@ static void process() printf("Loaded %lu meshes from file\n", meshes.size()); - printf(ANSI_COLOR_MAGENTA "[PS2-Mesh-Converter]: Applying modifications\n" ANSI_COLOR_RESET); - apply_gamma_correction(meshes); - std::vector strips; for (size_t i = 0; i < meshes.size(); ++i) { - //printf("Mesh %lu: Num verts (before stripping): %lu, num tris: %lu\n", i, meshes[i].vertices.size(), meshes[i].indices.size() / 3); + for (const std::string& modifier : meshes[i].modifiers) + { + apply_modification(modifier, meshes[i]); + } // Stripify it std::vector strip = stripify(meshes[i], false, 'S'); diff --git a/tools/ps2-mesh-converter/src/model_modifiers.cpp b/tools/ps2-mesh-converter/src/model_modifiers.cpp index 3a1ce19..9799334 100644 --- a/tools/ps2-mesh-converter/src/model_modifiers.cpp +++ b/tools/ps2-mesh-converter/src/model_modifiers.cpp @@ -1,15 +1,44 @@ #include "model_modifiers.hpp" +#include "utils.hpp" -void apply_gamma_correction(std::vector& meshes) +#include + +static void apply_gamma_correction(Mesh& mesh) +{ + for (Vertex& vertex : mesh.vertices) + { + //vertex.r = pow(vertex.r, 1.0 / 2.2); + //vertex.g = pow(vertex.g, 1.0 / 2.2); + //vertex.b = pow(vertex.b, 1.0 / 2.2); + vertex.a = pow(vertex.a, 1.0 / 2.2); + } +} + +// Converts the red channel to an integer +static void apply_vegetation_mod(Mesh& mesh) +{ + for (Vertex& vertex : mesh.vertices) + { + int32_t red_value = (int)std::round(vertex.r * 255.f); + vertex.r = *(reinterpret_cast(&red_value)); + } +} + +bool apply_modification(const std::string& mod, Mesh& mesh) { - for (Mesh& mesh : meshes) + if (mod == "vegetation") { - for (Vertex& vertex : mesh.vertices) - { - //vertex.r = pow(vertex.r, 1.0 / 2.2); - //vertex.g = pow(vertex.g, 1.0 / 2.2); - //vertex.b = pow(vertex.b, 1.0 / 2.2); - vertex.a = pow(vertex.a, 1.0 / 2.2); - } + printf(ANSI_COLOR_MAGENTA "[PS2-Mesh-Converter]: Applying mesh modification: %s\n" ANSI_COLOR_RESET, "vegetation"); + apply_vegetation_mod(mesh); + return true; } + else if (mod == "gamma_correct_alpha") + { + printf(ANSI_COLOR_MAGENTA "[PS2-Mesh-Converter]: Applying mesh modification: %s\n" ANSI_COLOR_RESET, "gamma correction"); + apply_gamma_correction(mesh); + return true; + } + + printf(ANSI_COLOR_RED "[PS2-Mesh-Converter]: Unable to find mesh mod: %s\n" ANSI_COLOR_RESET, mod.c_str()); + return false; } \ No newline at end of file diff --git a/vu1/vertex_color_renderer.vcl b/vu1/vertex_color_renderer.vcl index f748dba..6d01eec 100644 --- a/vu1/vertex_color_renderer.vcl +++ b/vu1/vertex_color_renderer.vcl @@ -45,9 +45,6 @@ main_loop_lid: set_strip_adcs - ; const_color = material emissive + global ambient - get_cnst_color const_color - init_bfc_strip xform_loop_lid: --LoopCS 1,3 @@ -57,7 +54,7 @@ xform_loop_lid: --LoopCS 1,3 ; Vert colors load_pvcolor vert_color - move.xyzw vert_color_old, vert_color + move.xyzw vert_color_old, vert_color ;move.xyzw vert_color, vf00 ;maxw.xyzw vert_color, vf00, vert_color_old[w] ; w is AO @@ -67,24 +64,24 @@ xform_loop_lid: --LoopCS 1,3 muli.xyzw vert_color, vert_color, i store_rgba vert_color - ; Wind - move.xyzw time, vf00 - lq.xyzw time, kTime(vi00) + ; Skip the vegetation stuff if this isn't rendering vegetation + ilw.x is_vegetation, kIsVegetation(vi00) + ibeq is_vegetation, vi00, project_lid - ; Add the time offset from the vert color - ; and calculate the sin value - loi 3.14159 - muli.x vert_color_old, vert_color_old, i + ; Wind + ; Lookup the leaf index from the vertex color + ilw.x leaf_index, 3(next_input) - ; Add a per-leaf time offset from the red channel - addx.xyzw time, time, vert_color_old[x] - esin p, time.y - mfp.xyzw time, p + ; Lookup the sine(time) from leaf index + move.xyzw time, vf00 + lq.xyzw time, kVegetationParams(leaf_index) ; Modulate leaf wind by the blue channel of the vertex color mulz.xyzw time, time, vert_color_old[z] add.y vert, vert, time +project_lid: + xform_vert xformed_vert, vert_xform, vert vert_to_gs gs_vert, xformed_vert diff --git a/vu1/vertex_color_renderer.vo b/vu1/vertex_color_renderer.vo index c84644e..1a66401 100644 Binary files a/vu1/vertex_color_renderer.vo and b/vu1/vertex_color_renderer.vo differ diff --git a/vu1/vertex_color_renderer_context.h b/vu1/vertex_color_renderer_context.h index a7a64d1..ef0d0a8 100644 --- a/vu1/vertex_color_renderer_context.h +++ b/vu1/vertex_color_renderer_context.h @@ -65,6 +65,8 @@ typedef struct #define kClipInfo (kGifTag + 1) -#define kTime (kClipInfo + 1) +#define kIsVegetation (kClipInfo + 1) -#define kContextLength (kTime - kContextStart + 1) +#define kVegetationParams (kIsVegetation + 1) + +#define kContextLength (kVegetationParams - kContextStart + 6)