Skip to content

Commit

Permalink
feature: Flip images when needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
jochumdev committed Feb 5, 2023
1 parent 0dea5a7 commit 81808e5
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 29 deletions.
10 changes: 6 additions & 4 deletions runscripts/lib/model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ function model.Convert3DOToS3O(input)
end
lfs.chdir(_oldDir)

print("-- Remove 3DO base")
model:Remove3DOBase()
-- print("-- Remove 3DO base")
-- model:Remove3DOBase()
-- print("-- Making all names lowercase")
-- model:AllLowerCaseNames()

print("-- Normalize normals")
model.root:NormalizeNormals();

print("-- Cleanup Model")
model:Cleanup()
-- print("-- Cleanup Model")
-- model:Cleanup()

local _s3oOut = lib.utils.join_paths(_dirname, _fileName .. ".s3o")
local ok = model:SaveS3O(_s3oOut)
Expand Down
6 changes: 4 additions & 2 deletions src/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,10 +661,12 @@ void EditorUI::BrowseForTexture(int textureIndex) {
static std::string fn;
if (FileOpenDlg("Select texture:", ImageFileExt, fn)) {
Texture* tex = new Texture(fn);
if (!tex->IsLoaded())
if (!tex->IsLoaded()) {
delete tex;
else
} else {
tex->image->FlipNonDDS(fn);
SetModelTexture(textureIndex, tex);
}

Update();
}
Expand Down
21 changes: 11 additions & 10 deletions src/FileIO/S3O.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "S3O.h"
#pragma pack(pop)

#include <filesystem>

#define S3O_ID "Spring unit"

static void MirrorX(MdlObject* o) {
Expand Down Expand Up @@ -165,23 +167,25 @@ bool Model::LoadS3O(const char* filename, IProgressCtl& /*progctl*/) {

// load textures
for (int tex = 0; tex < 2; tex++) {
if (!(tex ? header.texture2 : header.texture1)) continue;
if (!(tex == 1 ? header.texture2 : header.texture1)) continue;

texBindings.push_back(TextureBinding());
texBindings.emplace_back();
TextureBinding& tb = texBindings.back();

tb.name = Readstring(tex ? header.texture2 : header.texture1, file);
tb.name = Readstring(tex == 1 ? header.texture2 : header.texture1, file);
tb.texture = new Texture(tb.name, mdlPath);
if (!tb.texture->IsLoaded()) tb.texture = 0;
if (!tb.texture->IsLoaded()) {
tb.texture = nullptr;
continue;
}

tb.texture->image->FlipNonDDS(tb.name);
}

mapping = MAPPING_S3O;

fclose(file);

// Flip UV's on Load and again on Save
FlipUVs();

return true;
}

Expand Down Expand Up @@ -288,9 +292,6 @@ static inline void ApplyOrientationAndScaling(MdlObject* o) {
}

bool Model::SaveS3O(const char* filename, IProgressCtl& /*progctl*/) {
// we have them internal flipped.
FlipUVs();

S3OHeader header;
size_t write_result;
memset(&header, 0, sizeof(S3OHeader));
Expand Down
21 changes: 19 additions & 2 deletions src/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

struct DevILInitializer {
DevILInitializer() {
ilInit();
Expand Down Expand Up @@ -579,6 +577,25 @@ void Image::SetNonAlphaZero() {
}
}

void Image::Mirror() {
auto ilId = ToIL();
iluMirror();
FromIL(ilId);
}

void Image::Flip() {
auto ilId = ToIL();
iluFlipImage();
FromIL(ilId);
}

void Image::FlipNonDDS(const std::string &path) {
auto ext = std::filesystem::path(path).extension();
if (ext != ".dds") {
Flip();
}
}

#define DIV4MASK_32 ((63 << 24) + (63 << 16) + (63 << 8) + 63)
#define DIV4MASK_16 ((31 << 11) + (63 << 5) + 31)

Expand Down
4 changes: 4 additions & 0 deletions src/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ class Image : public Referenced {
void FillAlpha();
void SetAlphaZero();
void SetNonAlphaZero();
void Mirror();
void Flip();
void FlipNonDDS(const std::string &path);

void Convert(Image* dst); // copy this to dst in dst.format
void Blit(Image* dst, int sx, int sy, int dx, int dy, int w, int h);

Expand Down
27 changes: 16 additions & 11 deletions src/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,6 @@ bool Model::ConvertToS3O(std::string textureName, int texw, int texh) {
std::set<Texture*> textures;
std::map<uint, RefPtr<Texture>> coltextures;

// conver to lowecase names
auto objlist = GetObjectList();
for (auto& obj : objlist) {
std::transform(obj->name.begin(), obj->name.end(), obj->name.begin(),
[](unsigned char c) { return std::tolower(c); });
}

std::vector<PolyMesh*> pmlist = GetPolyMeshList();
std::vector<Poly*> polygons = GetElementList(&PolyMesh::poly, pmlist.begin(), pmlist.end());

Expand Down Expand Up @@ -489,12 +482,15 @@ bool Model::ConvertToS3O(std::string textureName, int texw, int texh) {
texToNode[*ti] = node;
}

auto img = tree.GetResult()->Clone();
img->Save((textureName + "1.png").c_str());
auto *img = tree.GetResult()->Clone();
auto saveName = textureName + "1.png";
img->FlipNonDDS(saveName);
img->Save(saveName.c_str());
img->FlipNonDDS(saveName);

auto tex1 = new Texture();
auto *tex1 = new Texture();
tex1->SetImage(img);
tex1->name = textureName + "1.png";
tex1->name = saveName;
SetTexture(0, tex1);

// now set new texture coordinates.
Expand Down Expand Up @@ -650,4 +646,13 @@ void Model::MirrorUVs() {
// vi->tc[1].x = 1.0F - vi->tc[1].x;
}
}
}

void Model::AllLowerCaseNames() {
// convert to lowecase names
auto objlist = GetObjectList();
for (auto& obj : objlist) {
std::transform(obj->name.begin(), obj->name.end(), obj->name.begin(),
[](unsigned char c) { return std::tolower(c); });
}
}
1 change: 1 addition & 0 deletions src/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ struct Model {
void Cleanup();
void FlipUVs();
void MirrorUVs();
void AllLowerCaseNames();

float radius; // radius of collision sphere
float height; // height of whole object
Expand Down

0 comments on commit 81808e5

Please sign in to comment.