Skip to content

Commit

Permalink
Implement unit tests for Camera3D
Browse files Browse the repository at this point in the history
This commit adds unit tests for Camera3D. Methods tested are:
- is_position_behind
- is_position_in_frustum
- project_position
- unproject_position

A placeholder size variable is also added here to DisplayServerHeadless in order to make it possible to project points in headless mode.
  • Loading branch information
TechnoPorg committed Jan 14, 2023
1 parent 0abd60b commit 808bbfd
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 10 deletions.
2 changes: 2 additions & 0 deletions editor/plugins/node_3d_editor_gizmos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,8 @@ void Camera3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
Vector3 tup(0, up.y + hsize / 2, side.z);
ADD_TRIANGLE(tup + offset, side + up + offset, nside + up + offset);
} break;
case Camera3D::PROJECTION_TYPE_MAX: // Can't happen, but silences warning.
break;
}

#undef ADD_TRIANGLE
Expand Down
2 changes: 2 additions & 0 deletions scene/3d/camera_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ void Camera3D::_update_camera_mode() {
case PROJECTION_FRUSTUM: {
set_frustum(size, frustum_offset, near, far);
} break;
case PROJECTION_TYPE_MAX: // Can't happen, but silences warning.
break;
}
}

Expand Down
3 changes: 2 additions & 1 deletion scene/3d/camera_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class Camera3D : public Node3D {
enum ProjectionType {
PROJECTION_PERSPECTIVE,
PROJECTION_ORTHOGONAL,
PROJECTION_FRUSTUM
PROJECTION_FRUSTUM,
PROJECTION_TYPE_MAX
};

enum KeepAspect {
Expand Down
20 changes: 12 additions & 8 deletions servers/display_server_headless.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,20 @@ class DisplayServerHeadless : public DisplayServer {
static DisplayServer *create_func(const String &p_rendering_driver, DisplayServer::WindowMode p_mode, DisplayServer::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Error &r_error) {
r_error = OK;
RasterizerDummy::make_current();
return memnew(DisplayServerHeadless());
// Non-zero resolution values can be necessary for unit testing.
return memnew(DisplayServerHeadless(p_resolution));
}

Size2i size_dummy;

public:
bool has_feature(Feature p_feature) const override { return false; }
String get_name() const override { return "headless"; }

int get_screen_count() const override { return 0; }
int get_primary_screen() const override { return 0; };
Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override { return Point2i(); }
Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override { return Size2i(); }
Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override { return size_dummy; }
Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override { return Rect2i(); }
int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override { return 96; /* 0 might cause issues */ }
float screen_get_scale(int p_screen = SCREEN_OF_MAIN_WINDOW) const override { return 1; }
Expand Down Expand Up @@ -97,14 +100,14 @@ class DisplayServerHeadless : public DisplayServer {
void window_set_transient(WindowID p_window, WindowID p_parent) override {}

void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override {}
Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const override { return Size2i(); }
Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const override { return size_dummy; }

void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override {}
Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const override { return Size2i(); }
Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const override { return size_dummy; }

void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override {}
Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override { return Size2i(); }
Size2i window_get_size_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override { return Size2i(); }
void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override { size_dummy = p_size; }
Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override { return size_dummy; }
Size2i window_get_size_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override { return size_dummy; }

void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID) override {}
WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const override { return WINDOW_MODE_MINIMIZED; }
Expand All @@ -131,7 +134,8 @@ class DisplayServerHeadless : public DisplayServer {

void set_icon(const Ref<Image> &p_icon) override {}

DisplayServerHeadless() {}
DisplayServerHeadless(const Size2i &p_resolution) :
size_dummy(p_resolution) {}
~DisplayServerHeadless() {}
};

Expand Down
100 changes: 100 additions & 0 deletions tests/scene/test_camera_3d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**************************************************************************/
/* test_camera_3d.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef TEST_CAMERA_3D_H
#define TEST_CAMERA_3D_H

#include "scene/3d/camera_3d.h"
#include "scene/main/window.h"
#include "tests/test_macros.h"

namespace TestCamera3D {

TEST_CASE("[Camera][SceneTree] Camera 3D Relative Position Tests") {
Camera3D *camera = memnew(Camera3D);
SceneTree::get_singleton()->get_root()->add_child(camera);

Vector3 camera_origin = camera->get_camera_transform().get_origin();
Basis camera_basis = camera->get_camera_transform().basis;

// Test is_position_behind
CHECK_MESSAGE(camera->is_position_behind(camera_origin + camera_basis[2]), "A point directly in front of the camera should be behind the camera.");
CHECK_FALSE_MESSAGE(camera->is_position_behind(camera_origin - camera_basis[2]), "A point directly behind the camera should be behind the camera.");

// Test is_position_in_frustum
CHECK_MESSAGE(camera->is_position_in_frustum(camera_origin - camera_basis[2] * (1 + camera->get_near())), "A point directly in front of the camera should be inside the frustum.");
CHECK_FALSE_MESSAGE(camera->is_position_in_frustum(camera_origin - camera_basis[2] * camera->get_far()), "A point on the camera's far plane should not be considered inside the frustum.");

memdelete(camera);
}

TEST_CASE("[Camera][SceneTree] Camera 3D Projection Tests") {
Camera3D *camera = memnew(Camera3D);
SceneTree::get_singleton()->get_root()->add_child(camera);
Vector2 viewport_size = SceneTree::get_singleton()->get_root()->get_size();

// Get the names of enum values as human-readable strings.
List<StringName> projection_constant_names;
ClassDB::get_enum_constants(Camera3D::get_class_static(), "ProjectionType", &projection_constant_names);

// Iterate over projection types for each test.
for (int projection_int = 0; projection_int < Camera3D::ProjectionType::PROJECTION_TYPE_MAX; projection_int++) {
Camera3D::ProjectionType projection_type = (Camera3D::ProjectionType)projection_int;
camera->set_projection(projection_type);

bool frustum = true; // Test whether projected points are within the camera's frustum.
bool inverse = true; // Test whether projection and unprojection are inverse operations.

for (int i = 0; i < 10; i++) {
float z_depth = Math::random(camera->get_near(), camera->get_far());
Vector2 projection_point(viewport_size.x * Math::randf(), viewport_size.y * Math::randf());
Vector3 projected_point = camera->project_position(projection_point, z_depth);

if (!camera->is_position_in_frustum(projected_point)) {
frustum = false;
}
Vector2 unprojected_point = camera->unproject_position(projected_point);
// Fudging precision is needed here, because the values will never be *that* close to each other.
bool x_approx = projection_point.x == doctest::Approx(unprojected_point.x);
bool y_approx = projection_point.y == doctest::Approx(unprojected_point.y);
if (!(x_approx && y_approx)) {
inverse = false;
}
}
CHECK_MESSAGE(frustum, vformat("Points on screen projected within the camera's near and far distances with projection mode %s should be inside the camera's frustum.", projection_constant_names[projection_int]));
CHECK_MESSAGE(inverse, vformat("Points projected then unprojected with projection mode %s should be equal to the originals.", projection_constant_names[projection_int]));
}

memdelete(camera);
}

} //namespace TestCamera3D

#endif // TEST_CAMERA_3D_H
3 changes: 2 additions & 1 deletion tests/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
#include "tests/scene/test_arraymesh.h"
#include "tests/scene/test_audio_stream_wav.h"
#include "tests/scene/test_bit_map.h"
#include "tests/scene/test_camera_3d.h"
#include "tests/scene/test_code_edit.h"
#include "tests/scene/test_curve.h"
#include "tests/scene/test_gradient.h"
Expand Down Expand Up @@ -203,7 +204,7 @@ struct GodotTestCaseListener : public doctest::IReporter {
OS::get_singleton()->set_has_server_feature_callback(nullptr);
for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
if (String("headless") == DisplayServer::get_create_function_name(i)) {
DisplayServer::create(i, "", DisplayServer::WindowMode::WINDOW_MODE_MINIMIZED, DisplayServer::VSyncMode::VSYNC_ENABLED, 0, nullptr, Vector2i(0, 0), DisplayServer::SCREEN_PRIMARY, err);
DisplayServer::create(i, "", DisplayServer::WindowMode::WINDOW_MODE_MINIMIZED, DisplayServer::VSyncMode::VSYNC_ENABLED, 0, nullptr, Vector2i(1152, 648), DisplayServer::SCREEN_PRIMARY, err);
break;
}
}
Expand Down

0 comments on commit 808bbfd

Please sign in to comment.