[B3D]2D-Schatten (3D benötigt!)

Übersicht BlitzBasic Codearchiv

Gehe zu Seite Zurück  1, 2, 3, 4

Neue Antwort erstellen

 

BIG BUG

BeitragDo, Jun 18, 2009 21:53
Antworten mit Zitat
Benutzer-Profile anzeigen
Ja, das Ganze ist natürlich nur für den Hintergrund eine Lösung, darauf befindlichen Objekte müssten auf eine andere Weise beleuchtet werden, (oder eben selbst Lichter sein). Bei wenigen kleinen Objekten könnte man dann z.B. nur den Teil hinter dem Objekt ausschneiden und diesen überblenden. Allerdings wäre das schon eine sehr spezifische Lösung.
Mit Z-Buffer-Tricks könnte man hier zwar noch Löcher in den Hintergrund schneiden, sobald es sich aber um konkave Meshes handelt oder sich Objekte überschneiden funktioniert diese Lösung auch nicht mehr. Ebenso wäre das aber auch nur ein sehr spezifischer Workaround.

Ansonsten gibt es aber auch noch FastExt, mit der ein Rendern von Texturbuffern fast ohne Geschwindigkeitsverlust möglich ist.
Und durch die Berechnung der Lichtflächen anstatt der Schatten wäre hier dann auch nur ein Puffer notwendig, unabhängig von der Anzahl der Lichter.
B3D-Exporter für Cinema4D!(V1.4)
MD2-Exporter für Cinema4D!(final)

aMul

Sieger des Minimalist Compo 01/13

BeitragSa, Jun 20, 2009 15:13
Antworten mit Zitat
Benutzer-Profile anzeigen
Nach ein paar Tagen Pause habe ich mich jetzt wieder rangesetzt um die von euch ange- und besprochenen Änderungen zu machen. Dazu habe ich allerdings zuerst die Funktion DrawShadows() optimiert, um möglichst viele Polys von vorne rein auszuschließen.
Das ganze hat eine ganze Zeit gedauert, da Vektoren bisher nicht auf dem Lehrplan standen. Rolling Eyes

Aber letzten Endes habe ich doch etwas zu Stande bekommen und ich bin sehr überrascht, dass sich durch diese Abfragen bis zu 70% Rechenleistung sparen lassen.
Die Funktion sieht jetzt wie folgt aus(auch im ersten Post aktualisiert):
Code: [AUSKLAPPEN]
Function DrawShadows(shadow.TShadow, drawLight = True, clear = True, LightDetail = 36)
  ; draws shadows
   ; shadow: TShadow to draw
   ; drawLight: draws light
   ; clear: clears previous shadows (highly recommended!)
   ; lightDetail: triangles used for light mesh
   Local wall.TWall, v0, v1, v2, v3
   Local lx#, ly#, lr#, nx#, ny#, d#
   Local x#, y#, x2#, y2#
   lx = shadow\light\x
   ly = shadow\light\y
   lr = shadow\light\radius
   If clear
      ClearSurface(shadow\surface)
   EndIf
   If drawLight
      DrawShadowLight(shadow, LightDetail)
   EndIf
   For wall = Each TWall
      If (wall\x1 > lx - lr Or wall\x2 > lx - lr) And (wall\x1 < lx + lr Or wall\x2 < lx + lr) And (wall\y1 > ly - lr Or wall\y2 > ly - lr) And (wall\y1 < ly + lr Or wall\y2 < ly + lr)
         nx = wall\y2 - wall\y1
         ny = wall\x1 - wall\x2
         d = Sqr(nx * nx + ny * ny)
         nx = nx / d
         ny = ny / d
         d = nx * (wall\x1 - lx) + ny * (wall\y1 - ly)
         If d > -lr And d < lr * shadow\backface
            x = (wall\x1 - lx) * 10000 + lx
            y = (wall\y1 - ly) * 10000 + ly
            x2 = (wall\x2 - lx) * 10000 + lx
            y2 = (wall\y2 - ly) * 10000 + ly
            v0 = AddVertex(shadow\surface, wall\x1, wall\y1, 0)
            v1 = AddVertex(shadow\surface, wall\x2, wall\y2, 0)
            v2 = AddVertex(shadow\surface, x, y, 0)
            v3 = AddVertex(shadow\surface, x2, y2, 0)
            VertexColor(shadow\surface, v0, shadow\red, shadow\grn, shadow\blu)
            VertexColor(shadow\surface, v1, shadow\red, shadow\grn, shadow\blu)
            VertexColor(shadow\surface, v2, shadow\red, shadow\grn, shadow\blu)
            VertexColor(shadow\surface, v3, shadow\red, shadow\grn, shadow\blu)
            AddTriangle(shadow\surface, v0, v3, v2)
            AddTriangle(shadow\surface, v0, v1, v3)
         EndIf
      EndIf
   Next
End Function

