00001 #include "Skin.h"
00002 #include "Triangle.h"
00003 #include "math/vector3.h"
00004 #include "Token.h"
00005
00006
00007 Skin::~Skin()
00008 {
00009 if(verts)
00010 delete verts;
00011 if(norms)
00012 delete norms;
00013
00014
00015
00016
00017
00018
00019
00020 }
00021
00022 void Skin::AllocateVertexArrays()
00023 {
00024 verts = new Vector3[numVerts];
00025 norms = new Vector3[numVerts];
00026 }
00027
00028 void Skin::Load(Tokenizer &tokens)
00029 {
00030 char temp[64];
00031 float skinScale = 1.0f, xScale = 1.0f, xoffs = 0.0f, yoffs = 0.0f;
00032 verts = NULL;
00033 norms = NULL;
00034 tokens.FindToken("mesh");
00035 tokens.GetToken(temp);
00036 if(strcmp(temp,"scale") == 0)
00037 {
00038 skinScale = tokens.GetFloat();
00039 tokens.GetToken(temp);
00040 }
00041 if(strcmp(temp,"xscale") == 0)
00042 {
00043 xScale = tokens.GetFloat();
00044 tokens.GetToken(temp);
00045 }
00046 if(strcmp(temp,"xoffs") == 0)
00047 {
00048 xoffs = tokens.GetFloat();
00049 tokens.GetToken(temp);
00050 }
00051 if(strcmp(temp,"yoffs") == 0)
00052 {
00053 yoffs = tokens.GetFloat();
00054 tokens.GetToken(temp);
00055 }
00056 if(strcmp(temp,"verts") != 0)
00057 tokens.FindToken("verts");
00058 numVerts = tokens.GetInt();
00059 skinVerts = new SkinVert[numVerts];
00060 texCoords = new TexCoord[numVerts];
00061 for(int i = 0; i < numVerts; i++)
00062 {
00063 float x = (tokens.GetFloat() + xoffs) * xScale;
00064 float y = tokens.GetFloat() + yoffs;
00065 float z = tokens.GetFloat();
00066 skinVerts[i].pos.Set(skinScale*x,skinScale*y,skinScale*z);
00067 skinVerts[i].numWeights = tokens.GetInt();
00068 if(skinVerts[i].numWeights > 0)
00069 skinVerts[i].weights = new SkinVertWeight[skinVerts[i].numWeights];
00070 for(int j=0; j < skinVerts[i].numWeights; j++)
00071 {
00072 skinVerts[i].weights[j].joint = tokens.GetInt();
00073 skinVerts[i].weights[j].weight = tokens.GetFloat();
00074 }
00075
00076 texCoords[i].x = tokens.GetFloat();
00077 texCoords[i].y = tokens.GetFloat();
00078
00079 skinVerts[i].texCoord = texCoords[i];
00080 x = tokens.GetFloat();
00081 y = tokens.GetFloat();
00082 z = tokens.GetFloat();
00083 skinVerts[i].normal.Set(x,y,z);
00084 }
00085 tokens.FindToken("faces");
00086 numFaces = tokens.GetInt();
00087 vertIndices = new FaceIndices[numFaces];
00088 normIndices = new FaceIndices[numFaces];
00089 for(int i = 0; i < numFaces; i++)
00090 {
00091 int a = tokens.GetInt();
00092 int b = tokens.GetInt();
00093 int c = tokens.GetInt();
00094 vertIndices[i].Set(a,b,c);
00095 a = tokens.GetInt();
00096 b = tokens.GetInt();
00097 c = tokens.GetInt();
00098 normIndices[i].Set(a,b,c);
00099 }
00100 }
00101 void Skin::Draw()
00102 {
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 glColor3f(.9,.9,.9);
00114 glEnableClientState(GL_VERTEX_ARRAY);
00115 glEnableClientState(GL_NORMAL_ARRAY);
00116 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00117 glVertexPointer(3,GL_FLOAT,0,verts);
00118 glNormalPointer(GL_FLOAT,0,norms);
00119 glTexCoordPointer(2,GL_FLOAT,0,texCoords);
00120 glDrawElements(GL_TRIANGLES,numFaces*3,GL_UNSIGNED_INT,normIndices);
00121 glDisableClientState(GL_VERTEX_ARRAY);
00122 glDisableClientState(GL_NORMAL_ARRAY);
00123 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 }