Prozedurales Schachmuster + Figuren

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Benutzeravatar
mpz
Beiträge: 505
Registriert: 14.06.2005 15:53
Computerausstattung: Win 11 Pro, 48 GB Ram, Intel I7 CPU und RX4070 Grafikkarte, PB (4/5) 6.12LT
Wohnort: Berlin, Tempelhof

Prozedurales Schachmuster + Figuren

Beitrag von mpz »

Hallo Leute,

ich habe mich gerade wieder etwas mit meinem Lieblingsthema Prozedurale Erzeugung von Grafiken und Körpern beschäftigt. Vielleicht kann der Eine oder Andere was damit anfangen. Das erste Programm erzeugt ein Schachbrettmuster, das andere Programm erzeugt die 3D Figuren. Bei der Pferdefigur allerdings nur den Rumpf, den Kopf kann man aus einem fertigen Muster erstellen (siehe dazu mein Mesh to Ogre Programm was ich mal reingestellt habe). Ich habe mit meiner 3D Engine ein kleines Windows Demo gemacht (70KB) damit man sieht was daraus werden kann. Das in Ogre umzusetzen hatte ich keine Zeit (und es fehlen mir zu viele 3D Befehle)...

http://rapidshare.de/files/48359541/MP_ ... o.exe.html

gruß Michael


Erzeuge Schachmuster

Code: Alles auswählen

;////////////////////////////////////////////////////////////////
;//
;// Project Title: MP 3D Engine Beispielprogramme konvertiert auf Ogre
;// Dateiname: MP_SchachBrett.pb
;// Erstellt am: 16.9.2009
;// Update am  : 
;// Author: Michael Paulwitz
;// 
;// Info: 
;// Procedurale Schachbrettgrafik mit Noise
;//
;//
;////////////////////////////////////////////////////////////////

;

ProcedureDLL MP_RandomInt(Min, Max) ; Erzeugt einen Zufallswert zwischen Min und Max 
  
  ProcedureReturn Min + Random(Max - Min) 

EndProcedure 

Width=512 : Height=512 : txp=9 : txcp=txp-6 : txcell= 1<< txcp-1

