void Renderer::RenderScene(){
	glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

	//First and second pass
	DrawShadowScene();
	DrawCombinedScene();
	SwapBuffers();
}
//int frame_idx = 0;
void Scene::RenderScene()
{
	//Check to see if the window has been resized
	if (m_ScreenTexWidth != width || m_ScreenTexHeight != height)
	{
		BuildScreenFBO();
	}


	//Reset all varying data
	textureMatrix.ToIdentity();
	modelMatrix.ToIdentity();
	viewMatrix = m_Camera->BuildViewMatrix();
	projMatrix = Matrix4::Perspective(0.01f, 1000.0f, (float)width / (float)height, 45.0f);
	m_FrameFrustum.FromMatrix(projMatrix * viewMatrix);



	//Update all Object's World Transform
	UpdateWorldMatrices(m_RootGameObject, Matrix4());

	NCLDebug::SetDebugDrawData(projMatrix * viewMatrix, m_Camera->GetPosition());

	

	//Setup Default Shader Uniforms
	Vector3 camPos = m_Camera->GetPosition();
	//Vector3 lightPos = m_InvLightDirection * -100.0f;
	
	Vector3 lightPos = m_InvLightDirection * -100.0f;

	Vector4 lightPosEyeSpace = viewMatrix * Vector4(lightPos.x, lightPos.y, lightPos.z, 1.0f);

	//SetCurrentShader(m_DefaultLightShader);
	//UpdateShaderMatrices();
	//glUniform1i(glGetUniformLocation(currentShader->GetProgram(), "diffuseTex"), 0);
	//glUniform3fv(glGetUniformLocation(currentShader->GetProgram(), "ambientColour"), 1, &m_AmbientColour.x);
	//glUniform3fv(glGetUniformLocation(currentShader->GetProgram(), "invLightDir"), 1, &m_InvLightDirection.x);
	//glUniform3fv(glGetUniformLocation(currentShader->GetProgram(), "cameraPos"), 1, &camPos.x);
	//glUniform1f(glGetUniformLocation(currentShader->GetProgram(), "specularIntensity"), m_SpecularIntensity);

	//SetCurrentShader(m_DefaultShadowShader);
	//UpdateShaderMatrices();
	//glUniform1i(glGetUniformLocation(currentShader->GetProgram(), "diffuseTex"), 0);
	//glUniform3fv(glGetUniformLocation(currentShader->GetProgram(), "ambientColour"), 1, &m_AmbientColour.x);

	//SetCurrentShader(m_ShadowVolumeShader);
	//UpdateShaderMatrices();
	//glUniform3fv(glGetUniformLocation(currentShader->GetProgram(), "invLightDir"), 1, &m_InvLightDirection.x);


	////Setup Render FBO/OpenGL States
	//glBindFramebuffer(GL_FRAMEBUFFER, m_ScreenFBO);
	//glStencilFunc(GL_ALWAYS, 0, 0xFF);
	//glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

	//glClearColor(clearcolor.x, clearcolor.y, clearcolor.z, clearcolor.w);
	glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

	/////////////////////////////////////////////////
	glDisable(GL_CULL_FACE);
	glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
	//glDepthFunc(GL_ALWAYS);
	glDepthMask(GL_FALSE);//disable the depth buffer, it won't write on depth buffer
	SetCurrentShader(m_skyboxShader);//set skyboxShader to be currentShader


	glUniform1i(glGetUniformLocation(currentShader->GetProgram(), "cubeTex"), 0);

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_CUBE_MAP, cubeMap);

	UpdateShaderMatrices(); //update "modelMatrix" "viewMatrix" "projMatrix" "textureMatrix"


	//SetTextureRepeating(quad->GetTexture(), true);
	quad->Draw(false);

	glUseProgram(0);
	glDepthMask(GL_TRUE);//enable depth buffer

	//glDepthFunc(GL_LEQUAL);
	/////////////////////////////////////////////////

	//glEnable(GL_MULTISAMPLE);
	glEnable(GL_DEPTH_TEST);
	//glEnable(GL_DEPTH_CLAMP);
	//glEnable(GL_STENCIL_TEST);
	glEnable(GL_CULL_FACE);
	//glEnable(GL_BLEND);
	//glDepthFunc(GL_LEQUAL);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	//Build Render List
	BuildNodeLists(m_RootGameObject);
	SortNodeLists();

	//Render the Scene in the Dark (As if it were a shadow)
	//SetCurrentShader(m_DefaultShadowShader);
	//DrawNodes(false);

	//Render the Shadow Volumes to the Stencil Buffer
	//SetCurrentShader(m_ShadowVolumeShader);
	//glDepthFunc(GL_LESS);
	//glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
	//glDepthMask(GL_FALSE);

	//glStencilFunc(GL_ALWAYS, 0, 0xFF);
	//glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_INCR_WRAP, GL_KEEP);
	//glStencilOpSeparate(GL_BACK, GL_KEEP, GL_DECR_WRAP, GL_KEEP);

	//glDisable(GL_CULL_FACE);
	//DrawNodes(false);
	//glEnable(GL_CULL_FACE);
	//

	////Finally Render the Light Sections of the scene where the shadow volumes overlapped
	//glDepthFunc(GL_LEQUAL);
	//glStencilFunc(GL_EQUAL, 0, 0xFF);
	//glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
	//glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
	//glDepthMask(GL_TRUE);

	//SetCurrentShader(m_DefaultLightShader);
	//DrawNodes(true);
	viewMatrix = m_Camera->BuildViewMatrix();
	projMatrix = Matrix4::Perspective(1.0f, 1000.0f, (float)width / (float)height, 45.0f);
	DrawShadowScene();
	DrawCominedScene();
	//Clear Render List
	ClearNodeLists();


	//Finally draw all debug data to FBO (this fbo has anti-aliasing and the screen buffer does not)
	glDisable(GL_DEPTH_TEST);
	glStencilFunc(GL_ALWAYS, 0, 0xFF);
	PhysicsEngine::Instance()->DebugRender();

	NCLDebug::SortDebugLists();
	NCLDebug::DrawDebugLists();
	NCLDebug::ClearDebugLists();

	/////////////////////////////////////////////////
	//PARTICLE
	/////////////////////////////////////////////////
	//glClearColor(0, 0, 0, 1);
	//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_DEPTH_TEST);
	glDepthMask(GL_FALSE);
	SetCurrentShader(m_ParticleShader);
	glUniform1i(glGetUniformLocation(currentShader->GetProgram(), "diffuseTex"), 0);

	DrawParticleList(m_RootParticleList);

	glDepthMask(GL_TRUE);
	/*glUniform1f(glGetUniformLocation(currentShader->GetProgram(), "particleSize"), emitter->GetParticleSize());
	emitter->SetParticleSize(0.5f);
	emitter->SetParticleVariance(1.0f);
	emitter->SetLaunchParticles(16.0f);
	emitter->SetParticleLifetime(100.0f);
	emitter->SetParticleSpeed(0.1f);
	emitter->SetSourcePosition(Vector3(0, 0, 0));
	UpdateShaderMatrices();

	emitter->Draw();*/


	//-------------------------------------------------------------
	//SetCurrentShader(m_ParticleShader);
	//glUniform1i(glGetUniformLocation(currentShader->GetProgram(), "diffuseTex"), 0);



	/*glUniform1f(glGetUniformLocation(currentShader->GetProgram(), "particleSize"), emitter1->GetParticleSize());
	emitter1->SetParticleSize(5.0f);
	emitter1->SetParticleVariance(1.0f);
	emitter1->SetLaunchParticles(16.0f);
	emitter1->SetParticleLifetime(100.0f);
	emitter1->SetParticleSpeed(0.1f);
	emitter1->SetSourcePosition(Vector3(10, 10, 10));
	UpdateShaderMatrices();

	emitter1->Draw();*/


	/////////////////////////////////////////////////
	//glDepthMask(GL_TRUE);



	////Present our Screen
	//glBindFramebuffer(GL_FRAMEBUFFER, 0);
	//PresentScreenFBO();

	////Swap Buffers and get ready to repeat the process
	//glUseProgram(0);
	SwapBuffers();
}