Exemple #1
0
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmd, int nCmd)
{
	u32 r = Log2(48);

	Window* window = Window::Create("BFAATW!", 480, 320, false);
	if ( ! window ) return 42;
	Renderer* renderer = Renderer::Create(window);
	if ( ! renderer ) { delete window; return 42; }

	ResourceManager* resources = new ResourceManager(renderer);

	Style* style = resources->LoadStyle("basic.style");
	renderer->SetStyle(style);

	Mesh* plane = CreatePlane(resources, "ground");
	Texture* ground = resources->LoadTexture("hill_mound_b.dds");
	Matrix planeWorld(Matrix::Identity);
	Matrix tmp;
	MatrixScaling(tmp, Vector(10.0f, 1.0f, 10.0f));
	MatrixMultiply(planeWorld, tmp);
	MatrixTranslation(tmp, Vector(0.0f, -3.0f, 0.0f));
	MatrixMultiply(planeWorld, tmp);

	Mesh* cube = CreateCube(resources, "cube");
	Texture* diffuse = resources->LoadTexture("Catwalk_03_Diffuse_512.dds");
	Matrix cubeWorld(Matrix::Identity);

	Matrix view, projection;
	Camera::Perspective(MATH_PI_3, 480.0f/320.0f, 0.1f, 100.0f, projection);

	RenderTarget* shadowMap = resources->CreateRenderTarget("shadowmap", 1024, 1024, RT_SHADOWMAP);

	Vector lightPosition(3.0f, 5.0f, 2.0f);
	Matrix lightView, lightProjection;
	Camera::Orthographic(-3.0f, 3.0f, -3.0f, 3.0f, 0.1f, 100.0f, lightProjection);
	Camera::LookAt(lightPosition, Vector::Zero, Vector::UnitY, lightView);

	Timer timer(60);
	timer.Start();

	Texture* depth = shadowMap->GetTexture(0);
	style->SetParameter("shadow", &depth);
	style->SetParameter("lightPosition", &lightPosition);

	while ( window->Update() && ! window->IsKeyDown(KEY_ESCAPE) )
	{
		while ( timer.Update() )
		{
			if ( window->IsMouseButtonDown(MOUSE_RIGHT) )
			{
				// Set camera position
				u32 wx, wy;
				window->GetSize(wx, wy);
				s32 mx, my;
				window->GetMousePosition(mx, my);
				Vector eye;
				float theta = -float(my) / float(wy) * MATH_2_PI;
				float rho = float(mx) / float(wx) * MATH_2_PI * 2.0f;
				theta = Clamp(theta, -4.5f, -2.0f);
				eye.x = 5.0f * cosf(theta) * cosf(rho);
				eye.y = 5.0f * sinf(theta);
				eye.z = 5.0f * cosf(theta) * sinf(rho);
				Camera::LookAt(eye, Vector::Zero, Vector::UnitY, view);
			}

			MatrixRotation(tmp, Vector::UnitY, MATH_PI / 120.0f);
			MatrixMultiply(planeWorld, tmp);

			//cubeWorld = Matrix::Identity * Matrix::Scaling(Vector::One * (cosf(timer.GetTime() * 13.0f)+1.2f));
		}

		{ // render to shadow map
			renderer->SetRenderTarget(shadowMap);
			renderer->SetViewport(0, 0, 1024, 1024);
			renderer->Clear();

			int b = false;
			style->SetParameter("processShadow", &b);

			Matrix viewProjection = lightView;
			MatrixMultiply(viewProjection, lightProjection);
			style->SetParameter("viewProjection", &viewProjection);

			style->SetParameter("diffuse", (const Texture**) &diffuse);
			style->SetParameter("world", &cubeWorld);
			renderer->Render(cube);

			style->SetParameter("diffuse", (const Texture**) &ground);
			style->SetParameter("world", &planeWorld);
			renderer->Render(plane);
		}

		{ // render to screen
			renderer->SetRenderTarget(0);
			renderer->SetViewport(0, 0, 480, 320);
			renderer->Clear();

			int b = true;
			style->SetParameter("processShadow", &b);

			Matrix viewProjection = view;
			MatrixMultiply(viewProjection, projection);
			style->SetParameter("viewProjection", &viewProjection);

			style->SetParameter("diffuse", (const Texture**) &diffuse);
			style->SetParameter("world", &cubeWorld);
			renderer->Render(cube);

			style->SetParameter("diffuse", (const Texture**) &ground);
			style->SetParameter("world", &planeWorld);
			renderer->Render(plane);
		}

		window->SwapBuffers();
	}

	delete resources;
	delete renderer;
	delete window;

	return 0;
}