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

游戏引擎

开发平台:

Visual C++

  1. // Grass.cpp: implementation of the CGrass class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Grass.h"
  6. #include "Texmanager.h"
  7. #include "math.h"
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. CGrass::CGrass()
  12. {
  13. m_size=8.0f;
  14.     m_height=m_size*0.8f;
  15. }
  16. CGrass::~CGrass()
  17. {
  18. }
  19. bool CGrass::InitGrass(int type)
  20. {
  21.  m_type=type;
  22.  CTexManager cTexManager;
  23.  //////////////read blend skin texture
  24.  if(type==1)
  25.  texGrass=cTexManager.GetTextureID(TEX_GRASS_0);
  26.  else
  27.  texGrass=cTexManager.GetTextureID(TEX_GRASS_1);
  28.      return true;
  29. }
  30. void CGrass::RenderGrass(VERTEX pos)
  31. {
  32.     glPushMatrix();
  33. glTranslatef(pos.xpos,pos.ypos,pos.zpos);
  34.     glAlphaFunc(GL_GREATER,0.10f);
  35. glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  36. glEnable(GL_BLEND);
  37.     glEnable(GL_ALPHA_TEST);
  38. glDisable(GL_CULL_FACE);
  39. ////////////////////////////////////
  40. if(m_type==1)
  41.         DrawGrassStrip();
  42. else
  43. DrawGrassStar();
  44.    ///////////////////////////////////////////
  45.     glDisable(GL_ALPHA_TEST);
  46. glDisable(GL_BLEND); 
  47.     glPopMatrix();
  48. }
  49. void CGrass::DrawGrassStrip()
  50. {
  51.     //////////////////////////////////////////
  52.   glBindTexture(GL_TEXTURE_2D, texGrass);
  53. glEnable(GL_TEXTURE_2D);
  54.     ///////////////////////////////////////////
  55. glBegin(GL_QUAD_STRIP);
  56.     glTexCoord2f(1,0);
  57.     glVertex3f(m_size*0.3f,0,-m_size*0.5f);
  58.     glTexCoord2f(1,1);
  59.     glVertex3f(m_size*0.3f,m_height*1.4f,-m_size*0.5f);
  60.     glTexCoord2f(0,0);
  61.     glVertex3f(0,0,0);
  62.     glTexCoord2f(0,1);
  63.     glVertex3f(0,m_height*1.5f,0);
  64.     glTexCoord2f(1,0);
  65.     glVertex3f(m_size,0,0);
  66.     glTexCoord2f(1,1);
  67.     glVertex3f(m_size,m_height*1.5f,0);
  68.     glTexCoord2f(0,0);
  69.     glVertex3f(m_size*1.1f,0,-m_size*0.4f);
  70.     glTexCoord2f(0,1);
  71.     glVertex3f(m_size*1.1f,m_height*1.3f,-m_size*0.4f);
  72.     glTexCoord2f(1,0);
  73.     glVertex3f(m_size*0.2f,0,-m_size*1.2f);
  74.     glTexCoord2f(1,1);
  75.     glVertex3f(m_size*0.2f,m_height*1.3f,-m_size*1.2f);
  76.     glTexCoord2f(0,0);
  77.     glVertex3f(-m_size*0.3f,0,-m_size*0.6f);
  78.     glTexCoord2f(0,1);
  79.     glVertex3f(-m_size*0.3f,m_height*1.2f,-m_size*0.6f);
  80.     
  81.     glTexCoord2f(1,0);
  82.     glVertex3f(-m_size*0.2f,0,m_size*0.2f);
  83.     glTexCoord2f(1,1);
  84.     glVertex3f(-m_size*0.2f,m_height*1.2f,m_size*0.2f);
  85.     glTexCoord2f(0,0);
  86.     glVertex3f(m_size*0.9f,0,m_size*0.4f);
  87.     glTexCoord2f(0,1);
  88.     glVertex3f(m_size*0.9f,m_height*1.2f,m_size*0.4f);
  89.     glTexCoord2f(1,0);
  90.     glVertex3f(m_size*1.5f,0,-m_size*0.1f);
  91.     glTexCoord2f(1,1);
  92.     glVertex3f(m_size*1.5f,m_height*1.3f,-m_size*0.1f);
  93.     glTexCoord2f(0,0);
  94.     glVertex3f(m_size*1.2f,0,-m_size*1.0f);
  95.     glTexCoord2f(0,1);
  96.     glVertex3f(m_size*1.2f,m_height*1.1f,-m_size*1.0f);
  97.     glTexCoord2f(1,0);
  98.     glVertex3f(m_size*0.1f,0,-m_size*1.1f);
  99.     glTexCoord2f(1,1);
  100.     glVertex3f(m_size*0.1f,m_height*1.4f,-m_size*1.1f);
  101. glEnd();
  102. }
  103. void CGrass::DrawGrassStar()
  104. {
  105.   glBindTexture(GL_TEXTURE_2D, texGrass);
  106. DrawCross(60);
  107. }
  108. void CGrass::DrawCross(int angle)
  109. {
  110. int num=180/angle;
  111. glBegin(GL_QUADS);
  112. int curAngle=0;
  113. for(int i=0;i<num;i++)
  114. {
  115. curAngle=angle*i;
  116.     glTexCoord2f(0,0);
  117.     glVertex3f(m_size*cosf(0.0174533f*curAngle),0,m_size*sinf(0.0174533f*curAngle));    
  118. glTexCoord2f(1,0);
  119.     glVertex3f(m_size*cosf(0.0174533f*(curAngle+180)),0,m_size*sinf(0.0174533f*(curAngle+180)));    
  120.        glTexCoord2f(1,1);
  121.     glVertex3f(m_size*cosf(0.0174533f*(curAngle+180)),m_height,m_size*sinf(0.0174533f*(curAngle+180)));    
  122. glTexCoord2f(0,1);
  123.     glVertex3f(m_size*cosf(0.0174533f*curAngle),m_height,m_size*sinf(0.0174533f*curAngle));    
  124. }
  125.     glEnd();
  126. }