Advertisement
macko9393

Untitled

Apr 10th, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.32 KB | None | 0 0
  1.  
  2. //
  3. // Week 6 - Demo 01 - Retained mode rendering 01 (Packed Arrays)
  4. //
  5.  
  6. #include "stdafx.h"
  7. #include <glew\glew.h>
  8. #include <freeglut.h>
  9. #include <CoreStructures.h>
  10. #include "texture_loader.h"
  11. #include "shader_setup.h"
  12. #include "CGModel\CGModel.h"
  13. #include "Importers\CGImporters.h"
  14.  
  15. using namespace std;
  16. using namespace CoreStructures;
  17.  
  18. #define FIXED_FUNCTION true
  19.  
  20. // Globals
  21.  
  22. bool lightToggled = false;
  23.  
  24. // Mouse input (rotation) example
  25. // Variable we'll use to animate (rotate) our star object
  26. float theta = 0.0f;
  27. GUClock cgClock;
  28. // Variables needed to track where the mouse pointer is so we can determine which direction it's moving in
  29. int mouse_x, mouse_y;
  30. bool mDown = false;
  31.  
  32. // uniform variables to access in shader
  33. GLuint gDiffuse; // Shader light diffuse
  34. GLuint gAmbient; // Shader light ambient
  35. GLuint gAttenuation; // Shader light attenuation
  36. GLuint gLPosition; // Shader light position
  37.  
  38. GLuint gEyePos; // Shader eye position
  39.  
  40. //Material Properties
  41. GLuint gMatAmbient;
  42. GLuint gMatDiffuse;
  43. GLuint gMatSpecular;
  44.  
  45. // Shader Matrix setup
  46. GLuint gModelMatrix;
  47. GLuint gViewMatrix;
  48. GLuint gProjectionMatrix;
  49. GLuint gNormalMatrix;
  50.  
  51.  
  52. // Shader program object
  53. GLuint myShaderProgram;
  54.  
  55. // Textures
  56. GLuint bearTexture = 0;
  57. GLuint pyramidTexture = 0;
  58. GLuint groundTexture = 0;
  59. GLuint fireTexture = 0;
  60. GLuint wallTexture = 0;
  61.  
  62. // Model
  63. CGModel *bearModel[1];
  64. CGModel *objectModel[1];
  65. CGModel *groundModel[1];
  66. CGModel *fireModel[1];
  67. CGModel *wallModel[1];
  68.  
  69. // Model Instance Matrix
  70. GUVector4 bearModelPos = GUVector4(0, 0, 0, 1);
  71. GUVector4 objectModelPos = GUVector4(0, 0, 0, 1);
  72. GUVector4 groundModelPos = GUVector4(0, 0, 0, 1);
  73. GUVector4 fireModelPos = GUVector4(0, 0, 0, 1);
  74. GUVector4 wallModelPos = GUVector4(0, 0, 0, 1);
  75.  
  76. GUMatrix4 bearModelMatrix = GUMatrix4::translationMatrix(bearModelPos);
  77. GUMatrix4 objectModelMatrix = GUMatrix4::translationMatrix(objectModelPos);
  78. GUMatrix4 groundModelMatrix = GUMatrix4::translationMatrix(groundModelPos);
  79. GUMatrix4 fireModelMatrix = GUMatrix4::translationMatrix(fireModelPos);
  80. GUMatrix4 wallModelMatrix = GUMatrix4::translationMatrix(wallModelPos);
  81.  
  82. GUPivotCamera *mainCamera = NULL;
  83.  
  84.  
  85. // Lights
  86. GLfloat light_ambient[] = { 0.1, 0.1, 0.1, 1.0 }; // Blue tint
  87. GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; // Blue tint
  88. GLfloat light_specular[] = { 10000.0, 10000.0, 10000.0, 1.0 }; // White highlight
  89. GLfloat light_position[] = { 50.0, 150.0, 70.0, 1.0 }; // Point light (w=1.0)
  90.  
  91. GLfloat light_ambient2[] = { 0.25, 0.2, 0.1, 1.0 }; // Blue tint
  92. GLfloat light_diffuse2[] = { 8.0, 0.4, 0.2, 1.0 }; // Blue tint
  93. GLfloat light_specular2[] = { 1.0, 1.0, 1.0, 1.0 }; // White highlight
  94. GLfloat light_position2[] = { 1.0, 8.0, 20.0, 1.0 }; // Point light (w=1.0)
  95.  
  96. // Materials
  97. GLfloat mat_amb_diff[] = { 1.2, 1.2, 1.2, 1.0 }; // Texture map will provide ambient and diffuse.
  98. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; // White highlight
  99. GLfloat mat_shininess[] = { 15.0 }; // Shiny surface
  100. GLfloat mat_emission[] = { 0.0, 0.0, 0.0, 1.0 }; // Not emissive
  101.  
  102.  
  103.  
  104. GLfloat ground_plane[] = { 0.0, 1.0, 0.0, 0.0 };
  105.  
  106.  
  107. // Function Prototypes
  108.  
  109. void init(int argc, char* argv[]);
  110. void report_version(void);
  111. void update(void);
  112. void display(void);
  113. void mouseButtonDown(int button_id, int state, int x, int y);
  114. void mouseMove(int x, int y);
  115. void keyDown(unsigned char key, int x, int y);
  116. void mouseWheelUpdate(int wheel_number, int direction, int x, int y);
  117.  
  118.  
  119. int _tmain(int argc, char* argv[]) {
  120.  
  121. init(argc, argv);
  122.  
  123. glutMainLoop();
  124.  
  125. // Shut down COM
  126. shutdownCOM();
  127.  
  128. return 0;
  129. }
  130.  
  131.  
  132. void init(int argc, char* argv[]) {
  133.  
  134. // Initialise COM so we can use Windows Imaging Component
  135. initCOM();
  136.  
  137. // Initialise FreeGLUT
  138. glutInit(&argc, argv);
  139.  
  140. glutInitContextVersion(3, 3);
  141. glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
  142. glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
  143.  
  144. glutInitWindowSize(800, 800);
  145. glutInitWindowPosition(64, 64);
  146. glutCreateWindow("GUPivotCamera Demo");
  147.  
  148. // Register callback functions
  149. glutDisplayFunc(display);
  150. glutKeyboardFunc(keyDown);
  151. glutMouseFunc(mouseButtonDown);
  152. glutMotionFunc(mouseMove);
  153. glutMouseWheelFunc(mouseWheelUpdate);
  154. // Initialise GLEW library
  155. GLenum err = glewInit();
  156.  
  157. // Ensure GLEW was initialised successfully before proceeding
  158. if (err == GLEW_OK) {
  159.  
  160. cout << "GLEW initialised okay\n";
  161.  
  162. } else {
  163.  
  164. cout << "GLEW could not be initialised\n";
  165. throw;
  166. }
  167.  
  168.  
  169. // Example query OpenGL state (get version number)
  170. report_version();
  171.  
  172. // Initialise OpenGL...
  173.  
  174. // Setup colour to clear the window
  175. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  176.  
  177. // setup main 3D camera
  178. mainCamera = new GUPivotCamera(-30.0f, 1.0f, 70.0f, 50.0f, 1.0f, 1.0f);
  179.  
  180. glEnable(GL_CULL_FACE);
  181. glFrontFace(GL_CCW);
  182.  
  183. glDepthFunc(GL_LEQUAL);
  184. glEnable(GL_DEPTH_TEST);
  185.  
  186. glLineWidth(4.0f);
  187.  
  188. // Shader setup - more on this next week!!!
  189. myShaderProgram = setupShaders(string("Shaders\\diffuse_vertex_shader.txt"), string("Shaders\\diffuse_fragment_shader.txt"));
  190.  
  191. // get unifom locations in shader
  192. gLPosition = glGetUniformLocation(myShaderProgram, "lposition");
  193. gAmbient = glGetUniformLocation(myShaderProgram, "lambient");
  194. gDiffuse = glGetUniformLocation(myShaderProgram, "ldiffuse");
  195. gAttenuation = glGetUniformLocation(myShaderProgram, "lattenuation");
  196. gEyePos = glGetUniformLocation(myShaderProgram, "eyePos");
  197.  
  198. // get material unifom locations in shader
  199. gMatAmbient = glGetUniformLocation(myShaderProgram, "matAmbient");
  200. gMatDiffuse = glGetUniformLocation(myShaderProgram, "matDiffuse");
  201. gMatSpecular = glGetUniformLocation(myShaderProgram, "matSpecular");
  202.  
  203. gModelMatrix = glGetUniformLocation(myShaderProgram, "g_ModelMatrix");
  204. gViewMatrix = glGetUniformLocation(myShaderProgram, "g_ViewMatrix");
  205. gProjectionMatrix = glGetUniformLocation(myShaderProgram, "g_ProjectionMatrix");
  206. gNormalMatrix = glGetUniformLocation(myShaderProgram, "g_NormalMatrix");
  207.  
  208.  
  209. // Load demo texture...
  210. //bearTexture = fiLoadTexture("..\\..\\Common\\Resources\\Textures\\bumblebee.png");
  211. bearTexture = fiLoadTexture("Resources\\Textures\\bear.tga");
  212. pyramidTexture = fiLoadTexture("Resources\\Textures\\fire.png");
  213. groundTexture = fiLoadTexture("Resources\\Textures\\ground.jpg");
  214. fireTexture = fiLoadTexture("Resources\\Textures\\fire2.jpg");
  215. wallTexture = fiLoadTexture("Resources\\Textures\\wall.jpg");
  216.  
  217.  
  218. // Enable Vertex Arrays
  219. // Tell OpenGL to expect vertex position information from an array
  220. glEnableClientState(GL_VERTEX_ARRAY);
  221. // Tell OpenGL to expect vertex colour information from an array
  222. glEnableClientState(GL_COLOR_ARRAY);
  223. // Tell OpenGL to expect vertex colour information from an array
  224. glEnableClientState(GL_NORMAL_ARRAY);
  225. // Tell OpenGL to expect texture coordinate information from an array
  226. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  227.  
  228. // load spaceship model
  229. bearModel[0] = new CGModel();
  230. objectModel[0] = new CGModel();
  231. groundModel[0] = new CGModel();
  232. fireModel[0] = new CGModel();
  233. wallModel[0] = new CGModel();
  234.  
  235. //importGSF(L"Resources\\Models\\dropship.gsf", bearModel);
  236.  
  237.  
  238. import3DS(L"Resources\\Models\\pole.3ds", fireModel[0]);
  239. import3DS(L"Resources\\Models\\bear.3ds", bearModel[0]);
  240. import3DS(L"Resources\\Models\\fire.3ds", objectModel[0]);
  241. import3DS(L"Resources\\Models\\ground.3ds", groundModel[0]);
  242. import3DS(L"Resources\\Models\\wall.3ds", wallModel[0]);
  243.  
  244. // importMD2(L"Resources\\Models\\tractor.md2", bearModel[0], 1);
  245.  
  246. bearModel[0]->setTextureForModel(bearTexture);
  247. objectModel[0]->setTextureForModel(pyramidTexture);
  248. groundModel[0]->setTextureForModel(groundTexture);
  249. fireModel[0]->setTextureForModel(fireTexture);
  250. wallModel[0]->setTextureForModel(wallTexture);
  251.  
  252. cgClock.start();
  253.  
  254.  
  255. }
  256.  
  257.  
  258. void report_version(void) {
  259.  
  260. int majorVersion, minorVersion;
  261.  
  262. glGetIntegerv(GL_MAJOR_VERSION, &majorVersion);
  263. glGetIntegerv(GL_MINOR_VERSION, &minorVersion);
  264.  
  265. cout << "OpenGL version " << majorVersion << "." << minorVersion << "\n\n";
  266. }
  267.  
  268. // Update Animated Scene Objects Using Delta Time
  269. void update(void) {
  270. // Animate Dropship
  271.  
  272. if (!lightToggled)
  273. {
  274. cgClock.tick();
  275. theta += cgClock.gameTimeDelta();
  276.  
  277. float tR = 12.55;
  278.  
  279. bearModelMatrix = GUMatrix4::translationMatrix(bearModelPos) *GUMatrix4::rotationMatrix(0.0f, (theta*0.5), 0.0f);
  280.  
  281. cout << theta << endl;
  282.  
  283. if (theta > tR)
  284. theta = 0;
  285.  
  286. float kappa = tR - theta;
  287. //cout << kappa << endl;
  288.  
  289. if (theta < 6.27)
  290. {
  291. glClearColor(theta*0.01, 0.0f, theta*0.1, 0.0f);
  292. }
  293. else
  294. {
  295. glClearColor(kappa*0.01, 0.0f, kappa*0.1, 0.0f);
  296. }
  297. }
  298. }
  299.  
  300. // Example rendering functions
  301. GUMatrix4 buildShadowMatrix(GLfloat groundplane[4], GLfloat lightpos[4])
  302. {
  303.  
  304. GLfloat dot;
  305. GLfloat shadowMat[4][4];
  306. /* Find dot product between light position vector and ground plane normal. */
  307. int X = 0, Y = 1, Z = 2, W = 3;
  308. dot = groundplane[X] * lightpos[X] + groundplane[Y] * lightpos[Y] + groundplane[Z] * lightpos[Z] + groundplane[W] * lightpos[W];
  309. shadowMat[0][0] = dot - lightpos[X] * groundplane[X];
  310. shadowMat[1][0] = 0.f - lightpos[X] * groundplane[Y];
  311. shadowMat[2][0] = 0.f - lightpos[X] * groundplane[Z];
  312. shadowMat[3][0] = 0.f - lightpos[X] * groundplane[W];
  313. shadowMat[X][1] = 0.f - lightpos[Y] * groundplane[X];
  314. shadowMat[1][1] = dot - lightpos[Y] * groundplane[Y];
  315. shadowMat[2][1] = 0.f - lightpos[Y] * groundplane[Z];
  316. shadowMat[3][1] = 0.f - lightpos[Y] * groundplane[W];
  317. shadowMat[X][2] = 0.f - lightpos[Z] * groundplane[X];
  318. shadowMat[1][2] = 0.f - lightpos[Z] * groundplane[Y];
  319. shadowMat[2][2] = dot - lightpos[Z] * groundplane[Z];
  320. shadowMat[3][2] = 0.f - lightpos[Z] * groundplane[W];
  321. shadowMat[X][3] = 0.f - lightpos[W] * groundplane[X];
  322. shadowMat[1][3] = 0.f - lightpos[W] * groundplane[Y];
  323. shadowMat[2][3] = 0.f - lightpos[W] * groundplane[Z];
  324. shadowMat[3][3] = dot - lightpos[W] * groundplane[W];
  325. GUVector4 temp[4];
  326. for (int i = 0; i < 4; i++)
  327. temp[i] = GUVector4(shadowMat[i][0], shadowMat[i][1], shadowMat[i][2], shadowMat[i][3]);
  328.  
  329.  
  330. GUMatrix4 shadowMatrix = GUMatrix4(temp[0], temp[1], temp[2], temp[3]);
  331.  
  332. return shadowMatrix;
  333. }
  334.  
  335. void display(void) {
  336.  
  337. // Update Animated Scene Objects Using Delta Time
  338. update();
  339.  
  340. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  341.  
  342. // get camera view and projection transforms
  343. GUMatrix4 viewMatrix = mainCamera->viewTransform();
  344. GUMatrix4 projMatrix = mainCamera->projectionTransform();
  345. GUMatrix4 shadowMatrix = buildShadowMatrix(ground_plane, light_position);
  346. GUMatrix4 modelViewMatrix = viewMatrix * bearModelMatrix;
  347.  
  348. if (FIXED_FUNCTION)
  349. {
  350. glUseProgram(0);
  351.  
  352. // set fixed-function projection transform
  353. glMatrixMode(GL_PROJECTION);
  354. glLoadMatrixf((GLfloat*)&projMatrix);
  355.  
  356. // set fixed-function model-view transform
  357. glMatrixMode(GL_MODELVIEW);
  358. GUMatrix4 modelViewMatrix = viewMatrix * bearModelMatrix;
  359. glLoadMatrixf((GLfloat*)&modelViewMatrix);
  360.  
  361. glEnable(GL_TEXTURE_2D);
  362.  
  363. // Turn on light
  364. glEnable(GL_LIGHTING);
  365. glEnable(GL_LIGHT0);
  366. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  367. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  368. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  369. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  370.  
  371. glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0);
  372. glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.5);
  373. glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.05);
  374.  
  375. //Light2
  376.  
  377. glEnable(GL_LIGHT1);
  378. glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient2);
  379. glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse2);
  380. glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular2);
  381. glLightfv(GL_LIGHT1, GL_POSITION, light_position2);
  382.  
  383. glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5);
  384. glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5);
  385. //glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.05);
  386.  
  387. // Set Material properties for object
  388. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);
  389. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  390. glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  391. glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  392.  
  393.  
  394.  
  395.  
  396. }
  397. else{
  398. // Get Camera Position in World Coordinates
  399. GUVector4 eyePos = mainCamera->cameraLocation();
  400.  
  401. // Get Normal Matrix (transform Normals to World Coordinates)
  402. GUMatrix4 normalMatrix = bearModelMatrix.inverseTranspose();
  403.  
  404. // Render using finished shader
  405. glUseProgram(myShaderProgram);
  406. glUniform4fv(gDiffuse, 1, (GLfloat*)&light_diffuse);
  407. glUniform4fv(gAmbient, 1, (GLfloat*)&light_ambient);
  408. //glUniform4fv(gLPosition, 1, (GLfloat*)&light_position);
  409. glUniform4fv(gEyePos, 1, (GLfloat*)&eyePos);
  410. GLfloat attenuation[] = { 1.0, 0.5, 0.05 };
  411.  
  412. glUniform3fv(gAttenuation, 1, (GLfloat*)&attenuation);
  413. glUniformMatrix4fv(gModelMatrix, 1, false, (GLfloat*)&bearModelMatrix);
  414. glUniformMatrix4fv(gViewMatrix, 1, false, (GLfloat*)&viewMatrix);
  415. glUniformMatrix4fv(gProjectionMatrix, 1, false, (GLfloat*)&projMatrix);
  416. glUniformMatrix4fv(gNormalMatrix, 1, false, (GLfloat*)&normalMatrix);
  417.  
  418. // Material Properties
  419. glUniform4fv(gMatAmbient, 1, (GLfloat*)&mat_amb_diff);
  420. glUniform4fv(gMatDiffuse, 1, (GLfloat*)&mat_amb_diff);
  421. // The shader requires shininess in w parameter hence:
  422. GLfloat mat_specular_shader[] = { mat_specular[0], mat_specular[1], mat_specular[2], mat_shininess[0] }; // White highlight
  423. glUniform4fv(gMatSpecular, 1, (GLfloat*)&mat_specular_shader);
  424.  
  425. }
  426.  
  427. if (bearModel)
  428. bearModel[0]->renderTexturedModel();
  429.  
  430. if (objectModel)
  431. objectModel[0]->renderTexturedModel();
  432.  
  433. if (groundModel)
  434. groundModel[0]->renderTexturedModel();
  435.  
  436. if (fireModel)
  437. fireModel[0]->renderTexturedModel();
  438.  
  439. if (wallModel)
  440. wallModel[0]->renderTexturedModel();
  441.  
  442.  
  443. if (FIXED_FUNCTION)
  444. {
  445. // Turn off light
  446. glDisable(GL_LIGHTING);
  447. // Load shadowMatrix
  448. modelViewMatrix = viewMatrix*shadowMatrix*bearModelMatrix * objectModelMatrix;
  449. glLoadMatrixf((GLfloat*)&modelViewMatrix);
  450. glColor4ub(0, 0, 0, 100);
  451. }
  452. // Render Shadow
  453. if (bearModel)
  454. bearModel[0]->render();
  455.  
  456. if (objectModel)
  457. objectModel[0]->render();
  458.  
  459. if (wallModel)
  460. wallModel[0]->render();
  461.  
  462. glutSwapBuffers();
  463.  
  464. glutPostRedisplay(); //Redraw is required for animation
  465. }
  466.  
  467. #pragma region Event handling functions
  468.  
  469. void mouseButtonDown(int button_id, int state, int x, int y) {
  470.  
  471. if (button_id == GLUT_LEFT_BUTTON) {
  472.  
  473. if (state == GLUT_DOWN) {
  474.  
  475. mouse_x = x;
  476. mouse_y = y;
  477.  
  478. mDown = true;
  479.  
  480. }
  481. else if (state == GLUT_UP) {
  482.  
  483. mDown = false;
  484. }
  485. }
  486. }
  487.  
  488.  
  489. void mouseMove(int x, int y) {
  490.  
  491. if (mDown) {
  492.  
  493. int dx = x - mouse_x;
  494. int dy = y - mouse_y;
  495.  
  496. theta += (float)dy * (3.142f * 0.01f);
  497.  
  498. // rotate camera around the origin
  499. if (mainCamera)
  500. mainCamera->transformCamera((float)-dy, (float)-dx, 0.0f);
  501.  
  502. mouse_x = x;
  503. mouse_y = y;
  504.  
  505. glutPostRedisplay();
  506. }
  507. }
  508.  
  509.  
  510. void mouseWheelUpdate(int wheel_number, int direction, int x, int y) {
  511.  
  512. if (mainCamera) {
  513.  
  514. if (direction<0)
  515. mainCamera->scaleCameraRadius(1.1f);
  516. else if (direction>0)
  517. mainCamera->scaleCameraRadius(0.9f);
  518.  
  519. glutPostRedisplay();
  520. }
  521. }
  522.  
  523.  
  524. void keyDown(unsigned char key, int x, int y) {
  525.  
  526. if (key == 'r') {
  527.  
  528. theta = 0.0f;
  529. glutPostRedisplay();
  530. }
  531.  
  532. if (key == 'x') {
  533.  
  534.  
  535. if (!lightToggled)
  536. {
  537. cgClock.stop();
  538. lightToggled = true;
  539. }
  540. else
  541. {
  542. cgClock.start();
  543. lightToggled = false;
  544. }
  545.  
  546. glutPostRedisplay();
  547. }
  548.  
  549. }
  550.  
  551. #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement