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

Post by Comtois »

New , add normals and lighting .

Code: Select all

;Comtois le 29 mai 2004 
;Merci à François Weil pour l'optimisation du code :) 

;>> #PB_Material_Phong et #PB_Material_Flat sont inversés ? (Bug) 
;PB : 3.90 

;-Constantes 
#ScreenWidth = 1024 
#ScreenHeight = 768 
#ScreenDepth = 32 
#Img_SnapShot = 0 

;/SnapShot 
hBitmap = CreateImage(#Img_SnapShot, #ScreenWidth, #ScreenHeight) 
hdc = StartDrawing(ImageOutput()) 
SelectObject_(hdc, hBitmap) 
BitBlt_(hdc, 0, 0, #ScreenWidth, #ScreenHeight, GetDC_(GetDesktopWindow_()), 0, 0, #SRCCOPY) 
StopDrawing() 
DeleteDC_(hdc) 

;-Structures 
Structure Vecteur 
  x.f 
  y.f 
  Z.f 
EndStructure 
Structure Lumiere 
  Couleur.l 
  x.f 
  y.f 
  Z.f 
EndStructure  
Structure Matrice 
  Largeur.f 
  Profondeur.f 
  NbFacetteX.l      
  NbFacetteZ.l      
  FaceVisible.l      ; si = #True on double les triangles pour voir l'entité des deux côtés 
  NbPointX.l 
  NbPointZ.l 
  Nbtriangle.l 
  TailleFacetteX.f   ; Je ne sais pas si je vais garder cette méthode 
  TailleFacetteZ.f   ; Je ne sais pas si je vais garder cette méthode  
  PointIDMemory.l 
  TriangleIDMemory.l 
  TextureIDMemory.l 
  NormaleIDMemory.l 
EndStructure 
Structure Vague 
  decaleX.l 
  decaleZ.l 
  Hauteur.l 
  Angle.l 
  Vitesse.l 
EndStructure  
;-Variables 
Global Matrice.Matrice 
Global Lumiere.Lumiere 
Global Vague.Vague 
Global Mode.b ,ShadingMode.b,Animation.l,AfficheAide.l 

;-Declare 
Declare Matrice() 
Declare NormalesMatrice() 
Declare vagues() 
Declare GestionTouches() 
Declare GestionSouris() 
Declare AfficheAide() 
; 
;- Main starts here 
; 
If InitEngine3D() And InitSprite() And InitKeyboard() And InitMouse() And OpenScreen(#ScreenWidth, #ScreenHeight, #ScreenDepth, "FRW Matrice") 
  ;/Divers 
  Vague\Vitesse=3 
  Vague\Hauteur=20 
  Vague\decaleX=6 
  Vague\decaleZ=6 
  Animation = 1 
  AfficheAide = 1 
  
  ;/Paramètres de la matrice (à renseigner) 
  Matrice\Largeur    = 1000 
  Matrice\Profondeur = 1000 
  Matrice\NbFacetteX = 60 
  Matrice\NbFacetteZ = 60 
  Matrice\FaceVisible = #False ; #True double les triangles pour rendre visible l'autre face de la matrice 
  
  ;/Quelques informations complémentaires sur la matrice ( Calculées ) 
  If Matrice\FaceVisible 
    Matrice\Nbtriangle = 4 
  Else 
    Matrice\Nbtriangle = 2 
  EndIf  
  Matrice\NbPointX = Matrice\NbFacetteX + 1 
  Matrice\NbPointZ = Matrice\NbFacetteZ + 1 
  Matrice\TailleFacetteX = Matrice\Largeur / Matrice\NbFacetteX 
  Matrice\TailleFacetteZ = Matrice\Profondeur / Matrice\NbFacetteZ 
  Matrice\PointIDMemory    = AllocateMemory(12 * Matrice\NbPointX * Matrice\NbPointZ) 
  Matrice\TriangleIDMemory = AllocateMemory(12 * Matrice\NbFacetteX * Matrice\NbFacetteZ * Matrice\Nbtriangle) 
  Matrice\TextureIDMemory  = AllocateMemory(12 * Matrice\NbPointX * Matrice\NbPointZ)  
  Matrice\NormaleIDMemory  = AllocateMemory(12 * Matrice\NbPointX * Matrice\NbPointZ)  
  Dim Points.Vecteur(Matrice\NbPointX * Matrice\NbPointZ) 
  Matrice() 
  
  ;/Mesh 
  CreateMesh(0) 
  SetMeshData(0, 0, Matrice\PointIDMemory   , Matrice\NbPointX * Matrice\NbPointZ) 
  SetMeshData(0, 1, Matrice\TriangleIDMemory, Matrice\NbFacetteX * Matrice\NbFacetteZ * Matrice\Nbtriangle) 
  SetMeshData(0, 2, Matrice\TextureIDMemory , Matrice\NbPointX * Matrice\NbPointZ)  
  SetMeshData(0, 3, Matrice\NormaleIDMemory , Matrice\NbPointX * Matrice\NbPointZ)  
  
  ;/Texture 
  ;>>>>>>>>>>>    LoadTexture Here !!   <<<<<< 
  TextureXSize = 256 
  TextureYSize = 256 
  ;ResizeImage(#Img_SnapShot,TextureXSize,TextureYSize) 
  CreateTexture(0, TextureXSize, TextureYSize) 
  StartDrawing(TextureOutput(0)) 
  DrawImage(UseImage(#Img_SnapShot),0,0) 
  StopDrawing() 
  ;/Material 
  CreateMaterial(0, TextureID(0)) ; Material 
  MaterialShadingMode(0, #PB_Material_Gouraud) 
  MaterialBlendingMode(0, #PB_Material_AlphaBlend) 
  MaterialAmbientColor(0, RGB(255,55,55)) 
  MaterialDiffuseColor(0, RGB(255,255,128)) 
  MaterialSpecularColor(0,RGB(255,0,0)) 
  ;/Entity 
  CreateEntity(0, MeshID(0), MaterialID(0)) 
  ;/Caméra 
  CreateCamera(0, 0, 0, 100, 100) 
  CameraLocate(0, 0, 845, 915) 
  CameraLookAt(0, EntityX(0), EntityY(0) , EntityZ(0)) 
  ;/Lumière 
  AmbientColor(RGB(160,160,160)) 
  Lumiere\Couleur = RGB(255,255,128) 
  Lumiere\x = EntityX(0)/2 
  Lumiere\y = 930 
  Lumiere\Z = EntityZ(0)/2 
  CreateLight(0,Lumiere\Couleur)  
  LightLocate(0,Lumiere\x, Lumiere\y, Lumiere\Z) 
  Repeat 
    ;ClearScreen(0, 0, 0) 
    GestionSouris() 
    vagues() 
    NormalesMatrice()    
    RenderWorld() 
    GestionTouches() ; Placé après RenderWorld sinon le snapshot ne fonctionne pas ! 
    If AfficheAide : AfficheAide() : EndIf    
    FlipBuffers() 
  Until KeyboardPushed(#PB_Key_Escape) 
Else 
  MessageRequester("Error", "Something fails to initialize 3D engine", 0) 
EndIf 
End 

;/ 
;-Procedures 
;/ 
Procedure calculVecteurNorme(*v1.Vecteur,*v2.Vecteur,*n.Vecteur) 
  x.f = *v2\x - *v1\x 
  y.f = *v2\y - *v1\y 
  Z.f = *v2\Z - *v1\Z 
  d.f = Sqr(x * x + y * y + Z * Z) 
  *n\x =x / d 
  *n\y =y / d 
  *n\Z =Z / d 
EndProcedure 
Procedure produitVectoriel(*v1.Vecteur,*v2.Vecteur,*n.Vecteur) 
  *n\x = *v1\y * *v2\Z - *v1\Z * *v2\y 
  *n\y = *v1\Z * *v2\x - *v1\x * *v2\Z 
  *n\Z = *v1\x * *v2\y - *v1\y * *v2\x 
EndProcedure 
Procedure Matrice() 
  Adresse1 = Matrice\PointIDMemory    ; Adresse Points 
  Adresse2 = Matrice\TextureIDMemory  ; Adresse Texture 
  adresse3 = Matrice\TriangleIDMemory ; Adresse triangles 
  Index = 6 * Matrice\Nbtriangle 
  For b = 0 To Matrice\NbFacetteZ 
    bx = b * Matrice\NbPointX 
    bx1 = bx + Matrice\NbPointX 
    For a = 0 To Matrice\NbFacetteX 
      ;/Points 
      PokeF(Adresse1, (a - Matrice\NbFacetteX / 2) * Matrice\TailleFacetteX) 
      PokeF(Adresse1 + 4, 0) 
      PokeF(Adresse1 + 8, (b - Matrice\NbFacetteZ / 2) * Matrice\TailleFacetteZ) 
      Adresse1 + 12 
      ;/Texture 
      PokeF(Adresse2, a / Matrice\NbFacetteX) 
      PokeF(Adresse2 + 4, b / Matrice\NbFacetteZ) 
      Adresse2 + 8 
      ;/Triangles 
      If b < Matrice\NbFacetteZ And a < Matrice\NbFacetteX 
        P1 = a + bx 
        P2 = P1 + 1 
        P3 = a + bx1 
        P4 = P3 + 1 
        PokeL(adresse3, P4 << 16 + P1)          
        PokeL(adresse3 + 4, P1 << 16 + P2) 
        PokeL(adresse3 + 8, P4 << 16 + P3) 
        If Matrice\FaceVisible 
          PokeL(adresse3 + 12, P2 << 16 + P1) 
          PokeL(adresse3 + 16, P4 << 16 + P3) 
          PokeL(adresse3 + 20, P2 << 16 + P3) 
        EndIf 
        adresse3 + Index 
      EndIf  
    Next 
  Next 
EndProcedure 
Procedure NormalesMatrice() 
  Vecteur1.Vecteur 
  Vecteur2.Vecteur 
  
  P1.Vecteur ; Point 1 de la Facette 
  P2.Vecteur ; Point 2 de la Facette  
  P3.Vecteur ; Point 3 de la Facette 
  P4.Vecteur ; Point 4 de la Facette 
  
  Normale1.Vecteur ; Normale Triangle 1 de la Facette Points 1,2,3 
  Normale2.Vecteur ; Normale Triangle 2 de la Facette Points 2,4,3 
  
  adresse = Matrice\PointIDMemory + 4 
  AdrNormale = Matrice\NormaleIDMemory 
  
  For Z = 0 To Matrice\NbFacetteZ - 1 
    zx = Z * Matrice\NbPointX 
    zx1 = zx + Matrice\NbPointX 
    For x = 0 To Matrice\NbFacetteX - 1 
      
      NoPoint1 = x + zx 
      P1\x = x 
      P1\y = PeekF(adresse + NoPoint1 * 12) 
      P1\Z = Z 
      NoPoint2 = NoPoint1 + 1 
      P2\x = x + 1 
      P2\y = PeekF(adresse + NoPoint2 * 12) 
      P2\Z = Z 
      NoPoint3 = x + zx1 
      P3\x = x  
      P3\y = PeekF(adresse + NoPoint3 * 12) 
      P3\Z = Z + 1 
      NoPoint4 = NoPoint3 + 1 
      P4\x = x + 1 
      P4\y = PeekF(adresse + NoPoint4 * 12) 
      P4\Z = Z + 1 
      
      calculVecteurNorme(@P1,@P2,@Vecteur1) 
      calculVecteurNorme(@P1,@P3,@Vecteur2) 
      produitVectoriel(@Vecteur2,@Vecteur1,@Normale1) 
      calculVecteurNorme(@P2,@P4,@Vecteur1) 
      calculVecteurNorme(@P2,@P3,@Vecteur2) 
      produitVectoriel(@Vecteur2,@Vecteur1,@Normale2) 
      
      Points(NoPoint1)\x + Normale1\x 
      Points(NoPoint1)\y + Normale1\y 
      Points(NoPoint1)\Z + Normale1\Z 
      Points(NoPoint2)\x + Normale1\x + Normale2\x 
      Points(NoPoint2)\y + Normale1\y + Normale2\y 
      Points(NoPoint2)\Z + Normale1\Z + Normale2\Z 
      Points(NoPoint3)\x + Normale1\x + Normale2\x 
      Points(NoPoint3)\y + Normale1\y + Normale2\y 
      Points(NoPoint3)\Z + Normale1\Z + Normale2\Z 
      Points(NoPoint4)\x + Normale2\x 
      Points(NoPoint4)\y + Normale2\y 
      Points(NoPoint4)\Z + Normale2\Z 
      
    Next x 
  Next Z  
  
  For Z = 0 To Matrice\NbFacetteZ  
    For x = 0 To Matrice\NbFacetteX  
      Distance.f = Sqr(Points(No)\x * Points(No)\x + Points(No)\y * Points(No)\y + Points(No)\Z * Points(No)\Z) 
      Points(No)\x / Distance 
      Points(No)\y / Distance 
      Points(No)\Z / Distance 
      PokeF(AdrNormale    , Points(No)\x) 
      PokeF(AdrNormale + 4, Points(No)\y) 
      PokeF(AdrNormale + 8, Points(No)\Z) 
      No + 1 
      AdrNormale + 12 
    Next x
  Next Z  
  SetMeshData(0, 3, Matrice\NormaleIDMemory , Matrice\NbPointX * Matrice\NbPointZ) 
EndProcedure 
Procedure vagues() 
  If Animation 
    Vague\Angle = (Vague\Angle + Vague\Vitesse) % 360 
  EndIf 
  adresse = Matrice\PointIDMemory + 4 
  For Z = 0 To Matrice\NbFacetteZ 
    For x = 0 To Matrice\NbFacetteX 
      Sommet.f = Sin(0.0174533 * (Vague\Angle + (x * Vague\decaleX) + (Z * Vague\decaleZ))) * Vague\Hauteur 
      PokeF(adresse, Sommet) 
      adresse + 12 
    Next x 
  Next Z 
  SetMeshData(0, 0, Matrice\PointIDMemory, Matrice\NbPointX * Matrice\NbPointZ) 
EndProcedure 
Procedure GestionTouches() 
  ExamineKeyboard() 
  If KeyboardReleased(#PB_Key_F1) 
    Mode = #PB_Camera_Wireframe - Mode 
    CameraRenderMode(0, Mode) 
  EndIf 
  If KeyboardReleased(#PB_Key_F2) 
    MaterialShadingMode(0, #PB_Material_Flat) 
  EndIf  
  If KeyboardReleased(#PB_Key_F3) 
    MaterialShadingMode(0, #PB_Material_Gouraud) 
  EndIf 
  If KeyboardReleased(#PB_Key_F4) 
    MaterialShadingMode(0, #PB_Material_Phong) 
  EndIf 
  If KeyboardReleased(#PB_Key_F5) 
    Animation = 1 - Animation 
  EndIf 
  If KeyboardReleased(#PB_Key_F10) 
    AfficheAide = 1 - AfficheAide 
  EndIf 
  Vague\decaleX - 1 * KeyboardPushed(#PB_Key_Left) / 128 
  Vague\decaleX + 1 * KeyboardPushed(#PB_Key_Right) / 128 
  Vague\Hauteur + 1 * KeyboardPushed(#PB_Key_PageUp) / 128 
  Vague\Hauteur - 1 * KeyboardPushed(#PB_Key_PageDown) / 128 
  Vague\decaleZ + 1 * KeyboardPushed(#PB_Key_Up) / 128 
  Vague\decaleZ - 1 * KeyboardPushed(#PB_Key_Down) / 128 
  
  ;SnapShot 
  If KeyboardReleased(#PB_Key_F12) 
    hBitmap = CreateImage(#Img_SnapShot, #ScreenWidth, #ScreenHeight) 
    hdc = StartDrawing(ImageOutput()) 
    SelectObject_(hdc, hBitmap) 
    BitBlt_(hdc, 0, 0, #ScreenWidth, #ScreenHeight, GetDC_(GetDesktopWindow_()), 0, 0, #SRCCOPY) 
    StopDrawing() 
    DeleteDC_(hdc) 
    SetClipboardData(#PB_ClipboardImage, ImageID()) 
  EndIf 
EndProcedure 
Procedure GestionSouris() 
  ExamineMouse() 
  If MouseButton(1) 
    Lumiere\y + 2 
  EndIf 
  If MouseButton(2) 
    Lumiere\y - 2 
  EndIf  
  Lumiere\x + MouseDeltaX() 
  Lumiere\Z + MouseDeltaY() 
  LightLocate(0,Lumiere\x,Lumiere\y,Lumiere\Z) 
EndProcedure  
Procedure AfficheAide() 
  StartDrawing(ScreenOutput()) 
  DrawingMode(1) 
  FrontColor(255, 255, 255) 
  Locate(10, 10) 
  DrawText("DecaleX = " + StrF(Vague\decaleX)) 
  Locate(10, 30) 
  DrawText("DecaleZ = " + StrF(Vague\decaleZ)) 
  Locate(10, 50) 
  DrawText("HauteurVague = " + StrF(Vague\Hauteur)) 
  Locate(10, 70) 
  DrawText("[F1] => Change Mode Caméra") 
  Locate(10, 90) 
  DrawText("[F2] => MaterialSharingMode=> Flat") 
  Locate(10, 110) 
  DrawText("[F3] => MaterialSharingMode=> Gouraud") 
  Locate(10, 130) 
  DrawText("[F4] => MaterialSharingMode=> Phong") 
  Locate(10, 150) 
  DrawText("[F5] => Marche/Arrêt Animation des vagues") 
  Locate(10, 170) 
  DrawText("Lumiere X => " + Str(Lumiere\x)) 
  Locate(10, 190) 
  DrawText("Lumiere Y (MouseClick) => " + Str(Lumiere\y)) 
  Locate(10, 210) 
  DrawText("Lumiere Z => " + Str(Lumiere\Z)) 
  Locate(10, 230) 
  DrawText("Image par seconde (Actuel) = " + Str(Engine3DFrameRate(#PB_Engine3D_Current))) 
  Locate(10, 250) 
  DrawText("Image par seconde (Moyenne) = " + Str(Engine3DFrameRate(#PB_Engine3D_Average))) 
  Locate(10, 270) 
  DrawText("Nombre de polygones (Triangles) = " + Str(CountRenderedTriangles())) 
  Locate(10, 290) 
  DrawText("Mémoire vidéo disponible = " + Str(AvailableScreenMemory())) 
  Locate(10, 310) 
  DrawText("[F10] Cache/Montre l'aide")  
  Locate(10, 330) 
  DrawText("[F12] SnapShot")    
  StopDrawing() 
EndProcedure  
Last edited by Comtois on Sat May 29, 2004 7:34 pm, edited 3 times in total.
Please correct my english
http://purebasic.developpez.com/
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

...,

:P

C'est beau !
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

@Comtois:

I am speechless ... excellent :D 8) :!: :!: :!:
regards,
benny!
-
pe0ple ar3 str4nge!!!
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Comtois,

Just a question I can't master : when I change the entity orientation using RotateEntity and giving angles so that it should display the image back, it is completely dark.

Another sample code I made was showing the image back (reversed image of course), but it is not the case there.

Any idea ?

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post by Comtois »

Yes , change

Code: Select all

  Matrice\FaceVisible = #False
by

Code: Select all

  Matrice\FaceVisible = #True
Please correct my english
http://purebasic.developpez.com/
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

I feel like a newbie ...
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Re: Matrix

Post by GeoTrail »

Comtois wrote:You need an image , i use this one

Image
I get this error when trying to compile it:
Line 41: AllocateMemory() : Incorrect number of parameters.
I'm using PB 3.91b and jaPBe.
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

GeoTrail,

This error occurs typically when using old code containing ie :

*Buffer = AllocateMemory(0, Length, 0)

AllocateMemory has changed since V3.90 or before to :

*Buffer = AllocateMemory(Length)

Maybe you will find the way to corret this code to make it running well.

In case you are using an older post of this code, than the last, I suggest you catch the latest code here.

By changing AllocateMemory to new syntax, with only one argument, can lose you in understanding what was the original way of using memory buffers in the code producing this error.

Ie, if you have UseMemory(n) after an old AllocateMemory(x, y, z) this has to be replaced by the *Buffer that gets the AllocateMemory function's result ...

Nevermind, easiest is to copy the last code posted just above. This one should work in 3.91.

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

K, thanks for the feedback :)
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
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 this nice improvement, Comtois, thanks.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Very nice. :)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Comtois
It doesn't work here
And you 3D collision demo neither.:cry:
In your example here, at 111 it reports a memory fault
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post by Comtois »

You use Ogre 1.0.3 beta ? the mesh format has changed

here is version for Ogre 1.0.3 beta

Code: Select all

;**********************************************
;** Comtois ** 22/08/05 ** Matrice / Vagues  **
;**********************************************
;Modifications and additions by Psychophanta

;########################
;# Need Ogre 1.0.3 beta #
;########################

;/SnapShot
#Img_SnapShot = 0
ExamineDesktops()
hBitmap = CreateImage(#Img_SnapShot, DesktopWidth(0), DesktopHeight(0))
hdc = StartDrawing(ImageOutput())
SelectObject_(hdc, hBitmap)
BitBlt_(hdc, 0, 0, DesktopWidth(0), DesktopHeight(0), GetDC_(GetDesktopWindow_()), 0, 0, #SRCCOPY)
StopDrawing()
DeleteDC_(hdc)

;-Initialisation
bitplanes.b=32:RX.w=1024:RY.w=768
If InitEngine3D()=0 Or InitSprite()=0 Or InitKeyboard()=0
  MessageRequester("Error","Something fails to open Screen for 3D. This requires Engine3D.dll",0)
  End
EndIf
While OpenScreen(RX.w,RY.w,bitplanes.b,"DemoMatrice")=0
  If bitplanes.b>16:bitplanes.b-8
  ElseIf RY.w>600:RX.w=800:RY.w=600
  ElseIf RY.w>480:RX.w=640:RY.w=480
  ElseIf RY.w>400:RX.w=640:RY.w=400
  ElseIf RY.w>240:RX.w=320:RY.w=240
  ElseIf RY.w>200:RX.w=320:RY.w=200
  Else:MessageRequester("VGA limitation","Can't open Screen!",0):End
  EndIf
Wend
Structure  Vecteur
x.f
y.f
z.f
EndStructure
Structure Vertex
x.f
y.f
z.f
Nx.f
Ny.f
Nz.f
;Cx.f
;Cy.f
;Cz.f
u.f
v.f
EndStructure

Structure DoubleFace
f1.w
f2.w
f3.w

f4.w
f5.w
f6.w

f7.w
f8.w
f9.w

f10.w
f11.w
f12.w
EndStructure

;-Constantes
#NbX=30 ; nombre de facettes
#NbZ=30 ; nombre de facettes
#DegConv=3.14159265/180

#PB_Mesh_Vertex       = 1 << 0
#PB_Mesh_Color        = 1 << 1
#PB_Mesh_Normal       = 1 << 2
#PB_Mesh_UVCoordinate = 1 << 3

 ;Additionnnal flag To specify the faces

#PB_Mesh_Face         = 1 << 4
   
;-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=3;=waves/second
WavePeriodX=5;=1/Wave lenght
WavePeriodZ=9;=1/Wave lenght
WaveAmplitude=2
xrot=-0.3:yrot=-0.4: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(*Vertex.Vertex,*Face.DoubleFace,FX.l,FZ.l)
  *Ptr.Vertex=*Vertex
   FX2=FX/2
   FZ2=FZ/2
  For b=0 To FZ
    For a=0 To FX
      *Ptr\x= a-FX2
      *Ptr\y=0
      *Ptr\z=b-FZ2
      *Ptr\u=a/FX
      *Ptr\v=b/FZ
      *Ptr + SizeOf(Vertex)
    Next a
  Next b

  *PtrF.DoubleFace=*Face
  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
      ;Face 1
      *PtrF\f1=P3 : *PtrF\f2=P2 : *PtrF\f3=P1
      *PtrF\f4=P2 : *PtrF\f5=P3 : *PtrF\f6=P4
      ;Face 2
      *PtrF\f7=P1 : *PtrF\f8=P2 : *PtrF\f9=P3   
      *PtrF\f10=P4 : *PtrF\f11=P3 : *PtrF\f12=P2
      *PtrF + SizeOf(DoubleFace)
    Next
  Next


EndProcedure


Procedure MakeNormale(*Vertex.Vertex,*Face.DoubleFace,Fx,Fz)
     Protected *Ptr.Vertex, *PtrF.DoubleFace, *PtrN.Vecteur, Temp.Vecteur
    Protected  Vecteur1.Vecteur,  Vecteur2.Vecteur,  P1.Vecteur,  P2.Vecteur,  P3.Vecteur ,*N.Vecteur
   
     *N=AllocateMemory((Fx+1)*(Fz+1)*SizeOf(Vecteur))
 
    *PtrF=*Face
     For b=0 To FZ-1
       For a=0 To FX-1
          ;{ Calcule la normale du premier triangle

          ;Avec les 3 vertices qui composent le triangle, on détermine deux vecteurs

            *Ptr=*Vertex + *PtrF\f1 * SizeOf(Vertex)
           P1\x = *Ptr\x
         P1\y = *Ptr\y
          P1\z = *Ptr\z
       
           *Ptr=*Vertex + *PtrF\f2 * SizeOf(Vertex)
            P2\x = *Ptr\x
           P2\y = *Ptr\y
           P2\z = *Ptr\z
       
           *Ptr=*Vertex + *PtrF\f3 * SizeOf(Vertex)
            P3\x = *Ptr\x
           P3\y = *Ptr\y
           P3\z = *Ptr\z
       
           Vecteur1\x = (P1\x - P2\x)
           Vecteur1\y = (P1\y - P2\y)
           Vecteur1\z = (P1\z - P2\z)
       
           Vecteur2\x = (P1\x - P3\x)
           Vecteur2\y = (P1\y - P3\y)
           Vecteur2\z = (P1\z - P3\z)
           
         ;Calcule la normale du premier triangle
           Temp\x = ((Vecteur1\y * Vecteur2\z) - (Vecteur1\z * Vecteur2\y))
           Temp\y = ((Vecteur1\z * Vecteur2\x) - (Vecteur1\x * Vecteur2\z))
           Temp\z = ((Vecteur1\x * Vecteur2\y) - (Vecteur1\y * Vecteur2\x))
           
           ;Et affecte cette normale aux vertices composants le premier triangle
           *PtrN=*N + *PtrF\f1 * SizeOf(Vecteur)
           *PtrN\x + Temp\x
           *PtrN\y + Temp\y
           *PtrN\z + Temp\z

           *PtrN=*N + *PtrF\f2 * SizeOf(Vecteur)
           *PtrN\x + Temp\x
           *PtrN\y + Temp\y
           *PtrN\z + Temp\z
           
           *PtrN=*N + *PtrF\f3 * SizeOf(Vecteur)
           *PtrN\x + Temp\x
           *PtrN\y + Temp\y
           *PtrN\z + Temp\z
          ;}
         
         ;{ Calcule la normale du second triangle

          ;Avec les 3 vertices qui composent le triangle, on détermine deux vecteurs

            *Ptr=*Vertex + *PtrF\f4 * SizeOf(Vertex)
           P1\x = *Ptr\x
         P1\y = *Ptr\y
          P1\z = *Ptr\z
       
           *Ptr=*Vertex + *PtrF\f5 * SizeOf(Vertex)
            P2\x = *Ptr\x
           P2\y = *Ptr\y
           P2\z = *Ptr\z
       
           *Ptr=*Vertex + *PtrF\f6 * SizeOf(Vertex)
            P3\x = *Ptr\x
           P3\y = *Ptr\y
           P3\z = *Ptr\z
       
           Vecteur1\x = (P1\x - P2\x)
           Vecteur1\y = (P1\y - P2\y)
           Vecteur1\z = (P1\z - P2\z)
       
           Vecteur2\x = (P1\x - P3\x)
           Vecteur2\y = (P1\y - P3\y)
           Vecteur2\z = (P1\z - P3\z)
           
         ;Calcule la normale du second triangle
           Temp\x = ((Vecteur1\y * Vecteur2\z) - (Vecteur1\z * Vecteur2\y))
           Temp\y = ((Vecteur1\z * Vecteur2\x) - (Vecteur1\x * Vecteur2\z))
           Temp\z = ((Vecteur1\x * Vecteur2\y) - (Vecteur1\y * Vecteur2\x))
           
           ;Et affecte cette normale aux vertices composants le second triangle
           *PtrN=*N + *PtrF\f4 * SizeOf(Vecteur)
           *PtrN\x + Temp\x
           *PtrN\y + Temp\y
           *PtrN\z + Temp\z

           *PtrN=*N + *PtrF\f5 * SizeOf(Vecteur)
           *PtrN\x + Temp\x
           *PtrN\y + Temp\y
           *PtrN\z + Temp\z
           
           *PtrN=*N + *PtrF\f6 * SizeOf(Vecteur)
           *PtrN\x + Temp\x
           *PtrN\y + Temp\y
           *PtrN\z + Temp\z
          ;}
         
         ;Face suivante
         *PtrF + SizeOf(DoubleFace)
        Next a
     Next b
   ;Norme et affecte la normale de chaque Vertex
   *PtrN=*N
   *Ptr=*Vertex
   For b=0 To FZ
       For a=0 To FX
          Norme=Sqr(*PtrN\x * *PtrN\x + *PtrN\y * *PtrN\y + *PtrN\z * *PtrN\z)
         If norme = 0
               *Ptr\Nx = 0
               *Ptr\Ny = 0
               *Ptr\Nz = 0
          Else
               *Ptr\Nx = *PtrN\x / Norme
               *Ptr\Ny = *PtrN\y / Norme
               *Ptr\Nz = *PtrN\z / Norme
          EndIf         
          *Ptr + SizeOf(Vertex)
          *PtrN + SizeOf(Vecteur)
       Next a
     Next b

   FreeMemory(*N)
EndProcedure

Procedure vagues(*Vertex.Vertex,*Face.DoubleFace)
 ; Modification sur l'axe des Y
  *Ptr.Vertex=*Vertex
  For z=0 To #NbZ
    For x=0 To #NbX
        *Ptr\y=Sin(#DegConv*(AngleVague+x*WavePeriodX+z*WavePeriodZ))*WaveAmplitude
      *Ptr + SizeOf(Vertex)
    Next
  Next
  MakeNormale(*Vertex.Vertex,*Face.DoubleFace,#NbX,#NbZ)
  SetMeshData(0,#PB_Mesh_Vertex | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate,*Vertex,(#NbX+1)*(#NbZ+1))
EndProcedure

Procedure.b ShowTextAndKeyTest(hidetext.b)
  If hidetext.b=0
    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))
    Locate(0,280)
    DrawText("[F11] => Show or hide text")
    Locate(0,300)
    DrawText("[Space] => Halt")
    StopDrawing()
  EndIf
  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 KeyboardReleased(#PB_Key_F2)
    If KeyboardPushed(#PB_Key_LeftShift) Or KeyboardPushed(#PB_Key_RightShift):xrot-0.1:Else:xrot+0.1:EndIf
  EndIf
  If KeyboardReleased(#PB_Key_F3)
    If KeyboardPushed(#PB_Key_LeftShift) Or KeyboardPushed(#PB_Key_RightShift):yrot-0.1:Else:yrot+0.1:EndIf
  EndIf
  If KeyboardReleased(#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
  If KeyboardReleased(#PB_Key_F11):hidetext.b!1:EndIf
  While KeyboardPushed(#PB_Key_Space):ExamineKeyboard():Wend
  ProcedureReturn hidetext.b
EndProcedure

;-Mémoires Mesh
*VertexID=AllocateMemory((#NbX+1)*(#NbZ+1)*SizeOf(vertex))
*FaceID=AllocateMemory(#NbX*#NbZ*4*SizeOf(DoubleFace))
Matrice(*VertexID,*FaceID,#NbX,#NbZ)

;-Mesh

CreateMesh(0)
SetMeshData(0,#PB_Mesh_Vertex | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate,*VertexID,(#NbX+1)*(#NbZ+1))
SetMeshData(0,#PB_Mesh_face,*FaceID,(#NbX)*(#NbZ)*4)

;-Texture
CreateTexture(0, 256, 256)
StartDrawing(TextureOutput(0))
DrawImage(UseImage(#Img_SnapShot),0,0)
StopDrawing()

;- MAterial
CreateMaterial(0, TextureID(0)) ; Material
;MaterialShadingMode(0, #PB_Material_Gouraud) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<BUG ?????
;MaterialShadingMode(0, #PB_Material_Phong) ; Phong = Flat ???
MaterialBlendingMode(0, #PB_Material_AlphaBlend)
MaterialAmbientColor(0, RGB(255,55,55))
MaterialDiffuseColor(0, RGB(255,255,128))
MaterialSpecularColor(0,RGB(255,0,0))

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

;-Camera
CreateCamera(0,0,0,100,100)
AmbientColor(RGB(95,95,95));<- Essential for clarity

;Light
CreateLight(0,RGB(255,255,128))
LightLocate(0,EntityX(0)/2,EntityY(0)+800,EntityZ(0)/2)
;-Boucle principale
Repeat
  ClearScreen(0,0,0)
  ExamineKeyboard()
 
  CameraLocate(0,CamLocateX,CamLocateY,CamLocateZ)
  CameraLookAt(0,CamLookAtX,CamLookAtY,CamLookAtZ)

  ;Calculate (AngleVague+WaveFrequency)%360: (coz % 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(*VertexID,*FaceID)
  RotateEntity(0,xrot,yrot,zrot)
  RenderWorld()
  hidetext.b=ShowTextAndKeyTest(hidetext.b)
  FlipBuffers():Delay(7)
Until KeyboardPushed(#PB_Key_Escape)
Please correct my english
http://purebasic.developpez.com/
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Thank you :D
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Only joined a few months back so this is the first I had seen of this.

Awesome.
Post Reply