main.bb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ;Texture Paint
  2. ;David Bird
  3. ;[email protected]
  4. Graphics3D 640,480
  5. HWMultiTex True
  6. HidePointer
  7. SetBuffer BackBuffer()
  8. lit=CreateLight()
  9. LightColor lit,0,60,0
  10. PositionEntity lit,0,0,-2
  11. cam=CreateCamera()
  12. CameraRange cam,.1,1000
  13. PositionEntity cam,0,0,-3
  14. tex=LoadTexture("tex0.bmp",1)
  15. bloodtex=CreateTexture(256,256)
  16. cu=CreateSphere(30)
  17. EntityTexture cu,tex,0,0
  18. EntityTexture cu,bloodtex,0,1
  19. TextureBlend bloodtex,3
  20. EntityPickMode cu,2
  21. ;
  22. spr=CreateSprite()
  23. EntityTexture spr,tex,0,0
  24. EntityTexture spr,bloodtex,0,1
  25. PositionEntity spr,4,4,4
  26. Global ent
  27. Global tria
  28. Global surf
  29. Global sx0#,sx1#,sx2#,sxp#
  30. Global sy0#,sy1#,sy2#,syp#
  31. Global un0#,vn0#
  32. Global un1#,vn1#
  33. Global u0#,v0#
  34. Global u1#,v1#
  35. Global u2#,v2#
  36. ;WireFrame True
  37. While Not KeyDown(1)
  38. EntityTexture spr,bloodtex
  39. xm=MouseX()
  40. ym=MouseY()
  41. If MouseDown(1) Then
  42. Check_Hit(cam,xm,ym,bloodtex)
  43. End If
  44. TurnEntity cu,0,.1,0
  45. UpdateWorld
  46. RenderWorld
  47. ;draw mouse cursor
  48. Line xm-4,ym,xm+4,ym
  49. Line xm,ym-4,xm,ym+4
  50. ;draw picked triangle
  51. Line sx0,sy0,sx1,sy1
  52. Line sx1,sy1,sx2,sy2
  53. Line sx2,sy2,sx0,sy0
  54. ;plot picked point on triangle
  55. Plot sxp,syp
  56. ; Text 0,50,"0X:"+sx0+" Y: "+sy0
  57. ; Text 0,65,"1X:"+sx1+" Y: "+sy1
  58. ; Text 0,80,"2X:"+sx2+" Y: "+sy2
  59. ; Text 0,95,"PX:"+sxp+" Y: "+syp
  60. ; Text 0,110,un0+" "+vn0
  61. ; Text 0,125,un1+" "+vn1
  62. ; Text 0,140,"U0:"+u0#+" V0:"+v0#
  63. ; Text 0,155,"U1:"+u1#+" V1:"+v1#
  64. ; Text 0,170,"U2:"+u2#+" V2:"+v2#
  65. Flip
  66. Wend
  67. FreeEntity lit
  68. FreeEntity cam
  69. EndGraphics
  70. End
  71. Function Check_Hit(cam,x,y,tex)
  72. ent=CameraPick(cam,x,y)
  73. surf=PickedSurface()
  74. tria=PickedTriangle()
  75. If ent<>0 Then
  76. ;Get three tex coords from triangle
  77. u0#=VertexU(surf,TriangleVertex(surf,tria,0))
  78. u1#=VertexU(surf,TriangleVertex(surf,tria,1))-u0
  79. u2#=VertexU(surf,TriangleVertex(surf,tria,2))-u0
  80. v0#=VertexV(surf,TriangleVertex(surf,tria,0))
  81. v1#=VertexV(surf,TriangleVertex(surf,tria,1))-v0
  82. v2#=VertexV(surf,TriangleVertex(surf,tria,2))-v0
  83. x0#=VertexX(surf,TriangleVertex(surf,tria,0))
  84. x1#=VertexX(surf,TriangleVertex(surf,tria,1))
  85. x2#=VertexX(surf,TriangleVertex(surf,tria,2))
  86. y0#=VertexY(surf,TriangleVertex(surf,tria,0))
  87. y1#=VertexY(surf,TriangleVertex(surf,tria,1))
  88. y2#=VertexY(surf,TriangleVertex(surf,tria,2))
  89. z0#=VertexZ(surf,TriangleVertex(surf,tria,0))
  90. z1#=VertexZ(surf,TriangleVertex(surf,tria,1))
  91. z2#=VertexZ(surf,TriangleVertex(surf,tria,2))
  92. TFormPoint x0,y0,z0,ent,0
  93. CameraProject cam,TFormedX(),TFormedY(),TFormedZ()
  94. sx0#=ProjectedX()
  95. sy0#=ProjectedY()
  96. TFormPoint x1,y1,z1,ent,0
  97. CameraProject cam,TFormedX(),TFormedY(),TFormedZ()
  98. sx1#=ProjectedX()-sx0
  99. sy1#=ProjectedY()-sy0
  100. TFormPoint x2,y2,z2,ent,0
  101. CameraProject cam,TFormedX(),TFormedY(),TFormedZ()
  102. sx2#=ProjectedX()-sx0
  103. sy2#=ProjectedY()-sy0
  104. ; TFormPoint PickedX(),PickedY(),PickedZ(),ent,0
  105. ; CameraProject cam,TFormedX(),TFormedY(),TFormedZ()
  106. CameraProject cam,PickedX(),PickedY(),PickedZ()
  107. sxp#=ProjectedX()-sx0
  108. syp#=ProjectedY()-sy0
  109. sx0=0
  110. sy0=0
  111. ; If u1<>0 Then us1#=u1/sx1 Else us1#=0
  112. ; If v1<>0 Then vs1#=v1/sy1 Else vs1#=0
  113. ; If u2<>0 Then us2#=u2/sx2 Else us2#=0
  114. ; If v2<>0 Then vs2#=v2/sy2 Else vs2#=0
  115. us1#=u1/sx1
  116. vs1#=v1/sy1
  117. us2#=u2/sx2
  118. vs2#=v2/sy2
  119. un0#=((sxp*us1))
  120. vn0#=((syp*vs1))
  121. un1#=((sxp*us2))
  122. vn1#=((syp*vs2))
  123. If un0=0 Or vn0=0 Then
  124. Paint_Blood(tex,(u0+un1)*TextureWidth(tex),(v0+vn1)*TextureHeight(tex))
  125. Else
  126. paint_Blood(tex,(u0+un0)*TextureWidth(tex),(v0+vn0)*TextureHeight(tex))
  127. End If
  128. End If
  129. End Function
  130. Function Paint_Blood(tex,x,y)
  131. SetBuffer TextureBuffer(tex)
  132. ; LockBuffer TextureBuffer(tex)
  133. ; Col=255 Shl 16
  134. ; WritePixelFast x Mod TextureWidth(tex),y Mod TextureHeight(tex),col,TextureBuffer(tex)
  135. Color 255,0,0
  136. s#=4
  137. Oval x-s/2,y-s/2,s,s
  138. Color 255,255,255
  139. ; UnlockBuffer TextureBuffer(tex)
  140. SetBuffer BackBuffer()
  141. End Function