Advertisement
NoxxyGizmo

SceneManager.cpp Bugged

Apr 15th, 2025
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.85 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // shadermanager.cpp
  3. // ============
  4. // manage the loading and rendering of 3D scenes
  5. //
  6. // AUTHOR: Brian Battersby - SNHU Instructor / Computer Science
  7. // Created for CS-330-Computational Graphics and Visualization, Nov. 1st, 2023
  8. ///////////////////////////////////////////////////////////////////////////////
  9.  
  10. #include "SceneManager.h"
  11.  
  12. #include <glm/gtx/transform.hpp>
  13.  
  14. // declare the global variables
  15. namespace
  16. {
  17. const char* g_ModelName = "model";
  18. const char* g_ColorValueName = "objectColor";
  19. const char* g_TextureValueName = "objectTexture";
  20. const char* g_UseTextureName = "bUseTexture";
  21. const char* g_UseLightingName = "bUseLighting";
  22. }
  23.  
  24. /***********************************************************
  25. * SceneManager()
  26. *
  27. * The constructor for the class
  28. ***********************************************************/
  29. SceneManager::SceneManager(ShaderManager *pShaderManager)
  30. {
  31. m_pShaderManager = pShaderManager;
  32. m_basicMeshes = new ShapeMeshes();
  33. }
  34.  
  35. /***********************************************************
  36. * ~SceneManager()
  37. *
  38. * The destructor for the class
  39. ***********************************************************/
  40. SceneManager::~SceneManager()
  41. {
  42. // free up the allocated memory
  43. m_pShaderManager = NULL;
  44. if (NULL != m_basicMeshes)
  45. {
  46. delete m_basicMeshes;
  47. m_basicMeshes = NULL;
  48. }
  49. // clear the collection of defined materials
  50. m_objectMaterials.clear();
  51. }
  52.  
  53. /***********************************************************
  54. * FindMaterial()
  55. *
  56. * This method is used for getting a material from the previously
  57. * defined materials list that is associated with the passed in tag.
  58. ***********************************************************/
  59. bool SceneManager::FindMaterial(std::string tag, OBJECT_MATERIAL& material)
  60. {
  61. if (m_objectMaterials.size() == 0)
  62. {
  63. return(false);
  64. }
  65.  
  66. int index = 0;
  67. bool bFound = false;
  68. while ((index < m_objectMaterials.size()) && (bFound == false))
  69. {
  70. if (m_objectMaterials[index].tag.compare(tag) == 0)
  71. {
  72. bFound = true;
  73. material.ambientColor = m_objectMaterials[index].ambientColor;
  74. material.ambientStrength = m_objectMaterials[index].ambientStrength;
  75. material.diffuseColor = m_objectMaterials[index].diffuseColor;
  76. material.specularColor = m_objectMaterials[index].specularColor;
  77. material.shininess = m_objectMaterials[index].shininess;
  78. }
  79. else
  80. {
  81. index++;
  82. }
  83. }
  84.  
  85. return(true);
  86. }
  87.  
  88. /***********************************************************
  89. * SetTransformation()
  90. *
  91. * This method is used for setting the transform buffer
  92. * using the passed in transformation values.
  93. ***********************************************************/
  94. void SceneManager::SetTransformations(
  95. glm::vec3 scaleXYZ,
  96. float XrotationDegrees,
  97. float YrotationDegrees,
  98. float ZrotationDegrees,
  99. glm::vec3 positionXYZ)
  100. {
  101. // variables for this method
  102. glm::mat4 modelView;
  103. glm::mat4 scale;
  104. glm::mat4 rotationX;
  105. glm::mat4 rotationY;
  106. glm::mat4 rotationZ;
  107. glm::mat4 translation;
  108.  
  109. // set the scale value in the transform buffer
  110. scale = glm::scale(scaleXYZ);
  111. // set the rotation values in the transform buffer
  112. rotationX = glm::rotate(glm::radians(XrotationDegrees), glm::vec3(1.0f, 0.0f, 0.0f));
  113. rotationY = glm::rotate(glm::radians(YrotationDegrees), glm::vec3(0.0f, 1.0f, 0.0f));
  114. rotationZ = glm::rotate(glm::radians(ZrotationDegrees), glm::vec3(0.0f, 0.0f, 1.0f));
  115. // set the translation value in the transform buffer
  116. translation = glm::translate(positionXYZ);
  117.  
  118. // matrix math is used to calculate the final model matrix
  119. modelView = translation * rotationX * rotationY * rotationZ * scale;
  120. if (NULL != m_pShaderManager)
  121. {
  122. // pass the model matrix into the shader
  123. m_pShaderManager->setMat4Value(g_ModelName, modelView);
  124. }
  125. }
  126.  
  127. /***********************************************************
  128. * SetShaderColor()
  129. *
  130. * This method is used for setting the passed in color
  131. * into the shader for the next draw command
  132. ***********************************************************/
  133. void SceneManager::SetShaderColor(
  134. float redColorValue,
  135. float greenColorValue,
  136. float blueColorValue,
  137. float alphaValue)
  138. {
  139. // variables for this method
  140. glm::vec4 currentColor;
  141.  
  142. currentColor.r = redColorValue;
  143. currentColor.g = greenColorValue;
  144. currentColor.b = blueColorValue;
  145. currentColor.a = alphaValue;
  146.  
  147. if (NULL != m_pShaderManager)
  148. {
  149. // pass the color values into the shader
  150. m_pShaderManager->setVec4Value(g_ColorValueName, currentColor);
  151. }
  152. }
  153.  
  154. /***********************************************************
  155. * SetShaderMaterial()
  156. *
  157. * This method is used for passing the material values
  158. * into the shader.
  159. ***********************************************************/
  160. void SceneManager::SetShaderMaterial(
  161. std::string materialTag)
  162. {
  163. if (m_objectMaterials.size() > 0)
  164. {
  165. OBJECT_MATERIAL material;
  166. bool bReturn = false;
  167.  
  168. // find the defined material that matches the tag
  169. bReturn = FindMaterial(materialTag, material);
  170. if (bReturn == true)
  171. {
  172. // pass the material properties into the shader
  173. m_pShaderManager->setVec3Value("material.ambientColor", material.ambientColor);
  174. m_pShaderManager->setFloatValue("material.ambientStrength", material.ambientStrength);
  175. m_pShaderManager->setVec3Value("material.diffuseColor", material.diffuseColor);
  176. m_pShaderManager->setVec3Value("material.specularColor", material.specularColor);
  177. m_pShaderManager->setFloatValue("material.shininess", material.shininess);
  178. }
  179. }
  180. }
  181.  
  182. /**************************************************************/
  183. /*** STUDENTS CAN MODIFY the code in the methods BELOW for ***/
  184. /*** preparing and rendering their own 3D replicated scenes.***/
  185. /*** Please refer to the code in the OpenGL sample project ***/
  186. /*** for assistance. ***/
  187. /**************************************************************/
  188.  
  189. /***********************************************************
  190. * DefineObjectMaterials()
  191. *
  192. * This method is used for configuring the various material
  193. * settings for all of the objects within the 3D scene.
  194. ***********************************************************/
  195. void SceneManager::DefineObjectMaterials()
  196. {
  197. /*** STUDENTS - add the code BELOW for defining object materials. ***/
  198. /*** There is no limit to the number of object materials that can ***/
  199. /*** be defined. Refer to the code in the OpenGL Sample for help ***/
  200.  
  201. OBJECT_MATERIAL clayMaterial;
  202. clayMaterial.ambientColor = glm::vec3(0.2f, 0.2f, 0.3f);
  203. clayMaterial.ambientStrength = 0.3f;
  204. clayMaterial.diffuseColor = glm::vec3(0.4f, 0.4f, 0.5f);
  205. clayMaterial.specularColor = glm::vec3(0.2f, 0.2f, 0.4f);
  206. clayMaterial.shininess = 0.5;
  207. clayMaterial.tag = "clay";
  208. m_objectMaterials.push_back(clayMaterial);
  209.  
  210. }
  211.  
  212. /***********************************************************
  213. * SetupSceneLights()
  214. *
  215. * This method is called to add and configure the light
  216. * sources for the 3D scene. There are up to 4 light sources.
  217. ***********************************************************/
  218. void SceneManager::SetupSceneLights()
  219. {
  220. // this line of code is NEEDED for telling the shaders to render
  221. // the 3D scene with custom lighting, if no light sources have
  222. // been added then the display window will be black - to use the
  223. // default OpenGL lighting then comment out the following line
  224. //m_pShaderManager->setBoolValue(g_UseLightingName, true);
  225.  
  226. /*** STUDENTS - add the code BELOW for setting up light sources ***/
  227. /*** Up to four light sources can be defined. Refer to the code ***/
  228. /*** in the OpenGL Sample for help ***/
  229.  
  230. // Soft white light on the upper left
  231. m_pShaderManager->setVec3Value("lightSources[0].position", -3.0f, 14.0f, 0.0f);
  232. m_pShaderManager->setVec3Value("lightSources[0].ambientColor", 0.3f, 0.3f, 0.3f);
  233. m_pShaderManager->setVec3Value("lightSources[0].diffuseColor", 0.8f, 0.8f, 0.8f);
  234. m_pShaderManager->setVec3Value("lightSources[0].specularColor", 0.2f, 0.2f, 0.2f);
  235. m_pShaderManager->setFloatValue("lightSources[0].focalStrength", 16.0f);
  236. m_pShaderManager->setFloatValue("lightSources[0].specularIntensity", 0.1f);
  237. // Soft red light on the upper right
  238. m_pShaderManager->setVec3Value("lightSources[1].position", 3.0f, 14.0f, 0.0f);
  239. m_pShaderManager->setVec3Value("lightSources[1].ambientColor", 0.3f, 0.1f, 0.1f);
  240. m_pShaderManager->setVec3Value("lightSources[1].diffuseColor", 0.8f, 0.3f, 0.3f);
  241. m_pShaderManager->setVec3Value("lightSources[1].specularColor", 0.3f, 0.1f, 0.1f);
  242. m_pShaderManager->setFloatValue("lightSources[1].focalStrength", 16.0f);
  243. m_pShaderManager->setFloatValue("lightSources[1].specularIntensity", 0.1f);
  244.  
  245. m_pShaderManager->setBoolValue("bUseLighting", true);
  246. }
  247.  
  248. /***********************************************************
  249. * PrepareScene()
  250. *
  251. * This method is used for preparing the 3D scene by loading
  252. * the shapes, textures in memory to support the 3D scene
  253. * rendering
  254. ***********************************************************/
  255. void SceneManager::PrepareScene()
  256. {
  257. // define the materials for objects in the scene
  258. DefineObjectMaterials();
  259. // add and define the light sources for the scene
  260. SetupSceneLights();
  261.  
  262. // only one instance of a particular mesh needs to be
  263. // loaded in memory no matter how many times it is drawn
  264. // in the rendered 3D scene - the following code loads
  265. // the basic 3D meshes into the graphics pipeline buffers
  266.  
  267. m_basicMeshes->LoadBoxMesh();
  268. m_basicMeshes->LoadPlaneMesh();
  269. m_basicMeshes->LoadCylinderMesh();
  270. m_basicMeshes->LoadConeMesh();
  271. m_basicMeshes->LoadSphereMesh();
  272. }
  273.  
  274. /***********************************************************
  275. * RenderScene()
  276. *
  277. * This method is used for rendering the 3D scene by
  278. * transforming and drawing the basic 3D shapes
  279. ***********************************************************/
  280. void SceneManager::RenderScene()
  281. {
  282. // declare the variables for the transformations
  283. glm::vec3 scaleXYZ;
  284. float XrotationDegrees = 0.0f;
  285. float YrotationDegrees = 0.0f;
  286. float ZrotationDegrees = 0.0f;
  287. glm::vec3 positionXYZ;
  288.  
  289. /*** Set needed transformations before drawing the basic mesh. ***/
  290. /*** This same ordering of code should be used for transforming ***/
  291. /*** and drawing all the basic 3D shapes. ***/
  292. /******************************************************************/
  293. // set the XYZ scale for the mesh
  294. scaleXYZ = glm::vec3(20.0f, 1.0f, 10.0f);
  295.  
  296. // set the XYZ rotation for the mesh
  297. XrotationDegrees = 0.0f;
  298. YrotationDegrees = 0.0f;
  299. ZrotationDegrees = 0.0f;
  300.  
  301. // set the XYZ position for the mesh
  302. positionXYZ = glm::vec3(0.0f, 0.0f, 0.0f);
  303.  
  304. // set the transformations into memory to be used on the drawn meshes
  305. SetTransformations(
  306. scaleXYZ,
  307. XrotationDegrees,
  308. YrotationDegrees,
  309. ZrotationDegrees,
  310. positionXYZ);
  311.  
  312. // set the active color values in the shader (RGBA)
  313. SetShaderColor(1, 1, 1, 1);
  314. SetShaderMaterial("clay");
  315.  
  316. // draw the mesh with transformation values - this plane is used for the base
  317. m_basicMeshes->DrawPlaneMesh();
  318. /****************************************************************/
  319.  
  320. /*** Set needed transformations before drawing the basic mesh. ***/
  321. /*** This same ordering of code should be used for transforming ***/
  322. /*** and drawing all the basic 3D shapes. ***/
  323. /******************************************************************/
  324. // set the XYZ scale for the mesh
  325. scaleXYZ = glm::vec3(0.9f, 2.8f, 0.9f);
  326.  
  327. // set the XYZ rotation for the mesh
  328. XrotationDegrees = 90.0f;
  329. YrotationDegrees = 0.0f;
  330. ZrotationDegrees = -15.0f;
  331.  
  332. // set the XYZ position for the mesh
  333. positionXYZ = glm::vec3(0.0f, 0.9f, 0.4f);
  334.  
  335. // set the transformations into memory to be used on the drawn meshes
  336. SetTransformations(
  337. scaleXYZ,
  338. XrotationDegrees,
  339. YrotationDegrees,
  340. ZrotationDegrees,
  341. positionXYZ);
  342.  
  343. // set the active color values in the shader (RGBA)
  344. SetShaderColor(1, 1, 1, 1);
  345. SetShaderMaterial("clay");
  346.  
  347. m_basicMeshes->DrawCylinderMesh();
  348. /****************************************************************/
  349.  
  350. /*** Set needed transformations before drawing the basic mesh. ***/
  351. /*** This same ordering of code should be used for transforming ***/
  352. /*** and drawing all the basic 3D shapes. ***/
  353. /******************************************************************/
  354. // set the XYZ scale for the mesh
  355. scaleXYZ = glm::vec3(1.0f, 9.0f, 1.3f);
  356.  
  357. // set the XYZ rotation for the mesh
  358. XrotationDegrees = 0.0f;
  359. YrotationDegrees = 0.0f;
  360. ZrotationDegrees = 95.0f;
  361.  
  362. // set the XYZ position for the mesh
  363. positionXYZ = glm::vec3(0.2f, 2.27f, 2.0f);
  364.  
  365. // set the transformations into memory to be used on the drawn meshes
  366. SetTransformations(
  367. scaleXYZ,
  368. XrotationDegrees,
  369. YrotationDegrees,
  370. ZrotationDegrees,
  371. positionXYZ);
  372.  
  373. // set the active color values in the shader (RGBA)
  374. SetShaderColor(1, 1, 1, 1);
  375. SetShaderMaterial("clay");
  376.  
  377. m_basicMeshes->DrawBoxMesh();
  378. /****************************************************************/
  379.  
  380. /*** Set needed transformations before drawing the basic mesh. ***/
  381. /*** This same ordering of code should be used for transforming ***/
  382. /*** and drawing all the basic 3D shapes. ***/
  383. /******************************************************************/
  384. // set the XYZ scale for the mesh
  385. scaleXYZ = glm::vec3(1.7f, 1.5f, 1.5f);
  386.  
  387. // set the XYZ rotation for the mesh
  388. XrotationDegrees = 0.0f;
  389. YrotationDegrees = 40.0f;
  390. ZrotationDegrees = 8.0f;
  391.  
  392. // set the XYZ position for the mesh
  393. positionXYZ = glm::vec3(3.3f, 3.85f, 2.19f);
  394.  
  395. // set the transformations into memory to be used on the drawn meshes
  396. SetTransformations(
  397. scaleXYZ,
  398. XrotationDegrees,
  399. YrotationDegrees,
  400. ZrotationDegrees,
  401. positionXYZ);
  402.  
  403. // set the active color values in the shader (RGBA)
  404. SetShaderColor(1, 1, 1, 1);
  405. SetShaderMaterial("clay");
  406.  
  407. m_basicMeshes->DrawBoxMesh();
  408. /****************************************************************/
  409.  
  410. /*** Set needed transformations before drawing the basic mesh. ***/
  411. /*** This same ordering of code should be used for transforming ***/
  412. /*** and drawing all the basic 3D shapes. ***/
  413. /******************************************************************/
  414. // set the XYZ scale for the mesh
  415. scaleXYZ = glm::vec3(1.0f, 1.0f, 1.0f);
  416.  
  417. // set the XYZ rotation for the mesh
  418. XrotationDegrees = 0.0f;
  419. YrotationDegrees = 0.0f;
  420. ZrotationDegrees = 0.0f;
  421.  
  422. // set the XYZ position for the mesh
  423. positionXYZ = glm::vec3(3.2f, 5.6f, 2.5f);
  424.  
  425. // set the transformations into memory to be used on the drawn meshes
  426. SetTransformations(
  427. scaleXYZ,
  428. XrotationDegrees,
  429. YrotationDegrees,
  430. ZrotationDegrees,
  431. positionXYZ);
  432.  
  433. // set the active color values in the shader (RGBA)
  434. SetShaderColor(1, 1, 1, 1);
  435. SetShaderMaterial("clay");
  436.  
  437. m_basicMeshes->DrawSphereMesh();
  438. /****************************************************************/
  439.  
  440. /*** Set needed transformations before drawing the basic mesh. ***/
  441. /*** This same ordering of code should be used for transforming ***/
  442. /*** and drawing all the basic 3D shapes. ***/
  443. /******************************************************************/
  444. // set the XYZ scale for the mesh
  445. scaleXYZ = glm::vec3(1.2f, 4.0f, 1.2f);
  446.  
  447. // set the XYZ rotation for the mesh
  448. XrotationDegrees = 0.0f;
  449. YrotationDegrees = 0.0f;
  450. ZrotationDegrees = 5.0f;
  451.  
  452. // set the XYZ position for the mesh
  453. positionXYZ = glm::vec3(-3.3f, 2.50f, 2.0f);
  454.  
  455. // set the transformations into memory to be used on the drawn meshes
  456. SetTransformations(
  457. scaleXYZ,
  458. XrotationDegrees,
  459. YrotationDegrees,
  460. ZrotationDegrees,
  461. positionXYZ);
  462.  
  463. // set the active color values in the shader (RGBA)
  464. SetShaderColor(1, 1, 1, 1);
  465. SetShaderMaterial("clay");
  466.  
  467. m_basicMeshes->DrawConeMesh();
  468. /****************************************************************/
  469. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement