Exemplo n.º 1
0
void Application::Render( void )
{
	if (!m_initialized)
	{
		return;
	}

	for (unsigned i = 0; i < m_sceneObjectList.size(); i++)
	{
		SceneObject* object = m_sceneObjectList[i];
		Material* material = object->GetMaterial();

		if (object->IsHidden())
		{
			continue;
		}

		RenderUnit* renderUnit = new RenderUnit();

		renderUnit->m_vb = object->GetMesh()->GetVertexBuffer();
		renderUnit->m_ib = object->GetMesh()->GetIndexBuffer();
		renderUnit->m_wireFrame = material->wireFrame;

		int vertexShaderId = m_renderer->GetShadeMode() == Renderer::SHADING_MODE_PHONG 
			? material->vertexShaderId 
			: (material->vertexShaderId == VS_TANGENT_SPACE_LIGHTING_SC2_UV ? VS_FIXED_FUNCTION_ALT_UV : VS_FIXED_FUNCTION);

		int pixelShaderId = m_renderer->GetShadeMode() == Renderer::SHADING_MODE_PHONG 
			? material->pixelShaderId 
			: PS_FIXED_FUNCTION;

		switch (vertexShaderId)
		{
		case VS_FIXED_FUNCTION:
			{
				VsFixedFunction* myVS = new VsFixedFunction();
				renderUnit->m_vs = myVS;

				Matrix4 worldMatrix;
				object->GetWorldMatrix(worldMatrix);
				MatrixTranspose(myVS->inverseWorldMatrix, worldMatrix);	// This only works when there is no translation 
																		// or scaling!!!
				MatrixMultiply(myVS->worldViewProjMatrix, worldMatrix, m_activeCamera->GetViewMatrix());
				MatrixMultiply(myVS->worldViewProjMatrix, myVS->worldViewProjMatrix, m_activeCamera->GetProjMatrix());

				myVS->lightPosition = m_activeLight->position;

				myVS->ambientColor = m_activeLight->ambientColor;
				myVS->diffuseColor = m_activeLight->diffuseColor;
			}
			break;
		case VS_FIXED_FUNCTION_ALT_UV:
			{
				VsFixedFunctionAltUv* myVS = new VsFixedFunctionAltUv();
				renderUnit->m_vs = myVS;

				Matrix4 worldMatrix;
				object->GetWorldMatrix(worldMatrix);
				MatrixTranspose(myVS->inverseWorldMatrix, worldMatrix);	// This only works when there is no translation 
				// or scaling!!!
				MatrixMultiply(myVS->worldViewProjMatrix, worldMatrix, m_activeCamera->GetViewMatrix());
				MatrixMultiply(myVS->worldViewProjMatrix, myVS->worldViewProjMatrix, m_activeCamera->GetProjMatrix());

				myVS->lightPosition = m_activeLight->position;

				myVS->ambientColor = m_activeLight->ambientColor;
				myVS->diffuseColor = m_activeLight->diffuseColor;
			}
			break;
		case VS_TANGENT_SPACE_LIGHTING:
			{
				VsTangentSpaceLighting* myVS = new VsTangentSpaceLighting();
				renderUnit->m_vs = myVS;

				object->GetWorldMatrix(myVS->worldMatrix);
				MatrixTranspose(myVS->inverseWorldMatrix, myVS->worldMatrix);	// This only works when there is no translation 
																				// or scaling!!!
				MatrixMultiply(myVS->worldViewProjMatrix, myVS->worldMatrix, m_activeCamera->GetViewMatrix());
				MatrixMultiply(myVS->worldViewProjMatrix, myVS->worldViewProjMatrix, m_activeCamera->GetProjMatrix());

				myVS->lightPosition = m_activeLight->position;
			}
			break;

		case VS_TANGENT_SPACE_LIGHTING_SC2_UV:
			{
				VsTangentSpaceLightingSc2Uv* myVS = new VsTangentSpaceLightingSc2Uv();
				renderUnit->m_vs = myVS;

				object->GetWorldMatrix(myVS->worldMatrix);
				MatrixTranspose(myVS->inverseWorldMatrix, myVS->worldMatrix);	// This only works when there is no translation 
																				// or scaling!!!
				MatrixMultiply(myVS->worldViewProjMatrix, myVS->worldMatrix, m_activeCamera->GetViewMatrix());
				MatrixMultiply(myVS->worldViewProjMatrix, myVS->worldViewProjMatrix, m_activeCamera->GetProjMatrix());

				myVS->lightPosition = m_activeLight->position;
			}
			break;

		default:
			break;
		}

		switch (pixelShaderId)
		{
		case PS_FIXED_FUNCTION:
			{
				PsFixedFunction* myPS = new PsFixedFunction();
				renderUnit->m_ps = myPS;

				myPS->baseTexture = m_textureManager->GetTexture(material->baseTextureId);
			}
			break;

		case PS_NORMAL_MAP:
			{
				PsNormalMap* myPS = new PsNormalMap();
				renderUnit->m_ps = myPS;

				myPS->baseTexture = m_textureManager->GetTexture(material->baseTextureId);
				myPS->normalTexture = m_textureManager->GetTexture(material->bumpTextureId);
				myPS->diffuseColor = m_activeLight->diffuseColor;
				myPS->ambientColor = m_activeLight->ambientColor;
			}
			break;

		case PS_TOON_LIGHTING:
			{
				PsToonLighting* myPS = new PsToonLighting();
				renderUnit->m_ps = myPS;

				myPS->baseTexture = m_textureManager->GetTexture(material->baseTextureId);
				myPS->diffuseColor = m_activeLight->diffuseColor;
				myPS->ambientColor = m_activeLight->ambientColor;
			}
			break;

		default:
			break;
		}

		m_renderer->AddRenderUnit(renderUnit);
	}

	m_backBuffer->Clear();
	m_depthBuffer->Clear();

	m_renderer->SetRenderTarget(m_backBuffer, m_depthBuffer);
	m_renderer->Render();

	m_backBuffer->Present();
}
Exemplo n.º 2
0
void Application::Update( void )
{
    if (!m_initialized)
    {
        return;
    }

    // calculate frame time
    LARGE_INTEGER counter;
    QueryPerformanceCounter(&counter);
    float dt = static_cast<float>((counter.QuadPart - m_nTicks) / double(m_nTicksPerSecond));
    m_nTicks = counter.QuadPart;

    // FPS display
    char str[256];
    sprintf_s(str, 256, "FPS: %.1f", 1.0f / dt);
    m_textOutput->Print(str, 5, 5);

    // cycle through objects
    if (m_inputCapturer->IsKeyPressed(KC_C))
    {
        m_sceneObjectList[m_activeObjectIndex]->Hide(true);

        if (m_activeObjectIndex < m_sceneObjectList.size() - 1)
        {
            m_activeObjectIndex++;
        }
        else
        {
            m_activeObjectIndex = 0;
        }

        m_sceneObjectList[m_activeObjectIndex]->Hide(false);
    }

    SceneObject* object = m_sceneObjectList[m_activeObjectIndex];

    // toggle wireframe mode
    if (m_inputCapturer->IsKeyPressed(KC_W))
    {
        bool& wireFrame = object->GetMaterial()->wireFrame;
        wireFrame = !wireFrame;
    }

    // camera movement
    int dx, dy, wheelRotation;
    m_inputCapturer->GetMouseMovement(dx, dy, wheelRotation);

    if (m_inputCapturer->IsLeftBtnDown())
    {
        if (dx != 0 || dy != 0)
        {
            m_activeCamera->Orbit(dy * 0.5f, dx * 0.5f);
        }
    }

    if (wheelRotation != 0)
    {
        m_activeCamera->LocalMove(0, 0, wheelRotation * 0.01f);
    }

    // light source movement
    if (m_inputCapturer->IsRightBtnDown())
    {
        if (dx != 0 || dy != 0)
        {
            m_activeLight->position.x += dx * 0.5f;
            m_activeLight->position.y -= dy * 0.5f;
        }
    }

    // reset scene
    if (m_inputCapturer->IsKeyPressed(KC_R))
    {
        m_activeCamera->Reset();
        m_activeLight->position = Vector3(30.0f, 15.0f, -15.0f);
    }

    m_inputCapturer->ClearMouseMovement();
}