openframeworks / openFrameworks

openFrameworks is a community-developed cross platform toolkit for creative coding in C++.
http://openframeworks.cc
Other
9.83k stars 2.56k forks source link

ofEasyCam : fix wrong scaling center in ortho mode #8026

Open hiroMTB opened 1 week ago

hiroMTB commented 1 week ago

This only happens with ofEasyCam's ortho mode + custom viewport.

Expected behavour

Expected behavour is to zoom in/out into the mouse pointer position when user scroll mouse wheel (or R click drag). It works fine with normal viewport with 0,0 position and full window w, h. But when we use custom viewport (e.g. camera.begin(viewport); ) then it zoom in/out into wrong position.

Issue

The problem is that ofEasyCam forgots to give viewport parameter to screenToWorld(). screenToWorld() calls ofCamera::getViewport() internaly but ofCamera seems not storing custom viewport parameter but just get current window's viewport, which is {0,0,fullW, fullH}. Hence it gives wrong world position.

Reproducing this bug

tested on

ofApp.h

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp {
public:
    void setup();
    void draw();
    void drawViewportOutline(const ofRectangle & viewport);

    ofRectangle viewport;
    ofEasyCam camera;
};

ofApp.cpp

#include "ofApp.h"

void ofApp::setup(){
    ofBackground(90);
    ofEnableSmoothing();

    camera.enableOrtho();
    camera.setNearClip(-1000000);
    camera.setFarClip(1000000);
    // camera.setVFlip(true);

    viewport.x = 500;
    viewport.y = 200;
    viewport.width = 500;
    viewport.height = 500;
}

void ofApp::draw(){

    drawViewportOutline(viewport);
    //camera.setControlArea(viewport);

    camera.begin(viewport);
    ofSetColor(255, 0, 0);
    ofFill();
    ofDrawCircle(0,0,0,10);
    ofDrawGrid(100);
    camera.end();
}

Before and after

Before

before

After

after