SyntaxBomb - Indie Coders

Languages & Coding => Blitz Code Archives => 3D Graphics - Misc => Topic started by: RemiD on October 01, 2019, 22:25:27

Title: colors texture on UVs coordset 0, lightmap texture on UVs coordset 1
Post by: RemiD on October 01, 2019, 22:25:27

;this demonstrate how to apply a colors texture (=diffusemap) on UVs coordset 0, and a lighting shading texture (=lightmap / shadowmap) on UVs coordset 1

;graphics window
Graphics3D(1000,625,32,2)

SeedRnd(MilliSecs())

;FONTS, IMAGES, MESHES, TEXTURES, JOINTS/BONES, ANIMATIONS, SOUNDS
;---------------------------------------------------------------------------------------

;one mesh
Global XCellRenderer = MakeCellShape() ;1.0w 0.0h 1.0d square
Surface = GetSurface(XCellRenderer,1)

Global XCellCTexture = MakeCellCTex() ;colors texture, 100x100 area on a 128x128texture

Global XCellLSTexture = MakeCellLSTex() ;lighting shading texture, 10x10 area on a 16x16texture

;sets the UVs for the colors texture (="diffusemap") (the area is 100x100texels in a 128x128texture) (coordset 0)
VertexTexCoords(Surface,0,0.0/128,0.0/128,0,0)
VertexTexCoords(Surface,1,100.0/128,0.0/128,0,0)
VertexTexCoords(Surface,2,0.0/128,100.0/128,0,0)
VertexTexCoords(Surface,3,100.0/128,100.0/128,0,0)
;apply the texture on UVs 0 (coords set 0) so that the texel size is 0.01unit (for colors)
TextureCoords(XCellCTexture,0) : EntityTexture(XCellRenderer,XCellCTexture,0,0) ;: TextureBlend(XCellCTexture,1)

;sets the UVs for the lighting shading texture (="lightmap"/"shadowmap") (the area is 10x10texels in a 16x16texture) (coords set 1)
VertexTexCoords(Surface,0,0.0/16,0.0/16,0,1)
VertexTexCoords(Surface,1,10.0/16,0.0/16,0,1)
VertexTexCoords(Surface,2,0.0/16,10.0/16,0,1)
VertexTexCoords(Surface,3,10.0/16,10.0/16,0,1)
;apply the texture on UVs 1 (coords set 1) so that the texel size is 0.1unit (for lighting shading on texels)
TextureCoords(XCellLSTexture,1) : EntityTexture(XCellRenderer,XCellLSTexture,0,1) ;: TextureBlend(XCellLSTexture,3)

;to display XCellCTexture on the screen :
Global XCellCImage = CreateImage(128,128)
CopyRect(0,0,128,128,0,0,TextureBuffer(XCellCTexture),ImageBuffer(XCellCImage))
;to display XCellLSTexture on the screen :
Global XCellLSImage = CreateImage(16,16)
CopyRect(0,0,16,16,0,0,TextureBuffer(XCellLSTexture),ImageBuffer(XCellLSImage))

;---------------------------------------------------------------------------------------

;Origine
Global Origine = CreateCube()
ScaleMesh(Origine,0.01,0.01,0.01)
EntityColor(Origine,255,000,255)
EntityFX(Origine,1)
HideEntity(Origine)

;Camera
Global Camera = CreateCamera()
CameraViewport(Camera,0,0,GraphicsWidth(),GraphicsHeight())
CameraRange(Camera,0.15,150)
CameraClsColor(Camera,000,000,000)

;light
DLight = CreateLight(1)
LightColor(DLight,255,255,255)
PositionEntity(DLight,0,1000,-1000,True)
RotateEntity(DLight,45,0,0,True)

AmbientLight(025,025,025)

PositionEntity(Camera,0,1.0,-0.75,True)
RotateEntity(Camera,45,0,0,True)

Global MainLoopTimer = CreateTimer(30)

Main()

End()

Function Main()

Repeat

  WireFrame(False)
  If( KeyDown(2)=1 )
   WireFrame(True)
  EndIf

  CameraViewport(Camera,0,0,GraphicsWidth(),GraphicsHeight())
  CameraClsColor(Camera,000,000,000)
  SetBuffer(BackBuffer())
  RenderWorld()

  DrawImage(XCellCImage,0,0)
  DrawImage(XCellLSImage,0,ImageHeight(XCellCImage))

  ;Flip(1)
  WaitTimer(MainLoopTimer)
  VWait():Flip(False)

Until( KeyDown(1)=1 )

End Function

Function MakeCellShape()

Mesh = CreateMesh()
Surface = CreateSurface(Mesh)
AddVertex(Surface,-0.5,0.0,+0.5)
AddVertex(Surface,+0.5,0.0,+0.5)
AddVertex(Surface,-0.5,0.0,-0.5)
AddVertex(Surface,+0.5,0.0,-0.5)
AddTriangle(Surface,0,1,2)
AddTriangle(Surface,2,1,3)

UpdateNormals(Mesh)

Return(Mesh)

End Function

Function MakeCellCTex()

Texture = CreateTexture(128,128,1)
SetBuffer(TextureBuffer(Texture))
ClsColor(255,000,255) : Cls()
Color(050,050,050) : Rect(0,0,100,100,True)
For i% = 1 To 1000 Step 1
  TX% = Rand(0,100-10) : TY% = Rand(0,100-10)
  RGB% = Rand(050-10,050+10) : Color(RGB,RGB,RGB) : Oval(TX,TY,10,10,True)
Next
Return Texture

End Function

Function MakeCellLSTex()

R% = 255 : G% = 255 : B% = 255
Texture = CreateTexture(16,16,1)
SetBuffer(TextureBuffer(Texture))
ClsColor(000,255,255) : Cls()
For TX% = 0 To 9
  For TY% = 0 To 9
   var# = Rnd(0.1,1.0)
   Color(R*var,G*var,B*var) : Plot(TX,TY)
  Next
Next
Return Texture

End Function