3DTree.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:9k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. // 3DTree.cpp: implementation of the C3DTree class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "3DTree.h"
  6. #include "Texmanager.h"
  7. #include "math.h"
  8. #include "heightmap.h"
  9. //////////////////////////////////////////////////////////////////////
  10. // Construction/Destruction
  11. //////////////////////////////////////////////////////////////////////
  12. C3DTree::C3DTree()
  13. {
  14. m_szTree=1;
  15.     m_height=m_szTree*60;
  16. }
  17. C3DTree::~C3DTree()
  18. {
  19. }
  20. bool C3DTree::Init3DTree(int type,float size)
  21. {
  22.  m_szTree = size;
  23.  m_height=m_szTree*60;
  24.  m_type=type;
  25.  CTexManager cTexManager;
  26.  //////////////read blend skin texture
  27.  if(type==1)
  28.  {
  29.          texTreeBody=cTexManager.GetTextureID(TEX_TREEBODY_0);
  30.  texBranch=cTexManager.GetTextureID(TEX_BRANCH_0);
  31.  }
  32.  else
  33.  {
  34.          texTreeBody=cTexManager.GetTextureID(TEX_TREEBODY_1);
  35.  texBranch=cTexManager.GetTextureID(TEX_BRANCH_1);
  36.  }
  37.      return true;
  38. }
  39. void C3DTree::Render3DTree(VERTEX pos)
  40. {
  41.     glPushMatrix();
  42. glTranslatef(pos.xpos,pos.ypos,pos.zpos);
  43.     Draw3DTree();
  44.     glPopMatrix();
  45. if(m_type==1)
  46. CHeightmap::m_numTriangles += 100 ;
  47. else
  48. CHeightmap::m_numTriangles += 68 ;
  49. }
  50. void C3DTree::Draw3DTree()
  51. {
  52.     ///////////////////////////////////
  53. //////////Draw Tree Body
  54.     glDisable(GL_ALPHA_TEST);
  55. glDisable(GL_BLEND);
  56.   glBindTexture(GL_TEXTURE_2D, texTreeBody);
  57. glEnable(GL_TEXTURE_2D);
  58. glBegin(GL_QUAD_STRIP);
  59.     for(float i=0;i<8;i++)
  60. {
  61.         glTexCoord2f(i*0.3f,0);    
  62. glVertex3f(-m_szTree*sinf(i*45*0.0174533f),0,-m_szTree*cosf(i*45*0.0174533f));
  63.         glTexCoord2f(i*0.3f+0.3f,8);    
  64. glVertex3f(-m_szTree*sinf(i*45*0.0174533f)*0.1f,m_height,-m_szTree*cosf(i*45*0.0174533f)*0.1f);
  65. }
  66.     glTexCoord2f(2.4f,0); 
  67.     glVertex3f(0,0,-m_szTree);
  68.     glTexCoord2f(2.7f,8);    
  69. glVertex3f(0,m_height,-m_szTree*0.1f);
  70.     glEnd();
  71.     //////////////////////////////////////////
  72. ///////////////// Draw Branch ////////////
  73.     glAlphaFunc(GL_GREATER,0.10f);
  74. glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  75. glEnable(GL_BLEND);
  76.     glEnable(GL_ALPHA_TEST);
  77.   glBindTexture(GL_TEXTURE_2D, texBranch);
  78. glEnable(GL_TEXTURE_2D);
  79.     
  80. if(m_type==1)
  81. DrawYellowTree();
  82. else
  83. DrawGreenTree();
  84. ////////////////////////////////////
  85.     glDisable(GL_ALPHA_TEST);
  86. glDisable(GL_BLEND); 
  87. }
  88. void C3DTree::DrawYellowTree()
  89. {
  90.     //////////////////////////////////
  91.     glPushMatrix();
  92.     //////////////////////////////////
  93. glTranslatef(0,m_height*0.24f,0);
  94. glRotatef(0,  0.0f,1.0f,0.0f);
  95.     DrawBranch(m_height*0.2f,m_height*0.2f);
  96. glTranslatef(0,m_height*0.02f,0);
  97. glRotatef(180,  0.0f,1.0f,0.0f);
  98.     DrawBranch(m_height*0.31f,m_height*0.31f);
  99.   
  100. glTranslatef(0,m_height*0.03f,0);
  101. glRotatef(70,  0.0f,1.0f,0.0f);
  102.     DrawBranch(m_height*0.32f,m_height*0.32f);
  103.  
  104. glTranslatef(0,m_height*0.03f,0);
  105. glRotatef(200,  0.0f,1.0f,0.0f);
  106.     DrawBranch(m_height*0.32f,m_height*0.32f);
  107. glTranslatef(0,m_height*0.03f,0);
  108. glRotatef(90,  0.0f,1.0f,0.0f);
  109.     DrawBranch(m_height*0.31f,m_height*0.31f);
  110.  
  111. glTranslatef(0,m_height*0.03f,0);
  112. glRotatef(150,  0.0f,1.0f,0.0f);
  113.     DrawBranch(m_height*0.31f,m_height*0.31f);
  114. glTranslatef(0,m_height*0.03f,0);
  115. glRotatef(80,  0.0f,1.0f,0.0f);
  116.     DrawBranch(m_height*0.31f,m_height*0.31f);
  117.  
  118. glTranslatef(0,m_height*0.031f,0);
  119. glRotatef(190,  0.0f,1.0f,0.0f);
  120.     DrawBranch(m_height*0.3f,m_height*0.3f);
  121. glTranslatef(0,m_height*0.03f,0);
  122. glRotatef(70,  0.0f,1.0f,0.0f);
  123.     DrawBranch(m_height*0.29f,m_height*0.29f);
  124. /*
  125. glTranslatef(0,m_height*0.05f,0);
  126. glRotatef(180,  0.0f,1.0f,0.0f);
  127.     DrawBranch(m_height*0.31f,m_height*0.31f);
  128.   
  129. glTranslatef(0,m_height*0.032f,0);
  130. glRotatef(110,  0.0f,1.0f,0.0f);
  131.     DrawBranch(m_height*0.29f,m_height*0.29f);
  132.  
  133. glTranslatef(0,m_height*0.032f,0);
  134. glRotatef(70,  0.0f,1.0f,0.0f);
  135.     DrawBranch(m_height*0.28f,m_height*0.28f);
  136. */
  137. glTranslatef(0,m_height*0.041f,0);
  138. glRotatef(190,  0.0f,1.0f,0.0f);
  139.     DrawBranch(m_height*0.26f,m_height*0.26f);
  140.  
  141. glTranslatef(0,m_height*0.031f,0);
  142. glRotatef(110,  0.0f,1.0f,0.0f);
  143.     DrawBranch(m_height*0.24f,m_height*0.24f);
  144. glTranslatef(0,m_height*0.031f,0);
  145. glRotatef(80,  0.0f,1.0f,0.0f);
  146.     DrawBranch(m_height*0.23f,m_height*0.23f);
  147. glTranslatef(0,m_height*0.03f,0);
  148. glRotatef(200,  0.0f,1.0f,0.0f);
  149.     DrawBranch(m_height*0.22f,m_height*0.22f);
  150. glTranslatef(0,m_height*0.03f,0);
  151. glRotatef(90,  0.0f,1.0f,0.0f);
  152.     DrawBranch(m_height*0.21f,m_height*0.21f);
  153.  
  154. glTranslatef(0,m_height*0.04f,0);
  155. glRotatef(150,  0.0f,1.0f,0.0f);
  156.     DrawBranch(m_height*0.20f,m_height*0.20f);
  157. glTranslatef(0,m_height*0.03f,0);
  158. glRotatef(200,  0.0f,1.0f,0.0f);
  159.     DrawBranch(m_height*0.19f,m_height*0.19f);
  160. glTranslatef(0,m_height*0.03f,0);
  161. glRotatef(90,  0.0f,1.0f,0.0f);
  162.     DrawBranch(m_height*0.18f,m_height*0.18f);
  163.  
  164. glTranslatef(0,m_height*0.03f,0);
  165. glRotatef(150,  0.0f,1.0f,0.0f);
  166.     DrawBranch(m_height*0.17f,m_height*0.17f);
  167. /*
  168. glTranslatef(0,m_height*0.03f,0);
  169. glRotatef(160,  0.0f,1.0f,0.0f);
  170.     DrawBranch(m_height*0.16f,m_height*0.16f);
  171.  
  172. glTranslatef(0,m_height*0.032f,0);
  173. glRotatef(190,  0.0f,1.0f,0.0f);
  174.     DrawBranch(m_height*0.15f,m_height*0.15f);
  175. glTranslatef(0,m_height*0.031f,0);
  176. glRotatef(100,  0.0f,1.0f,0.0f);
  177.     DrawBranch(m_height*0.14f,m_height*0.14f);
  178.   */
  179. glTranslatef(0,m_height*0.051f,0);
  180. glRotatef(190,  0.0f,1.0f,0.0f);
  181.     DrawBranch(m_height*0.13f,m_height*0.13f);
  182. glTranslatef(0,m_height*0.032f,0);
  183. glRotatef(120,  0.0f,1.0f,0.0f);
  184.     DrawBranch(m_height*0.12f,m_height*0.12f);
  185. ////////////top
  186. glTranslatef(0,-m_height*0.05f,0);
  187. DrawTop(m_height*0.2f,m_height*0.3f);
  188.    
  189. ////////////////////////////
  190. glPopMatrix();
  191. ////////////////////////////
  192. }
  193. void C3DTree::DrawGreenTree()
  194. {
  195.     //////////////////////////////////
  196.     glPushMatrix();
  197.     //////////////////////////////////
  198. glTranslatef(0,m_height*0.3f,0);
  199. glRotatef(0,  0.0f,1.0f,0.0f);
  200.     DrawBranch(m_height*0.2f,m_height*0.2f);
  201. glTranslatef(0,m_height*0.04f,0);
  202. glRotatef(170,  0.0f,1.0f,0.0f);
  203.     DrawBranch(m_height*0.25f,m_height*0.25f);
  204. glTranslatef(0,m_height*0.04f,0);
  205. glRotatef(80,  0.0f,1.0f,0.0f);
  206.     DrawBranch(m_height*0.3f,m_height*0.3f);
  207.   
  208. glTranslatef(0,m_height*0.04f,0);
  209. glRotatef(130,  0.0f,1.0f,0.0f);
  210.     DrawBranch(m_height*0.32f,m_height*0.32f);
  211.  
  212. glTranslatef(0,m_height*0.045f,0);
  213. glRotatef(200,  0.0f,1.0f,0.0f);
  214.     DrawBranch(m_height*0.32f,m_height*0.32f);
  215. glTranslatef(0,m_height*0.04f,0);
  216. glRotatef(90,  0.0f,1.0f,0.0f);
  217.     DrawBranch(m_height*0.28f,m_height*0.28f);
  218.  
  219. glTranslatef(0,m_height*0.045f,0);
  220. glRotatef(150,  0.0f,1.0f,0.0f);
  221.     DrawBranch(m_height*0.25f,m_height*0.25f);
  222. glTranslatef(0,m_height*0.05f,0);
  223. glRotatef(80,  0.0f,1.0f,0.0f);
  224.     DrawBranch(m_height*0.23f,m_height*0.23f);
  225.  
  226. glTranslatef(0,m_height*0.05f,0);
  227. glRotatef(90,  0.0f,1.0f,0.0f);
  228.     DrawBranch(m_height*0.2f,m_height*0.2f);
  229.  
  230. glTranslatef(0,m_height*0.05f,0);
  231. glRotatef(180,  0.0f,1.0f,0.0f);
  232.     DrawBranch(m_height*0.2f,m_height*0.2f);
  233.  
  234. glTranslatef(0,m_height*0.04f,0);
  235. glRotatef(90,  0.0f,1.0f,0.0f);
  236.     DrawBranch(m_height*0.16f,m_height*0.16f);
  237. glTranslatef(0,m_height*0.04f,0);
  238. glRotatef(180,  0.0f,1.0f,0.0f);
  239.     DrawBranch(m_height*0.15f,m_height*0.15f);
  240. ////////////top
  241. DrawTop(m_height*0.3f,m_height*0.3f);
  242.    
  243. ////////////////////////////
  244. glPopMatrix();
  245. ////////////////////////////
  246. }
  247. void C3DTree::DrawBranch(float width,float height)
  248. {
  249. glBegin(GL_QUADS);
  250.     ////////// face 1
  251.     glTexCoord2f(1,0);
  252.     glVertex3f(0,0,0);
  253.     glTexCoord2f(0,0);
  254.     glVertex3f(width,0,0);
  255.     glTexCoord2f(0,1);
  256.     glVertex3f(width,height,0);
  257.     glTexCoord2f(1,1);
  258.     glVertex3f(0,height,0);
  259.     ////////// face 2
  260.     glTexCoord2f(1,0);
  261.     glVertex3f(0,0,0);
  262.     glTexCoord2f(0,0);
  263.     glVertex3f(width*0.5f,height*0.5f,0.7071f*width);
  264.     glTexCoord2f(0,1);
  265.     glVertex3f(width,height,0);
  266.     glTexCoord2f(1,1);
  267.     glVertex3f(width*0.5f,height*0.5f,-0.7071f*width);
  268. glEnd();
  269. }
  270. void C3DTree::DrawTop(float width,float height)
  271. {
  272. glBegin(GL_QUADS);
  273.     ////////// face 1
  274.     glTexCoord2f(1,0);
  275.     glVertex3f(0,0,0);
  276.     glTexCoord2f(0,0);
  277.     glVertex3f(0.7071f*width,0.5f*height,0);
  278.     glTexCoord2f(0,1);
  279.     glVertex3f(0,height,0);
  280.     glTexCoord2f(1,1);
  281.     glVertex3f(-0.7071f*width,0.5f*height,0);
  282.     ////////// face 2
  283.     glTexCoord2f(1,0);
  284.     glVertex3f(0,0,0);
  285.     glTexCoord2f(0,0);
  286.     glVertex3f(0,height*0.5f,0.7071f*width);
  287.     glTexCoord2f(0,1);
  288.     glVertex3f(0,height,0);
  289.     glTexCoord2f(1,1);
  290.     glVertex3f(0,height*0.5f,-0.7071f*width);
  291. glEnd();
  292. }