Zum Vergleich, die alte:
Code: [AUSKLAPPEN]
Function DrawShadows(shadow.TShadow, drawLight = True, clear = True, LightDetail = 36)
   ; draws shadows
   ; shadow: TShadow to draw
   ; drawLight: draws light
   ; clear: clears previous shadows (highly recommended!)
   ; lightDetail: triangles used for light mesh
   Local x#, x2#, y#, y2#, wall.TWall
   Local v0, v1, v2, v3
   If clear
      ClearSurface(shadow\surface)
   EndIf
   If drawLight
      DrawShadowLight(shadow, LightDetail)
   EndIf
   For wall = Each TWall
      x = (wall\x1 - shadow\light\x) * 10000 + shadow\light\x
      y = (wall\y1 - shadow\light\y) * 10000 + shadow\light\y
      x2 = (wall\x2 - shadow\light\x) * 10000 + shadow\light\x
      y2 = (wall\y2 - shadow\light\y) * 10000 + shadow\light\y
      v0 = AddVertex(shadow\surface, wall\x1, wall\y1, 0)
      v1 = AddVertex(shadow\surface, wall\x2, wall\y2, 0)
      v2 = AddVertex(shadow\surface, x, y, 0)
      v3 = AddVertex(shadow\surface, x2, y2, 0)
      VertexColor(shadow\surface, v0, shadow\red, shadow\grn, shadow\blu)
      VertexColor(shadow\surface, v1, shadow\red, shadow\grn, shadow\blu)
      VertexColor(shadow\surface, v2, shadow\red, shadow\grn, shadow\blu)
      VertexColor(shadow\surface, v3, shadow\red, shadow\grn, shadow\blu)
      AddTriangle(shadow\surface, v0, v2, v3)
      AddTriangle(shadow\surface, v0, v3, v1)
   Next
End Function


Da hiermit Rechenleistung von der GPU zur CPU verschoben wurde ist der Geschwindigkeitsgewinn natürlich sehr Hardware-abhängig, weshalb ich mich darüber freuen würde, wenn auch andere ein paar Messungen durchführen könnten(einfach die Objektanzahl höher setzten, damit man vergleichbare Zahlen erhält).
Panic Pong - ultimate action mashup of Pong and Breakout <= aktives Spiele-Projekt, Downloads mit vielen bunten Farben!
advASCIIdraw - the advanced ASCII art program <= aktives nicht-Spiele-Projekt, must-have für ASCII/roguelike/dungeon-crawler fans!
Alter BB-Kram: ThroughTheAsteroidBelt - mit Quelltext! | RGB-Palette in 32²-Textur / Farbige Beleuchtung mit Dot3 | Stereoskopie in Blitz3D | Teleport-Animation Screensaver

Xaymar

ehemals "Cgamer"

BeitragSa, Jun 20, 2009 15:29
Antworten mit Zitat
Benutzer-Profile anzeigen
10ms, standart.

ersetz mal deine DrawWalls mit dieser
Code: [AUSKLAPPEN]
Function DrawWalls()
   ; draws walls
   ;! This function is very slow! Do not use for purposes other than testing or debugging!
   ;CGamer: fixed:D
   LockBuffer
   Local wall.TWall
   For wall = Each TWall
      Line wall\x1, wall\y1, wall\x2, wall\y2
   Next
   UnlockBuffer
End Function
Warbseite

aMul

Sieger des Minimalist Compo 01/13

BeitragSa, Jun 20, 2009 15:39
Antworten mit Zitat
Benutzer-Profile anzeigen
Wie mein Kommentar innerhalb der Funktion sagt, ist sie bestenfalls zum Debuggen oder Testen geeignet.
Sie ist nicht dazu gedacht schnell zu sein und sollte für eine vernünftige Geschwindigkeitsmessung wie schon mehrfach erwähnt nicht aufgerufen werden.

Deine 10 Millisekunden sind leider ohne Vergleich(und mit DrawWalls()) auch nicht vielsagend.
Panic Pong - ultimate action mashup of Pong and Breakout <= aktives Spiele-Projekt, Downloads mit vielen bunten Farben!
advASCIIdraw - the advanced ASCII art program <= aktives nicht-Spiele-Projekt, must-have für ASCII/roguelike/dungeon-crawler fans!
Alter BB-Kram: ThroughTheAsteroidBelt - mit Quelltext! | RGB-Palette in 32²-Textur / Farbige Beleuchtung mit Dot3 | Stereoskopie in Blitz3D | Teleport-Animation Screensaver

Xaymar

ehemals "Cgamer"

BeitragSa, Jun 20, 2009 16:09
Antworten mit Zitat
Benutzer-Profile anzeigen
ich hab keine ahnung wo ich die objekt anzahl hoch stellen kann. genauere angabe wäre einfacher für mich...

[Edit]Dir ist aber bekannt das du ohne Lockbuffer/Unlockbuffer die cpu dann unnötig belastest oder?
Warbseite
  • Zuletzt bearbeitet von Xaymar am Sa, Jun 20, 2009 16:11, insgesamt einmal bearbeitet

aMul

Sieger des Minimalist Compo 01/13

BeitragSa, Jun 20, 2009 16:10
Antworten mit Zitat
Benutzer-Profile anzeigen
Einfach den Parameter von CreateRandomObjects() (vor der Hauptschleife) ändern.
Panic Pong - ultimate action mashup of Pong and Breakout <= aktives Spiele-Projekt, Downloads mit vielen bunten Farben!
advASCIIdraw - the advanced ASCII art program <= aktives nicht-Spiele-Projekt, must-have für ASCII/roguelike/dungeon-crawler fans!
Alter BB-Kram: ThroughTheAsteroidBelt - mit Quelltext! | RGB-Palette in 32²-Textur / Farbige Beleuchtung mit Dot3 | Stereoskopie in Blitz3D | Teleport-Animation Screensaver

Xaymar

ehemals "Cgamer"

