Rabu, 13 Juli 2011

Algoritma Ray Tracing

Ray tracing adalah suatu metode merender gambar dengan cara menelusuri sinar yang mengenai obyek. Jika sinar yang ditelusuri tersebut mengenai suatu obyek maka diperhitungkan intensitas warna obyek tersebut.


gambar hasil dari ray tracing

Hampir semua program raytracing (dikenal sebagai "raytracers") menggunakan algoritma dasar yang sama dalam satu cara atau yang lain. Algoritma ini disajikan di bawah ini.

    Bagian Pra-Rendering
    Loop Utama Rendering
    Bagian Post-Rendering

Bagian Pra-Rendering

Bagian ini dilaksanakan sebelum rendering sebenarnya berlangsung. Ini adalah yang paling diimplementasikan dan programmer tergantung dari semua bagian dari algoritma. Struktur dan isi bergantung sepenuhnya pada bagaimana programmer telah memilih untuk menggambarkan dan menyimpan scene dan variabel apa yang perlu diinisialisasi untuk program untuk berjalan dengan baik. Beberapa bagian yang ditampilkan dalam beberapa bentuk atau yang lain termasuk generasi struktur efisiensi, inisialisasi perangkat output, menyiapkan berbagai dll variabel global Tidak ada algoritma standar untuk bagian ini.

Loop Utama Rendering
Bagian ini benar-benar menghasilkan gambar 3-D scene. Algoritma untuk bagian ini diberikan di bawah ini. Perhatikan bahwa prosedur RayTrace secara recursive, artinya, memanggil dirinya sendiri.

Procedure RenderPicture()
  For each pixel on the screen,
    Generate a ray R from the viewing position through the point on the view
      plane corresponding to this pixel.
    Call the procedure RayTrace() with the arguments R and 0
    Plot the pixel in the colour value returned by RayTrace()
  Next pixel
End Procedure

Procedure RayTrace(ray R, integer Depth) returns colour
  Set the numerical variable Dis to a maximum value
  Set the object pointer Obj to null
  For each object in the scene
    Calculate the distance (from the starting point of R) of the nearest
      intersection of R with the object in the forward direction
    If this distance is less than Dis
      Update Dis to this distance
      Set Obj to point to this object
    End if
  Next object
  If Obj is not null
    Set the position variable Pt to the nearest intersection point of R and Obj
    Set the total colour C to black
    For each light source in the scene
      For each object in the scene
        If this object blocks the light coming from the light source to Pt
          Attenuate the intensity of the received light by the transmittivity
            of the object
        End if
      Next object
      Calculate the perceived colour of Obj at Pt due to this light source
        using the value of the attenuated light intensity
      Add this colour value to C
    Next light source
    If Depth is less than a maximum value
      Generate two rays Refl and Refr in the reflected and refracted directions,
        starting from Pt
      Call RayTrace with arguments Refl and Depth + 1
      Add (the return value * reflectivity of Obj) to C
      Call RayTrace with arguments Refr and Depth + 1
      Add (the return value * transmittivity of Obj) to C
    End if
  Else
    Set the total colour C to the background colour
  End if
  Return C
End Procedure


Bagian Post-Rendering

Bagian ini juga lebih atau kurang tergantung pada programmer, tetapi fungsi yang dilakukan biasanya dimasukkan ke dalam satu atau lebih dari kelompok berikut

    Memori cleanup: Ruang diambil untuk scene dan data rendering dilepaskan.

    Menerapkan proses pasca-filter: efek khusus seperti blur, edge-highlighting, suppression of colour channels, dll koreksi gamma dapat ditambahkan ke gambar setelah waktu rendering yang sebenarnya telah terjadi. Antialiasing, dll blur biasanya merupakan bagian dari bagian rendering utama.

    Membebaskan perangkat output: Jika perangkat output adalah sebuah file, itu ditutup. Jika sebuah perangkat keras, maka setiap link yang didirikan dengan itu mungkin bisa ditunda.

    Menampilkan statistik dan data pengguna lain: Hal ini, tentu saja, sepenuhnya ke pemrogram.
Tapi ini berguna untuk mengetahui rincian proses rendering.

Tidak ada komentar:

Posting Komentar