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

OpenGL

开发平台:

Visual C++

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