fork download
  1.  
  2. // 視点
  3. PVector eyePos = new PVector(0, 0, -5);
  4.  
  5. // 物体
  6. ArrayList<Shape> shapes = new ArrayList<>() {{
  7. add(new Sphere(new PVector( 3, 0, 25), 1));
  8. add(new Sphere(new PVector( 2, 0, 20), 1));
  9. add(new Sphere(new PVector( 1, 0, 15), 1));
  10. add(new Sphere(new PVector( 0, 0, 10), 1));
  11. add(new Sphere(new PVector(-1, 0, 5), 1));
  12. add(new Plane(new PVector(0, 1, 0), new PVector(0, -1, 0)));
  13. }};
  14.  
  15. // 光源
  16. PointLightSource lightSource = new PointLightSource() {{
  17. position = new PVector(-5,5,-5);
  18. }};
  19.  
  20. // 環境光強度
  21. FColor ambientIntensity = new FColor(0.10, 0.10, 0.10);
  22.  
  23. // 環境光反射係数
  24. FColor kAmb = new FColor(0.01, 0.01, 0.01);
  25. // 拡散反射係数
  26. FColor kDif = new FColor(0.69, 0.00, 0.00);
  27. // 鏡面反射係数
  28. FColor kSpe = new FColor(0.30, 0.30, 0.30);
  29. // 光沢度
  30. float shininess = 8;
  31.  
  32. void setup()
  33. {
  34. size(512, 512);
  35. background(color(0, 0, 0));
  36.  
  37. noSmooth();
  38. noLoop();
  39. }// void setup()
  40.  
  41. void draw()
  42. {
  43. PVector pw = new PVector();
  44. pw.z = 0;
  45.  
  46. for (int y = 0; y < height; ++y)
  47. {
  48. pw.y = map(y, 0, height-1, 1, -1);
  49. for (int x = 0; x < width; ++x)
  50. {
  51. pw.x = map(x, 0, width-1, -1, 1);
  52.  
  53. // レイ
  54. Ray eyeRay = new Ray(eyePos, PVector.sub(pw, eyePos));
  55.  
  56. // レイ上にある最近傍の物体を調べる
  57. Shape nearestShape = null;
  58. IntersectionPoint nearestIp = null;
  59.  
  60. for (Shape sh : shapes)
  61. {
  62. // レイと物体との交点を計算する
  63. IntersectionPoint ip = sh.testIntersection(eyeRay);
  64.  
  65. // レイ上に物体がない場合
  66. if (ip == null)
  67. {
  68. // 次の物体へ
  69. continue;
  70. }
  71.  
  72. // 初めて発見された物体である場合
  73. if (nearestShape == null)
  74. {
  75. // 最近傍の物体として更新
  76. nearestShape = sh;
  77. nearestIp = ip;
  78.  
  79. // 次の物体へ
  80. continue;
  81. }
  82.  
  83. // 最近傍の物体ではなかった場合
  84. if (ip.distance > nearestIp.distance)
  85. {
  86. // 次の物体へ
  87. continue;
  88. }
  89.  
  90. // 最近傍の物体として更新
  91. nearestShape = sh;
  92. nearestIp = ip;
  93.  
  94. }// for
  95.  
  96. // レイ上に物体がない場合
  97. if (nearestShape == null)
  98. {
  99. // 背景色をプロットする
  100. stroke(color(100,149, 237));
  101. point(x, y);
  102.  
  103. // 次の (x, y) へ
  104. continue;
  105. }
  106.  
  107. // 放射輝度
  108. FColor col = new FColor();
  109.  
  110. // 環境光の反射光の放射輝度を加算する
  111. col.add(colorPi(kAmb, ambientIntensity));
  112.  
  113. // 法線ベクトルと入射ベクトルの内積を計算する.
  114. Lighting lighting = lightSource.lightingAt(nearestIp.position);
  115. float nlDot = constrain(nearestIp.normal.dot(lighting.direction), 0, 1);
  116.  
  117. // 拡散反射光の放射輝度を加算する
  118. col.add(colorPi(kDif, lightSource.intensity, new FColor(nlDot)));
  119.  
  120. // 鏡面反射が無い場合
  121. if (nlDot <= 0)
  122. {
  123. // プロットする
  124. stroke(col.toColor());
  125. point(x, y);
  126.  
  127. // 次の (x, y) へ
  128. continue;
  129. }
  130.  
  131. // 視線ベクトルの逆ベクトルと正反射ベクトルの内積を計算する
  132. PVector invEyeDir = vectorSigma(-1, eyeRay.direction).normalize();
  133. PVector refDir = vectorSigma(2 * nlDot, nearestIp.normal, -1, lighting.direction);
  134. float vrDot = constrain(invEyeDir.dot(refDir), 0, 1);
  135.  
  136. // 鏡面反射光の放射輝度を加算する
  137. col.add(colorPi(kSpe, lightSource.intensity, new FColor(pow(vrDot, shininess))));
  138.  
  139. // プロットする
  140. stroke(col.toColor());
  141. point(x, y);
  142.  
  143. }//for
  144. }//for
  145. }// void draw()
  146.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty