November 30, 2020, 01:29:32 AM

Author Topic: [bb] render scene or render screenmesh+screentexture by RemiD [ 1+ years ago ]  (Read 613 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : render scene or render screenmesh+screentexture
Author : RemiD
Posted : 1+ years ago

Description : This shows how to create a screenmesh+screentexture (useful for fullscreen effects)
If you want to remove the blured effect on the screenmesh+screentexture, you need to disable bilinear filtering ( see : <a href="../Community/posts477b.html?topic=103591" target="_blank">http://www.blitzbasic.com/Community/posts.php?topic=103591[/url] )


Code :
Code: BlitzBasic
  1. Graphics3D(1000,625,32,2)
  2.  
  3. SeedRnd(MilliSecs())
  4.  
  5. Camera = CreateCamera()
  6. CameraRange(Camera,0.1,100)
  7. CameraClsColor(Camera,000,000,000)
  8.  
  9. Global ThingsCount%
  10. Dim ThingMesh(1000)
  11.  
  12. For n% = 1 To 1000 Step 1
  13.  ThingsCount = ThingsCount + 1
  14.  I% = ThingsCount
  15.  ThingMesh(I) = CreateCube()
  16.  ScaleMesh(ThingMesh(I),1.0/2,1.0/2,1.0/2)
  17.  EntityColor(ThingMesh(I),Rand(025,255),Rand(025,255),Rand(025,255))
  18.  PositionEntity(ThingMesh(I),Rnd(-15,15),Rnd(-15,15),Rnd(-15,15),True)
  19. Next
  20.  
  21. OLight = CreateLight(2)
  22. LightRange(OLight,3.0)
  23. LightColor(OLight,224,224,224)
  24.  
  25. AmbientLight(032,032,032)
  26.  
  27. ScreenMesh = CreateMesh()
  28. Surface = CreateSurface(ScreenMesh)
  29. AddVertex(Surface,-1.0,0.625,0.0)
  30. VertexTexCoords(Surface,0,Float(0)/1024,Float(0)/1024)
  31. AddVertex(Surface,1.0,0.625,0.0)
  32. VertexTexCoords(Surface,1,Float(1000)/1024,Float(0)/1024)
  33. AddVertex(Surface,-1.0,-0.625,0.0)
  34. VertexTexCoords(Surface,2,Float(0)/1024,Float(625)/1024)
  35. AddVertex(Surface,1.0,-0.625,0.0)
  36. VertexTexCoords(Surface,3,Float(1000)/1024,Float(625)/1024)
  37. AddTriangle(Surface,0,1,2)
  38. AddTriangle(Surface,2,1,3)
  39. UpdateNormals(ScreenMesh)
  40. EntityColor(ScreenMesh,255,255,255)
  41. EntityFX(ScreenMesh,1)
  42.  
  43. ScreenTexture = CreateTexture(1024,1024,16+32+256)
  44. SetBuffer(TextureBuffer(ScreenTexture))
  45.  ClsColor(255,255,255)
  46.  Cls()
  47. ;ScaleTexture(ScreenTexture,1.0,1.0)
  48. TextureBlend(ScreenTexture,1)
  49. EntityTexture(ScreenMesh,ScreenTexture)
  50.  
  51. PositionEntity(Camera,0,0,-16,True)
  52.  
  53. Const CScene% = 1
  54. Const CScreen% = 2
  55. RenderMode% = CScene
  56.  
  57. Repeat
  58.  
  59.  MXDiff% = MouseXSpeed()
  60.  MYDiff% = MouseYSpeed()
  61.  MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2)
  62.  TurnEntity(Camera,MYDiff*0.1,-MXDiff*0.1,0)
  63.  
  64.  If( KeyHit(57)=1 )
  65.   If( RenderMode = CScene )
  66.    RenderMode = CScreen
  67.   Else If( RenderMode = CScreen )
  68.    RenderMode = CScene
  69.   EndIf
  70.  EndIf
  71.  
  72.  PositionRotateEntityLikeOtherEntity(ScreenMesh,Camera)
  73.  MoveEntity(ScreenMesh,0,0,1.0)
  74.  
  75.  WireFrame(False)
  76.  If( KeyDown(2)=1 )
  77.   WireFrame(True)
  78.  EndIf
  79.  
  80.  If( RenderMode = CScene )
  81.  
  82.   For I% = 1 To ThingsCount Step 1
  83.    ShowEntity(ThingMesh(I))
  84.   Next
  85.   HideEntity(ScreenMesh)
  86.   SetBuffer(BackBuffer())
  87.   RenderWorld()
  88.  
  89.   Color(255,255,255)
  90.   CText("RenderMode = Scene",0,0)
  91.  
  92.  Else If( RenderMode = CScreen )
  93.  
  94.   For I% = 1 To ThingsCount Step 1
  95.    ShowEntity(ThingMesh(I))
  96.   Next
  97.   HideEntity(ScreenMesh)
  98.   SetBuffer(BackBuffer())
  99.   RenderWorld()
  100.   CopyRect(0,0,GraphicsWidth(),GraphicsHeight(),0,0,BackBuffer(),TextureBuffer(ScreenTexture))
  101.  
  102.   For I% = 1 To ThingsCount Step 1
  103.    HideEntity(ThingMesh(I))
  104.   Next
  105.   ShowEntity(ScreenMesh)
  106.   SetBuffer(BackBuffer())
  107.   RenderWorld()
  108.  
  109.   Color(255,255,255)
  110.   CText("RenderMode = ScreenMesh+ScreenTexture",0,0)
  111.  
  112.  EndIf
  113.  
  114.  Flip(1)
  115.  
  116. Until( KeyDown(1)=1 )
  117.  
  118. End()
  119.  
  120. Function CText(TextStr$,PX%,PY%)
  121.  Text(PX,PY,TextStr,False,False)
  122. End Function
  123.  
  124. Function PositionEntityLikeOtherEntity(Entity,OEntity)
  125.  PositionEntity(Entity,EntityX(OEntity,True),EntityY(OEntity,True),EntityZ(OEntity,True),True)
  126. End Function
  127.  
  128. Function RotateEntityLikeOtherEntity(Entity,OEntity)
  129.  RotateEntity(Entity,EntityPitch(OEntity,True),EntityYaw(OEntity,True),EntityRoll(OEntity,True),True)
  130. End Function
  131.  
  132. Function PositionRotateEntityLikeOtherEntity(Entity,OEntity)
  133.  PositionEntity(Entity,EntityX(OEntity,True),EntityY(OEntity,True),EntityZ(OEntity,True),True)
  134.  RotateEntity(Entity,EntityPitch(OEntity,True),EntityYaw(OEntity,True),EntityRoll(OEntity,True),True)
  135. End Function


Comments :


Bobysait(Posted 1+ years ago)

 SetBuffer(TextureBuffer(ScreenTexture))You should "SetBuffer backBuffer" once the texture is readyWhatever, the filtering you're speaking of about bilinear filtering, it's just that you can't set a texture like this if you want a "pixel perfect" texture. You don't need to remove texture filtering, it's just a matter of UV settings.> Texels on texture are centered at half pixels (not at 0,0) which makes the texture blury if you start the UV at 0,0.BTW "Float(0)/1024" ... that's 0So, offset your UVs with half a texel and it will appears exactly like a direct renderworld
Code: [Select]
Local TexSize% = 1024
Local halfPixel#= 0.5/TexSize;
Local u0# = halfPixel
Local u1# = u0 + Float(GraphicsWidth())/TexSize
Local v0# = halfPixel
Local v1# = v0 + Float(GraphicsHeight())/TexSize
Local ScreenMesh= CreateMesh ( )
Local Surface = CreateSurface ( ScreenMesh )
AddVertex ( Surface, -1.0,0.625, 0.0, u0, v0 );
AddVertex ( Surface, 1.0,0.625,0.0  , u1, v0 )
AddVertex ( Surface, -1.0,-0.625,0.0, u0, v1 )
AddVertex ( Surface, 1.0,-0.625,0.0 , u1, v1 )
AddTriangle ( Surface, 0,1,2 )
AddTriangle ( Surface, 2,1,3 )
UpdateNormals ( ScreenMesh )
EntityColor ( ScreenMesh, 255,255,255 )
EntityFX ( ScreenMesh, 1 )




RemiD(Posted 1+ years ago)

 <div class="quote"> You should "SetBuffer backBuffer" once the texture is ready </div>There is no need to, since i don't need to draw on the backbuffer at this time...About the pixel perfect texture same as the render image, yes i have noticed a slight distortion but it is not really important (but thanks for the example), especially if the screenmesh+screentexture is used to create a fullscreen effect like precise glow of some pixels (of some surfaces)That's the purpose of this code and this other code :<a href="codearcscab2.html?code=3257" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=3257[/url] [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal