procedurales chessboard image and figures

Share your advanced PureBasic knowledge/code with the community.
mpz
Enthusiast
Enthusiast
Posts: 497
Joined: Sat Oct 11, 2008 9:07 pm
Location: Germany, Berlin > member German forum

procedurales chessboard image and figures

Post by mpz »

Hello,

here comes a little piece of code. The first program creates a chessboard image, the second code the chess figures. Only the horse has no head, but you can use my mesh to Ogre program to import a horse head. I have made a little demo example with my 3d engine for windows. It was not possible for me to create this kind of program with ogre (not enough 3d functions for me). Perhaps somebody makes a complete chess program in ogre with this...

Greatings Michael


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

gruß Michael


Create chess image

Code: Select all

;////////////////////////////////////////////////////////////////
;//
;// 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

Create chess figures

Code: Select all

;////////////////////////////////////////////////////////////////
;//
;// 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 - MP3D Library - PB 5.73 version ready for download
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: procedurales chessboard image and figures

Post by luis »

Niiiiiiiiiiiiiiiiiiiiiiiiice!

I *hate" play chess with 3d chessboards (I play always in 2d) buy I like your work :)
"Have you tried turning it off and on again ?"
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: procedurales chessboard image and figures

Post by luis »

There is something weird tough, the pieces seems to drift a little on the left and then a little on the right on their squares while the chessboard is rotating...
"Have you tried turning it off and on again ?"
mpz
Enthusiast
Enthusiast
Posts: 497
Joined: Sat Oct 11, 2008 9:07 pm
Location: Germany, Berlin > member German forum

Re: procedurales chessboard image and figures

Post by mpz »

Hi Luis,

in this program the camera are moving and not the board. So i think the figures are a little bit over the board and perspective of the camera makes the "weird tough"

Code: Select all


count.f + 0.01
MP_PositionCamera (Camera, Sin(count) * 10, 5, Cos(count) * 10)
MP_CameraLookAt(Camera,MP_EntityGetX(brd),MP_EntityGetY(brd),MP_EntityGetZ(brd))

Greatings Michael
Working on - MP3D Library - PB 5.73 version ready for download
Post Reply