完成相机注视

test-headless
ink-soul 2024-04-22 18:19:55 +08:00
parent 9281b77178
commit b588fa1636
6 changed files with 220 additions and 15 deletions

View File

@ -41,7 +41,7 @@ private:
}
else
{
matrices.view = transM * rotM;
matrices.view = LookAt(position, to, up, true, true);
}
viewPos = glm::vec4(position, 0.0f) * glm::vec4(-1.0f, 1.0f, -1.0f, 1.0f);
@ -55,7 +55,11 @@ public:
glm::vec3 rotation = glm::vec3();
glm::vec3 position = glm::vec3();
glm::vec4 viewPos = glm::vec4();
glm::mat3 rotationMatrix = glm::mat3();
glm::vec3 from = glm::vec3();
glm::vec3 to = glm::vec3();
glm::vec3 up = glm::vec3();
glm::mat4 rotationMatrix = glm::mat4();
float rotationSpeed = 1.0f;
float movementSpeed = 1.0f;
@ -90,6 +94,151 @@ public:
return zfar;
}
void setTo(glm::vec3 to)
{
this->to = to;
}
void setUp(glm::vec3 up)
{
this->up = up;
}
/*
·½°¸À´Ô´£º
https://zhuanlan.zhihu.com/p/635801612
*/
glm::mat4 GetProjectionMatrixFromClip(float zleft, float zright, float zbottom, float ztop, float znear, float zfar, bool zPositive)
{
if (zPositive)
{
float A = -(zfar + znear) / (znear - zfar);
float B = 2 * (zfar * znear) / (znear - zfar);
glm::mat4 T = glm::mat4(
2 * znear / (zright - zleft), 0, -(zright + zleft) / (zright - zleft), 0,
0, 2 * znear / (ztop - zbottom), -(ztop + zbottom) / (ztop - zbottom), 0,
0, 0, A, B,
0, 0, 1, 0);
T = glm::transpose(T); // let's transpose it back to column major
return T;
}
else
{
float A = (zfar + znear) / (znear - zfar);
float B = 2 * (zfar * znear) / (znear - zfar);
glm::mat4 T = glm::mat4(
2 * znear / (zright - zleft), 0, (zright + zleft) / (zright - zleft), 0,
0, 2 * znear / (ztop - zbottom), (ztop + zbottom) / (ztop - zbottom), 0,
0, 0, A, B,
0, 0, -1, 0);
T = glm::transpose(T); // let's transpose it back to column major
return T;
}
}
void setProjectionMatrix(float fx, float fy, float u0, float v0, float znear, float zfar, float viewWidth, float viewHeight, bool zPositive, bool rightHand)
{
float l = -u0 / fx * znear;
float r = (viewWidth - u0) / fx * znear;
float t = -v0 / fy * znear;
float b = (viewHeight - v0) / fy * znear;
glm::mat4 perspective = glm::mat4();
if (rightHand)
{
if (zPositive)
{ // right hand system and z positive, usually used in 3d reconstruction/SFM/SLAM
perspective = GetProjectionMatrixFromClip(l, r, b, t, znear, zfar, zPositive);
}
else
{ // right hand system, z negative, usually used in OpenGL
perspective = GetProjectionMatrixFromClip(l, r, -b, -t, znear, zfar, zPositive);
}
}
else
{
if (zPositive)
{ // left hand system, z positive
perspective = GetProjectionMatrixFromClip(l, r, -b, -t, znear, zfar, zPositive);
}
else
{ // left hand system, z negative
perspective = GetProjectionMatrixFromClip(l, r, b, t, znear, zfar, zPositive);
}
}
matrices.perspective = perspective;
}
glm::mat4 LookAt(const glm::vec3& from, const glm::vec3& to, const glm::vec3& up, bool zPositive, bool rightHand)
{
this->from = from;
this->to = to;
this->up = up;
// we stands on "from" and look at "to"
// if zPositive = true, the destination is in the area of the positive half-axis of z, otherwise it is negative
// if rightHand = true, the coordinate system is right-handed, otherwise it is left-handed
glm::vec3 zAxis = glm::normalize(to - from);
glm::vec3 yAxis = glm::normalize(up);
glm::vec3 xAxis;
if (rightHand)
{
if (zPositive)
{ // rightHand = true and zPositive = true, usually used in 3d reconstruction
yAxis = -yAxis;
}
else
{ // rightHand = true and zPositive = false, usually used in OpenGL
zAxis = -zAxis;
}
}
else
{ // left-hand
if (!zPositive)
{
zAxis = -zAxis;
yAxis = -yAxis;
}
}
xAxis = glm::normalize(glm::cross(yAxis, zAxis));
yAxis = glm::normalize(glm::cross(zAxis, xAxis));
// glm matrix is column major, so if you set like this, you will notice that the matrix is transposed when you use it.
glm::mat3 R = glm::mat3(
xAxis.x, xAxis.y, xAxis.z,
yAxis.x, yAxis.y, yAxis.z,
zAxis.x, zAxis.y, zAxis.z
);
R = glm::transpose(R); // Let's transpose it back
glm::vec3 t = -R * from; // t = -R * from;
glm::mat4 m = glm::mat4(
xAxis.x, xAxis.y, xAxis.z, t.x,
yAxis.x, yAxis.y, yAxis.z, t.y,
zAxis.x, zAxis.y, zAxis.z, t.z,
0, 0, 0, 1);
m = glm::transpose(m); // Let's transpose it back
return m;
}
/*
GDC2007·½°¸£ºKoshy George
*/
void setPerspective(float fx, float fy, float cx, float cy, float znear, float zfar)
{
this->znear = znear;
this->zfar = zfar;
float A = -(zfar + znear) / (znear - zfar);
float B = 2 * (zfar * znear) / (znear - zfar);
glm::mat4 persepctive = glm::mat4(glm::vec4(fx / cx, 0, 0, 0),
glm::vec4(0, fy / cy, 0, 0),
glm::vec4(0, 0, A, B),
glm::vec4(0, 0, 1, 0));
matrices.perspective = persepctive;
}
void setPerspective(float fov, float aspect, float znear, float zfar)
{
this->fov = fov;

View File

@ -18,14 +18,17 @@ void PlumageConfig::PlumageConfiguration::readConfigurationFromToml(std::string
settings.endFrameIndex = toml::find<uint32_t>(tomlSettings, "endFrameIndex");
settings.videoFrameRate = toml::find<uint32_t>(tomlSettings, "videoFrameRate");
auto& camera = toml::find(tomlSettings, "camera");
settings.fovX = toml::find<float>(camera, "fovX");
settings.fovY = toml::find<float>(camera, "fovY");
settings.calibrationWidth = toml::find<int>(camera, "cam_width");
settings.calibrationHeight = toml::find<int>(camera, "cam_height");
std::string fX = toml::find<std::string>(camera, "fx");
std::string fY = toml::find<std::string>(camera, "fy");
std::string cX = toml::find<std::string>(camera, "cX");
std::string cY = toml::find<std::string>(camera, "cY");
std::vector<float> bottomCenter = toml::find<std::vector<float>>(camera, "bottomCenter");
std::vector<float> bottomNormal = toml::find<std::vector<float>>(camera, "bottomNormal");
std::vector<std::vector<float>> cameraTracks = toml::find<std::vector<std::vector<float>>>(camera, "cameraTracks");
std::vector<std::vector<std::vector<float>>> cameraAngle = toml::find<std::vector<std::vector<std::vector<float>>>>(camera, "cameraAngle");
auto& debug = toml::find(tomlSettings, "debug");
settings.validation = toml::find<bool>(debug, "validation");
settings.vsync = toml::find<bool>(debug, "vsync");
@ -38,6 +41,8 @@ void PlumageConfig::PlumageConfiguration::readConfigurationFromToml(std::string
conversion
*/
size_t sz;
settings.fX = std::stof(fX, &sz);
settings.fY = std::stof(fY, &sz);
settings.cX = std::stof(cX, &sz);
settings.cY = std::stof(cY, &sz);
settings.bottomCenter = glm::vec3(bottomCenter[0], -bottomCenter[1], -bottomCenter[2]);
@ -55,6 +60,12 @@ void PlumageConfig::PlumageConfiguration::readConfigurationFromToml(std::string
glm::vec3(cameraAngle[i][2][0], cameraAngle[i][2][1], cameraAngle[i][2][2]));
}
float ratioX = settings.width / settings.calibrationWidth;
float ratioY = settings.height / settings.calibrationWidth;
settings.fX = settings.fX * ratioX;
settings.fY = settings.fY * ratioY;
settings.cX = settings.cX * ratioX;
settings.cY = settings.cY * ratioY;
}
PlumageConfig::PlumageConfiguration::PlumageConfiguration()

View File

@ -40,11 +40,14 @@ namespace PlumageConfig
uint32_t videoFrameRate = 25; // 视频帧率
uint32_t selectedPhysicalDeviceIndex = 7;
float fX = 565.5f;
float fY = 516.3f;
float fovX = 1.f;
float fovY = 1.f;
float cX = 2.f;
float cY = 0.f;
float cX = 328.2f;
float cY = 238.8f;
int calibrationWidth = 640.0f;
int calibrationHeight = 480.0f;
glm::vec3 bottomCenter = { 0.f,0.f,-6.f };
//std::vector<float> bottomCenter = { 0.f,0.f,-6.f };
glm::vec3 bottomNormal = { 0.f,1.0f,0.f };
@ -127,7 +130,7 @@ namespace PlumageConfig
std::string hdr2ktxShFilePath = getAssetPath() + "script/hdr2ktxShFilePath.sh";
// 配置文件路径,命令行传入后保存在这
//std::string configFilePath = getAssetPath() + "config/guanzi.toml";
std::string configFilePath = getAssetPath() + "config/fangshai_traj1_matrix.toml";
std::string configFilePath = getAssetPath() + "config/yukino_traj1_matrix.toml";
} filePath;

