void DrawScene(Texture& renderTarget) { // Begin drawing to render target and clear it renderTarget.BeginDrawing(&Color::Black); // Background with moving moon background.Draw(); moon.Draw(moonPosition, sinf(moonPosition.x * 0.1f) * 0.3f); // Particle effects spinningEffect.Draw(); cursorEffect.Draw(); // Draw content of the render texture renderTexture.Draw(Vec2(100, 100), -moonPosition.x * 0.01f); // Walking character { Sprite::DrawParams params; params.position = characterPosition; params.flipX = characterDir == Dir_Left; params.scale = 1.4f; character.Draw(¶ms); } // End drawing to render target renderTarget.EndDrawing(); }
void DrawToRenderTexture(Texture& renderTarget) { renderTarget.BeginDrawing(&Color::Black); // Draw the moon { const Rect rect(0.0f, 0.0f, 256.0f, 256.0f); Sprite::DrawParams params; params.rect = ▭ moon.Draw(¶ms); } // Draw some debug shapes Shape::DrawCircle(Vec2(50, 50), 50, 5, moonPosition.x * 0.1f, Color(1, 0, 0, 0.7f)); Shape::DrawCircle(Vec2(130, 130), 70, 15, -moonPosition.x * 0.1f, Color(0, 1, 0, 0.7f)); Shape::DrawRectangle(150, 150, 240, 220, moonPosition.x * 0.05f, Color(0, 0, 1, 0.7f)); Shape::DrawLine(Vec2(10, 10), Vec2(200, 200), Color::White); // Draw debug text font.Draw("This is\nrender\ntexture", Vec2(20, 70), Color::Yellow); renderTarget.EndDrawing(); }
void Postprocessing_DrawQuake(Texture& output, Texture& scene) { output.BeginDrawing(&Color::Black); quake->material.SetTechnique("quake"); quake->material.SetTextureParameter("ColorMap", scene); quake->material.SetFloatParameter("Offset", (const float*) &quake->targetOffset, 2); quake->material.DrawFullscreenQuad(); output.EndDrawing(); }
void Postprocessing_DrawOldTV(Texture& renderTarget, Texture& scene) { renderTarget.BeginDrawing(); oldtv->material.SetTechnique("oldtv"); oldtv->material.SetTextureParameter("ColorMap", scene, Sampler::DefaultPostprocess); oldtv->material.SetFloatParameter("Time", oldtv->time); oldtv->material.SetFloatParameter("OverExposureAmount", 0.1f); oldtv->material.SetFloatParameter("DustAmount", 4.0f); oldtv->material.SetFloatParameter("FrameJitterFrequency", 3.0f); oldtv->material.SetFloatParameter("MaxFrameJitter", 1.4f); const Color filmColor(1.0f, 0.7559052f, 0.58474624f, 1.0f); oldtv->material.SetFloatParameter("FilmColor", (const float*) &filmColor, 4); oldtv->material.SetFloatParameter("GrainThicknes", 1.0f); oldtv->material.SetFloatParameter("GrainAmount", 0.8f); oldtv->material.SetFloatParameter("ScratchesAmount", 3.0f); oldtv->material.SetFloatParameter("ScratchesLevel", 0.7f); Sampler dustSampler; dustSampler.SetFiltering(true, true); dustSampler.SetWrapMode(Sampler::WrapMode_ClampToBorder, Sampler::WrapMode_ClampToBorder, Color::White); oldtv->material.SetTextureParameter("DustMap", oldtv->dustMap, dustSampler); Sampler lineSampler; lineSampler.SetFiltering(false, false); lineSampler.SetWrapMode(Sampler::WrapMode_ClampToBorder, Sampler::WrapMode_Clamp, Color::White); oldtv->material.SetTextureParameter("LineMap", oldtv->lineMap, lineSampler); Sampler tvSampler; tvSampler.SetFiltering(true, true); tvSampler.SetWrapMode(Sampler::WrapMode_Clamp, Sampler::WrapMode_Clamp); oldtv->material.SetTextureParameter("TvMap", oldtv->tvAndNoiseMap, tvSampler); Sampler noiseSampler; noiseSampler.SetFiltering(true, true); noiseSampler.SetWrapMode(Sampler::WrapMode_Repeat, Sampler::WrapMode_Repeat); oldtv->material.SetTextureParameter("NoiseMap", oldtv->tvAndNoiseMap, noiseSampler); oldtv->material.DrawFullscreenQuad(); renderTarget.EndDrawing(); }
void DrawLoadingScreen(Texture& renderTarget) { // Begin drawing to render target renderTarget.BeginDrawing(); // Draw background loadingScreen.Draw(); // Draw rotating cog wheels DrawCogWheel(640, 930 - 750, 110, cogWheelRotation, Color(0.7f, 0.4f, 0.1f, 1)); DrawCogWheel(820, 880 - 750, 70, -cogWheelRotation * 1.3f, Color(0.6f, 0.3f, 0.1f, 1)); DrawCogWheel(900, 970 - 750, 42, cogWheelRotation * 1.8f, Color(0.6f, 0.4f, 0.1f, 1)); DrawCogWheel(952, 914 - 750, 30, -cogWheelRotation * 2.8f, Color(0.7f, 0.4f, 0.1f, 1)); DrawCogWheel(990, 957 - 750, 22, cogWheelRotation * 3.4f, Color(0.5f, 0.3f, 0.1f, 1)); // End drawing to render target renderTarget.EndDrawing(); }
void Postprocessing_DrawRainyGlass(Texture& renderTarget, Texture& scene) { Texture dropletTarget = RenderTexturePool::Get(256, 256); // Clear droplet render target to default front-facing direction const Color frontClearColor(0.5f /* x == 0 */, 0.5f /* y = 0 */, 1.0f /* z = 1 */, 0.0f); dropletTarget.BeginDrawing(&frontClearColor); // Evaporation if (rain->dropletBuffer.IsValid()) { if (rain->rainEvaporation > 0.02f) // Apply rain evaporation { rain->material.SetTechnique("rain_evaporation"); rain->rainEvaporation = 0; } else // Just copy previous buffer (skip evaporation step this time) { rain->material.SetTechnique("copy_texture"); } rain->material.SetTextureParameter("ColorMap", rain->dropletBuffer, Sampler::DefaultPostprocess); rain->material.DrawFullscreenQuad(); } // Droplets const Vec2 sizeScale( (float) dropletTarget.GetWidth(), (float) dropletTarget.GetHeight() ); while (rain->deltaTime > 0.0f) { const float stepDeltaTime = min(rain->deltaTime, 1 / 60.0f); rain->deltaTime -= stepDeltaTime; Postprocessing_UpdateRainyGlassStep(stepDeltaTime); const unsigned int numDroplets = rain->droplets.size(); if (!numDroplets) continue; std::vector<Vec2> verts(numDroplets * 4); std::vector<Vec2> tex(numDroplets * 4); std::vector<unsigned short> indices(numDroplets * 6); int currVertexIndex = 0; unsigned short* currIndex = &indices[0]; std::vector<RainyGlass::Droplet>::iterator dropletsEnd = rain->droplets.end(); for (std::vector<RainyGlass::Droplet>::iterator it = rain->droplets.begin(); it != dropletsEnd; ++it) { Vec2 size = Vec2(it->size, it->size); Vec2 pos = it->pos - size * 0.5f; size *= sizeScale; pos *= sizeScale; Vec2* currVertex = &verts[currVertexIndex]; *(currVertex++) = pos; *(currVertex++) = pos + Vec2(size.x, 0.0f); *(currVertex++) = pos + size; *(currVertex++) = pos + Vec2(0.0f, size.y); Vec2* currTex = &tex[currVertexIndex]; (currTex++)->Set(0.0f, 0.0f); (currTex++)->Set(1.0f, 0.0f); (currTex++)->Set(1.0f, 1.0f); (currTex++)->Set(0.0f, 1.0f); *(currIndex++) = currVertexIndex; *(currIndex++) = currVertexIndex + 1; *(currIndex++) = currVertexIndex + 2; *(currIndex++) = currVertexIndex; *(currIndex++) = currVertexIndex + 2; *(currIndex++) = currVertexIndex + 3; currVertexIndex += 4; } Shape::DrawParams drawParams; drawParams.SetGeometryType(Shape::Geometry::Type_Triangles); drawParams.SetNumVerts(verts.size()); drawParams.SetPosition(&verts[0], sizeof(Vec2)); drawParams.SetTexCoord(&tex[0], 0, sizeof(Vec2)); drawParams.SetIndices(indices.size(), &indices[0]); Material& defaultMaterial = App::GetDefaultMaterial(); defaultMaterial.SetTechnique("tex_col"); defaultMaterial.SetFloatParameter("Color", (const float*) &Color::White, 4); defaultMaterial.SetTextureParameter("ColorMap", rain->dropletTexture); defaultMaterial.Draw(&drawParams); } dropletTarget.EndDrawing(); // Swap droplet buffer if (rain->dropletBuffer.IsValid()) RenderTexturePool::Release(rain->dropletBuffer); rain->dropletBuffer = dropletTarget; // Apply droplet buffer distortion to scene renderTarget.BeginDrawing(); Sampler colorSampler = Sampler::DefaultPostprocess; colorSampler.minFilterLinear = true; colorSampler.magFilterLinear = true; rain->material.SetTechnique("rain_distortion"); rain->material.SetFloatParameter("DropletColor", (const float*) &rain->dropletColor, 4); rain->material.SetTextureParameter("ColorMap", scene, colorSampler); rain->material.SetTextureParameter("NormalMap", rain->dropletBuffer); rain->material.DrawFullscreenQuad(); renderTarget.EndDrawing(); }
void Postprocessing_DrawBloom(Texture& output, Texture& scene) { Assert(bloom); const int width = App::GetWidth(); const int height = App::GetHeight(); // Downsample to half size Texture halfSizeRenderTexture = RenderTexturePool::Get(width / 2, height / 2); halfSizeRenderTexture.BeginDrawing(); bloom->material.SetTechnique("downsample2x2"); bloom->material.SetTextureParameter("ColorMap", scene); bloom->material.DrawFullscreenQuad(); halfSizeRenderTexture.EndDrawing(); // Downsample to quarter size Texture quarterSizeRenderTextures[2]; quarterSizeRenderTextures[0] = RenderTexturePool::Get(width / 4, height / 4); quarterSizeRenderTextures[0].BeginDrawing(); bloom->material.SetTextureParameter("ColorMap", halfSizeRenderTexture); bloom->material.DrawFullscreenQuad(); quarterSizeRenderTextures[0].EndDrawing(); RenderTexturePool::Release(halfSizeRenderTexture); // Blur downsampled scene vertically and horizontally if (bloom->numBlurSteps) { bloom->material.SetFloatParameter("BlurKernel", &bloom->blurKernel); quarterSizeRenderTextures[1] = RenderTexturePool::Get(width / 4, height / 4); } for (int i = 0; i < bloom->numBlurSteps; i++) { quarterSizeRenderTextures[1].BeginDrawing(); bloom->material.SetTechnique("vertical_blur"); bloom->material.SetTextureParameter("ColorMap", quarterSizeRenderTextures[0], Sampler::DefaultPostprocess); bloom->material.DrawFullscreenQuad(); quarterSizeRenderTextures[1].EndDrawing(); quarterSizeRenderTextures[0].BeginDrawing(); bloom->material.SetTechnique("horizontal_blur"); bloom->material.SetTextureParameter("ColorMap", quarterSizeRenderTextures[1], Sampler::DefaultPostprocess); bloom->material.DrawFullscreenQuad(); quarterSizeRenderTextures[0].EndDrawing(); } if (bloom->numBlurSteps) RenderTexturePool::Release(quarterSizeRenderTextures[1]); // Blend result with the scene output.BeginDrawing(); bloom->material.SetTechnique("blend"); bloom->material.SetTextureParameter("ColorMap0", quarterSizeRenderTextures[0]); bloom->material.SetTextureParameter("ColorMap1", scene, Sampler::DefaultPostprocess); bloom->material.SetFloatParameter("BlendFactor", &bloom->blendFactor); bloom->material.DrawFullscreenQuad(); output.EndDrawing(); RenderTexturePool::Release(quarterSizeRenderTextures[0]); }