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

游戏引擎

开发平台:

Visual C++

  1. // Pine.cpp: implementation of the CPine class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Pine.h"
  6. #include "Texmanager.h"
  7. #include "math.h"
  8. #include "heightmap.h"
  9. //////////////////////////////////////////////////////////////////////
  10. // Construction/Destruction
  11. //////////////////////////////////////////////////////////////////////
  12. CPine::CPine()
  13. {
  14. m_szTree=0.2f;
  15.     m_height=m_szTree*60;
  16. }
  17. CPine::~CPine()
  18. {
  19. }
  20. bool CPine::InitPine(int type,float size)
  21. {
  22.  m_szTree = size;
  23.  m_height=m_szTree*60;
  24.  CTexManager cTexManager;
  25.  //////////////read blend skin texture
  26.  texTreeBody=cTexManager.GetTextureID(TEX_TREEBODY_1);
  27.      texBranch=cTexManager.GetTextureID(TEX_BRANCH_2);
  28.      return true;
  29. }
  30. void CPine::RenderPine(VERTEX pos)
  31. {
  32.     glPushMatrix();
  33. glTranslatef(pos.xpos,pos.ypos,pos.zpos);
  34.     DrawPine();
  35.     glPopMatrix();
  36. CHeightmap::m_numTriangles += 92 ;
  37. }
  38. void CPine::DrawPine()
  39. {
  40.     ///////////////////////////////////
  41. //////////Draw Tree Body
  42.     glDisable(GL_ALPHA_TEST);
  43. glDisable(GL_BLEND);
  44.   glBindTexture(GL_TEXTURE_2D, texTreeBody);
  45. glEnable(GL_TEXTURE_2D);
  46. glBegin(GL_QUAD_STRIP);
  47.     for(float i=0;i<8;i++)
  48. {
  49.         glTexCoord2f(i*0.3f,0);    
  50. glVertex3f(-m_szTree*sinf(i*45*0.0174533f),0,-m_szTree*cosf(i*45*0.0174533f));
  51.         glTexCoord2f(i*0.3f+0.3f,8);    
  52. glVertex3f(-m_szTree*sinf(i*45*0.0174533f)*0.1f,m_height,-m_szTree*cosf(i*45*0.0174533f)*0.1f);
  53. }
  54.     glTexCoord2f(2.4f,0);    
  55.     glVertex3f(0,0,-m_szTree);
  56.     glTexCoord2f(2.7f,8);    
  57. glVertex3f(0,m_height,-m_szTree*0.1f);
  58.     glEnd();
  59.     //////////////////////////////////////////
  60. ///////////////// Draw Branch ////////////
  61.     glAlphaFunc(GL_GREATER,0.10f);
  62. glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  63. glEnable(GL_BLEND);
  64.     glEnable(GL_ALPHA_TEST);
  65.   glBindTexture(GL_TEXTURE_2D, texBranch);
  66. glEnable(GL_TEXTURE_2D);
  67.     
  68.     //////////////////////////////////
  69.     glPushMatrix();
  70.     //////////////////////////////////
  71. glTranslatef(0,m_height*0.2f,0);
  72. glRotatef(0,  0.0f,1.0f,0.0f);
  73.     DrawBranch(m_height*0.2f);
  74. glTranslatef(0,m_height*0.04f,0);
  75. glRotatef(170,  0.0f,1.0f,0.0f);
  76.     DrawBranch(m_height*0.35f);
  77. glTranslatef(0,m_height*0.04f,0);
  78. glRotatef(80,  0.0f,1.0f,0.0f);
  79.     DrawBranch(m_height*0.38f);
  80.   
  81. glTranslatef(0,m_height*0.03f,0);
  82. glRotatef(130,  0.0f,1.0f,0.0f);
  83.     DrawBranch(m_height*0.38f);
  84. glTranslatef(0,m_height*0.04f,0);
  85. glRotatef(170,  0.0f,1.0f,0.0f);
  86.     DrawBranch(m_height*0.3f);
  87. glTranslatef(0,m_height*0.04f,0);
  88. glRotatef(80,  0.0f,1.0f,0.0f);
  89.     DrawBranch(m_height*0.32f);
  90.   
  91. glTranslatef(0,m_height*0.04f,0);
  92. glRotatef(130,  0.0f,1.0f,0.0f);
  93.     DrawBranch(m_height*0.3f);
  94.  
  95. glTranslatef(0,m_height*0.035f,0);
  96. glRotatef(200,  0.0f,1.0f,0.0f);
  97.     DrawBranch(m_height*0.3f);
  98. glTranslatef(0,m_height*0.035f,0);
  99. glRotatef(90,  0.0f,1.0f,0.0f);
  100.     DrawBranch(m_height*0.2f);
  101. glTranslatef(0,m_height*0.05f,0);
  102. glRotatef(150,  0.0f,1.0f,0.0f);
  103.     DrawBranch(m_height*0.28f);
  104. glTranslatef(0,m_height*0.055f,0);
  105. glRotatef(80,  0.0f,1.0f,0.0f);
  106.     DrawBranch(m_height*0.28f);
  107.  
  108. glTranslatef(0,m_height*0.055f,0);
  109. glRotatef(90,  0.0f,1.0f,0.0f);
  110.     DrawBranch(m_height*0.3f);
  111.  
  112. glTranslatef(0,m_height*0.035f,0);
  113. glRotatef(180,  0.0f,1.0f,0.0f);
  114.     DrawBranch(m_height*0.18f);
  115.  
  116. glTranslatef(0,m_height*0.05f,0);
  117. glRotatef(150,  0.0f,1.0f,0.0f);
  118.     DrawBranch(m_height*0.25f);
  119. glTranslatef(0,m_height*0.05f,0);
  120. glRotatef(80,  0.0f,1.0f,0.0f);
  121.     DrawBranch(m_height*0.22f);
  122.  
  123. glTranslatef(0,m_height*0.055f,0);
  124. glRotatef(90,  0.0f,1.0f,0.0f);
  125.     DrawBranch(m_height*0.2f);
  126.  
  127. glTranslatef(0,m_height*0.045f,0);
  128. glRotatef(180,  0.0f,1.0f,0.0f);
  129.     DrawBranch(m_height*0.18f);
  130.  
  131. glTranslatef(0,m_height*0.03f,0);
  132. glRotatef(90,  0.0f,1.0f,0.0f);
  133.     DrawBranch(m_height*0.1f);
  134. glTranslatef(0,m_height*0.03f,0);
  135. glRotatef(180,  0.0f,1.0f,0.0f);
  136.     DrawBranch(m_height*0.14f);
  137.   
  138. ////////////////////////////
  139. glPopMatrix();
  140. ////////////////////////////
  141.     glDisable(GL_ALPHA_TEST);
  142. glDisable(GL_BLEND); 
  143. }
  144. void CPine::DrawBranch(float width)
  145. {
  146. glBegin(GL_TRIANGLE_STRIP);
  147.     ////////// face 
  148.     glTexCoord2f(0,0);
  149.     glVertex3f(width*0.5f,-width*0.4f,0.7071f*width);
  150.     glTexCoord2f(0,1);
  151.     glVertex3f(width,-width*0.1f,0);
  152.     glTexCoord2f(1,0);
  153.     glVertex3f(0,0,0);
  154.     glTexCoord2f(1,1);
  155.     glVertex3f(width*0.5f,-width*0.4f,-0.7071f*width);
  156. glEnd();
  157. }