Example #1
0
//-------------------------------------------------------------------------------------------------
// loads MD2 model
bool CCMD2Model::Load(char *filename, char *textureName)
{

	//FILE *filePtr;						// file pointer
	unsigned long fileLen = 0;			// length of model file
    //char *buffer;						// file buffer
		
	modelHeader_t *modelHeader;			// model header

	stIndex_t *stPtr;					// texture data
    frame_t *frame;						// frame data
	kmVec3 *pointListPtr;				// index variable
    mesh_t *triIndex, *bufIndexPtr;		// index variables
    int i, j;							// index variables

	// open the model file

	std::string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(filename);

	//unsigned long nSize = 0;
	unsigned char* buffer = CCFileUtils::sharedFileUtils()->getFileData(path.c_str(), "rb", &fileLen);
    
	if (buffer == NULL)
		return false;

	//const char *path = filename;//GetPath(filename);
	/*filePtr = fopen(path, "rb");
	if (filePtr == NULL)
		return false;*/

	// find length of file
    //fseek(filePtr, 0, SEEK_END);
    //fileLen = ftell(filePtr);
    //fseek(filePtr, 0, SEEK_SET);

    //// read entire file into buffer
    //buffer = (char*)malloc(fileLen + 1);
    //fread(buffer, sizeof(char), fileLen, filePtr);

	// extract model file header from buffer
    modelHeader = (modelHeader_t*)buffer;

	// allocate memory for model data
   	mModel = (modelData_t*)malloc(sizeof(modelData_t));
	if (mModel == NULL)
    {
        CC_SAFE_DELETE_ARRAY(buffer);
		return false;
    }

	// allocate memory for all vertices used in model, including animations
    mModel->pointList = (kmVec3 *)malloc(sizeof(kmVec3)*modelHeader->numXYZ * modelHeader->numFrames);

	// store vital model data
    mModel->numPoints = modelHeader->numXYZ;
    mModel->numFrames = modelHeader->numFrames;
	mModel->frameSize = modelHeader->framesize;

    // loop number of frames in model file
    for(j = 0; j < modelHeader->numFrames; j++)
    {
       // offset to the points in this frame
       frame = (frame_t*)&buffer[modelHeader->offsetFrames + modelHeader->framesize * j];

	   // calculate the point positions based on frame details
       pointListPtr = (kmVec3 *)&mModel->pointList[modelHeader->numXYZ * j];
       for(i = 0; i < modelHeader->numXYZ; i++)
       {
          pointListPtr[i].x = frame->scale[0] * frame->fp[i].v[0] + frame->translate[0];
          pointListPtr[i].y = frame->scale[1] * frame->fp[i].v[1] + frame->translate[1];
          pointListPtr[i].z = frame->scale[2] * frame->fp[i].v[2] + frame->translate[2];
       }
    }
			 
	CCTexture2D *tex = CCTextureCache::sharedTextureCache()->addImage(textureName);
	if (tex)
		mModel->modelTex = tex;
	else
	{
		free(mModel);
		mModel = NULL;
		
		CC_SAFE_DELETE_ARRAY(buffer);
		//free(buffer);
		return false;
	}


	CCSize size = tex->getContentSizeInPixels();
	float texWidth = size.width;
	float texHeight = size.height;


    // allocate memory for the model texture coordinates
    mModel->st = (texCoord_t*)malloc(sizeof(texCoord_t)*modelHeader->numST);

	// store number of texture coordinates
    mModel->numST = modelHeader->numST;

	// set texture pointer to texture coordinate offset
    stPtr = (stIndex_t*)&buffer[modelHeader->offsetST];

	// calculate and store the texture coordinates for the model
    for (i = 0; i < modelHeader->numST; i++)
    {
		mModel->st[i].s = (float)stPtr[i].s / texWidth;
        mModel->st[i].t = (float)stPtr[i].t / texHeight;
    }

	// allocate an index of triangles
	triIndex = (mesh_t*)malloc(sizeof(mesh_t) * modelHeader->numTris);

	// set total number of triangles
	mModel->numTriangles = modelHeader->numTris;
	mModel->triIndex = triIndex;
	
	// point to triangle indexes in buffer
	bufIndexPtr = (mesh_t*)&buffer[modelHeader->offsetTris];

	// create a mesh (triangle) list
	for (j = 0; j < mModel->numFrames; j++)		
	{
		// for all triangles in each frame
		for(i = 0; i < modelHeader->numTris; i++)
		{
		   triIndex[i].meshIndex[0] = bufIndexPtr[i].meshIndex[0];
		   triIndex[i].meshIndex[1] = bufIndexPtr[i].meshIndex[1];
		   triIndex[i].meshIndex[2] = bufIndexPtr[i].meshIndex[2];
		   triIndex[i].stIndex[0] = bufIndexPtr[i].stIndex[0];
		   triIndex[i].stIndex[1] = bufIndexPtr[i].stIndex[1];
		   triIndex[i].stIndex[2] = bufIndexPtr[i].stIndex[2];
		}
	}

	// close file and free memory
	//fclose(filePtr);
    //free(buffer);
	CC_SAFE_DELETE_ARRAY(buffer);

	mModel->currentFrame = 0;
	mModel->nextFrame = 1;
	mModel->interpol = 0.0;

	uv = new float[mModel->numTriangles * 6];
	vertices = new float[mModel->numTriangles * 9];

	CheckNextState();

    return true;
}
Example #2
0
	bool CScutAniGroup::Load(const char* sprName)
	{
		if (NULL == sprName)
		{
			return false;
		}
		
		string spr = sprName;
		
		if (spr.size() == 0)
		{
			return false;
		}
		
		if (this->m_sprName.size() > 0)
		{
			return this->m_sprName == spr;
		}
		
		this->m_sprName = sprName;

		spr += ".spr";
		spr = SPRITE_FILE_DIR + spr;
		spr = CFileHelper::getPath(spr.c_str());
		
		char cMode[3] = {'r', 'b', 0};
		unsigned long uSize = 0;
		unsigned char* pBuf = (unsigned char*)CFileHelper::getFileData(spr.c_str(), cMode, &uSize);
		CSpriteFileStream fileSpr;
		if (fileSpr.Write((const char*)pBuf, uSize) == 0)
		{
			delete[] pBuf;
			return false;
		}

		delete[] pBuf;
		fileSpr.SetPosition(0);
		int tableSize = fileSpr.ReadByte();
		this->m_vTileTable.assign(tableSize, ScutTileTable());

		for (int i = 0; i < tableSize; i++)
		{
			ScutTileTable& tt = this->m_vTileTable[i];
			tt.imgIndex = fileSpr.ReadByte();
			tt.clip.origin.x = fileSpr.ReadShort();
			tt.clip.origin.y = fileSpr.ReadShort();
			tt.clip.size.width = fileSpr.ReadShort();
			tt.clip.size.height = fileSpr.ReadShort();
		}

		double pixcelFactor = 1.0;
		
		int imgSize = fileSpr.ReadByte();
		CCTexture2D* pTex = NULL;
		this->m_vTexture.assign(imgSize, ReplaceTexture());
		string texFile;
		for (int j = 0; j < imgSize; j++)
		{
			ReplaceTexture& texture = this->m_vTexture[j];
			fileSpr.ReadUTF(texFile);
			texFile = SPRITE_IMAGE_DIR + texFile;
			texFile = CFileHelper::getPath(texFile.c_str());
			pTex = CCTextureCache::sharedTextureCache()->addImage(texFile.c_str());
			assert(pTex != NULL);
			CC_SAFE_RETAIN(pTex);
			texture.pTex = pTex;
			texture.replaceIndex = fileSpr.ReadByte();
			texture.dFactor = (double)pTex->getContentSizeInPixels().width / (double)fileSpr.ReadShort();

			// 取第一张图片的缩放比例作为动画整体的估计值
			if (0 == j)
			{
				pixcelFactor = texture.dFactor;
			}
			
		}

		this->caculateTileTableTextureCoord();
		
		CScutAniData* pAniData = NULL;
		CScutFrame* pFrame = NULL;
		CScutTile* pTile = NULL;
		
		int aniSize = fileSpr.ReadByte();
		this->m_vAniData.assign(aniSize, pAniData);
		
		for (int m = 0; m < aniSize; m++)
		{
			pAniData = new CScutAniData;
			this->m_vAniData[m] = pAniData;

			pAniData->m_tContentSize.width = fileSpr.ReadShort() * pixcelFactor;
			pAniData->m_tContentSize.height = fileSpr.ReadShort() * pixcelFactor;
			pAniData->m_anchorX = fileSpr.ReadShort();
			pAniData->m_anchorY = fileSpr.ReadShort();
			pAniData->m_type = fileSpr.ReadByte();
			
			int frameSize = fileSpr.ReadByte();
			pAniData->m_vFrame.assign(frameSize, pFrame);
			
			for (int n = 0; n < frameSize; n++)
			{
				pFrame = new CScutFrame;
				pAniData->m_vFrame[n] = pFrame;
				pFrame->m_duration = fileSpr.ReadShort();
				
				int tileSize = fileSpr.ReadByte();
				pFrame->m_vTiles.assign(tileSize, pTile);
				
				for (int tl = 0; tl < tileSize; tl++)
				{
					float fx = fileSpr.ReadShort();
					float fy = fileSpr.ReadShort();
					int flipXY = fileSpr.ReadByte();
					float rotation = fileSpr.ReadShort();
					float scale = fileSpr.ReadFloat();
					int tableIndex = fileSpr.ReadShort();
					
					assert(tableIndex > -1 && tableIndex < this->m_vTileTable.size());
					if (tableIndex > -1 && tableIndex < this->m_vTileTable.size())
					{
						pTile = new CScutTile(this->m_vTileTable[tableIndex]);
						assert(pTile->m_tt.imgIndex > -1 && pTile->m_tt.imgIndex < m_vTexture.size());

						// 根据每个tile所属的图片精确计算缩放因子
						double factor = this->m_vTexture[pTile->m_tt.imgIndex].dFactor;

						pFrame->m_vTiles[tl] = pTile;
						pFrame->setScutAniGroup(this);
						pTile->m_frameX = fx * factor;
						pTile->m_frameY = fy * factor;
						pTile->m_flipX = (flipXY & CScutTile::FLIP_X) > 0;
						pTile->m_flipY = (flipXY & CScutTile::FLIP_Y) > 0;
						pTile->m_rotation = rotation;
						pTile->m_scale = scale;
						pTile->caculateTileVertices(pAniData->m_anchorX * factor, pAniData->m_anchorY * factor);
					}
				}
			}
		}

		return true;
	}