Skip to content

Commit 4c1c3a8

Browse files
Some textures are correct.
1 parent f73d71a commit 4c1c3a8

13 files changed

+236
-126
lines changed

DefaultPixelShader.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ float4 main(PixelInputType input) : SV_TARGET
9090

9191
// Apply textures
9292
float4 sampledTextureColor = float4(0, 0, 0, 0);
93+
float2 texCoordsArray[8] = { input.tex_coords0, input.tex_coords1, input.tex_coords2, input.tex_coords3,
94+
input.tex_coords4, input.tex_coords5, input.tex_coords6, input.tex_coords7 };
95+
9396
for (int j = 0; j < (num_uv_texture_pairs + 3) / 4; ++j)
9497
{
9598
for (int k = 0; k < 4; ++k)
@@ -102,26 +105,22 @@ float4 main(PixelInputType input) : SV_TARGET
102105
break;
103106
}
104107

105-
switch (uv_set_index)
108+
for (int t = 0; t < 8; ++t)
106109
{
107-
case 0: sampledTextureColor = shaderTextures[0].Sample(ss, input.tex_coords0); break;
108-
case 1: sampledTextureColor = shaderTextures[1].Sample(ss, input.tex_coords1); break;
109-
case 2: sampledTextureColor = shaderTextures[2].Sample(ss, input.tex_coords2); break;
110-
case 3: sampledTextureColor = shaderTextures[3].Sample(ss, input.tex_coords3); break;
111-
case 4: sampledTextureColor = shaderTextures[4].Sample(ss, input.tex_coords4); break;
112-
case 5: sampledTextureColor = shaderTextures[5].Sample(ss, input.tex_coords5); break;
113-
case 6: sampledTextureColor = shaderTextures[6].Sample(ss, input.tex_coords6); break;
114-
case 7: sampledTextureColor = shaderTextures[7].Sample(ss, input.tex_coords7); break;
115-
default: break;
110+
if (t == texture_index)
111+
{
112+
sampledTextureColor = shaderTextures[t].Sample(ss, texCoordsArray[uv_set_index]);
113+
sampledTextureColor += sampledTextureColor * (1.0 / num_uv_texture_pairs); // placeholder
114+
}
116115
}
117-
118-
sampledTextureColor += sampledTextureColor * (1.0 / num_uv_texture_pairs); // placeholder
119116
}
120117
}
121118

119+
120+
122121
// Multiply the blended color with the finalColor
123122
if (num_uv_texture_pairs > 0) {
124-
float4 finalColor = finalColor * sampledTextureColor;
123+
finalColor = finalColor * sampledTextureColor;
125124
}
126125

127126
// Return the result

FFNA_ModelFile.h

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ struct ComplexStruct
148148
{
149149
parsed_correctly = false;
150150
return;
151-
return;
152151
}
153152

154153
u0x0 = *reinterpret_cast<const uint32_t*>(data + curr_offset);
@@ -267,7 +266,6 @@ struct GeometryModel
267266
{
268267
parsed_correctly = false;
269268
return;
270-
return;
271269
}
272270

273271
std::memcpy(&unknown, &data[curr_offset], sizeof(unknown));
@@ -463,7 +461,6 @@ struct InteractiveModelMaybe
463461
{
464462
parsed_correctly = false;
465463
return;
466-
return;
467464
}
468465

469466
std::memcpy(&num_indices, &data[curr_offset], sizeof(num_indices));
@@ -496,7 +493,6 @@ struct InteractiveModelMaybe
496493
{
497494
parsed_correctly = false;
498495
return;
499-
return;
500496
}
501497
}
502498
};
@@ -520,14 +516,15 @@ struct TextureAndVertexShader
520516
std::vector<uint8_t> zeros;
521517
std::vector<uint8_t> some_pixel_shader_flags_maybe;
522518
std::vector<uint8_t> texture_index_UV_mapping_maybe;
519+
std::vector<uint8_t> unknown;
523520

524521
TextureAndVertexShader() = default;
525522
TextureAndVertexShader(size_t max_UV_index, size_t num1, bool f0x20, uint32_t& curr_offset,
526523
const unsigned char* data, uint32_t data_size_bytes, bool& parsed_correctly)
527524
{
528-
if (max_UV_index > 40 || num1 > 40)
525+
if (max_UV_index > 100 || num1 > 100)
529526
{
530-
parsed_correctly == false;
527+
parsed_correctly = false;
531528
return;
532529
}
533530
// Resize vectors
@@ -591,6 +588,19 @@ struct TextureAndVertexShader
591588
}
592589
std::memcpy(texture_index_UV_mapping_maybe.data(), &data[curr_offset], sizeof(uint8_t) * num1);
593590
curr_offset += sizeof(uint8_t) * num1;
591+
592+
if (-(f0x20 != 0))
593+
{
594+
if (curr_offset + sizeof(uint8_t) * num1 > data_size_bytes)
595+
{
596+
parsed_correctly = false;
597+
return;
598+
}
599+
600+
unknown.resize(num1);
601+
std::memcpy(unknown.data(), &data[curr_offset], sizeof(uint8_t) * num1);
602+
curr_offset += sizeof(uint8_t) * num1;
603+
}
594604
}
595605
};
596606

