Guest User

Untitled

a guest
Feb 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. #include "Triangle.h"
  2.  
  3.  
  4. Triangle::Triangle(float scale, float position)
  5. : Model()
  6. {
  7. mVertices[0][0]= -5.0f; mVertices[0][1]= 0.0f; mVertices[0][2]= 5.0f;
  8. mVertices[1][0]= 5.0f; mVertices[1][1]= 0.0f; mVertices[1][2]= 0.0f;
  9. mVertices[2][0]= 0.0f; mVertices[2][1]= 0.0f; mVertices[2][2]= -5.0f;
  10. mScaleFactor = scale;
  11. mYPosition = position;
  12. mFloorShadow = true;
  13. }
  14.  
  15.  
  16. Triangle::~Triangle(void)
  17. {
  18. }
  19.  
  20.  
  21. void Triangle::draw()
  22. {
  23. glColor3f(0.0f,0.0f,0.0f);
  24. glPushMatrix();
  25. glTranslatef(0.0f, mYPosition, 0.0f);
  26. glBegin( GL_TRIANGLES );
  27. //Want to avoid culling
  28. for ( int i = 0; i < 3; ++i )
  29. {
  30. glVertex3f(mVertices[i][0], mVertices[i][1], mVertices[i][2]);
  31. }
  32. for ( int i = 2; i > -1; --i )
  33. {
  34. glVertex3f(mVertices[i][0], mVertices[i][1], mVertices[i][2]);
  35. }
  36. glEnd();
  37. glPopMatrix();
  38. }
  39. void Triangle::draw_bottom()
  40. {
  41. glColor3f(1.0f,1.0f,1.0f);
  42. glPushMatrix();
  43. glTranslatef(0.0f, mYPosition, 0.0f);
  44. glBegin(GL_TRIANGLES);
  45. for ( int i = 0; i < 3; ++i )
  46. {
  47. glVertex3f(mBottomVertices[i][0], mBottomVertices[i][1], mBottomVertices[i][2]);
  48. }
  49. for ( int i = 2; i > -1; --i )
  50. {
  51. glVertex3f(mBottomVertices[i][0], mBottomVertices[i][1], mBottomVertices[i][2]);
  52. }
  53. glEnd();
  54. glPopMatrix();
  55. }
  56.  
  57. void Triangle::build_volume(GLfloat* light_pos, float* camera_pos)
  58. {
  59. static float volume_size = 10.0f; //Some big number that will for sure cover our needs
  60. //Initialize our bottom vertices to our top ones....
  61. /*
  62. for ( int i = 0; i < 3; ++i )
  63. {
  64. mBottomVertices[i][0] = light_pos[0];
  65. mBottomVertices[i][1] = light_pos[1];
  66. mBottomVertices[i][2] = light_pos[2];
  67. }
  68. */
  69. // Given out light point at L, and our three vertices: A,B,C
  70. // the direction vectors for our volume can be generated as following.
  71. // v1 = A - L. This direction vector can then be scaled with out volume_size
  72. // maginute and added on to our initial A to give us out bottom vertice:
  73. // A' = A + volume_size * v1
  74. if (light_pos[1] > mYPosition)
  75. {
  76. mFloorShadow = true;
  77. for ( int i = 0; i < 3; ++i )
  78. {
  79. mBottomVertices[i][0] = light_pos[0] + (mVertices[i][0] - light_pos[0]) * volume_size;
  80. mBottomVertices[i][1] = light_pos[1] + (mVertices[i][1] - light_pos[1]) * volume_size;
  81. mBottomVertices[i][2] = light_pos[2]+ (mVertices[i][2] - light_pos[2]) * volume_size;
  82. }
  83. }
  84. else
  85. {
  86. mFloorShadow = false;
  87. for ( int i = 0; i < 3; ++i )
  88. {
  89. mBottomVertices[i][0] = light_pos[0] + (mVertices[i][0] + light_pos[0]) * volume_size;
  90. mBottomVertices[i][1] = light_pos[1] + (mVertices[i][1] + light_pos[1]) * volume_size;
  91. mBottomVertices[i][2] = light_pos[2] + (mVertices[i][2] + light_pos[2]) * volume_size;
  92.  
  93. }
  94. }
  95.  
  96. }
  97.  
  98. void Triangle::draw_volume()
  99. {
  100.  
  101. //glEnable(GL_CULL_FACE);
  102. //glCullFace(GL_FRONT);
  103.  
  104. glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  105.  
  106.  
  107. glPushMatrix();
  108. glTranslatef(0.0f, mYPosition, 0.0f);
  109. //Draw top cap...
  110. glBegin( GL_TRIANGLES );
  111. glColor4f(0.0f,0.0f,0.0f,0.3f);
  112. for ( int i = 2; i > -1; --i )
  113. {
  114. glVertex3f(mVertices[i][0], mVertices[i][1], mVertices[i][2]);
  115. }
  116. glEnd();
  117.  
  118. glColor4f(1.0f,1.0f,0.0f,0.3f);
  119. draw_quad(0,0,1,1);
  120. glColor4f(1.0f,0.0f,0.0f,0.3f);
  121. draw_quad(1,1,2,2);
  122. glColor4f(0.0f,1.0f,0.0f,0.3f);
  123. draw_quad(2,2,0,0);
  124. //Draw bottom cap...
  125. glBegin( GL_TRIANGLES );
  126. glColor4f(0.0f,0.0f,0.0f,0.3f);
  127. for ( int i = 0; i < 3; ++i )
  128. {
  129. glVertex3f(mBottomVertices[i][0], mBottomVertices[i][1], mBottomVertices[i][2]);
  130. }
  131. glEnd();
  132.  
  133. glPopMatrix();
  134.  
  135.  
  136. glDisable (GL_BLEND);
  137.  
  138. //glDisable(GL_CULL_FACE);
  139.  
  140.  
  141.  
  142. }
  143.  
  144. void Triangle::draw_quad(int first_top, int second_bottom, int third_bottom, int fourth_top)
  145. {
  146. /*cout << "Drawing quad from: ("<<mVertices[first_top][0]<<","<<mVertices[first_top][1]<<","<<mVertices[first_top][2]<<"), "
  147. << "("<<mBottomVertices[second_bottom][0]<<","<<mBottomVertices[second_bottom][1]<<","<<mBottomVertices[second_bottom][2]<<"), "
  148. << "("<<mBottomVertices[third_bottom][0]<<","<<mBottomVertices[third_bottom][1]<<","<<mBottomVertices[third_bottom][2]<<"), "
  149. << "("<<mVertices[fourth_top][0]<<","<<mVertices[fourth_top][1]<<","<<mVertices[fourth_top][2]<<"), " << endl;*/
  150. if (mFloorShadow)
  151. {
  152. glBegin(GL_QUADS);
  153. glVertex3f(mVertices[first_top][0], mVertices[first_top][1], mVertices[first_top][2]);
  154. glVertex3f(mBottomVertices[second_bottom][0], mBottomVertices[second_bottom][1],mBottomVertices[second_bottom][2]);
  155. glVertex3f(mBottomVertices[third_bottom][0], mBottomVertices[third_bottom][1],mBottomVertices[third_bottom][2]);
  156. glVertex3f(mVertices[fourth_top][0], mVertices[fourth_top][1], mVertices[fourth_top][2]);
  157. glEnd();
  158. }
  159. else
  160. //Reverse order to fix culling
  161. {
  162. glBegin(GL_QUADS);
  163. glVertex3f(mVertices[first_top][0], mVertices[first_top][1], mVertices[first_top][2]);
  164. glVertex3f(mVertices[fourth_top][0], mVertices[fourth_top][1], mVertices[fourth_top][2]);
  165. glVertex3f(mBottomVertices[third_bottom][0], mBottomVertices[third_bottom][1],mBottomVertices[third_bottom][2]);
  166. glVertex3f(mBottomVertices[second_bottom][0], mBottomVertices[second_bottom][1],mBottomVertices[second_bottom][2]);
  167. glEnd();
  168. }
  169. }
Add Comment
Please, Sign In to add comment