- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
render_data.cpp
资源名称:chap03.rar [点击查看]
上传用户:gzqinmao
上传日期:2022-07-13
资源大小:472k
文件大小:3k
源码类别:
OpenGL
开发平台:
Visual C++
- #include "Vector4.h"
- #include "render_data.h"
- #include "gut.h"
- // 镜头位置
- Vector4 g_eye(0.0f, 0.0f, 15.0f);
- // 镜头对准的点
- Vector4 g_lookat(0.0f, 0.0f, 0.0f);
- // 镜头正上方的方向
- Vector4 g_up(0.0f, 1.0f, 0.0f);
- // 镜头转换矩阵
- Matrix4x4 g_view_matrix;
- //
- Matrix4x4 g_sun_matrix, g_earth_matrix, g_moon_matrix;
- // 球的模型
- Vertex_VC *g_pSunVertices = NULL;
- Vertex_VC *g_pEarthVertices = NULL;
- Vertex_VC *g_pMoonVertices = NULL;
- unsigned short *g_pSphereIndices = NULL;
- int g_iNumSphereVertices = 0;
- int g_iNumSphereTriangles = 0;
- int g_iNumSphereIndices = 0;
- //
- bool CreateSphere(float radius, // 半径
- Vertex_VC **ppVertices, // 返回球面的顶点
- unsigned short **ppIndices, // 返回球面的三角形索引
- float *color, // 球的颜色
- int stacks, // 纬度的切面数目
- int slices // 径度的切面数目
- )
- {
- *ppVertices = NULL;
- int num_vertices = (stacks+1)*(slices+1);
- int num_triangles = stacks*slices*2;
- Vertex_VC *pVertices = new Vertex_VC[num_vertices];
- if ( pVertices==NULL )
- return false;
- *ppVertices = pVertices;
- g_iNumSphereVertices = num_vertices;
- g_iNumSphereTriangles = num_triangles;
- g_iNumSphereIndices = num_triangles * 3;
- float default_color[] = {1.0f, 1.0f, 1.0f, 1.0f};
- if ( color==NULL )
- color = default_color;
- const float theta_start_degree = 0.0f;
- const float theta_end_degree = 360.0f;
- const float phi_start_degree = -90.0f;
- const float phi_end_degree = 90.0f;
- float ts = FastMath::DegreeToRadian(theta_start_degree);
- float te = FastMath::DegreeToRadian(theta_end_degree);
- float ps = FastMath::DegreeToRadian(phi_start_degree);
- float pe = FastMath::DegreeToRadian(phi_end_degree);
- float theta_total = te - ts;
- float phi_total = pe - ps;
- float theta_inc = theta_total/stacks;
- float phi_inc = phi_total/slices;
- int i,j;
- int index = 0;
- float theta = ts;
- float sin_theta, cos_theta;
- float sin_phi, cos_phi;
- float r = color[0];
- float g = color[1];
- float b = color[2];
- float a = color[3];
- if ( GutGetGraphicsDeviceType()==GUT_DX9 )
- {
- r = color[2];
- b = color[0];
- }
- for ( i=0; i<=stacks; i++ )
- {
- float phi = ps;
- FastMath::SinCos(theta, sin_theta, cos_theta);
- for ( j=0; j<=slices; j++, index++ )
- {
- FastMath::SinCos(phi, sin_phi, cos_phi);
- // vertex
- pVertices[index].m_Position[0] = radius * cos_phi * cos_theta;
- pVertices[index].m_Position[1] = radius * sin_phi;
- pVertices[index].m_Position[2] = radius * cos_phi * sin_theta;
- // Color
- float shading = (float) j / (float) slices;
- //float shading = 1.0f;
- pVertices[index].m_RGBA[0] = 255 * r * shading;
- pVertices[index].m_RGBA[1] = 255 * g * shading;
- pVertices[index].m_RGBA[2] = 255 * b * shading;
- pVertices[index].m_RGBA[3] = 255 * a * shading;
- // inc phi
- phi += phi_inc;
- }
- // inc theta
- theta += theta_inc;
- }
- int base = 0;
- index = 0;
- if ( ppIndices )
- {
- *ppIndices = NULL;
- unsigned short *pIndices = new unsigned short[num_triangles*3];
- if ( pIndices==NULL )
- {
- delete [] pVertices;
- return false;
- }
- *ppIndices = pIndices;
- // triangle list
- for ( i=0; i<stacks; i++ )
- {
- for ( j=0; j<slices; j++ )
- {
- pIndices[index++] = base;
- pIndices[index++] = base+1;
- pIndices[index++] = base+slices+1;
- pIndices[index++] = base+1;
- pIndices[index++] = base+slices+2;
- pIndices[index++] = base+slices+1;
- base++;
- }
- base++;
- }
- }
- return true;
- }