Merge pull request #21 from andreja6/MusicalProgrammer
Musical programmer
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
#include "Demo.h"
|
||||
#include "AudioPlayer.h"
|
||||
|
||||
|
||||
|
||||
CameraController::CameraController(){
|
||||
yaw=0;
|
||||
pitch=0;
|
||||
@@ -13,8 +15,9 @@ CameraController::CameraController(){
|
||||
backwards=false;
|
||||
left=false;
|
||||
right=false;
|
||||
zoom=7.f;
|
||||
rightButtonHolding=false;
|
||||
|
||||
focusPosition=Vector3(0,0,0);
|
||||
}
|
||||
|
||||
GCamera* CameraController::getCamera()
|
||||
@@ -23,13 +26,10 @@ GCamera* CameraController::getCamera()
|
||||
}
|
||||
|
||||
void CameraController::lookAt(const Vector3& position) {
|
||||
//g3dCamera.lookAt(position,g3dCamera.getCoordinateFrame().upVector());
|
||||
|
||||
const Vector3 look = (position - g3dCamera.getCoordinateFrame().translation);
|
||||
yaw = aTan2(look.x, -look.z);
|
||||
|
||||
pitch = -aTan2(look.y, distance(look.x, look.z));
|
||||
std::cout << distance(look.x, look.z) << "pitch:" << pitch << std::endl;
|
||||
CoordinateFrame frame = g3dCamera.getCoordinateFrame().translation;
|
||||
frame.rotation = Matrix3::fromEulerAnglesZYX(0, -yaw, -pitch);
|
||||
g3dCamera.setCoordinateFrame(frame);
|
||||
@@ -39,6 +39,7 @@ void CameraController::setFrame(const CoordinateFrame& cf) {
|
||||
Vector3 look = cf.getLookVector();
|
||||
g3dCamera.setCoordinateFrame(cf);
|
||||
lookAt(cf.translation + look);
|
||||
focusPosition=cf.translation+cf.lookVector()*zoom;
|
||||
}
|
||||
|
||||
CoordinateFrame CameraController::getCoordinateFrame() {
|
||||
@@ -48,77 +49,80 @@ CoordinateFrame CameraController::getCoordinateFrame() {
|
||||
return cf;
|
||||
}
|
||||
|
||||
void CameraController::pan(int spdX, int spdY)
|
||||
void CameraController::pan(CoordinateFrame* frame,float spdX, float spdY)
|
||||
{
|
||||
yaw+=spdX;
|
||||
pitch+=spdY;
|
||||
|
||||
if (pitch>1.4f) pitch=1.4f;
|
||||
if (pitch<-1.4f) pitch=-1.4f;
|
||||
frame->translation = Vector3(sin(-yaw)*zoom*cos(pitch),sin(pitch)*zoom,cos(-yaw)*zoom*cos(pitch))+focusPosition;
|
||||
frame->lookAt(focusPosition);
|
||||
}
|
||||
bool CameraController::onMouseWheel(int x, int y, short delta)
|
||||
{
|
||||
CoordinateFrame frame = g3dCamera.getCoordinateFrame();
|
||||
|
||||
if (delta>0) { // Mouse wheel up
|
||||
g3dCamera.setCoordinateFrame(g3dCamera.getCoordinateFrame() + frame.lookVector()*2);
|
||||
if (zoom>CAM_ZOOM_MIN)
|
||||
frame = frame+frame.lookVector()*(zoom/5);
|
||||
}
|
||||
else {
|
||||
g3dCamera.setCoordinateFrame(g3dCamera.getCoordinateFrame() - frame.lookVector()*2);
|
||||
if (zoom<CAM_ZOOM_MAX)
|
||||
frame = frame-frame.lookVector()*(zoom/5);
|
||||
}
|
||||
|
||||
zoom=(frame.translation-focusPosition).magnitude();
|
||||
if (zoom<CAM_ZOOM_MIN) zoom=CAM_ZOOM_MIN;
|
||||
if (zoom>CAM_ZOOM_MAX) zoom=CAM_ZOOM_MAX;
|
||||
|
||||
setFrame(frame);
|
||||
return true;
|
||||
}
|
||||
|
||||
void CameraController::panLeft()
|
||||
{
|
||||
CoordinateFrame frame = g3dCamera.getCoordinateFrame();
|
||||
float y = frame.translation.y;
|
||||
CoordinateFrame frame2 = CoordinateFrame(frame.rotation, frame.translation + frame.lookVector()*25);
|
||||
Vector3 focus = Vector3(0,0,0); //frame.translation+frame.lookVector()*25;
|
||||
frame2 = frame2 * Matrix3::fromEulerAnglesXYZ(0,toRadians(-45),0);
|
||||
frame2 = frame2 - frame2.lookVector()*25;
|
||||
Vector3 cameraPos = Vector3(frame2.translation.x, y, frame2.translation.z);
|
||||
CoordinateFrame newFrame = CoordinateFrame(frame2.rotation, Vector3(frame2.translation.x, y, frame2.translation.z));
|
||||
newFrame.lookAt(focus,frame2.upVector());
|
||||
setFrame(CoordinateFrame(focus));
|
||||
pan(&frame,toRadians(-45),0);
|
||||
setFrame(frame);
|
||||
|
||||
}
|
||||
void CameraController::panRight()
|
||||
{
|
||||
CoordinateFrame frame = g3dCamera.getCoordinateFrame();
|
||||
float y = frame.translation.y;
|
||||
CoordinateFrame frame2 = CoordinateFrame(frame.rotation, frame.translation + frame.lookVector()*25);
|
||||
Vector3 focus = frame.translation+frame.lookVector()*25;
|
||||
frame2 = frame2 * Matrix3::fromEulerAnglesXYZ(0,toRadians(45),0);
|
||||
frame2 = frame2 - frame2.lookVector()*25;
|
||||
Vector3 cameraPos = Vector3(frame2.translation.x, y, frame2.translation.z);
|
||||
CoordinateFrame newFrame = CoordinateFrame(frame2.rotation, Vector3(frame2.translation.x, y, frame2.translation.z));
|
||||
newFrame.lookAt(focus);
|
||||
setFrame(newFrame);
|
||||
pan(&frame,toRadians(45),0);
|
||||
setFrame(frame);
|
||||
}
|
||||
|
||||
void CameraController::tiltUp()
|
||||
{
|
||||
|
||||
CoordinateFrame frame = CoordinateFrame(g3dCamera.getCoordinateFrame().rotation, g3dCamera.getCoordinateFrame().translation);
|
||||
Vector3 camerapoint = frame.translation;
|
||||
|
||||
Vector3 focalPoint = camerapoint + frame.lookVector() * 25;
|
||||
float distance = pow(pow((double)focalPoint.x - (double)camerapoint.x, 2) + pow((double)camerapoint.y - (double)camerapoint.y, 2) + pow((double)focalPoint.z - (double)camerapoint.z, 2), 0.5);
|
||||
float x = distance * cos(22.5 * G3D::pi() / 180) + focalPoint.x;
|
||||
float z = distance * sin(22.5 * G3D::pi() / 180) + focalPoint.z;
|
||||
camerapoint = Vector3(camerapoint.x, camerapoint.y+2, camerapoint.z);
|
||||
|
||||
CoordinateFrame newFrame = CoordinateFrame(camerapoint);
|
||||
newFrame.lookAt(focalPoint);
|
||||
Vector3 cameraPos = camerapoint;
|
||||
frame = newFrame;
|
||||
setFrame(newFrame);
|
||||
pan(&frame,0,toRadians(25));
|
||||
setFrame(frame);
|
||||
}
|
||||
void CameraController::tiltDown()
|
||||
{
|
||||
CoordinateFrame frame = CoordinateFrame(g3dCamera.getCoordinateFrame().rotation, g3dCamera.getCoordinateFrame().translation);
|
||||
pan(&frame,0,toRadians(-25));
|
||||
setFrame(frame);
|
||||
}
|
||||
|
||||
void CameraController::centerCamera(Instance* selection)
|
||||
{
|
||||
CoordinateFrame frame = CoordinateFrame(g3dCamera.getCoordinateFrame().translation);
|
||||
if(selection == NULL)
|
||||
{
|
||||
lookAt(Vector3(0,0,0));
|
||||
focusPosition=Vector3(0,0,0);
|
||||
}
|
||||
else
|
||||
lookAt(((PhysicalInstance*)selection)->getPosition()/2);
|
||||
{
|
||||
Vector3 partPos = ((PhysicalInstance*)selection)->getPosition()/2;
|
||||
lookAt(partPos);
|
||||
focusPosition=partPos;
|
||||
zoom=((partPos-frame.translation).magnitude());
|
||||
}
|
||||
}
|
||||
|
||||
void CameraController::update(Demo* demo)
|
||||
@@ -127,18 +131,22 @@ void CameraController::update(Demo* demo)
|
||||
|
||||
Vector3 cameraPos = g3dCamera.getCoordinateFrame().translation;
|
||||
CoordinateFrame frame = g3dCamera.getCoordinateFrame();
|
||||
|
||||
bool moving=false;
|
||||
if(GetHoldKeyState('U')) {
|
||||
forwards = true;
|
||||
moving=true;
|
||||
}
|
||||
if(GetHoldKeyState('J')) {
|
||||
backwards = true;
|
||||
moving=true;
|
||||
}
|
||||
if(GetHoldKeyState('H')) {
|
||||
left = true;
|
||||
moving=true;
|
||||
}
|
||||
if(GetHoldKeyState('K')) {
|
||||
right = true;
|
||||
moving=true;
|
||||
}
|
||||
|
||||
if(forwards) {
|
||||
@@ -158,16 +166,17 @@ void CameraController::update(Demo* demo)
|
||||
frame.translation += frame.rightVector()*moveRate;
|
||||
}
|
||||
|
||||
if (moving)
|
||||
{
|
||||
zoom=7;
|
||||
focusPosition=frame.translation+frame.lookVector()*zoom;
|
||||
}
|
||||
|
||||
if(rightButtonHolding) {
|
||||
POINT mouse;
|
||||
GetCursorPos(&mouse);
|
||||
|
||||
yaw+=(mouse.x-oldDesktopMouse.x)/100.f;
|
||||
pitch+=(mouse.y-oldDesktopMouse.y)/100.f;
|
||||
|
||||
pan(&frame,(mouse.x-oldDesktopMouse.x)/100.f,(mouse.y-oldDesktopMouse.y)/100.f);
|
||||
SetCursorPos(oldDesktopMouse.x,oldDesktopMouse.y);
|
||||
frame.rotation = Matrix3::fromEulerAnglesZYX(0, -yaw, -pitch);
|
||||
}
|
||||
|
||||
if(GetHoldKeyState(VK_RSHIFT) || GetHoldKeyState(VK_LSHIFT)) {
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
#include "Instance.h"
|
||||
#include <string>
|
||||
|
||||
#define CAM_ZOOM_MIN 0.1f
|
||||
#define CAM_ZOOM_MAX 100.f
|
||||
|
||||
class Demo;
|
||||
|
||||
class CameraController {
|
||||
@@ -13,7 +16,7 @@ class CameraController {
|
||||
|
||||
void setFrame(const CoordinateFrame& cf);
|
||||
void lookAt(const Vector3& position);
|
||||
void pan(int spdX,int spdY);
|
||||
void pan(CoordinateFrame* frame,float spdX,float spdY);
|
||||
void update(Demo* demo);
|
||||
void centerCamera(Instance* selection);
|
||||
void panLeft();
|
||||
@@ -28,6 +31,7 @@ class CameraController {
|
||||
float yaw;
|
||||
float pitch;
|
||||
float moveRate;
|
||||
float zoom;
|
||||
bool forwards;
|
||||
bool backwards;
|
||||
bool left;
|
||||
|
||||
18
main.cpp
18
main.cpp
@@ -180,6 +180,8 @@ void CameraButtonListener::onButton1MouseClick(BaseButtonInstance* button)
|
||||
usableApp->cameraController.panLeft();
|
||||
else if(button->name == "TiltUp")
|
||||
usableApp->cameraController.tiltUp();
|
||||
else if(button->name == "TiltDown")
|
||||
usableApp->cameraController.tiltDown();
|
||||
}
|
||||
|
||||
class GUDButtonListener : public ButtonListener {
|
||||
@@ -612,7 +614,7 @@ void Demo::initGUI()
|
||||
void Demo::onInit() {
|
||||
|
||||
// Called before Demo::run() beings
|
||||
cameraController.getCamera()->setCoordinateFrame(cameraPos);
|
||||
cameraController.setFrame(cameraPos);
|
||||
dataModel = new DataModelInstance();
|
||||
dataModel->setParent(NULL);
|
||||
dataModel->name = "undefined";
|
||||
@@ -1140,10 +1142,6 @@ void Demo::onGraphics(RenderDevice* rd) {
|
||||
|
||||
void Demo::onKeyPressed(int key)
|
||||
{
|
||||
if (key=='A')
|
||||
{
|
||||
std::cout << "A PRESS" << std::endl;
|
||||
}
|
||||
if(key==VK_DELETE)
|
||||
{
|
||||
deleteInstance();
|
||||
@@ -1151,10 +1149,7 @@ void Demo::onKeyPressed(int key)
|
||||
}
|
||||
void Demo::onKeyUp(int key)
|
||||
{
|
||||
if (key=='A')
|
||||
{
|
||||
std::cout << "A UP" << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Demo::onMouseLeftPressed(int x,int y)
|
||||
@@ -1205,7 +1200,7 @@ void Demo::onMouseLeftPressed(int x,int y)
|
||||
}
|
||||
void Demo::onMouseLeftUp(int x,int y)
|
||||
{
|
||||
std::cout << "Release: " << x << "," << y << std::endl;
|
||||
//std::cout << "Release: " << x << "," << y << std::endl;
|
||||
|
||||
dragging = false;
|
||||
//message = "Dragging = false.";
|
||||
@@ -1226,16 +1221,13 @@ void Demo::onMouseLeftUp(int x,int y)
|
||||
}
|
||||
void Demo::onMouseRightPressed(int x,int y)
|
||||
{
|
||||
std::cout << "Click: " << x << "," << y << std::endl;
|
||||
}
|
||||
void Demo::onMouseRightUp(int x,int y)
|
||||
{
|
||||
std::cout << "Release: " << x << "," << y << std::endl;
|
||||
}
|
||||
void Demo::onMouseMoved(int x,int y)
|
||||
{
|
||||
oldMouse = dataModel->getMousePos();
|
||||
std::cout << "Moved: " << x << "," << y << std::endl;
|
||||
dataModel->mousex = x;
|
||||
dataModel->mousey = y;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user