Skip to content

Commit

Permalink
[tie] add wind effect (#1046)
Browse files Browse the repository at this point in the history
* wip tie wind stuff

* wind

* clang
  • Loading branch information
water111 authored Jan 3, 2022
1 parent c9204f2 commit b999422
Show file tree
Hide file tree
Showing 10 changed files with 647 additions and 59 deletions.
32 changes: 32 additions & 0 deletions common/custom_data/TFrag3Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ void StripDraw::serialize(Serializer& ser) {
ser.from_ptr(&num_triangles);
}

void InstancedStripDraw::serialize(Serializer& ser) {
ser.from_ptr(&mode);
ser.from_ptr(&tree_tex_id);
ser.from_pod_vector(&vertex_index_stream);
ser.from_pod_vector(&instance_groups);
ser.from_ptr(&num_triangles);
}

void TieWindInstance::serialize(Serializer& ser) {
ser.from_ptr(&matrix);
ser.from_ptr(&wind_idx);
ser.from_ptr(&stiffness);
}

void TfragTree::serialize(Serializer& ser) {
ser.from_ptr(&kind);

Expand Down Expand Up @@ -38,6 +52,24 @@ void TieTree::serialize(Serializer& ser) {
draw.serialize(ser);
}

if (ser.is_saving()) {
ser.save<size_t>(instanced_wind_draws.size());
} else {
instanced_wind_draws.resize(ser.load<size_t>());
}
for (auto& draw : instanced_wind_draws) {
draw.serialize(ser);
}

if (ser.is_saving()) {
ser.save<size_t>(instance_info.size());
} else {
instance_info.resize(ser.load<size_t>());
}
for (auto& inst : instance_info) {
inst.serialize(ser);
}

ser.from_pod_vector(&vertices);
ser.from_pod_vector(&colors);
bvh.serialize(ser);
Expand Down
34 changes: 32 additions & 2 deletions common/custom_data/Tfrag3Data.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

// Data format for the tfrag3 renderer.
#include <array>

#include "common/common_types.h"
#include "common/dma/gs.h"
Expand All @@ -10,7 +11,7 @@

namespace tfrag3 {

constexpr int TFRAG3_VERSION = 7;
constexpr int TFRAG3_VERSION = 8;

// These vertices should be uploaded to the GPU at load time and don't change
struct PreloadedVertex {
Expand Down Expand Up @@ -51,6 +52,27 @@ struct StripDraw {
void serialize(Serializer& ser);
};

struct InstancedStripDraw {
DrawMode mode; // the OpenGL draw settings.
u32 tree_tex_id = 0; // the texture that should be bound for the draw

// the list of vertices in the draw. This includes the restart code of UINT32_MAX that OpenGL
// will use to start a new strip.
std::vector<u32> vertex_index_stream;

// the vertex stream above is segmented by instance.
struct InstanceGroup {
u32 num = 0; // number of vertex indices in this group
u32 instance_idx = 0; // the instance they belong to
u32 vis_idx = 0;
};
std::vector<InstanceGroup> instance_groups;

// for debug counting.
u32 num_triangles = 0;
void serialize(Serializer& ser);
};

// node in the BVH.
struct VisNode {
math::Vector<float, 4> bsphere; // the bounding sphere, in meters (4096 = 1 game meter). w = rad
Expand Down Expand Up @@ -114,14 +136,22 @@ struct TfragTree {
void serialize(Serializer& ser);
};

struct TieWindInstance {
std::array<math::Vector4f, 4> matrix;
u16 wind_idx;
float stiffness;
void serialize(Serializer& ser);
};

// A tie model
struct TieTree {
BVH bvh;
std::vector<StripDraw> static_draws; // the actual topology and settings
std::vector<PreloadedVertex> vertices; // mesh vertices
std::vector<TimeOfDayColor> colors; // vertex colors (pre-interpolation)

// TODO wind stuff
std::vector<InstancedStripDraw> instanced_wind_draws;
std::vector<TieWindInstance> instance_info;

void serialize(Serializer& ser);
};
Expand Down
2 changes: 1 addition & 1 deletion decompiler/level_extractor/BspHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ void ProxyPrototypeArrayTie::read_from_file(TypedRef ref,
prototype_array_tie.read_from_file(
get_and_check_ref_to_basic(ref, "prototype-array-tie", "prototype-array-tie", dts), dts,
stats);
// TODO wind
wind_vectors = deref_label(get_field_ref(ref, "wind-vectors", dts));
}

std::string ProxyPrototypeArrayTie::print(const PrintSettings& settings, int indent) const {
Expand Down
2 changes: 1 addition & 1 deletion decompiler/level_extractor/BspHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ struct ProxyPrototypeArrayTie {
std::string print(const PrintSettings& settings, int indent) const;

PrototypeArrayTie prototype_array_tie;
// todo wind vectors.
Ref wind_vectors;
};

struct DrawableTreeInstanceTie : public DrawableTree {
Expand Down
14 changes: 14 additions & 0 deletions decompiler/level_extractor/extract_tfrag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,19 @@ void extract_time_of_day(const level_tools::DrawableTreeTfrag* tree, tfrag3::Tfr
}
}

void merge_groups(std::vector<tfrag3::StripDraw::VisGroup>& grps) {
std::vector<tfrag3::StripDraw::VisGroup> result;
result.push_back(grps.at(0));
for (size_t i = 1; i < grps.size(); i++) {
if (grps[i].vis_idx == result.back().vis_idx) {
result.back().num += grps[i].num;
} else {
result.push_back(grps[i]);
}
}
std::swap(result, grps);
}

} // namespace

void extract_tfrag(const level_tools::DrawableTreeTfrag* tree,
Expand Down Expand Up @@ -2190,6 +2203,7 @@ void extract_tfrag(const level_tools::DrawableTreeTfrag* tree,
str.vis_idx = it->second;
}
}
merge_groups(draw.vis_groups);
}
out.tfrag_trees.push_back(this_tree);
}
Expand Down
Loading

0 comments on commit b999422

Please sign in to comment.