3D rendering in A2 operating systemSAGE Source code of lessons from www.scratchapixel.comThere are the source code of lessons from www.scratchapixel.com, ported to Active Oberon. When porting from C++ the possibility of operator overloading are used that exists in Active Oberon language. Active Objects concept for multithreading is used. Lesson 1. The Ray-Tracing Algorithm
AntialiasingBased on the code of this lesson, as an experiment, one-level adaptive antialiasing algorithm carried out. The essence of the algorithm is to find problematic pixels on the edges of objects. Then the colors of pixels refines by mixing with colors of four sub-pixels according to their weights. For better results, you can refine even the colors of sub-pixels by its further dividing into an even more minor elements, but it was decided to confine with only one sub-pixel level. Finding of problematic pixels, was agreed by calculating the distance between the color of each pixel and color of its neighboring pixels in the color space, just as the distance between points in three-dimensional space calculates. If the color-distance of at least with one of the neighboring pixels exceeding a specified threshold, pixel is considered to be problematic. The result of detecting problematic pixels with the threshold value is equal to 0.05:
Well you can see that this threshold gives a good stock and lot of pixels, without visible antialiasing artifacts. The threshold can be raised to higher level. Then for all problematic pixels additional four rays passing through the centers of sub-pixels are calculated, and traced. Then the average sub-pixels' color calculated, and its distance to sub-pixels' colors calculated. Weights are calculated of the principle: the sub-pixel with the greatest color-distance from the average color has greatest weight. Then the average color of sub-pixels according its weight calculated and the results averaged with previously calculated pixel color. The result of the algorithm is quite good, but that were achieved with only 28000 rays traced further. Thus for rendering whole scene were traced about 1.09 rays per each pixel. The result of the antialiasing algorithm:
Antialiasing in 2D graphicsThe experience gained from experiments with adaptive antialiasin found suitable for antialiasing in 2D graphics. BohdanT modified module WMClock to draw a pretty clock's arrows in analog mode, the result was good, but the whole picture was spoiled by the effect of aliasing on arrows. I decided to demonstrate the possibility of using adaptive antialiasing, in this case too. The multilevel adaptive antialiasing is now fully implemented, and the number of the levels may be provided. Here is the result of the algorithm:
Lesson 6. Ray-Triangle IntersectionThe color at the intersection point is set with the barycentric coordinates:
The triangle is shaded by interpolating the vertex color:
Adaptive antialiasing + multithreading + SSE optimizationAfter long delay I found some time for full rewriting of 1st and 6th examples. Results in Raytracer.zip. Adaptive antialiasing now fully implemented. Multithreading code now trully multithread, what you can see by benchmarks. Also SSE optimizations are present. SSE optimizations are in separated module, if required they may be turned on, by replacing lib := Lib3D to lib := Lib3D_SSE in import section. One can see from benchmarks, that SSE optimizations does not give big perfomance improvement, because unaligned variants of SSE comands used. So, there is a room for further optimizations :) Benchmarks results (parameters of the system: AMD A10-7850K Radeon R7, 12 Compute Cores 4C+8G @ 3.70 GHz (4 cores, 4 threads)). Lesson1, without SSE: nThreadsTotal: 1, bucket size: 640 x 480, nBucketsTotal: 1, time elapsed: 1.2909 nThreadsTotal: 2, bucket size: 320 x 240, nBucketsTotal: 4, time elapsed: 0.7199 nThreadsTotal: 4, bucket size: 160 x 120, nBucketsTotal: 16, time elapsed: 0.3787 Lesson1, SSE: nThreadsTotal: 1, bucket size: 640 x 480, nBucketsTotal: 1, time elapsed: 1.0590 nThreadsTotal: 2, bucket size: 320 x 240, nBucketsTotal: 4, time elapsed: 0.5875 nThreadsTotal: 4, bucket size: 160 x 120, nBucketsTotal: 16, time elapsed: 0.3032 Lesson6, without SSE: nThreadsTotal: 1, bucket size: 320 x 240, nBucketsTotal: 4, time elapsed: 0.2253 nThreadsTotal: 2, bucket size: 160 x 120, nBucketsTotal: 16, time elapsed: 0.1196 nThreadsTotal: 4, bucket size: 80 x 60, nBucketsTotal: 64, time elapsed: 0.0673 Lesson6, SSE: nThreadsTotal: 1, bucket size: 320 x 240, nBucketsTotal: 4, time elapsed: 0.1826 nThreadsTotal: 2, bucket size: 160 x 120, nBucketsTotal: 16, time elapsed: 0.0976 nThreadsTotal: 4, bucket size: 80 x 60, nBucketsTotal: 64, time elapsed: 0.0541 Lesson6, fast triangle-ray intersection algorithm (Möller–Trumbore), without SSE: nThreadsTotal: 1, bucket size: 320 x 240, nBucketsTotal: 4, time elapsed: 0.2030 nThreadsTotal: 2, bucket size: 160 x 120, nBucketsTotal: 16, time elapsed: 0.1079 nThreadsTotal: 4, bucket size: 80 x 60, nBucketsTotal: 64, time elapsed: 0.0592 Lesson6, fast triangle-ray intersection algorithm (Möller–Trumbore), SSE: nThreadsTotal: 1, bucket size: 320 x 240, nBucketsTotal: 4, time elapsed: 0.1565 nThreadsTotal: 2, bucket size: 160 x 120, nBucketsTotal: 16, time elapsed: 0.0826 nThreadsTotal: 4, bucket size: 80 x 60, nBucketsTotal: 64, time elapsed: 0.0451 Algorythms work results. With antialiasing it's look more pretty!
Last update: 28-1-16 21:39:36 |
|||||||||||||||||||||||||||
Copyright © 2005-2017 SAGE. All rights reserved. |