瀏覽代碼

Improved camera and lighting in scenes.

angel 7 年之前
父節點
當前提交
7ce7213055

+ 1 - 0
include/camera.h

@@ -44,6 +44,7 @@ struct Camera{
 
     //Values related to orbiting mode
     float radius  = 2;
+    float period  = 30; //in seconds
     bool orbiting = true;
 
     //Momentary fixed camera speed (FPS dependent)

+ 40 - 0
include/light.h

@@ -12,6 +12,46 @@
 struct BaseLight{
     Vector3f position;
     Vector3f color;
+    float radius;
+    char type;
+    float time = 2 * M_PI ;
+    unsigned int totalTime;
+
+    void update(unsigned int deltaT){
+        totalTime += deltaT;
+        float ang    = static_cast<float>(totalTime) * time;
+        switch(type){
+            case 'o':
+                float orbX   = std::sin(ang) * radius; 
+                float orbZ   = std::cos(ang) * radius;
+                position.x   = orbX;
+                position.y   = orbX;
+                position.z   = orbZ;
+            break;
+
+            case 'f':
+            break;
+
+            case 'c':
+                float colX    = (std::sin(ang/12e3) + 1)/2.0f ; 
+                float colY    = (std::cos(ang/6e3) + 1)/2.0f ; 
+                color.x       = colX;
+                color.y       = 1.0f-colY;
+                color.z       = colY;
+                printf("%f\n",ang);
+                color.print();
+            break;
+
+            case 'l':
+                float linX    = std::sin(ang) * radius ; 
+                position.x    = linX;
+            break;
+
+            default:
+            break;
+        }
+    }
+
 };
 
 #endif

+ 11 - 13
scenes/chest/chest_config.txt

@@ -8,25 +8,23 @@ pos 0.0 0.0 0.0
 rot 0.0 0.0 0.0
 sca 1.0 1.0 1.0 
 /
-
 //SECTION: lights
-l 1
-l01 sun
+l 3
+l01 c
+pos 0.0 0.0 -1.0
+col 0.8 0.0 0.4
+/
+l02 o
+orb 4 20
 pos 1.0 0.0 0.0
 col 1.0 1.0 1.0
 /
-l02 sun
-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.5 0.0 0.0
-/
-l04 sun
+l03 l
+lin 4.0 20
 pos 1.0 1.0 1.0
-col 0.0 0.0 1.0
+col 0.0 1.0 1.0
 /
+
 //SECTION: cameras
 c 
 /

+ 4 - 4
scenes/firehydrant/firehydrant_config.txt

@@ -10,19 +10,19 @@ sca 1.0 1.0 1.0
 /
 //SECTION: lights
 l 3
-l01 sun
+l01 sun fixed
 pos 1.0 0.0 0.0
 col 1.0 1.0 1.0
 /
-l02 sun
+l02 sun fixed
 pos 1.0 0.0 1.0
 col 1.0 0.0 1.0
 /
-l03 sun
+l03 sun fixed
 pos 1.0 0.0 1.0
 col 0.0 0.0 1.0
 /
-l04 sun
+l04 sun fixed
 pos 1.0 1.0 1.0
 col 1.0 0.0 1.0
 /

+ 0 - 0
scenes/bunny/bunny.mtl → scenes/old_scenes/bunny/bunny.mtl


+ 0 - 0
scenes/bunny/bunny.obj → scenes/old_scenes/bunny/bunny.obj


+ 0 - 0
scenes/cow/cow.mtl → scenes/old_scenes/cow/cow.mtl


+ 0 - 0
scenes/cow/cow.obj → scenes/old_scenes/cow/cow.obj


+ 0 - 0
scenes/cube/cube.obj → scenes/old_scenes/cube/cube.obj


+ 0 - 0
scenes/dragon/dragon.mtl → scenes/old_scenes/dragon/dragon.mtl


+ 0 - 0
scenes/dragon/dragon.obj → scenes/old_scenes/dragon/dragon.obj


+ 0 - 0
scenes/elephant/elephant.obj → scenes/old_scenes/elephant/elephant.obj


+ 5 - 7
scenes/teapot/teapot_config.txt

@@ -15,24 +15,22 @@ sca 0.4 0.4 0.4
 /
 //SECTION: lights
 l 4
-l01 sun
+l01 sun fixed
 pos 1.0 0.0 0.0
 col 1.0 1.0 1.0
 /
-l02 sun
+l02 sun fixed
 pos 1.0 0.0 2.0
 col 0.0 1.0 0.0
 /
-l03 sun
+l03 sun fixed
 pos 0.0 0.0 -1.0
 col 0.5 0.0 0.0
 /
-l04 sun
+l04 sun fixed
 pos 1.0 1.0 1.0
 col 0.0 0.0 1.0
 /
-//SECTION: cameras
-c 
-/
+
 
 

+ 2 - 1
src/camera.cpp

@@ -18,7 +18,7 @@ Camera::Camera(){
 ///Also updates view matrix and projection matrix for rendering
 void Camera::update(unsigned int deltaT){
     if(orbiting){
-        float ang    = 2 * M_PI * static_cast<float>(SDL_GetTicks()) / 3e4;
+        float ang    = 2 * M_PI * static_cast<float>(SDL_GetTicks()) / (period*1000);
         float camX   = std::sin(ang) * radius; 
         float camZ   = std::cos(ang) * radius;
         position.x   = camX;
@@ -47,4 +47,5 @@ void Camera::resetCamera(){
     radius   =   2;
     pitch    =   0;
     yaw      = -90;
+    period   =  30;
 }

+ 0 - 1
src/engine.cpp

@@ -97,7 +97,6 @@ void Engine::run(){
         deltaT = SDL_GetTicks() - start;
         printf("%2.1d: Frame elapsed time (ms):%d\n",count, deltaT);
         total += deltaT;
-        if(count == 500) break;
     }
     printf("Closing down engine.\n");
     printf("Average frame time over %2.1d frames:%2.fms.\n", count, total/(float)count);

+ 15 - 1
src/inputManager.cpp

@@ -70,7 +70,7 @@ void InputManager::handleEvent(SDL_Event * event, bool &done, unsigned int delta
             break;
 
             case SDLK_2:
-            sceneID = "teapotMetal";
+            sceneID = "chest";
             break;
 
             case SDLK_3:
@@ -135,6 +135,20 @@ void InputManager::handleEvent(SDL_Event * event, bool &done, unsigned int delta
             sceneCamera->resetCamera();   
             break;
 
+            case SDLK_UP:
+            sceneCamera->period -= 2;
+            if (sceneCamera->period  < 4){
+                sceneCamera->period = 4;
+            }
+            break;
+
+            case SDLK_DOWN:
+            sceneCamera->period += 2;
+            if (sceneCamera->period  > 60){
+                sceneCamera->period = 60;
+            }
+            break;
+
             default:
             break;
 

+ 31 - 3
src/scene.cpp

@@ -38,8 +38,11 @@ Scene::~Scene(){
 //Update Order is critical for correct culling
 void Scene::update(unsigned int deltaT){
     mainCamera.update(deltaT);
-    for(Model *models : modelsInScene){
-        models->update();
+    for(int i=0; i < lightCount; ++i){
+        lights[i].update(deltaT);
+    }
+    for(Model *model : modelsInScene){
+        model->update();
     }
     frustrumCulling();
 }
@@ -144,11 +147,36 @@ bool Scene::loadContent(const std::string &baseFilePath, const std::string &scen
                         iss >> key;
                         lightCount = stoi(key);
                         //Initializes light array
+                        std::string lightType, radius, period;
                         lights = new BaseLight[lightCount];
                         for(int i = 0; i < lightCount; ++i){
 
-                            //Burn one line with light type for now
+                            //Obtain light type and depending on that get orbit or linear
                             std::getline(file,line);
+                            std::istringstream lightData(line);
+                            lightData >> key >> lightType;
+                            if(lightType == "o"){
+                                lights[i].type = 'o';
+                                std::getline(file,line);
+                                std::istringstream orb(line);
+                                orb >> key >> radius >> period;
+                                lights[i].radius = stof(radius);
+                                lights[i].time /= stof(period)*1000; //miliseconds
+                            }
+                            else if(lightType == "l"){
+                                lights[i].type = 'l';
+                                std::getline(file,line);
+                                std::istringstream orb(line);
+                                orb >> key >> radius >> period;
+                                lights[i].radius = stof(radius);
+                                lights[i].time /= stof(period)*1000;
+                            }
+                            else if(lightType == "c"){
+                                lights[i].type = 'c';
+                            }
+                            else if(lightType == "f"){
+                                lights[i].type = 'f';
+                            }
 
                             //Position
                             std::getline(file,line);