struct Vector 3 public float x float y
เวกเตอร struct Vector 3 { public: float x; float y; float z; // Other parts of the struct omitted. };
สงทคณสามารถทำไดกบเวกเตอร • คำนวณ cross product – โคด inline Vector 3 cross( const Vector 3 &v 1, const Vector 3 &v 2) { return Vector 3( (v 1. y * v 2. z) - (v 1. z * v 2. y), (v 1. z * v 2. x) - (v 1. x * v 2. z), (v 1. x * v 2. y) - (v 1. y * v 2. x)); } – ตวอยาง Vector 3 a(1, 0, 0); // a = (1, 0, 0) Vector 3 b(0, 1, 0); // b = (0, 1, 0) Vector 3 c = cross(a, b) // c = (0, 0, 1)
จด struct Point 3 { public: float x; float y; float z; // Other parts of the struct omitted. };
เมตรกซ struct Matrix 4 x 4 { private: float m[4][4]; // Other parts of the struct omitted. };
สงทคณสามารถทำไดกบเมตรกซ Matrix 4 x 4 A(2, 0, 0, 1, 0, 2, 0, 1, 0, 0, 2, 1, 0, 0, 0, 1); // A is "scale by factor of 2" then "translate by (1, 1, 1). " Vector 3 Point 3 v(1, 1, 1); p(1, 1, 1); u = A*v; // u = (2, 2, 2) q = A*p; // v = (3, 3, 3)
สงทคณทำไดกบเมตรกซ Matrix 4 x 4 A(2, 0, 0, 2, 0, 0, 2, 2, 0, 0, 0, 1); // A is "scale by factor of 2" then "translate by (2, 2, 2). " Matrix 4 x 4 B = transpose(A); // B = [[2, 0, 0, 0], [0, 2, 0, 0], [0, 0, 2, 0], [2, 2, 2, 1]] Matrix 4 x 4 C = inverse(A); // C = [[0. 5, 0, 0, 1], [0, 0. 5, 0, 1], [0, 0, 0. 5, 1], [0, 0, 0, 1]] float d = det(A) // d = 8
สงทคณทำไดกบเมตรกซ – static Matrix 4 x 4 look_at( Point 3 eye, Point 3 at, Vector 3 up); • สรางเมตรกซของการแปลง look-at – static Matrix 4 x 4 orthographic_projection( float l, float r, float b, float t, float n=0, float f=1); • สรางเมตรกซของ orthographic projection – static Matrix 4 x 4 perspective_projection( float l, float r, float b, float t, float n, float f); static Matrix 4 x 4 perspective_projection( float fovy, float aspect, float near, float far); • สรางการแปลงของ perspective projection
สงทคณทำไดกบเมตรกซ • ตวอยาง Matrix 4 x 4 I = Matrix 4 x 4: : identity(); Matrix 4 x 4 T = Matrix 4 x 4: : translation(1, 2, 3); Matrix 4 x 4 R = Matrix 4 x 4: : rotation(60, Vector 3(0, 0, 1)); Matrix 4 x 4 S = Matrix 4 xx: : scale(2, 2, 2);
การแปลง struct Transform { public: Matrix 4 x 4 m; Matrix 4 x 4 mit; // Other parts of the class omitted. };
สงทคณสามารถทำไดกบการแปลง • ตวอยาง Transform T 1 = Transform(); Transform T 2 = Transform(Matrix 4 x 4: : translate(1, 2, 3)); Transform T 3 = Transform(Matrix 4 x 4: : translate(1, 2, 3), Matrix 4 x 4: : translate(-1, -2, -3)); Transform T 4 = Transform(Matrix 4 x 4: : translate(1, 2, 3), Matrix 4 x 4: : translate(-1, -2, -3), transpose(Matrix 4 x 4: : translate(-1, -2, -3)));
สงทคณทำไดกบการแปลง • สรางการแปลง affine พนฐานตางๆ – static Transform identity(); – static Transform translate( float x, float y, float z); – static Transform scale( float x, float y, float z); – static Transform rotate( float degrees, Vector 3 axis);
Texture Coordinate struct Uv { public: float u; float v; // Other parts of the class omitted. };
ส Rgba struct Rgba { public: float r; float g; float b; float a; // Other parts of the struct omitted. };
ขอมลรปทรง class Universal. Mesh : public Mesh { public: virtual void append_position(const Point 3 &position); virtual void append_normal(const Vector 3 &normal); virtual void append_new_face(); virtual void append_vertex_to_last_face( int position_index, int normal_index); // Other parts of the class omitted. }
ขอมลรปทรง – หลงจากนนจงใสจด cube. append_position(Point 3(-1, -1)); cube. append_position(Point 3(-1, 1, -1)); cube. append_position(Point 3(-1, 1, 1)); cube. append_position(Point 3( 1, -1)); cube. append_position(Point 3( 1, -1, 1)); cube. append_position(Point 3( 1, 1, -1)); cube. append_position(Point 3( 1, 1, 1));
ขอมลรปทรง – แลวจงใส normal cube. append_normal(Vector 3( 0, 0, 1)); cube. append_normal(Vector 3( 0, 0, -1)); cube. append_normal(Vector 3( 1, 0, 0)); cube. append_normal(Vector 3(-1, 0, 0)); cube. append_normal(Vector 3( 0, 1, 0)); cube. append_normal(Vector 3( 0, -1, 0));
ขอมล • หลงจากนนจงเพมหนาใหมทละหนา แลวใสขอมลจดยอดมมของหนานนเขาไปเรอยๆ cube. append_new_face(); cube. append_vertex_to_last_face(1, 0); cube. append_vertex_to_last_face(5, 0); cube. append_vertex_to_last_face(7, 0); cube. append_vertex_to_last_face(3, 0);
. obj ตวอยางไฟล # cube. obj # g cube v v v v 0. 0 1. 0 0. 0 1. 0 vn 0. 0 -1. 0 vn 0. 0 1. 0 0. 0 vn 0. 0 -1. 0 0. 0 vn -1. 0 0. 0 f f f 1//2 1//6 3//3 5//5 1//4 2//1 7//2 3//2 4//6 2//6 8//3 4//3 7//5 8//5 5//4 6//1 8//1 5//2 7//2 3//6 4//6 7//3 8//5 6//4 2//4 8//1 4//1
- Slides: 63