Simpler RayTracer *update* Code 0.04

Übersicht BlitzBasic Codearchiv

Gehe zu Seite 1, 2  Weiter

Neue Antwort erstellen

StepTiger

Betreff: Simpler RayTracer *update* Code 0.04

BeitragMo, Okt 09, 2006 22:00
Antworten mit Zitat
Benutzer-Profile anzeigen
Tja! Endlich ist es so weit Smile ein Raytracer von mir, den ich endlich fertig stellen konnte.

Ist noch nicht sehr gut, wird jedoch noch weiter verbessert.

Ziemlich langsam das ganze aber funktioniert Smile

Code: [AUSKLAPPEN]
Graphics 200,200,32,2
SetBuffer BackBuffer()

Type Camera
   
   Field x#,y#,z#
   Field xwink#,ywink#

End Type

Type Entity
   
   Field x#,y#,z#
   Field v.Vector[65535]
   Field tri.Triangle[65535]
   Field tris
   Field vecs
   
End Type

Type Vector
   
   Field x#,y#,z#
   Field col
   
End Type

Type Triangle
   
   Field v.vector[3]
   
End Type

cam.Camera=New Camera

lastfps=MilliSecs()

z=R_CreateCube()

wink#=0

While Not KeyHit(1)

   Cls

   wink#=wink#+10.
   
   update3d(cam)
   
   fps#=(9.*fps#+(1000./(MilliSecs()-lastfps)))/10.:lastfps=MilliSecs()
   
   cam\x=Sin(wink#+180)*11.
   cam\z=11+Cos(wink#+180)*11.
   cam\ywink=wink#
   
   Text 1,1,fps#
   
   Flip
   
Wend

End

Function R_CreateCube()
   
   z=R_CreateEntity()
   
   R_AddVertex(z,-1,1,10)
   R_AddVertex(z,-1,-1,10)
   R_AddVertex(z,-1,1,12)
   R_AddVertex(z,-1,-1,12)
   R_AddVertex(z,1,1,10)
   R_AddVertex(z,1,-1,10)
   R_AddVertex(z,1,1,12)
   R_AddVertex(z,1,-1,12)
   
   R_AddTriangle(z,1,2,3)
   R_AddTriangle(z,2,3,4)
   R_AddTriangle(z,1,5,2)
   R_AddTriangle(z,2,5,6)
   R_AddTriangle(z,5,6,7)
   R_AddTriangle(z,6,7,8)
   R_AddTriangle(z,3,4,8)
   R_AddTriangle(z,3,7,8)
   R_AddTriangle(z,1,3,5)
   R_AddTriangle(z,3,5,7)
   R_AddTriangle(z,2,4,6)
   R_AddTriangle(z,4,6,8)
   
   Return z
   
End Function

Function R_CreateEntity()
   
   x.Entity=New Entity
   Return Handle(x)
   
End Function

Function R_AddTriangle(Entity,v1,v2,v3)
   
   Ent.Entity=Object.Entity(Entity)
   
   Ent\tris=Ent\tris+1
   s=Ent\tris
   
   Ent\tri[s]=New Triangle
   Ent\tri[s]\v[1]=Ent\v[v1]
   Ent\tri[s]\v[2]=Ent\v[v2]
   Ent\tri[s]\v[3]=Ent\v[v3]
   
   Return s
   
End Function

Function R_AddVertex(Entity,tx#,y#,z#,col=255 Shl 16 + 255 Shl 8 + 255)
   
   Ent.Entity=Object.Entity(Entity)
   
   Ent\vecs=Ent\vecs+1
   s=Ent\vecs
   Ent\v[s]=New Vector
   Ent\v[s]\x#=tx#
   Ent\v[s]\y#=y#
   Ent\v[s]\z#=z#
   Ent\v[s]\col=col
   
   Return s
   
End Function

Function update3d(camera.Camera,dist#=1000,fovx#=90,fovy#=0)
   
   If fovy#=0
      fovy#=GraphicsHeight()/GraphicsWidth()*90.
   EndIf
   
   LockBuffer
   
   wid=GraphicsWidth()
   hei=GraphicsHeight()
   
   camxwink#=camera\xwink#
   camywink#=camera\ywink#

   fakx#=wid/fovx#
   faky#=hei/fovy#
   
   For x=-wid/2 To wid/2-1
      For y=-wid/2 To wid/2-1
         
         sx#=camera\x#
         sy#=camera\y#
         sz#=camera\z#
         
         winky#=x/fakx#+camywink#
         winkx#=y/faky#+camxwink#
         
         dx#=sx#+Sin(winky#)*Cos(winkx#)*dist#
         dz#=sz#+Cos(winky#)*Cos(winkx#)*dist#
         dy#=sy#+Sin(winkx#)*dist#
         
         col=0
         short.Triangle=Null
         shortest=dist#
         
         For s.Triangle=Each Triangle
            
            r#=Ray_Intersect_Triangle#(sx#,sy#,sz#,dx#,dy#,dz#,s\v[1]\x#,s\v[1]\y#,s\v[1]\z#,s\v[2]\x#,s\v[2]\y#,s\v[2]\z#,s\v[3]\x#,s\v[3]\y#,s\v[3]\z#)

            If r#<>False; And r#<shortest
               
               shortest=r#
               short=s
               
            EndIf
            
         Next
         
         If short<>Null
            
            col=short\v[1]\col
            
         EndIf
         
         WritePixelFast(x+wid/2,y+hei/2,col,BackBuffer())
         
      Next
   Next
   
   UnlockBuffer
   
End Function

Function Ray_Intersect_Triangle#(Px#, Py#, Pz#, Dx#, Dy#, Dz#, V0x#, V0y#, V0z#, V1x#, V1y#, V1z#, V2x#, V2y#, V2z#, Extend_To_Infinity=False, Cull_Backfaces=False)
   
   ; crossproduct(b,c) =
   ; ax = (by * cz) - (cy * bz)
   ; ay = (bz * cx) - (cz * bx)    
   ; az = (bx * cy) - (cx * by)

   ; dotproduct(v,q) =
   ; (vx * qx) + (vy * qy) + (vz * qz)   
   ; DP =  1 = Vectors point in same direction.          (  0 degrees of seperation)
   ; DP =  0 = Vectors are perpendicular to one another. ( 90 degrees of seperation)
   ; DP = -1 = Vectors point in opposite directions.     (180 degrees of seperation)
   ;
   ; The dot product is also reffered to as "the determinant" or "the inner product"

   ; Calculate the vector that represents the first side of the triangle.
   E1x# = V2x# - V0x#
   E1y# = V2y# - V0y#
   E1z# = V2z# - V0z#

   ; Calculate the vector that represents the second side of the triangle.
   E2x# = V1x# - V0x#
   E2y# = V1y# - V0y#
   E2z# = V1z# - V0z#

   ; Calculate a vector which is perpendicular to the vector between point 0 and point 1,
   ; and the direction vector for the ray.
   ; Hxyz = Crossproduct(Dxyz, E2xyz)
   Hx# = (Dy# * E2z#) - (E2y# * Dz#)
   Hy# = (Dz# * E2x#) - (E2z# * Dx#)
   Hz# = (Dx# * E2y#) - (E2x# * Dy#)

   ; Calculate the dot product of the above vector and the vector between point 0 and point 2.
   A# = (E1x# * Hx#) + (E1y# * Hy#) + (E1z# * Hz#)

   ; If we should ignore triangles the ray passes through the back side of,
   ; and the ray points in the same direction as the normal of the plane,
   ; then the ray passed through the back side of the plane, 
   ; and the ray does not intersect the plane the triangle lies in.
   If (Cull_Backfaces = True) And (A# >= 0) Then Return False
      
   ; If the ray is almost parralel to the plane,
   ; then the ray does not intersect the plane the triangle lies in.
   If (A# > -0.00001) And (A# < 0.00001) Then Return False
   
   ; Inverse Determinant. (Dot Product)
   ; (Scaling factor for UV's?)
   F# = 1.0 / A#

   ; Calculate a vector between the starting point of our ray, and the first point of the triangle,
   ; which is at UV(0,0)
   Sx# = Px# - V0x#
   Sy# = Py# - V0y#
   Sz# = Pz# - V0z#
   
   ; Calculate the U coordinate of the intersection point.
   ;
   ;   Sxyz is the vector between the start of our ray and the first point of the triangle.
   ;   Hxyz is the normal of our triangle.
   ;   
   ; U# = F# * (DotProduct(Sxyz, Hxyz))
   U# = F# * ((Sx# * Hx#) + (Sy# * Hy#) + (Sz# * Hz#))
   
   ; Is the U coordinate outside the range of values inside the triangle?
   If (U# < 0.0) Or (U# > 1.0)

      ; The ray has intersected the plane outside the triangle.
      Return False
   
   EndIf

   ; Not sure what this is, but it's definitely NOT the intersection point.
   ;
   ;   Sxyz is the vector from the starting point of the ray to the first corner of the triangle.
   ;   E1xyz is the vector which represents the first side of the triangle.
   ;   The crossproduct of these two would be a vector which is perpendicular to both.
   ;
   ; Qxyz = CrossProduct(Sxyz, E1xyz)
   Qx# = (Sy# * E1z#) - (E1y# * Sz#)
   Qy# = (Sz# * E1x#) - (E1z# * Sx#)
   Qz# = (Sx# * E1y#) - (E1x# * Sy#)
   
   ; Calculate the V coordinate of the intersection point.
   ;   
   ;   Dxyz is the vector which represents the direction the ray is pointing in.
   ;   Qxyz is the intersection point I think?
   ;
   ; V# = F# * DotProduct(Dxyz, Qxyz)
   V# = F# * ((Dx# * Qx#) + (Dy# * Qy#) + (Dz# * Qz#))
   
   ; Is the V coordinate outside the range of values inside the triangle?   
   ; Does U+V exceed 1.0? 
   If (V# < 0.0) Or ((U# + V#) > 1.0)

      ; The ray has intersected the plane outside the triangle.      
      Return False

      ; The reason we check U+V is because if you imagine the triangle as half a square, U=1 V=1 would
      ; be in the lower left hand corner which would be in the lower left triangle making up the square.
      ; We are looking for the upper right triangle, and if you think about it, U+V will always be less
      ; than or equal to 1.0 if the point is in the upper right of the triangle.

   EndIf

   ; Calculate the distance of the intersection point from the starting point of the ray, Pxyz.
   ; This distance is scaled so that at Pxyz, the start of the ray, T=0, and at Dxyz, the end of the ray, T=1.
   ; If the intersection point is behind Pxyz, then T will be negative, and if the intersection point is
   ; beyond Dxyz then T will be greater than 1.
   T# = F# * ((E2x# * Qx#) + (E2y# * Qy#) + (E2z# * Qz#))

   ; If the triangle is behind Pxyz, ignore this intersection.
   ; We want a directional ray, which only intersects triangles in the direction it points.
   If (T# < 0) Then Return False

   ; If the plane is beyond Dxyz, amd we do not want the ray to extend to infinity, then ignore this intersection.
   If (Extend_To_Infinity = False) And (T# > 1) Return False

   ; The ray intersects the triangle!      
   Return T#

End Function
Noch gestern standen wir am Abgrund, doch heute sind wir schon einen Schritt weiter.
Computer:
AMD Sempron 3000+; ATI Radeon 9800 Pro; 512 MB DDR RAM 400Mhz; Asus E7N8X-E Deluxe; Samsung 200GB HD 5.4ns acces t
Gewinner: BP Code Compo #2
Π=3.141592653589793238...<--- und das aus dem kopf Laughing
Seit der Earthlings-Diskussion überzeugter Fleisch(fr)esser.
  • Zuletzt bearbeitet von StepTiger am So, Okt 15, 2006 13:22, insgesamt 6-mal bearbeitet

Dante

BeitragMo, Okt 09, 2006 22:31
Antworten mit Zitat
Benutzer-Profile anzeigen
Hmm bekomm nur nen MAV Crying or Very sad
 

timmeTheOnly

BeitragDi, Okt 10, 2006 6:09
Antworten mit Zitat
Benutzer-Profile anzeigen
Ja ich auch.

derAtomkeks

ehemals "Sethus"

BeitragDi, Okt 10, 2006 8:11
Antworten mit Zitat
Benutzer-Profile anzeigen
Was ist nen RayTracer? Embarassed

PowerProgrammer

BeitragDi, Okt 10, 2006 9:10
Antworten mit Zitat
Benutzer-Profile anzeigen
Kann ich schlecht erklären, aber ich versuche mal das, was ich weiß:

Ein RayTracer ist ein Programm, womit man per Befehlen 3D-Szenen programmieren kann. Dabei gibt es keine Polygone etc., weil es Grundobjekte, wie z.B. eine Kugel oder eine Box gibt, die dann sodargestellt werden, wie man sie erzeugt. Also muss man sich nicht um kantige Objekte kümmern, jedenfalls nur selten. Das Programm berechnbet dann alles und das Ergebnis ist dann aber keine 3D-Datei sondern ein gerendertes Bild.

Naja, jedenfalls so ähnliches. Kanns wohl doch nicht so gut erklären, wie dieser Wikipedia-Artikel. Schau da, da wirst du fündig.

Es lebe Pov Ray Wink
www.xairro.com Alles für Webmaster und Programmierer! Es gibt mehr als bloß einen Counter!

derAtomkeks

ehemals "Sethus"

BeitragDi, Okt 10, 2006 9:27
Antworten mit Zitat
Benutzer-Profile anzeigen
Achso, das kenne ich. Nur nicht unter dem Begriff! Danke das du mir die Auge geöffnet hast Smile

StepTiger

BeitragDi, Okt 10, 2006 12:59
Antworten mit Zitat
Benutzer-Profile anzeigen
ersetzt eventuell mal writepixelfast durch writepixel

neuer code, siehe oben
Noch gestern standen wir am Abgrund, doch heute sind wir schon einen Schritt weiter.
Computer:
AMD Sempron 3000+; ATI Radeon 9800 Pro; 512 MB DDR RAM 400Mhz; Asus E7N8X-E Deluxe; Samsung 200GB HD 5.4ns acces t
Gewinner: BP Code Compo #2
Π=3.141592653589793238...<--- und das aus dem kopf Laughing
Seit der Earthlings-Diskussion überzeugter Fleisch(fr)esser.

Spikespine

BeitragDi, Okt 10, 2006 13:51
Antworten mit Zitat
Benutzer-Profile anzeigen
Der MAV tritt auf, weil du mit writepixelfast außerhalb des Grafikbereichs arbeitest. Beachte, dass du bei einer Auflösung von 200*200 nur die Pixel 0-199 ansprechen kannst, beim 200. gibt es einen MAV. Ist sozusagen eine Falle, vor allem weil der Fehler nicht bei allen PCs (bzw. Grafikkarten) auftritt. Ich würde aber das WritepixelFast beibehalten, schließlich ist es um einiges schneller.

Ansonsten: solide Arbeit, auch wenn es natürlich nichts neues ist Smile
Athlon 64 3700+ | 1024 MB RAM | GeForce 7900 GT | Blitz2D, Blitz3D, BlitzPlus, BlitzMax

StepTiger

BeitragDi, Okt 10, 2006 20:07
Antworten mit Zitat
Benutzer-Profile anzeigen
geht bei mir trotzdem ^^

habs mal so verändert, dass es geht
Noch gestern standen wir am Abgrund, doch heute sind wir schon einen Schritt weiter.
Computer:
AMD Sempron 3000+; ATI Radeon 9800 Pro; 512 MB DDR RAM 400Mhz; Asus E7N8X-E Deluxe; Samsung 200GB HD 5.4ns acces t
Gewinner: BP Code Compo #2
Π=3.141592653589793238...<--- und das aus dem kopf Laughing
Seit der Earthlings-Diskussion überzeugter Fleisch(fr)esser.

tft

BeitragSo, Okt 15, 2006 8:43
Antworten mit Zitat
Benutzer-Profile anzeigen
Hallo ...

cool ..... bei mir leuft es ...... aber ist ein wenig langsam .....
TFT
https://www.sourcemagic.ch
Monkey,HTML5,CSS3,W 10 64 Bit, 32 GB Ram, GTX Titan, W8 ist Müll !!!!!!

PowerProgrammer

BeitragSo, Okt 15, 2006 8:57
Antworten mit Zitat
Benutzer-Profile anzeigen
Nicht schlecht.
Mir ist beim beenden folgendes aufgefallen: Man muss ganz lange die Escape-Taste drücken, da du KeyDown(1) nutzt. Besser wäre KeyHit(1)
Aber das interessiert ja wohl keinen Wink

Die Geschwindigkeit lässt aber wirklich zu wünschen übrig, und das bei 200x200 ohne AA und einem kleinen Würfel Shocked Shocked Was passiert, wenn man da was vernünfitges später rendert, z.B. ein Bild mit Spiegeln... Das will ich mir garnicht ausmalen.

Als Test zu diesem Thema ist der Code aber wirklich gelungen.

Außerdem dachte ich immer, das Ray-Tracer ohne Dreiecke und Polygone und son Zeugs arbeiten...
www.xairro.com Alles für Webmaster und Programmierer! Es gibt mehr als bloß einen Counter!

Goodjee

BeitragSo, Okt 15, 2006 9:05
Antworten mit Zitat
Benutzer-Profile anzeigen
warum benutzt du graphics3d?
"Ideen sind keine Coladosen, man kann sie nicht recyclen"-Dr. House
http://deeebian.redio.de/ http://goodjee.redio.de/

StepTiger

BeitragSo, Okt 15, 2006 13:21
Antworten mit Zitat
Benutzer-Profile anzeigen
@Goodjee:
Jetzt, wo du es sagst, keine Ahnung!
Habs oben verändert.

@PowerProgrammer:
Natürlich nutzt ein Raytracer ebenso Dreiecke.
Basiert ja auf Polygonen
Das ganze ist so langsam, weil es nicht von der Grafikkarte/dem Grafikchip beschleunigt wird. Der Prozessor übernimmt die ganze Arbeit. Du kannst den Würfel auch vergrößern, wenn es dich freut. Ist genau so schnell/langsam.

Keydown macht ja fast das Selbe wie keyhit, musst also nur länger drücken.
Habe allerdings nicht daran gedacht, dass es so verdammt langsam ist ^^
Da merkt man den Unterschied eben deutlich.
Noch gestern standen wir am Abgrund, doch heute sind wir schon einen Schritt weiter.
Computer:
AMD Sempron 3000+; ATI Radeon 9800 Pro; 512 MB DDR RAM 400Mhz; Asus E7N8X-E Deluxe; Samsung 200GB HD 5.4ns acces t
Gewinner: BP Code Compo #2
Π=3.141592653589793238...<--- und das aus dem kopf Laughing
Seit der Earthlings-Diskussion überzeugter Fleisch(fr)esser.

PowerProgrammer

BeitragSo, Okt 15, 2006 13:46
Antworten mit Zitat
Benutzer-Profile anzeigen
Hm, Pov-Ray ist auch nicht Hardware-Beschleunigt, trotzdem ist es vielfach schneller. MAl sehen, ob du es schaffst, das ganze zu optimieren.
www.xairro.com Alles für Webmaster und Programmierer! Es gibt mehr als bloß einen Counter!

Mr.Keks

BeitragSo, Okt 15, 2006 13:52
Antworten mit Zitat
Benutzer-Profile anzeigen
anderes raytracerzeugs:

in dem thread finden sich einige ziemlich coole sachen von filax und devils child, die auch ordentlich schnell sind. (schaut euch mal das bouncy balls und so an! xD)
http://www.blitzbasic.com/Comm...opic=57143

mit alter bmaxversion.. (habe jetzt nicht geprüft, ob es mit aktuellem bmax noch läuft):
http://www.blitzbasic.com/Comm...opic=51138
http://www.blitzbasic.com/Comm...opic=49763

ne jugendsünde von mir:
https://www.blitzforum.de/foru...php?t=8162
(hielt es damals für schlau, keine rays, sondern auf einem ray bewegte punkte zu verwenden... weiß auch nicht meh warum. leider fehlt auch ein ordentliches matrixkoordinatensystem)
MrKeks.net

StepTiger

BeitragSo, Okt 15, 2006 16:05
Antworten mit Zitat
Benutzer-Profile anzeigen
das von devils child usw. ist ja kein richtiges raytracing, da es über die eingebaute 3d engine arbeitet.

Damit kann ich auch leicht was bauen Smile (besser gesagt, damit hatte ich mal was gebaut)
Noch gestern standen wir am Abgrund, doch heute sind wir schon einen Schritt weiter.
Computer:
AMD Sempron 3000+; ATI Radeon 9800 Pro; 512 MB DDR RAM 400Mhz; Asus E7N8X-E Deluxe; Samsung 200GB HD 5.4ns acces t
Gewinner: BP Code Compo #2
Π=3.141592653589793238...<--- und das aus dem kopf Laughing
Seit der Earthlings-Diskussion überzeugter Fleisch(fr)esser.

Mr.Keks

BeitragSo, Okt 15, 2006 16:17
Antworten mit Zitat
Benutzer-Profile anzeigen
öhm? wiebitte? ich sehe da nen 1a raytracer ^^
MrKeks.net

StepTiger

BeitragSo, Okt 15, 2006 16:26
Antworten mit Zitat
Benutzer-Profile anzeigen
komisch! Ich sehe da ein RenderWorld
Noch gestern standen wir am Abgrund, doch heute sind wir schon einen Schritt weiter.
Computer:
AMD Sempron 3000+; ATI Radeon 9800 Pro; 512 MB DDR RAM 400Mhz; Asus E7N8X-E Deluxe; Samsung 200GB HD 5.4ns acces t
Gewinner: BP Code Compo #2
Π=3.141592653589793238...<--- und das aus dem kopf Laughing
Seit der Earthlings-Diskussion überzeugter Fleisch(fr)esser.

Goodjee

BeitragSo, Okt 15, 2006 16:37
Antworten mit Zitat
Benutzer-Profile anzeigen
aber es rendert schatten und so wie ein raytracer und berechnet nur geometrische figuren über die engine...würde ich mal vermuten von den funktionsnamen her....
"Ideen sind keine Coladosen, man kann sie nicht recyclen"-Dr. House
http://deeebian.redio.de/ http://goodjee.redio.de/

Goodjee

BeitragSo, Okt 15, 2006 17:16
Antworten mit Zitat
Benutzer-Profile anzeigen
PowerProgrammer hat Folgendes geschrieben:
Hm, Pov-Ray ist auch nicht Hardware-Beschleunigt, trotzdem ist es vielfach schneller. MAl sehen, ob du es schaffst, das ganze zu optimieren.


Naja, PovRay stehen auch ganz andere Möglichkeiten zur Verfügung...Threads, Assembler um nur einige zu nennen...

Bist du sicher das sie nicht auch die Grafikkarte benutzen?
"Ideen sind keine Coladosen, man kann sie nicht recyclen"-Dr. House
http://deeebian.redio.de/ http://goodjee.redio.de/

Gehe zu Seite 1, 2  Weiter

Neue Antwort erstellen


Übersicht BlitzBasic Codearchiv

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group