Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Pipeline
- {
- Vector3f pos, scale, rotation;
- Matrix4f m;
- Pipeline() : pos(0,0,0),
- rotation(0,0,0),
- scale(1,1,1)
- {
- calculateModel();
- }
- void setRotation(float angleX, float angleY, float angleZ)
- {
- rotation = Vector3f(angleX, angleY, angleZ);
- calculateModel();
- }
- void setScale(float factorX, float factorY, float factorZ)
- {
- scale = Vector3f(factorX, factorY, factorZ);
- calculateModel();
- }
- void setTranslation(float transX, float transY, float transZ)
- {
- pos = Vector3f(transX, transY, transZ);
- calculateModel();
- }
- Matrix4f getMatrix(Camera* c)
- {
- return c->getPerspective() * c->getView() * m;
- }
- void calculateModel()
- {
- Matrix4f rot =
- Matrix4f(1.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, cosf(rotation.x()), -sinf(rotation.x()), 0.0f,
- 0.0f, sinf(rotation.x()), cosf(rotation.x()), 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f)
- *
- Matrix4f(cosf(rotation.y()), 0.0f, -sinf(rotation.y()), 0.0f,
- 0.0f, 1.0f, 0.0f, 0.0f,
- sinf(rotation.y()), 0.0f, cosf(rotation.y()), 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f)
- *
- Matrix4f(cosf(rotation.z()), -sinf(rotation.z()), 0.0f, 0.0f,
- sinf(rotation.z()), cosf(rotation.z()), 0.0f, 0.0f,
- 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f);
- Matrix4f trans =
- Matrix4f(1.0f, 0.0f, 0.0f, pos.x(),
- 0.0f, 1.0f, 0.0f, pos.y(),
- 0.0f, 0.0f, 1.0f, pos.z(),
- 0.0f, 0.0f, 0.0f, 1.0f);
- Matrix4f scaling =
- Matrix4f(scale.x(), 0.0f, 0.0f, 0.0f,
- 0.0f, scale.y(), 0.0f, 0.0f,
- 0.0f, 0.0f, scale.z(), 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f);
- m = trans * rot * scaling;
- }
- }
- class Camera
- {
- Matrix4f perspective;
- Matrix4f view;
- float ar, near, far, fov;
- float x, y, z;
- Vector3f n, u, v;
- Camera(float near, float far, float fov, float ar) : near(near), far(far), fov(fov), ar(ar), view(Matrix4f::identity()), perspective(Matrix4f::identity()), x(0), y(0), z(0)
- {
- setLookat(Vector3f(0, 0, 1), Vector3f(0, 1, 0));
- }
- void computePerspective()
- {
- float invTanHalveFOV = 1 / tan(fov / 2);
- perspective = Matrix4f(1 / tan(fov / 2), 0, 0, 0,
- 0, 1 / (ar * tan(fov / 2) ), 0, 0,
- 0, 0, (-near - (far * 2)) / (near - far), (2 * far * near) / (near - far),
- 0, 0, 1, 0);
- }
- void computeView()
- {
- view = Matrix4f(u.x(), u.y(), u.z(), 0 ,
- v.x(), v.y(), v.z(), 0 ,
- n.x(), n.y(), n.z(), 0 ,
- 0 , 0 , 0 , 1 )
- *
- Matrix4f(1 , 0 , 0 , -x ,
- 0 , 1 , 0 , -y ,
- 0 , 0 , 1 , -z ,
- 0 , 0 , 0 , 1 );
- }
- void setLookat(const Vector3f& target, const Vector3f& up)
- {
- n = target.normalized();
- u = up.normalized().cross(n);
- v = n.cross(u);
- computeView();
- }
- }
- // And the code that uses all the above:
- Camera c(1, 100, .25*TAU, WINDOW_WIDTH / WINDOW_HEIGHT);
- Pipeline p;
- p.setRotation(0, 0, .125*TAU);
- Matrix4f MVP = p.getMatrix(&c);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement