当前位置: 首页>>代码示例>>C++>>正文


C++ Primitive::IsLight方法代码示例

本文整理汇总了C++中Primitive::IsLight方法的典型用法代码示例。如果您正苦于以下问题:C++ Primitive::IsLight方法的具体用法?C++ Primitive::IsLight怎么用?C++ Primitive::IsLight使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Primitive的用法示例。


在下文中一共展示了Primitive::IsLight方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Raytrace

// -----------------------------------------------------------
// Engine::Raytrace
// Naive ray tracing: Intersects the ray with every primitive
// in the scene to determine the closest intersection
// -----------------------------------------------------------
Primitive* Engine::Raytrace( Ray& a_Ray, Color& a_Acc, int a_Depth, float a_RIndex, float& a_Dist )
{
	if (a_Depth > TRACEDEPTH) return 0;
	// trace primary ray
	a_Dist = 1000000.0f;
	vector3 pi;
	Primitive* prim = 0;
	int result;
	// find the nearest intersection
	for ( int s = 0; s < m_Scene->GetNrPrimitives(); s++ )
	{
		Primitive* pr = m_Scene->GetPrimitive( s );
		int res;
		if (res = pr->Intersect( a_Ray, a_Dist )) 
		{
			prim = pr;
			result = res; // 0 = miss, 1 = hit, -1 = hit from inside primitive
		}
	}
	// no hit, terminate ray
	if (!prim) return 0;
	// handle intersection
	if (prim->IsLight())
	{
		// we hit a light, stop tracing
		a_Acc = Color( 1, 1, 1 );
	}
	else
	{
		// determine color at point of intersection
		pi = a_Ray.GetOrigin() + a_Ray.GetDirection() * a_Dist;
		// trace lights
		for ( int l = 0; l < m_Scene->GetNrPrimitives(); l++ )
		{
			Primitive* p = m_Scene->GetPrimitive( l );
			if (p->IsLight()) 
			{
				Primitive* light = p;
				// calculate diffuse shading
				vector3 L = ((Sphere*)light)->GetCentre() - pi;
				NORMALIZE( L );
				vector3 N = prim->GetNormal( pi );
				if (prim->GetMaterial()->GetDiffuse() > 0)
				{
					float dot = DOT( N, L );
					if (dot > 0)
					{
						float diff = dot * prim->GetMaterial()->GetDiffuse();
						// add diffuse component to ray color
						a_Acc += diff * prim->GetMaterial()->GetColor() * light->GetMaterial()->GetColor();
					}
				}
			}
		}
	}
	// return pointer to primitive hit by primary ray
	return prim;
}
开发者ID:patback66,项目名称:COEN148,代码行数:63,代码来源:raytracer.cpp

示例2: Raytrace

ci::ColorA Scene::Raytrace(const ci::Ray& inRay, int inDepth, float& inDist, const float inRefractIndex) {
	if (inDepth > 3)
		return ci::ColorA::black();

	Primitive* hitPrim = NULL;
	Primitive::E_INTERSECT_RESULT result;
	float currentInDist = inDist;
	for (int i=0; i<m_primitives.size(); i++) {
		Primitive* prim = m_primitives[i];
		Primitive::E_INTERSECT_RESULT res = prim->Intersect(inRay, inDist);
		if (res != Primitive::MISS) {
			if (inDist <= currentInDist) {
				hitPrim = prim;
				result = res;
			}
		}
	}

	if (!hitPrim)
		return ci::ColorA::black();

	if (hitPrim->IsLight()) 
		return hitPrim->GetMaterial().GetDiffuseColor();
	else {
		ci::Vec3f hitPoint = inRay.getOrigin() + inRay.getDirection() * inDist;
		ci::ColorA accumColor(0, 0, 0, 0);
		{//shading
			for (int i=0; i<m_primitives.size(); i++) {
				Primitive* prim = m_primitives[i];
				if (prim->IsLight()) {
					Primitive* light = prim;
					//shadow feeler
					float shade = 1.0f;
					//get shadow feeler ray in direction of light
					ci::Vec3f L = (((SpherePrimitive*)light)->getCenter() - hitPoint).normalized();
					float lightToHitPointDist = (((SpherePrimitive*)light)->getCenter() - hitPoint).length();
					ci::Ray shadowFeelerRay(hitPoint + L * ci::EPSILON_VALUE, L);
					float LDist = 1000.0f;
					for (int j = 0; j<m_primitives.size(); j++) {
						Primitive* primJ = m_primitives[j];
						if (primJ->IsLight())
							break;
						else {
							Primitive::E_INTERSECT_RESULT result = primJ->Intersect(shadowFeelerRay, LDist);
							if (result == Primitive::HIT && LDist <= lightToHitPointDist) {
								shade = 0.0f;
								break;
							}
						}
					}

					ci::Vec3f N = hitPrim->GetNormal(hitPoint);
					float dotLN = L.dot(N);
					ci::Vec3f R = (L - 2.0f * dotLN * N).normalized();
					ci::Vec3f V = inRay.getDirection().normalized();
					if (dotLN > 0) {
						//calculate diffuse component
						float diffuseC = dotLN * hitPrim->GetMaterial().GetDiffuseCoefficient();
						accumColor += diffuseC 
										* hitPrim->GetMaterial().GetDiffuseColor() 
										* light->GetMaterial().GetDiffuseColor()
										* shade;

						float dotVR = V.dot(R);
						if (dotVR > 0) {
							//calculate specular component
							float specularC = ci::math<float>::pow(dotVR, 20) * hitPrim->GetMaterial().GetSpecularCoefficient();
							accumColor += specularC 
											* hitPrim->GetMaterial().GetSpecularColor() 
											* light->GetMaterial().GetDiffuseColor() 
											* shade;
						}
					}
				}
			}
		}

		{//reflection
			if (hitPrim->GetMaterial().GetReflectionCoefficient() > 0.0f) {
				ci::Vec3f N = hitPrim->GetNormal(hitPoint);
				ci::Vec3f V = inRay.getDirection().normalized();
				float dotVN = V.dot(N);
				ci::Vec3f R = (V - 2.0f * dotVN * N).normalized();
				float reflectDist = 10000.0f;
				ci::ColorA reflectedColor = Raytrace(ci::Ray(hitPoint + R * ci::EPSILON_VALUE * 2, R), inDepth + 1, reflectDist, inRefractIndex);
				accumColor += reflectedColor * hitPrim->GetMaterial().GetDiffuseColor() * hitPrim->GetMaterial().GetReflectionCoefficient();
			}
		}

		{//refraction
			if (hitPrim->GetMaterial().GetRefractionCoefficient() > 0.0f) {
				float refractIndex = hitPrim->GetMaterial().GetRefractionCoefficient();
				float n = inRefractIndex / refractIndex;
				ci::Vec3f N = hitPrim->GetNormal(hitPoint) * (int)result;
				float cosI = N.dot(inRay.getDirection());
				//float I = acos(cosI);
				float cosT2 = 1.0f - n * n * (1.0f - cosI * cosI);
				//float sinT2 = n * n * (1.0f - cosI * cosI);
				if (cosT2 >= 0.0f) {
				//if (sinT2 <= 1.0f) {
//.........这里部分代码省略.........
开发者ID:akshayloke,项目名称:RayTracer,代码行数:101,代码来源:Scene.cpp


注:本文中的Primitive::IsLight方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。