void MeshRenderer::BindVertexArray(){

	glBindVertexArray(m_vaoID);

	m_mesh->BindVertexArray();

// 	glBindBuffer(GL_ARRAY_BUFFER, (GLuint)m_mesh->m_vboID);
// 
// 	if (m_mesh->m_numIndicesToDraw != 0) {
// 		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (GLuint)m_mesh->m_iboID);
// 	}

	GLuint materialProgramID = (GLuint)m_material->GetProgramID();

	if (theOGLRenderer){
		BindPosition(materialProgramID);
		BindColor(materialProgramID);
		BindTexCoords(materialProgramID);
		BindNormals(materialProgramID);

		//tangent and bi tangent for lighting
		theOGLRenderer->ProgramBindAttribute((GLuint)materialProgramID, "inTangent", 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), offsetof(Vertex3D, m_tangent));

		theOGLRenderer->ProgramBindAttribute((GLuint)materialProgramID, "inBiTangent", 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), offsetof(Vertex3D, m_biTangent));
	}
}
void SceneIntersection::Update()
{
	ssbo->ClearBuffer();

	computeShader->Use();

	auto rezolution = gameFBO->GetResolution();
	auto camera = Manager::GetScene()->GetActiveCamera();

	auto cameraPos = camera->transform->GetWorldPosition();
	auto planePos = cameraPos - camera->transform->GetLocalOZVector() * 3.0f;
	auto direction = -camera->transform->GetLocalOZVector();
	glUniform3f(plane_direction, direction.x, direction.y, direction.z);
	glUniform1f(sphere_size, sphereRadius);
	gameFBO->SendResolution(computeShader);
	camera->BindPosition(computeShader->loc_eye_pos);

	ssbo->BindBuffer(0);
	glBindImageTexture(0, visualization->GetTextureID(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R8UI);
	glBindImageTexture(1, gameFBO->GetTextureID(3), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F);
	glBindImageTexture(2, Manager::GetPicker()->FBO->GetTextureID(0), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F);
	glBindImageTexture(3, gameFBO->GetTextureID(0), 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
	OpenGL::DispatchCompute(rezolution.x, rezolution.y, 1, 32);
	ssbo->ReadBuffer();

	// Compute Virtual 3D position - average of 3D points intersected by the sphere/plane for an object
	auto size = ssbo->GetSize();
	auto values = ssbo->GetBuffer();

	// Used for computing world position from view position
	//auto invView = glm::inverse(camera->GetViewMatrix());
	centerPoints.clear();
	objects.clear();

	for (uint i = 0; i < size; i++) {
		if (values[i].w) {
			glm::vec4 averageViewSpacePos = glm::vec4(values[i]) / (float(1000 * values[i].w));

			// Compute world position from view position
			// the 4th component needs to be set to 1 otherwise the mutiplication will yield a wrong answer
			//averageViewSpacePos.w = 1;
			//glm::vec4 worldPos = invView * averageViewSpacePos;
			//worldPos.w = float(i);

			averageViewSpacePos.w = float(i);
			centerPoints.push_back(averageViewSpacePos);
		}
	}

	// ------------------------------------------------------------------------
	// Get Objects and asign virtual camera space center position

	if (centerPoints.size()) {
		objects.reserve(centerPoints.size());

		for (auto const &pos : centerPoints)
		{
			auto obj = Manager::GetColor()->GetObjectByID((unsigned int)pos.w);
			auto source = dynamic_cast<CSound3DSource*>(obj);
			if (source)
			{
				source->SetVirtualCameraSpacePosition(glm::vec3(pos));
				objects.push_back(source);
			}
		}
	}

	for (auto callback : callbacks) {
		callback(objects);
	}
}