Esempio n. 1
0
void Renderer_Update(Renderer* renderer) {

//    memcpy(&renderer->scene->player.posRobot, &renderer->scene->lampe[0].pos, sizeof(Vec3));
    Player_update(&renderer->scene->player);
    updateShadowMatrix(renderer);
//    memcpy(&renderer->scene->player.mondeToCam, &renderer->depth_mondeToCam, sizeof(float)*16);
//    memcpy(&renderer->camToClip, &renderer->depth_camToProj, sizeof(float)*16);

    ShaderLibrary_Refresh();

}
	void GLWidgetSimpleShadow::makeShadowMap(GLWidget3D* glWidget3D){
	
		glDisable(GL_TEXTURE_2D);

		int origWidth=glWidget3D->width();
		int origHeigh=glWidget3D->height();
		

		// update camera fustrum (for overview)
		//updateSplitDist(glWidget3D->getCam()->f, 1.0f, FAR_DIST);//FAR_DIST/6 to display
		
		// generate shadow map using drawScene(1)
		glBindFramebuffer(GL_FRAMEBUFFER,FBO);
		glViewport(0,0,shadowWidth,shadowHeight);
		//glEnable(GL_CULL_FACE);
		//glCullFace(GL_FRONT);
		//glPolygonOffset( 1.0f, 4096.0f);
		glPolygonOffset( 1.1f, 4096.0f);
		glEnable(GL_POLYGON_OFFSET_FILL);
		// draw all faces since our terrain is not closed.
		//glDisable(GL_CULL_FACE);
		for(int i=0; i<cur_num_splits; i++){//cur_num_splits
			//clientMain->mGLWidget_3D->myCam->updateFrustumPoints(clientMain->mGLWidget_3D->myCam->f[i], clientMain->mGLWidget_3D->myCam->cam_pos, clientMain->mGLWidget_3D->myCam->cam_view);

			updateShadowMatrix(glWidget3D);
		
			glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, shadowMap[i], 0);
			//glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadowMap[i], 0);
			glDrawBuffer(GL_NONE); // No color buffer is drawn to.
			CheckFramebufferStatus();
			glClear(GL_DEPTH_BUFFER_BIT);
			float light_mvpMatrixArray[16];
			for(int j=0;j<16;j++){
				light_mvpMatrixArray[j]=light_mvpMatrix.data()[j];
			}
			glUniformMatrix4fv(light_mvpMatrixLoc,1,GL_FALSE,light_mvpMatrixArray);
			//RENDER
			glWidget3D->drawScene(1);//1 light mode1
			
			// save camera for later
			shad_cpm[i]=light_mvpMatrix;
		}

		glBindFramebuffer(GL_FRAMEBUFFER,0);
		// revert to normal back face culling as used for rendering
		glEnable(GL_CULL_FACE);
		glCullFace(GL_BACK);
		glDisable(GL_POLYGON_OFFSET_FILL);
		glViewport(0,0,origWidth,origHeigh);

	}//
	void GLWidgetSimpleShadow::initShadow(int _programId,GLWidget3D* glWidget3D){
		printf("---------- >> INIT SHADOW\n");
		
		programId=_programId;

		// PROJECTION MATRIX
		float fov=45.0f; // Think if bigger
		float aspect=(float)shadowWidth/(float)shadowHeight;
		
		float zfar=3300.0f;// 
		float znear=1200.0f;// FIX

		float f = 1.0f / tan (fov * (M_PI / 360.0));
		double m[16]=
		{	f/aspect,	0,								0,									0,
			0,			f,								0,						 			0,
			0,			0,		(zfar+znear)/(znear-zfar),		(2.0f*zfar*znear)/(znear-zfar),
			0,			0,								-1,									0
		};

		

		light_pMatrix=QMatrix4x4(m);
		// BIAS MATRIX
		light_biasMatrix.setToIdentity();
		light_biasMatrix.scale(0.5f);
		light_biasMatrix.translate(0.5f,0.5f,0.5f);
		// UNIFORMS LOC
		light_mvpMatrixLoc= glGetUniformLocation(programId, "light_mvpMatrix");
		light_biasMatrixLoc= glGetUniformLocation(programId, "light_biasMatrix");
		light_dirLoc= glGetUniformLocation(programId, "lightDir");
		printf("LOC lights %d %d %d\n",light_mvpMatrixLoc,light_biasMatrixLoc,light_dirLoc);
		
		// UPDATE MATRIX
		//QVector3D light_position=glWidget3D->getCamera()->light_dir.toVector3D();
		light_mvMatrix.setToIdentity();
		
		// FIX

		light_mvMatrix.lookAt(QVector3D(1365.0,-1200.0f,1245.0),
			QVector3D(-0.60f,0.55,-0.6),
			QVector3D(0.0f, 0.0f, 1.0f));
		light_mvpMatrix=light_pMatrix*light_mvMatrix;
		
		updateShadowMatrix(glWidget3D);
		
		// INIT shadowMap
		// FBO
		glGenFramebuffers(1,&FBO);
		glBindFramebuffer(GL_FRAMEBUFFER,FBO);
		
		////////////////////
		glGenTextures(MAX_SPLITS, &shadowMap[0]);
	

			glActiveTexture(GL_TEXTURE6);
			glBindTexture(GL_TEXTURE_2D,shadowMap[0]);

			glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT32, shadowWidth, shadowHeight, 0,GL_DEPTH_COMPONENT, GL_FLOAT, 0);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		

		glActiveTexture(GL_TEXTURE0);
		
		////
		VBOUtil::check_gl_error("Init ShadowMap");
		glUniform1i(glGetUniformLocation(programId,"shadowMap"), 6);//shadowMap in tex1
		glBindFramebuffer(GL_FRAMEBUFFER,0);
		printf("---------- << INIT SHADOW\n");
	}//