@@ -641,11 +651,18 @@ struct GeometryChunk
641651
curr_offset += sizeof(sub_1);
642652
if (sub_1.num_models > 0)
643653
{
654+
const bool prev_parsed_correctly = parsed_correctly;
655+
const int prev_offset = curr_offset;
644656
tex_and_vertex_shader_struct =
645657
TextureAndVertexShader(sub_1.max_UV_index, sub_1.some_num1, sub_1.f0x20, curr_offset, data,
646658
data_size_bytes, parsed_correctly);
647-
if (! parsed_correctly)
648-
return;
659+
if (prev_parsed_correctly != parsed_correctly)
660+
{
661+
// We want to render the models even if we can't apply textures.
662+
parsed_correctly == true;
663+
curr_offset = prev_offset + sub_1.max_UV_index * 3 + sub_1.some_num1 * 9 +
664+
(-(sub_1.f0x20 != 0) & sub_1.some_num1);
665+
}
649666

650667
if (sub_1.f0x19 > 0)
651668
{
@@ -889,8 +906,32 @@ struct FFNA_ModelFile
889906

890907
auto sub_model = geometry_chunk.models[model_index];
891908

909+
bool parsed_texture = sizeof(geometry_chunk.tex_and_vertex_shader_struct) > 0 &&
910+
geometry_chunk.tex_and_vertex_shader_struct.uts0.size() > 0 &&
911+
geometry_chunk.tex_and_vertex_shader_struct.tex_array.size() > 0 &&
912+
geometry_chunk.tex_and_vertex_shader_struct.texture_index_UV_mapping_maybe.size() > 0;
892913
int max_num_tex_coords = 0;
893914

915+
int num_uv_coords_start_index = 0;
916+
if (parsed_texture)
917+
{
918+
for (int i = 0; i < model_index; i++)
919+
{
920+
auto uts = geometry_chunk.tex_and_vertex_shader_struct
921+
.uts0[i % geometry_chunk.tex_and_vertex_shader_struct.uts0.size()];
922+
num_uv_coords_start_index += uts.f0x7;
923+
}
924+
}
925+
926+
int num_uv_coords_to_use = 0;
927+
if (parsed_texture)
928+
{
929+
num_uv_coords_to_use =
930+
geometry_chunk.tex_and_vertex_shader_struct
931+
.uts0[model_index % geometry_chunk.tex_and_vertex_shader_struct.uts0.size()]
932+
.f0x7;
933+
}
934+
894935
for (int i = 0; i < sub_model.vertices.size(); i++)
895936
{
896937
ModelVertex model_vertex = sub_model.vertices[i];
@@ -958,27 +999,37 @@ struct FFNA_ModelFile
958999
indices.push_back(index2);
9591000
}
9601001

961-
for (int i = 0; i < geometry_chunk.tex_and_vertex_shader_struct.tex_array.size(); i++)
1002+
std::vector<uint8_t> uv_coords_indices;
1003+
std::vector<uint8_t> tex_indices;
1004+
if (parsed_texture)
9621005
{
963-
uint8_t& uv_set_index = geometry_chunk.tex_and_vertex_shader_struct.tex_array[i];
964-
if (max_num_tex_coords < uv_set_index && max_num_tex_coords > 0)
1006+
for (int i = num_uv_coords_start_index; i < num_uv_coords_start_index + num_uv_coords_to_use; i++)
9651007
{
966-
uv_set_index = max_num_tex_coords - 1;
1008+
uint8_t uv_set_index =
1009+
geometry_chunk.tex_and_vertex_shader_struct
1010+
.tex_array[i % geometry_chunk.tex_and_vertex_shader_struct.tex_array.size()];
1011+
if (max_num_tex_coords < uv_set_index && max_num_tex_coords > 0)
1012+
{
1013+
uv_set_index = max_num_tex_coords - 1;
1014+
}
1015+
uv_coords_indices.push_back(uv_set_index);
9671016
}
968-
}
9691017

970-
for (int i = 0; i < geometry_chunk.tex_and_vertex_shader_struct.texture_index_UV_mapping_maybe.size();
971-
i++)
972-
{
973-
uint8_t& texture_index =
974-
geometry_chunk.tex_and_vertex_shader_struct.texture_index_UV_mapping_maybe[i];
975-
if (texture_filenames_chunk.num_texture_filenames < texture_index &&
976-
texture_filenames_chunk.num_texture_filenames > 0)
1018+
for (int i = num_uv_coords_start_index; i < num_uv_coords_start_index + num_uv_coords_to_use; i++)
9771019
{
978-
texture_index = texture_filenames_chunk.num_texture_filenames - 1;
1020+
uint8_t texture_index =
1021+
geometry_chunk.tex_and_vertex_shader_struct.texture_index_UV_mapping_maybe
1022+
[i % geometry_chunk.tex_and_vertex_shader_struct.texture_index_UV_mapping_maybe.size()];
1023+
if (texture_filenames_chunk.num_texture_filenames < texture_index &&
1024+
texture_filenames_chunk.num_texture_filenames > 0)
1025+
{
1026+
texture_index = texture_filenames_chunk.num_texture_filenames - 1;
1027+
}
1028+
tex_indices.push_back(texture_index);
9791029
}
9801030
}
9811031

982-
return Mesh(vertices, indices, texture_filenames_chunk.num_texture_filenames);
1032+
return Mesh(vertices, indices, uv_coords_indices, tex_indices,
1033+
texture_filenames_chunk.num_texture_filenames);
9831034
}
9841035
};