If OpenWindow(0, 100, 100, Width, Height, "PureBasic - Schachbrett")

  If CreateImage(0, Width, Height)

  StartDrawing(ImageOutput(0))

  For x=0 To 63
    ux=x << txcp
    For y=0 To 63
      uy=y << txcp
      col=1
      ; Definition of color of section
      If x>=4 And x <60 And y>=4 And y <60

        hups = Round(((x-4)/7), #PB_Round_Down) + Round(((y-4)/7), #PB_Round_Down)
        If hups & 1
          col=0
        EndIf
 
      ElseIf x>=3 And x <61 And y>=3 And y <61 
        col=0
      EndIf
 
      ;  A shading of section with addition of color noise
      For xx=ux To ux+txcell
        For yy=uy To uy+txcell
          FrontColor (RGB( MP_RandomInt(-8,8) +192*col+16, MP_RandomInt(-8,8) +128*col+16, MP_RandomInt(-8,8) +16)) ; nen bischen Noise
          Plot (xx, yy)
        Next
      Next  
    Next
  Next

  StopDrawing() ; 
    
  EndIf
  
  Repeat
    EventID = WaitWindowEvent()
    
    If EventID = #PB_Event_Repaint
      StartDrawing(WindowOutput(0))
        DrawImage(ImageID(0), 0, 0)
      StopDrawing()    
    EndIf
    
  Until EventID = #PB_Event_CloseWindow  ; If the user has pressed on the close button
  
EndIf

End   ; All the opened windows are closed automatically by PureBasic

Erzeuge Schachfiguren

Code: Alles auswählen

;////////////////////////////////////////////////////////////////
;//
;// Project Title: MP 3D Engine Beispielprogramme konvertiert auf Ogre
;// Dateiname: MP_Schachfiguren.pb
;// Erstellt am: 16.9.2009
;// Update am  : 
;// Author: Michael Paulwitz
;// 
;// Info: 
;// Procedurale Schachfiguren 
;//
;//
;////////////////////////////////////////////////////////////////


Global stp.f=0.001, sen.f=4, maxr.f=30
Global Dim a.f(1),Dim b.f(1),Dim c.f(1),Dim d.f(1),Dim oc.f(1)
Global advert , adtri, *MemoryID, *MemoryID2

#Mesh1 = 0
#Mesh2 = 1
#Mesh3 = 2
#Mesh4 = 3
#Mesh5 = 4
#Mesh6 = 5

Procedure vertexes(s)

  vx.f=1*(400-oc(0))/300
  vy.f=1*(600-oc(1))/300
  r1=Abs(400-oc(0))

  If sen.f>=r1*2 
    r1=1

    ;MP_AddVertex (s, 0, vy.f, 0,0,0)
    PokeF (*MemoryID + advert*36 + 4, vy)  ; Setze y, x und z = 0
    PokeL (*MemoryID + advert*36 + 24, $FFFFFFFF) ; Color Weiss      
    Color.l = $FFFFFF
    advert + 1

  Else
    
    r1=Round (6.2831/ACos(1.0-sen.f/r1), #PB_Round_Up)
    ang.f=0
    dang.f=360.0/r1
    Repeat

      ;MP_AddVertex ( s, Cos(ang* 0.017453)*vx, vy, Sin(ang* 0.017453)*vx,0,0)
      PokeF (*MemoryID + advert*36 , Cos(ang* 0.017453)*vx)    ; Setze x
      PokeF (*MemoryID + advert*36 + 4, vy)                    ; Setze y
      PokeF (*MemoryID + advert*36 + 8, Sin(ang* 0.017453)*vx) ; Setze z
      PokeL (*MemoryID + advert*36 + 24, $FFFFFFFF) ; Color Weiss
      advert + 1
      
      ang  + dang
      
      If ang>359.9
        Break 
      EndIf
    ForEver
  EndIf

  ProcedureReturn r1

EndProcedure

If InitEngine3D() And InitSprite() And InitKeyboard()
  OpenWindow(0,0,0,640,480,"3D Mesh TEST  - Procedurale Schach Figuren",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)
 
  Restore NumericalData

 For n1=0 To 5
  Read.i advert1 : Read.i adtri1
  Read.i q : Read.i x2 : Read.i y2 : Read.i dx21 : Read.i dy21 : Read.i dx22 : Read.i  dy22
 
  *MemoryID = AllocateMemory(advert1 * 36)
  *MemoryID2 = AllocateMemory(adtri1 * 6)
 
  advert = 0
  adtri = 0
 
  rbeg=0

 
 For n2=2 To q
    x1=x2:y1=y2:dx11=dx21:dy11=dy21:dx12=dx22:dy12=dy22
    Read.i x2 : Read.i y2: Read.i dx21: Read.i dy21: Read.i dx22: Read.i dy22

    r.f =0.05 * Sqr((x1-x2)*(x1-x2) +(y1-y2)*(y1-y2))
    For n3=0 To 1
      If n3 
        ux1=y1
        ux2=y2
        c(n3) =r*dy12
        dy2.f=r*dy21
      Else
        ux1=x1
        ux2=x2
        c(n3) =r*dx12
        dy2=r*dx21
      EndIf
      d(n3) = ux1
      b(n3) =3.0*ux2-dy2.f-2.0*c(n3)-3.0*d(n3)
      a(n3) =(dy2-2*b(n3)-c(n3))/3.0
    Next

    t.f=0
    tt.f=0
    oc(0) =d(0)
    oc(1) =d(1)
    If n2=2 
       r1=vertexes(n1)
    EndIf   

    Repeat
      x=oc(0)
      y=oc(1)
      la.f=-3.0*a(1)*tt-2.0*b(1)*t-c(1)
      lb.f=3.0*a(0)*tt+2.0*b(0)*t+c(0)
      If la=0 And lb=0
        la=y1-y2
        lb=x2-x1
      EndIf
      lc.f=-la*x-lb*y
      sen2=Sqr(la*la+lb*lb)*sen
      Repeat

        t=t+stp
        If t>1 
          t=1
        EndIf
        tt.f=t*t
        For n3=0 To 1
          oc(n3) =a(n3)*tt*t+b(n3)*tt+c(n3)*t+d(n3)
        Next
        If Sqr((x-oc(0))*(x-oc(0)) +(y-oc(1))*(y-oc(1))) > maxr
           Break
        EndIf
      Until t=1 Or Abs(la*oc(0) +lb*oc(1) +lc) >= sen2
      
      r2=vertexes(n1)

      rbeg2=rbeg+r1
      r10=0
      r20=0
      Repeat
      
      If(r10+1)*r2 <(r20+1)*r1 
        r10=r10+1
        r11=r12
        r12=r10 % r1
        
        PokeW (*MemoryID2 + adtri*6, rbeg2+r22) 
        PokeW (*MemoryID2 + adtri*6+2, rbeg+r11) 
        PokeW (*MemoryID2 + adtri*6+4, rbeg+r12) 
        
        adtri + 1

      Else
        r20=r20+1
        r21=r22
        r22=r20 % r2

        PokeW (*MemoryID2 + adtri*6, rbeg+r12) 
        PokeW (*MemoryID2 + adtri*6+2, rbeg2+r22) 
        PokeW (*MemoryID2 + adtri*6+4, rbeg2+r21) 
        
        adtri + 1

      EndIf
            
      Until r12=0 And r22=0

    rbeg=rbeg2
    r1=r2
    If t=1 : Break : EndIf
 ForEver

 Next

 CreateMesh(n1, advert1)
 SetMeshData(n1, #PB_Mesh_Vertex | #PB_Mesh_Normal | #PB_Mesh_Color  | #PB_Mesh_UVCoordinate,  *MemoryID, advert1)
 SetMeshData(n1, #PB_Mesh_Face,  *MemoryID2, adtri1)
 CreateEntity(n1, MeshID(n1),#PB_Material_None)
 
Next

 FreeMemory(*MemoryID) 
 FreeMemory(*MemoryID2)

 CreateCamera(0, 0, 0, 100, 100)
 z+8
 CameraLocate(0,0,0,z)
 
 MoveEntity(#Mesh1, -2.5, 0, 0)
 MoveEntity(#Mesh2, -1.5, 0, 0)
 MoveEntity(#Mesh3, -0.5,0,0)
 MoveEntity(#Mesh4, 0.5,0,0)
 MoveEntity(#Mesh5, 1.5,0,0)
 MoveEntity(#Mesh6, 2.5,0,0)
 
    Repeat
      ExamineKeyboard()
      Select WindowEvent()
        Case #PB_Event_CloseWindow
          Quit = #True
      EndSelect
 
      If KeyboardPushed(#PB_Key_A) ; Key A = Zoom +
         z-1
         CameraLocate(0,0,0,z)
      EndIf
 
      If KeyboardPushed(#PB_Key_Z) ; Key Z = Zoom +
         z+1
         CameraLocate(0,0,0,z)
      EndIf
 
      If KeyboardPushed(#PB_Key_Y) ; Key Y = Zoom +
         z+1
         CameraLocate(0,0,0,z)
      EndIf

      RotateEntity(#Mesh1, 0.2, 0.2, 0.2, #PB_Relative)
      RotateEntity(#Mesh2, 0.2, 0.2, 0.2, #PB_Relative)
      RotateEntity(#Mesh3, 0.2, 0.2, 0.2, #PB_Relative)
      RotateEntity(#Mesh4, 0.2, 0.2, 0.2, #PB_Relative)
      RotateEntity(#Mesh5, 0.2, 0.2, 0.2, #PB_Relative)
      RotateEntity(#Mesh6, 0.2, 0.2, 0.2, #PB_Relative)
 
      RenderWorld()
      FlipBuffers()
 
    Until KeyboardPushed(#PB_Key_Escape) Or Quit
Else
  MessageRequester("Error", "Cant init DirectX 3D Engine",0)
EndIf
 
End

; Data for plotting:
DataSection
    NumericalData:

; 0-König/King
Data.i 868 , 1733
Data.i 24
Data.i 400,0,0,0,0,0
Data.i 391,0,0,0,0,0
Data.i 391,26,0,0,0,0
Data.i 377,26,0,0,0,0
Data.i 377,36,0,0,0,0
Data.i 391,36,0,0,0,0
Data.i 391,62,0,0,0,0
Data.i 352,79,-5,31,0,1
Data.i 331,74,0,0,0,0
Data.i 355,162,0,0,-53,0
Data.i 355,177,53,0,0,0
Data.i 355,190,0,0,-53,0
Data.i 355,205,53,0,0,0
Data.i 326,205,0,0,-69,0
Data.i 326,219,69,0,0,0
Data.i 373,219,0,0,0,0
Data.i 341,462,-10,28,0,0
Data.i 351,473,0,0,-33,14
Data.i 295,573,0,28,0,0
Data.i 305,573,0,0,0,0
Data.i 305,584,0,0,0,0
Data.i 295,584,0,0,0,0
Data.i 295,599,0,0,0,0
Data.i 400,599,0,0,0,0

; 1-Königin/Queen
Data.i 831 , 1659
Data.i 20
Data.i 400,45,0,0,0,0
Data.i 380,53,0,0,0,0
Data.i 391,62,0,0,0,0
Data.i 352,79,-5,31,0,1
Data.i 331,74,0,0,30,39
Data.i 355,162,0,0,-53,0
Data.i 355,177,53,0,0,0
Data.i 355,190,0,0,-53,0
Data.i 355,205,53,0,0,0
Data.i 326,205,0,0,-69,0
Data.i 326,219,69,0,0,0
Data.i 373,219,0,0,0,0
Data.i 341,462,-10,28,0,0
Data.i 351,473,0,0,-33,14
Data.i 295,573,0,28,0,0
Data.i 305,573,0,0,0,0
Data.i 305,584,0,0,0,0
Data.i 295,584,0,0,0,0
Data.i 295,599,0,0,0,0
Data.i 400,599,0,0,0,0

; 2-Läufer/bishop
Data.i 641 , 1278
Data.i 18
Data.i 400,84,0,0,-46,1
Data.i 392,107,24,8,6,13
Data.i 369,162,-29,33,-29,33
Data.i 366,253,0,0,-53,0
Data.i 366,266,53,0,0,0
Data.i 366,292,0,0,-53,0
Data.i 366,304,53,0,0,0
Data.i 346,304,0,0,-69,0
Data.i 346,317,69,0,0,0
Data.i 380,317,0,0,0,0
Data.i 352,497,-10,28,0,0
Data.i 359,505,0,0,-19,15
Data.i 309,577,-5,20,0,0
Data.i 321,577,0,0,0,0
Data.i 321,586,0,0,0,0
Data.i 309,586,0,0,0,0
Data.i 309,599,0,0,0,0
Data.i 400,599,0,0,0,0

; 3-Pferd/Horse aber nur unterer Teil
Data.i 285 , 567
Data.i 9
Data.i 400,475,0,0,0,0
Data.i 333,475,0,0,0,0
Data.i 345,493,0,0,0,0
Data.i 297,578,-2,20,0,0
Data.i 310,578,0,0,0,0
Data.i 310,586,0,0,0,0
Data.i 297,586,0,0,0,0
Data.i 297,599,0,0,0,0
Data.i 400,599,0,0,0,0

; 4-Turm/Tower
Data.i 582 , 1160
Data.i 20
Data.i 400,181,0,0,0,0
Data.i 332,181,0,0,0,0
Data.i 332,161,0,0,0,0
Data.i 316,161,0,0,0,0
Data.i 316,193,0,0,0,0
Data.i 332,193,0,0,0,0
Data.i 332,215,0,0,0,0
Data.i 344,215,0,0,0,0
Data.i 344,238,0,0,0,0
Data.i 359,238,0,0,0,0
Data.i 359,268,0,0,0,0
Data.i 346,454,0,0,0,0
Data.i 333,460,0,0,0,0
Data.i 342,473,0,0,0,0
Data.i 309,577,-2,20,0,0
Data.i 321,577,0,0,0,0
Data.i 321,586,0,0,0,0
Data.i 309,586,0,0,0,0
Data.i 309,599,0,0,0,0
Data.i 400,599,0,0,0,0

; 5-Bauer/Pawn
Data.i 456 , 908
Data.i 13
Data.i 400,209,0,0,-37,0
Data.i 375,283,31,11,0,0
Data.i 339,334,0,0,0,0
Data.i 382,334,0,0,-1,46
Data.i 355,501,0,0,0,0
Data.i 339,498,0,0,0,0
Data.i 346,512,0,0,0,0
Data.i 309,577,-2,20,0,0
Data.i 321,577,0,0,0,0
Data.i 321,586,0,0,0,0
Data.i 309,586,0,0,0,0
Data.i 309,599,0,0,0,0
Data.i 400,599,0,0,0,0

EndDataSection


Working on :lol: - LibSGD - MP3D Engine - 8)
Benutzeravatar
gnasen
Beiträge: 578
Registriert: 01.08.2007 14:28
Computerausstattung: PB 4.60

Re: Prozedurales Schachmuster + Figuren

Beitrag von gnasen »

Sehr interessant, aber sicher keine leichte Kost für zwischendurch.
Was mich allerdings am meisten begeistert hat ist die sehr geringe Systemauslastung deines Beispielprogrammes. Während PBs Ogre Implementierung den Drang hat einen ganzen Kern alleine zu vernaschen, ist die Auslastung des Programmes mit 0%-1% äußerst erfreulich.
Auf welcher Engine basiert eigentlich deine MP3D Engine? Oder ist das ganze eine Marke Eigenbau?
pb 4.51
Benutzeravatar
mpz
Beiträge: 505
Registriert: 14.06.2005 15:53
Computerausstattung: Win 11 Pro, 48 GB Ram, Intel I7 CPU und RX4070 Grafikkarte, PB (4/5) 6.12LT
Wohnort: Berlin, Tempelhof

Re: Prozedurales Schachmuster + Figuren

Beitrag von mpz »

Hi,

Meine Engine basiert auf DX9 und ist Eigenbau. Mit derzeit 250 x 3d Befehlen allerdings auch kein Leichtbau mehr. Die exe Dateien werden aber trotzdem angenehm klein...

Gruß Michael
Working on :lol: - LibSGD - MP3D Engine - 8)
Antworten