00001
00007 #include "Tube.h"
00008
00009 Tube::Tube(int resolution, float radius, float height, float innerRadius) {
00010 m_pTubeVertices = NULL;
00011
00012 if (innerRadius == 0.0f) {
00013 if (!createFlatTube(resolution, radius, height)) {
00014 if (m_pTubeVertices) delete []m_pTubeVertices;
00015 m_pTubeVertices = NULL;
00016 LOG_ERROR(("Unable to load object tube"));
00017 return;
00018 }
00019 }
00020
00021
00022 LOG_SUCCESS(("Object tube loaded"));
00023 }
00024
00025 bool Tube::createFlatTube(int resolution, float radius, float height) {
00026 float stepsize = TWOPI/(float)resolution;
00027 int numVertices = resolution*2+2;
00028
00029 m_pTubeVertices = new Vertex[numVertices];
00030
00031 int index = 0;
00032 float tu, tv;
00033 float nx, ny, nz;
00034 float vx, vy, vz;
00035
00036 float halfHeight = height/2.0f;
00037
00038 tu = tv = nx = ny = nz = vx = vy = vz = 0.0f;
00039
00040 for (float i=0.0f; i<=TWOPI; i+=stepsize) {
00041 vx = sinf(i)*radius;
00042 vz = cosf(i)*radius;
00043 vy = -halfHeight;
00044
00045 tu = (1.0f/resolution)*(float)index;
00046 tv = 0.0f;
00047
00048 setVertData(index, tu, tv, nx, ny, nz, vx, vy, vz);
00049
00050 vy = halfHeight;
00051 tv = 1.0f;
00052 setVertData(index+1, tu, tv, nx, ny, nz, vx, vy, vz);
00053
00054 index += 2;
00055 }
00056
00062 addFormat(ATT_VERTEX, ATT_FLOAT, 3, sizeof(float)*5);
00063 addFormat(ATT_NORMAL, ATT_FLOAT, 3, sizeof(float)*2);
00064 addFormat(ATT_TEX, ATT_FLOAT, 2, 0);
00065 setPrimitive(PRIM_TRIANGLE_STRIP);
00066 setVertices(m_pTubeVertices, numVertices, sizeof(Vertex));
00067
00068 return true;
00069 }
00070
00071 void Tube::setVertData(int index, float tu, float tv,
00072 float nx, float ny, float nz,
00073 float vx, float vy, float vz)
00074 {
00075 (m_pTubeVertices+index)->tu = tu;
00076 (m_pTubeVertices+index)->tv = tv;
00077 (m_pTubeVertices+index)->nx = nx;
00078 (m_pTubeVertices+index)->ny = ny;
00079 (m_pTubeVertices+index)->nz = nz;
00080 (m_pTubeVertices+index)->vx = vx;
00081 (m_pTubeVertices+index)->vy = vy;
00082 (m_pTubeVertices+index)->vz = vz;
00083 }
00084
00085 Tube::~Tube() {
00086 m_pTubeVertices = NULL;
00087 }