BeitragSa, Jun 20, 2009 16:17
Antworten mit Zitat
Benutzer-Profile anzeigen
10 Objekte - MS Min: 4, MS Max: 8
20 Objekte - MS Min: 6, MS Max: 8
40 Objekte - MS Min: 7, MS Max: 10
80 Objekte - MS Min: 10, MS Max: 12
160 Objekte - MS Min: 17, MS Max: 20
320 Objekte - MS Min: 29, MS Max: 31
640 Objekte - MS Min: 49, MS Max: 53
1280 Objekte - MS Min: 77, MS Max: 100
1940 Objekte - MS Min: 122, MS Max: 236
2560 Objekte - MS Min: MAV, MS Max: MAV

Besser?

[Nachtrag]Ohne Debugger, 2D Off, 2 Lichter
Warbseite

aMul

Sieger des Minimalist Compo 01/13

BeitragSa, Jun 20, 2009 16:19
Antworten mit Zitat
Benutzer-Profile anzeigen
Nicht wirklich... Wenn, dann bräuchte ich schon einen Vergleich zwischen der alten und der neuen Version.
Von den Zahlen kann ich nur sagen, dass dein Rechner langsamer ist als meiner... keine wirklich nützliche Information. Mr. Green
Panic Pong - ultimate action mashup of Pong and Breakout <= aktives Spiele-Projekt, Downloads mit vielen bunten Farben!
advASCIIdraw - the advanced ASCII art program <= aktives nicht-Spiele-Projekt, must-have für ASCII/roguelike/dungeon-crawler fans!
Alter BB-Kram: ThroughTheAsteroidBelt - mit Quelltext! | RGB-Palette in 32²-Textur / Farbige Beleuchtung mit Dot3 | Stereoskopie in Blitz3D | Teleport-Animation Screensaver

Xaymar

ehemals "Cgamer"

BeitragSa, Jun 20, 2009 16:25
Antworten mit Zitat
Benutzer-Profile anzeigen
wenn ich die alte version hätte >_>[€dit]gefunden
naja ich schreib grade ein "addin" zur ms Messung für bis zu 1280 Objekten(oder mehr)

