Skip to content

Commit

Permalink
Textures!!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
nchalkley2 committed Aug 28, 2024
1 parent f797aa1 commit 8508c3c
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 39 deletions.
Binary file modified assets/models/kettle.fbx
Binary file not shown.
Binary file modified assets/models/plane.fbx
Binary file not shown.
1 change: 1 addition & 0 deletions assets/models/plane.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"modifiers": [
"gamma_correct_alpha"
],
"texture": "assets/textures/bricks1.tex",
"normals": false
}
2 changes: 1 addition & 1 deletion assets/textures/bricks1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"type": "texture",
"psm": "palette",
"alpha": false,
"file": "vjofdal_2K_Albedo.gif"
"file": "uv_tester.png"
}
Binary file added assets/textures/uv_tester.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/vjofdal_2K_Albedo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 13 additions & 13 deletions dependencies/egg-library/include/egg/texture_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ struct TextureFileHeader

static size_t serialize(Serializer& serializer, const TextureFileHeader& texture_header, size_t alignment = 1)
{
const size_t begin = serialize(serializer, texture_header.size_x);
serialize(serializer, texture_header.size_y);
serialize(serializer, texture_header.psm);
serialize(serializer, texture_header.function);
serialize(serializer, texture_header.components);

serialize(serializer, texture_header.horizontal);
serialize(serializer, texture_header.vertical);

serialize(serializer, texture_header.minu);
serialize(serializer, texture_header.maxu);
serialize(serializer, texture_header.minv);
serialize(serializer, texture_header.maxv);
const size_t begin = serialize(serializer, texture_header.size_x, 1);
serialize(serializer, texture_header.size_y, 1);
serialize(serializer, texture_header.psm, 1);
serialize(serializer, texture_header.function, 1);
serialize(serializer, texture_header.components, 1);

serialize(serializer, texture_header.horizontal, 1);
serialize(serializer, texture_header.vertical, 1);

serialize(serializer, texture_header.minu, 1);
serialize(serializer, texture_header.maxu, 1);
serialize(serializer, texture_header.minv, 1);
serialize(serializer, texture_header.maxv, 1);

serialize(serializer, texture_header.clut, 1, 16);
serialize(serializer, texture_header.data, 1, 16);
Expand Down
2 changes: 1 addition & 1 deletion dependencies/egg-ps2-graphics-lib/src/texture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void upload_texture(texture_descriptor& texture, void* texture_data, void* clut_
clut_height,
texture.clut.psm,
texture.clut.address,
64));
clut_width));
}

