Matrix

Share your advanced PureBasic knowledge/code with the community.
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Matrix

Post by Comtois »

You need an image , i use this one

Image


Code: Select all

;******************************************* 
;** Comtois ** 15/11/03 ** Matrice / Vagues  ** 
;******************************************* 

;-Initialisation 
#ScreenWidth = 800 : #ScreenHeight = 600 : #ScreenDepth = 32 
If InitEngine3D()=0 
   MessageRequester("Erreur", "Impossible d'initialiser la 3D , Vérifiez la présence de la DLL Engine3D.dll", 0) 
   End 
ElseIf InitSprite() = 0 Or InitKeyboard() = 0 
   MessageRequester("Erreur", "Impossible d'initialiser DirectX 7 Ou plus", 0) 
   End 
ElseIf OpenScreen( #ScreenWidth , #ScreenHeight , #ScreenDepth , "DemoMatrice" ) = 0 
   MessageRequester( "Erreur" , "Impossible d'ouvrir l'écran " , 0 ) 
   End 
EndIf 


;-Variables Globales 
Global AngleVague.f ,vitesse.f ,decaleX.f ,decaleZ.f ,hauteur.f 
AngleVague = Random(360) 
vitesse = 3.0 
decaleX = 23 
decaleZ = 0 
hauteur = 4 

;- Declaration des procédures 
Declare Matrice(NbpointsX.l,NbpointsZ.l) 

;-Constantes 
Enumeration 
   #PointID 
   #TriangleID 
   #TextureID 
EndEnumeration 

#NbX= 30 ; nombre de facettes 
#NbZ= 30 ; nombre de facettes 

;-Mémoires Mesh 
AllocateMemory(#PointID    , 12 * (#NbX + 1 )* (#NbZ + 1 ) , 0) 
AllocateMemory(#TriangleID , 12 * #NbX * #NbZ * 4          , 0) 
AllocateMemory(#TextureID  , 12 * (#NbX + 1 )* (#NbZ + 1 ) , 0) 
Matrice(#NbX , #NbZ) 

;-Mesh 
CreateMesh(0) 
SetMeshData(0, 0, UseMemory(#PointID)    , (#NbX + 1 )* (#NbZ + 1 )) 
SetMeshData(0, 1, UseMemory(#TriangleID) , (#NbX ) * (#NbZ ) * 4) 
SetMeshData(0, 2, UseMemory(#TextureID)  , (#NbX + 1 )* (#NbZ + 1 )) 

;-Texture 
UsePNGImageDecoder() 
;LoadTexture(0,"purebasiclogoNew.png") ; <<< pourquoi ça plante quand je mets juste ça ? 
LoadImage(0,"purebasiclogoNew.png")    ; alors que l'image se charge ? Je verrai ça plus tard 
CreateTexture(0,256,256) 
StartDrawing(TextureOutput(0)) 
DrawImage(UseImage(0),0,0) 
DrawingMode(4) 
Box(1,1,254,254,RGB(255,255,255)) 
StopDrawing() 


;- MAterial 
CreateMaterial(0,TextureID(0)) 
MaterialFilteringMode(0 , #PB_Material_Trilinear ) 

;-Entity 
CreateEntity(0, MeshID(0), MaterialID(0)) 
RotateEntity(0,0,45,0) 
ScaleEntity(0, 10, 10, 10) 

;-Camera 
CreateCamera(0, 0, 0, 100, 100) 
CameraLocate(0,0,350,350) 
CameraLookAt(0,0,0,0) 

;-Procédures 
Procedure Matrice(FX.l,FZ.l) 
   
   adresse=UseMemory(#PointID) 
   For b=0 To FZ 
      For a=0 To FX 
         PokeF(adresse, a - FX/2) : PokeF(adresse + 4, 0 ) : PokeF(adresse + 8, b - FZ/2)  
         ;PokeF(adresse, a ) : PokeF(adresse + 4, 0 ) : PokeF(adresse + 8, b ) 
         adresse + 12 
      Next a 
   Next b 
   
   adresse=UseMemory(#TriangleID) 
   Nb = FX + 1 
   For b=0 To FZ - 1 
      For a=0 To FX - 1 
         P1 = a + (b * Nb) 
         P2 = a + 1 + (b * Nb) 
         P3 = a + ((b + 1) * Nb) 
         P4 = a + 1 + ((b + 1) * Nb) 
         PokeW(adresse     , P3) : PokeW(adresse +  2, P2) : PokeW(adresse +  4, P1) 
         PokeW(adresse +  6, P2) : PokeW(adresse +  8, P3) : PokeW(adresse + 10, P4)    
         PokeW(adresse + 12, P1) : PokeW(adresse + 14, P2) : PokeW(adresse + 16, P3) 
         PokeW(adresse + 18, P4) : PokeW(adresse + 20, P3) : PokeW(adresse + 22, P2)            
         adresse + 24 
      Next a 
   Next b 
   
   adresse=UseMemory(#TextureID) 
   For b=0 To FZ 
      For a=0 To FX 
         Px.f = a/FX 
         Pz.f = b/FZ 
         PokeF(adresse, Px) : PokeF(adresse + 4, Pz)    
         adresse + 8 
      Next a 
   Next b 
   
EndProcedure  

Procedure.f wrapvalue( angle.f ) 
   ;Permet de toujours avoir un angle compris entre 0° et 360° 
   While angle < 0 
      angle + 360 
   Wend 
   While angle - 360 >= 0 
      angle - 360 
   Wend 
   ProcedureReturn angle 
EndProcedure 

Procedure.f Cosd( angle.f ) 
   ;calcule le cos d'un angle en degré 
   a.f = angle * 0.0174533 
   ProcedureReturn Cos( a ) 
EndProcedure 

Procedure.f Sind( angle.f ) 
   ;calcule le sin d'un angle en degré 
   a.f = angle  * 0.0174533 
   ProcedureReturn Sin( a ) 
EndProcedure 

Procedure vagues() 
   ; Modification sur l'axe des Y 
   adresse = UseMemory(#PointID) + 4 
   For z = 0 To #NbZ 
      For x = 0 To #NbX 
         Sommet.f = Sind(AngleVague + (x * decaleX) + (z * decaleZ)) * hauteur 
         PokeF(adresse, Sommet) 
         adresse + 12 
      Next x 
   Next z 
   SetMeshData(0, 0, UseMemory(#PointID) , (#NbX + 1 )* (#NbZ + 1 )) 
EndProcedure 

Procedure AffAide() 
   StartDrawing(ScreenOutput()) 
   DrawingMode(1) 
   FrontColor(255,255,255) 
   Locate(0,0) 
   DrawText("[F1] / [F2] => Change Mode affichage") 
   Locate(0,20) 
   DrawText("[PageUp] / [PageDown] => Hauteur : " + StrF(hauteur)) 
   Locate(0,40) 
   DrawText("[Flèche Haut] / [Flèche bas] => DecaleZ : " + Str(decaleZ)) 
   Locate(0,60) 
   DrawText("[Flèche Gauche] / [Flèche droite] => DecaleX : " + Str(decaleX)) 
   StopDrawing() 
EndProcedure 

;-Boucle principale 

Repeat 
   
   ClearScreen(0, 0, 0) 
   ExamineKeyboard() 
   AffAide() 
   If KeyboardReleased(#PB_Key_F1) :ClearScreen(0, 0, 0): CameraRenderMode(0, #PB_Camera_Wireframe)  : EndIf 
   If KeyboardReleased(#PB_Key_F2) :ClearScreen(0, 0, 0): CameraRenderMode(0, #PB_Camera_Textured)  : EndIf 
   
   If KeyboardReleased(#PB_Key_PageUp) : hauteur + 0.5  : EndIf 
   If KeyboardReleased(#PB_Key_PageDown) : hauteur - 0.5  : EndIf 
   
   If KeyboardReleased(#PB_Key_Up) : decaleZ + 1  : EndIf 
   If KeyboardReleased(#PB_Key_Down) : decaleZ - 1  : EndIf 
   If KeyboardReleased(#PB_Key_Left) : decaleX - 1  : EndIf 
   If KeyboardReleased(#PB_Key_Right) : decaleX + 1  : EndIf 
   
   AngleVague = wrapvalue(AngleVague + vitesse) 
   vagues() 
   RotateEntity(0,0,0,0.1) 
   RenderWorld() 
   FlipBuffers() 
   
Until KeyboardPushed(#PB_Key_Escape)
Last edited by Comtois on Sun Nov 16, 2003 5:20 pm, edited 3 times in total.
cykotic
User
User
Posts: 24
Joined: Wed Nov 12, 2003 6:24 am
Location: Canada
Contact:

Post by cykotic »

very cool!
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

nice!
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
coma
Enthusiast
Enthusiast
Posts: 164
Joined: Fri Aug 15, 2003 3:46 am
Location: Canada

Post by coma »

Code: Select all

;LoadTexture(0,"purebasiclogoNew.png") ; <<< pourquoi ça plante quand je mets juste ça ? 
=> seems that loadtexture() don't support PNG 256 colors.
Jose
User
User
Posts: 34
Joined: Sat Apr 26, 2003 9:20 pm

Post by Jose »

Very Cool 8O
Skipsy
User
User
Posts: 98
Joined: Wed Apr 30, 2003 12:26 pm
Location: France

Post by Skipsy »

Very Nice...


(Laptop P4 1.8 Gb. Graph = mobility Radeon 7500)
Beware of the man who has the solution before he understands the problem...
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

That's great. 8O


Your wrapvalue() function should be dangerous for very big or very small values, because it is based in loops.

If you need speed i have replaced with mine (which is in ASM and isn't based in any loop, only obtaining the modulo), using:
"wrapvalue(AngleVague + vitesse,0,360)". Recently updated to be "perfect", and Just for speed :wink:


How can be managed the rotation speed?
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

I like it, so i added some stuff and modified somethings.

Code: Select all

;*******************************************
;** Comtois ** 15/11/03 ** Matrice / Vagues  **
;*******************************************
;Modifications and additions by Psychophanta

;-Initialisation
#ScreenWidth=800:#ScreenHeight=600:#ScreenDepth=32
If InitEngine3D()=0 Or InitSprite()=0 Or InitKeyboard()=0 Or OpenScreen(#ScreenWidth,#ScreenHeight,#ScreenDepth,"DemoMatrice")=0
  MessageRequester("Error","Something fails to open Screen for 3D",0)
  End
EndIf

;-Constantes
Enumeration
  #PointID
  #TriangleID
  #TextureID
  #NbX=30 ; nombre de facettes
  #NbZ=30 ; nombre de facettes
EndEnumeration
#DegConv=3.14159265/180

;-Variables Globales
Global AngleVague.f,WaveFrequency.f,WavePeriodX.f,WavePeriodZ.f,WaveAmplitude.f
Global xrot.f,yrot.f,zrot.f
Global CamLocateX.l,CamLocateY.l,CamLocateZ.l,CamLookAtX.l,CamLookAtY.l,CamLookAtZ.l
Global Mode.b
circle.l=360
AngleVague=Random(circle)
WaveFrequency=10;=waves/second
WavePeriodX=5;=1/Wave lenght
WavePeriodZ=17;=1/Wave lenght
WaveAmplitude=3
xrot=-0.6:yrot=-0.8:zrot=0.2
CamLocateX.l=0:CamLocateY.l=0:CamLocateZ.l=50
CamLookAtX.l=0:CamLookAtY.l=0:CamLookAtZ.l=0

;-Procédures
Procedure Matrice(FX.l,FZ.l)
  adresse=UseMemory(#PointID)
  For b=0 To FZ
    For a=0 To FX
      PokeF(adresse,a-FX/2):PokeF(adresse+4,0):PokeF(adresse+8,b-FZ/2)
      adresse+12
    Next
  Next

  adresse=UseMemory(#TriangleID)
  Nb=FX+1
  For b=0 To FZ-1
    For a=0 To FX-1
      P1=a+(b*Nb)
      P2=P1+1
      P3=a+(b+1)*Nb
      P4=P3+1
      PokeW(adresse,P3):PokeW(adresse+2,P2):PokeW(adresse+4,P1)
      PokeW(adresse+6,P2):PokeW(adresse+8,P3):PokeW(adresse+10,P4)
      PokeW(adresse+12,P1):PokeW(adresse+14,P2):PokeW(adresse+16,P3)
      PokeW(adresse+18,P4):PokeW(adresse+20,P3):PokeW(adresse+22,P2)        
      adresse+24
    Next
  Next

  adresse=UseMemory(#TextureID)
  For b=0 To FZ
    For a=0 To FX
      PokeF(adresse,a/FX):PokeF(adresse+4,b/FZ) 
      adresse+8
    Next
  Next
EndProcedure

Procedure vagues()
  ; Modification sur l'axe des Y
  adresse=UseMemory(#PointID)+4
  For z=0 To #NbZ
    For x=0 To #NbX
      Sommet.f=Sin(#DegConv*(AngleVague+x*WavePeriodX+z*WavePeriodZ))*WaveAmplitude
      PokeF(adresse,Sommet)
      adresse+12
    Next
  Next
  SetMeshData(0,0,UseMemory(#PointID),(#NbX+1)*(#NbZ+1))
EndProcedure

Procedure ShowTextAndKeyTest()
  StartDrawing(ScreenOutput())
  DrawingMode(1)
  FrontColor(20,180,115)
  Locate(0,0)
  DrawText("[F1] => Toggle Mode affichage")
  Locate(0,20)
  DrawText("[PageUp] / [PageDown] => Wave Amplitude : "+StrF(WaveAmplitude))
  Locate(0,40)
  DrawText("[Up Arrow] / [Down Arrow] => Wave Period on Z axis : "+Str(WavePeriodZ))
  Locate(0,60)
  DrawText("[Right Arrow] / [Left Arrow] => Wave Period on X axis : "+Str(WavePeriodX))
  Locate(0,80)
  DrawText("[Home key] / [End key] => Wave speed : "+Str(WaveFrequency))
  Locate(0,100)
  DrawText("[F2] / [Shift+F2] => X rotation speed : "+StrF(xrot))
  Locate(0,120)
  DrawText("[F3] / [Shift+F3] => Y rotation speed : "+StrF(yrot))
  Locate(0,140)
  DrawText("[F4] / [Shift+F4] => Z rotation speed : "+StrF(zrot))
  Locate(0,160)
  DrawText("[F5] / [Shift+F5] => X Camera location : "+Str(CamLocateX))
  Locate(0,180)
  DrawText("[F6] / [Shift+F6] => Y Camera location : "+Str(CamLocateY))
  Locate(0,200)
  DrawText("[F7] / [Shift+F7] => Z Camera location : "+Str(CamLocateZ))
  Locate(0,220)
  DrawText("[F8] / [Shift+F8] => X Camera look at : "+Str(CamLookAtX))
  Locate(0,240)
  DrawText("[F9] / [Shift+F9] => Y Camera look at : "+Str(CamLookAtY))
  Locate(0,260)
  DrawText("[F10] / [Shift+F10] => Z Camera look at : "+Str(CamLookAtZ))
  StopDrawing()
  If KeyboardReleased(#PB_Key_F1)
    If Mode.b:Mode=0:CameraRenderMode(0,#PB_Camera_Textured):Else:Mode=1:CameraRenderMode(0,#PB_Camera_Wireframe):EndIf
  EndIf
  If KeyboardReleased(#PB_Key_PageUp):WaveAmplitude+0.1:EndIf
  If KeyboardReleased(#PB_Key_PageDown):WaveAmplitude-0.1:EndIf
  If KeyboardReleased(#PB_Key_Up):WavePeriodZ+1:EndIf
  If KeyboardReleased(#PB_Key_Down):WavePeriodZ-1:EndIf
  If KeyboardReleased(#PB_Key_Left):WavePeriodX-1:EndIf
  If KeyboardReleased(#PB_Key_Right):WavePeriodX+1:EndIf
  If KeyboardReleased(#PB_Key_Home):WaveFrequency+1:EndIf
  If KeyboardReleased(#PB_Key_End):WaveFrequency-1:EndIf
  
  If KeyboardPushed(#PB_Key_F2)
    If KeyboardPushed(#PB_Key_LeftShift) Or KeyboardPushed(#PB_Key_RightShift):xrot-0.1:Else:xrot+0.1:EndIf
  EndIf
  If KeyboardPushed(#PB_Key_F3)
    If KeyboardPushed(#PB_Key_LeftShift) Or KeyboardPushed(#PB_Key_RightShift):yrot-0.1:Else:yrot+0.1:EndIf
  EndIf
  If KeyboardPushed(#PB_Key_F4)
    If KeyboardPushed(#PB_Key_LeftShift) Or KeyboardPushed(#PB_Key_RightShift):zrot-0.1:Else:zrot+0.1:EndIf
  EndIf

  If KeyboardPushed(#PB_Key_F5)
    If KeyboardPushed(#PB_Key_LeftShift) Or KeyboardPushed(#PB_Key_RightShift):CamLocateX-1:Else:CamLocateX+1:EndIf
  EndIf
  If KeyboardPushed(#PB_Key_F6)
    If KeyboardPushed(#PB_Key_LeftShift) Or KeyboardPushed(#PB_Key_RightShift):CamLocateY-1:Else:CamLocateY+1:EndIf
  EndIf
  If KeyboardPushed(#PB_Key_F7)
    If KeyboardPushed(#PB_Key_LeftShift) Or KeyboardPushed(#PB_Key_RightShift):CamLocateZ-1:Else:CamLocateZ+1:EndIf
  EndIf

  If KeyboardPushed(#PB_Key_F8)
    If KeyboardPushed(#PB_Key_LeftShift) Or KeyboardPushed(#PB_Key_RightShift):CamLookAtX-1:Else:CamLookAtX+1:EndIf
  EndIf
  If KeyboardPushed(#PB_Key_F9)
    If KeyboardPushed(#PB_Key_LeftShift) Or KeyboardPushed(#PB_Key_RightShift):CamLookAtY-1:Else:CamLookAtY+1:EndIf
  EndIf
  If KeyboardPushed(#PB_Key_F10)
    If KeyboardPushed(#PB_Key_LeftShift) Or KeyboardPushed(#PB_Key_RightShift):CamLookAtZ-1:Else:CamLookAtZ+1:EndIf
  EndIf
EndProcedure

;-Mémoires Mesh
AllocateMemory(#PointID,12*(#NbX+1)*(#NbZ+1),0)
AllocateMemory(#TriangleID,12*#NbX*#NbZ*4,0)
AllocateMemory(#TextureID,12*(#NbX+1)*(#NbZ+1),0)
Matrice(#NbX,#NbZ)

;-Mesh
CreateMesh(0)
SetMeshData(0,0,UseMemory(#PointID),(#NbX+1)*(#NbZ+1))
SetMeshData(0,1,UseMemory(#TriangleID),(#NbX)*(#NbZ)*4)
SetMeshData(0,2,UseMemory(#TextureID),(#NbX+1)*(#NbZ+1))

;-Texture
UsePNGImageDecoder()
;LoadTexture(0,"purebasiclogoNew.png") ; <<< pourquoi ça plante quand je mets juste ça ?
LoadImage(0,"purebasiclogoNew.png")    ; alors que l'image se charge ? Je verrai ça plus tard
CreateTexture(0,256,256)
StartDrawing(TextureOutput(0))
DrawImage(UseImage(0),0,0)
DrawingMode(4)
Box(1,1,254,254,RGB(255,255,255));Why this box is not white?????????????
StopDrawing()

;- MAterial
CreateMaterial(0,TextureID(0))
MaterialFilteringMode(0,#PB_Material_Trilinear)

;-Entity
CreateEntity(0,MeshID(0),MaterialID(0))

;-Camera
CreateCamera(0,0,0,100,100)

;-Boucle principale

Repeat
  ClearScreen(0,0,0)
  ExamineKeyboard()
  ShowTextAndKeyTest()
  
  CameraLocate(0,CamLocateX,CamLocateY,CamLocateZ)
  CameraLookAt(0,CamLookAtX,CamLookAtY,CamLookAtZ)

  ;Calculate (AngleVague+WaveFrequency)%360: (cause % operand doesn't accept floats)
  !fild dword[v_circle]
  !fld dword[v_AngleVague]
  !fadd dword[v_WaveFrequency]
  !fprem
  !fstp dword[v_AngleVague]
  !fstp st1
  
  vagues()
  RotateEntity(0,xrot,yrot,zrot)
  RenderWorld()
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
But i wonder why the textured image and the drawings on it are so dark.
The drawed box outlined on the texture should be completely white... :?
Last edited by Psychophanta on Wed Nov 19, 2003 5:48 pm, edited 2 times in total.
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

@ Comtois : Excellent work Comtois :D
@ Psychophanta: nice improvement :!: [/quote]
regards,
benny!
-
pe0ple ar3 str4nge!!!
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post by Comtois »

But i wonder why the textured image and the drawings on it are so dark.
The drawed box outlined on the texture should be completely white...
just add

Code: Select all

AmbientColor(RGB(255,255,255))

I forgot it

thank's for modification :)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Thanks, :D , i realize AmbientColor(RGB(255,255,255)) is essential, at least we are making Silent Hill 4, and Konami would pursue us.

Putting this line in main loop you can stop all like a snap:
"While KeyboardPushed(#PB_Key_Space):ExamineKeyboard():Wend"
coma
Enthusiast
Enthusiast
Posts: 164
Joined: Fri Aug 15, 2003 3:46 am
Location: Canada

Post by coma »

very nice :)

next step : add normals and lighting ?
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post by Comtois »

next step : add normals and lighting ?
Yes , but i search how setmeshdata work with normals ??
have a look to demo collision 3D , i tried some combinaisons , but it don't work .

have someone an example ?
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

As a bug of the new Engine3D.dll (at www.purebasic.com/beta)
in this example can be watched that RenderWorld() function erases all the drawed texts... :o
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Ohhh!, modified to work with PB3.89B1, and doesn't work 8O
Engine3D.dll again? :?
Post Reply