Finally fix the aspect ratio on the blasted FPS meter

This commit is contained in:
rdb 2015-01-07 22:34:43 +01:00
parent af15797a25
commit 6ce891f748
2 changed files with 40 additions and 16 deletions

View File

@ -30,10 +30,13 @@ TypeHandle FrameRateMeter::_type_handle;
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: FrameRateMeter::Constructor // Function: FrameRateMeter::Constructor
// Access: Published // Access: Published
// Description: // Description:
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
FrameRateMeter:: FrameRateMeter::
FrameRateMeter(const string &name) : TextNode(name) { FrameRateMeter(const string &name) :
TextNode(name),
_last_aspect_ratio(-1) {
set_cull_callback(); set_cull_callback();
Thread *current_thread = Thread::get_current_thread(); Thread *current_thread = Thread::get_current_thread();
@ -53,13 +56,13 @@ FrameRateMeter(const string &name) : TextNode(name) {
// the calculation within TextAssembler. // the calculation within TextAssembler.
PN_stdfloat height = 1.0f; PN_stdfloat height = 1.0f;
TextFont *font = get_font(); TextFont *font = get_font();
if (font != NULL){ if (font != NULL){
height = font->get_line_height() * 0.8; height = font->get_line_height() * 0.8;
} }
set_align(A_right); set_align(A_right);
set_transform(LMatrix4::scale_mat(frame_rate_meter_scale) * set_transform(LMatrix4::scale_mat(frame_rate_meter_scale) *
LMatrix4::translate_mat(LVector3::rfu(1.0f - frame_rate_meter_side_margins * frame_rate_meter_scale, 0.0f, 1.0f - frame_rate_meter_scale * height))); LMatrix4::translate_mat(LVector3::rfu(-frame_rate_meter_side_margins * frame_rate_meter_scale, 0.0f, -frame_rate_meter_scale * height)));
set_card_color(0.0f, 0.0f, 0.0f, 0.4); set_card_color(0.0f, 0.0f, 0.0f, 0.4);
set_card_as_margin(frame_rate_meter_side_margins, frame_rate_meter_side_margins, 0.1f, 0.0f); set_card_as_margin(frame_rate_meter_side_margins, frame_rate_meter_side_margins, 0.1f, 0.0f);
// set_usage_hint(Geom::UH_client); // set_usage_hint(Geom::UH_client);
@ -70,7 +73,7 @@ FrameRateMeter(const string &name) : TextNode(name) {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: FrameRateMeter::Destructor // Function: FrameRateMeter::Destructor
// Access: Published, Virtual // Access: Published, Virtual
// Description: // Description:
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
FrameRateMeter:: FrameRateMeter::
~FrameRateMeter() { ~FrameRateMeter() {
@ -99,25 +102,26 @@ setup_window(GraphicsOutput *window) {
_root.node()->set_attrib(dw, 1); _root.node()->set_attrib(dw, 1);
_root.set_material_off(1); _root.set_material_off(1);
_root.set_two_sided(1, 1); _root.set_two_sided(1, 1);
// Create a display region that covers the entire window. // Create a display region that covers the entire window.
_display_region = _window->make_mono_display_region(); _display_region = _window->make_mono_display_region();
_display_region->set_sort(frame_rate_meter_layer_sort); _display_region->set_sort(frame_rate_meter_layer_sort);
// Finally, we need a camera to associate with the display region. // Finally, we need a camera to associate with the display region.
PT(Camera) camera = new Camera("frame_rate_camera"); PT(Camera) camera = new Camera("frame_rate_camera");
NodePath camera_np = _root.attach_new_node(camera); NodePath camera_np = _root.attach_new_node(camera);
PT(Lens) lens = new OrthographicLens; PT(Lens) lens = new OrthographicLens;
static const PN_stdfloat left = -1.0f; // We choose these values such that we can place the text against (0, 0).
static const PN_stdfloat right = 1.0f; static const PN_stdfloat left = -2.0f;
static const PN_stdfloat bottom = -1.0f; static const PN_stdfloat right = 0.0f;
static const PN_stdfloat top = 1.0f; static const PN_stdfloat bottom = -2.0f;
static const PN_stdfloat top = 0.0f;
lens->set_film_size(right - left, top - bottom); lens->set_film_size(right - left, top - bottom);
lens->set_film_offset((right + left) * 0.5, (top + bottom) * 0.5); lens->set_film_offset((right + left) * 0.5, (top + bottom) * 0.5);
lens->set_near_far(-1000, 1000); lens->set_near_far(-1000, 1000);
camera->set_lens(lens); camera->set_lens(lens);
camera->set_scene(_root); camera->set_scene(_root);
_display_region->set_camera(camera_np); _display_region->set_camera(camera_np);
@ -170,7 +174,24 @@ cull_callback(CullTraverser *trav, CullTraverserData &data) {
// Statistics // Statistics
PStatTimer timer(_show_fps_pcollector, current_thread); PStatTimer timer(_show_fps_pcollector, current_thread);
// This is probably a good time to check if the aspect ratio on
// the window has changed.
int width = _display_region->get_pixel_width();
int height = _display_region->get_pixel_height();
PN_stdfloat aspect_ratio = 1;
if (width != 0 && height != 0) {
aspect_ratio = (PN_stdfloat)height / (PN_stdfloat)width;
}
// Scale the transform by the calculated aspect ratio.
if (aspect_ratio != _last_aspect_ratio) {
_aspect_ratio_transform = TransformState::make_scale(LVecBase3(aspect_ratio, 1, 1));
_last_aspect_ratio = aspect_ratio;
cerr << aspect_ratio << "\n";
}
data._net_transform = data._net_transform->compose(_aspect_ratio_transform);
// Check to see if it's time to update. // Check to see if it's time to update.
double now = _clock_object->get_frame_time(current_thread); double now = _clock_object->get_frame_time(current_thread);
double elapsed = now - _last_update; double elapsed = now - _last_update;

View File

@ -77,6 +77,9 @@ private:
string _text_pattern; string _text_pattern;
ClockObject *_clock_object; ClockObject *_clock_object;
PN_stdfloat _last_aspect_ratio;
CPT(TransformState) _aspect_ratio_transform;
static PStatCollector _show_fps_pcollector; static PStatCollector _show_fps_pcollector;
public: public: