summaryrefslogtreecommitdiff
path: root/src/3d/object.h
blob: 3d4ff35a1293769a4a597aa01e230b386d44a8d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// This file is part of the 64k demo project.
// It defines the base 3D Object structure.
// Handles transforms and bounding volumes for hybrid rendering.

#pragma once

#include "util/mini_math.h"
#include "util/asset_manager.h"

enum class ObjectType {
  CUBE,
  SPHERE,
  PLANE,
  TORUS,
  BOX,
  SKYBOX,
  MESH
  // Add more SDF types here
};

struct BoundingVolume {
  vec3 min;
  vec3 max;
};

class Object3D {
 public:
  vec3 position;
  quat rotation;
  vec3 scale;

  ObjectType type;
  // Material parameters could go here (color, roughness, etc.)
  vec4 color;

  // Physics fields
  vec3 velocity;
  float mass;
  float restitution;
  bool is_static;

  AssetId mesh_asset_id;
  vec3 local_extent; // Half-extents for AABB (used by meshes for shadows)
  void* user_data; // For tool-specific data, not for general use

  Object3D(ObjectType t = ObjectType::CUBE)
      : position(0, 0, 0), rotation(0, 0, 0, 1), scale(1, 1, 1), type(t),
        color(1, 1, 1, 1), velocity(0, 0, 0), mass(1.0f), restitution(0.5f),
        is_static(false), mesh_asset_id((AssetId)0), local_extent(1, 1, 1), 
        user_data(nullptr) {
  }

  mat4 get_model_matrix() const {
    mat4 T = mat4::translate(position);
    mat4 R = rotation.to_mat();
    mat4 S = mat4::scale(scale);
    // M = T * R * S
    return T * (R * S);
  }

  // Returns the local-space AABB of the primitive (before transform)
  // Used to generate the proxy geometry for rasterization.
  BoundingVolume get_local_bounds() const {
    if (type == ObjectType::TORUS)
      return {{-1.5f, -0.5f, -1.5f}, {1.5f, 0.5f, 1.5f}};
    if (type == ObjectType::MESH)
      return {-local_extent, local_extent};
    // Simple defaults for unit primitives
    return {{-1, -1, -1}, {1, 1, 1}};
  }
};