View File

@ -11,7 +11,7 @@ videoFrameRate = 24
selectedPhysicalDeviceIndex = 7
[FilePath]
glTFModelFilePath = "./data/models/DamagedHelmet/DamagedHelmet.gltf"
glTFModelFilePath = "./data/models/kettle.glb"
envMapFilePath = "./data/environments/metro_noord_4k_hdr16f_cube.ktx"
imageOutputPath = "./data/output/imageSequence"
videoOutputPath = "./data/output/video"

File diff suppressed because one or more lines are too long

View File

@ -1492,10 +1492,10 @@ PlumageRender::PlumageRender()
float modelSize = std::max(models.scene.aabb[0][0], std::max(models.scene.aabb[1][1], models.scene.aabb[2][2]));
// Center and scale model
float scale = (1.0f / modelSize ) * 0.5f;
float scale = (1.0f / modelSize ) * 2.0f;
glm::vec3 translate = -glm::vec3(models.scene.aabb[3][0], models.scene.aabb[3][1], models.scene.aabb[3][2]);
translate += -0.5f * glm::vec3(models.scene.aabb[0][0], models.scene.aabb[1][1], models.scene.aabb[2][2]);
translate += -0.5f * settings.bottomCenter;
translate += -2.0f * glm::vec3(models.scene.aabb[0][0], models.scene.aabb[1][1], models.scene.aabb[2][2]);
translate += -2.0f * settings.bottomCenter;
//camera.setPosition(glm::vec3(0, 0, -modelSize - 2));
shaderDataScene.model = glm::mat4(1.0f);
@ -1510,13 +1510,14 @@ PlumageRender::PlumageRender()
shaderDataScene.model = glm::rotate(shaderDataScene.model, glm::radians(modelrot), glm::vec3(0, 1, 0));
}
/*
shaderDataScene.camPos = glm::vec3(
-camera.position.z * sin(glm::radians(camera.rotation.y)) * cos(glm::radians(camera.rotation.x)),
-camera.position.z * sin(glm::radians(camera.rotation.x)),
camera.position.z * cos(glm::radians(camera.rotation.y)) * cos(glm::radians(camera.rotation.x))
);
*/
shaderDataScene.camPos = settings.cameraTracks[]
// Skybox
@ -1544,7 +1545,10 @@ PlumageRender::PlumageRender()
camera.type = Camera::CameraType::lookat;
camera.setPerspective(45.0f, (float)settings.width / (float)settings.height, 0.1f, 256.0f);
camera.setProjectionMatrix(settings.fX,settings.fY,settings.cX,settings.cY,1.f, 256.f,settings.calibrationWidth,settings.calibrationHeight,true,true);
//camera.setPerspective(settings.fX, settings.fY, settings.cX, settings.cY, 1.0f, 256.0f);
camera.setTo(settings.bottomCenter);
camera.setUp(settings.bottomNormal);
camera.rotationSpeed = 0.25f;
camera.movementSpeed = 0.1f;