Skip to content

Commit

Permalink
Fix glTF camera aspect ratio (google#2807)
Browse files Browse the repository at this point in the history
  • Loading branch information
bejado authored Jul 13, 2020
1 parent c7b6d45 commit 7c4661c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
8 changes: 7 additions & 1 deletion filament/include/filament/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,13 @@ class UTILS_PUBLIC Camera : public FilamentAPI {
* First, pass an aspect of 1.0 to setProjection. Then set the scaling with the desired aspect
* ratio:
*
* camera->setScaling(double4 {1.0, width / height, 1.0, 1.0});
* const double aspect = width / height;
*
* // with Fov::HORIZONTAL passed to setProjection:
* camera->setScaling(double4 {1.0, aspect, 1.0, 1.0});
*
* // with Fov::VERTICAL passed to setProjection:
* camera->setScaling(double4 {1.0 / aspect, 1.0, 1.0, 1.0});
*
*
* By default, this is an identity matrix.
Expand Down
4 changes: 4 additions & 0 deletions libs/gltfio/include/gltfio/FilamentAsset.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class FilamentAsset {
* The camera's scaling matrix allows clients to adjust the aspect ratio independently from the
* camera's projection.
*
* To change the aspect ratio of the glTF camera:
*
* camera->setScaling(double4 {1.0 / newAspectRatio, 1.0, 1.0, 1.0});
*
* @see filament::Camera::setScaling
*/
const utils::Entity* getCameraEntities() const noexcept;
Expand Down
2 changes: 1 addition & 1 deletion libs/gltfio/src/AssetLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ void FAssetLoader::createCamera(const cgltf_camera* camera, Entity entity) {
const double aspect = projection.aspect_ratio > 0.0 ? projection.aspect_ratio : 1.0;

// Use the scaling matrix to set the aspect ratio, so clients can easily change it.
filamentCamera->setScaling(double4 {1.0, aspect, 1.0, 1.0});
filamentCamera->setScaling(double4 {1.0 / aspect, 1.0, 1.0, 1.0});
} else if (camera->type == cgltf_camera_type_orthographic) {
auto& projection = camera->data.orthographic;

Expand Down
4 changes: 2 additions & 2 deletions samples/gltf_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ int main(int argc, char** argv) {
}
const Viewport& vp = view->getViewport();
double aspectRatio = (double) vp.width / vp.height;
camera.setScaling(double4 {1.0, aspectRatio, 1.0, 1.0});
camera.setScaling(double4 {1.0 / aspectRatio, 1.0, 1.0, 1.0});
};

auto gui = [&app](Engine* engine, View* view) {
Expand Down Expand Up @@ -799,7 +799,7 @@ int main(int argc, char** argv) {
// camera to the viewport.
const Viewport& vp = view->getViewport();
double aspectRatio = (double) vp.width / vp.height;
c->setScaling(double4 {1.0, aspectRatio, 1.0, 1.0});
c->setScaling(double4 {1.0 / aspectRatio, 1.0, 1.0, 1.0});
} else {
// gltfCamera is out of bounds. Reset camera selection to main camera.
app.currentCamera = 0;
Expand Down

0 comments on commit 7c4661c

Please sign in to comment.