[Nachtrag #1]https://www.blitzforum.de/upload/file.php?id=5927
MS Liste des neuen Licht berechnung dingens

[Nachtrag #2]https://www.blitzforum.de/upload/file.php?id=5928
MS Liste des alten Licht berechnungs dingens

[Nachtrag #3]Messung mit folgenden code (neu):
Code: [AUSKLAPPEN]
Const SCRW = 800
Const SCRH = 600

Graphics3D SCRW, SCRH, 32, 2

SeedRnd MilliSecs()

Local camera, shadow.TShadow, shadow2.TShadow, ms
camera = CreateCamera() ; creates camera to render shadows
CameraClsColor camera, 255, 255, 255

shadow = CreateShadow() ; initializes shadow
shadow\light\radius = 300 ; set light radius
shadow\light\blu = 0 ; set light color to yellow

shadow2 = CreateShadow() ; initializes shadow
shadow2\light\radius = 300 ; set light radius
shadow2\light\red = 0 ; set light color to blue
shadow2\light\grn = 0

Local shadow1_tex, shadow2_tex, mesh, surface, v0, v1, v2, v3

shadow1_tex = CreateTexture(1024, 1024)
shadow2_tex = CreateTexture(1024, 1024)
TextureBlend shadow2_tex, 3

mesh = CreateMesh()
surface = CreateSurface(mesh)
PositionEntity mesh, - SCRW / 2 - 0.5, SCRH / 2 + 0.5, SCRW / 2
ScaleEntity mesh, 1, -1, 1
v0 = AddVertex(surface, 0, 0, 0)
v1 = AddVertex(surface, 0, 1024, 0)
v2 = AddVertex(surface, 1024, 1024, 0)
v3 = AddVertex(surface, 1024, 0, 0)
VertexTexCoords(surface, v0, 0, 0)
VertexTexCoords(surface, v1, 0, 1)
VertexTexCoords(surface, v2, 1, 1)
VertexTexCoords(surface, v3, 1, 0)
AddTriangle(surface, v0, v3, v2)
AddTriangle(surface, v0, v2, v1)

EntityFX mesh, 1 + 2
EntityBlend mesh, 2

EntityTexture mesh, shadow1_tex, 0, 0
EntityTexture mesh, shadow2_tex, 0, 1


;CreateRandomObjects() ; create some objects to cast shadows

SetBuffer BackBuffer()

Dim MST(1280)
SMS = 0

Local render2D = False

FPS = CreateTimer(60)
Repeat
   SMS = SMS + 1
   If SMS = 1281 Goto Final
   CreateRandomObjects(1)
   
   If MouseHit(2)
      render2D = 1 - render2D
   EndIf
   
   shadow\light\x = MouseX() ;set light position
   shadow\light\y = MouseY()
   shadow2\light\x = SCRW / 2 + Cos(MilliSecs() / 20) * 100 ;set light position
   shadow2\light\y = SCRH / 2 + Sin(MilliSecs() / 20) * 100

   ms = MilliSecs()
   DrawShadows(shadow) ; draw light and shadows
   RenderToTexture(shadow1_tex)
   ClearSurface shadow\surface
   DrawShadows(shadow2)
   RenderToTexture(shadow2_tex)
   ClearSurface shadow2\surface
   
   ms = MilliSecs() - ms
   MST(SMS) = ms
   
   RenderWorld ; render
   
   If render2D
      DrawWalls() ; draw walls(slow!)
      Oval shadow\light\x - 5, shadow\light\y - 5, 10, 10, 0
      Oval shadow2\light\x - 5, shadow2\light\y - 5, 10, 10, 0
   EndIf
   
   Text 0, 0, ms
   Text 0,15, SMS
   Flip 0
   WaitTimer FPS
Until KeyHit(1)

End

.Final
Stream = WriteFile("newms.txt")
For A = 1 To 1280
   WriteLine Stream, "Objekte "+A+" MS "+MST(A)
Next
CloseFile Stream

Function CreateRandomObjects(n = 50)
   ; creates n random light blocking objects
   Local i, j, x#, y#, a#, e, r#, wall.TWall
   For i = 1 To n
      x = Rnd(SCRW)
      y = Rnd(SCRH)
      a = Rnd(360)
      e = Rand(1, 4)
      e = (e = 1) * 3 + (e = 2) * 4 + (e = 3) * 6 + (e = 4) * 36
      r = Rnd(20, 50)
      For j = 1 To e
         CreateWall(x + Cos(a + j * 360 / e) * r, y + Sin(a + j * 360 / e) * r, x + Cos(a + (j + 1) * 360 / e) * r, y + Sin(a + (j + 1) * 360 / e) * r)
      Next
   Next
End Function

Function DrawWalls()
   ; draws walls
   ;! This function is very slow! Do not use for purposes other than testing or debugging!
   LockBuffer
   Local wall.TWall
   For wall = Each TWall
      Line wall\x1, wall\y1, wall\x2, wall\y2
   Next
   UnlockBuffer
End Function

Function RenderToTexture(texture)
   ; renders and than copies the backbuffer into the texturebuffer
   RenderWorld
   CopyRect 0, 0, SCRW, SCRH, 0, 0, BackBuffer(), TextureBuffer(texture)
End Function

;! ------------------------------------------------------------------------------- !;
;! ------------------------------- LIB STARTS HERE ------------------------------- !;
;! ------------------------------------------------------------------------------- !;


Type TWall
   ; a line that blocks light
   Field x1#
   Field y1#
   Field x2#
   Field y2#
End Type

Type TShadow
   ; a shadow object, needed to cast shadows
   Field mesh
   Field surface
   Field light.TLight
   Field red
   Field grn
   Field blu
   Field backface
End Type

Type TLight
   ; a light, needed to cast shadows
   Field x#
   Field y#
   Field radius#
   Field red
   Field grn
   Field blu
End Type

Function CreateWall.TWall(x1#, y1#, x2#, y2#)
   ; creates a TWall from (x1|y1) to (x2|y2)
   ; returns that TWall
   Local wall.TWall
   wall = New TWall
   wall\x1 = x1
   wall\y1 = y1
   wall\x2 = x2
   wall\y2 = y2
   Return wall
End Function

Function CreateShadow.TShadow(parent = 0, backfaceCulling = True, alignToCamera = True)
   ; creates a TShadow and TLight
   ; returns that TShadow
   ; parent: any entity or 0
   ; backfaceCulling: makes walls opaque from both sides
   ; alignToCamera: aligns shadows to the 2D coordinate system
   Local shadow.TShadow
   shadow = New TShadow
   shadow\mesh = CreateMesh(parent)
   shadow\surface = CreateSurface(shadow\mesh)
   EntityFX shadow\mesh, 1 + 2 + 16 * backfaceCulling
   ;EntityBlend shadow\mesh, 2
   shadow\backface = backfaceCulling
   shadow\light = New TLight
   shadow\light\radius = 100
   shadow\light\red = 255
   shadow\light\grn = 255
   shadow\light\blu = 255
   If alignToCamera
      AlignShadowMeshToCamera(shadow)
   EndIf
   Return shadow
End Function

Function AlignShadowMeshToCamera(shadow.TShadow, distance# = 100)
   ; aligns shadows to the 2D coordinate system
   ; shadow: TShadow to align
   ; distance: mesh and therefor triangle distance to the camera
   PositionEntity shadow\mesh, - distance / 2, distance * GraphicsHeight() / GraphicsWidth() / 2, distance / 2
   ScaleEntity shadow\mesh, distance / GraphicsWidth(), - distance / GraphicsWidth(), 1
End Function

Function DrawShadows(shadow.TShadow, drawLight = True, clear = True, LightDetail = 36)
  ; draws shadows
   ; shadow: TShadow to draw
   ; drawLight: draws light
   ; clear: clears previous shadows (highly recommended!)
   ; lightDetail: triangles used for light mesh
   Local wall.TWall, v0, v1, v2, v3
   Local lx#, ly#, lr#, nx#, ny#, d#
   Local x#, y#, x2#, y2#
   lx = shadow\light\x
   ly = shadow\light\y
   lr = shadow\light\radius
   If clear
      ClearSurface(shadow\surface)
   EndIf
   If drawLight
      DrawShadowLight(shadow, LightDetail)
   EndIf
   For wall = Each TWall
      If (wall\x1 > lx - lr Or wall\x2 > lx - lr) And (wall\x1 < lx + lr Or wall\x2 < lx + lr) And (wall\y1 > ly - lr Or wall\y2 > ly - lr) And (wall\y1 < ly + lr Or wall\y2 < ly + lr)
         nx = wall\y2 - wall\y1
         ny = wall\x1 - wall\x2
         d = Sqr(nx * nx + ny * ny)
         nx = nx / d
         ny = ny / d
         d = nx * (wall\x1 - lx) + ny * (wall\y1 - ly)
         If d > -lr And d < lr * shadow\backface
            x = (wall\x1 - lx) * 10000 + lx
            y = (wall\y1 - ly) * 10000 + ly
            x2 = (wall\x2 - lx) * 10000 + lx
            y2 = (wall\y2 - ly) * 10000 + ly
            v0 = AddVertex(shadow\surface, wall\x1, wall\y1, 0)
            v1 = AddVertex(shadow\surface, wall\x2, wall\y2, 0)
            v2 = AddVertex(shadow\surface, x, y, 0)
            v3 = AddVertex(shadow\surface, x2, y2, 0)
            VertexColor(shadow\surface, v0, shadow\red, shadow\grn, shadow\blu)
            VertexColor(shadow\surface, v1, shadow\red, shadow\grn, shadow\blu)
            VertexColor(shadow\surface, v2, shadow\red, shadow\grn, shadow\blu)
            VertexColor(shadow\surface, v3, shadow\red, shadow\grn, shadow\blu)
            AddTriangle(shadow\surface, v0, v3, v2)
            AddTriangle(shadow\surface, v0, v1, v3)
         EndIf
      EndIf
   Next
End Function

Function DrawShadowLight(shadow.TShadow, detail)
   ; draws light
   ; shadow: shadow which light is to be drawn
   ; detail: triangles used for light mesh
   Local i, v0, v1, v2, v3, v4, v5, v6
   v0 = AddVertex(shadow\surface, shadow\light\x, shadow\light\y, 0)
   VertexColor(shadow\surface, v0, shadow\light\red, shadow\light\grn, shadow\light\blu)
   v1 = AddVertex(shadow\surface, shadow\light\x + shadow\light\radius, shadow\light\y, 0)
   VertexColor(shadow\surface, v1, shadow\red, shadow\grn, shadow\blu)
   v4 = AddVertex(shadow\surface, shadow\light\x + 1000000, shadow\light\y, 0)
   VertexColor(shadow\surface, v4, shadow\red, shadow\grn, shadow\blu)
   v2 = v1
   v5 = v4
   For i = 1 To detail - 1
      v3 = AddVertex(shadow\surface, shadow\light\x + Cos(i * 360 / detail) * shadow\light\radius, shadow\light\y + Sin(i * 10) * shadow\light\radius, 0)
      v6 = AddVertex(shadow\surface, shadow\light\x + Cos(i * 360 / detail) * 1000000, shadow\light\y + Sin(i * 10) * 1000000, 0)
      VertexColor(shadow\surface, v3, shadow\red, shadow\grn, shadow\blu)
      VertexColor(shadow\surface, v5, shadow\red, shadow\grn, shadow\blu)
      AddTriangle(shadow\surface, v0, v2, v3)
      AddTriangle(shadow\surface, v2, v5, v6)
      AddTriangle(shadow\surface, v2, v6, v3)
      v2 = v3
      v5 = v6
   Next
   AddTriangle(shadow\surface, v0, v2, v1)
   
   AddTriangle(shadow\surface, v2, v4, v1)
   AddTriangle(shadow\surface, v2, v6, v4)
End Function


(alt)
Code: [AUSKLAPPEN]
Const SCRW = 800
Const SCRH = 600

Graphics3D SCRW, SCRH, 32, 2

SeedRnd MilliSecs()

Local camera, shadow.TShadow, shadow2.TShadow, ms
camera = CreateCamera() ; creates camera to render shadows
CameraClsColor camera, 255, 255, 255

shadow = CreateShadow() ; initializes shadow
shadow\light\radius = 300 ; set light radius
shadow\light\blu = 0 ; set light color to yellow

shadow2 = CreateShadow() ; initializes shadow
shadow2\light\radius = 300 ; set light radius
shadow2\light\red = 0 ; set light color to blue
shadow2\light\grn = 0

Local shadow1_tex, shadow2_tex, mesh, surface, v0, v1, v2, v3

shadow1_tex = CreateTexture(1024, 1024)
shadow2_tex = CreateTexture(1024, 1024)
TextureBlend shadow2_tex, 3

mesh = CreateMesh()
surface = CreateSurface(mesh)
PositionEntity mesh, - SCRW / 2 - 0.5, SCRH / 2 + 0.5, SCRW / 2
ScaleEntity mesh, 1, -1, 1
v0 = AddVertex(surface, 0, 0, 0)
v1 = AddVertex(surface, 0, 1024, 0)
v2 = AddVertex(surface, 1024, 1024, 0)
v3 = AddVertex(surface, 1024, 0, 0)
VertexTexCoords(surface, v0, 0, 0)
VertexTexCoords(surface, v1, 0, 1)
VertexTexCoords(surface, v2, 1, 1)
VertexTexCoords(surface, v3, 1, 0)
AddTriangle(surface, v0, v3, v2)
AddTriangle(surface, v0, v2, v1)

EntityFX mesh, 1 + 2
EntityBlend mesh, 2

EntityTexture mesh, shadow1_tex, 0, 0
EntityTexture mesh, shadow2_tex, 0, 1


;CreateRandomObjects() ; create some objects to cast shadows

SetBuffer BackBuffer()

Dim MST(1024)
SMS = 0

Local render2D = False

FPS = CreateTimer(60)
Repeat
   SMS = SMS + 1
   If SMS = 1025 Goto Final
   CreateRandomObjects(1)
   
   If MouseHit(2)
      render2D = 1 - render2D
   EndIf
   
   shadow\light\x = MouseX() ;set light position
   shadow\light\y = MouseY()
   shadow2\light\x = SCRW / 2 + Cos(MilliSecs() / 20) * 100 ;set light position
   shadow2\light\y = SCRH / 2 + Sin(MilliSecs() / 20) * 100

   ms = MilliSecs()
   DrawShadows(shadow) ; draw light and shadows
   RenderToTexture(shadow1_tex)
   ClearSurface shadow\surface
   DrawShadows(shadow2)
   RenderToTexture(shadow2_tex)
   ClearSurface shadow2\surface
   
   ms = MilliSecs() - ms
   MST(SMS) = ms
   
   RenderWorld ; render
   
   If render2D
      DrawWalls() ; draw walls(slow!)
      Oval shadow\light\x - 5, shadow\light\y - 5, 10, 10, 0
      Oval shadow2\light\x - 5, shadow2\light\y - 5, 10, 10, 0
   EndIf
   
   Text 0, 0, ms
   Text 0,15, SMS
   Flip 0
   WaitTimer FPS
Until KeyHit(1)

End

.Final
Stream = WriteFile("oldms.txt")
For A = 1 To 1024
   WriteLine Stream, "Objekte "+A+" MS "+MST(A)
Next
CloseFile Stream

Function CreateRandomObjects(n = 50)
   ; creates n random light blocking objects
   Local i, j, x#, y#, a#, e, r#, wall.TWall
   For i = 1 To n
      x = Rnd(SCRW)
      y = Rnd(SCRH)
      a = Rnd(360)
      e = Rand(1, 4)
      e = (e = 1) * 3 + (e = 2) * 4 + (e = 3) * 6 + (e = 4) * 36
      r = Rnd(20, 50)
      For j = 1 To e
         CreateWall(x + Cos(a + j * 360 / e) * r, y + Sin(a + j * 360 / e) * r, x + Cos(a + (j + 1) * 360 / e) * r, y + Sin(a + (j + 1) * 360 / e) * r)
      Next
   Next
End Function

Function DrawWalls()
   ; draws walls
   ;! This function is very slow! Do not use for purposes other than testing or debugging!
   LockBuffer
   Local wall.TWall
   For wall = Each TWall
      Line wall\x1, wall\y1, wall\x2, wall\y2
   Next
   UnlockBuffer
End Function

Function RenderToTexture(texture)
   ; renders and than copies the backbuffer into the texturebuffer
   RenderWorld
   CopyRect 0, 0, SCRW, SCRH, 0, 0, BackBuffer(), TextureBuffer(texture)
End Function

;! ------------------------------------------------------------------------------- !;
;! ------------------------------- LIB STARTS HERE ------------------------------- !;
;! ------------------------------------------------------------------------------- !;


Type TWall
   ; a line that blocks light
   Field x1#
   Field y1#
   Field x2#
   Field y2#
End Type

Type TShadow
   ; a shadow object, needed to cast shadows
   Field mesh
   Field surface
   Field light.TLight
   Field red
   Field grn
   Field blu
   Field backface
End Type

Type TLight
   ; a light, needed to cast shadows
   Field x#
   Field y#
   Field radius#
   Field red
   Field grn
   Field blu
End Type

Function CreateWall.TWall(x1#, y1#, x2#, y2#)
   ; creates a TWall from (x1|y1) to (x2|y2)
   ; returns that TWall
   Local wall.TWall
   wall = New TWall
   wall\x1 = x1
   wall\y1 = y1
   wall\x2 = x2
   wall\y2 = y2
   Return wall
End Function

Function CreateShadow.TShadow(parent = 0, backfaceCulling = True, alignToCamera = True)
   ; creates a TShadow and TLight
   ; returns that TShadow
   ; parent: any entity or 0
   ; backfaceCulling: makes walls opaque from both sides
   ; alignToCamera: aligns shadows to the 2D coordinate system
   Local shadow.TShadow
   shadow = New TShadow
   shadow\mesh = CreateMesh(parent)
   shadow\surface = CreateSurface(shadow\mesh)
   EntityFX shadow\mesh, 1 + 2 + 16 * backfaceCulling
   ;EntityBlend shadow\mesh, 2
   shadow\backface = backfaceCulling
   shadow\light = New TLight
   shadow\light\radius = 100
   shadow\light\red = 255
   shadow\light\grn = 255
   shadow\light\blu = 255
   If alignToCamera
      AlignShadowMeshToCamera(shadow)
   EndIf
   Return shadow
End Function

Function AlignShadowMeshToCamera(shadow.TShadow, distance# = 100)
   ; aligns shadows to the 2D coordinate system
   ; shadow: TShadow to align
   ; distance: mesh and therefor triangle distance to the camera
   PositionEntity shadow\mesh, - distance / 2, distance * GraphicsHeight() / GraphicsWidth() / 2, distance / 2
   ScaleEntity shadow\mesh, distance / GraphicsWidth(), - distance / GraphicsWidth(), 1
End Function

Function DrawShadows(shadow.TShadow, drawLight = True, clear = True, LightDetail = 36)
   ; draws shadows
   ; shadow: TShadow to draw
   ; drawLight: draws light
   ; clear: clears previous shadows (highly recommended!)
   ; lightDetail: triangles used for light mesh
   Local x#, x2#, y#, y2#, wall.TWall
   Local v0, v1, v2, v3
   If clear
      ClearSurface(shadow\surface)
   EndIf
   If drawLight
      DrawShadowLight(shadow, LightDetail)
   EndIf
   For wall = Each TWall
      x = (wall\x1 - shadow\light\x) * 10000 + shadow\light\x
      y = (wall\y1 - shadow\light\y) * 10000 + shadow\light\y
      x2 = (wall\x2 - shadow\light\x) * 10000 + shadow\light\x
      y2 = (wall\y2 - shadow\light\y) * 10000 + shadow\light\y
      v0 = AddVertex(shadow\surface, wall\x1, wall\y1, 0)
      v1 = AddVertex(shadow\surface, wall\x2, wall\y2, 0)
      v2 = AddVertex(shadow\surface, x, y, 0)
      v3 = AddVertex(shadow\surface, x2, y2, 0)
      VertexColor(shadow\surface, v0, shadow\red, shadow\grn, shadow\blu)
      VertexColor(shadow\surface, v1, shadow\red, shadow\grn, shadow\blu)
      VertexColor(shadow\surface, v2, shadow\red, shadow\grn, shadow\blu)
      VertexColor(shadow\surface, v3, shadow\red, shadow\grn, shadow\blu)
      AddTriangle(shadow\surface, v0, v2, v3)
      AddTriangle(shadow\surface, v0, v3, v1)
   Next
End Function
Function DrawShadowLight(shadow.TShadow, detail)
   ; draws light
   ; shadow: shadow which light is to be drawn
   ; detail: triangles used for light mesh
   Local i, v0, v1, v2, v3, v4, v5, v6
   v0 = AddVertex(shadow\surface, shadow\light\x, shadow\light\y, 0)
   VertexColor(shadow\surface, v0, shadow\light\red, shadow\light\grn, shadow\light\blu)
   v1 = AddVertex(shadow\surface, shadow\light\x + shadow\light\radius, shadow\light\y, 0)
   VertexColor(shadow\surface, v1, shadow\red, shadow\grn, shadow\blu)
   v4 = AddVertex(shadow\surface, shadow\light\x + 1000000, shadow\light\y, 0)
   VertexColor(shadow\surface, v4, shadow\red, shadow\grn, shadow\blu)
   v2 = v1
   v5 = v4
   For i = 1 To detail - 1
      v3 = AddVertex(shadow\surface, shadow\light\x + Cos(i * 360 / detail) * shadow\light\radius, shadow\light\y + Sin(i * 10) * shadow\light\radius, 0)
      v6 = AddVertex(shadow\surface, shadow\light\x + Cos(i * 360 / detail) * 1000000, shadow\light\y + Sin(i * 10) * 1000000, 0)
      VertexColor(shadow\surface, v3, shadow\red, shadow\grn, shadow\blu)
      VertexColor(shadow\surface, v5, shadow\red, shadow\grn, shadow\blu)
      AddTriangle(shadow\surface, v0, v2, v3)
      AddTriangle(shadow\surface, v2, v5, v6)
      AddTriangle(shadow\surface, v2, v6, v3)
      v2 = v3
      v5 = v6
   Next
   AddTriangle(shadow\surface, v0, v2, v1)
   
   AddTriangle(shadow\surface, v2, v4, v1)
   AddTriangle(shadow\surface, v2, v6, v4)
End Function
Warbseite

Lastmayday

BeitragSa, Jun 20, 2009 16:43
Antworten mit Zitat
Benutzer-Profile anzeigen
also bei 640 wänden ( max ) ist 35ms bei der neuen und 40ms beim alten. ( ohne linien zeichnen )

Tankbuster

BeitragSa, Jun 20, 2009 17:43
Antworten mit Zitat
Benutzer-Profile anzeigen
Wow, ich bin wirklich überrascht Smile

Hätte nicht gedacht, dass man da noch soviel raushohlen kann. Jetzt läuft es bei mir sogar mit 2 Lichtern flüssig Very Happy
Twitter
Download Jewel Snake!
Windows|Android

aMul

Sieger des Minimalist Compo 01/13

BeitragSa, Jun 20, 2009 18:09
Antworten mit Zitat
Benutzer-Profile anzeigen
Danke fürs Testen.
Allerdings bin ich verwundert, dass bei euch die Unterschiede so klein sind(im Verhältnis zu meinen Tests).
Wäre toll, wenn ihr noch posten könntet, was für CPU und GPU ihr habt.

@Tankbuster:
Ich habe schon eine Version geplant, bei der ein, oder auch zwei dutzend Lichter schneller sein sollten als zwei zurzeit. Wink
Panic Pong - ultimate action mashup of Pong and Breakout <= aktives Spiele-Projekt, Downloads mit vielen bunten Farben!
advASCIIdraw - the advanced ASCII art program <= aktives nicht-Spiele-Projekt, must-have für ASCII/roguelike/dungeon-crawler fans!
Alter BB-Kram: ThroughTheAsteroidBelt - mit Quelltext! | RGB-Palette in 32²-Textur / Farbige Beleuchtung mit Dot3 | Stereoskopie in Blitz3D | Teleport-Animation Screensaver

Tankbuster

BeitragSa, Jun 20, 2009 18:52
Antworten mit Zitat
Benutzer-Profile anzeigen
Also ich hab 1,8 Ghz,512MB Ram, meine Graka ist ne GeForce 7300 GS mit 256MB Speicher. Hat vor ein paar Monaten nur 30 € gekostet, also nichts großartiges.

Aber der Unterschied ist enorm. Der erste Code lief kaum flüssig. Das Licht hat immer hinter der Maus hergezogen. Ich hatte da ~5-10 FPS. Jetzt sieht aber alles flüssig aus, und läuft wie geschmiert (wortwörtlich)
Twitter
Download Jewel Snake!
Windows|Android

Xaymar

ehemals "Cgamer"

BeitragSa, Jun 20, 2009 19:17
Antworten mit Zitat
Benutzer-Profile anzeigen
amd sempron 2,2ghz + hidden core 800mhz(managed)
geforce 9500 gt 512mb
2048mb ram
Warbseite

Lastmayday

BeitragSa, Jun 20, 2009 19:21
Antworten mit Zitat
Benutzer-Profile anzeigen
Code: [AUSKLAPPEN]

------------------
System Information
------------------
Time of this report: 6/20/2009, 19:17:25
   Operating System: Windows XP Professional (5.1, Build 2600) Service Pack 3 (2600.xpsp_sp3_gdr.090206-1234)
           Language: German (Regional Setting: German)
System Manufacturer: System manufacturer
       System Model: System Product Name
               BIOS: Phoenix - AwardBIOS v6.00PG
          Processor: AMD Athlon(tm) Dual Core Processor 4850e,  MMX,  3DNow (2 CPUs), ~2.5GHz
             Memory: 2046MB RAM
          Page File: 445MB used, 3493MB available
        Windows Dir: F:\WINDOWS
    DirectX Version: DirectX 9.0c (4.09.0000.0904)
DX Setup Parameters: Not found
     DxDiag Version: 5.03.2600.5512 32bit Unicode

---------------
Display Devices
---------------
        Card name: ATI Radeon HD 3800 Series
     Manufacturer: ATI Technologies Inc.
        Chip type: ATI Radeon Graphics Processor (0x9501)
         DAC type: Internal DAC(400MHz)
       Device Key: Enum\PCI\VEN_1002&DEV_9501&SUBSYS_E620174B&REV_00
   Display Memory: 512.0 MB
     Current Mode: 1440 x 900 (32 bit) (75Hz)
          Monitor: Plug und Play-Monitor
  Monitor Max Res: 1600,1200
      Driver Name: ati2dvag.dll
   Driver Version: 6.14.0010.6869 (English)
      DDI Version: 9 (or higher)
Driver Attributes: Final Retail
 Driver Date/Size: 9/24/2008 04:17:07, 311296 bytes
      WHQL Logo'd: n/a
  WHQL Date Stamp: n/a
              VDD: Nicht zutreffend
         Mini VDD: ati2mtag.sys
    Mini VDD Date: 9/24/2008 05:09:07, 3331072 bytes
Device Identifier: {D7B71EE2-D641-11CF-2E6E-2AC6A1C2CB35}
        Vendor ID: 0x1002
        Device ID: 0x9501
        SubSys ID: 0xE620174B
      Revision ID: 0x0000
      Revision ID: 0x0000
      Video Accel: ModeMPEG2_C ModeMPEG2_D ModeWMV8_A ModeWMV9_A
         Registry: OK
     DDraw Status: Enabled
       D3D Status: Enabled
       AGP Status: Enabled
 

#Reaper

Newsposter

BeitragSa, Jun 20, 2009 19:36
Antworten mit Zitat
Benutzer-Profile anzeigen
Nett nett Very Happy

Meine Stats:
Ohne DrawWalls, Auflösung auf 1280*1024 in Fullscreen, kein Debugger, 1000 Objekte, ein Licht.
Beim alten Code ~16-17ms.
Beim neuen Code ~3ms.

Bei einer ATI Radeon X800 und AMD Athlon 64 3500+ (@ std. 2,2 Ghz)


PS: Ach ja, bei dem neuen Code funktioniert das mit den Lichtern in einem Objekt nicht mehr richtig..(?)
AMD Athlon 64 3500+, ATI AX800 Pro/TD, 2048 MB DRR 400 von Infineon, ♥RIP♥ (2005 - Juli 2015 -> sic!)
Blitz3D, BlitzMax, MaxGUI, Monkey X; Win7

aMul

Sieger des Minimalist Compo 01/13

BeitragSa, Jun 20, 2009 22:42
Antworten mit Zitat
Benutzer-Profile anzeigen
Ich bin wirklich überrascht, wie sehr die Geschwindigkeitsunterschiede doch von der Hardwarekonfiguration abhängen.
Das macht den Code zwar relativ unberechenbar, aber da es ja bei allen schneller wurde kann man den wohl erst einmal so lassen. Mr. Green

Noch einmal Danke fürs Testen.
Panic Pong - ultimate action mashup of Pong and Breakout <= aktives Spiele-Projekt, Downloads mit vielen bunten Farben!
advASCIIdraw - the advanced ASCII art program <= aktives nicht-Spiele-Projekt, must-have für ASCII/roguelike/dungeon-crawler fans!
Alter BB-Kram: ThroughTheAsteroidBelt - mit Quelltext! | RGB-Palette in 32²-Textur / Farbige Beleuchtung mit Dot3 | Stereoskopie in Blitz3D | Teleport-Animation Screensaver

Tankbuster

BeitragSa, Jun 20, 2009 23:01
Antworten mit Zitat
Benutzer-Profile anzeigen
Nja, bei den Guten PCs, gibts halt nicht so große Unterschiede, weil die mit der ersten Lösung auchnoch ziemlich gut vorran kamen. Und die Geschwindigkeit mit der das ganze abgearbeitet wird, ist ja exponentiell (oder wie das heißt), da es irgendwann garnichtmehr schneller geht.
Twitter
Download Jewel Snake!
Windows|Android

Gehe zu Seite Zurück  1, 2, 3, 4

Neue Antwort erstellen


Übersicht BlitzBasic Codearchiv

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group