<commit_before>//============================================================================
// Name        : DepthOfField.cpp
// Author      : <NAME>
// Version     :
// Copyright   : ;)
// Description : Game Example
//============================================================================

#include "DepthOfField.h"

using namespace p3d;

DepthOfFieldEffect::DepthOfFieldEffect(Texture* texture1, Texture* texture2, const uint32 Width, const uint32 Height) : IEffect(Width, Height)
{
	// Set RTT
	UseCustomTexture(texture1);
	UseCustomTexture(texture2);
	UseRTT(RTT::Color);
	UseRTT(RTT::Depth);

	// Create Fragment Shader
	FragmentShaderString =
		"float DecodeNativeDepth(float native_z, vec4 z_info_local)\n"
		"{\n"
		"return z_info_local.z / (native_z * z_info_local.w + z_info_local.y);\n"
		"}\n"
		"uniform sampler2D uTex0, uTex1, uTex2, uTex3;\n"
		"uniform float uFocalPosition, uFocalRange, uRatioL, uRatioH;\n"
		"uniform vec2 uNearFar;\n"
		"varying vec2 vTexcoord;\n"
		"void main() {\n"
		"float ratioL = uRatioL;\n"
		"float ratioH = uRatioH;\n"
		"float focalPosition = uFocalPosition;\n"
		"float focalRange = uFocalRange;\n"
		"vec4 z_info_local = vec4(uNearFar.x,uNearFar.y,uNearFar.x*uNearFar.y,uNearFar.x-uNearFar.y);\n"
		"float depth = texture2D(uTex3, vTexcoord).x;\n"
		"float linearDepth = DecodeNativeDepth(depth, z_info_local);\n"
		"float ratio = clamp(abs(focalPosition-linearDepth)-focalRange, 0.0, ratioL);\n"
		"if (ratio < 0.4) gl_FragColor = mix(texture2D(uTex2, vTexcoord), texture2D(uTex1, vTexcoord), ratio / (ratioL - ratioH));\n"
		"else gl_FragColor =  mix(texture2D(uTex1, vTexcoord), texture2D(uTex0, vTexcoord), (ratio-ratioH) / (ratioL - ratioH));\n"
		"}";

	CompileShaders();

	Uniform nearFarPlane;
	nearFarPlane.Name = "uNearFar";
	nearFarPlane.Type = DataType::Vec2;
	nearFarPlane.Usage = PostEffects::NearFarPlane;
	AddUniform(nearFarPlane);

	f32 fPosition = 20.f;
	f32 fRange = 2.f;
	f32 rL = 3.1f;
	f32 rH = 1.0f;

	Uniform focalPosition;
	Uniform focalRange;
	Uniform ratioL;
	Uniform ratioH;

	focalPosition.Name = "uFocalPosition";
	focalPosition.Type = DataType::Float;
	focalPosition.Usage = PostEffects::Other;
	focalPosition.SetValue(&fPosition);
	focalRange.Name = "uFocalRange";
	focalRange.Type = DataType::Float;
	focalRange.Usage = PostEffects::Other;
	focalRange.SetValue(&fRange);
	ratioL.Name = "uRatioL";
	ratioL.Type = DataType::Float;
	ratioL.Usage = PostEffects::Other;
	ratioL.SetValue(&rL);
	ratioH.Name = "uRatioH";
	ratioH.Type = DataType::Float;
	ratioH.Usage = PostEffects::Other;
	ratioH.SetValue(&rH);

	AddUniform(focalPosition);
	AddUniform(focalRange);
	AddUniform(ratioL);
	AddUniform(ratioH);
}

DepthOfField::DepthOfField() : ClassName(1024, 768, "Pyros3D - Depth Of Field", WindowType::Close | WindowType::Resize) {}

void DepthOfField::OnResize(const uint32 width, const uint32 height)
{
	// Execute Parent Resize Function
	ClassName::OnResize(width, height);

	// Resize
	Renderer->Resize(width, height);
	projection.Perspective(70.f, (f32)width / (f32)height, 1.f, 1000.f);

	EffectManager->Resize(width, height);
	blurX->Resize(width, height);
	blurY->Resize(width, height);
	resize->Resize((uint32)(width*0.25f), (uint32)(height*0.25f));
	blurXlow->Resize((uint32)(width*0.25f), (uint32)(height*0.25f));
	blurYlow->Resize((uint32)(width*0.25f), (uint32)(height*0.25f));
	depthOfField->Resize((uint32)(width), (uint32)(height));
}