SourceFiles/Box.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class Box : public MeshInstance
8585
// Bottom face
8686
21, 20, 23, 21, 23, 22};
8787

88-
return Mesh(vertices, indices, 1);
88+
return Mesh(vertices, indices);
8989
}
9090

9191
std::unique_ptr<MeshInstance> m_meshInstance;

SourceFiles/DefaultPixelShader.hlsl

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ float4 main(PixelInputType input) : SV_TARGET
8585

8686
// Apply textures
8787
float4 sampledTextureColor = float4(0, 0, 0, 0);
88+
float2 texCoordsArray[8] = { input.tex_coords0, input.tex_coords1, input.tex_coords2, input.tex_coords3,
89+
input.tex_coords4, input.tex_coords5, input.tex_coords6, input.tex_coords7 };
90+
8891
for (int j = 0; j < (num_uv_texture_pairs + 3) / 4; ++j)
8992
{
9093
for (int k = 0; k < 4; ++k)
@@ -97,26 +100,22 @@ float4 main(PixelInputType input) : SV_TARGET
97100
break;
98101
}
99102

100-
switch (uv_set_index)
103+
for (int t = 0; t < 8; ++t)
101104
{
102-
case 0: sampledTextureColor = shaderTextures[0].Sample(ss, input.tex_coords0); break;
103-
case 1: sampledTextureColor = shaderTextures[1].Sample(ss, input.tex_coords1); break;
104-
case 2: sampledTextureColor = shaderTextures[2].Sample(ss, input.tex_coords2); break;
105-
case 3: sampledTextureColor = shaderTextures[3].Sample(ss, input.tex_coords3); break;
106-
case 4: sampledTextureColor = shaderTextures[4].Sample(ss, input.tex_coords4); break;
107-
case 5: sampledTextureColor = shaderTextures[5].Sample(ss, input.tex_coords5); break;
108-
case 6: sampledTextureColor = shaderTextures[6].Sample(ss, input.tex_coords6); break;
109-
case 7: sampledTextureColor = shaderTextures[7].Sample(ss, input.tex_coords7); break;
110-
default: break;
105+
if (t == texture_index)
106+
{
107+
sampledTextureColor = shaderTextures[t].Sample(ss, texCoordsArray[uv_set_index]);
108+
sampledTextureColor += sampledTextureColor * (1.0 / num_uv_texture_pairs); // placeholder
109+
}
111110
}
112-
113-
sampledTextureColor += sampledTextureColor * (1.0 / num_uv_texture_pairs); // placeholder
114111
}
115112
}
116113

114+
115+
117116
// Multiply the blended color with the finalColor
118117
if (num_uv_texture_pairs > 0) {
119-
float4 finalColor = finalColor * sampledTextureColor;
118+
finalColor = finalColor * sampledTextureColor;
120119
}
121120

122121
// Return the result

SourceFiles/Line.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Line : public MeshInstance
3636
};
3737
std::vector<uint32_t> indices = {0, 1};
3838

39-
return Mesh(vertices, indices, 1);
39+
return Mesh(vertices, indices);
4040
}
4141

4242
std::unique_ptr<MeshInstance> m_meshInstance;

SourceFiles/MapRenderer.h

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class MapRenderer
5757

5858
// Add a sphere at (0,0,0) in world coordinates. For testing the renderer.
5959
auto box_id = m_mesh_manager->AddBox({300, 300, 300});
60+
//auto box_mesh_instance = m_mesh_manager->GetMesh(box_id);
6061
auto sphere_id = m_mesh_manager->AddSphere(300, 100, 100);
6162

