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


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

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


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

示例1: rayTrace

vector rayTrace(Line *ray, Primitive **primitives, Light **lights, float r_idx) {
  Intersection *bestIntersection = NULL;
  vector retval = vectorCreate(0.0, 0.0, 0.0);
  
  for (int p = 0; p < 10; p++) {
    if (!primitives[p])
      break;
    Intersection *thisIntersection = primitiveIntersect(primitives[p], ray);
    if (!bestIntersection) {
      bestIntersection = thisIntersection;
      continue;
    }
    if (thisIntersection && thisIntersection->distance < bestIntersection->distance) {
      free(bestIntersection);
      bestIntersection = thisIntersection;
    }
  }
  
  if (bestIntersection) {
    Primitive *primitive = bestIntersection->primitive;
    Material *material = primitive->material;

    vector mcolor = vectorMultiply(material->color, 0.0);
    
    vector N = primitiveNormal(primitive, bestIntersection);
    vector V = vectorUnit(vectorSubtraction(ray->end, ray->start));
    
    for (int l = 0; l < 2; l++) {
      Light *light = lights[l];
      vector lvec = vectorSubtraction(light->location, bestIntersection->intersectionPoint);
      
      vector pcolor = material->color;
      vector lcolor = light->color;

      Intersection *linter = NULL;
      Line *lightRay = makeLine(bestIntersection->intersectionPoint, light->location);
      
      float shade = 0.0;
      
      for (int p = 0; p < 3; p++) {
        if (primitives[p] == primitive)
          continue;
        if ((linter = primitiveIntersect(primitives[p], lightRay)))
          shade += 1.0 - linter->primitive->material->transparency;
      }
      
      free(lightRay);
      
      if (shade < 1.0) {
        if (material->specular > 0.0) {
          float sintensity = 0.0;
          
          vector L = vectorUnit(lvec);          
          vector R = vectorSubtraction(L, vectorMultiply(N, 2 * vectorDotProduct(L,N)));
          
          float dot = vectorDotProduct(V, R);
          
          if (dot > 0.0) {
            sintensity = pow(dot, 20) * material->specular * light->intensity * (1.0 - shade);
          }
          
          if (sintensity > 0.0) {
            mcolor = vectorAddition(mcolor, vectorMultiply(lcolor, sintensity));
          }
        }

        if (material->diffuse > 0.0) {
          float dintensity = material->diffuse * vectorDotProduct(vectorUnit(lvec), primitive->normal(bestIntersection->primitive, bestIntersection)) * light->intensity * (1.0 - shade);

          if (dintensity > 0.0) {
            mcolor = vectorAddition(mcolor, vectorMultiply(vectorCProduct(pcolor, lcolor), dintensity));
          }
        }
      }
      
      free(linter);
    }
    
    if (material->reflection > 0.0) {
      vector R = vectorUnit(vectorSubtraction(V, vectorMultiply(N, 2 * vectorDotProduct(V,N))));
      Line *rline = makeLine(vectorAddition(bestIntersection->intersectionPoint, vectorMultiply(R, EPS)), vectorAddition(bestIntersection->intersectionPoint, vectorMultiply(R, 30)));

      vector rcolor = rayTrace(rline, primitives, lights, r_idx);

      mcolor = vectorAddition(vectorMultiply(mcolor, 1.0 - material->reflection), vectorMultiply(rcolor, material->reflection));

      free(rline);
    }
    
    if (material->transparency > 0) {
      float refraction = material->refraction;
      float n = r_idx / refraction;
      vector Nr = vectorMultiply(N, bestIntersection->direction);
      float cosI = - vectorDotProduct(Nr, V);
      float cosT2 = 1.0 - n * n * (1.0 - cosI * cosI);
      if (cosT2 > 0.0) {
        vector T = vectorAddition(vectorMultiply(V, n), vectorMultiply(Nr, n * cosI - sqrt(cosT2)));

        Line *rline = makeLine(vectorAddition(bestIntersection->intersectionPoint, vectorMultiply(T, EPS)), vectorAddition(bestIntersection->intersectionPoint, vectorMultiply(T, 30)));
        vector rfcol = rayTrace(rline, primitives, lights, r_idx);
//.........这里部分代码省略.........
开发者ID:wito,项目名称:raytrace,代码行数:101,代码来源:raytrace.c


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