Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion game/game/compatibility/cpu32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ void Cpu32::callFunction(ptr32_t func) {

std::string Cpu32::popString() {
if(stack.size()==0)
return 0;
return {};
auto ptr = stack.back();
stack.pop_back();

Expand Down
3 changes: 3 additions & 0 deletions game/graphics/drawbuckets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ bool DrawBuckets::commit(Encoder<CommandBuffer>& cmd, uint8_t fId) {
bx.waveMaxAmplitude = i.mat.waveMaxAmplitude;
bx.alphaWeight = i.mat.alphaWeight;
bx.envMapping = i.mat.envMapping;
bx.g1BarrierLayer = i.mat.g1BarrierLayer;
if(i.staticMesh!=nullptr) {
auto& bbox = i.staticMesh->bbox.bbox;
bx.bboxRadius = i.staticMesh->bbox.rConservative;
Expand All @@ -147,6 +148,8 @@ bool DrawBuckets::commit(Encoder<CommandBuffer>& cmd, uint8_t fId) {
bx.flags |= BK_SOLID;
if(i.mat.alpha==Material::Water)
bx.flags |= BK_WATER;
if(i.mat.isG1Barrier)
bx.flags |= BK_G1_BARRIER;
bucket.push_back(bx);
}

Expand Down
3 changes: 2 additions & 1 deletion game/graphics/drawbuckets.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class DrawBuckets {
BK_SKIN = 0x2,
BK_MORPH = 0x4,
BK_WATER = 0x8,
BK_G1_BARRIER = 0x10,
};

struct BucketGpu final {
Expand All @@ -73,7 +74,7 @@ class DrawBuckets {
float alphaWeight = 1;
float envMapping = 0;
uint32_t flags = 0;
uint32_t padd[1] = {};
uint32_t g1BarrierLayer = 0;
};

struct {
Expand Down
14 changes: 14 additions & 0 deletions game/graphics/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,20 @@ Material::Material(const zenkit::Material& m, bool enableAlphaTest) {
}

loadFrames(m);
if(m.name=="MAGICFRONTIER_BARRIER") {
isG1Barrier = true;
alphaWeight = float(m.color.a)/255.f;
if(m.texture=="BARRIERE.TGA")
g1BarrierLayer = 1;
else if(m.texture.rfind("MAGBA_",0)==0)
g1BarrierLayer = 2;
else if(m.texture.rfind("MAGICFRONTIER_",0)==0)
g1BarrierLayer = 3;
}

alpha = loadAlphaFunc(m.alpha_func,m.group,m.color.a,tex,enableAlphaTest);
if(isG1Barrier && m.alpha_func==zenkit::AlphaFunction::BLEND)
alpha = Transparent;
if(alpha==Water && m.name=="OWODWFALL_WATERFALL_01") {
// NOTE: waterfall heuristics
alpha = Solid;
Expand Down Expand Up @@ -70,6 +82,8 @@ bool Material::operator ==(const Material& other) const {
texAniMapDirPeriod==other.texAniMapDirPeriod &&
texAniFPSInv==other.texAniFPSInv &&
isGhost==other.isGhost &&
isG1Barrier==other.isG1Barrier &&
g1BarrierLayer==other.g1BarrierLayer &&
waveMaxAmplitude==other.waveMaxAmplitude &&
envMapping==other.envMapping;
}
Expand Down
3 changes: 2 additions & 1 deletion game/graphics/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Material final {
Tempest::Point texAniMapDirPeriod;
uint64_t texAniFPSInv = 1;
bool isGhost = false;
bool isG1Barrier = false;
uint32_t g1BarrierLayer = 0;
float waveMaxAmplitude = 0;
float envMapping = 0;

Expand Down Expand Up @@ -59,4 +61,3 @@ class Material final {
void loadFrames(const zenkit::Material& m);
void loadFrames(const std::string_view fr, float fps);
};

6 changes: 5 additions & 1 deletion game/physics/physicmesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "graphics/mesh/pose.h"
#include "graphics/mesh/protomesh.h"

#include <algorithm>

PhysicMesh::PhysicMesh(const ProtoMesh& proto, DynamicWorld& owner, bool movable)
:ani(&proto) {
Tempest::Matrix4x4 pos;
Expand All @@ -20,7 +22,9 @@ PhysicMesh::PhysicMesh(const ProtoMesh& proto, DynamicWorld& owner, bool movable
}

bool PhysicMesh::isEmpty() const {
return sub.empty();
return std::all_of(sub.begin(),sub.end(),[](const DynamicWorld::Item& i) {
return i.isEmpty();
});
}

void PhysicMesh::setObjMatrix(const Tempest::Matrix4x4& obj) {
Expand Down
175 changes: 175 additions & 0 deletions game/resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <Tempest/Color>

#include <zenkit/MultiResolutionMesh.hh>
#include <zenkit/Mesh.hh>
#include <zenkit/ModelHierarchy.hh>
#include <zenkit/Model.hh>
#include <zenkit/ModelScript.hh>
Expand Down Expand Up @@ -51,6 +52,154 @@ static void emplaceTag(char* buf, char tag){
}
}

static bool isG1BarrierMesh(std::string_view name) {
return name=="MAGICFRONTIER_OUT.MSH";
}

static void prepareG1BarrierMesh(zenkit::Mesh& mesh) {
const bool hasBarrierTexture = Resources::vdfsIndex().find("BARRIERE-C.TEX")!=nullptr;
for(auto& mat:mesh.materials) {
mat.name = "MAGICFRONTIER_BARRIER";
mat.alpha_func = zenkit::AlphaFunction::ADD;
mat.disable_collision = false;
mat.ignore_sun = true;
mat.texture_anim_fps = 0;

if(hasBarrierTexture) {
mat.texture = "BARRIERE.TGA";
mat.color = zenkit::Color(255,255,255,20);
} else {
mat.texture.clear();
mat.color = zenkit::Color(60,110,210,20);
}
}
}

static void addG1BarrierLayer(zenkit::Mesh& mesh, const char* assetName, const char* textureName,
zenkit::AlphaFunction alphaFunc, uint8_t alpha, float fps) {
if(mesh.materials.empty() || Resources::vdfsIndex().find(assetName)==nullptr)
return;

const size_t triCount = std::min({mesh.polygons.material_indices.size(),
mesh.polygons.lightmap_indices.size(),
mesh.polygons.flags.size(),
mesh.polygons.vertex_indices.size()/3,
mesh.polygons.feature_indices.size()/3});
if(triCount==0)
return;

zenkit::Material mat = mesh.materials.front();
mat.name = "MAGICFRONTIER_BARRIER";
mat.texture = textureName;
mat.color = zenkit::Color(255,255,255,alpha);
mat.alpha_func = alphaFunc;
mat.texture_anim_fps = fps;
mat.disable_collision = true;
mat.ignore_sun = true;

const uint32_t matId = uint32_t(mesh.materials.size());
mesh.materials.push_back(std::move(mat));

mesh.polygons.material_indices.reserve(mesh.polygons.material_indices.size()+triCount);
mesh.polygons.lightmap_indices.reserve(mesh.polygons.lightmap_indices.size()+triCount);
mesh.polygons.flags.reserve(mesh.polygons.flags.size()+triCount);
mesh.polygons.vertex_indices.reserve(mesh.polygons.vertex_indices.size()+triCount*3);
mesh.polygons.feature_indices.reserve(mesh.polygons.feature_indices.size()+triCount*3);

for(size_t i=0; i<triCount; ++i) {
const size_t v = i*3;
mesh.polygons.vertex_indices.push_back(mesh.polygons.vertex_indices[v + 0]);
mesh.polygons.vertex_indices.push_back(mesh.polygons.vertex_indices[v + 1]);
mesh.polygons.vertex_indices.push_back(mesh.polygons.vertex_indices[v + 2]);
mesh.polygons.feature_indices.push_back(mesh.polygons.feature_indices[v + 0]);
mesh.polygons.feature_indices.push_back(mesh.polygons.feature_indices[v + 1]);
mesh.polygons.feature_indices.push_back(mesh.polygons.feature_indices[v + 2]);
mesh.polygons.material_indices.push_back(matId);
mesh.polygons.lightmap_indices.push_back(mesh.polygons.lightmap_indices[i]);
mesh.polygons.flags.push_back(mesh.polygons.flags[i]);
}
}

static void addG1BarrierLayers(zenkit::Mesh& mesh) {
addG1BarrierLayer(mesh,"MAGBA_A0-C.TEX", "MAGBA_A0.TGA", zenkit::AlphaFunction::ADD, 96,6.f);
addG1BarrierLayer(mesh,"MAGICFRONTIER_A0-C.TEX","MAGICFRONTIER_A0.TGA",zenkit::AlphaFunction::ADD, 80,9.f);
}


static void makeMeshTwoSided(zenkit::Mesh& mesh) {
const size_t triCount = std::min({mesh.polygons.material_indices.size(),
mesh.polygons.lightmap_indices.size(),
mesh.polygons.flags.size(),
mesh.polygons.vertex_indices.size()/3,
mesh.polygons.feature_indices.size()/3});
mesh.polygons.material_indices.reserve(triCount*2);
mesh.polygons.lightmap_indices.reserve(triCount*2);
mesh.polygons.flags.reserve(triCount*2);
mesh.polygons.vertex_indices.reserve(triCount*6);
mesh.polygons.feature_indices.reserve(triCount*6);

for(size_t i=0; i<triCount; ++i) {
const size_t v = i*3;
mesh.polygons.vertex_indices.push_back(mesh.polygons.vertex_indices[v + 0]);
mesh.polygons.vertex_indices.push_back(mesh.polygons.vertex_indices[v + 2]);
mesh.polygons.vertex_indices.push_back(mesh.polygons.vertex_indices[v + 1]);
mesh.polygons.feature_indices.push_back(mesh.polygons.feature_indices[v + 0]);
mesh.polygons.feature_indices.push_back(mesh.polygons.feature_indices[v + 2]);
mesh.polygons.feature_indices.push_back(mesh.polygons.feature_indices[v + 1]);
mesh.polygons.material_indices.push_back(mesh.polygons.material_indices[i]);
mesh.polygons.lightmap_indices.push_back(mesh.polygons.lightmap_indices[i]);
mesh.polygons.flags.push_back(mesh.polygons.flags[i]);
}
}

static void triangulateStandaloneMesh(zenkit::Mesh& mesh) {
if(!mesh.polygons.vertex_indices.empty())
return;

size_t triCount = 0;
for(const auto& poly:mesh.geometry) {
const size_t end = poly.index_offset + poly.index_count;
if(poly.index_count<3 || poly.material>=mesh.materials.size() ||
end>mesh.polygon_vertex_indices.size() || end>mesh.polygon_feature_indices.size())
continue;
triCount += poly.index_count - 2;
}

mesh.polygons.material_indices.clear();
mesh.polygons.lightmap_indices.clear();
mesh.polygons.feature_indices.clear();
mesh.polygons.vertex_indices.clear();
mesh.polygons.flags.clear();

mesh.polygons.material_indices.reserve(triCount);
mesh.polygons.lightmap_indices.reserve(triCount);
mesh.polygons.feature_indices.reserve(triCount*3);
mesh.polygons.vertex_indices.reserve(triCount*3);
mesh.polygons.flags.reserve(triCount);

for(const auto& poly:mesh.geometry) {
const size_t end = poly.index_offset + poly.index_count;
if(poly.index_count<3 || poly.material>=mesh.materials.size() ||
end>mesh.polygon_vertex_indices.size() || end>mesh.polygon_feature_indices.size())
continue;

for(size_t i=2; i<poly.index_count; ++i) {
const size_t root = poly.index_offset;
const size_t a = root + i - 1;
const size_t b = root + i;
mesh.polygons.vertex_indices.push_back(mesh.polygon_vertex_indices[root]);
mesh.polygons.vertex_indices.push_back(mesh.polygon_vertex_indices[a]);
mesh.polygons.vertex_indices.push_back(mesh.polygon_vertex_indices[b]);
mesh.polygons.feature_indices.push_back(mesh.polygon_feature_indices[root]);
mesh.polygons.feature_indices.push_back(mesh.polygon_feature_indices[a]);
mesh.polygons.feature_indices.push_back(mesh.polygon_feature_indices[b]);
mesh.polygons.material_indices.push_back(poly.material);
mesh.polygons.lightmap_indices.push_back(poly.lightmap);
mesh.polygons.flags.push_back(poly.flags);
}
}
}

Resources::Resources(Tempest::Device &device)
: dev(device) {
inst=this;
Expand Down Expand Up @@ -441,6 +590,32 @@ ProtoMesh* Resources::implLoadMesh(std::string_view name) {
}

std::unique_ptr<ProtoMesh> Resources::implLoadMeshMain(std::string name) {
if(FileExt::hasExt(name,"MSH")) {
const auto* entry = Resources::vdfsIndex().find(name);
if(entry == nullptr)
return nullptr;

zenkit::Mesh zmsh;
auto reader = entry->open_read();
zmsh.load(reader.get(),false);
if(isG1BarrierMesh(name))
prepareG1BarrierMesh(zmsh);
triangulateStandaloneMesh(zmsh);
if(isG1BarrierMesh(name))
addG1BarrierLayers(zmsh);
if(isG1BarrierMesh(name))
makeMeshTwoSided(zmsh);

if(zmsh.polygons.vertex_indices.empty())
return nullptr;

PackedMesh packed(zmsh,PackedMesh::PK_Visual);
if(packed.indices.empty() || packed.subMeshes.empty())
return nullptr;

return std::unique_ptr<ProtoMesh>{new ProtoMesh(std::move(packed),name)};
}

if(FileExt::hasExt(name,"3DS")) {
FileExt::exchangeExt(name,"3DS","MRM");

Expand Down
Loading