printf("Texture max: %d, %d\n", texture.wrap.maxu, texture.wrap.maxv);
Expand Down
5 changes: 3 additions & 2 deletions src/renderer/texture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ Texture::Texture(Asset::Reference texture_asset_ref, AssetRegistry::Asset* in_te
debug_name = Asset::lookup_path(texture_asset_ref).data();

const TextureFileHeader* texture = get_texture();

if (texture)
{
printf("Texture width height: %u, %u\n", texture->size_x, texture->size_y);

texture_descriptor.t_texbuff.info.components = texture->components;
texture_descriptor.t_texbuff.info.function = texture->function;
texture_descriptor.t_texbuff.psm = texture->psm;

texture_descriptor.clut.psm = GS_PSM_32;
texture_descriptor.clut.load_method = CLUT_LOAD;
texture_descriptor.clut.load_method = texture->clut.length > 0 ? CLUT_LOAD : CLUT_NO_LOAD;
texture_descriptor.clut.storage_mode = CLUT_STORAGE_MODE1;

texture_descriptor.wrap.horizontal = texture->horizontal;
Expand Down
61 changes: 39 additions & 22 deletions tools/ps2-mesh-converter/src/texture/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ union PalleteColor
};
static_assert(sizeof(PalleteColor) == 4);

static PalleteColor fromColorRGB(const Magick::ColorRGB& c, bool alpha)
{
PalleteColor new_color;
new_color.alpha = alpha ? c.alpha() * 255 : 255;
new_color.red = c.red() * 255;
new_color.green = c.green() * 255;
new_color.blue = c.blue() * 255;
return new_color;
}

union PalleteColor16
{
struct
Expand Down Expand Up @@ -129,13 +139,7 @@ static bool collectPalette(const Magick::Image& image, std::vector<uint32_t>& pa
// The pixel read doesn't work without this line. I don't understand why
const void* pixel = image.getConstPixels(x, y, 1, 1);

ColorRGB c = image.pixelColor(x, y);

PalleteColor new_color;
new_color.alpha = alpha ? c.alpha() * 255 : 255;
new_color.red = c.red() * 255;
new_color.green = c.green() * 255;
new_color.blue = c.blue() * 255;
PalleteColor new_color = fromColorRGB(image.pixelColor(x, y), alpha);

colors.insert(new_color);
}
Expand Down Expand Up @@ -167,6 +171,7 @@ static bool collectPalette(const Magick::Image& image, std::vector<uint32_t>& pa
for (const PalleteColor& c : colors)
{
palette_colors[i] = c.color;
print("Palette color: r: %u, g: %u, b: %u, a: %u", c.red, c.green, c.blue, c.alpha);
i++;
}

Expand Down Expand Up @@ -201,13 +206,7 @@ static bool writePalletizedImageData(const Magick::Image& image, const std::vect
// The pixel read doesn't work without this line. I don't understand why
const void* pixel = image.getConstPixels(x, y, 1, 1);

ColorRGB c = image.pixelColor(x, y);

PalleteColor new_color;
new_color.alpha = alpha ? c.alpha() * 255 : 255;
new_color.red = c.red() * 255;
new_color.green = c.green() * 255;
new_color.blue = c.blue() * 255;
PalleteColor new_color = fromColorRGB(image.pixelColor(x, y), alpha);

ptrdiff_t new_index = std::find(palette_colors.begin(), palette_colors.end(), new_color.color) - palette_colors.begin();
assert(new_index < palette_colors.size());
Expand Down Expand Up @@ -251,11 +250,32 @@ static std::vector<std::byte> serialize_texture(const TextureFileHeader& texture
{
std::vector<std::byte> out_data;
Serializer texture_serializer(out_data);
serialize(texture_serializer, texture_header);
assert(serialize(texture_serializer, texture_header) == 0);
texture_serializer.finish_serialization();
return out_data;
}

void checkTextureHeader(const TextureFileHeader& texture_header, const std::vector<std::byte>& data)
{
const TextureFileHeader& new_texture_header = *(const TextureFileHeader*)data.data();
assert(texture_header.size_x == new_texture_header.size_x);
assert(texture_header.size_y == new_texture_header.size_y);
assert(texture_header.psm == new_texture_header.psm);
assert(texture_header.function == new_texture_header.function);
assert(texture_header.components == new_texture_header.components);
assert(texture_header.horizontal == new_texture_header.horizontal);
assert(texture_header.vertical == new_texture_header.vertical);
assert(texture_header.minu == new_texture_header.minu);
assert(texture_header.maxu == new_texture_header.maxu);
assert(texture_header.minv == new_texture_header.minv);
assert(texture_header.maxv == new_texture_header.maxv);

print("Texture header length: %lu", texture_header.data.length);
assert(memcmp(texture_header.data.get_ptr(), new_texture_header.data.get_ptr(), texture_header.data.length) == 0);

assert(memcmp(texture_header.clut.get_ptr(), new_texture_header.clut.get_ptr(), texture_header.clut.length) == 0);
}

bool parseTexture(std::string_view path, const Json::Value& obj, std::vector<std::byte>& out_data)
{
using namespace Magick;
Expand Down Expand Up @@ -311,6 +331,7 @@ bool parseTexture(std::string_view path, const Json::Value& obj, std::vector<std
print("Pixels in palletized image: %lu", my_image.columns() * my_image.rows());

out_data = serialize_texture(texture_header);
checkTextureHeader(texture_header, out_data);
}
else if (iequals(color_type, "32") || iequals(color_type, "24"))
{
Expand All @@ -327,13 +348,7 @@ bool parseTexture(std::string_view path, const Json::Value& obj, std::vector<std
// The pixel read doesn't work without this line. I don't understand why
const void* pixel = my_image.getConstPixels(x, y, 1, 1);

ColorRGB c = my_image.pixelColor(x, y);

PalleteColor* new_color = (PalleteColor*)image_data.data();
new_color[i].alpha = obj["alpha"].asBool() ? c.alpha() * 255 : 255;
new_color[i].red = c.red() * 255;
new_color[i].green = c.green() * 255;
new_color[i].blue = c.blue() * 255;
*(PalleteColor*)image_data.data() = fromColorRGB(my_image.pixelColor(x, y), obj["alpha"].asBool());

i++;
}
Expand All @@ -343,6 +358,7 @@ bool parseTexture(std::string_view path, const Json::Value& obj, std::vector<std
texture_header.data.set(image_data);

out_data = serialize_texture(texture_header);
checkTextureHeader(texture_header, out_data);
}
else if (iequals(color_type, "16"))
{
Expand Down Expand Up @@ -375,6 +391,7 @@ bool parseTexture(std::string_view path, const Json::Value& obj, std::vector<std
texture_header.data.set(image_data);

out_data = serialize_texture(texture_header);
checkTextureHeader(texture_header, out_data);
}
else
{
Expand Down

0 comments on commit 8508c3c

Please sign in to comment.