作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目标:使用 OpenSceneGraph (OSG) 创建第三人称 View ,其中包括控制模型。该模型可以使用 WASD 和鼠标移动。单击鼠标右键时,相机和 View 应该会发生变化。按住鼠标右键时,模型和相机的方向应该相同。
我在这方面的工作已经有一段时间了,我可以同时改变模型和相机的方向,但我只能让相机显示俯 View 。我希望相机也能以其他角度(与世界平面成 0 - 90 度)渲染世界。
bool EventHandlerMouse::performMovementRightMouseButton(
const double eventTimeDelta,
const double dx,
const double dy
) {
// Read in the mouse movement to determine if the user is turning left
// or right. Then set the bool attribute of this->_screen_model to true
// for the direction that the user is moving and then sets the other to
// false. The else statement turns them both off.
if ( dx < -0.01 ) {
this->_screen_model->turn_left();
this->_screen_model->stop_turn_right();
}
else if ( dx > 0.01 ) {
this->_screen_model->turn_right();
this->_screen_model->stop_turn_left();
}
else {
this->_screen_model->stop_turn_right();
this->_screen_model->stop_turn_left();
}
// Get the model's orientation. Orientation is a float between 0.0 and
// 360.0 representing the models orientation. 0.0 and 360.0 are true
// north.
float local_o = this->_screen_model->get_orientation();
// Create the OSG variable to hold the camera manipulator's current and
// future rotation.
osg::Quat rotation, new_rotation;
osg::Vec3d eye;
// Retrieve the camera's current rotation and eye.
this->getTransformation( eye, rotation );
// Retrieve the camera's rotation matrix.
osg::Matrixd rotation_matrix;
rotation.get( rotation_matrix );
new_rotation = rotation_matrix.getRotate();
// I don't really know what makeRotate does. I couldn't find good
// documentation. From what I'm seeing, it takes the current attitude
// ( rotation ) and changes it to what you have specified. I think this
// is where my problem/solution will be. Here, I set the angle to the
// orientation of the model. I am then setting that angle to the z axis in
// the Vec3d. I think that this is what is giving me the top down view,
// but it does turn with the model. I don't know how I can change this so
// that it gives a view that would be at an over the shoulder type of
// angle similar to a typical 3rd person view.
new_rotation.makeRotate(
osg::DegreesToRadians( local_o ), Vec3d( 0.0, 0.0, 1.0 )
);
// Setting the camera's eye and rotation to the newly calculated
// perspective.
this->setTransformation( eye, new_rotation );
}
我对使用 OSG 还是很陌生。我正在阅读书籍并完成教程。我也一直在寻找如何做我想做的事,但我一直没有成功。如有任何帮助,我将不胜感激。如果我可以提供我的初出茅庐的应用程序的任何其他部分的代码片段,请告诉我您想看到的内容。
最佳答案
我最终通过相当简单的 NodeTrackManipulator 实现获得了预期的结果。我刚刚多写了一些鼠标处理,它符合我的需要。
event_handler_mouse.hpp
#ifndef EVENT_HANDLER_MOUSE_H
#define EVENT_HANDLER_MOUSE_H
#include <osgGA/GUIEventHandler>
#include <osgGA/NodeTrackerManipulator>
#include <osgViewer/View>
#include <iostream>
#include "../headers/client_obj_char.hpp"
using std::cout;
using std::endl;
class EventHandlerMouse : public osgGA::NodeTrackerManipulator {
public:
virtual bool performMovementLeftMouseButton(
const double eventTimeDelta,
const double dx,
const double dy
);
virtual bool performMovementRightMouseButton(
const double eventTimeDelta,
const double dx,
const double dy
);
virtual bool performMovementMiddleMouseButton(
const double eventTimeDelta,
const double dx,
const double dy
);
EventHandlerMouse( ClientObjChar* sm ) :
_client_char( sm ),
_distance( 1.0 )
{}
protected:
virtual ~EventHandlerMouse() {}
osg::Vec3 _center;
double _distance;
ClientObjChar* _client_char;
osg::observer_ptr<osg::Node> _target;
};
#endif
event_handler_mouse.cpp
#include "../headers/event_handler_mouse.hpp"
bool EventHandlerMouse::performMovementLeftMouseButton(
const double eventTimeDelta,
const double dx,
const double dy
) {
rotateWithFixedVertical( dx, dy );
return true;
}
bool EventHandlerMouse::performMovementRightMouseButton(
const double eventTimeDelta,
const double dx,
const double dy
) {
if ( true ) {
if ( dx < -0.01 ) {
this->_client_char->turn_left();
this->_client_char->stop_turn_right();
}
else if ( dx > 0.01 ) {
this->_client_char->turn_right();
this->_client_char->stop_turn_left();
}
else {
this->_client_char->stop_turn_right();
this->_client_char->stop_turn_left();
}
}
return true;
}
bool EventHandlerMouse::performMovementMiddleMouseButton(
const double eventTimeDelta,
const double dx,
const double dy
) {
return false;
}
关于c++ - OpenSceneGraph:第三人称视角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47145362/
我是一名优秀的程序员,十分优秀!