Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camera #198

Merged
merged 9 commits into from
Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
560 changes: 297 additions & 263 deletions game/camera.cpp

Large diffs are not rendered by default.

61 changes: 36 additions & 25 deletions game/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class Camera final {
Dive
};

void reset(World& world);
void reset();
void reset(const Npc* pl);

void save(Serialize &s);
void load(Serialize &s,Npc* pl);

Expand All @@ -54,7 +56,7 @@ class Camera final {

void toogleDebug();

void tick(const Npc& npc, uint64_t dt, bool inMove, bool includeRot);
void tick(uint64_t dt);
void debugDraw(DbgPainter& p);

Tempest::PointF spin() const;
Expand All @@ -63,7 +65,7 @@ class Camera final {
void setSpin(const Tempest::PointF& p);
void setDestSpin(const Tempest::PointF& p);

void setPosition(float x,float y,float z);
void setPosition(const Tempest::Vec3& pos);
void setDestPosition(const Tempest::Vec3& pos);

void setDialogDistance(float d);
Expand All @@ -77,44 +79,53 @@ class Camera final {

private:
struct State {
Tempest::Vec3 pos = {};
Tempest::Vec3 spin = {};
Tempest::Vec3 spin2 = {};
float range = 0.3f;
float range = 3.f;
Tempest::Vec3 spin = {};
Tempest::Vec3 target = {};
};

State state, dest;
float dlgDist = 0;
Tempest::Vec3 cameraPos = {};
Tempest::Vec3 origin = {};
Tempest::Vec3 rotOffset = {};
Tempest::Vec3 offsetAng = {};
State src, dst;

float dlgDist = 0;
float userRange = 0.13f;

Tempest::Matrix4x4 proj;
uint32_t vpWidth=0;
uint32_t vpHeight=0;

bool hasPos = false;
bool dbg = false;
bool tgEnable = true;
bool fpEnable = false;
bool lbEnable = false;
Mode camMod = Normal;
bool dbg = false;
bool tgEnable = true;
bool fpEnable = false;
bool lbEnable = false;
bool inertiaTarget = true;
Mode camMod = Normal;

mutable int raysCasted = 0;

Tempest::Vec3 applyModPosition(const Tempest::Vec3& pos);
Tempest::Vec3 applyModRotation(const Tempest::Vec3& spin);
static float maxDist;
static float baseSpeeed;
static float offsetAngleMul;

void calcControlPoints(float dtF);

Tempest::Vec3 calcTranslation(float dist) const;
Tempest::Vec3 calcOffsetAngles(const Tempest::Vec3& srcOrigin, const Tempest::Vec3& target) const;
Tempest::Vec3 calcOffsetAngles(Tempest::Vec3 srcOrigin, Tempest::Vec3 dstOrigin, Tempest::Vec3 target) const;
float calcCameraColision(const Tempest::Vec3& target, const Tempest::Vec3& origin, const Tempest::Vec3& rotSpin, float dist) const;

void implReset(const Npc& pl);
void implMove(Tempest::KeyEvent::KeyType t);
Tempest::Matrix4x4 mkView (const Tempest::Vec3& pos, float dist) const;
Tempest::Matrix4x4 mkView (const Tempest::Vec3& pos, const Tempest::Vec3& spin) const;
Tempest::Matrix4x4 mkRotation(const Tempest::Vec3& spin) const;
void clampRange(float& z);

void followPos(Tempest::Vec3& pos, Tempest::Vec3 dest, bool inMove, float dtF);
void followAng(Tempest::Vec3& spin, Tempest::Vec3 dest, float dtF);
static void followAng(float& ang,float dest,float speed);
void clampRotation(Tempest::Vec3& spin);

float calcCameraColision(const Tempest::Matrix4x4& view, const float dist) const;
void followCamera(Tempest::Vec3& pos, Tempest::Vec3 dest, float dtF);
void followPos (Tempest::Vec3& pos, Tempest::Vec3 dest, float dtF);
void followAng (Tempest::Vec3& spin, Tempest::Vec3 dest, float dtF);
static void followAng (float& ang, float dest, float speed, float dtF);

const Daedalus::GEngineClasses::CCamSys& cameraDef() const;
};
4 changes: 2 additions & 2 deletions game/game/gamesession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ GameSession::GameSession(std::string file) {
if(!testMode)
initScripts(true);
wrld->triggerOnStart(true);
cam->reset(*wrld);
cam->reset(wrld->player());
Gothic::inst().setLoadingProgress(96);
ticks = 1;
// wrld->setDayTime(8,0);
Expand Down Expand Up @@ -318,7 +318,7 @@ auto GameSession::implChangeWorld(std::unique_ptr<GameSession>&& game,
break;
}

cam->reset(*wrld);
cam->reset();
Log::i("Done loading world[",world,"]");
return std::move(game);
}
Expand Down
7 changes: 3 additions & 4 deletions game/game/playercontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ PlayerControl::PlayerControl(DialogMenu& dlg, InventoryMenu &inv)
:dlg(dlg),inv(inv) {
}

bool PlayerControl::isInMove() {
return ctrl[Action::Forward] | ctrl[Action::Left] | ctrl[Action::Right]; // | ctrl[Action::Back];
}

void PlayerControl::setTarget(Npc *other) {
auto w = Gothic::inst().world();
auto pl = w ? w->player() : nullptr;
Expand Down Expand Up @@ -369,6 +365,9 @@ void PlayerControl::marvinF8(uint64_t dt) {
pl.clearSpeed();
pl.quitIneraction();
pl.setAnim(AnimationSolver::Idle);

if(auto c = Gothic::inst().camera())
c->reset();
}

void PlayerControl::marvinK(uint64_t dt) {
Expand Down
1 change: 0 additions & 1 deletion game/game/playercontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class PlayerControl final {
bool interact(Item& item);

void clearInput();
bool isInMove();

void setTarget(Npc* other);
void actionFocus(Npc& other);
Expand Down
2 changes: 1 addition & 1 deletion game/game/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Serialize final {
public:
enum {
MinVersion = 0,
Version = 34
Version = 35
};

Serialize(Tempest::ODevice& fout);
Expand Down
13 changes: 9 additions & 4 deletions game/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,6 @@ void MainWindow::tickCamera(uint64_t dt) {

auto& camera = *pcamera;
const auto ws = player.weaponState();
const bool followCamera = player.isInMove();
const bool meleeFocus = (ws==WeaponState::Fist ||
ws==WeaponState::W1H ||
ws==WeaponState::W2H);
Expand Down Expand Up @@ -709,7 +708,7 @@ void MainWindow::tickCamera(uint64_t dt) {
return;
if(camera.isToogleEnabled())
camera.setMode(solveCameraMode());
camera.tick(*pl, dt, followCamera, (!mouseP[Event::ButtonLeft] || player.hasActionFocus() || fs));
camera.tick(dt);
renderer.setCameraView(camera);
}

Expand Down Expand Up @@ -866,8 +865,14 @@ void MainWindow::render(){

video.tick();
uint64_t dt = 0;
if(!video.isActive())
if(!video.isActive()) {
/*
Note: game update goes first
once player position is updated, we can update the camera
lastly - update animation (since cameraBone ca be moved)
*/
dt = tick();
}

auto& sync = fence[cmdId];
if(!sync.wait(0)) {
Expand All @@ -876,8 +881,8 @@ void MainWindow::render(){
}

if(!video.isActive()) {
Gothic::inst().updateAnimation(dt);
tickCamera(dt);
Gothic::inst().updateAnimation(dt);
}

if(video.isActive()) {
Expand Down
4 changes: 1 addition & 3 deletions game/ui/dialogmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ void DialogMenu::dialogCamera(Camera& camera) {
if(pl && other){
auto p0 = pl ->cameraBone();
auto p1 = other->cameraBone();
camera.setPosition(0.5f*(p0.x+p1.x),
0.5f*(p0.y+p1.y) + 50,
0.5f*(p0.z+p1.z));
camera.setPosition((p0+p1)*0.5f + Vec3(0,50,0));
p0 -= p1;

if(pl==other) {
Expand Down