3DBox.cpp
上传用户:eehhbb
上传日期:2022-08-03
资源大小:2550k
文件大小:6k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // 3DBox.cpp: implementation of the C3DBox class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "jhy3d.h"
  6. #include "3DBox.h"
  7. #include "Material.h"
  8. #include "glglut.h"
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char THIS_FILE[]=__FILE__;
  12. #define new DEBUG_NEW
  13. #endif
  14. //////////////////////////////////////////////////////////////////////
  15. // Construction/Destruction
  16. //////////////////////////////////////////////////////////////////////
  17. C3DBox::C3DBox()
  18. {
  19. }
  20. C3DBox::~C3DBox()
  21. {
  22. }
  23. void C3DBox::Draw3dBox()
  24. {
  25. glBegin(GL_QUADS); // 开始绘制立方体
  26. glTranslatef(m_position.m_x,m_position.m_y,m_position.m_z);
  27. /*
  28. glNormal3f(0.0f,1.0f,0.0f); 
  29. glVertex3f( 0.7f, 0.5f,-0.5f); // 四边形的右上顶点 (顶面)
  30. glVertex3f(-0.7f, 0.5f,-0.5f); // 四边形的左上顶点 (顶面)
  31. glVertex3f(-0.7f, 0.5f, 0.5f); // 四边形的左下顶点 (顶面)
  32. glVertex3f( 0.7f, 0.5f, 0.5f); // 四边形的右下顶点 (顶面)
  33. glNormal3f(0.0f,-1.0f,0.0f); 
  34. glVertex3f( 0.7f,-0.5f, 0.5f); // 四边形的右上顶点(底面)
  35. glVertex3f(-0.7f,-0.5f, 0.5f); // 四边形的左上顶点(底面)
  36. glVertex3f(-0.7f,-0.5f,-0.5f); // 四边形的左下顶点(底面)
  37. glVertex3f( 0.7f,-0.5f,-0.5f); // 四边形的右下顶点(底面)
  38. glNormal3f(0.0f,0.0f,1.0f); 
  39. glVertex3f( 0.7f, 0.5f, 0.5f); // 四边形的右上顶点(前面)
  40. glVertex3f(-0.7f, 0.5f, 0.5f); // 四边形的左上顶点(前面)
  41. glVertex3f(-0.7f,-0.5f, 0.5f); // 四边形的左下顶点(前面)
  42. glVertex3f( 0.7f,-0.5f, 0.5f); // 四边形的右下顶点(前面)
  43. glNormal3f(0.0f,0.0f,-1.0f); 
  44. glVertex3f( 0.7f,-0.5f,-0.5f); // 四边形的右上顶点(后面)
  45. glVertex3f(-0.7f,-0.5f,-0.5f); // 四边形的左上顶点(后面)
  46. glVertex3f(-0.7f, 0.5f,-0.5f); // 四边形的左下顶点(后面)
  47. glVertex3f( 0.7f, 0.5f,-0.5f); // 四边形的右下顶点(后面)
  48. glNormal3f(-1.0f,0.0f,0.0f); 
  49. glVertex3f(-0.7f, 0.5f, 0.5f); // 四边形的右上顶点(左面)
  50. glVertex3f(-0.7f, 0.5f,-0.5f); // 四边形的左上顶点(左面)
  51. glVertex3f(-0.7f,-0.5f,-0.5f); // 四边形的左下顶点(左面)
  52. glVertex3f(-0.7f,-0.5f, 0.5f); // 四边形的右下顶点(左面)
  53. glNormal3f(1.0f,0.0f,0.0f); 
  54. glVertex3f( 0.7f, 0.5f,-0.5f); // 四边形的右上顶点(右面)
  55. glVertex3f( 0.7f, 0.5f, 0.5f); // 四边形的左上顶点(右面)
  56. glVertex3f( 0.7f,-0.5f, 0.5f); // 四边形的左下顶点(右面)
  57. glVertex3f( 0.7f,-0.5f,-0.5f); // 四边形的右下顶点(右面)
  58. glEnd(); // 立方体绘制结束
  59. */
  60. glNormal3f(0.0f,1.0f,0.0f); 
  61. glTexCoord2f(1.0f, 1.0f); 
  62. glVertex3f(m_vBox[0].m_x,m_vBox[0].m_y,m_vBox[1].m_z);// 四边形的右上顶点(顶面)
  63. glTexCoord2f(0.0f, 1.0f);
  64. glVertex3f(m_vBox[1].m_x,m_vBox[0].m_y,m_vBox[1].m_z);// 四边形的左上顶点(顶面)
  65. glTexCoord2f(0.0f, 0.0f);
  66. glVertex3f(m_vBox[1].m_x,m_vBox[0].m_y,m_vBox[0].m_z); // 四边形的左下顶点(顶面)
  67. glTexCoord2f(1.0f, 0.0f);
  68. glVertex3f(m_vBox[0].m_x,m_vBox[0].m_y,m_vBox[0].m_z);// 四边形的右下顶点(顶面)
  69. glNormal3f(0.0f,-1.0f,0.0f); 
  70. glTexCoord2f(1.0f, 1.0f); 
  71. glVertex3f( m_vBox[0].m_x,m_vBox[1].m_y,m_vBox[0].m_z); // 四边形的右上顶点(底面)
  72. glTexCoord2f(0.0f, 1.0f);
  73. glVertex3f(m_vBox[1].m_x,m_vBox[1].m_y,m_vBox[0].m_z); // 四边形的左上顶点(底面)
  74. glTexCoord2f(0.0f, 0.0f);
  75. glVertex3f(m_vBox[1].m_x,m_vBox[1].m_y,m_vBox[1].m_z); // 四边形的左下顶点(底面)
  76. glTexCoord2f(1.0f, 0.0f);
  77. glVertex3f(m_vBox[0].m_x,m_vBox[1].m_y,m_vBox[1].m_z); // 四边形的右下顶点(底面)
  78. glNormal3f(0.0f,0.0f,1.0f); 
  79. glTexCoord2f(1.0f, 1.0f);
  80. glVertex3f(m_vBox[0].m_x,m_vBox[0].m_y,m_vBox[0].m_z); // 四边形的右上顶点(前面)
  81. glTexCoord2f(0.0f, 1.0f);
  82. glVertex3f(m_vBox[1].m_x,m_vBox[0].m_y,m_vBox[0].m_z); // 四边形的左上顶点(前面)
  83. glTexCoord2f(0.0f, 0.0f);
  84. glVertex3f(m_vBox[1].m_x,m_vBox[1].m_y,m_vBox[0].m_z); // 四边形的左下顶点(前面)
  85. glTexCoord2f(1.0f, 0.0f);
  86. glVertex3f(m_vBox[0].m_x,m_vBox[1].m_y,m_vBox[0].m_z); // 四边形的右下顶点(前面)
  87. glNormal3f(0.0f,0.0f,-1.0f); 
  88. glTexCoord2f(1.0f, 1.0f);
  89. glVertex3f(m_vBox[0].m_x,m_vBox[1].m_y,m_vBox[1].m_z); // 四边形的右上顶点(后面)
  90. glTexCoord2f(0.0f, 1.0f);
  91. glVertex3f(m_vBox[1].m_x,m_vBox[1].m_y,m_vBox[1].m_z); // 四边形的左上顶点(后面)
  92. glTexCoord2f(0.0f, 0.0f);
  93. glVertex3f(m_vBox[1].m_x,m_vBox[0].m_y,m_vBox[1].m_z); // 四边形的左下顶点(后面)
  94. glTexCoord2f(1.0f, 0.0f);
  95. glVertex3f(m_vBox[0].m_x,m_vBox[0].m_y,m_vBox[1].m_z); // 四边形的右下顶点(后面)
  96. glNormal3f(-1.0f,0.0f,0.0f); 
  97. glTexCoord2f(1.0f, 1.0f);
  98. glVertex3f(m_vBox[1].m_x,m_vBox[0].m_y,m_vBox[0].m_z); // 四边形的右上顶点(左面)
  99. glTexCoord2f(0.0f, 1.0f);
  100. glVertex3f(m_vBox[1].m_x,m_vBox[0].m_y,m_vBox[1].m_z); // 四边形的左上顶点(左面)
  101. glTexCoord2f(0.0f, 0.0f);
  102. glVertex3f(m_vBox[1].m_x,m_vBox[1].m_y,m_vBox[1].m_z); // 四边形的左下顶点(左面)
  103. glTexCoord2f(1.0f, 0.0f);
  104. glVertex3f(m_vBox[1].m_x,m_vBox[1].m_y,m_vBox[0].m_z); // 四边形的右下顶点(左面)
  105. glNormal3f(1.0f,0.0f,0.0f); 
  106. glTexCoord2f(1.0f, 1.0f);
  107. glVertex3f(m_vBox[0].m_x,m_vBox[0].m_y,m_vBox[1].m_z); // 四边形的右上顶点(右面)
  108. glTexCoord2f(0.0f, 1.0f);
  109. glVertex3f(m_vBox[0].m_x,m_vBox[0].m_y,m_vBox[0].m_z); // 四边形的左上顶点(右面)
  110. glTexCoord2f(0.0f, 0.0f);
  111. glVertex3f(m_vBox[0].m_x,m_vBox[1].m_y,m_vBox[0].m_z); // 四边形的左下顶点(右面)
  112. glTexCoord2f(1.0f, 0.0f);
  113. glVertex3f(m_vBox[0].m_x,m_vBox[1].m_y,m_vBox[1].m_z); // 四边形的右下顶点(右面)
  114. glEnd(); // 立方体绘制结束
  115. }
  116. void C3DBox::RenderLightSolid3dBox()
  117. {
  118. glMaterialfv(GL_FRONT,GL_AMBIENT,m_matBox.m_vAmbient);
  119. glMaterialfv(GL_FRONT,GL_DIFFUSE,m_matBox.m_vDiffuse);
  120. glMaterialfv(GL_FRONT,GL_SPECULAR,m_matBox.m_vSpecular);
  121. glMaterialf(GL_FRONT,GL_SHININESS,m_matBox.m_vShininess);
  122. glMaterialfv(GL_FRONT,GL_EMISSION,m_matBox.m_vEmission);
  123. glPushMatrix();
  124. glTranslatef(m_position.m_x,m_position.m_y,m_position.m_z);
  125. Draw3dBox();
  126. glPopMatrix();
  127. }