Skip to content

Commit

Permalink
Changed OBJ exporter so exported models can be easier reimported by R…
Browse files Browse the repository at this point in the history
…BDOOM-3-BFG
  • Loading branch information
RobertBeckebans committed Mar 5, 2022
1 parent 4cf3850 commit 5cb9601
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Here is an overview of the changes made to TrenchBroom:
* Creating new entities like a light will automatically receive unique names like light_2
* Changed Rotation Tool behaviour to use "angles" by default
* Translucent materials that might cause BSP leaks have a purple rectangle border in the Texture Browser
* Changed OBJ exporter so exported models can be easier reimported by RBDOOM-3-BFG

## Issues
* It doesn't allow to create bezier patches at the moment so you won't be able to edit existing Doom 3 maps
Expand Down
19 changes: 12 additions & 7 deletions common/src/IO/ObjSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ std::ostream& operator<<(std::ostream& str, const ObjSerializer::IndexedVertex&
std::ostream& operator<<(std::ostream& str, const ObjSerializer::BrushObject& object) {
str << "o entity" << object.entityNo << "_brush" << object.brushNo << "\n";
for (const auto& face : object.faces) {
str << "usemtl " << face.textureName << "\n";
// RB: add textures/ for Doom 3 because the material name is the interface for the OBJ loader
str << "usemtl textures/" << face.textureName << "\n";
str << "f";
for (const auto& vertex : face.verts) {
str << " " << vertex;
Expand All @@ -58,7 +59,8 @@ std::ostream& operator<<(std::ostream& str, const ObjSerializer::BrushObject& ob

std::ostream& operator<<(std::ostream& str, const ObjSerializer::PatchObject& object) {
str << "o entity" << object.entityNo << "_patch" << object.patchNo << "\n";
str << "usemtl " << object.textureName << "\n";
// RB: add textures/ for Doom 3 because the material name is the interface for the OBJ loader
str << "usemtl textures/" << object.textureName << "\n";
for (const auto& quad : object.quads) {
str << "f";
for (const auto& vertex : quad.verts) {
Expand Down Expand Up @@ -116,7 +118,8 @@ static void writeMtlFile(

const auto basePath = options.exportPath.deleteLastComponent();
for (const auto& [textureName, texture] : usedTextures) {
str << "newmtl " << textureName << "\n";
// RB: add textures/ for Doom 3 because the material name is the interface for the OBJ loader
str << "newmtl textures/" << textureName << "\n";
if (texture) {
switch (options.mtlPathMode) {
case ObjMtlPathMode::RelativeToGamePath:
Expand All @@ -138,9 +141,10 @@ static void writeMtlFile(
static void writeVertices(std::ostream& str, const std::vector<vm::vec3>& vertices) {
str << "# vertices\n";
for (const vm::vec3& elem : vertices) {
// no idea why I have to switch Y and Z
// RB: use raw X, Y, Z without scaling or flipping for Doom 3 so we can reuse the .obj directly
// in the engine
fmt::format_to(
std::ostreambuf_iterator<char>(str), "v {} {} {}\n", elem.x(), elem.z(), -elem.y());
std::ostreambuf_iterator<char>(str), "v {} {} {}\n", elem.x(), elem.y(), elem.z());
}
}

Expand All @@ -156,9 +160,10 @@ static void writeTexCoords(std::ostream& str, const std::vector<vm::vec2f>& texC
static void writeNormals(std::ostream& str, const std::vector<vm::vec3>& normals) {
str << "# normals\n";
for (const vm::vec3& elem : normals) {
// no idea why I have to switch Y and Z
// RB: use raw X, Y, Z without scaling or flipping for Doom 3 so we can reuse the .obj directly
// in the engine
fmt::format_to(
std::ostreambuf_iterator<char>(str), "vn {} {} {}\n", elem.x(), elem.z(), -elem.y());
std::ostreambuf_iterator<char>(str), "vn {} {} {}\n", elem.x(), elem.y(), elem.z());
}
}

Expand Down

0 comments on commit 5cb9601

Please sign in to comment.