示例#1
0
文件: main.cpp 项目: meta-inf/raytr
void PT (const json &j)
{
	PTParams params;
	params.LoadFrom(j.at("pt_params"));

	omp_set_num_threads(params.n_threads);
	Scene scene = Scene::Load(j);
	Image image(scene.Width() * 2, scene.Height(), "tmp/im_last", params.display);
	PathTracer pathTracer(scene, image, params);

	Ray ray;
	rand_data rd;
	rand_init(&rd);
	scene.GetCamera()->Project(&rd, 818, 1800 - 1043, ray);
	pathTracer.Trace(&rd, ray);

	pathTracer.PT();
	image.Show();
	image.Dump(-1);
	image.Show();
}
示例#2
0
void Renderer::RenderScene(Scene& scene)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	Camera &cam = scene.GetCamera();

	// view matrix
	Mat4 view_matrix = glm::lookAt(cam.position, cam.point, cam.up);

	// gather info
	std::vector<DirectionalLightComponent*> directional_lights;
	std::vector<PointLightComponent*> point_lights;
	std::map<Shader*, std::vector<VisibleComponent*>> visible_by_shader;

	for (auto x : scene.GetVisibleComponents()) {
		Shader *shader = x->GetShader();

		if (shader != nullptr) {
			auto it = visible_by_shader.find(x->GetShader());

			if (it == visible_by_shader.end())
				it = visible_by_shader.emplace(std::piecewise_construct, std::forward_as_tuple(shader), std::forward_as_tuple()).first;

			it->second.push_back(x);
		}
	}

	for (auto x : scene.GetLightComponents()) {
		switch (x->GetType()) {
			case LightComponentType::Directional:
				directional_lights.push_back(static_cast<DirectionalLightComponent*>(x));
				break;

			case LightComponentType::Point:
				point_lights.push_back(static_cast<PointLightComponent*>(x));
				break;

			default:
				break;
		}
	}

	// render
	for (auto it = visible_by_shader.begin(); it != visible_by_shader.end(); ++it) {
		Shader *shader = it->first;

		shader->Enable();
		shader->SetupUniforms(view_matrix, projection_, ambient_, directional_lights, point_lights);

		for (auto x : it->second) {
			Mesh *mesh = x->GetMesh();

			if (mesh == nullptr)
				continue;

			glBindVertexArray(mesh->GetVAO());

			const MeshEntries &entries = mesh->GetEntries();
			int i = 0;

			const Mat4 *generic_model_matrix = x->GetModelMatrix(-1);
			Mat4 generic_normal_matrix = glm::inverse(view_matrix * *generic_model_matrix);
			Vec4 &blend = x->GetColor();

			for (auto it2 = entries.begin(); it2 != entries.end(); ++it2, ++i) {
				const Mat4 *model_matrix = x->GetModelMatrix(i);

				if (model_matrix != nullptr) {
					shader->SetupUniformsForMesh(blend, *model_matrix, glm::inverse(view_matrix * *model_matrix), *mesh, *it2, placeholder_tex_);
				} else {
					shader->SetupUniformsForMesh(blend, *generic_model_matrix, generic_normal_matrix, *mesh, *it2, placeholder_tex_);
				}

				glDrawElementsBaseVertex(GL_TRIANGLES, it2->vertex_count, GL_UNSIGNED_INT,
					reinterpret_cast<void*>(sizeof(uint) * it2->index_base), it2->vertex_base);
			}
		}
	}

	glBindVertexArray(0);
}
////////////////////////////////////////////////////////////////////////////////
// Render them shadowmapz
void cGraphicsEngine::cShadowRenderer::RenderShadowMaps(Scene& sceneManager) {
	//// set states
	tBlendDesc blend = blendDefault;
	blend[0].writeMask = 0;
	gApi->SetBlendState(blend);

	tDepthStencilDesc dsState = depthStencilDefault;
	dsState.depthCompare = eComparisonFunc::LESS;
	gApi->SetDepthStencilState(dsState, 0x00);

	// render lights
	auto& lights = sceneManager.GetLights();
	for (auto& v : lights) {
		// get light components
		auto& light = *v;
		
		auto* shadowMap = light.GetShadowMap();

		// check shadow map parameters
		//if (!light.enabled && light.shadowQuality != cGraphicsLight::eShadowQuality::DISABLED) {
		//	continue;
		//}
		//continue;
		//if (light.shadowResolution < 256) {
		//	light.shadowResolution = 256;
		//}
		
		// light type
		//switch (light.type) {
		//	case cGraphicsLight::DIRECTIONAL : {
				// set sh map type
				//shadowMap->SetType(light.type);
				//shadowMap->ClearUnneeded();
				// get map for type
				auto& shadowMapDir = *(cShadowMapDir*)shadowMap;
				// init map if not compatible with currently inited
				//auto resolution = 2048;//== light.shadowResolution;
				//auto nCascadeQuality = light.shadowQuality;
				//try {
				//	if (!shadowMapDir.IsValid(gApi, resolution, eFormat::R24_UNORM_X8_TYPELESS, eFormat::D24_UNORM_S8_UINT, nCascadeQuality)) {
				//		shadowMapDir.Init(gApi, resolution, eFormat::R24_UNORM_X8_TYPELESS, eFormat::D24_UNORM_S8_UINT, nCascadeQuality);
				//	}
				//}
				//catch (std::exception& e) {
				//	std::cerr << e.what() << std::endl;
				//	break; // breaks switch case label
				//}

				// generate cascade splits
				size_t nCascades = shadowMapDir.GetNumCascades();
				std::vector<float> cascadeSplits(nCascades + 1, 0.0f);

				float near = parent.camera->GetNearPlane();
				float far = parent.camera->GetFarPlane();

				for (size_t i = 0; i <= nCascades; i++) {
					cascadeSplits[i] = near*pow((far / near), float(i) / float(nCascades));
				}
				for (float& v : cascadeSplits) {
					v -= near;
					v /= (far - near);
				}

				auto win = parent.GetTargetWindow();
				float aspectRatio = (float)win->GetClientWidth() / win->GetClientHeight();

				// foreach cascade
				for (size_t i = 0; i < nCascades; i++) {
					// compute transforms
					auto& transform = shadowMapDir.GetTransforms()[i];
					bool isGoodTransform = shadowMapDir.Transform(
							transform.projMat,
							transform.viewMat,
							light.GetDirection(), 
							sceneManager.GetCamera()->GetViewMatrix(), sceneManager.GetCamera()->GetProjMatrix(aspectRatio),
							cascadeSplits[i], cascadeSplits[i+1]);
					if (!isGoodTransform)
						continue;

					// setup render
					gApi->SetShaderProgram(shaderDirectional);
					ITexture2D* textureSlice = shadowMapDir.GetTexture()->GetArraySlice(i);
					gApi->SetRenderTargets(0, nullptr, textureSlice);
					gApi->ClearTexture(textureSlice);

					
					// foreach inst grp
					for (auto& entity : sceneManager.GetEntities())
					{
						Mesh* mesh = (Mesh*)entity->GetMesh();
						Material& mtl = *(Material*)entity->GetMaterial();

						// set geom params
						for (int j = 0; j < mesh->GetNumVertexBuffers(); j++)
						{
							auto stream = mesh->GetVertexBuffers()[j];
							gApi->SetVertexBuffers(&stream.vb, &stream.stride, &stream.offset, j, 1);
						}

						gApi->SetIndexBuffer(mesh->GetIndexBuffer());

						mm::mat4 posMat = mm::create_translation(entity->GetPos());
						mm::mat4 rotMat = mm::mat4(entity->GetRot());
						mm::mat4 skewMat = mm::mat4(entity->GetSkew());

						mm::mat4 worldMat = posMat * (rotMat * skewMat);

						//auto win = parent.GetTargetWindow();
						//float aspectRatio = (float)win->GetClientWidth() / win->GetClientHeight();
						//mm::mat4 projMat = sceneManager.GetCamera()->GetProjMatrix(aspectRatio);
						//mm::mat4 viewMat = sceneManager.GetCamera()->GetViewMatrix();
						mm::mat4 viewProjMat = transform.projMat * transform.viewMat;// projMat * viewMat;
						//mm::mat4 invViewProjMat = mm::inverse(viewProjMat);
						//mm::mat4 invProjMat = mm::inverse(projMat);
						mm::mat4 worldViewProj = viewProjMat * worldMat;
						gApi->SetVSConstantBuffer(&worldViewProj, sizeof(worldViewProj), 0);

						for (auto& matGroup : mesh->GetMaterialIds())
						{
							gApi->DrawIndexed((matGroup.endFace - matGroup.beginFace) * 3, matGroup.beginFace * 3);
						}
						
					}

					//gGapi->SaveTextureToFile(textureSlice, ITexture2D::BMP, "teszt.bmp");
					textureSlice->Release();
				}
				//break;
			//}
			//case cGraphicsLight::SPOT:{
			//
			//	break;
			//}
			//case cGraphicsLight::POINT:{
			//
			//	break;
			//}
		//}
	}
}