Mode7 & generation de texture

Programmation d'applications complexes
G-Rom
Messages : 3626
Inscription : dim. 10/janv./2010 5:29

Mode7 & generation de texture

Message par G-Rom »

J'ai retrouvé un vieux code bien pourri qui trainais sur mon DD lors d'un nettoyage , à lançer sans debugger , c'est pas optimiser , mais ca marche ^^
je m'étais inspiré ici : http://lodev.org/cgtutor/ pour les textures procedurale

Code : Tout sélectionner

 Declare generateNoise()
 Declare.d smoothnoise(x.d,y.d)
 Declare.d Turbulence(x.d,y.d,size.d)
 Declare generateSinePattern(imageid)
 Declare generateMarble(imageid)
Declare repeatable(imageid)

Global _Trig_Resolution_.w = 1024   ; steps around unity circle, needs
                                     ; to be a power of 2, i.e.
                                     ; 512, 1024, 2048, 4096, 8192, ...
                                     ; the larger this value, the larger
                                     ; the tables become, but one also
                                     ; gets more precision
                                     
Global _Resolution_minus_One_.w = _Trig_Resolution_.w - 1                                     

Global Dim SinTable.f(_Resolution_minus_One_.w)
Global Dim CosTable.f(_Resolution_minus_One_.w)

Global Angle_to_Index.f = ( _Trig_Resolution_ / 360.0 )

