render_data.cpp
上传用户:gzqinmao
上传日期:2022-07-13
资源大小:472k
文件大小:3k
源码类别:

OpenGL

开发平台:

Visual C++

  1. #include "Vector4.h"
  2. #include "render_data.h"
  3. #include "gut.h"
  4. // 镜头位置
  5. Vector4 g_eye(0.0f, 0.0f, 15.0f); 
  6. // 镜头对准的点
  7. Vector4 g_lookat(0.0f, 0.0f, 0.0f); 
  8. // 镜头正上方的方向
  9. Vector4 g_up(0.0f, 1.0f, 0.0f); 
  10. // 镜头转换矩阵
  11. Matrix4x4 g_view_matrix;
  12. //
  13. Matrix4x4 g_sun_matrix, g_earth_matrix, g_moon_matrix;
  14. // 球的模型
  15. Vertex_VC *g_pSunVertices = NULL;
  16. Vertex_VC *g_pEarthVertices = NULL;
  17. Vertex_VC *g_pMoonVertices = NULL;
  18. unsigned short *g_pSphereIndices = NULL;
  19. int g_iNumSphereVertices = 0;
  20. int g_iNumSphereTriangles = 0;
  21. int g_iNumSphereIndices = 0;
  22. //
  23. bool CreateSphere(float radius, // 半径 
  24.   Vertex_VC **ppVertices, // 返回球面的顶点
  25.   unsigned short **ppIndices, // 返回球面的三角形索引
  26.   float *color, // 球的颜色
  27.   int stacks, // 纬度的切面数目
  28.   int slices // 径度的切面数目
  29.   )
  30. {
  31. *ppVertices = NULL;
  32. int num_vertices = (stacks+1)*(slices+1);
  33. int num_triangles = stacks*slices*2;
  34. Vertex_VC *pVertices = new Vertex_VC[num_vertices];
  35. if ( pVertices==NULL )
  36. return false;
  37. *ppVertices = pVertices;
  38. g_iNumSphereVertices = num_vertices;
  39. g_iNumSphereTriangles = num_triangles;
  40. g_iNumSphereIndices = num_triangles * 3;
  41. float default_color[] = {1.0f, 1.0f, 1.0f, 1.0f};
  42. if ( color==NULL )
  43. color = default_color;
  44. const float theta_start_degree = 0.0f;
  45. const float theta_end_degree = 360.0f;
  46. const float phi_start_degree = -90.0f;
  47. const float phi_end_degree = 90.0f;
  48. float ts = FastMath::DegreeToRadian(theta_start_degree);
  49. float te = FastMath::DegreeToRadian(theta_end_degree);
  50. float ps = FastMath::DegreeToRadian(phi_start_degree);
  51. float pe = FastMath::DegreeToRadian(phi_end_degree);
  52. float theta_total = te - ts;
  53. float phi_total = pe - ps;
  54. float theta_inc = theta_total/stacks;
  55. float phi_inc = phi_total/slices;
  56. int i,j;
  57. int index = 0;
  58. float theta = ts;
  59. float sin_theta, cos_theta;
  60. float sin_phi, cos_phi;
  61. float r = color[0];
  62. float g = color[1];
  63. float b = color[2];
  64. float a = color[3];
  65. if ( GutGetGraphicsDeviceType()==GUT_DX9 )
  66. {
  67. r = color[2];
  68. b = color[0];
  69. }
  70. for ( i=0; i<=stacks; i++ )
  71. {
  72. float phi = ps;
  73. FastMath::SinCos(theta, sin_theta, cos_theta);
  74. for ( j=0; j<=slices; j++, index++ )
  75. {
  76. FastMath::SinCos(phi, sin_phi, cos_phi);
  77. // vertex
  78. pVertices[index].m_Position[0] = radius * cos_phi * cos_theta;
  79. pVertices[index].m_Position[1] = radius * sin_phi;
  80. pVertices[index].m_Position[2] = radius * cos_phi * sin_theta;
  81. // Color
  82. float shading = (float) j / (float) slices;
  83. //float shading = 1.0f;
  84. pVertices[index].m_RGBA[0] = 255 * r * shading;
  85. pVertices[index].m_RGBA[1] = 255 * g * shading;
  86. pVertices[index].m_RGBA[2] = 255 * b * shading;
  87. pVertices[index].m_RGBA[3] = 255 * a * shading;
  88. // inc phi
  89. phi += phi_inc;
  90. }
  91. // inc theta
  92. theta += theta_inc;
  93. }
  94. int base = 0;
  95. index = 0;
  96. if ( ppIndices )
  97. {
  98. *ppIndices = NULL;
  99. unsigned short *pIndices = new unsigned short[num_triangles*3];
  100. if ( pIndices==NULL )
  101. {
  102. delete [] pVertices;
  103. return false;
  104. }
  105. *ppIndices = pIndices;
  106. // triangle list
  107. for ( i=0; i<stacks; i++ )
  108. {
  109. for ( j=0; j<slices; j++ )
  110. {
  111. pIndices[index++] = base;
  112. pIndices[index++] = base+1;
  113. pIndices[index++] = base+slices+1;
  114. pIndices[index++] = base+1;
  115. pIndices[index++] = base+slices+2;
  116. pIndices[index++] = base+slices+1;
  117. base++;
  118. }
  119. base++;
  120. }
  121. }
  122. return true;
  123. }