Add normales interface to mesh

TODO: need refactor ASAP
This commit is contained in:
JackCarterSmith 2025-02-07 12:52:48 +01:00
parent f9935d6b77
commit adbc8f1ccc
Signed by: JackCarterSmith
GPG Key ID: 832E52F4E23F8F24
6 changed files with 32 additions and 18 deletions

View File

@ -2134,6 +2134,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
has_d = false;
has_tr = false;
has_kd = false;
// set new mtl name
token += 7;
@ -2708,7 +2709,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
sw.vertex_id = vid;
while (!IS_NEW_LINE(token[0])) {
while (!IS_NEW_LINE(token[0]) && token[0] != '#') {
real_t j, w;
// joint_id should not be negative, weight may be negative
// TODO(syoyo): # of elements check
@ -2749,7 +2750,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
__line_t line;
while (!IS_NEW_LINE(token[0])) {
while (!IS_NEW_LINE(token[0]) && token[0] != '#') {
vertex_index_t vi;
if (!parseTriple(&token, static_cast<int>(v.size() / 3),
static_cast<int>(vn.size() / 3),
@ -2780,7 +2781,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
__points_t pts;
while (!IS_NEW_LINE(token[0])) {
while (!IS_NEW_LINE(token[0]) && token[0] != '#') {
vertex_index_t vi;
if (!parseTriple(&token, static_cast<int>(v.size() / 3),
static_cast<int>(vn.size() / 3),
@ -2815,7 +2816,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
face.smoothing_group_id = current_smoothing_id;
face.vertex_indices.reserve(3);
while (!IS_NEW_LINE(token[0])) {
while (!IS_NEW_LINE(token[0]) && token[0] != '#') {
vertex_index_t vi;
if (!parseTriple(&token, static_cast<int>(v.size() / 3),
static_cast<int>(vn.size() / 3),
@ -2951,7 +2952,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
std::vector<std::string> names;
while (!IS_NEW_LINE(token[0])) {
while (!IS_NEW_LINE(token[0]) && token[0] != '#') {
std::string str = parseString(&token);
names.push_back(str);
token += strspn(token, " \t\r"); // skip tag
@ -3245,7 +3246,7 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
token += strspn(token, " \t");
indices.clear();
while (!IS_NEW_LINE(token[0])) {
while (!IS_NEW_LINE(token[0]) && token[0] != '#') {
vertex_index_t vi = parseRawTriple(&token);
index_t idx;
@ -3360,7 +3361,7 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
if (token[0] == 'g' && IS_SPACE((token[1]))) {
names.clear();
while (!IS_NEW_LINE(token[0])) {
while (!IS_NEW_LINE(token[0]) && token[0] != '#') {
std::string str = parseString(&token);
names.push_back(str);
token += strspn(token, " \t\r"); // skip tag

View File

@ -7,6 +7,9 @@
#define SF_COLOR_4CHEX(h) sf::Color((uint32_t)h)
#define FAR_Z (100.f)
#define NEAR_Z (0.1f)
//#define DISABLE_AABB_CLIPPING
//#define DISABLE_TRIANGLE_CLIPPING
//#define DISABLE_WIREFRAME_MODE
@ -82,7 +85,7 @@ Graphic3DRenderer::~Graphic3DRenderer() {}
void Graphic3DRenderer::SetRTSize(unsigned int w, unsigned int h) {
mRTSize.x = w; mRTSize.y = h;
mMainCamera->SetFrustrum(75.0f, mRTSize.x/mRTSize.y, 1.0f, 100.f);
mMainCamera->SetFrustrum(75.0f, mRTSize.x/mRTSize.y, NEAR_Z, FAR_Z);
}
void Graphic3DRenderer::UpdateCamera(CAMERA_MOVE type, const float value) {
@ -172,8 +175,8 @@ float Graphic3DRenderer::ComputeSGRatio() {
// The triangle made by the ground plane intersection with the frustum. This intersection
// cross the far plane at some point. Instead of computing the coordinate of the point, we
// directly use the far plane length to get the corresponding ratio for the screen.
sgRatio = -(altitude * fovCos - 1000.f * fovThetaSin)
/ (2.f * 1000.f * fovSin * thetaCos);
sgRatio = -(altitude * fovCos - FAR_Z * fovThetaSin)
/ (2.f * FAR_Z * fovSin * thetaCos);
}
// Clamp
@ -317,14 +320,12 @@ void Graphic3DRenderer::DrawSceneObjects(sf::RenderTexture& context) {
V3 = M3D_V4Divide(V3, M3D_V4SplatW(V3));
// Finally project from NDC to the screen
V1 = M3D_V3TransformNDCToViewport(V1, 0.f, 0.f, mRTSize.x, mRTSize.y, 1.f, 100.f);
V2 = M3D_V3TransformNDCToViewport(V2, 0.f, 0.f, mRTSize.x, mRTSize.y, 1.f, 100.f);
V3 = M3D_V3TransformNDCToViewport(V3, 0.f, 0.f, mRTSize.x, mRTSize.y, 1.f, 100.f);
V1 = M3D_V3TransformNDCToViewport(V1, 0.f, 0.f, mRTSize.x, mRTSize.y, NEAR_Z, FAR_Z);
V2 = M3D_V3TransformNDCToViewport(V2, 0.f, 0.f, mRTSize.x, mRTSize.y, NEAR_Z, FAR_Z);
V3 = M3D_V3TransformNDCToViewport(V3, 0.f, 0.f, mRTSize.x, mRTSize.y, NEAR_Z, FAR_Z);
// Simplified back-face culling on 2D viewport triangle
if (M3D_V4GetX(M3D_Tri2DNormal(V1,V2,V3)) > 0) {
// Set pixels color depending of frustrum clipping type - debug purpose
if (ri.frustrumClipType == DISJOINT) {
drawPoints[0].color = drawPoints[1].color = drawPoints[2].color = sf::Color::Red;

View File

@ -10,6 +10,8 @@
#include <SFML/Graphics/CircleShape.hpp>
#define SF_COLOR_4CHEX(h) sf::Color((uint32_t)h)
void UI::SetRTSize(unsigned int w, unsigned int h, bool recreateCanvas) {
mRTSize.x = w; mRTSize.y = h;

View File

@ -26,6 +26,7 @@ struct Vertex {
}
M3D_F3 pos = {0.0f, 0.0f, 0.0f};
int32_t norm_index = -1;
sf::Color color = sf::Color::White;
};
@ -61,6 +62,7 @@ private:
struct Mesh {
std::vector<Vertex> vertices;
std::vector<M3D_F3> normales;
std::vector<MeshPart> parts;
inline constexpr size_t GetVertexStride() const noexcept { return sizeof(Vertex); }

View File

@ -23,6 +23,7 @@ public:
const M3D_BoundingBox& GetAABB() const noexcept { return aabb; }
//TODO: recompute AABB when changing these parameters
void SetPosition(M3D_F3& _pos) noexcept { pos = _pos; }
void SetPosition(float _x, float _y, float _z) noexcept { pos = M3D_F3(_x, _y, _z); }
void SetRotation(M3D_F3& _rot) noexcept { rot = _rot; }
@ -34,7 +35,7 @@ public:
protected:
WorldObject() = default;
M3D_BoundingBox aabb;
inline static M3D_BoundingBox aabb;
M3D_F3 scale = M3D_F3(1.0f, 1.0f, 1.0f);
M3D_F3 rot = M3D_F3(0.0f, 0.0f, 0.0f);

View File

@ -30,6 +30,10 @@ void WorldObjectAbstract<D>::LoadMeshFromObjFile(std::string file) {
for (size_t i = 0; i < attrib.vertices.size(); i += 3)
mMesh.vertices.push_back(Vertex(attrib.vertices[i], attrib.vertices[i+1], attrib.vertices[i+2]));
// Reinterprete normales to our format
for (size_t n = 0; n < attrib.normals.size(); n += 3)
mMesh.normales.push_back(M3D_F3(attrib.normals[n], attrib.normals[n+1], attrib.normals[n+2]));
// Loop over shapes/subparts
for (size_t s = 0; s < shapes.size(); s++) {
MeshPart subpart;
@ -47,9 +51,12 @@ void WorldObjectAbstract<D>::LoadMeshFromObjFile(std::string file) {
// Loop over indices in the face.
for (size_t v = 0; v < fv; v++) {
subpart.indices.push_back(shapes[s].mesh.indices[index_offset + v].vertex_index);
tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
subpart.indices.push_back(idx.vertex_index);
// Check if `normal_index` is zero or positive. negative = no normal data
//if (idx.normal_index >= 0) {}
if (idx.normal_index >= 0)
mMesh.vertices[idx.vertex_index].norm_index = idx.normal_index;
// Check if `texcoord_index` is zero or positive. negative = no texcoord data
//if (idx.texcoord_index >= 0) {}