void gfx::GraphicsEngine::RenderSprites(RenderQueue* drawQueue){
	glViewport(0, 0, m_Width, m_Height);
	//Render Sprites
	ShaderProgram* spriteProg = g_ShaderBank.GetProgramFromHandle(m_SpriteShader);
	spriteProg->Apply();
	glBindVertexArray(0);
	glDisable(GL_DEPTH_TEST);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	for (int layer = 0; layer < 3; layer++){
		for (auto& spr : drawQueue->GetSpriteQueue()[layer]){
			spriteProg->SetUniformVec4("g_Pos", spr.GetPosFlt());
			spriteProg->SetUniformVec4("g_Size", spr.GetSizeFlt());
			spriteProg->SetUniformVec4("g_Color", spr.GetColor());
			g_MaterialBank.GetTexture(spr.GetTexture())->Apply(spriteProg->FetchUniform("g_Texture"), 0);
			if (g_MaterialBank.GetTexture(spr.GetTexture())->GetChannels() == 1){
				spriteProg->SetUniformBool("g_GreyScale", true);
			}
			else{
				spriteProg->SetUniformBool("g_GreyScale", false);
			}
			glDrawArrays(GL_POINTS, 0, 1);
		}
	}
}
void gfx::GraphicsEngine::RenderToTexture(RenderQueue* drawQueue){
	TextureHandle target = drawQueue->GetTargetTexture();
	m_FrameBuffer.SetTexture(target);
	m_FrameBuffer.Apply();

	ShaderProgram* spriteProg = g_ShaderBank.GetProgramFromHandle(m_SpriteShader);
	spriteProg->Apply();
	glBindVertexArray(0);
	glDisable(GL_DEPTH_TEST);
	glEnable(GL_BLEND);
	static bool additive = false; 
	ImGui::Checkbox("Additive", &additive);
	if (additive){
		glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	}
	else {
		glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
	}
	//static glm::vec4 color = glm::vec4(1);
	//ImGui::ColorEdit4("BrushColor", &color[0], true);

	for (auto& brush : drawQueue->GetBrushQueue()){
		Texture* brushTex = g_MaterialBank.GetTexture(brush.Texture);
		glm::vec2 brushSize(brush.Size / (m_Width * 0.5f), brush.Size / m_Height);

		spriteProg->SetUniformVec4("g_Pos", glm::vec4(glm::vec2(brush.Position.x - brushSize.x * 0.5f, 1.0f - brush.Position.y + brushSize.y * 0.5f), 0, 0));
		spriteProg->SetUniformVec4("g_Size", glm::vec4(brushSize.x, brushSize.y, 1, 1));
		spriteProg->SetUniformVec4("g_Color", ColorPicker::m_color);
		brushTex->Apply(spriteProg->FetchUniform("g_Texture"), 0);
		spriteProg->SetUniformBool("g_GreyScale", false);
		glDrawArrays(GL_POINTS, 0, 1);
	}
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void gfx::GraphicsEngine::RenderActiveTarget(){
	glViewport((GLint)(m_Width * 0.5f), BUTTON_SIZE, (GLint)(m_Width * 0.5f), m_Height - BUTTON_SIZE * 2);
	glDisable(GL_DEPTH_TEST);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	ShaderProgram* spriteProg = g_ShaderBank.GetProgramFromHandle(m_SpriteShader);
	spriteProg->Apply();
	glBindVertexArray(0);
	Texture* tex = g_MaterialBank.GetTexture(m_FrameBuffer.GetTexture());
	float sizeH;
	sizeH = tex->GetHeight() / tex->GetWidth();
	tex->Apply(spriteProg->FetchUniform("g_Texture"), 0);
	spriteProg->SetUniformVec4("g_Color", glm::vec4(1));
	spriteProg->SetUniformVec4("g_Pos", glm::vec4(0.0f, 0.5f + sizeH * 0.5f, 0.0f,0.0f));
	spriteProg->SetUniformVec4("g_Size", glm::vec4(1.0f, sizeH, 1.0f, 1.0f));
	if (tex->GetChannels() == 1){
		spriteProg->SetUniformBool("g_GreyScale", true);
	}
	else{
		spriteProg->SetUniformBool("g_GreyScale", false);
	}
	glDrawArrays(GL_POINTS, 0, 1);

}
void TerrainDeformationProgram::Render(RenderQueue* rq){
	ShaderProgram* prog = g_ShaderBank.GetProgramFromHandle(m_DrawProgram);
	prog->Apply();
	
	glBindVertexArray(0);
	glEnable(GL_BLEND);
	glDisable(GL_DEPTH_TEST);
	glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
	//glBlendFunc(GL_SRC_ALPHA, GL_ONE); //additive
	for (auto& td : rq->GetDeformations()){
		Terrain* terrain = g_TerrainManager.GetTerrainFromHandle(td.Terrain);
		glm::vec2 textureSize = terrain->GetHeightHighResSize();

		m_FrameBuffer->SetTexture(terrain->GetHeightMap(), textureSize.x, textureSize.y);
		m_FrameBuffer->Apply();
		glViewport(0, 0, textureSize.x, textureSize.y);
		//prog->SetUniformTextureHandle("g_Texture", m_BrushTexture, 0);
		m_BrushTex->Apply(prog->FetchUniform("g_Texture"), 0);
		prog->SetUniformVec4("g_Pos", glm::vec4(td.Position.x , 1.0f - td.Position.y, 0, 0));
		prog->SetUniformVec4("g_Size", glm::vec4(td.Size, 1, 1));
		glm::vec3 dir = td.Up ? glm::vec3(1) : glm::vec3(0);
		prog->SetUniformVec4("g_Color", glm::vec4(dir, td.Strength));
		prog->SetUniformBool("g_GreyScale", false);
		glDrawArrays(GL_POINTS, 0, 1);
		//filter area
		glm::vec4 pixelArea = glm::vec4((td.Position * textureSize) - glm::vec2(-2), (td.Size * textureSize) + glm::vec2(4));
		const int WORK_GROUP_SIZE = 32;
		GLuint WorkGroupSizeX = GLuint((pixelArea.z + WORK_GROUP_SIZE - 1) / float(WORK_GROUP_SIZE));
		GLuint WorkGroupSizeY = GLuint((pixelArea.w + WORK_GROUP_SIZE - 1) / float(WORK_GROUP_SIZE));

		ShaderProgram* prog = g_ShaderBank.GetProgramFromHandle(m_FilterProgram);
		prog->Apply();
		prog->SetUniformVec4("g_PixelArea", pixelArea);
		glBindImageTexture(0, terrain->GetHeightMap(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F);
		glDispatchCompute(WorkGroupSizeX, WorkGroupSizeY, 1);
	}
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glDisable(GL_BLEND);
}
void gfx::BasicRenderProgram::Draw(DrawData* data)
{
	//draw
	int flag = data->ShaderFlags;
	g_ModelBank.ApplyBuffers( POS_NORMAL_TEX_TANGENT );
	ShaderProgram* prog = g_ShaderBank.GetProgramFromHandle(m_ProgramHandle);
	
	prog->Apply();
	m_RenderJobManager->BindBuffers(prog);
	GLint loc = -1;
	if(flag & FRAGMENT_DIFFUSEMAP)
	{
		Texture* diffuse = g_MaterialBank.GetTextureAtlasTex(Texture_Atlas_Type::Diffuse);
		loc = prog->FetchUniform("g_DiffuseTex");
		diffuse->Apply(loc, 0);
		prog->SetUniformBool("useDiffuse",true);
	}
	else
	{
		prog->SetUniformBool("useDiffuse",false);
	}
	if(flag & FRAGMENT_NORMALMAP)
	{
		Texture* normal = g_MaterialBank.GetTextureAtlasTex(Texture_Atlas_Type::Normal);
		loc = prog->FetchUniform("g_NormalTex");
		normal->Apply(loc, 1);
		prog->SetUniformBool("useNormal",true);
	}
	else
	{
		prog->SetUniformBool("useNormal",false);
	}
	if(flag & FRAGMENT_ROUGHNESSMAP)
	{
		Texture* roughness = g_MaterialBank.GetTextureAtlasTex(Texture_Atlas_Type::Roughness);
		loc = prog->FetchUniform("g_RoughnessTex");
		roughness->Apply(loc, 2);
		prog->SetUniformBool("useRoughness",true);
	}
	else
	{
		prog->SetUniformBool("useRoughness",false);
	}
	if(flag & FRAGMENT_METALMAP)
	{
		Texture* metal = g_MaterialBank.GetTextureAtlasTex(Texture_Atlas_Type::Metal);
		loc = prog->FetchUniform("g_MetalTex");
		metal->Apply(loc, 3);
		prog->SetUniformBool("useMetal",true);
	}
	else
	{
		prog->SetUniformBool("useMetal",false);
	}
	BasicData* frameData = (BasicData*)(data->ExtraData);
	prog->SetUniformInt("numPLights", frameData->PointLightCount);
	prog->SetUniformInt("numDLights", frameData->DirLightCount);
	prog->SetUniformUInt("BatchCounts",frameData->BatchOffset);
	prog->SetUniformVec2("g_WorldSize", frameData->WorldSize);
	prog->SetUniformMat4("ShadowMat",frameData->ShadowMat);
	loc = prog->FetchUniform("g_LightCubeTex");
	frameData->SkyTex->Apply(loc, 4);
	loc = prog->FetchUniform("g_IrradianceCube");
	frameData->IrradianceTex->Apply(loc, 5);

	//fog tex
	loc = prog->FetchUniform("g_FogOfWarTex");
	glUniform1i(loc,6);
	glActiveTexture(GL_TEXTURE6);
	glBindTexture(GL_TEXTURE_2D, frameData->FogTex);

	//fog tex
	loc = prog->FetchUniform("g_ShadowMap");
	glUniform1i(loc,7);
	glActiveTexture(GL_TEXTURE7);
	glBindTexture(GL_TEXTURE_2D, frameData->ShadowTex);

	
	if (m_HasDrawID)
	{
		GPU_PROFILE( AutoGPUProfiler gpMultiDraw( "BasicRenderProgramMultiDrawElementsIndirect" ); );
		glMultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, (GLvoid*)(sizeof(IndirectDrawCall) * (frameData->BatchOffset)), frameData->BatchCount, 0);
		GPU_PROFILE( gpMultiDraw.Stop(); );