Skip to content
Browse files

added pitch and roll

  • Loading branch information...
1 parent 5c4b5fa commit 05e87781a5dbca5c18eaf7288ef43345d4372311 @obviousjim obviousjim committed Jul 26, 2011
Showing with 198 additions and 37 deletions.
  1. +1 −0 .gitignore
  2. +48 −1 example/GameCameraExample.xcodeproj/project.pbxproj
  3. +135 −36 src/ofxGameCamera.cpp
  4. +14 −0 src/ofxGameCamera.h
View
1 .gitignore
@@ -40,6 +40,7 @@ libs/openFrameworks/Release Mac OS X
apps/*/*/Debug Mac OS X
apps/*/*/Release Mac OS X
.DS_Store
+*.perspectivev3
#Linux
addons/Debug Linux
View
49 example/GameCameraExample.xcodeproj/project.pbxproj
@@ -25,6 +25,10 @@
E4C2424910CC5A17004149E2 /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E4C2424610CC5A17004149E2 /* IOKit.framework */; };
E4EB6799138ADC1D00A09F29 /* GLUT.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BBAB23BE13894E4700AA2426 /* GLUT.framework */; };
E76B8EC613DF42650091E482 /* ofxGameCamera.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E76B8EC413DF42650091E482 /* ofxGameCamera.cpp */; };
+ E76B8F1513DFA1E50091E482 /* tinyxml.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E76B8F0E13DFA1E50091E482 /* tinyxml.cpp */; };
+ E76B8F1613DFA1E50091E482 /* tinyxmlerror.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E76B8F1013DFA1E50091E482 /* tinyxmlerror.cpp */; };
+ E76B8F1713DFA1E50091E482 /* tinyxmlparser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E76B8F1113DFA1E50091E482 /* tinyxmlparser.cpp */; };
+ E76B8F1813DFA1E50091E482 /* ofxXmlSettings.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E76B8F1313DFA1E50091E482 /* ofxXmlSettings.cpp */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -81,6 +85,13 @@
E4EB6923138AFD0F00A09F29 /* Project.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Project.xcconfig; sourceTree = "<group>"; };
E76B8EC413DF42650091E482 /* ofxGameCamera.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofxGameCamera.cpp; sourceTree = "<group>"; };
E76B8EC513DF42650091E482 /* ofxGameCamera.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofxGameCamera.h; sourceTree = "<group>"; };
+ E76B8F0C13DFA1E50091E482 /* install.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = install.xml; sourceTree = "<group>"; };
+ E76B8F0E13DFA1E50091E482 /* tinyxml.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = tinyxml.cpp; sourceTree = "<group>"; };
+ E76B8F0F13DFA1E50091E482 /* tinyxml.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tinyxml.h; sourceTree = "<group>"; };
+ E76B8F1013DFA1E50091E482 /* tinyxmlerror.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = tinyxmlerror.cpp; sourceTree = "<group>"; };
+ E76B8F1113DFA1E50091E482 /* tinyxmlparser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = tinyxmlparser.cpp; sourceTree = "<group>"; };
+ E76B8F1313DFA1E50091E482 /* ofxXmlSettings.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofxXmlSettings.cpp; sourceTree = "<group>"; };
+ E76B8F1413DFA1E50091E482 /* ofxXmlSettings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofxXmlSettings.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -111,6 +122,7 @@
BB4B014C10F69532006C3DED /* addons */ = {
isa = PBXGroup;
children = (
+ E76B8F0B13DFA1E50091E482 /* ofxXmlSettings */,
E76B8EBF13DF42570091E482 /* ofxGameCamera */,
);
name = addons;
@@ -203,13 +215,44 @@
E76B8EC313DF42650091E482 /* src */ = {
isa = PBXGroup;
children = (
- E76B8EC413DF42650091E482 /* ofxGameCamera.cpp */,
E76B8EC513DF42650091E482 /* ofxGameCamera.h */,
+ E76B8EC413DF42650091E482 /* ofxGameCamera.cpp */,
);
name = src;
path = ../src;
sourceTree = SOURCE_ROOT;
};
+ E76B8F0B13DFA1E50091E482 /* ofxXmlSettings */ = {
+ isa = PBXGroup;
+ children = (
+ E76B8F0C13DFA1E50091E482 /* install.xml */,
+ E76B8F0D13DFA1E50091E482 /* libs */,
+ E76B8F1213DFA1E50091E482 /* src */,
+ );
+ name = ofxXmlSettings;
+ path = ../../ofxXmlSettings;
+ sourceTree = SOURCE_ROOT;
+ };
+ E76B8F0D13DFA1E50091E482 /* libs */ = {
+ isa = PBXGroup;
+ children = (
+ E76B8F0E13DFA1E50091E482 /* tinyxml.cpp */,
+ E76B8F0F13DFA1E50091E482 /* tinyxml.h */,
+ E76B8F1013DFA1E50091E482 /* tinyxmlerror.cpp */,
+ E76B8F1113DFA1E50091E482 /* tinyxmlparser.cpp */,
+ );
+ path = libs;
+ sourceTree = "<group>";
+ };
+ E76B8F1213DFA1E50091E482 /* src */ = {
+ isa = PBXGroup;
+ children = (
+ E76B8F1313DFA1E50091E482 /* ofxXmlSettings.cpp */,
+ E76B8F1413DFA1E50091E482 /* ofxXmlSettings.h */,
+ );
+ path = src;
+ sourceTree = "<group>";
+ };
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@@ -296,6 +339,10 @@
E4B69E200A3A1BDC003C02F2 /* main.cpp in Sources */,
E4B69E210A3A1BDC003C02F2 /* testApp.cpp in Sources */,
E76B8EC613DF42650091E482 /* ofxGameCamera.cpp in Sources */,
+ E76B8F1513DFA1E50091E482 /* tinyxml.cpp in Sources */,
+ E76B8F1613DFA1E50091E482 /* tinyxmlerror.cpp in Sources */,
+ E76B8F1713DFA1E50091E482 /* tinyxmlparser.cpp in Sources */,
+ E76B8F1813DFA1E50091E482 /* ofxXmlSettings.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
171 src/ofxGameCamera.cpp
@@ -22,63 +22,162 @@ ofxGameCamera::ofxGameCamera()
{
sensitivityX = 0.15f;
sensitivityY = 0.15f;
-
+
minimumX = -360.0f;
maximumX = 360.0f;
-
+
minimumY = -60.0f;
maximumY = 60.0f;
-
+
rotationX = 0.0f;
rotationY = 0.0f;
+ rotationZ = 0.0f;
+
+ speed = 10.0f;
+
+ lastMouse = ofVec2f(0,0);
- speed = 1.0;
+ usemouse = true;
+ autosavePosition = false;
+ useArrowKeys = true;
+
+ cameraPositionFile = "_gameCameraPosition.xml";
}
void ofxGameCamera::begin(ofRectangle rect)
-{
+{
+ ofVec3f startPos = getPosition();
+ ofVec2f startRot = ofVec3f(rotationX, rotationY, rotationZ);
+
//forward
- if(ofGetKeyPressed('w')){
- dolly(-speed);
- }
-
- if(ofGetKeyPressed('s')){
- dolly(speed);
- }
-
- if(ofGetKeyPressed('a')){
- truck(-speed);
- }
-
- if(ofGetKeyPressed('d')){
- truck(speed);
- }
+ if(usemouse){
+ if(ofGetKeyPressed('w') || (useArrowKeys && ofGetKeyPressed(OF_KEY_UP)) ){
+ dolly(-speed);
+ }
- if(ofGetKeyPressed('c')){
- boom(-speed);
- }
+ if(ofGetKeyPressed('s') || (useArrowKeys && ofGetKeyPressed(OF_KEY_DOWN)) ){
+ dolly(speed);
+ }
+
+ if(ofGetKeyPressed('a') || (useArrowKeys && ofGetKeyPressed(OF_KEY_LEFT)) ){
+ truck(-speed);
+ }
- if(ofGetKeyPressed('e')){
- boom(speed);
+ if(ofGetKeyPressed('d') || (useArrowKeys && ofGetKeyPressed(OF_KEY_RIGHT)) ){
+ truck(speed);
+ }
+
+ if(ofGetKeyPressed('c') || (useArrowKeys && ofGetKeyPressed(OF_KEY_PAGE_DOWN)) ){
+ boom(-speed);
+ }
+
+ if(ofGetKeyPressed('e') || (useArrowKeys && ofGetKeyPressed(OF_KEY_PAGE_UP)) ){
+ boom(speed);
+ }
+
+ //TODO make variable
+ if(ofGetKeyPressed('r')){
+ //roll(.01);
+ rotationZ += 0.1;
+ updateRotation();
+ }
+ if(ofGetKeyPressed('q')){
+ rotationZ -= 0.1;
+ updateRotation();
+ }
+
}
-
+
ofVec2f mouse( ofGetMouseX(), ofGetMouseY() );
-
- if(ofGetMousePressed(0)){
-
+
+ if(usemouse && ofGetMousePressed(0)){
+
rotationX += (mouse.x - lastMouse.x) * sensitivityX;
rotationX = ClampAngle(rotationX, minimumX, maximumX);
-
+
rotationY += (mouse.y - lastMouse.y) * sensitivityY;
rotationY = ClampAngle(rotationY, minimumY, maximumY);
-
- setOrientation(ofQuaternion()); //reset
- rotate(ofQuaternion(-rotationX, getYAxis()));
- rotate(ofQuaternion(-rotationY, getXAxis()));
+
+ updateRotation();
}
-
+
lastMouse = mouse;
-
+
+ if(autosavePosition && (startPos != getPosition() || startRot != ofVec3f(rotationX, rotationY, rotationZ))){
+ saveCameraPosition();
+ }
+
ofCamera::begin(rect);
}
+void ofxGameCamera::updateRotation()
+{
+ setOrientation(ofQuaternion()); //reset
+ rotate(ofQuaternion(-rotationX, getYAxis()));
+ rotate(ofQuaternion(-rotationY, getXAxis()));
+ rotate(ofQuaternion(-rotationZ, getZAxis()));
+}
+
+void ofxGameCamera::saveCameraPosition()
+{
+ ofxXmlSettings savePosition;
+ savePosition.addTag("camera");
+ savePosition.pushTag("camera");
+
+ savePosition.addTag("position");
+ savePosition.pushTag("position");
+
+ savePosition.addValue("X", getPosition().x);
+ savePosition.addValue("Y", getPosition().y);
+ savePosition.addValue("Z", getPosition().z);
+
+ savePosition.popTag(); //pop position
+
+ savePosition.addTag("rotation");
+ savePosition.pushTag("rotation");
+
+ savePosition.addValue("X", rotationX);
+ savePosition.addValue("Y", rotationY);
+ savePosition.addValue("Z", rotationZ);
+ savePosition.addValue("FOV", getFov());
+ savePosition.popTag(); //pop rotation
+
+ savePosition.popTag(); //camera;
+
+ savePosition.saveFile(cameraPositionFile);
+}
+
+void ofxGameCamera::loadCameraPosition()
+{
+ ofxXmlSettings loadPosition;
+ if(loadPosition.loadFile(cameraPositionFile)){
+
+ loadPosition.pushTag("camera");
+ loadPosition.pushTag("position");
+ // tig: defaulting with floats so as to get floats back.
+ setPosition(ofVec3f(loadPosition.getValue("X", 0.),
+ loadPosition.getValue("Y", 0.),
+ loadPosition.getValue("Z", 0.)));
+ loadPosition.popTag();
+
+ loadPosition.pushTag("rotation");
+ rotationX = loadPosition.getValue("X", 0.);
+ rotationY = loadPosition.getValue("Y", 0.);
+ rotationZ = loadPosition.getValue("Z", 0.);
+ float fov = loadPosition.getValue("FOV", -1);
+ if(fov != -1){
+ setFov(fov);
+ }
+
+ loadPosition.popTag();
+
+ loadPosition.popTag(); //pop camera;
+
+ updateRotation();
+ }
+ else{
+ ofLog(OF_LOG_ERROR, "ofxGameCamera: couldn't load position!");
+ }
+
+}
+
View
14 src/ofxGameCamera.h
@@ -9,6 +9,7 @@
#include "ofMain.h"
+#include "ofxXmlSettings.h"
class ofxGameCamera : public ofCamera {
public:
@@ -26,10 +27,23 @@ class ofxGameCamera : public ofCamera {
float rotationX;
float rotationY;
+ float rotationZ;
float speed;
virtual void begin(ofRectangle rect = ofGetWindowRect());
+ void updateRotation();
ofVec2f lastMouse;
+ bool usemouse;
+
+ bool useArrowKeys;
+
+ bool autosavePosition;
+
+ string cameraPositionFile;
+ void saveCameraPosition();
+ void loadCameraPosition();
+
+
};

0 comments on commit 05e8778

Please sign in to comment.
Something went wrong with that request. Please try again.