scene.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 is loaded.
  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. TransformParameters initParameters;
  33. initParameters.translation = Vector3(0, -1.5, 0);
  34. modelsInScene.push_back(new Model(fullPath, initParameters));
  35. return false;
  36. }
  37. }
  38. void Scene::frustrumCulling(){
  39. bool visible = true;
  40. for(Model *model : modelsInScene){
  41. //TO DO Visibility check against camera's frustrum planes
  42. if (visible){
  43. visibleModels.push_back(model);
  44. }
  45. }
  46. }
  47. std::vector<Model*>* Scene::getVisiblemodels(){
  48. return &visibleModels;
  49. }
  50. Camera* Scene::getCurrentCamera(){
  51. return &mainCamera;
  52. }
  53. bool Scene::checkIfEmpty(){
  54. return emptyScene;
  55. }
  56. // //If the frustrum culling returns true it means the model has been culled
  57. // //Because no other models are in the scene we return
  58. // // if (frustrumCulling(models, viewMatrix)){
  59. // // printf("Model culled!\n");
  60. // // return;
  61. // // }
  62. // //stupid frustrum culling
  63. // //if(v1.x < -1 || v1.x > 1 || v1.y < -1 || v1.y > 1 || v1.z > 1 || v1.z < -1) continue;
  64. // //if(v2.x < -1 || v2.x > 1 || v2.y < -1 || v2.y > 1 || v2.z > 1 || v2.z < -1) continue;
  65. // //if(v3.x < -1 || v3.x > 1 || v3.y < -1 || v3.y > 1 || v3.z > 1 || v3.z < -1) continue;