_Trig_Res_halved_.w = _Trig_Resolution_.w >> 1              ; populate tables
For n = 0 To _Resolution_minus_One_.w                        
    SinTable(n) = Sin( (#PI / _Trig_Res_halved_.w) * n )
    CosTable(n) = Cos( (#PI / _Trig_Res_halved_.w) * n )
Next 

Macro fSIN( degrees )
  SinTable( Int((Angle_to_Index.f * degrees) + 0.5 ) & (_Resolution_minus_One_.w) )
EndMacro

Macro fCOS( degrees )
  CosTable( Int((Angle_to_Index.f * degrees) + 0.5 ) & (_Resolution_minus_One_.w) )
EndMacro


UsePNGImageDecoder()

width.l     = 384
height.l    = 384
image.i     = CreateImage(#PB_Any,256,256);LoadImage(#PB_Any,"ground.png")
generateNoise()
generateSinePattern(image)
generateMarble(image)
repeatable(image)

SaveImage(image,"marble.bmp")

Dim texture.l(ImageWidth(image),ImageHeight(image))

StartDrawing(ImageOutput(image))
For y = 0 To ImageHeight(image) - 1
  For x = 0 To ImageWidth(image) - 1
    texture(x,y) = Point(x,y)
  Next x
Next y
StopDrawing()


InitKeyboard()
InitSprite()

ExamineDesktops()
w = DesktopWidth(0)
h = DesktopHeight(0)

OpenScreen(w,h,32,"mode7")

CreateSprite(0,width,height)

deltaTime.f = 0

While 1
  ExamineKeyboard()
  timer.l = ElapsedMilliseconds()

  If KeyboardPushed(#PB_Key_Escape)
    End
  EndIf
  
  
    offset - 10 * deltaTime
    angle.f  + 360 * deltaTime
  
     horizon.l = -120 - 10 * Cos(ElapsedMilliseconds()/1000)
    
     dd.f + 0.05 * deltaTime
    
   StartDrawing(SpriteOutput(0))
   
    *pixel = DrawingBuffer()
   
   
   Box(0,0,width,height,0)
  For y = -height/2 To (height/2)-1
  For x = -width/2 To (width/2)-1
    
    fov       = 200 
    px.f = x 
    py.f = (y - horizon - fov) 
    pz.f = (y - horizon) 
    
    sx.f = px / (pz*10)
    sy.f = py / (pz*10)
    
    scaling.l = 200
    
    sxx.f = sx * fCos(angle*#PI/180) - sy * fSin(angle*#PI/180) + dd
    syx.f = sx * fSin(angle*#PI/180) + sy * fCos(angle*#PI/180);
      
    pixel_x.l =  Int((sxx*scaling))%256
    pixel_y.l =  Int((syx*scaling)+offset)%256
    
  
  
    If pixel_x < 0
      pixel_x + 256
    EndIf 
    
    If pixel_y < 0
      pixel_y + 256
    EndIf 
    
    If pz>16
      
      pix = x+(width/2)
      piy= y+(height/2) + 100
            
      piy - 15 * Sin(x/(width/4))+15
    
      
      If pix=>0 And pix<width-1 And piy=>0 And piy<height-1
        
        color = texture(pixel_x,pixel_y)
        
        br.f = ((height) - (y + (height/2))) / (height)
        br   = 1-(br)
        
        If br<0
          br = 0
        EndIf 
        If br>1
          br = 1 
        EndIf
        r=Red(color) * br 
        g=Green(color) * br 
        b=Blue(color) * br 

        PokeL(*pixel + ( pix*4 ) + width * (piy*4),RGB(b,g,r))   
        
      EndIf 
    EndIf 
    
    Next 
Next

StopDrawing()



TransformSprite(0,0,0,w,0,w,h,0,h)
DisplaySprite(0,0,0)




FlipBuffers()
deltaTime = (ElapsedMilliseconds() - timer) / 1000
Wend 
   
 Procedure generateNoise()
   Global noiseWidth  = 256
   Global noiseHeight = 256
   Global Dim noise.d(noiseWidth,noiseHeight)
   
   For y = 0 To noiseHeight-1
     For x = 0 To noiseWidth-1
       noise(x,y) = Random(32768) / 32768
     Next
   Next
 EndProcedure
   
 Procedure.d smoothnoise(x.d,y.d)
   fractX.d = x - Int(x)
   fractY.d = y - Int(y)
   
   x1 = (Int(x) + noiseWidth -1) % noiseWidth
   y1 = (Int(y) + noiseHeight -1) % noiseHeight
   
   x2 = (x1 + noiseWidth -1) % noiseWidth
   y2 = (y1 + noiseHeight -1) % noiseHeight
   
   value.d = 0
   value + fractX * fractY         * noise(x1,y1)
   value + fractX * (1-fractY)     * noise(x1,y2)
   value + (1-fractx) * fractY     * noise(x2,y1)
   value + (1-fractx) * (1-fractY) * noise(x2,y2)
   
   ProcedureReturn value
 EndProcedure
   

Procedure.d Turbulence(x.d,y.d,size.d)
  initialSize.d = size
  While(size=>1)
    value.d + smoothnoise(x/size,y/size) * size
    size / 2
  Wend
  ProcedureReturn 128 * value  / initialSize
EndProcedure



Procedure.i generateSinePattern(imageid)
  StartDrawing(ImageOutput(imageid))
    For y = 0 To OutputHeight()-1
      For x = 0 To OutputWidth()-1
        
        comp.f = 255 * Sin(x+(OutputWidth()))
        Plot(x,y,RGB(comp,comp,comp))
        
        
      Next
    Next
  StopDrawing()
EndProcedure


Procedure generateMarble(imageid)
  StartDrawing(ImageOutput(imageid))
    xPeriod.d = 5.0
    yPeriod.d = 15.0
    turbPower.d = 15.0
    turbSize.d = 256.0

    For y = 0 To OutputHeight()-1
      For x = 0 To OutputWidth()-1
        

        
        xyValue.d = x * xPeriod / noiseHeight + y * yPeriod / noiseWidth + turbPower * turbulence(x, y, turbSize) / 256.0;
        sineValue.d = 256 * Abs(Sin(xyValue * #PI));
        
        If(sineValue>255) : sineValue = 255 : EndIf
        If(sineValue<0) : sineValue = 0 : EndIf
        
        r.l = sineValue * 0.9
        g.l = sineValue * 0.7
        b.l = sineValue * 0.8
        
        Plot(x, y, RGB(r,g,b));
        
      Next
    Next
  StopDrawing()
EndProcedure


Procedure repeatable(imageid)
  
  copy = CopyImage(imageid,#PB_Any)
  
  ResizeImage(copy,ImageWidth(imageid)/2,ImageHeight(imageid)/2)
  
  
  StartDrawing(ImageOutput(imageid))
  
  DrawImage(ImageID(copy),0,0)
  
  For y = 0 To (OutputHeight()/2)-1
    For x = 0 To (OutputWidth()/2)-1
            
      color = Point(x,y)
      
      Plot( (OutputWidth()-1)-x,y,color)
      Plot( (OutputWidth()-1)-x,(OutputHeight()-1)-y,color)
      Plot( x,(OutputHeight()-1)-y,color)
      
      
    Next
  Next
   
  
  
  StopDrawing()
  
EndProcedure
Avatar de l’utilisateur
Fig
Messages : 1176
Inscription : jeu. 14/oct./2004 19:48

Re: Mode7 & generation de texture

Message par Fig »

C'est assez impressionnant, mais il manque une explication car on ne sait pas trop ce qu'on regarde en fait... :wink:
Il y a deux méthodes pour écrire des programmes sans erreurs. Mais il n’y a que la troisième qui marche.
Version de PB : 6.00LTS - 64 bits
G-Rom
Messages : 3626
Inscription : dim. 10/janv./2010 5:29

Re: Mode7 & generation de texture

Message par G-Rom »

le mode 7 est une vieille technique de projection de texture pour faire de la fausse "3D"

https://fr.wikipedia.org/wiki/Mode_7
https://www.google.fr/webhp?sourceid=ch ... q=mode%207
Micheao
Messages : 533
Inscription : dim. 07/déc./2014 10:12
Localisation : Sud-Est

Re: Mode7 & generation de texture

Message par Micheao »

Impressionnant G-Rom
Avatar de l’utilisateur
Ar-S
Messages : 9472
Inscription : dim. 09/oct./2005 16:51
Contact :

Re: Mode7 & generation de texture

Message par Ar-S »

ça marche bien même sous linux même si ça rame.
Pense à virer le bmp en quittant le soft.
~~~~Règles du forum ~~~~
⋅.˳˳.⋅ॱ˙˙ॱ⋅.˳Ar-S ˳.⋅ॱ˙˙ॱ⋅.˳˳.⋅
W11x64 PB 6.x
Section HORS SUJET : ICI
LDV MULTIMEDIA : Dépannage informatique & mes Logiciels PB
UPLOAD D'IMAGES : Uploader des images de vos logiciels
Répondre