2016-09-23 09:06:46 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-01-03 22:47:17 +01:00
|
|
|
#include "math_types.h"
|
2016-11-20 21:50:53 +01:00
|
|
|
|
2017-02-23 15:19:26 +01:00
|
|
|
#include "Globals.h"
|
2016-09-23 09:06:46 +02:00
|
|
|
|
|
|
|
struct Camera {
|
2017-01-03 22:47:17 +01:00
|
|
|
Vector3f eye;
|
|
|
|
Vector3f poi;
|
|
|
|
Vector3f up;
|
2016-09-23 09:06:46 +02:00
|
|
|
|
|
|
|
float near;
|
|
|
|
float far;
|
|
|
|
float fov;
|
|
|
|
bool orthographic;
|
|
|
|
float width;
|
|
|
|
float height;
|
|
|
|
|
2018-02-12 21:35:44 +01:00
|
|
|
Matrix44f mtxProj;
|
|
|
|
Matrix44f mtxView;
|
2016-09-23 09:06:46 +02:00
|
|
|
|
|
|
|
Camera() :
|
|
|
|
eye {5.f, 4.f, 5.f},
|
|
|
|
poi {0.f, 2.f, 0.f},
|
|
|
|
up {0.f, 1.f, 0.f},
|
2016-10-21 22:07:23 +02:00
|
|
|
near (0.1f),
|
|
|
|
far (150.f),
|
2016-11-30 22:11:08 +01:00
|
|
|
fov (60.f),
|
2016-09-23 09:06:46 +02:00
|
|
|
orthographic (false),
|
|
|
|
width (-1.f),
|
|
|
|
height (-1.f),
|
|
|
|
|
2018-02-12 21:35:44 +01:00
|
|
|
mtxProj (
|
2016-10-21 22:07:23 +02:00
|
|
|
1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, 1.f, 0.f, 0.f,
|
|
|
|
0.f, 0.f, 1.f, 0.f,
|
2018-02-12 21:35:44 +01:00
|
|
|
0.f, 0.f, 0.f, 1.f),
|
|
|
|
mtxView (
|
2016-09-23 09:06:46 +02:00
|
|
|
1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, 1.f, 0.f, 0.f,
|
|
|
|
0.f, 0.f, 1.f, 0.f,
|
2018-02-12 21:35:44 +01:00
|
|
|
0.f, 0.f, 0.f, 1.f)
|
|
|
|
{}
|
2016-09-23 09:06:46 +02:00
|
|
|
|
|
|
|
void updateMatrices();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Light {
|
2017-04-11 08:16:10 +02:00
|
|
|
Vector3f pos;
|
|
|
|
Vector3f dir;
|
2016-09-23 09:06:46 +02:00
|
|
|
|
|
|
|
float mtxView[16];
|
|
|
|
float mtxProj[16];
|
|
|
|
float mtxLight[16];
|
|
|
|
float mtxShadow[16];
|
|
|
|
|
|
|
|
float shadowMapBias;
|
|
|
|
uint16_t shadowMapSize;
|
|
|
|
|
|
|
|
bool enabled;
|
|
|
|
float near;
|
|
|
|
float far;
|
|
|
|
float area;
|
|
|
|
|
|
|
|
Light() :
|
2017-04-11 08:16:10 +02:00
|
|
|
pos (Vector3f(0.f, 10.f, 10.f)),
|
|
|
|
dir (Vector3f(-1.f, -1.f, -1.f)),
|
2016-09-23 09:06:46 +02:00
|
|
|
mtxView {
|
|
|
|
1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, 1.f, 0.f, 0.f,
|
|
|
|
0.f, 0.f, 1.f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f
|
|
|
|
},
|
|
|
|
mtxProj {
|
|
|
|
1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, 1.f, 0.f, 0.f,
|
|
|
|
0.f, 0.f, 1.f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f
|
|
|
|
},
|
|
|
|
mtxShadow {
|
|
|
|
1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, 1.f, 0.f, 0.f,
|
|
|
|
0.f, 0.f, 1.f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f
|
|
|
|
},
|
2016-11-10 21:29:47 +01:00
|
|
|
shadowMapBias (0.004f),
|
|
|
|
shadowMapSize (2048),
|
|
|
|
near (0.1f),
|
2016-09-23 09:06:46 +02:00
|
|
|
far (100.f),
|
|
|
|
area (10.f),
|
|
|
|
enabled (false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-03 22:47:17 +01:00
|
|
|
struct Transform {
|
|
|
|
Quaternion rotation = Quaternion (0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
Vector3f translation = Vector3f (0.0f, 0.0f, 0.0f);
|
|
|
|
Vector3f scale = Vector3f (1.0f, 1.0f, 1.0f);
|
|
|
|
|
|
|
|
Transform () {};
|
|
|
|
|
|
|
|
Transform (
|
|
|
|
const Vector3f &translation,
|
|
|
|
const Quaternion &rotation,
|
|
|
|
const Vector3f &scale)
|
|
|
|
:
|
|
|
|
translation(translation),
|
|
|
|
rotation(rotation),
|
|
|
|
scale(scale)
|
|
|
|
{}
|
|
|
|
|
|
|
|
Transform (const Matrix44f& mat) {
|
|
|
|
fromMatrix(mat);
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix44f toMatrix() const {
|
|
|
|
Matrix44f result;
|
|
|
|
|
|
|
|
Matrix33f scale_mat (
|
|
|
|
scale[0], 0.0f, 0.0f,
|
|
|
|
0.0f, scale[1], 0.0f,
|
|
|
|
0.0f, 0.0f, scale[2]
|
|
|
|
);
|
|
|
|
result.block<3,3>(0,0) = scale_mat * rotation.toMatrix();
|
|
|
|
result.block<1,3>(3,0) = translation.transpose();
|
|
|
|
result.block<3,1>(0,3) = Vector3f::Zero();
|
|
|
|
result(3,3) = 1.0f;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fromMatrix(const Matrix44f &matrix) {
|
|
|
|
// Extract rotation matrix and the quaternion
|
|
|
|
Matrix33f rot_matrix (matrix.block<3,3>(0,0));
|
|
|
|
|
|
|
|
Vector3f row0 = rot_matrix.block<1,3>(0,0).transpose();
|
|
|
|
Vector3f row1 = rot_matrix.block<1,3>(1,0).transpose();
|
|
|
|
Vector3f row2 = rot_matrix.block<1,3>(2,0).transpose();
|
|
|
|
|
|
|
|
scale.set(
|
|
|
|
row0.norm(),
|
|
|
|
row1.norm(),
|
|
|
|
row2.norm()
|
|
|
|
);
|
|
|
|
|
|
|
|
rot_matrix.block<1,3>(0,0) = (row0 / scale[0]).transpose();
|
|
|
|
rot_matrix.block<1,3>(1,0) = (row1 / scale[1]).transpose();
|
|
|
|
rot_matrix.block<1,3>(2,0) = (row2 / scale[2]).transpose();
|
|
|
|
|
|
|
|
rotation = Quaternion::fromMatrix(rot_matrix).normalize();
|
|
|
|
|
|
|
|
row0 = rot_matrix.block<1,3>(0,0).transpose();
|
|
|
|
row1 = rot_matrix.block<1,3>(1,0).transpose();
|
|
|
|
row2 = rot_matrix.block<1,3>(2,0).transpose();
|
|
|
|
|
|
|
|
Vector3f trans (
|
|
|
|
matrix(3,0),
|
|
|
|
matrix(3,1),
|
|
|
|
matrix(3,2)
|
|
|
|
);
|
|
|
|
|
|
|
|
translation = trans;
|
|
|
|
}
|
|
|
|
Transform operator*(const Transform &other) const {
|
|
|
|
Matrix44f this_mat (toMatrix());
|
|
|
|
Matrix44f other_mat (other.toMatrix());
|
|
|
|
|
|
|
|
return Transform(this_mat * other_mat);
|
|
|
|
}
|
|
|
|
Vector3f operator*(const Vector3f &vec) const {
|
|
|
|
assert(false);
|
|
|
|
return Vector3f::Zero();
|
|
|
|
}
|
|
|
|
|
2017-01-14 14:40:21 +01:00
|
|
|
static Transform fromTrans(
|
|
|
|
const Vector3f &translation
|
|
|
|
) {
|
|
|
|
return Transform (
|
|
|
|
translation,
|
|
|
|
Quaternion(0.0f, 0.0f, 0.0f, 1.0f),
|
|
|
|
Vector3f(1.0f, 1.0f, 1.0f)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Transform fromTransRot(
|
|
|
|
const Vector3f &translation,
|
|
|
|
const Quaternion &rotation
|
|
|
|
) {
|
|
|
|
return Transform (
|
|
|
|
translation,
|
|
|
|
rotation,
|
|
|
|
Vector3f(1.0f, 1.0f, 1.0f)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-03-11 22:16:12 +01:00
|
|
|
static Transform fromTransRot(
|
|
|
|
const Vector3f &translation,
|
|
|
|
const Matrix33f &rotation
|
|
|
|
) {
|
|
|
|
return Transform (
|
|
|
|
translation,
|
|
|
|
Quaternion::fromMatrix(rotation),
|
|
|
|
Vector3f(1.0f, 1.0f, 1.0f)
|
|
|
|
);
|
|
|
|
}
|
2017-01-03 22:47:17 +01:00
|
|
|
static Transform fromTransRotScale(
|
|
|
|
const Vector3f &translation,
|
|
|
|
const Quaternion &rotation,
|
|
|
|
const Vector3f &scale
|
|
|
|
) {
|
|
|
|
return Transform (translation, rotation, scale);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
struct Renderer {
|
2018-02-12 21:35:44 +01:00
|
|
|
bool initialized = false;
|
2017-02-18 17:22:21 +01:00
|
|
|
uint32_t view_width = 1;
|
|
|
|
uint32_t view_height = 1;
|
2016-09-23 09:06:46 +02:00
|
|
|
|
|
|
|
std::vector<Camera> cameras;
|
|
|
|
|
|
|
|
Renderer() :
|
|
|
|
initialized(false),
|
2017-04-11 08:16:10 +02:00
|
|
|
view_width (0),
|
2018-02-12 21:35:44 +01:00
|
|
|
view_height (0)
|
2017-07-02 23:26:02 +02:00
|
|
|
{ }
|
2016-09-23 09:06:46 +02:00
|
|
|
|
2018-02-12 21:35:44 +01:00
|
|
|
void Initialize(int width, int height);
|
|
|
|
void Shutdown();
|
|
|
|
void RenderGl();
|
|
|
|
void RenderGui();
|
|
|
|
void Resize (int width, int height);
|
2016-09-23 09:06:46 +02:00
|
|
|
};
|