Nugget
Bare-metal libraries and examples for the original PlayStation
Loading...
Searching...
No Matches
psxm.hh
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2026 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <stdint.h>
30
68namespace psxm {
69
70struct Header {
71 char magic[4];
72 uint16_t vertexCount;
73 uint16_t normalCount;
74 uint16_t triangleCount;
75 uint16_t reserved;
76 uint32_t vertexOffset;
77 uint32_t normalOffset;
79};
80static_assert(sizeof(Header) == 24, "PXM2 header size mismatch");
81
82struct Vec3 {
83 int16_t x, y, z;
84};
85static_assert(sizeof(Vec3) == 6, "PXM2 vector size mismatch");
86
87struct UV {
88 uint8_t u, v;
89};
90
91struct Triangle {
92 uint16_t vertices[3];
93 uint16_t normals[3];
94 UV uv[3];
95 uint16_t padding;
96};
97static_assert(sizeof(Triangle) == 20, "PXM2 triangle size mismatch");
98
103class Model {
104 public:
109 bool parse(const uint8_t* blob) {
110 auto header = reinterpret_cast<const Header*>(blob);
111 if (header->magic[0] != 'P' || header->magic[1] != 'X' || header->magic[2] != 'M' || header->magic[3] != '2') {
112 return false;
113 }
114 m_header = header;
115 m_vertices = reinterpret_cast<const Vec3*>(blob + header->vertexOffset);
116 m_normals = reinterpret_cast<const Vec3*>(blob + header->normalOffset);
117 m_triangles = reinterpret_cast<const Triangle*>(blob + header->triangleOffset);
118 return true;
119 }
120
121 unsigned vertexCount() const { return m_header->vertexCount; }
122 unsigned normalCount() const { return m_header->normalCount; }
123 unsigned triangleCount() const { return m_header->triangleCount; }
124
125 const Vec3* vertices() const { return m_vertices; }
126 const Vec3* normals() const { return m_normals; }
127 const Triangle* triangles() const { return m_triangles; }
128
129 private:
130 const Header* m_header = nullptr;
131 const Vec3* m_vertices = nullptr;
132 const Vec3* m_normals = nullptr;
133 const Triangle* m_triangles = nullptr;
134};
135
136} // namespace psxm
Points into a PXM2 blob without copying it, so the blob has to outlive it. For an asset linked in by ...
Definition psxm.hh:103
unsigned normalCount() const
Definition psxm.hh:122
bool parse(const uint8_t *blob)
Accepts the blob if it carries the PXM2 magic. The blob has to be four byte aligned.
Definition psxm.hh:109
unsigned triangleCount() const
Definition psxm.hh:123
const Vec3 * vertices() const
Definition psxm.hh:125
const Triangle * triangles() const
Definition psxm.hh:127
unsigned vertexCount() const
Definition psxm.hh:121
const Vec3 * normals() const
Definition psxm.hh:126
Definition psxm.hh:68
Definition psxm.hh:70
uint16_t vertexCount
Definition psxm.hh:72
uint16_t reserved
Zero.
Definition psxm.hh:75
char magic[4]
"PXM2"
Definition psxm.hh:71
uint32_t vertexOffset
Definition psxm.hh:76
uint16_t normalCount
Definition psxm.hh:73
uint16_t triangleCount
Definition psxm.hh:74
uint32_t triangleOffset
Definition psxm.hh:78
uint32_t normalOffset
Definition psxm.hh:77
Definition psxm.hh:91
UV uv[3]
Definition psxm.hh:94
uint16_t vertices[3]
Definition psxm.hh:92
uint16_t normals[3]
Definition psxm.hh:93
uint16_t padding
Zero. Keeps the record a multiple of four bytes.
Definition psxm.hh:95
Definition psxm.hh:87
uint8_t v
Definition psxm.hh:88
uint8_t u
Definition psxm.hh:88
Definition psxm.hh:82
int16_t x
Definition psxm.hh:83
int16_t y
Definition psxm.hh:83
int16_t z
Definition psxm.hh:83