Exemplo n.º 1
0
// ***************************************************************************
void CScene::renderOcclusionTestMeshs()
{
	nlassert(RenderTrav.getDriver());
	RenderTrav.getDriver()->setupViewport(RenderTrav.getViewport());
	RenderTrav.getDriver()->activeVertexProgram(NULL);
	RenderTrav.getDriver()->activePixelProgram(NULL);
	RenderTrav.getDriver()->activeGeometryProgram(NULL);
	IDriver::TPolygonMode oldPolygonMode = RenderTrav.getDriver()->getPolygonMode();
	CMaterial m;
	m.initUnlit();
	m.setColor(CRGBA(255, 255, 255, 127));
	m.setBlend(true);
	m.setDstBlend(CMaterial::invsrcalpha);
	m.setSrcBlend(CMaterial::srcalpha);
	m.setZWrite(false);
	RenderTrav.getDriver()->setupMaterial(m);
	getDriver()->setPolygonMode(IDriver::Filled);
	renderOcclusionTestMeshsWithCurrMaterial();
	m.setColor(CRGBA::Black);
	RenderTrav.getDriver()->setupMaterial(m);
	getDriver()->setPolygonMode(IDriver::Line);
	renderOcclusionTestMeshsWithCurrMaterial();
	getDriver()->setPolygonMode(oldPolygonMode);
}
Exemplo n.º 2
0
// ---------------------------------------------------------------------------
void CBuilderZone::displayGrid (const NLMISC::CVector &viewMin, const NLMISC::CVector &viewMax)
{
	// Select all blocks visible
	float rMinX = floorf (viewMin.x / _Display->_CellSize)*_Display->_CellSize;
	float rMinY = floorf (viewMin.y / _Display->_CellSize)*_Display->_CellSize;
	float rMaxX = ceilf  (viewMax.x / _Display->_CellSize)*_Display->_CellSize;
	float rMaxY = ceilf  (viewMax.y / _Display->_CellSize)*_Display->_CellSize;

	sint32 nMinX = (sint32)floor (rMinX / _Display->_CellSize);
	sint32 nMinY = (sint32)floor (rMinY / _Display->_CellSize);
	sint32 nMaxX = (sint32)floor (rMaxX / _Display->_CellSize);
	sint32 nMaxY = (sint32)floor (rMaxY / _Display->_CellSize);

	static vector<uint8> vBars;
	sint32 nBarsW = (nMaxX-nMinX)+1;
	sint32 nBarsH = (nMaxY-nMinY)+1;
	vBars.resize (nBarsW*nBarsH);
	sint32 x, y, i, j, zoneSelected;
	for (i = 0; i < nBarsW*nBarsH; ++i)
		vBars[i] = 0;


	for (y = nMinY; y <= nMaxY; ++y)
	for (x = nMinX; x <= nMaxX; ++x)
	{

		string sZone = STRING_OUT_OF_BOUND;
		zoneSelected = 0;
		for (i = 0; i < (sint32)_ZoneRegions.size(); ++i)
		{
			const string &rSZone = getDocument ()->getZoneRegion (i).getName (x, y);
			if ((sZone == STRING_OUT_OF_BOUND) && (rSZone == STRING_UNUSED))
			{
				sZone = STRING_UNUSED;
				zoneSelected = i;
			}
			if ((rSZone != STRING_OUT_OF_BOUND) && (rSZone != STRING_UNUSED))
			{
				sZone = rSZone;
				zoneSelected = i;
			}
		}


		//const string &sZone = _ZoneRegion.getName (x, y);
		CZoneBankElement *pZBE = _ZoneBank.getElementByZoneName (sZone);
		if (pZBE != NULL)
		if ((pZBE->getSizeX() > 1) || (pZBE->getSizeY() > 1))
		{
			const CZoneRegion *pSelected = &(getDocument ()->getZoneRegion (zoneSelected));
			sint32 sizeX = pZBE->getSizeX(), sizeY = pZBE->getSizeY();
			sint32 posX = pSelected->getPosX (x, y), posY = pSelected->getPosY (x, y);
			uint8 rot = pSelected->getRot (x, y);
			uint8 flip = pSelected->getFlip (x, y);
			sint32 deltaX, deltaY;
		
			if (flip == 0)
			{
				switch (rot)
				{
					case 0: deltaX = -posX; deltaY = -posY; break;
					case 1: deltaX = -(sizeY-1-posY); deltaY = -posX; break;
					case 2: deltaX = -(sizeX-1-posX); deltaY = -(sizeY-1-posY); break;
					case 3: deltaX = -posY; deltaY = -(sizeX-1-posX); break;
				}
			}
			else
			{
				switch (rot)
				{
					case 0: deltaX = -(sizeX-1-posX); deltaY = -posY; break;
					case 1: deltaX = -(sizeY-1-posY); deltaY = -(sizeX-1-posX); break;
					case 2: deltaX = -posX; deltaY = -(sizeY-1-posY); break;
					case 3: deltaX = -posY; deltaY = -posX; break;
				}
			}

			static SPiece sMask;
			sMask.Tab.resize (sizeX*sizeY);
			for(i = 0; i < sizeX*sizeY; ++i)
				sMask.Tab[i] = pZBE->getMask()[i];
			sMask.w = sizeX;
			sMask.h = sizeY;
			sMask.rotFlip (rot, flip);

			for (j = 0; j < sMask.h; ++j)
			for (i = 0; i < sMask.w; ++i)
			if (sMask.Tab[i+j*sMask.w])
			{
				if (((x+deltaX+i-nMinX)>=0) && ((x+deltaX+i-nMinX)<nBarsW) &&
					((y+deltaY+j-nMinY)>=0) && ((y+deltaY+j-nMinY)<nBarsH))
				{
					if ((i > 0) && (sMask.Tab[i-1+j*sMask.w]))
						vBars[x+deltaX+i-nMinX + (y+deltaY+j-nMinY)*nBarsW] |= 1;

					if ((j > 0) && (sMask.Tab[i+(j-1)*sMask.w]))
						vBars[x+deltaX+i-nMinX + (y+deltaY+j-nMinY)*nBarsW] |= 2;
				}
			}
		}
	}

	CVertexBuffer VB;
	CIndexBuffer PB;
	CMaterial Mat;

	Mat.initUnlit ();
	Mat.setBlend (false);
	VB.setVertexFormat (CVertexBuffer::PositionFlag);
	VB.setNumVertices ((nBarsW+1)*(nBarsH+1));
	CVertexBufferReadWrite vba;
	VB.lock (vba);
	
	for (y = nMinY; y <= nMaxY+1; ++y)
	for (x = nMinX; x <= nMaxX+1; ++x)
	{
		CVector pos;

		pos.x = (x*_Display->_CellSize - viewMin.x)/(viewMax.x-viewMin.x);
		pos.y = 0.0f;
		pos.z = (y*_Display->_CellSize - viewMin.y)/(viewMax.y-viewMin.y);
		vba.setVertexCoord (x-nMinX+(y-nMinY)*(nBarsW+1), pos);
	}

	PB.setNumIndexes (nBarsW*nBarsH*2*2);
	CIndexBufferReadWrite iba;
	PB.lock (iba);
	uint32 nNbLine = 0;
	for (y = 0; y < nBarsH; ++y)
	for (x = 0; x < nBarsW; ++x)
	{
		// Vertical Line ?
		if ((vBars[x+y*nBarsW] & 1) == 0)
		{
			iba.setLine (nNbLine*2, x+y*(nBarsW+1), x+(y+1)*(nBarsW+1));
			++nNbLine;
		}

		// Horizontal Line ?
		if ((vBars[x+y*nBarsW] & 2) == 0)
		{
			iba.setLine (nNbLine*2, x+y*(nBarsW+1), (x+1)+y*(nBarsW+1));
			++nNbLine;
		}
	}
	iba.unlock();
	PB.setNumIndexes (nNbLine*2);

	if (DontUse3D)
		return;

	// Render with driver
	CMatrix mtx;
	mtx.identity();
	vba.unlock();
	CNELU::Driver->setupViewport (CViewport());
	CNELU::Driver->setupViewMatrix (mtx);
	CNELU::Driver->setupModelMatrix (mtx);
	CNELU::Driver->setFrustum (0.f, 1.f, 0.f, 1.f, -1.f, 1.f, false);
	CNELU::Driver->activeVertexBuffer (VB);
	CNELU::Driver->activeIndexBuffer (PB);
	CNELU::Driver->renderLines (Mat, 0, PB.getNumIndexes()/2);
	
}
Exemplo n.º 3
0
// TMP TMP
void tempDumpColPolys()
{
	CPackedWorld *pw = R2::getEditor().getIslandCollision().getPackedIsland();
	if (pw)
	{
		static CMaterial material;
		static CMaterial wiredMaterial;
		static CMaterial texturedMaterial;
		static CVertexBuffer vb;
		static bool initDone = false;
		if (!initDone)
		{
			vb.setVertexFormat(CVertexBuffer::PositionFlag);
			vb.setPreferredMemory(CVertexBuffer::AGPVolatile, false);
			material.initUnlit();
			material.setDoubleSided(true);
			material.setZFunc(CMaterial::lessequal);
			wiredMaterial.initUnlit();
			wiredMaterial.setDoubleSided(true);
			wiredMaterial.setZFunc(CMaterial::lessequal);
			wiredMaterial.setColor(CRGBA(255, 255, 255, 250));
			wiredMaterial.texEnvOpAlpha(0, CMaterial::Replace);
			wiredMaterial.texEnvArg0Alpha(0, CMaterial::Diffuse, CMaterial::SrcAlpha);
			wiredMaterial.setBlend(true);
			wiredMaterial.setBlendFunc(CMaterial::srcalpha, CMaterial::invsrcalpha);
			texturedMaterial.initUnlit();
			texturedMaterial.setDoubleSided(true);
			texturedMaterial.setZFunc(CMaterial::lessequal);
			initDone = true;
		}
		// just add a projected texture
		R2::getEditor().getIslandCollision().loadEntryPoints();
		R2::CScenarioEntryPoints &sep = R2::CScenarioEntryPoints::getInstance();
		CVectorD playerPos = UserEntity->pos();
		R2::CScenarioEntryPoints::CCompleteIsland *island = sep.getCompleteIslandFromCoords(CVector2f((float) playerPos.x, (float) playerPos.y));
		static CSString currIsland;
		if (island && island->Island != currIsland)
		{
			currIsland = island->Island;
			CTextureFile *newTex = new CTextureFile(currIsland + "_sp.tga");
			newTex->setWrapS(ITexture::Clamp);
			newTex->setWrapT(ITexture::Clamp);
			texturedMaterial.setTexture(0, newTex);
			texturedMaterial.texEnvOpRGB(0, CMaterial::Replace);
			texturedMaterial.texEnvArg0RGB(0, CMaterial::Texture, CMaterial::SrcColor);
			texturedMaterial.setTexCoordGen(0, true);
			texturedMaterial.setTexCoordGenMode(0, CMaterial::TexCoordGenObjectSpace);
			CMatrix mat;
			CVector scale((float) (island->XMax -  island->XMin),
						  (float) (island->YMax -  island->YMin), 0.f);
			scale.x = 1.f / favoid0(scale.x);
			scale.y = 1.f / favoid0(scale.y);
			scale.z = 0.f;
			mat.setScale(scale);
			mat.setPos(CVector(- island->XMin * scale.x, - island->YMin * scale.y, 0.f));
			//
			CMatrix uvScaleMat;
			//
			uint texWidth = (uint) (island->XMax -  island->XMin);
			uint texHeight = (uint) (island->YMax -  island->YMin);
			float UScale = (float) texWidth / 	raiseToNextPowerOf2(texWidth);
			float VScale = (float) texHeight / raiseToNextPowerOf2(texHeight);
			//
			uvScaleMat.setScale(CVector(UScale, - VScale, 0.f));
			uvScaleMat.setPos(CVector(0.f, VScale, 0.f));
			//
			texturedMaterial.enableUserTexMat(0, true);
			texturedMaterial.setUserTexMat(0, uvScaleMat * mat);
		}
		const CFrustum &frust = MainCam.getFrustum();

		//
		IDriver *driver = ((CDriverUser  *) Driver)->getDriver();

		driver->enableFog(true);
		const CRGBA clearColor = CRGBA(0, 0, 127, 0);
		driver->setupFog(frust.Far * 0.8f, frust.Far, clearColor);
		CViewport vp;
		vp.init(0.f, 0.f, 1.f, 1.f);
		driver->setupViewport(vp);
		CScissor scissor;
		viewportToScissor(vp, scissor);
		driver->setupScissor(scissor);
		//
		driver->setFrustum(frust.Left, frust.Right, frust.Bottom, frust.Top, frust.Near, frust.Far, frust.Perspective);
		driver->setupViewMatrix(MainCam.getMatrix().inverted());
		driver->setupModelMatrix(CMatrix::Identity);
		//
		//
		const CVector localFrustCorners[8] =
		{
			CVector(frust.Left, frust.Near, frust.Top),
			CVector(frust.Right, frust.Near, frust.Top),
			CVector(frust.Right, frust.Near, frust.Bottom),
			CVector(frust.Left, frust.Near, frust.Bottom),
			CVector(frust.Left  * frust.Far / frust.Near, frust.Far, frust.Top * frust.Far / frust.Near),
			CVector(frust.Right * frust.Far / frust.Near, frust.Far, frust.Top * frust.Far / frust.Near),
			CVector(frust.Right * frust.Far / frust.Near, frust.Far, frust.Bottom * frust.Far / frust.Near),
			CVector(frust.Left  * frust.Far / frust.Near, frust.Far, frust.Bottom * frust.Far / frust.Near)
		};
		// roughly compute covered zones
		//
		/*
		sint frustZoneMinX = INT_MAX;
		sint frustZoneMaxX = INT_MIN;
		sint frustZoneMinY = INT_MAX;
		sint frustZoneMaxY = INT_MIN;
		for(uint k = 0; k < sizeofarray(localFrustCorners); ++k)
		{
			CVector corner = camMat * localFrustCorners[k];
			sint zoneX = (sint) (corner.x / 160.f) - zoneMinX;
			sint zoneY = (sint) floorf(corner.y / 160.f) - zoneMinY;
			frustZoneMinX = std::min(frustZoneMinX, zoneX);
			frustZoneMinY = std::min(frustZoneMinY, zoneY);
			frustZoneMaxX = std::max(frustZoneMaxX, zoneX);
			frustZoneMaxY = std::max(frustZoneMaxY, zoneY);
		}
		*/

		const uint TRI_BATCH_SIZE = 10000; // batch size for rendering
		static std::vector<TPackedZoneBaseSPtr> zones;
		zones.clear();
		pw->getZones(zones);
		for(uint k = 0; k < zones.size(); ++k)
		{
			zones[k]->render(vb, *driver, texturedMaterial, wiredMaterial, MainCam.getMatrix(), TRI_BATCH_SIZE, localFrustCorners);
		}
	}
}
Exemplo n.º 4
0
/*********************************************************\
					displayOrientation()
\*********************************************************/
void displayOrientation()
{
	float x = 0.9f*4.f/3.f;
	float y = 0.1f;
	float radius = 0.015f;

	// Triangle
	CMaterial mat;
	mat.initUnlit();
	mat.setSrcBlend(CMaterial::srcalpha);
	mat.setDstBlend(CMaterial::invsrcalpha);
	mat.setBlend(true);
	
	CVertexBuffer vb;
	vb.setVertexFormat (CVertexBuffer::PositionFlag);
	vb.setNumVertices (7);
	{
		CVertexBufferReadWrite vba;
		vb.lock(vba);
		
		// tri
		vba.setVertexCoord (0, CVector (-radius, 0, 0));
		vba.setVertexCoord (1, CVector (radius, 0, 0));
		vba.setVertexCoord (2, CVector (0, 0, 3*radius));

		// quad
		vba.setVertexCoord (3, CVector (-radius, 0, -radius));
		vba.setVertexCoord (4, CVector (radius, 0, -radius));
		vba.setVertexCoord (5, CVector (radius, 0, radius));
		vba.setVertexCoord (6, CVector (-radius, 0, radius));
	}
	
	CNELU::Driver->activeVertexBuffer(vb);

	CIndexBuffer pbTri;
	pbTri.setNumIndexes (3);
	{
		CIndexBufferReadWrite iba;
		pbTri.lock (iba);
		iba.setTri (0, 0, 1, 2);
	}
	
	CIndexBuffer pbQuad;
	pbQuad.setNumIndexes (6);
	{
		CIndexBufferReadWrite iba;
		pbQuad.lock(iba);
		iba.setTri (0, 3, 4, 5);
		iba.setTri (3, 5, 6, 3);
	}
	
	CNELU::Driver->setFrustum (0.f, 4.f/3.f, 0.f, 1.f, -1.f, 1.f, false);
	CMatrix mtx;
	mtx.identity();
	CNELU::Driver->setupViewMatrix (mtx);

	mat.setColor(CRGBA(50,255,255,150));

	// up
	mtx.identity();
	mtx.translate(CVector(x,0,y));
	mtx.rotateY(MoveListener.getRotZ() );
	mtx.translate(CVector(0,0,radius));
	CNELU::Driver->setupModelMatrix (mtx);
	CNELU::Driver->activeVertexBuffer(vb);
	CNELU::Driver->activeIndexBuffer(pbTri);
	CNELU::Driver->renderTriangles(mat, 0, pbTri.getNumIndexes()/3);

	mat.setColor(CRGBA(50,50,255,150));

	// down
	mtx.identity();
	mtx.translate(CVector(x,0,y));
	mtx.rotateY(MoveListener.getRotZ() + (float)Pi);
	mtx.translate(CVector(0,0,radius));
	CNELU::Driver->setupModelMatrix (mtx);
	CNELU::Driver->renderTriangles(mat, 0, pbTri.getNumIndexes()/3);

	// left
	mtx.identity();
	mtx.translate(CVector(x,0,y));
	mtx.rotateY(MoveListener.getRotZ() - (float)Pi/2);
	mtx.translate(CVector(0,0,radius));
	CNELU::Driver->setupModelMatrix (mtx);
	CNELU::Driver->renderTriangles(mat, 0, pbTri.getNumIndexes()/3);

	// right
	mtx.identity();
	mtx.translate(CVector(x,0,y));
	mtx.rotateY(MoveListener.getRotZ() + (float)Pi/2);
	mtx.translate(CVector(0,0,radius));
	CNELU::Driver->setupModelMatrix (mtx);
	CNELU::Driver->renderTriangles(mat, 0, pbTri.getNumIndexes()/3);
	
	// center
	mtx.identity();
	mtx.translate(CVector(x,0,y));
	mtx.rotateY(MoveListener.getRotZ());
	CNELU::Driver->setupModelMatrix (mtx);
	CNELU::Driver->activeIndexBuffer(pbQuad);
	CNELU::Driver->renderTriangles(mat, 0, pbQuad.getNumIndexes()/3);	
}