add triangle vcm shape - 'tri'

This commit is contained in:
MihailRis 2026-01-05 15:09:19 +03:00
parent 97d2623a06
commit 79bf3a0f2c
5 changed files with 72 additions and 0 deletions

View file

@ -71,6 +71,39 @@ static void perform_rect(const xmlelement& root, model::Model& model) {
);
}
static void perform_triangle(const xmlelement& root, model::Model& model) {
auto pointA = root.attr("a").asVec3();
auto pointB = root.attr("b").asVec3();
auto pointC = root.attr("c").asVec3();
float uvs[6] {0, 0, 1, 0, 1, 1};
bool shading = true;
if (root.has("shading")) {
shading = to_boolean(root.attr("shading"));
}
if (root.has("uv")) {
root.attr("uv").asNumbers(uvs, sizeof(uvs) / sizeof(float));
}
glm::vec3 ba = pointB - pointA;
glm::vec3 ca = pointC - pointA;
glm::vec3 normal = glm::normalize(glm::cross(ba, ca));
std::string texture = root.attr("texture", "$0").getText();
auto& mesh = model.addMesh(texture, shading);
mesh.addTriangle(
pointA,
pointB,
pointC,
normal,
{uvs[0], uvs[1]},
{uvs[2], uvs[3]},
{uvs[4], uvs[5]}
);
}
static void perform_box(const xmlelement& root, model::Model& model) {
auto from = root.attr("from").asVec3();
auto to = root.attr("to").asVec3();
@ -181,6 +214,8 @@ static std::unique_ptr<model::Model> load_model(const xmlelement& root) {
perform_rect(*elem, model);
} else if (tag == "box") {
perform_box(*elem, model);
} else if (tag == "tri") {
perform_triangle(*elem, model);
}
}

View file

@ -105,6 +105,19 @@ glm::vec4 Attribute::asColor() const {
}
}
int Attribute::asNumbers(float* dst, size_t dstSize) const {
size_t pos = 0;
size_t count = 0;
while (pos < text.length() && count < dstSize) {
size_t end = text.find(',', pos);
dst[count++] = util::parse_double(
text, pos, std::min<size_t>(end - pos, text.length() - pos)
);
pos = end + 1;
}
return count;
}
Node::Node(std::string tag) : tag(std::move(tag)) {
}

View file

@ -29,6 +29,7 @@ namespace xml {
glm::vec3 asVec3() const;
glm::vec4 asVec4() const;
glm::vec4 asColor() const;
int asNumbers(float* dst, size_t dstSize) const;
};
/// @brief XML element class. Text element has tag 'text' and attribute

View file

@ -41,6 +41,20 @@ void Mesh::addPlane(
);
}
void Mesh::addTriangle(
const glm::vec3& a,
const glm::vec3& b,
const glm::vec3& c,
const glm::vec3& norm,
const glm::vec2& uvA,
const glm::vec2& uvB,
const glm::vec2& uvC
) {
vertices.push_back({a, uvA, norm});
vertices.push_back({b, uvB, norm});
vertices.push_back({c, uvC, norm});
}
void Mesh::addRect(
const glm::vec3& pos,
const glm::vec3& right,

View file

@ -39,6 +39,15 @@ namespace model {
const glm::vec3& up,
const glm::vec3& norm,
const UVRegion& region
);
void addTriangle(
const glm::vec3& a,
const glm::vec3& b,
const glm::vec3& c,
const glm::vec3& norm,
const glm::vec2& uvA,
const glm::vec2& uvB,
const glm::vec2& uvC
);
void addBox(const glm::vec3& pos, const glm::vec3& size);
void addBox(