Browse Source

First attempt at free camera movement.

angel 7 years ago
parent
commit
bd3667b292

+ 1 - 1
include/camera.h

@@ -26,7 +26,7 @@ struct Camera{
 
     //Values related to orbiting mode
     float radius = 2;
-    bool orbiting = true;
+    bool orbiting = false;
 
     //Momentary fixed camera speed (FPS dependent)
     float camSpeed = 0.1f;

+ 1 - 1
include/sceneManager.h

@@ -28,7 +28,7 @@ class SceneManager{
         bool closeScene();
 
         std::string currentSceneID;
-        Scene *currentScene;
+        Scene* currentScene;
 };
 
 

BIN
scenes/teapotMetal/teapotMetal_albedo.png


+ 0 - 0
scenes/teapotMetal/teapot_ao.png → scenes/teapotMetal/teapotMetal_ao.png


+ 7 - 7
scenes/teapotMetal/teapot_config.txt → scenes/teapotMetal/teapotMetal_config.txt

@@ -1,4 +1,4 @@
-s teapot
+s teapotMetal
 //First line just checks that you are indeed reading the right config file
 /
 //SECTION: models
@@ -14,22 +14,22 @@ rot 90.0 0 0.0
 sca 0.4 0.4 0.4 
 /
 //SECTION: lights
-l 2
+l 4
 l01 sun
 pos 1.0 0.0 0.0
 col 1.0 1.0 1.0
 /
 l02 sun
-pos 1.0 0.0 1.0
-col 1.0 1.0 1.0
+pos 1.0 0.0 2.0
+col 0.0 1.0 0.0
 /
 l03 sun
-pos 0.0 0.0 1.0
-col 0.0 0.0 1.0
+pos 0.0 0.0 -1.0
+col 0.5 0.0 0.0
 /
 l04 sun
 pos 1.0 1.0 1.0
-col 1.0 0.0 1.0
+col 0.0 0.0 1.0
 /
 //SECTION: cameras
 c 

+ 0 - 0
scenes/teapotMetal/teapot_mesh.obj → scenes/teapotMetal/teapotMetal_mesh.obj


BIN
scenes/teapotMetal/teapotMetal_metal.png


BIN
scenes/teapotMetal/teapotMetal_normal.png


BIN
scenes/teapotMetal/teapotMetal_rough.png


BIN
scenes/teapotMetal/teapot_albedo.png


+ 0 - 12
scenes/teapotMetal/teapot_mesh.mtl

@@ -1,12 +0,0 @@
-# Blender MTL File: 'teapot.blend'
-# Material Count: 1
-
-newmtl None
-Ns 0.000000
-Ka 0.000000 0.000000 0.000000
-Kd 0.800000 0.800000 0.800000
-Ks 0.800000 0.800000 0.800000
-Ke 0.000000 0.000000 0.000000
-Ni 1.000000
-d 1.000000
-illum 2

BIN
scenes/teapotMetal/teapot_metal.png


BIN
scenes/teapotMetal/teapot_normal.png


BIN
scenes/teapotMetal/teapot_rough.png


+ 5 - 3
src/camera.cpp

@@ -10,15 +10,17 @@ Camera::Camera(){
 
 void Camera::update(unsigned int deltaT){
     if(orbiting){
-        float t = static_cast<float>(SDL_GetTicks());
-        float camX   = std::sin(t/6000) * radius;
-        float camZ   = std::cos(t/6000) * radius;
+        float ang = 2*M_PI*static_cast<float>(SDL_GetTicks())/3e4;
+        float camX   = std::sin(ang) * radius; 
+        float camZ   = std::cos(ang) * radius;
         position.x   = camX;
         position.y   = camX;
         position.z   = camZ;
     }
     else{
+        // target = front;
         target = position + front;
+
     }
     viewMatrix   = Matrix4::lookAt(position,target,up);
     cameraFrustrum.updatePlanes(viewMatrix, position);

+ 2 - 3
src/engine.cpp

@@ -57,7 +57,6 @@ void Engine::run(){
 
     //Main flags
     bool done = false;
-    bool switchScene = false;
 
     //Iteration and time keeping counters
     int count = 0;
@@ -84,10 +83,10 @@ void Engine::run(){
         deltaT = SDL_GetTicks() - start;
         printf("%2.1d: Loop elapsed time (ms):%d\n",count, deltaT);
         total += deltaT;
-        if(count == 500) break;
+        //if(count == 500) break;
     }
 
     printf("Closing down engine.\n");
-    printf("Average frame time over %2.1d frames:%2.fms.\n", count,total/(float)count);
+    printf("Average frame time over %2.1d frames:%2.fms.\n", count, total/(float)count);
     
 }

+ 58 - 6
src/inputManager.cpp

@@ -6,8 +6,10 @@ InputManager::~InputManager(){}
 bool InputManager::startUp(SceneManager &sceneManager){
     sceneController = &sceneManager;
     sceneCamera = (sceneController->getCurrentScene()->getCurrentCamera());
-
-    return true;
+    
+    //Only really care about relative mouse motion because we're building a space camera
+    bool success = !SDL_SetRelativeMouseMode(SDL_TRUE);
+    return success;
 }
 
 void InputManager::shutDown(){
@@ -48,7 +50,7 @@ void InputManager::handleEvent(SDL_Event * event, bool &done, unsigned int delta
             break;
 
             case SDLK_2:
-            sceneID = "firehydrant";
+            sceneID = "teapotMetal";
             break;
 
             case SDLK_3:
@@ -99,14 +101,18 @@ void InputManager::handleEvent(SDL_Event * event, bool &done, unsigned int delta
             case SDLK_r:
             sceneCamera->position = Vector3f(0, 0, 8.0);
             sceneCamera->target.zero();  
-            sceneCamera->radius = 3;  
+            sceneCamera->side = sceneCamera->front.crossProduct(sceneCamera->up);
+            sceneCamera->front = Vector3f(0, 0, -1);
+            sceneCamera->radius = 2;  
             break;
 
             case SDLK_TAB:
             sceneCamera->orbiting = !sceneCamera->orbiting;
             sceneCamera->position = Vector3f(0, 0, 8.0);
             sceneCamera->target.zero();
-            sceneCamera->radius = 3;    
+            sceneCamera->side = sceneCamera->front.crossProduct(sceneCamera->up);
+            sceneCamera->front = Vector3f(0, 0, -1);
+            sceneCamera->radius = 2;    
             break;
 
             default:
@@ -127,8 +133,54 @@ void InputManager::handleEvent(SDL_Event * event, bool &done, unsigned int delta
                 sceneCamera = (sceneController->getCurrentScene()->getCurrentCamera());
                 sceneCamera->position = Vector3f(0, 0, 8.0);
                 sceneCamera->target.zero();
+                sceneCamera->side = sceneCamera->front.crossProduct(sceneCamera->up);
+                sceneCamera->front = Vector3f(0, 0, -1);
+                
             }
 
         }
     }
-}
+    //Handling Mouse Motion
+    else if( event->type == SDL_MOUSEMOTION){
+        float sens = 0.001f;
+        float xRot = -(float)event->motion.yrel * sens;
+        float yRot = -(float)event->motion.xrel * sens;
+
+        Matrix4 cameraRot = Matrix4::fullRotMat(xRot, yRot, 0.0f);
+
+        sceneCamera->front = cameraRot.matMultVec(sceneCamera->front).normalized();
+        sceneCamera->side  = cameraRot.matMultVec(sceneCamera->side).normalized();
+        
+        // Matrix4 camTransform = (sceneCamera->viewMatrix);
+        // Vector3f newDir      = Vector3f((float)event->motion.xrel*0.01f, -(float)event->motion.yrel*0.01f, 0.0f);
+        // Vector3f relMov      = camTransform.matMultDir(newDir);
+
+        // sceneCamera->front = (sceneCamera->front + relMov).normalized();
+        
+        // if(event->motion.xrel > 0){
+        //     sceneCamera->front = (sceneCamera->front + Vector3f((float)event->motion.xrel*0.001f, 0.0f, 0.0f)).normalized();
+        //     printf("Moved right!\n");
+            
+        // }
+
+        // if(event->motion.xrel < 0){
+            
+        //     printf("Moved left!\n");
+        // }
+
+        // if(event->motion.yrel > 0){
+        //     printf("Moved down!\n");
+        // }
+
+        // if(event->motion.yrel < 0){
+        //     printf("Moved up!\n");
+        // }
+    }
+}
+
+
+//Handling Mouse Input
+    // else if((event->type == SDL_MOUSEBUTTONDOWN) && (event->button.button == SDL_BUTTON_RIGHT)){
+    //     printf("Right mouse Click detected\n");
+    //     SDL_Delay(1000);
+    // }

+ 0 - 1
src/rasterizer.cpp

@@ -90,7 +90,6 @@ void Rasterizer::drawTriangles(Vector3f *vertices, IShader &shader, Buffer<Uint3
     Vector3f e, e_row, f;
     Vector3f rgbVals{255,255,255};
     
-
     //Transform into viewport coordinates 
     Rasterizer::viewportTransform(pixelBuffer, vertices);
 

+ 0 - 1
src/softwareRenderer.cpp

@@ -71,7 +71,6 @@ void SoftwareRenderer::drawTriangularMesh(Model * currentModel){
     
     shader.lightPos = lightPositions;
 
-
     //Building worldToObject matrix
     Matrix4 worldToObject = (*(currentModel->getModelMatrix())).inverse();