6263
// Move the sphere and box next to eachother
@@ -65,6 +66,7 @@ class MapRenderer
6566
DirectX::XMStoreFloat4x4(&boxWorldMatrix, DirectX::XMMatrixTranslation(30000, 0, 0));
6667
PerObjectCB boxPerObjectData;
6768
boxPerObjectData.world = boxWorldMatrix;
69+
boxPerObjectData.num_uv_texture_pairs = 1;
6870
m_mesh_manager->UpdateMeshPerObjectData(box_id, boxPerObjectData);
6971

7072
// Move the sphere to the right of the box (e.g., 250 units on the X-axis)
@@ -81,7 +83,7 @@ class MapRenderer
8183
auto texture_id =
8284
m_texture_manager->AddTexture((void*)checkerboard_texture.getData().data(), texture_width,
8385
texture_height, DXGI_FORMAT_R8G8B8A8_UNORM);
84-
m_mesh_manager->SetTexturesForMesh(box_id, {m_texture_manager->GetTexture(texture_id)}, {0}, {0});
86+
m_mesh_manager->SetTexturesForMesh(box_id, {m_texture_manager->GetTexture(texture_id)});
8587

8688
// Create and initialize the VertexShader
8789
m_vertex_shader = std::make_unique<VertexShader>(m_device, m_deviceContext);
@@ -178,8 +180,21 @@ class MapRenderer
178180
UnsetTerrain();
179181
}
180182

181-
m_terrain_mesh_id =
182-
m_mesh_manager->AddCustomMesh(terrain->get_mesh(), m_terrain_current_pixel_shader_type);
183+
auto mesh = terrain->get_mesh();
184+
mesh->num_textures = 1;
185+
m_terrain_mesh_id = m_mesh_manager->AddCustomMesh(mesh, m_terrain_current_pixel_shader_type);
186+
187+
PerObjectCB terrainPerObjectData;
188+
terrainPerObjectData.num_uv_texture_pairs = mesh->num_textures;
189+
for (int i = 0; i < mesh->uv_coord_indices.size(); i++)
190+
{
191+
int index0 = i / 4;
192+
int index1 = i % 4;
193+
194+
terrainPerObjectData.uv_indices[index0][index1] = (uint32_t)mesh->uv_coord_indices[i];
195+
terrainPerObjectData.texture_indices[index0][index1] = (uint32_t)mesh->tex_indices[i];
196+
}
197+
m_mesh_manager->UpdateMeshPerObjectData(m_terrain_mesh_id, terrainPerObjectData);
183198

184199
auto water_level = m_terrain ? m_terrain->m_per_terrain_cb.water_level : 0.0f;
185200
terrain->m_per_terrain_cb =
@@ -206,7 +221,7 @@ class MapRenderer
206221
texture_height, DXGI_FORMAT_R8G8B8A8_UNORM);
207222
}
208223
m_mesh_manager->SetTexturesForMesh(m_terrain_mesh_id,
209-
{m_texture_manager->GetTexture(m_terrain_texture_id)}, {0}, {0});
224+
{m_texture_manager->GetTexture(m_terrain_texture_id)});
210225

211226
m_terrain = std::move(terrain);
212227
m_is_terrain_mesh_set = true;
@@ -232,11 +247,15 @@ class MapRenderer
232247
}
233248

234249
// A prop consists of 1+ sub models/meshes.
235-
std::vector<int> AddProp(std::vector<Mesh> meshes, PerObjectCB per_object_cb, uint32_t model_id)
250+
std::vector<int> AddProp(std::vector<Mesh> meshes, std::vector<PerObjectCB> per_object_cbs,
251+
uint32_t model_id)
236252
{
237253
std::vector<int> mesh_ids;
238-
for (const auto& mesh : meshes)
254+
for (int i = 0; i < meshes.size(); i++)
239255
{
256+
const auto& mesh = meshes[i];
257+
const auto& per_object_cb = per_object_cbs[i];
258+
240259
int mesh_id = m_mesh_manager->AddCustomMesh(mesh);
241260
m_mesh_manager->UpdateMeshPerObjectData(mesh_id, per_object_cb);
242261
mesh_ids.push_back(mesh_id);

SourceFiles/Mesh.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
struct Mesh
55
{
6-
std::vector<GWVertex> vertices; // A vector of vertices
7-
std::vector<uint32_t> indices; // A vector of indices
6+
std::vector<GWVertex> vertices;
7+
std::vector<uint32_t> indices;
8+
std::vector<uint8_t> uv_coord_indices; // The number of uv_coords used to draw the mesh.
9+
std::vector<uint8_t> tex_indices; // The indices of the texture files used by the mesh.
10+
811
int num_textures;
912
};

0 commit comments

Comments
 (0)