October 28, 2020, 06:28:45 AM

Author Topic: [bb] DrawTexture - Like DrawImage but with TONS of real-time options by Streaksy [ 1+ years ago ]  (Read 1232 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : DrawTexture - Like DrawImage but with TONS of real-time options
Author : Streaksy
Posted : 1+ years ago

Description : Here are my beloved DrawTexture functions.  They're my babies so treat them nice.

The code is kinda messy.  It used to be mixed up with other stuff that I have remade better in seperate libraries.  Also, sorry if you come accross an obscenity or something :P  This is a lot to proof read and I have a lot of work to do :P

Oh, yeah, and changing the ViewPort messes with it.  And any cameras should be hidden before this.  DrawTexture already hides and unhides any camera called "camera" or "cam".

This makes a good include file.  Just use the following functions:

(GRAPHICSMODE MUST BE 3D, OBVIOUSLY)

DrawTexture(texture,x,y,alpha#,filter,red,green,blue,angle#,width,height,scalex#,scaley#,axisx,axisy) (Alternative to DrawImage)

Filter is the usual. 1=normal, 2=multiply, 3=add
If width and height are ommitted or left at zero, the texture will be drawn in its correct size.
ScaleX# and ScaleY# control the scale, shrinking/stretching it from its center rather than the top-left.
AxisX and AxisY are the point that it rotates around if you use Angle#.

AlphaRect(x,y,width,height,alpha#) (Filled, transparent rectangle)
Uses the current drawing colour.

DrawTextureQuad(texture,x1,y1, x2,y2, x3,y3, x4,y4, alpha#,filter, red,green,blue,u1#,v1#,u2#,v2#) (You specify coordinates of the four corners!)

DrawTextureQuadGrid(texture,x1,y1, x2,y2, x3,y3, x4,y4, alpha#,filter, red,green,blue,u1#,v1#,u2#,v2#, steps) (This draws an interpolated quad so it conforms smoothly to mad shapes.

TexLine(x1,y1 ,x2,y2, thickness, texture, ignorecolour) (Draws a line between the two points with the texture)
If ignorecolour isn't true then the line will be drawn in the current drawing colour.

TextureText(x,y,string$,centerX,centerY, alpha#, filter, shadow, angle#, scalex#, scaley#, axisx, axisy) (Draws text with effects)
This works best when drawing the same strings to the screen over many frames instead of lots of something new every frame.  You'd start getting slow-down if you drew a high random number every frame, for example.  Don't be scared of it though.  It's pretty robust.  Oh and drawing with the add-filter (filter=3) is a lot faster for such times when the string keeps changing.

GLTriangle
I don't know if this actually works..  Haven't tested it since I made this ages ago, but I imagine it draws a triangle with a texture to the three given points... probably.


Code :
Code: BlitzBasic
  1. ;NOTE: This lib has leftover junk because it's been stripped of some functions I've remade better seperately
  2.  
  3.  
  4.  
  5. Global yrat#,guicam,desktopmesh,flaggy,GLcursoralpha#=1,GLcursorhidden=0
  6. Global glcamoffset=30000
  7. Global GLInited
  8. Const GLEnabled=1 ;so apps know if they can use these commands!!
  9. Const GLLibPresent=1 ;so apps know if they can use these commands!!
  10. Global masktext=1
  11. Global DrawTextureMesh,DrawTexturePiv
  12. Global DrawTextureMeshhidden
  13. Global lastalph#=1
  14. Global lastfilter=1
  15. Const DrawTextureMethod=1
  16. Global quadded ;remember if last drawtexture command was for a quad, so it can return the UV to 0-1 instead of the custom range
  17. Dim quadgridx(513,513)
  18. Dim quadgridy(513,513)
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27. ;TEXT PLATES
  28. Const TextAlphaFlag=2 ;2 for smooth, 4 for sharp
  29. Global texplates,maxtexplates=300
  30. Dim texplate(maxtexplates)
  31. Dim texplateimg(maxtexplates)
  32. Dim texplatew(maxtexplates)
  33. Dim texplateh(maxtexplates)
  34. Dim texplatetime(maxtexplates)
  35. Dim texplatefontfullw(maxtexplates)
  36. Dim texplatefontw(maxtexplates)
  37. Dim texplatefonth(maxtexplates)
  38. Dim texplatetext$(maxtexplates)
  39. Dim texplatefilter(maxtexplates)
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. ;SILLY DEMO
  50. AppTitle "DrawTexture, DrawTextureQuad, TextureLine, Texture Text Demo"
  51. Global FPS_fpstime ;FPS()
  52. Graphics3D 1024,768,32,2
  53. SetBuffer BackBuffer()
  54. SetFont LoadFont("impact",60,1)
  55. ;ClsColor 150,150,150
  56. Cls
  57. SeedRnd MilliSecs()
  58.         w=300:h=300
  59.         For t=1 To 230
  60.         Color Rnd(255),Rnd(255),Rnd(255)
  61.         thickLine Rand(-100,w),Rand(-100,h),Rand(-100,w)/2,Rand(-100,h)/2
  62.         Next
  63.         testtex=CreateTexture(128,128,1+16+32)
  64.         CopyRect 0,0,128,128,0,0,BackBuffer(),TextureBuffer(testtex)
  65.         Cls
  66.         w=GraphicsWidth():h=GraphicsHeight()
  67.         For t=1 To 130
  68.         sh=Rand(1,3)
  69.         If Rand(1,100)=1 Then Color Rnd(255),Rnd(255),Rnd(255) Else Color Rnd(155),Rnd(155),Rnd(155)
  70.         If sh=1 Then thickLine Rand(0,w),Rand(0,h),Rand(0,w),Rand(0,h)
  71.         If sh=2 Then Rect Rand(0,w)-75,Rand(0,h)-75,150,150,1
  72.         If sh=3 Then thickOval Rand(0,w)-75,Rand(0,h)-75,150,150
  73.         Next
  74. bgpic=CreateImage(w,h):GrabImage bgpic,0,0
  75. mem=AvailVidMem()
  76. Repeat
  77. mx2#=MouseX()/2:my2#=MouseY()/2
  78. DrawBlock bgpic,0,0
  79. Color 255,0,0
  80. texturetext MouseX(),MouseY(),"Behind",1,1,.7,1,0,(mx2/2)-110,(my2/200)+1,(my2/150)+1
  81. drawtexture testtex,0,0,.8,1,(MouseX()*255)/w,(MouseY()*255)/h,255,Rand(-5,5),(mx2/2)+1,(my2/2)+1
  82. drawtexturequadgrid testtex,0,500,MouseX(),MouseY(),0,h,500,h-144,.7,3,255-(MouseDown(1)*100),255-(MouseDown(2)*100),255,5
  83. Color 255,255,255
  84. texline 0,500,MouseX(),MouseY(),20,testtex
  85. texline 500,h-144,MouseX(),MouseY(),20,testtex
  86. Color 255,255,0
  87. texturetext 100+mx2,200+my2,"Infront but transparent",0,0,.5
  88. Color 255,255,255
  89. texturetext 200+mx2,300+my2,"This text drawn with shadow",0,0,1,1,1
  90. Color 150,50,255
  91. texturetext 200+mx2,600+my2,"This text drawn with add-filter",0,0,.65,2
  92. Color 255,255,255
  93. Text 300,50,"Memory used by text: "+((mem-AvailVidMem())/1024)+"kb";,0,0,.6,2
  94. Text 0,0,"FPS: "+FPS()
  95. Flip
  96. Until KeyHit(1)
  97. End
  98. Function FPS()
  99. oldtime=FPS_fpstime
  100. FPS_fpstime=MilliSecs()
  101. elapsed=FPS_fpstime-oldtime
  102. If Not elapsed elapsed=1
  103. FPS_fps=1000/elapsed
  104. Return FPS_FPS
  105. End Function
  106. Function ThickLine(x1,y1,x2,y2,th=4)
  107. For x=-th To th
  108. For y=-th To th
  109. Line x1+x,y1+y,x2+x,y2+y
  110. Next
  111. Next
  112. End Function
  113. Function Thickoval(x1,y1,w,h,th=1)
  114. For x=-th To th
  115. For y=-th To th
  116. Oval x1+x,y1+y,w,h,0
  117. Next
  118. Next
  119. End Function
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. Function X3D#(x):Return betweengl(-1,1,Float(x)/(Float(GraphicsWidth()))):End Function
  137. Function Y3D#(y):If yrat=0 Then yrat#=Float(GraphicsHeight())/Float(GraphicsWidth())
  138. Return -betweengl(-yrat,yrat,Float(y)/(Float(GraphicsHeight()))):End Function
  139. Function directionGL#(EnX#,EnY#,OtX#,OtY#)
  140. If OtX#>EnX# And EnY#>=OtY# Then Return ATan((OtX#-EnX#)/(EnY#-OtY#))
  141. If OtX#>=EnX# And OtY#>EnY# Then Return 90+ATan((OtY#-EnY#)/(OtX#-EnX#))
  142. If EnX#>OtX# And OtY#>=EnY# Then Return 180+ATan((EnX#-OtX#)/(OtY#-EnY#))
  143. If EnX#>=OtX# And EnY#>OtY# Then Return 270+ATan((EnY#-OtY#)/(EnX#-OtX#))
  144. End Function
  145. Function distanceGL#(x1#,y1#,x2#,y2#):Return Sqr((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)):End Function
  146.  
  147.  
  148.  
  149.  
  150. Function DrawTexture(tex,x,y,alph#=1,filter=1,r=255,g=255,b=255,ang#=0,sx=0,sy=0,scx#=1,scy#=1,axisx=-1,axisy=-1)
  151. If GLInited=0 Then InitGL
  152. If axisx=-1 And axisy=-1 Then DrawTextureRaw tex,x,y,alph,filter,r,g,b,ang,sx,sy,scx,scy:Return
  153. If sx=0 Then sx=TextureWidth(tex)
  154. If sy=0 Then sy=TextureHeight(tex)
  155. If axisx=-1 Then axisx=sx/2
  156. If axisy=-1 Then axisy=sy/2
  157. x1=x+(axisx)    ;center of rotation
  158. y1=y+(axisy)
  159. x2=x+(sx/2)     ;center of texture
  160. y2=y+(sy/2)
  161. dir#=directionGL(x1,y1,x2,y2):dis#=distanceGL(x1,y1,x2,y2)
  162. x=x1+(anglex2(dir+ang)*dis)
  163. y=y1+(angley2(dir+ang)*dis)
  164. x=x-(sx/2)
  165. y=y-(sy/2)
  166. DrawTextureRaw tex,x,y,alph,filter,r,g,b,ang,sx,sy,scx,scy
  167. End Function
  168.  
  169.  
  170.  
  171. Function DrawTextureRAW(tex,x,y,alph#=1,filter=1,r=255,g=255,b=255,ang#=0,sx=0,sy=0,scx#=1,scy#=1);,axisx=-1,axisy=-1)
  172. If tex=0 Then RuntimeError "Texture doesn't exist."
  173. ;DrawTextureMethod=1
  174. dtox=x:dtoy=y
  175. If sx=0 Then sx=TextureWidth(tex)
  176. If sy=0 Then sy=TextureHeight(tex)
  177. If camera<>0 Then HideEntity camera
  178. If cam<>0 Then HideEntity cam
  179.  
  180. If DrawTextureMethod=2 Then
  181. If ang<>0 Or scx<>1 Or scy<>1 Then
  182. If axisx=0 Then tx=x-(sx*.5) Else tx=axisx
  183. If axisy=0 Then ty=y-(sy*.5) Else ty=axisy
  184. dis#=distanceGL#(dtox,dtoy,tx,ty)
  185. If (scx*sx)=(scy*sy) Then ank=-45 Else ank=directionGL(dtox,dtoy,tx,ty)
  186. x=x+anglex2(ang+ank)*dis*scx
  187. y=y+angley2(ang+ank)*dis*scy
  188. x=x+(sx*.5)
  189. y=y+(sy*.5)
  190. EndIf
  191. EndIf
  192.  
  193. If DrawTextureMeshhidden=1 Then
  194. DrawTextureMeshhidden=0
  195. ShowEntity DrawTextureMesh
  196. msh=DrawTextureMesh
  197. srf=GetSurface(msh,1)
  198. xx#=X3D(x)
  199. yy#=Y3D(y)
  200. w#=X3D#(x+sx)-xx
  201. h#=Y3D#(y+sy)-yy
  202. VertexCoords srf,0,0,0,0
  203. VertexCoords srf,1,w,0,0
  204. VertexCoords srf,2,0,h,0
  205. VertexCoords srf,3,w,h,0
  206.         If quadded Then ;return texcoords to normal after the drawtexturequad command has messed with them
  207.         lo#=.0056-.0008
  208.         hi#=1.0033
  209.         VertexTexCoords srf,0,lo,lo
  210.         VertexTexCoords srf,1,hi,lo
  211.         VertexTexCoords srf,2,lo,hi
  212.         VertexTexCoords srf,3,hi,hi
  213.         EndIf
  214. Else
  215. datex=DrawTextureMesh
  216. msh=CreateMesh():srf=CreateSurface(msh)
  217. DrawTextureMesh=msh
  218. xx#=X3D(x)
  219. yy#=Y3D(y)
  220. w#=X3D#(x+sx)-xx
  221. h#=Y3D#(y+sy)-yy
  222. lo#=.0056-.0008
  223. hi#=1.0033
  224. v1=AddVertex(srf,0,0,0,lo,lo)
  225. v2=AddVertex(srf,w,0,0,hi,lo)
  226. v3=AddVertex(srf,0,h,0,lo,hi)
  227. v4=AddVertex(srf,w,h,0,hi,hi)
  228. AddTriangle srf,v1,v2,v3
  229. AddTriangle srf,v4,v3,v2
  230. EntityFX msh,1
  231. EntityOrder msh,-1
  232. EndIf
  233.  
  234. If DrawTexturePiv=0 Then DrawTexturePiv=CreatePivot():EntityParent msh,DrawTexturePiv,1
  235.  
  236. PositionEntity DrawTexturePiv,xx,yy+glcamoffset,0,1:PositionEntity msh,0,0,0,0
  237.  
  238. EntityTexture msh,tex,0,0
  239. If lastalph#<>alph# Then EntityAlpha msh,alph#:lastalph#=alph#
  240. If lastfilter<>filter Then EntityBlend msh,filter:lastfilter=filter
  241. EntityColor msh,r,g,b
  242.  
  243. If DrawTextureMethod=1 Then
  244. If scx<>1 Or scy<>1 Or ang<>0 Then PositionMesh msh,-(w/2),-(h/2),0
  245. If scx<>1 Or scy<>1 Then ScaleMesh msh,scx,scy,1
  246. If ang<>0 Then RotateMesh msh,0,0,-ang
  247. If scx<>1 Or scy<>1 Or ang<>0 Then PositionMesh msh,(w/2)*1,(h/2)*1,0
  248. EndIf
  249.  
  250. If DrawTextureMethod=2 Then If scx<>1 Or scy<>1 Then ScaleMesh msh,scx,scy,1
  251. If DrawTextureMethod=2 Then If ang<>0 Then RotateMesh msh,0,0,-ang
  252.  
  253. If DrawTextureMethod=3 Or DrawTextureMethod=4 Then
  254. RotateEntity msh,0,0,-ang
  255. ScaleEntity msh,scx,scy,1
  256. MoveEntity msh,-(w/2)*scx,-(h/2)*scy,0
  257. TranslateEntity DrawTexturePiv,(w/2)*scx,(h/2)*scy,0
  258. EndIf
  259.  
  260. If DrawTextureMethod=4 Then
  261. oxx=xx:oyy=yy
  262. If ang<>0 Or scx<>1 Or scy<>1 Then
  263. xx=xx-(w*scx*.5)
  264. yy=yy-(h*scy*.5)
  265. dis#=distanceGL(oxx,oyy,xx,yy)
  266. If scx=scy Then ank=-45 Else ank=directionGL(oxx,oyy,xx,yy)
  267. xx=xx+anglex2(ang+ank)*dis
  268. yy=yy+angley2(ang+ank)*dis
  269. xx=xx+(w*scx)
  270. yy=yy+(h*scy)
  271. PositionEntity DrawTexturePiv,xx,yy,0,1
  272. EndIf
  273. EndIf
  274.  
  275. ShowEntity guicam:RenderWorld:UpdateGL:HideEntity guicam
  276. If camera<>0 Then ShowEntity camera
  277. If cam<>0 Then ShowEntity cam
  278. End Function
  279.  
  280. Function AngleX2#(aa#):Return Cos(aa-90):End Function
  281. Function AngleY2#(aa#):Return Sin(aa-90):End Function
  282.  
  283.  
  284.  
  285. Function UpdateGL()
  286. If DrawTextureMesh<>0 Then If DrawTextureMeshhidden=0 Then DrawTextureMeshhidden=1:HideEntity DrawTextureMesh
  287. End Function
  288.  
  289.  
  290.  
  291. Function InitGL()
  292. glinited=1
  293. guicam=CreateCamera()
  294. PositionEntity guicam,0,glcamoffset,-1
  295. CameraRange guicam,1,20000
  296. CameraProjMode guicam,2
  297. CameraZoom guicam,1
  298. ;AmbientLight 255,255,255
  299. CameraClsMode guicam,0,0
  300. ;Dither 0
  301. End Function
  302.  
  303.  
  304.  
  305.  
  306. Function AlphaRect(x,y,w,h,a#=.5,filt=1)
  307. If whitetex=0 Then
  308. oldybuf=GraphicsBuffer()
  309. whitetex=CreateTexture(2,2,1+2)
  310. SetBuffer TextureBuffer(whitetex)
  311. rrr=ColorRed()
  312. ggg=ColorGreen()
  313. bbb=ColorBlue()
  314. Color 255,255,255
  315. Rect 0,0,2,2,1
  316. SetBuffer oldybuf
  317. Color rrr,ggg,bbb
  318. EndIf
  319. DrawTexture whitetex,x,y,a,filt,ColorRed(),ColorGreen(),ColorBlue(),0,w,h,1,1
  320. End Function
  321.  
  322.  
  323. Function betweenglgl#(v1#,v2#,t#):dif#=v2-v1:Return v1+(dif*t):End Function
  324.  
  325.  
  326. Function Limglgl#(vl#,lw#,up#) ; A.K.A - Clamp
  327. If vl<lw Then Return lw
  328. If vl>up Then Return up
  329. Return vl:End Function
  330.  
  331.  
  332. Function ARGBglgl(A,R,G,B):Return RGBAglgl(r,g,b,a):End Function
  333.  
  334. Function RGBAglgl(R,G,B,A=255):If a<0 Then Return (R*256*256)+(g*256)+b
  335. Return A Shl 24 Or R Shl 16 Or G Shl 8 Or B Shl 0:End Function
  336.  
  337.  
  338.  
  339.  
  340. Function GLTriangle(x1#,y1#,x2#,y2#,x3#,y3#,fill=0)
  341. If fill=0 Then Line x1,y1,x2,y2:Line x2,y2,x3,y3:Line x3,y3,x1,y1:Return
  342. If x2<x1 Then x#=x2:y#=y2:x2=x1:y2=y1:x1=x:y1=y
  343. If x3<x1 Then x#=x3:y#=y3:x3=x1:y3=y1:x1=x:y1=y
  344. If x3<x2 Then x#=x3:y#=y3:x3=x2:y3=y2:x2=x:y2=y
  345. If x1<>x3 Then slope1#=(y3-y1)/(x3-x1)
  346. length#=x2-x1
  347. If length<>0 Then slope2#=(y2-y1)/(x2-x1):For x=0 To length:Line x+x1,x*slope1+y1,x+x1,x*slope2+y1:Next
  348. y=length*slope1+y1:length=x3-x2
  349. If length<>0 Then slope3#=(y3-y2)/(x3-x2):For x=0 To length:Line x+x2,x*slope1+y,x+x2,x*slope3+y2:Next
  350. End Function
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359. Function glLim#(vl#,lw#,up#) ; A.K.A - Clamp
  360. If vl<lw Then Return lw
  361. If vl>up Then Return up
  362. Return vl:End Function
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371. Function BetweenGL#(v1#,v2#,t#):dif#=v2-v1
  372. Return v1+(dif*t)
  373. End Function
  374.  
  375. Function TexLine(dx1,dy1,dx2,dy2,th=5,tex,ignorecolour=0)
  376. dis#=distancegl(dx1,dy1,dx2,dy2)
  377. dir#=directiongl(dx1,dy1,dx2,dy2)-90
  378. bx=betweengl(dx1,dx2,.5)
  379. by=betweengl(dy1,dy2,.5)
  380. dnx=dx1:dny=dy1:dnx=dnx+(anglex2(dir)*(th/2)):dny=dny+(angley2(dir)*(th/2))
  381. If ignorecolour=0 Then DrawTexture tex,  dnx,dny,    1,1,  ColorRed(),ColorGreen(),ColorBlue(),  dir,   dis,th,   1,1,    0,0
  382. If ignorecolour<>0 Then DrawTexture tex,  dnx,dny,    1,1,  255,255,255,  dir,   dis,th,   1,1,    0,0
  383. End Function
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392. Function DrawTextureQuad(tex,x1,y1,x2,y2,x3,y3,x4,y4,alph#=1,filter=1,r=255,g=255,b=255,ulo#=0,vlo#=0,uhi#=1,vhi#=1)
  393. quadded=1
  394. ;pass the cordinates in this order:
  395. ;  1----2
  396. ;  :    :
  397. ;  :    :
  398. ;  3----4
  399. If tex=0 Then RuntimeError "Texture doesn't exist."
  400. If GLInited=0 Then InitGL
  401. dtox=x:dtoy=y
  402. If sx=0 Then sx=TextureWidth(tex)
  403. If sy=0 Then sy=TextureHeight(tex)
  404. If camera<>0 Then HideEntity camera
  405. If cam<>0 Then HideEntity cam
  406. xx#=0
  407. yy#=0
  408. vx1#=x3d(x1)
  409. vy1#=y3d(y1)
  410. vx2#=x3d(x2)
  411. vy2#=y3d(y2)
  412. vx3#=x3d(x3)
  413. vy3#=y3d(y3)
  414. vx4#=x3d(x4)
  415. vy4#=y3d(y4)
  416.  
  417. If DrawTextureMeshhidden=1 Then ;reposition mesh
  418. DrawTextureMeshhidden=0
  419. ShowEntity DrawTextureMesh
  420. msh=DrawTextureMesh
  421. srf=GetSurface(msh,1)
  422. VertexCoords srf,0,vx1,vy1,0
  423. VertexCoords srf,1,vx2,vy2,0
  424. VertexCoords srf,2,vx3,vy3,0
  425. VertexCoords srf,3,vx4,vy4,0
  426. VertexTexCoords srf,0,ulo,vlo
  427. VertexTexCoords srf,1,uhi,vlo
  428. VertexTexCoords srf,2,ulo,vhi
  429. VertexTexCoords srf,3,uhi,vhi
  430. Else
  431. datex=DrawTextureMesh                   ;create mesh in the first place
  432. msh=CreateMesh():srf=CreateSurface(msh)
  433. DrawTextureMesh=msh
  434.  
  435. ;baselo#=.0056-.0008
  436. ;basehi#=1.0033
  437. ;xlo#=betweengl(baselo,basehi,ulo)
  438. ;ylo#=betweengl(baselo,basehi,vlo)
  439. ;xhi#=betweengl(baselo,basehi,uhi)
  440. ;yhi#=betweengl(baselo,basehi,vhi)
  441. xlo#=ulo
  442. ylo#=vlo
  443. xhi#=uhi
  444. yhi#=vhi
  445.  
  446. v1=AddVertex(srf,vx1,vy1,0,xlo,ylo)
  447. v2=AddVertex(srf,vx2,vy2,0,xhi,ylo)
  448. v3=AddVertex(srf,vx3,vy3,0,xlo,yhi)
  449. v4=AddVertex(srf,vx4,vy4,0,xhi,yhi)
  450.  
  451. AddTriangle srf,v1,v2,v3
  452. AddTriangle srf,v4,v3,v2
  453. EntityFX msh,1
  454. EntityOrder msh,-1
  455. EndIf
  456. If DrawTexturePiv=0 Then DrawTexturePiv=CreatePivot():EntityParent msh,DrawTexturePiv,1
  457. PositionEntity DrawTexturePiv,xx,yy+glcamoffset,0,1:PositionEntity msh,0,0,0,0
  458. EntityTexture msh,tex,0,0
  459. If lastalph#<>alph# Then EntityAlpha msh,alph#:lastalph#=alph#
  460. If lastfilter<>filter Then EntityBlend msh,filter:lastfilter=filter
  461. EntityColor msh,r,g,b
  462. ShowEntity guicam:RenderWorld:UpdateGL:HideEntity guicam
  463. If camera<>0 Then ShowEntity camera
  464. If cam<>0 Then ShowEntity cam
  465. End Function
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478. ;draw an interpolated quad!
  479. Function DrawTextureQuadGrid(tex,x1,y1,x2,y2,x3,y3,x4,y4,A#=1,filter=1,rd=255,gr=255,bl=255,steps=5)
  480. ;pass the cordinates in this order:
  481. ;  1----2
  482. ;  :    :
  483. ;  :    :
  484. ;  3----4
  485. steps=steps+1
  486. If steps<3 Then steps=3
  487. If steps>512 Then steps=512
  488.  
  489. For x=1 To steps                ;get the grid positions
  490. For y=1 To steps
  491. twx#=Float(x-1)/Float(steps-1)
  492. twy#=Float(y-1)/Float(steps-1)
  493. dxup=betweengl(x1,x2,twx)
  494. dyup=betweengl(y1,y2,twx)
  495. dxdn=betweengl(x3,x4,twx)
  496. dydn=betweengl(y3,y4,twx)
  497. dxlf=betweengl(x1,x3,twy)
  498. dylf=betweengl(y1,y3,twy)
  499. dxrt=betweengl(x2,x4,twy)
  500. dyrt=betweengl(y2,y4,twy)
  501. quadgridx(x,y)=betweengl(dxlf,dxrt,twx)
  502. quadgridy(x,y)=betweengl(dyup,dydn,twy)
  503. Next
  504. Next
  505.  
  506.  
  507. For x=1 To steps-1              ;draw in the quad segments
  508. For y=1 To steps-1
  509. dx1=quadgridx(x,y)
  510. dy1=quadgridy(x,y)
  511. dx2=quadgridx(x+1,y)
  512. dy2=quadgridy(x+1,y)
  513. dx3=quadgridx(x,y+1)
  514. dy3=quadgridy(x,y+1)
  515. dx4=quadgridx(x+1,y+1)
  516. dy4=quadgridy(x+1,y+1)
  517. u1#=Float(x-1) / Float(steps-1)
  518. v1#=Float(y-1) / Float(steps-1)
  519. u2#=Float(x-0) / Float(steps-1)
  520. v2#=Float(y-0) / Float(steps-1)
  521. DrawTextureQuad tex,dx1,dy1,dx2,dy2,dx3,dy3,dx4,dy4,a,filter,rd,gr,bl,u1#,v1#,u2#,v2#
  522. Next
  523. Next
  524.  
  525.  
  526. End Function
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542. Function TextureText(x,y,tx$,cx=0,cy=0,a#=1,filter=1,shadow=0,angle#=0,scalex#=1,scaley#=1,axisx=0,axisy=0)
  543. startred=ColorRed():startgreen=ColorGreen():startblue=ColorBlue()
  544.                 If shadow Then
  545.                 Color 0,0,0
  546.                 TextureText x-2,y+3,tx$,cx,cy,a*.6,1,0,angle#,scalex#,scaley#,axisx,axisy
  547.                 EndIf
  548. tw=gettexturesize(StringWidth(tx))
  549. th=gettexturesize(StringHeight(tx))
  550.  
  551. fontw=FontWidth()
  552. fonth=FontHeight()
  553. fontfullw=StringWidth(tx)
  554. If cx Then x=x-((fontfullw/2)*scalex)
  555. If cy Then y=y-((fonth/2)*scaley)
  556.                 For t=1 To texplates ;see if the exact plate already exists
  557.                 If texplate(t)<>0 Then
  558.                 If texplatew(t)=tw And texplateh(t)=th Then
  559.                 If texplatefontfullw(t)=fontfullw Then
  560.                 If texplatefontw(t)=fontw And texplatefonth(t)=fonth Then
  561.                 If texplatefilter(t)=filter Then
  562.                 If texplatetext(t)=tx Then
  563.                 clearplate=0
  564.                 daplate=t
  565.                 Goto drawplate
  566.                 EndIf
  567.                 EndIf
  568.                 EndIf
  569.                 EndIf
  570.                 EndIf
  571.                 EndIf
  572.                 Next
  573. If texplates=maxtexplates Then ;no space left, so find a plate to replace
  574.         ms=MilliSecs()
  575.         For t=1 To texplates ;find a suitable existing plate thats been disused for at least 2 minutes
  576.         If texplate(t)<>0 Then
  577.         If texplatew(t)=tw And texplateh(t)=th Then
  578.         If texplatefilter(t)=filter Then
  579.         If ms-texplatetime(t)<120000 Then
  580.         clearplate=1
  581.         daplate=t
  582.         Goto makeplate
  583.         EndIf
  584.         EndIf
  585.         EndIf
  586.         EndIf
  587.         Next
  588. oldest=0:time=0 ;find the oldest and most disused plate
  589. For t=1 To texplates
  590. If texplatetime(t)>time Then oldest=t:time=texplatetime(t)
  591. Next
  592. If oldest=0 Then Color startred,startgreen,startblue:Text x,y,tx,cx,cy:Return ;couldn't find the oldest one for some reason so give up
  593. daplate=oldest
  594. If texplate(daplate)<>0 Then FreeTexture texplate(daplate):texplate(daplate)=0
  595. If texplateimg(daplate)<>0 Then FreeImage texplateimg(daplate):texplateimg(daplate)=0
  596. Goto updateplate
  597. EndIf
  598.                                                         texplates=texplates+1:daplate=texplates ;none found so make new one
  599.                         .updateplate
  600.                         texplate(daplate)=CreateTexture(tw,th,1+((filter=1)*textalphaflag)+16+32)
  601.                         If filter=1 Then texplateimg(daplate)=CreateImage(tw,th)
  602. .makeplate
  603. buf=GraphicsBuffer()
  604.         If filter=2 Then ;add filter is simple
  605.         SetBuffer TextureBuffer(texplate(daplate))
  606.         If clearplate Then Color 0,0,0:Rect 0,0,tw,th,1
  607.         Color 255,255,255
  608.         Text 0,0,tx
  609.         SetBuffer buf
  610.         EndIf
  611. ;               If filter=2 Then ;multiply filter is simple (having problems with this)
  612. ;               SetBuffer TextureBuffer(texplate(daplate))
  613. ;               If clearplate Then Color 255,255,255:Rect 0,0,tw,th,1
  614. ;               Color startted,startgreen,startblue
  615. ;               Text 0,0,tx
  616. ;               SetBuffer buf
  617. ;               EndIf
  618.         If filter=1 Then ;normal filter requires a bit extra messing around with images cos blitz complicates it by not allowing text on alpha-capable textures
  619.         SetBuffer ImageBuffer(texplateimg(daplate))
  620.         If clearplate Then Color 0,0,0:Rect 0,0,tw,th,1
  621.         Color 255,255,255
  622.         Text 0,0,tx
  623.         SetBuffer buf
  624.         LockBuffer ImageBuffer(texplateimg(daplate))
  625.         LockBuffer TextureBuffer(texplate(daplate))
  626.         For dx=0 To tw-1
  627.         For dy=0 To th-1
  628.         hue=ReadPixelFast(dx,dy,ImageBuffer(texplateimg(daplate)))
  629.         cred=(hue And $00FF0000) Shr 16
  630.         calp=cred
  631.         hue=calp Shl 24 Or 255 Shl 16 Or 255 Shl 8 Or 255
  632.         WritePixelFast dx,dy,hue,TextureBuffer(texplate(daplate))
  633.         Next
  634.         Next
  635.         UnlockBuffer ImageBuffer(texplateimg(daplate))
  636.         UnlockBuffer TextureBuffer(texplate(daplate))
  637.         EndIf
  638. .drawplate
  639. texplatew(daplate)=tw
  640. texplateh(daplate)=th
  641. texplatefontfullw(daplate)=fontfullw
  642. texplatefontw(daplate)=fontw
  643. texplatefonth(daplate)=fonth
  644. texplatetext(daplate)=tx
  645. texplatefilter(daplate)=filter
  646. texplatetime(daplate)=MilliSecs()
  647. If filter=2 Then filter=3
  648. drawtexture texplate(daplate),x,y,a,filter,startred,startgreen,startblue,angle#,0,0,scalex#,scaley#,axisx,axisy
  649. Color startred,startgreen,startblue
  650. End Function
  651.  
  652.  
  653.  
  654. Function GetTextureSize(s) ;returns the smallest texture size that will encompass the given size.
  655. power=2^18:oldpower=power
  656. Repeat
  657. If s=>power Then Return oldpower
  658. oldpower=power:power=power/2
  659. Until power=1
  660. Return 1
  661. End Function


Comments :


Streaksy(Posted 1+ years ago)

 Just added a small demo to the code...


Tranz(Posted 1+ years ago)

 Very, very nice functions!I thank you for sharing them.


Streaksy(Posted 1+ years ago)

 *salute*


Nate the Great(Posted 1+ years ago)

 wow, this is truly awesome and a must have in my b3d 'awesome code' folder


_PJ_(Posted 1+ years ago)

 The idea behind this is amazing, but the functions are quite complex ,so I'm having a little difficulty trying to understand what's going on in order to use them in my own code...The demo doesn't seem to be running properly, I don't et any text, just a diagonal line shown on the top-left corner of the screen in various colours :S


puki(Posted 1+ years ago)

 The code is currently working fine.  You should be seeing this:<a href="
" target="_blank">
[/url]


_PJ_(Posted 1+ years ago)

 Oh dear... then something must be really messed up for me :(I literally just copy & pasted the code as-is from above, and this is what I get when it runs:Any ideas on just what this might be?It MIGHT just be something to do with hiding/showing of or using multiple cameras... not sure why but I recall something else no longer working for me (that used to work fine some time ago) that uses this technique


_PJ_(Posted 1+ years ago)

 hrm.. seems to be a lot to do with the background drawing...Possibly a result of the numerous ForNext loops to draw almost 16000 lines and 8000 ovals?
Code: [Select]
w=300:h=300
;For t=1 To 230
;Color Rnd(255),Rnd(255),Rnd(255)
;thickLine(Rand(-100,w),Rand(-100,h),Rand(-100,w)/2,Rand(-100,h)/2)
;Next
testtex=CreateTexture(128,128,1+16+32)
CopyRect 0,0,128,128,0,0,BackBuffer(),TextureBuffer(testtex)
Cls
w=GraphicsWidth():h=GraphicsHeight()
;For t=1 To 130
;sh=Rand(1,3)
;If Rand(1,100)=1 Then Color Rnd(255),Rnd(255),Rnd(255) Else Color Rnd(155),Rnd(155),Rnd(155)
;If sh=1 Then thickLine Rand(0,w),Rand(0,h),Rand(0,w),Rand(0,h)
;If sh=2 Then Rect Rand(0,w)-75,Rand(0,h)-75,150,150,1
;If sh=3 Then thickOval Rand(0,w)-75,Rand(0,h)-75,150,150
;Next
With the repetitious for/next loops removed, I get the text showing up - very nice effects :) [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal