render_data.cpp
Upload User: gzqinmao
Upload Date: 2022-07-13
Package Size: 472k
Code Size: 4k
Category:

OpenGL program

Development Platform:

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