void DepthOfField::Init()
{
	// Initialization

		// Initialize Scene
	Scene = new SceneGraph();<commit_msg>Small fix on Depth Of Field example<commit_after>//============================================================================
// Name        : DepthOfField.cpp
// Author      : <NAME>
// Version     :
// Copyright   : ;)
// Description : Game Example
//============================================================================

#include "DepthOfField.h"

using namespace p3d;

DepthOfFieldEffect::DepthOfFieldEffect(Texture* texture1, Texture* texture2, const uint32 Width, const uint32 Height) : IEffect(Width, Height)
{
	// Set RTT
	UseCustomTexture(texture1);
	UseCustomTexture(texture2);
	UseRTT(RTT::Color);
	UseRTT(RTT::Depth);

	// Create Fragment Shader
	FragmentShaderString =
		"float DecodeNativeDepth(float native_z, vec4 z_info_local)\n"
		"{\n"
		"return z_info_local.z / (native_z * z_info_local.w + z_info_local.y);\n"
		"}\n"
		"uniform sampler2D uTex0; // lower res blur\n"
		"uniform sampler2D uTex1; // medium res blur\n"
		"uniform sampler2D uTex2; // high res\n"
		"uniform sampler2D uTex3; // depth\n"
		"uniform float uFocalPosition, uFocalRange, uRatioL, uRatioH;\n"
		"uniform vec2 uNearFar;\n"
		"varying vec2 vTexcoord;\n"
		"void main() {\n"
		"float ratioL = uRatioL;\n"
		"float ratioH = uRatioH;\n"
		"float focalPosition = uFocalPosition;\n"
		"float focalRange = uFocalRange;\n"
		"vec4 z_info_local = vec4(uNearFar.x,uNearFar.y,uNearFar.x*uNearFar.y,uNearFar.x-uNearFar.y);\n"
		"float depth = texture2D(uTex3, vTexcoord).x;\n"
		"float linearDepth = DecodeNativeDepth(depth, z_info_local);\n"
		"float ratio = clamp(abs(focalPosition-linearDepth)-focalRange, 0.0, ratioL);\n"
		"if (ratio < 0.4) gl_FragColor = mix(texture2D(uTex2, vTexcoord), texture2D(uTex1, vTexcoord), ratio / (ratioL - ratioH));\n"
		"else gl_FragColor =  mix(texture2D(uTex1, vTexcoord), texture2D(uTex0, vTexcoord), (ratio-ratioH) / (ratioL - ratioH));\n"
		"}";

	CompileShaders();

	Uniform nearFarPlane;
	nearFarPlane.Name = "uNearFar";
	nearFarPlane.Type = DataType::Vec2;
	nearFarPlane.Usage = PostEffects::NearFarPlane;
	AddUniform(nearFarPlane);

	f32 fPosition = 20.f;
	f32 fRange = 2.f;
	f32 rL = 3.1f;
	f32 rH = 1.0f;

	AddUniform(Uniform("uFocalPosition", Uniforms::DataType::Float, &fPosition));
	AddUniform(Uniform("uFocalRange", Uniforms::DataType::Float, &fRange));
	AddUniform(Uniform("uRatioL", Uniforms::DataType::Float, &rL));
	AddUniform(Uniform("uRatioH", Uniforms::DataType::Float, &rH));
}

DepthOfField::DepthOfField() : ClassName(1024, 768, "Pyros3D - Depth Of Field", WindowType::Close | WindowType::Resize) {}

void DepthOfField::OnResize(const uint32 width, const uint32 height)
{
	// Execute Parent Resize Function
	ClassName::OnResize(width, height);

	// Resize
	Renderer->Resize(width, height);
	projection.Perspective(70.f, (f32)width / (f32)height, 1.f, 1000.f);

	EffectManager->Resize(width, height);
	blurX->Resize(width, height);
	blurY->Resize(width, height);
	resize->Resize((uint32)(width*0.25f), (uint32)(height*0.25f));
	blurXlow->Resize((uint32)(width*0.25f), (uint32)(height*0.25f));
	blurYlow->Resize((uint32)(width*0.25f), (uint32)(height*0.25f));
	depthOfField->Resize((uint32)(width), (uint32)(height));
}

void DepthOfField::Init()
{
	// Initialization

		// Initialize Scene
	Scene = new SceneGraph();
{'fraction_non_alphanumeric': 0.1960352422907489, 'fraction_numerical': 0.0315712187958884, 'mean_word_length': 5.480494766888677, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2873545', 'n_tokens_mistral': 2738, 'n_tokens_neox': 2502, 'n_words': 456}