Ooops
November 28, 2020, 02:33:04 PM

Author Topic: [bb] Image/Texture To Model by GIB3D [ 1+ years ago ]  (Read 601 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Image/Texture To Model by GIB3D [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Image/Texture To Model
Author : GIB3D
Posted : 1+ years ago

Description : This may not be very useful but it was fun making it. I started this tiny project by loading an image, then I made and colored a cube for each pixel. Next I made a CreatePixel function which created a triangle mesh for each image pixel. Each vertex on the triangle were separated by the colors RGB.

Alpha wouldn't work normally with Images so I made a TextureToModel version and that worked. Then I added another pixel type that is a square. One vertex was colored using all the colors "RGBA" then the other ones were colored with separate colors for each vertex (Ex. Vertex1 = Red, Vertex2 = Green, Vertex3 = Blue, Vertex4 = White).

After that I figured I should try making a pixel that looks just like the one on my LCD screen. I then coded in a little inset to the edges of that last pixel type so that blackness can come through.

All the pixels are stored in the mesh called "Screen"

Hope you like it :D


Code :
Code: BlitzBasic
  1. Graphics3D 800,600,0,2
  2.         SetBuffer BackBuffer()
  3.         SeedRnd MilliSecs()
  4.  
  5. Const VertexLimit = 16379
  6. Global Screen = CreateMesh():EntityFX Screen,1+2+16+32
  7. Global ScreenSurface = CreateSurface(Screen)
  8.        
  9. Global Light = CreateLight()
  10.  
  11. Global PivotYaw = CreatePivot():PositionEntity PivotYaw,0,20,0:RotateEntity PivotYaw,0,180,0
  12. Global PivotPitch = CreatePivot(PivotYaw):PositionEntity PivotPitch,0,0,0:RotateEntity PivotPitch,90,0,0
  13. Global Camera = CreateCamera(PivotPitch)
  14. CameraZoom Camera,.8
  15.  
  16.  
  17. ;WARNING: If you load an image/texture that is too large, it may take a very long time for it to finish loading.
  18. ;It may (Note the "may") be safer to just use images at/under 1024x768 and textures at/under 1024x1024.
  19. ;It depends on the computer uberness...
  20.  
  21. ;Start here by entering an image/texture
  22. ;The different pixel_type numbers are specified in the CreatePixel function
  23.  
  24. ;ImageToModel("Image.png","ImageAlpha.png")
  25. TextureToModel("Texture.png",1+256,3)
  26.  
  27. Print "Hold left mouse button to use Mouse Look"
  28. Print "WASD keys to move"
  29. Print "Press any key to start"
  30. WaitKey
  31.  
  32. ;PositionEntity CreatePlane(),0,-1,0
  33.  
  34. MoveMouse GraphicsWidth()*.5,GraphicsHeight()*.5
  35. MouseXSpeed():MouseYSpeed()
  36. While Not KeyDown(1)
  37.        
  38.         If MouseHit(2)
  39.                 MoveMouse GraphicsWidth()*.5,GraphicsHeight()*.5
  40.                 MouseXSpeed():MouseYSpeed()
  41.         EndIf
  42.  
  43.         If MouseDown(2)
  44.                 FUNC_GTATurnCamera(PivotPitch,PivotYaw)
  45.         EndIf
  46.        
  47.         FUNC_MoveEntity(PivotYaw,10)
  48.        
  49.         UpdateWorld
  50.         RenderWorld
  51.        
  52.        
  53.        
  54.         Flip 0
  55.         Delay(10)
  56.        
  57. Wend
  58. FreeEntity Screen
  59. End
  60.  
  61. Function CreatePixel(x#,y#,z#,red,green,blue,alpha#=1,pixel_type=1,inset#=.01)
  62.         ;1 - Triangle Pixel
  63.         ;2 - Square Pixel
  64.         ;3 - LCD Pixel
  65.        
  66.         If CountVertices(ScreenSurface) > VertexLimit
  67.                 ScreenSurface = CreateSurface(Screen)
  68.         EndIf
  69.        
  70.         Local Surface = ScreenSurface
  71.         Local v1,v2,v3,v4
  72.        
  73.         Select pixel_type
  74.                 Case 1
  75.                         v1 = AddVertex(Surface,x,y,z);1 Left
  76.                         v2 = AddVertex(Surface,x+.5,y,z+1);0 Top
  77.                         v3 = AddVertex(Surface,x+1,y,z);2 Right
  78.                         AddTriangle(Surface,v1,v2,v3)
  79.                         VertexColor(Surface,v1,red,0,0,alpha)
  80.                         VertexColor(Surface,v2,0,green,0,alpha)
  81.                         VertexColor(Surface,v3,0,0,blue,alpha)
  82.                 Case 2
  83.                         v1 = AddVertex(Surface,x,y,z);Bottom Left
  84.                         v2 = AddVertex(Surface,x,y,z+1);Top Left
  85.                         v3 = AddVertex(Surface,x+1,y,z+1);Top Right
  86.                         v4 = AddVertex(Surface,x+1,y,z);Bottom Right
  87.                         AddTriangle(Surface,v1,v2,v3)
  88.                         AddTriangle(Surface,v4,v1,v3)
  89.                         VertexColor(Surface,v1,red,0,0,alpha)
  90.                         VertexColor(Surface,v2,0,green,0,alpha)
  91.                         VertexColor(Surface,v3,0,0,blue,alpha)
  92.                         VertexColor(Surface,v4,red,green,blue,alpha)
  93.                 Case 3
  94.                         Local N# = 1.0/3.0
  95.                        
  96.                         ;Red
  97.                         v1 = AddVertex(Surface,x+inset,y,z+inset);Bottom Left
  98.                         v2 = AddVertex(Surface,x+inset,y,z+1-inset);Top Left
  99.                         v3 = AddVertex(Surface,x+N,y,z+1-inset);Top Right
  100.                         v4 = AddVertex(Surface,x+N,y,z+inset);Bottom Right
  101.                         AddTriangle(Surface,v1,v2,v3)
  102.                         AddTriangle(Surface,v4,v1,v3)
  103.                         VertexColor(Surface,v1,red,0,0,alpha)
  104.                         VertexColor(Surface,v2,red,0,0,alpha)
  105.                         VertexColor(Surface,v3,red,0,0,alpha)
  106.                         VertexColor(Surface,v4,red,0,0,alpha)
  107.                        
  108.                         ;Green
  109.                         v1 = AddVertex(Surface,x+N,y,z+inset);Bottom Left
  110.                         v2 = AddVertex(Surface,x+N,y,z+1-inset);Top Left
  111.                         v3 = AddVertex(Surface,x+(N*2),y,z+1-inset);Top Right
  112.                         v4 = AddVertex(Surface,x+(N*2),y,z+inset);Bottom Right
  113.                         AddTriangle(Surface,v1,v2,v3)
  114.                         AddTriangle(Surface,v4,v1,v3)
  115.                         VertexColor(Surface,v1,0,green,0,alpha)
  116.                         VertexColor(Surface,v2,0,green,0,alpha)
  117.                         VertexColor(Surface,v3,0,green,0,alpha)
  118.                         VertexColor(Surface,v4,0,green,0,alpha)
  119.                        
  120.                         ;Blue
  121.                         v1 = AddVertex(Surface,x+(N*2),y,z+inset);Bottom Left
  122.                         v2 = AddVertex(Surface,x+(N*2),y,z+1-inset);Top Left
  123.                         v3 = AddVertex(Surface,x+1-inset,y,z+1-inset);Top Right
  124.                         v4 = AddVertex(Surface,x+1-inset,y,z+inset);Bottom Right
  125.                         AddTriangle(Surface,v1,v2,v3)
  126.                         AddTriangle(Surface,v4,v1,v3)
  127.                         VertexColor(Surface,v1,0,0,blue,alpha)
  128.                         VertexColor(Surface,v2,0,0,blue,alpha)
  129.                         VertexColor(Surface,v3,0,0,blue,alpha)
  130.                         VertexColor(Surface,v4,0,0,blue,alpha)
  131.                        
  132.         End Select
  133. End Function
  134.  
  135. Function FUNC_MoveEntity(entity,speed_multiplier#=1)
  136.         TFormVector (KeyDown(32)-KeyDown(30))*speed_multiplier,0,(KeyDown(17)-KeyDown(31))*speed_multiplier,Camera,0
  137.        
  138.         TranslateEntity entity,TFormedX(),TFormedY(),TFormedZ()
  139. End Function
  140.  
  141. Function FUNC_GTATurnCamera(pitch_pivot,yaw_pivot,turn_multiplier#=1)
  142.         TurnEntity yaw_pivot,0,-MouseXSpeed()*turn_multiplier,0
  143.         TurnEntity pitch_pivot,MouseYSpeed()*turn_multiplier,0,0
  144.        
  145.         RotateEntity pitch_pivot,EntityPitch(pitch_pivot),0,0
  146.        
  147.         MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
  148. End Function
  149.  
  150.  
  151. Function ImageToModel(file$,alphafile$="",pixel_type=1,inset#=.01)
  152.         Local Image = LoadImage(file)
  153.         Local ImageAlpha = LoadImage(alphafile)
  154.        
  155.         Local Width,Height
  156.         Local RGB,R,G,B,A#,Pixels,Dupe
  157.        
  158.         If Image
  159.                 DebugLog "ImageToModel("+file+")"
  160.                
  161.                 Width = ImageWidth(Image)
  162.                 Height = ImageHeight(Image)
  163.                
  164.                 If ImageAlpha
  165.                         DebugLog "ImageAlpha exists"
  166.                         LockBuffer ImageBuffer(ImageAlpha)
  167.                 EndIf
  168.                 LockBuffer ImageBuffer(Image)
  169.                         For x = 0 To Width-1
  170.                                 For y = 0 To Height-1
  171.                                         A = 1
  172.                                         If ImageAlpha
  173.                                                 RGB = ReadPixelFast(x,y,ImageBuffer(ImageAlpha))
  174.                                                 R = GetRed(RGB)
  175.                                                 G = GetGreen(RGB)
  176.                                                 B = GetBlue(RGB)
  177.                                                 A = ((R+G+B)/3.0)/255.0
  178.                                         EndIf
  179.                                        
  180.                                         RGB = ReadPixelFast(x,y,ImageBuffer(Image))
  181.                                         R = GetRed(RGB)
  182.                                         G = GetGreen(RGB)
  183.                                         B = GetBlue(RGB)
  184.                                        
  185.                                         If R Or G Or B
  186.                                                 CreatePixel((Width-x)-(Width*.5),0,y-(Height*.5),R,G,B,A,pixel_type,inset)
  187.                                         EndIf
  188.                                        
  189.                                         Pixels=Pixels+1
  190.                                 Next
  191.                         Next
  192.                 UnlockBuffer ImageBuffer(Image)
  193.                 If ImageAlpha
  194.                         UnlockBuffer ImageBuffer(ImageAlpha)
  195.                         FreeImage ImageAlpha
  196.                 EndIf
  197.                
  198.                 DebugLog "Done : "+Pixels+"Pixels"
  199.                 FreeImage Image
  200.                 Return True
  201.         EndIf
  202.        
  203.         DebugLog "File ("+file+") Not found"
  204. End Function
  205.  
  206. Function TextureToModel(file$,flags=1+2,pixel_type=1,inset#=.01)
  207.         Local Texture = LoadTexture(file,flags)
  208.        
  209.         Local Width,Height
  210.         Local RGB,R,G,B,A#,Pixels,Dupe
  211.        
  212.         If Texture
  213.                 DebugLog "TextureToModel("+file+")"
  214.                
  215.                 Width = TextureWidth(Texture)
  216.                 Height = TextureHeight(Texture)
  217.                
  218.                 LockBuffer TextureBuffer(Texture)
  219.                         For x = 0 To Width-1
  220.                                 For y = 0 To Height-1
  221.                                         RGB = ReadPixelFast(x,y,TextureBuffer(Texture))
  222.                                         R = GetRed(RGB)
  223.                                         G = GetGreen(RGB)
  224.                                         B = GetBlue(RGB)
  225.                                         A = GetAlpha(RGB)/255.0
  226.                                        
  227.                                         CreatePixel((Width-x)-(Width*.5),0,y-(Height*.5),R,G,B,A,pixel_type,inset)
  228.                                        
  229.                                         Pixels=Pixels+1
  230.                                 Next
  231.                         Next
  232.                 UnlockBuffer TextureBuffer(Texture)
  233.                
  234.                 DebugLog "Done : "+Pixels+"Pixels"
  235.                 FreeTexture Texture
  236.                 Return True
  237.         EndIf
  238.        
  239.         DebugLog "File ("+file+") Not found"
  240. End Function
  241.  
  242. Function GetRed(rgb)
  243.         Return (rgb And $FF0000) Shr 16
  244. End Function
  245.  
  246. Function GetGreen(rgb)
  247.         Return (rgb And $FF00) Shr 8
  248. End Function
  249.  
  250. Function GetBlue(rgb)
  251.         Return rgb And $FF
  252. End Function
  253.  
  254. Function GetAlpha(rgb)
  255.         Return (rgb And $FF000000) Shr 24
  256. End Function


Comments :


_PJ_(Posted 1+ years ago)

 Interesting effect, helps to show how RGB composites are made :)


GIB3D(Posted 1+ years ago)

 After I went to bed I was thinking more about it. Maybe I/we could use this to make a virtual Monitor. That'd be awesome! Changing virtual pixels in real time on a virtual monitor.


_PJ_(Posted 1+ years ago)

 What about the yellow pixels?!?! :P(j/k)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal