Archived
1
Fork 0

Properly clean up cinematics, and fix model positioning

This commit is contained in:
Joshua Goins 2018-12-15 07:26:40 -05:00
parent 0f9e46637e
commit 41e2be33ab
5 changed files with 77 additions and 9 deletions

View file

@ -81,17 +81,31 @@
} }
], ],
"animations": [ "animations": [
{
"target": "suzanne",
"property": "position",
"keyframes": [
{
"time": 5,
"value": "0,5,0"
},
{
"time": 10,
"value": "0,5,0"
}
]
},
{ {
"target": "camera", "target": "camera",
"property": "position", "property": "position",
"keyframes": [ "keyframes": [
{ {
"time": 5, "time": 5,
"value": "-5,0,2" "value": "-5,5,2"
}, },
{ {
"time": 10, "time": 10,
"value": "5,0,2" "value": "5,5,2"
} }
] ]
}, },
@ -108,6 +122,20 @@
"value": 75 "value": 75
} }
] ]
},
{
"target": "camera",
"property": "target",
"keyframes": [
{
"time": 5,
"value": "0,5,0"
},
{
"time": 10,
"value": "0,5,0"
}
]
} }
] ]
} }

View file

@ -13,6 +13,7 @@ struct Keyframe {
enum class AnimationProperty { enum class AnimationProperty {
Position, Position,
Target,
FoV FoV
}; };

View file

@ -15,7 +15,7 @@ struct Vertex {
class Mesh { class Mesh {
public: public:
std::string name; std::string name, tag;
glm::vec3 position; glm::vec3 position;
Material* material = nullptr; Material* material = nullptr;

View file

@ -214,12 +214,15 @@ Cinematic* loadCinematic(const std::string& path) {
shot->start = shotObject["start"]; shot->start = shotObject["start"];
shot->end = shotObject["end"]; shot->end = shotObject["end"];
// TODO: concurrent worlds not implemented, we just load the first one
if(world == nullptr)
world = loadWorld(shotObject["world"]); world = loadWorld(shotObject["world"]);
for(auto meshObject : shotObject["meshes"]) { for(auto meshObject : shotObject["meshes"]) {
Mesh* mesh = loadMesh(meshObject["path"]); Mesh* mesh = loadMesh(meshObject["path"]);
mesh->name = meshObject["name"]; mesh->name = meshObject["name"];
mesh->material = loadMaterial(meshObject["material"]); mesh->material = loadMaterial(meshObject["material"]);
mesh->tag = "cinematic";
shot->meshes.push_back(mesh); shot->meshes.push_back(mesh);
} }
@ -234,6 +237,8 @@ Cinematic* loadCinematic(const std::string& path) {
const auto property = animationObject["property"]; const auto property = animationObject["property"];
if(property == "position") if(property == "position")
animation->property = AnimationProperty::Position; animation->property = AnimationProperty::Position;
else if(property == "target")
animation->property = AnimationProperty::Target;
else if(property == "fov") else if(property == "fov")
animation->property = AnimationProperty::FoV; animation->property = AnimationProperty::FoV;
@ -241,7 +246,7 @@ Cinematic* loadCinematic(const std::string& path) {
Keyframe keyframe; Keyframe keyframe;
keyframe.time = keyframeObject["time"]; keyframe.time = keyframeObject["time"];
if(animation->property == AnimationProperty::Position) { if(animation->property == AnimationProperty::Position || animation->property == AnimationProperty::Target) {
auto tokens = tokenize(keyframeObject["value"]); auto tokens = tokenize(keyframeObject["value"]);
keyframe.valueVec3[0] = atof(tokens[0].c_str()); keyframe.valueVec3[0] = atof(tokens[0].c_str());
@ -360,7 +365,20 @@ int main(int argc, char* argv[]) {
if(animationTime >= shot->start && animationTime < shot->end && currentShot != shot) { if(animationTime >= shot->start && animationTime < shot->end && currentShot != shot) {
if(currentShot != nullptr) { if(currentShot != nullptr) {
world->meshes.clear(); for(auto mesh : world->meshes) {
if(mesh->tag == "cinematic") {
renderer->destroyMaterialBuffers(mesh->material);
delete mesh->material;
renderer->destroyMeshBuffers(mesh);
delete mesh;
}
}
world->meshes.erase(std::remove_if(world->meshes.begin(),
world->meshes.end(),
[](Mesh* m){return m->tag == "cinematic";}),
world->meshes.end());
} }
for(auto mesh : shot->meshes) for(auto mesh : shot->meshes)
@ -399,13 +417,23 @@ int main(int argc, char* argv[]) {
if(animation->target == nullptr) if(animation->target == nullptr)
camera.position = pos; camera.position = pos;
else else {
animation->target->position = pos; animation->target->position = pos;
} }
}
break;
case AnimationProperty::Target:
{
auto pos = currentFrame.valueVec3 + delta * (nextFrame.valueVec3 - currentFrame.valueVec3);
camera.target = pos;
}
break; break;
case AnimationProperty::FoV: case AnimationProperty::FoV:
{
auto pos = currentFrame.valueInt + delta * (nextFrame.valueInt - currentFrame.valueInt); auto pos = currentFrame.valueInt + delta * (nextFrame.valueInt - currentFrame.valueInt);
camera.fov = pos; camera.fov = pos;
}
break; break;
} }
} }
@ -454,6 +482,16 @@ int main(int argc, char* argv[]) {
} }
} }
for(auto mesh : world->meshes) {
renderer->destroyMaterialBuffers(mesh->material);
delete mesh->material;
renderer->destroyMeshBuffers(mesh);
delete mesh;
}
delete world;
renderer->destroyRenderTarget(target); renderer->destroyRenderTarget(target);
vkDestroySurfaceKHR(renderer->getInstance(), surface, nullptr); vkDestroySurfaceKHR(renderer->getInstance(), surface, nullptr);

View file

@ -63,6 +63,7 @@ void WorldPass::render(VkCommandBuffer commandBuffer, World& world, Camera& came
glm::mat4 vp; glm::mat4 vp;
vp = glm::perspective(glm::radians(camera.fov), (float)target->extent.width / target->extent.height, camera.near, camera.far); vp = glm::perspective(glm::radians(camera.fov), (float)target->extent.width / target->extent.height, camera.near, camera.far);
vp *= glm::lookAt(camera.position, camera.target, glm::vec3(0, -1, 0)); vp *= glm::lookAt(camera.position, camera.target, glm::vec3(0, -1, 0));
vp = glm::translate(vp, mesh->position);
vkCmdPushConstants(commandBuffer, pipelineLayout_, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::mat4), &vp); vkCmdPushConstants(commandBuffer, pipelineLayout_, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::mat4), &vp);