Cube.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "Cube.h"
  2. #include "DXBuffer.h"
  3. #include "Textur.h"
  4. #include "Globals.h"
  5. #include "Model3DList.h"
  6. #include "TexturList.h"
  7. using namespace Framework;
  8. // Inhalt der Cube Klasse
  9. // Konstruktor
  10. // size: Die Größe des Würfels
  11. Cube::Cube( float size )
  12. : Model3D()
  13. {
  14. if( m3dRegister->hatModel( Standart3DTypes::würfel ) )
  15. model = m3dRegister->getModel( Standart3DTypes::würfel );
  16. else
  17. {
  18. model = new Model3DData();
  19. m3dRegister->addModel( model->getThis(), Standart3DTypes::würfel );
  20. float stdSize = 100;
  21. float left, right, top, bottom;
  22. // Calculate the screen coordinates of the left side of the bitmap.
  23. left = (float)( ( stdSize / 2.0 ) * -1 );
  24. // Calculate the screen coordinates of the right side of the bitmap.
  25. right = left + (float)stdSize;
  26. // Calculate the screen coordinates of the top of the bitmap.
  27. top = (float)( stdSize / 2.0 );
  28. // Calculate the screen coordinates of the bottom of the bitmap.
  29. bottom = top - (float)stdSize;
  30. float front = -stdSize / 2;
  31. float back = front + stdSize;
  32. Vertex3D *vertecies = new Vertex3D[ 24 ];
  33. vertecies[ 0 ].pos = Vec3<float >( left, top, front );
  34. vertecies[ 0 ].tPos = Vec2< float >( 0.f, 0.f );
  35. vertecies[ 1 ].pos = Vec3<float >( right, top, front );
  36. vertecies[ 1 ].tPos = Vec2< float >( 1.f, 0.f );
  37. vertecies[ 2 ].pos = Vec3<float >( left, bottom, front );
  38. vertecies[ 2 ].tPos = Vec2< float >( 0.f, 1.f );
  39. vertecies[ 3 ].pos = Vec3<float >( right, bottom, front );
  40. vertecies[ 3 ].tPos = Vec2< float >( 1.f, 1.f );
  41. vertecies[ 4 ].pos = Vec3<float >( left, top, back );
  42. vertecies[ 4 ].tPos = Vec2< float >( 0.0f, 0.0f );
  43. vertecies[ 5 ].pos = Vec3<float >( right, top, back );
  44. vertecies[ 5 ].tPos = Vec2< float >( 1.0f, 0.0f );
  45. vertecies[ 6 ].pos = Vec3<float >( left, bottom, back );
  46. vertecies[ 6 ].tPos = Vec2< float >( 0.0f, 1.0f );
  47. vertecies[ 7 ].pos = Vec3<float >( right, bottom, back );
  48. vertecies[ 7 ].tPos = Vec2< float >( 1.0f, 1.0f );
  49. vertecies[ 8 ].pos = Vec3<float >( left, top, front );
  50. vertecies[ 8 ].tPos = Vec2< float >( 1.f, 0.f );
  51. vertecies[ 9 ].pos = Vec3<float >( right, top, front );
  52. vertecies[ 9 ].tPos = Vec2< float >( 0.f, 0.f );
  53. vertecies[ 10 ].pos = Vec3<float >( left, bottom, front );
  54. vertecies[ 10 ].tPos = Vec2< float >( 1.f, 1.f );
  55. vertecies[ 11 ].pos = Vec3<float >( right, bottom, front );
  56. vertecies[ 11 ].tPos = Vec2< float >( 0.f, 1.f );
  57. vertecies[ 12 ].pos = Vec3<float >( left, top, back );
  58. vertecies[ 12 ].tPos = Vec2< float >( 0.0f, 0.0f );
  59. vertecies[ 13 ].pos = Vec3<float >( right, top, back );
  60. vertecies[ 13 ].tPos = Vec2< float >( 1.0f, 0.0f );
  61. vertecies[ 14 ].pos = Vec3<float >( left, bottom, back );
  62. vertecies[ 14 ].tPos = Vec2< float >( 0.0f, 1.0f );
  63. vertecies[ 15 ].pos = Vec3<float >( right, bottom, back );
  64. vertecies[ 15 ].tPos = Vec2< float >( 1.0f, 1.0f );
  65. vertecies[ 16 ].pos = Vec3<float >( left, top, front );
  66. vertecies[ 16 ].tPos = Vec2< float >( 0.f, 1.f );
  67. vertecies[ 17 ].pos = Vec3<float >( right, top, front );
  68. vertecies[ 17 ].tPos = Vec2< float >( 1.f, 1.f );
  69. vertecies[ 18 ].pos = Vec3<float >( left, bottom, front );
  70. vertecies[ 18 ].tPos = Vec2< float >( 0.f, 0.f );
  71. vertecies[ 19 ].pos = Vec3<float >( right, bottom, front );
  72. vertecies[ 19 ].tPos = Vec2< float >( 1.f, 0.f );
  73. vertecies[ 20 ].pos = Vec3<float >( left, top, back );
  74. vertecies[ 20 ].tPos = Vec2< float >( 0.0f, 0.0f );
  75. vertecies[ 21 ].pos = Vec3<float >( right, top, back );
  76. vertecies[ 21 ].tPos = Vec2< float >( 1.0f, 0.0f );
  77. vertecies[ 22 ].pos = Vec3<float >( left, bottom, back );
  78. vertecies[ 22 ].tPos = Vec2< float >( 0.0f, 1.0f );
  79. vertecies[ 23 ].pos = Vec3<float >( right, bottom, back );
  80. vertecies[ 23 ].tPos = Vec2< float >( 1.0f, 1.0f );
  81. model->setVertecies( vertecies, 24 );
  82. // front side
  83. Polygon3D *p = new Polygon3D();
  84. p->indexAnz = 6;
  85. p->indexList = new int[ p->indexAnz ];
  86. p->indexBuffer->setLänge( p->indexAnz * 4 );
  87. p->indexBuffer->setData( p->indexList );
  88. p->indexList[ 0 ] = 0;
  89. p->indexList[ 1 ] = 3;
  90. p->indexList[ 2 ] = 2;
  91. p->indexList[ 3 ] = 0;
  92. p->indexList[ 4 ] = 1;
  93. p->indexList[ 5 ] = 3;
  94. model->addPolygon( p );
  95. // back side
  96. p = new Polygon3D();
  97. p->indexAnz = 6;
  98. p->indexList = new int[ p->indexAnz ];
  99. p->indexBuffer->setLänge( p->indexAnz * 4 );
  100. p->indexBuffer->setData( p->indexList );
  101. p->indexList[ 0 ] = 4;
  102. p->indexList[ 1 ] = 6;
  103. p->indexList[ 2 ] = 7;
  104. p->indexList[ 3 ] = 4;
  105. p->indexList[ 4 ] = 7;
  106. p->indexList[ 5 ] = 5;
  107. model->addPolygon( p );
  108. // right side
  109. p = new Polygon3D();
  110. p->indexAnz = 6;
  111. p->indexList = new int[ p->indexAnz ];
  112. p->indexBuffer->setLänge( p->indexAnz * 4 );
  113. p->indexBuffer->setData( p->indexList );
  114. p->indexList[ 0 ] = 1 + 8;
  115. p->indexList[ 1 ] = 7 + 8;
  116. p->indexList[ 2 ] = 3 + 8;
  117. p->indexList[ 3 ] = 1 + 8;
  118. p->indexList[ 4 ] = 5 + 8;
  119. p->indexList[ 5 ] = 7 + 8;
  120. model->addPolygon( p );
  121. // left side
  122. p = new Polygon3D();
  123. p->indexAnz = 6;
  124. p->indexList = new int[ p->indexAnz ];
  125. p->indexBuffer->setLänge( p->indexAnz * 4 );
  126. p->indexBuffer->setData( p->indexList );
  127. p->indexList[ 0 ] = 0 + 8;
  128. p->indexList[ 1 ] = 2 + 8;
  129. p->indexList[ 2 ] = 6 + 8;
  130. p->indexList[ 3 ] = 0 + 8;
  131. p->indexList[ 4 ] = 6 + 8;
  132. p->indexList[ 5 ] = 4 + 8;
  133. model->addPolygon( p );
  134. // top side
  135. p = new Polygon3D();
  136. p->indexAnz = 6;
  137. p->indexList = new int[ p->indexAnz ];
  138. p->indexBuffer->setLänge( p->indexAnz * 4 );
  139. p->indexBuffer->setData( p->indexList );
  140. p->indexList[ 0 ] = 4 + 16;
  141. p->indexList[ 1 ] = 1 + 16;
  142. p->indexList[ 2 ] = 0 + 16;
  143. p->indexList[ 3 ] = 4 + 16;
  144. p->indexList[ 4 ] = 5 + 16;
  145. p->indexList[ 5 ] = 1 + 16;
  146. model->addPolygon( p );
  147. // down side
  148. p = new Polygon3D();
  149. p->indexAnz = 6;
  150. p->indexList = new int[ p->indexAnz ];
  151. p->indexBuffer->setLänge( p->indexAnz * 4 );
  152. p->indexBuffer->setData( p->indexList );
  153. p->indexList[ 0 ] = 6 + 16;
  154. p->indexList[ 1 ] = 2 + 16;
  155. p->indexList[ 2 ] = 3 + 16;
  156. p->indexList[ 3 ] = 6 + 16;
  157. p->indexList[ 4 ] = 3 + 16;
  158. p->indexList[ 5 ] = 7 + 16;
  159. model->addPolygon( p );
  160. }
  161. textur = new Model3DTextur();
  162. }
  163. // Setzt die Textur des Würfels, so dass sie an allen Seiten gleich ist
  164. // textur: Die Textur als Bild
  165. void Cube::setTextur( Bild *textur )
  166. {
  167. Textur *t = new Textur();
  168. t->setBildZ( textur );
  169. this->textur->setPolygonTextur( LINKS, t->getThis() );
  170. this->textur->setPolygonTextur( OBEN, t->getThis() );
  171. this->textur->setPolygonTextur( RECHTS, t->getThis() );
  172. this->textur->setPolygonTextur( UNTEN, t->getThis() );
  173. this->textur->setPolygonTextur( VORNE, t->getThis() );
  174. this->textur->setPolygonTextur( HINTEN, t );
  175. rend = 1;
  176. }
  177. // Setzt die Textur des Würfels, so dass sie an allen Seiten gleich ist
  178. // id: Die id der Textur. Sie muss im Textur Register des Frameworks registriert sein
  179. void Cube::setTextur( int id )
  180. {
  181. Textur *t = texturRegister->getTextur( id );
  182. if( !t )
  183. return;
  184. this->textur->setPolygonTextur( LINKS, t->getThis() );
  185. this->textur->setPolygonTextur( OBEN, t->getThis() );
  186. this->textur->setPolygonTextur( RECHTS, t->getThis() );
  187. this->textur->setPolygonTextur( UNTEN, t->getThis() );
  188. this->textur->setPolygonTextur( VORNE, t->getThis() );
  189. this->textur->setPolygonTextur( HINTEN, t );
  190. rend = 1;
  191. }
  192. // Setzt die Textur von einer bestimmten Seite des Würfels
  193. // textur: Die Textur als Bild
  194. // s: Die Seite, die gesetzt werden soll
  195. void Cube::setTextur( Bild *textur, CubeSeite s )
  196. {
  197. Textur *t = new Textur();
  198. t->setBildZ( textur );
  199. this->textur->setPolygonTextur( s, t );
  200. rend = 1;
  201. }
  202. // Setzt die Textur von einer bestimmten Seite des Würfels
  203. // id: Die id der Textur. Sie muss im Textur Register des Frameworks registriert sein
  204. // s: Die Seite, die gesetzt werden soll
  205. void Cube::setTextur( int id, CubeSeite s )
  206. {
  207. Textur *t = texturRegister->getTextur( id );
  208. if( !t )
  209. return;
  210. this->textur->setPolygonTextur( s, t );
  211. rend = 1;
  212. }
  213. // Verringert den Reference Counting Zähler. Wenn der Zähler 0 erreicht, wird das Zeichnung automatisch gelöscht.
  214. // return: 0.
  215. Model3D *Cube::release()
  216. {
  217. ref--;
  218. if( !ref )
  219. delete this;
  220. return 0;
  221. }