Esempio n. 4
0
	void RenderCameraSystem::mainUpdate(float time)
	{
		SCOPE_profile_cpu_function("Camera system");
		_scene->getBfcLinkTracker()->reset();

		// check if the render thread does not already have stuff to draw
		if (GetMainThread()->isRenderFrame() == false)
		{
			return;
		}

		std::list<std::shared_ptr<DRBSpotLightDrawableList>> spotLightList;
		std::list<std::shared_ptr<DRBData>> pointLightList;


		for (auto &spotEntity : _spotLights.getCollection())
		{
			auto spot = spotEntity->getComponent<SpotLightComponent>();
			auto spotDrawableList = std::make_shared<DRBSpotLightDrawableList>();
			spotDrawableList->spotLight = spot->getCullableHandle().getPtr()->getDatas();

			auto spotData = std::static_pointer_cast<DRBSpotLightData>(spotDrawableList->spotLight);

			glm::mat4 spotViewProj = spot->updateShadowMatrix();

			Frustum spotlightFrustum;
			spotlightFrustum.setMatrix(spotViewProj);

			// Draw spotlight frustum debug:
			glm::vec4 worldPos = glm::inverse(spotViewProj) * glm::vec4(-1, -1, -1, 1.0f);
			glm::vec3 aNear = glm::vec3(worldPos / worldPos.w);
			worldPos = glm::inverse(spotViewProj) * glm::vec4(-1, 1, -1, 1.0f);
			glm::vec3 bNear = glm::vec3(worldPos / worldPos.w);
			worldPos = glm::inverse(spotViewProj) * glm::vec4(1, 1, -1, 1.0f);
			glm::vec3 cNear = glm::vec3(worldPos / worldPos.w);
			worldPos = glm::inverse(spotViewProj) * glm::vec4(1, -1, -1, 1.0f);
			glm::vec3 dNear = glm::vec3(worldPos / worldPos.w);
			worldPos = glm::inverse(spotViewProj) * glm::vec4(-1, -1, 1, 1.0f);
			glm::vec3 aFar = glm::vec3(worldPos / worldPos.w);
			worldPos = glm::inverse(spotViewProj) * glm::vec4(-1, 1, 1, 1.0f);
			glm::vec3 bFar = glm::vec3(worldPos / worldPos.w);
			worldPos = glm::inverse(spotViewProj) * glm::vec4(1, 1, 1, 1.0f);
			glm::vec3 cFar = glm::vec3(worldPos / worldPos.w);
			worldPos = glm::inverse(spotViewProj) * glm::vec4(1, -1, 1, 1.0f);
			glm::vec3 dFar = glm::vec3(worldPos / worldPos.w);

			glm::vec3 color = glm::vec3(1, 0, 0);
			bool activateDepth = true;

			if (_drawDebugLines)
			{
				AGE::GetRenderThread()->getQueue()->emplaceTask<Commands::ToRender::Draw3DQuad>(aNear, bNear, cNear, dNear, color, activateDepth);
				AGE::GetRenderThread()->getQueue()->emplaceTask<Commands::ToRender::Draw3DQuad>(aFar, bFar, cFar, dFar, color, activateDepth);
				AGE::GetRenderThread()->getQueue()->emplaceTask<Commands::ToRender::Draw3DLine>(aNear, aFar, color, activateDepth);
				AGE::GetRenderThread()->getQueue()->emplaceTask<Commands::ToRender::Draw3DLine>(bNear, bFar, color, activateDepth);
				AGE::GetRenderThread()->getQueue()->emplaceTask<Commands::ToRender::Draw3DLine>(cNear, cFar, color, activateDepth);
				AGE::GetRenderThread()->getQueue()->emplaceTask<Commands::ToRender::Draw3DLine>(dNear, dFar, color, activateDepth);
			}

			std::atomic_size_t counter = 0;

			LFList<BFCItem> meshInLightList;
			std::size_t meshBlocksToCullNumber = _scene->getBfcBlockManagerFactory()->getBlockNumberToCull(BFCCullableType::CullableMesh);

			for (std::size_t i = 0; i < meshBlocksToCullNumber; ++i)
			{
				BFCBlockManagerFactory *bf = _scene->getBfcBlockManagerFactory();
				EmplaceTask<Tasks::Basic::VoidFunction>([bf, i, &spotlightFrustum, &counter, &meshInLightList](){
					bf->cullOnBlock(BFCCullableType::CullableMesh, meshInLightList, spotlightFrustum, i);
					counter.fetch_add(1);
				});
			}

			{
				SCOPE_profile_cpu_i("Camera system", "Cull for spots");
				while (counter < meshBlocksToCullNumber )
				{
				}
				//GetMainThread()->computeTasksWhile(std::function<bool()>([&counter, meshBlocksToCullNumber]() {
				//	return counter >= meshBlocksToCullNumber;
				//}));
			}

			while (meshInLightList.getSize() > 0)
			{
				spotDrawableList->meshs.push_back(meshInLightList.pop()->getDrawable()->getDatas());
			}

			spotLightList.push_back(spotDrawableList);
		}
		for (auto &pointLightEntity : _pointLights.getCollection())
		{
			auto point = pointLightEntity->getComponent<PointLightComponent>();
			
			pointLightList.push_back(point->getCullableHandle().getPtr()->getDatas());
		}

		for (auto &cameraEntity : _cameras.getCollection())
		{
			Frustum cameraFrustum;
			auto camera = cameraEntity->getComponent<CameraComponent>();

			std::atomic_size_t counter = 0;

			auto cameraList = std::make_shared<DRBCameraDrawableList>();
			cameraList->cameraInfos.data = camera->getData();
			cameraList->cameraInfos.view = glm::inverse(cameraEntity->getLink().getGlobalTransform());

			cameraFrustum.setMatrix(camera->getProjection() * cameraList->cameraInfos.view);

			LFList<BFCItem> meshList;
			LFList<BFCItem> pointLightListToCull;
			std::size_t meshBlocksToCullNumber = _scene->getBfcBlockManagerFactory()->getBlockNumberToCull(BFCCullableType::CullableMesh);
			std::size_t pointLightBlocksToCullNumber = _scene->getBfcBlockManagerFactory()->getBlockNumberToCull(BFCCullableType::CullablePointLight);
			std::size_t totalToCullNumber = meshBlocksToCullNumber + pointLightBlocksToCullNumber;
			
			for (std::size_t i = 0; i < meshBlocksToCullNumber; ++i)
			{
				BFCBlockManagerFactory *bf = _scene->getBfcBlockManagerFactory();
				EmplaceTask<Tasks::Basic::VoidFunction>([bf, i, &cameraFrustum, &counter, &meshList](){
					bf->cullOnBlock(BFCCullableType::CullableMesh, meshList, cameraFrustum, i);
					counter.fetch_add(1);
				});
			}

			for (std::size_t i = 0; i < pointLightBlocksToCullNumber; ++i)
			{
				BFCBlockManagerFactory *bf = _scene->getBfcBlockManagerFactory();
				EmplaceTask<Tasks::Basic::VoidFunction>([bf, i, &cameraFrustum, &counter, &pointLightListToCull](){
					bf->cullOnBlock(BFCCullableType::CullablePointLight, pointLightListToCull, cameraFrustum, i);
					counter.fetch_add(1);
				});
			}

			{
				SCOPE_profile_cpu_i("Camera system", "Cull for cam");
				while (counter < totalToCullNumber)
				{ }
				//GetMainThread()->computeTasksWhile(std::function<bool()>([&counter, totalToCullNumber]() {
				//	return counter >= totalToCullNumber;
				//}));
			}

			{
				SCOPE_profile_cpu_i("Camera system", "Copy LFList to std");
				while (meshList.getSize() > 0)
				{
					cameraList->meshs.push_back(meshList.pop()->getDrawable()->getDatas());
				}

				while (pointLightListToCull.getSize() > 0)
				{
					cameraList->pointLights.push_back(pointLightListToCull.pop()->getDrawable()->getDatas());
				}
			}
			if (OcclusionConfig::g_Occlusion_is_enabled)
			{
				occlusionCulling(cameraList->meshs, _drawDebugLines);
			}

			cameraList->spotLights = spotLightList;
			cameraList->pointLights = pointLightList;
			AGE::GetRenderThread()->getQueue()->emplaceTask<AGE::DRBCameraDrawableListCommand>(cameraList);
		}
	}