Ooops
November 28, 2020, 10:34:48 AM

Author Topic: [bb] 3D Entity Highlighting by _PJ_ [ 1+ years ago ]  (Read 622 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] 3D Entity Highlighting by _PJ_ [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : 3D Entity Highlighting
Author : _PJ_
Posted : 1+ years ago

Description : 'Quite' quickly plots to an image buffer according to the visible 'surfaces' of an entity specified

(Fixed a coupla little bugs)


Code :
Code: BlitzBasic
  1. ;-----------------------------------------------------------------------------------------------------------------------------------
  2. ;Example
  3. ;-----------------------------------------------------------------------------------------------------------------------------------
  4.  
  5. Graphics3D 800,600,32,6
  6. SetBuffer BackBuffer()
  7.  
  8. Cube=CreateSphere()
  9. EntityColor Cube,Rand(32,224),Rand(32,224),Rand(32,224)
  10. EntityPickMode Cube,2,1
  11.  
  12. Cam=CreateCamera()
  13. MoveEntity Cam,0,0,-5
  14.  
  15. Local MyImage%=0
  16.  
  17. PointEntity cam,cube
  18.  
  19. While Not(KeyDown(1))
  20.         ;Control Camera Movement
  21.         MoveEntity Cam,(KeyDown(205)-KeyDown(203))*0.1,0,(KeyDown(200)-KeyDown(208))*0.1
  22.         TurnEntity Cam,0,(MouseXSpeed())*0.25,0,True
  23.        
  24.         If KeyHit(57)
  25.                 FlushKeys()
  26.                 MyImage=EntityHighlightImage(Cube,Cam)
  27.                 If (MyImage) Then DrawImage MyImage,0,0
  28.                 Flip
  29.                 WaitKey()
  30.                 End
  31.         Else
  32.                 UpdateWorld
  33.                 RenderWorld
  34.         End If
  35.         Flip
  36. Wend   
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. ;-----------------------------------------------------------------------------------------------------------------------------------
  59. ;Functions
  60. ;-----------------------------------------------------------------------------------------------------------------------------------
  61.  
  62.  
  63.  
  64. Function EntityHighlightImage%(Entity%,Camera%,RGBa%=-1)
  65.         If (Not(Entity)) Then Return 0
  66.         If (Not (Camera)) Then Return 0
  67.         If (Not(((EntityClass(Entity)="Mesh") Or (EntityClass(Entity)="Terrain") Or (EntityClass(Entity)="Plane") Or (EntityClass(Entity)="Mirror") Or (EntityClass(Entity)="MD2") Or (EntityClass(Entity)="BSP")))) Then Return 0
  68.         If (Not(EntityVisible(Entity,Camera))) Then Return 0
  69.         If (Not(EntityInView(Entity,Camera))) Then Return 0
  70.        
  71.         ; Make a copy so as not to 'harm' the original entity
  72.         Local WorkingCopy%=CopyEntity(Entity)
  73.        
  74.         ;GraphicsWidth() and GraphicsHeight() should match the camera viewport
  75.         Local W%=GraphicsWidth()
  76.         Local H%=GraphicsHeight()
  77.        
  78.         Local X%,Y%
  79.         Local RGB
  80.         Local IterSurface%,Surface%,IterVertex%
  81.        
  82.         ;OPTIONAL      
  83.         ; These are really only if you wish to make use of the CropImage() function to restrict output to the highlight only.
  84.         ;Else, the image dimensions will match the W & H Viewport given above.
  85. ;       {
  86.         Local L%=W-1,R%=0,T%=H-1,B%=0
  87. ;       }
  88.        
  89.         ; Create an Image to draw the highlight separately.
  90.        
  91.         Local ReturnImage=CreateImage(W,H)
  92.         UpdateWorld
  93.         RenderWorld
  94.         LockBuffer ImageBuffer(ReturnImage)
  95.         For IterSurface=1 To CountSurfaces(Entity)
  96.                 Surface=GetSurface(Entity,IterSurface)
  97.                 For IterVertex=0 To CountVertices(Surface)-1
  98.                         TFormPoint(VertexNX(Surface,IterVertex),VertexNY(Surface,IterVertex),VertexNZ(Surface,IterVertex),Entity,0)
  99.                        
  100.                         CameraProject Camera,TFormedX(),TFormedY(),TFormedZ()
  101.                        
  102.                         X=ProjectedX()
  103.                         Y=ProjectedY()
  104.                        
  105.                         RGB=RGBa((X-(GraphicsWidth() Shr True)/GraphicsWidth())*255,0,(Y-(GraphicsHeight() Shr True)/GraphicsHeight())*255)
  106.                        
  107.                         WritePixelFast X,Y,RGB,ImageBuffer(ReturnImage)        
  108.                        
  109.                         ;OPTIONAL      
  110.                         ; These are really only if you wish to make use of the CropImage() function to restrict output to the highlight only.
  111.                         ;Else, the image dimensions will match the W & H Viewport given above.
  112. ;                       {
  113.                         If (X<L) Then L=X
  114.                         If (X>R) Then R=X
  115.                         If (Y<T) Then T=Y
  116.                         If (Y>B) Then B=Y
  117. ;                       }
  118.                        
  119.                 Next
  120.         Next
  121.         UnlockBuffer ImageBuffer(ReturnImage)
  122.        
  123.        
  124.         ;Free up duplicates
  125.         FreeEntity WorkingCopy
  126.        
  127.        
  128.                         ;OPTIONAL      
  129.                 ; These are really only if you wish to make use of the CropImage() function to restrict output to the highlight only.
  130.                 ;Else, the image dimensions will match the W & H Viewport given above.
  131. ;               {
  132.        
  133.         Local CroppedImage
  134.         If (L*T*R*B)
  135.                 If (((B-T)<1) Or ((R-L)<1))
  136.                         L=0
  137.                         T=0
  138.                 End If
  139.                
  140.                 CroppedImage=CropImage(ReturnImage,L,T,R,B)
  141.                
  142.                         ;Free up duplicates
  143.                 FreeImage ReturnImage
  144.                 ReturnImage=CroppedImage               
  145.         End If
  146. ;               }
  147.        
  148.         Return ReturnImage
  149.        
  150. End Function
  151.  
  152. Function CropImage%(Image%,X1%,Y1%,X2%,Y2%)
  153.         If (Not(Image)) Then Return 0
  154.         Local W%=X2-X1
  155.         Local H%=Y2-Y1
  156.         Local CroppedImage%=CreateImage(W,H)
  157.         Local XRW%,YRW%
  158.         Local RGB%
  159.         LockBuffer ImageBuffer(Image)
  160.         LockBuffer ImageBuffer(CroppedImage)
  161.         For XRW=0 To W-1
  162.                 For YRW=0 To H-1
  163.                         RGB=ReadPixelFast(XRW+X1,YRW+Y1,ImageBuffer(Image))
  164.                         WritePixelFast XRW,YRW,RGB,ImageBuffer(CroppedImage)
  165.                 Next
  166.         Next
  167.         UnlockBuffer ImageBuffer(Image)
  168.         UnlockBuffer ImageBuffer(CroppedImage)
  169.         Return CroppedImage
  170. End Function
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184. Function RGBa%(R%,G%,B%,a%=0)
  185.         ;       Returns aRGB Value from components.
  186.        
  187.         Return ((a% Shl 24) Or (R% Shl 16) Or (G% Shl 8) Or B%)
  188. End Function
  189.  
  190. ;_______________________________________________________________________________________________________________________
  191. ;_______________________________________________________________________________________________________________________
  192.  
  193. Function Red(RGBa_Value%)
  194.         ;       Returns Red component.
  195.        
  196.         Return ((RGBa_Value% Shr 16) And 255)
  197. End Function
  198.  
  199. ;_______________________________________________________________________________________________________________________
  200. ;_______________________________________________________________________________________________________________________
  201.  
  202. Function Green(RGBa_Value%)
  203.         ;       Returns Green component.
  204.        
  205.         Return ((RGBa_Value% Shr 8) And 255)
  206. End Function
  207.  
  208. ;_______________________________________________________________________________________________________________________
  209. ;_______________________________________________________________________________________________________________________
  210.  
  211. Function Blue(RGBa_Value%)
  212.         ;       Returns Blue component.
  213.        
  214.         Return (RGBa_Value% And 255)
  215. End Function
  216.  
  217. ;_______________________________________________________________________________________________________________________
  218. ;_______________________________________________________________________________________________________________________
  219.  
  220. Function Alpha%(RGBa_Value%)
  221.         ;       Returns Alpha component.
  222.        
  223.         Return (RGBa_Value% Shr 24 And 255)
  224. End Function
  225.  
  226. ;_____________


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal