Browse Source

Added multiple objects.

angel 7 years ago
parent
commit
8d6c9b1307
7 changed files with 178 additions and 55 deletions
  1. 11 0
      include/light.h
  2. 1 1
      include/model.h
  3. 14 8
      include/scene.h
  4. 21 0
      scenes/teapot/teapot_config.txt
  5. 129 42
      src/scene.cpp
  6. 0 4
      src/softwareRenderer.cpp
  7. 2 0
      src/texture.cpp

+ 11 - 0
include/light.h

@@ -0,0 +1,11 @@
+// #ifndef LIGHT_H
+// #define LIGHT_H
+
+// #include "vector3D.h"
+
+// struct BaseLight{
+//     Vector3f position;
+//     Vector3f color;
+// };
+
+// #endif

+ 1 - 1
include/model.h

@@ -10,7 +10,7 @@
 
 class Model{
     public:
-        Model(std::string basePath, TransformParameters &initParameters) : 
+        Model(std::string basePath,const TransformParameters &initParameters) : 
             mAlbedo(basePath + "_albedo.png", "RGB"),
             mNormal(basePath + "_normal.png", "XYZ"),
             mAmbient(basePath + "_ao.png", "BW"),

+ 14 - 8
include/scene.h

@@ -14,7 +14,7 @@
 class Scene{
     public:
         //Builds a scene based on a path to the folder containing the scene's
-        //content
+        //content and setup information
         Scene(const std::string &sceneFolder);
         ~Scene();
 
@@ -32,18 +32,24 @@ class Scene{
         bool checkIfEmpty();
     private:
         Camera mainCamera;
-
         bool emptyScene;
-
         std::vector<Model*> modelsInScene;
-
         //Contains the models that remain after frustrum culling
         std::queue<Model*> visibleModels;
-        
-        bool loadSceneModels(const std::string &sceneFolder);
 
-        //Cull objects that should not be visible and add the visible to the 
-        //visibleModels list for rendering TO DO 
+        //Check if the current scene folder acually exists both in windows and linux
+        //And also checks for accessibility 
+        bool findSceneFolder(const std::string &scenePath);
+
+        //Loads scene models, checks first if they exist by looking for the 
+        //mesh .If it finds it assumes (dangerously) that all the other 
+        //texture data also exists
+        void loadSceneModel(const std::string &baseFilePath, const TransformParameters &init);
+
+        bool loadContent(const std::string &baseFilePath, const std::string &sceneName);
+
+        //Cull objects that should not be visible and add the visible ones to the 
+        //visibleModels list for rendering 
         void frustrumCulling();
 };
 

+ 21 - 0
scenes/teapot/teapot_config.txt

@@ -0,0 +1,21 @@
+s teapot
+//First line just checks that you are indeed reading the right config file
+/
+//SECTION: models
+m 1
+01 teapot
+pos 0.0 0.0 0.0 
+rot 90.0 0.0 0.0
+sca 0.4 0.4 0.4 
+/
+//SECTION: lights
+l 1
+l01 sun
+pos 1.0 1.0 1.0
+col 1.0 1.0 1.0
+//
+//SECTION: cameras
+c 
+/
+
+

+ 129 - 42
src/scene.cpp

@@ -1,14 +1,25 @@
 #include "scene.h"
 #include "objParser.h"
+#include <fstream>
+#include <sstream>
 #include <sys/stat.h>
 #include <sys/types.h>
 
-struct stat info;
-
 //For now a scene only contains a single model
-Scene::Scene(const std::string &sceneFolder){
-
-    emptyScene = loadSceneModels(sceneFolder);
+Scene::Scene(const std::string &sceneName){
+    //Building all the useful path strings
+    std::string folderPath = "../scenes/" + sceneName;
+    std::string baseFilePath = folderPath + "/" + sceneName;
+    
+    if( !findSceneFolder(folderPath)){
+        //If you do not find the scene folder quit early and gracefully
+        emptyScene = true; 
+    }
+    else{
+        //Load all cameras, models and lights and return false if it failed
+        //to load anything
+        emptyScene = !loadContent(baseFilePath, sceneName);
+    }
 }
 
 Scene::~Scene(){
@@ -28,44 +39,15 @@ void Scene::update(){
     frustrumCulling();
 }
 
-//Check if the scene exists and if it contains any valid mesh
-bool Scene::loadSceneModels(const std::string &sceneFolder){
-    std::string path = "../scenes/";
-    path +=  sceneFolder;
-
-    //Check if the current scene folder acually exists both in windows and linux
-    //And also checks if it can be accessed
-    if( stat( path.c_str(), &info ) != 0 ){
-        printf( "cannot access %s\n", path.c_str() );
-    }
-    else if( info.st_mode & S_IFDIR ){
-        printf( "%s is a valid scene\n", path.c_str() );
-
-        //Build a base file path that all models will use 
-        //Use it to do an early quit in case the mesh does not exist
-        std::string baseFilePath = path + "/" + sceneFolder;
-        std::string meshFilePath = baseFilePath + "_mesh.obj";
-        if(!OBJ::fileExists(meshFilePath)){
-            printf("Error! Mesh: %s does not exist.\n",meshFilePath.c_str());
-            return true;
-        }
-        else{
-            printf( "%s is a valid mesh\n", meshFilePath.c_str() );
-            //For now initParameters are calculated here
-            //IMPLEMENT: read directly from file
-            TransformParameters initParameters;
-            initParameters.rotation = Vector3f(90*M_PI/180.0f, 0 , 0);
-            // initParameters.translation = Vector3f(0, -0.5, 0);
-            initParameters.scaling = Vector3f(0.4, 0.4, 0.4);
-            //initParameters.translation = Vector3f(0, 0, 0);
-            modelsInScene.push_back(new Model(baseFilePath, initParameters));
-            return false;
-        }
-
+//Returns false if there was any issue loading the scene object
+void Scene::loadSceneModel(const std::string &baseFilePath, const TransformParameters &init){
+    std::string meshFilePath = baseFilePath + "_mesh.obj";
+    if(!OBJ::fileExists(meshFilePath)){
+        printf("Error! Mesh: %s does not exist.\n",meshFilePath.c_str());
     }
     else{
-        printf("Error! Scene: %s does not exist.\n",path.c_str());
-        return true;
+        printf( "%s is a valid mesh\n", meshFilePath.c_str() );
+        modelsInScene.push_back(new Model(baseFilePath, init));
     }
 }
 
@@ -91,4 +73,109 @@ Camera* Scene::getCurrentCamera(){
 
 bool Scene::checkIfEmpty(){
     return emptyScene;
-}
+}
+
+bool Scene::findSceneFolder(const std::string &scenePath){
+    struct stat info;
+    if( stat( scenePath.c_str(), &info ) != 0 ){
+        printf( "cannot access %s\n", scenePath.c_str() );
+         return false;
+    }
+    else if( info.st_mode & S_IFDIR ){
+        printf( "%s is a valid scene\n", scenePath.c_str() );
+        return true;
+    }
+    else{
+        printf("Error! Scene: %s does not exist.\n",scenePath.c_str());
+        return false;
+    }
+}
+
+bool Scene::loadContent(const std::string &baseFilePath, const std::string &sceneName ){
+    std::string configFilePath = baseFilePath + "_config.txt";
+    std::ifstream file(configFilePath.c_str());
+    TransformParameters initParameters;
+
+    //Does config file parsing here
+    //TODO: separate into new class or function
+    if(!file.good()){
+        printf("Error! Config: %s does not exist.\n",configFilePath.c_str());
+        return false;
+    }
+    else{
+        //Checking that config file belongs to current scene and is properly formatted
+        std::string line, key, x, y, z;
+        std::getline(file,line);
+        std::istringstream iss(line);
+        iss >> key;
+        if(key != "s"){
+            printf("Error! Config file: %s is not properly formatted.\n",configFilePath.c_str());
+            return false;
+        }
+        else{
+            iss >> key;
+            if(key != sceneName){
+                printf("Error! Config file: %s does not belong to current scene.\n",configFilePath.c_str());
+                return false;
+            }
+            else{
+                //Now we can parse the rest of the file "safely"
+                while(!file.eof()){
+                    std::getline(file,line);
+                    std::istringstream iss(line);
+                    iss >> key;
+                    if(key == "m"){ //model related setup
+                        printf("Loading models...\n");
+                        iss >> key;
+                        int max = stoi(key);
+                        for(int i = 0; i < max; ++i){
+
+                            //Burn one line with model type for now
+                            std::getline(file,line); 
+
+                            //Position
+                            std::getline(file,line);
+                            std::istringstream pos(line);
+                            pos >> key >> x >> y >> z;
+                            initParameters.translation = Vector3f(stof(x), stof(y), stof(z));
+
+                            //Rotation
+                            std::getline(file,line);
+                            std::istringstream rot(line);
+                            rot >> key >> x >> y >> z;
+                            initParameters.rotation = Vector3f(stof(x)*M_PI/180.0f, stof(y)*M_PI/180.0f, stof(z)*M_PI/180.0f);
+
+                            //Scaling
+                            std::getline(file,line);
+                            std::istringstream sca(line);
+                            sca >> key >> x >> y >> z;
+                            initParameters.scaling = Vector3f(stof(x), stof(y), stof(z));
+
+                            //Burning empty line that makes the config easier to read
+                            std::getline(file,line);
+                            
+                            //Attempts to load model with the initparameters it has read
+                            //if it fails it won't do anything much now 
+                            loadSceneModel(baseFilePath, initParameters);
+                        }
+                    }
+                    else if(key == "l"){ //light related setup
+                        printf("Loading lights...\n");
+                        iss >> key;
+                        int max = stoi(key);
+                        for(int i = 0; i < max; ++i){
+                            printf("Light 1\n");
+                        }
+                    }
+                    else if(key == "c"){ //camera related setup
+                        printf("Loading camera...\n");
+                        //TODO: camera initialization setup
+                    }
+                }
+
+                //Lastly we check if the scene is empty and return 
+                return !modelsInScene.empty();       
+            }
+        }
+    }
+}

+ 0 - 4
src/softwareRenderer.cpp

@@ -58,10 +58,6 @@ void SoftwareRenderer::drawTriangularMesh(Model * currentModel){
     shader.cameraPos = mCamera->position;
 
     //Basic light direction
-    float t = static_cast<float>(SDL_GetTicks());
-    float radius = 1;
-    float lX   = std::sin(t/4000) * radius;
-    float lY   = std::cos(t/4000) * radius;
     Vector3f lightDir{1, 0, 0};
     lightDir = lightDir.normalized();
 

+ 2 - 0
src/texture.cpp

@@ -42,6 +42,7 @@ Texture::~Texture(){
 Vector3f Texture::getPixelVal(float u, float v){
 
     //Simple bilinear filtering
+    //DISABLED FOR NOW since it trashes the FPS
     // float intU;
     // float tU = std::modf(u * (width-1), &intU);
     // int uIntLo = (int)intU; 
@@ -72,6 +73,7 @@ Vector3f Texture::getPixelVal(float u, float v){
 
 float Texture::getIntensityVal(float u, float v){
     //Simple bilinear filtering
+    //DISABLED FOR NOW since it trashes the FPS
     // float intU;
     // float tU = std::modf(u * (width-1), &intU);
     // int uIntLo = (int)intU;