mesh.h

C++ software renderer

src/mesh.h

1.3 KB
#pragma once

#include <vector>

#include "globals.h"
#include "util_math.h"

using std::vector;

struct Face {
    int vertex_idx;
    int vertex_texture_idx;
    int vertex_normal_idx;
    u32 mat_idx;
};

struct Transform {
    vec3f position{4.0, 0, 0};
    vec3f rotation{0, 0, 0};
    vec3f scale{1.0, 1.0, 1.0};

    Mat4 model_matrix() const {
        Mat4 scale_matrix     = Mat4::scale(scale.x, scale.y, scale.z);
        Mat4 rx               = Mat4::rotation_x(rotation.x);
        Mat4 ry               = Mat4::rotation_y(rotation.y);
        Mat4 rz               = Mat4::rotation_z(rotation.z);
        Mat4 rotation_matrix  = rx * ry * rz;
        Mat4 translate_matrix = Mat4::translate(position.x, position.y, position.z);
        return scale_matrix * rotation_matrix * translate_matrix;
    }
};

struct Material {
    char  name[128];
    char  texture_path[512];
    vec3f ambient;          // Ka
    vec3f diffuse;          // Kd;
    vec3f specular;         // Ks;
    u32   texture_img_idx;  // 0 sentinel for missing, populated in initialize_scene()
};
extern vector<Material> materials;

struct Mesh {
    vector<vec3f> vertices;
    vector<vec2f> texcoords;
    vector<vec3f> normals;
    vector<Face>  faces;
    Transform     transform;
    vec3f         color;
};

Mesh parse_obj(const char *filename);