//! renders from a render map
	void EntityRenderer::Render(RenderMap& renderMap)
	{
		for(RenderMap::iterator it = renderMap.begin(); it != renderMap.end(); ++it)
		{
			Material* pMaterial = (*it).second.pMaterial;
			pMaterial->Begin();

			for(VertexMap::iterator it2 = (*it).second.m_VertexMap.begin(); 
				it2 != (*it).second.m_VertexMap.end();
				++it2)
			{
				VertexInfo& vertexInfo = (*it2).second;
				vertexInfo.pVertexBuffer->Begin();
				vertexInfo.pVertexBuffer->Draw(vertexInfo.aWorldTransforms, vertexInfo.aTextureTransforms, pMaterial);
				vertexInfo.pVertexBuffer->End();
			}
			
			pMaterial->End();
			m_Stats.Increment(pMaterial);
		}
	}
void StencilEffects::RenderConstructionEffect()
{
    if(m_aStencilMeshes.empty())
        return;

    GraphicsDriver::Instance()->SetRenderState(GraphicsDriver::RS_StencilWriting, 0xff);

    // Fill stencil with 1s
    GraphicsDriver::Instance()->SetStencilFunc(GraphicsDriver::CF_Never, 1 << 1, 0xff);
    GraphicsDriver::Instance()->SetStencilOps(GraphicsDriver::SO_Replace, GraphicsDriver::SO_Keep, GraphicsDriver::SO_Keep);

    GraphicsDriver::Instance()->SetRenderState(GraphicsDriver::RS_ColorWriting, false);

    // Render stencil meshes
    m_DefaultMaterial->Begin();
    m_DefaultMaterial->GetShader()->Begin();
    std::vector<Matrix44> worldTransforms, textureTransforms;
    worldTransforms.resize(1);
    textureTransforms.resize(1);
    for(std::vector< Handle<MeshEntity> >::iterator it = m_aStencilMeshes.begin(); it != m_aStencilMeshes.end(); )
    {
        if(!(*it).IsValid())
        {
            it = m_aStencilMeshes.erase(it);
            continue;
        }

        for(auto submesh : (*it)->GetSubMeshes())
        {
            GraphicComponent* gc = submesh->GetComponent<GraphicComponent>();
            gc->GetVertexBuffer()->Begin(m_DefaultMaterial->GetShader());
            worldTransforms[0] = submesh->GetWorldTransform();
            gc->GetVertexBuffer()->Draw(worldTransforms, std::vector<Matrix44>());
        }
        ++it;
    }

    GraphicsDriver::Instance()->SetRenderState(GraphicsDriver::RS_DepthTesting, false);
    GraphicsDriver::Instance()->SetRenderState(GraphicsDriver::RS_ColorWriting, true);
    GraphicsDriver::Instance()->SetRenderState(GraphicsDriver::RS_StencilWriting, 0); // no more writing into the stencil buffer
    GraphicsDriver::Instance()->SetStencilFunc(GraphicsDriver::CF_Equal, 1 << 1, 0xff); // Render only where stencil is 1

    // render billboards
    m_BillBoardEntity->GetComponent<TextureAnimationVisitor>()->Update();
    GraphicComponent* pGC = m_BillBoardEntity->GetComponent<GraphicComponent>();
    VertexBuffer* pVB = pGC->GetVertexBuffer();
    Material* pMat = pGC->GetMaterial();
    Shader* pShader = pMat->GetShader();
    pMat->Begin();
    pShader->GetConstant("uAmbientColor")->Set(&pMat->GetColor(), 1);
    pShader->Begin();
    pVB->Begin(pShader);
    const Matrix44& billboardMatrix = EntityRenderer::Instance()->Get3DCamera()->GetViewMatrixInverse();
    textureTransforms[0] = pGC->GetTextureTransform();
    for(auto &stencilMesh : m_aStencilMeshes)
    {
        worldTransforms[0] = billboardMatrix;
        worldTransforms[0].SetTranslation(stencilMesh->GetAbsolutePosition());
        pVB->Draw(worldTransforms, textureTransforms);
    }

    GraphicsDriver::Instance()->SetRenderState(GraphicsDriver::RS_DepthTesting, true);
}
Example #3
0
void EEViewPort::OnPaint(wxPaintEvent& event)
{
	wxPaintDC dc(this);
	_glContext->SetCurrent(*this);

	Engine::Instance()->Render();

	// custom rendering
	GraphicsDriver::Instance()->SetRenderTarget(NULL);
	GraphicsDriver::Instance()->SetRenderState(GraphicsDriver::RS_DepthFunc, GraphicsDriver::CF_LessOrEqual);
	GraphicsDriver::Instance()->SetTransform(GraphicsDriver::TS_Projection, EntityRenderer::Instance()->Get3DCamera()->GetProjectionMatrix());
	GraphicsDriver::Instance()->SetTransform(GraphicsDriver::TS_View, EntityRenderer::Instance()->Get3DCamera()->GetViewMatrix());		
	GraphicsDriver::Instance()->SetRenderState(GraphicsDriver::RS_LineWidth, 1.0f);
	ExplosionEditor* pEditor = static_cast<ExplosionEditor*>(wxTheApp->GetTopWindow());
	MeshEntity* pMesh = pEditor->GetCurrentMesh();
	ExplosionVisitor* pExplosionVisitor = pMesh ? pMesh->GetComponent<ExplosionVisitor>() : NULL;
	if(pMesh && pExplosionVisitor)
	{
		Color wireColor = Color::Create(0.5f, 0.5f, 0.5f);
		Color selectedColor = Color::White;
		Color chunkColor = Color::Create(0.5f, 0.0f, 0.5f, 0.5f);
		Color selectedChunkColor = Color::Create(1.0f, 1.0f, 0.0f, 0.5f);

		std::vector<SubMesh*> subMeshes = pMesh->GetSubMeshes();
		Array<ExplosionVisitor::ChunkGroup>& aChunkGroups = pExplosionVisitor->GetChunkGroups();
		Material* pMaterial = pEditor->GetMaterial();
		pMaterial->Begin();
		pMaterial->GetShader()->Begin();

		for(uint i=0; i<(uint)subMeshes.size(); ++i)
		{
			VertexBuffer* pVB = subMeshes[i]->GetComponent<GraphicComponent>()->GetVertexBuffer();
			Vertex3D* pVertices = reinterpret_cast<Vertex3D*>(pVB->GetVertices());
			//ushort* pIndices = (ushort*)pVB->GetIndices();
			GraphicsDriver::Instance()->SetTransform(GraphicsDriver::TS_World, subMeshes[i]->GetWorldTransform());
			GraphicsDriver::Instance()->SetRenderState(GraphicsDriver::RS_FillMode, GraphicsDriver::FM_Line);
			pMaterial->GetShader()->UploadConstant<Color>("uAmbientColor", &wireColor, 1);
			pVB->Begin(pMaterial->GetShader());
			pVB->Draw();
			pVB->End();

			// render explosion chunks	
			int selectedChunk = pEditor->GetSelectedChunk();
			int selectedSubMesh = pEditor->GetSelectedSubMesh();

			GraphicsDriver::Instance()->SetRenderState(GraphicsDriver::RS_AlphaBlending, true);
			GraphicsDriver::Instance()->SetRenderState(GraphicsDriver::RS_FillMode, GraphicsDriver::FM_Solid);
			Array<ExplosionVisitor::Chunk>& aChunks = aChunkGroups[i].m_aChunks;
			for(uint j=0; j<aChunks.GetSize(); ++j)
			{
				if(i == selectedSubMesh
					&& j == selectedChunk)
				{
					GraphicsDriver::Instance()->SetCullMode(GraphicsDriver::CM_None);
					pMaterial->GetShader()->UploadConstant<Color>("uAmbientColor", &selectedChunkColor, 1);
				}
				else
				{
					GraphicsDriver::Instance()->SetCullMode(GraphicsDriver::CM_CounterClockWise);
					pMaterial->GetShader()->UploadConstant<Color>("uAmbientColor", &chunkColor, 1);
				}

				for (uint k = 0; k<aChunks[j].aTriangleIndices.GetSize(); ++k)
				{
					uint triangleIndex = aChunks[j].aTriangleIndices[k];
					pEditor->DrawTriangle(pVertices[(triangleIndex*3)+0].Pos, pVertices[(triangleIndex*3)+1].Pos, pVertices[(triangleIndex*3)+2].Pos);
				}
			}

			GraphicsDriver::Instance()->SetCullMode(GraphicsDriver::CM_CounterClockWise);
			if(m_SelectedTriangleIndex >= 0 && m_SelectedSubMeshIndex == i)
			{
				GraphicsDriver::Instance()->SetRenderState(GraphicsDriver::RS_FillMode, GraphicsDriver::FM_Line);
				pMaterial->GetShader()->UploadConstant<Color>("uAmbientColor", &selectedColor, 1);
				pEditor->DrawTriangle(pVertices[(m_SelectedTriangleIndex*3)+0].Pos, pVertices[(m_SelectedTriangleIndex*3)+1].Pos, pVertices[(m_SelectedTriangleIndex*3)+2].Pos);
			}
		}

		pEditor->GetMaterial()->GetShader()->End();
		pEditor->GetMaterial()->End();
	}

	GraphicsDriver::Instance()->SetRenderState(GraphicsDriver::RS_FillMode, GraphicsDriver::FM_Solid);

	glFlush();
	SwapBuffers();
}