scene.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <scene.h>
  2. #include "objParser.h"
  3. Scene::Scene(std::string path){
  4. emptyScene = loadSceneModels(path);
  5. }
  6. Scene::~Scene(){
  7. //Making sure you don't attempt to delete models that don't exist
  8. if (!emptyScene){
  9. for(Model *models : modelsInScene){
  10. delete models;
  11. }
  12. }
  13. }
  14. void Scene::update(){
  15. visibleModels.clear();
  16. mainCamera.update();
  17. for(Model *models : modelsInScene){
  18. models->update();
  19. }
  20. frustrumCulling();
  21. }
  22. bool Scene::loadSceneModels(std::string &path){
  23. //In the future I want to read all o the models in the model folder
  24. //And build them here. For now only one
  25. std::string fullPath = "../models/";
  26. fullPath = fullPath + path;
  27. if(!OBJ::fileExists(fullPath)){
  28. printf("Error! File:%s does not exist.\n",path.c_str());
  29. return true;
  30. }
  31. else{
  32. modelsInScene.push_back(new Model(fullPath));
  33. //We also initialize the model position here position here
  34. TransformParameters initParameters;
  35. //initParameters.scaling = Vector3(1, 60, 60);
  36. //initParameters.rotation = Vector3(0,0,0);
  37. initParameters.translation = Vector3(0, -1.5, 0);
  38. modelsInScene[0]->initPosition(initParameters);
  39. //sceneModel->describeMesh();
  40. return false;
  41. }
  42. }
  43. void Scene::frustrumCulling(){
  44. bool visible = true;
  45. for(Model *model : modelsInScene){
  46. //TO DO Visibility check against camera's frustrum planes
  47. if (visible){
  48. visibleModels.push_back(model);
  49. }
  50. }
  51. }
  52. std::vector<Model*>* Scene::getVisiblemodels(){
  53. return &visibleModels;
  54. }
  55. Camera* Scene::getCurrentCamera(){
  56. return &mainCamera;
  57. }
  58. bool Scene::checkIfEmpty(){
  59. return emptyScene;
  60. }
  61. // //If the frustrum culling returns true it means the model has been culled
  62. // //Because no other models are in the scene we return
  63. // // if (frustrumCulling(models, viewMatrix)){
  64. // // printf("Model culled!\n");
  65. // // return;
  66. // // }
  67. // //stupid frustrum culling
  68. // //if(v1.x < -1 || v1.x > 1 || v1.y < -1 || v1.y > 1 || v1.z > 1 || v1.z < -1) continue;
  69. // //if(v2.x < -1 || v2.x > 1 || v2.y < -1 || v2.y > 1 || v2.z > 1 || v2.z < -1) continue;
  70. // //if(v3.x < -1 || v3.x > 1 || v3.y < -1 || v3.y > 1 || v3.z > 1 || v3.z < -1) continue;