void CUIDraw::GetTextureSize(int iTextureID,float &rfSizeX,float &rfSizeY)
{
	TTexturesMap::iterator Iter = m_texturesMap.find(iTextureID);
	if(Iter != m_texturesMap.end())
	{
		ITexture *pTexture = (*Iter).second;
		rfSizeX = (float) pTexture->GetWidth	();
		rfSizeY = (float) pTexture->GetHeight	();
	}
	else
	{
		// Unknow texture !
		CRY_ASSERT(0);
		rfSizeX = 0.0f;
		rfSizeY = 0.0f;
	}
}
void VFSPlugin_Q2BSP::AssignTextureCoords(IVertexBuffer *vb)
{
	int					a,b;
	Mesh				*m	=	m_fileinfo->mesh;
	Vertex3f		*v	=	m->GetPosition();
	Vertex2f		*tc	=	NULL;
	BSPTexInfo	*ti	=	NULL;
	int					*i	= vb->GetIndex();

	if((tc = (Vertex2f *)m->GetTexcoord()) == NULL){
		tc = new Vertex2f[m->GetNumVertex()];
		memset(tc,0,m->GetNumVertex()*sizeof(Vertex2f));
		m->SetTexcoord(tc);
	}

	for(a=0;a<m_numtexinfo;a++){
		ti = &m_texinfo[a];

		if(strcmp(vb->GetName(),ti->name) == 0){
			Maths::Vector tu_vec(ti->u_axis);
			Maths::Vector tv_vec(ti->v_axis);

			ITexture *texture = vb->GetTexture();

			for(b=0;b<vb->GetNumIndex();b++){
				Maths::Vector vec(v[i[b]]);
				
				tc[i[b]].x	=	( vec.DotProduct(tu_vec) + ti->u_offset) / texture->GetWidth();
				tc[i[b]].y	=	( vec.DotProduct(tv_vec) + ti->v_offset) / texture->GetHeight();
			}
		}
	}

	//for(a=0;a<m->GetNumVertex();a++)	logfile << "texcoord["<<a<<"] = { " << tc[a].x << ", " << tc[b].y << " };" << std::endl;

//	vb->SetTexcoord((float *)tc);
	vb->SetTextureLayer(0,(float *)tc,vb->GetTexture());
}
Beispiel #3
0
//------------------------------------------------------------------------------
void sqr::CLoadMapResInfo::MakeTerrainTexture()
{
	CRenderDevice * pRender = NULL;
	pRender = CRenderDevice::GetInst();
	if ( pRender == NULL )
		return;

	CTerrainMesh * pTerrain = CMapEditApp::GetInst()->GetTerrain();

	int texCnt = 0;
	ITexture* pTexture = NULL;

	if ( pTerrain->m_pTerrainTexture == NULL )
		CGraphic::GetInst()->CreateEmptyTexture(EXPAND_MAX_SIZE,EXPAND_MAX_SIZE,1, TF_XRGB32,&(pTerrain->m_pTerrainTexture));

	string texname = "";
	map<string, int16>::iterator iter = m_mapTerrainTextureNames.begin();
	map<string, int16>::iterator eiter = m_mapTerrainTextureNames.end();
	set<string> erroTexList;
	for ( iter; iter != eiter; ++iter )
	{
		texname = iter->first;
		if( texname.empty() )
			continue;

		pTexture = pRender->GetTexture(texname);
		if( texname.find("water.dds") != -1 )
			continue;

		if (NULL==pTexture)
		{
			string log = "拼接过程:"+texname+"纹理无法找到";
			MessageBox(NULL,log.c_str(),"错误",MB_OK);
			erroTexList.insert(texname);
			continue;
		}

		if (NULL==pTexture->GetBuffer())
		{
			string log = "拼接过程:"+texname+"纹理无法找到";
			MessageBox(NULL,log.c_str(),"错误",MB_OK);
			continue;
		}

		UINT H = (texCnt/ROW_SIZE)*EXPAND_TEXTURE_SIZE;
		UINT W = (texCnt%ROW_SIZE)*EXPAND_TEXTURE_SIZE;


		if (pTexture->GetWidth()!=TEXTURE_SIZE||pTexture->GetHeight()!=TEXTURE_SIZE)
		{
			string log = "拼接过程:"+texname+"尺寸不合法,所有贴图纹理应为128X128";
			erroTexList.insert(texname);
			MessageBox(NULL,log.c_str(),"错误",MB_OK);
			continue;
		}

		pRender->m_TerrainTexIndexs[texname] = texCnt;
		//DebugOut("%d, %s\n", texCnt, texname.c_str());
		++texCnt;

		CIRect rectT,rectO;
		_TEXLOCKED_RECT lockT,lockO;
		rectO.top = rectO.left = 0;
		rectO.bottom = rectO.right = TEXTURE_SIZE;
		rectT.top = H;
		rectT.bottom = H + EXPAND_TEXTURE_SIZE;
		rectT.left = W;
		rectT.right = W + EXPAND_TEXTURE_SIZE;
		pTerrain->m_pTerrainTexture->Lock(0,&lockT,&rectT,0,LOCK_DISCARD);
		pTexture->Lock(0,&lockO,&rectO,0,LOCK_READONLY);
		//pTerrain->pTar = (TCHAR*)lockT.pBits;
		TCHAR* pTar = (TCHAR*)lockT.pBits;
		TCHAR* pOrc = (TCHAR*)lockO.pBits;

		for (UINT i=0;i<EXPAND_SIZE;++i,pTar+=lockT.Pitch)
		{
			for ( UINT j=0;j<EXPAND_SIZE;++j)
			{
				memcpy(pTar+PixelSize*j,pOrc,PixelSize);
				memcpy(pTar+lockO.Pitch+PixelSize*(j+EXPAND_SIZE),pOrc+lockO.Pitch-PixelSize,PixelSize);
			}
			memcpy(pTar+PixelSize*EXPAND_SIZE,pOrc,lockO.Pitch);
		}

		for (UINT i = 0;i<TEXTURE_SIZE;++i,pTar+=lockT.Pitch,pOrc+=lockO.Pitch)
		{

			for ( UINT j=0;j<EXPAND_SIZE;++j)
			{
				memcpy(pTar+PixelSize*j,pOrc,PixelSize);
				memcpy(pTar+lockO.Pitch+PixelSize*(j+EXPAND_SIZE),pOrc+lockO.Pitch-PixelSize,PixelSize);
			}
			memcpy(pTar+PixelSize*EXPAND_SIZE,pOrc,lockO.Pitch);
		}
		pOrc-=lockO.Pitch;
		for (UINT i=0;i<EXPAND_SIZE;++i,pTar+=lockT.Pitch)
		{
			for ( UINT j=0;j<EXPAND_SIZE;++j)
			{
				memcpy(pTar+PixelSize*j,pOrc,PixelSize);
				memcpy(pTar+lockO.Pitch+PixelSize*(j+EXPAND_SIZE),pOrc+lockO.Pitch-PixelSize,PixelSize);
			}
			memcpy(pTar+PixelSize*EXPAND_SIZE,pOrc,lockO.Pitch);
		}

		pTexture->Unlock(0);
		pTerrain->m_pTerrainTexture->Unlock(0);
	}

	///删掉载入不成功的纹理列表
	set<string>::iterator erroriter = erroTexList.begin();
	set<string>::iterator eeiter = erroTexList.end();
	map<string, DWORD>::iterator fiter;
	map<string, DWORD>::iterator feiter = pRender->m_TerrainTexIndexs.end();
	for (erroriter; erroriter != eeiter; ++erroriter)
	{
		texname = *erroriter;

		fiter = pRender->m_TerrainTexIndexs.find(texname);

		if ( fiter != feiter )
			pRender->m_TerrainTexIndexs.erase(fiter);
	}

	int nGridCnt = pTerrain->GetGridCount();
	string gridtexname = "";
	for ( int i = 0; i < nGridCnt; ++i )
	{
		SGrid &Grid = pTerrain->GetGrid(i);

		gridtexname = Grid.nodeInfo.strTextureNames[0];
		erroriter = erroTexList.find(gridtexname);
		if (erroriter != eeiter)
			Grid.nodeInfo.strTextureNames[0] = "";

		gridtexname = Grid.nodeInfo.strTextureNames[1];
		erroriter = erroTexList.find(gridtexname);
		if (erroriter != eeiter)
			Grid.nodeInfo.strTextureNames[1] = "";

		gridtexname = Grid.nodeInfo.strTextureNames[2];
		erroriter = erroTexList.find(gridtexname);
		if (erroriter != eeiter)
			Grid.nodeInfo.strTextureNames[2] = "";
	}
	texCnt = 0;
	map<string, DWORD>::iterator titer = pRender->m_TerrainTexIndexs.begin();
	map<string, DWORD>::iterator teiter = pRender->m_TerrainTexIndexs.end();

	for ( titer; titer != teiter; ++titer )
	{
		texname = titer->first;
		pRender->m_TerrainTexIndexs[texname] = texCnt;

		++texCnt;
	}

	IoSystem* ioSystem = IoSystem::GetInst();
	string homePath = IoSystem::GetInst()->GetFullPath("home:");
	string selfTexPath = homePath + pTerrain->m_strMapName + ".jpg";

	pTerrain->m_pTerrainTexture->SaveToFile(selfTexPath.c_str(),IFF_JPG);
	//D3DXSaveTextureToFile(selfTexPath.c_str(),D3DXIFF_JPG,pTerrain->m_pTerrainTexture,&pale);
}