Painting on a Transparent Texture

Started by Hardcoal, November 01, 2021, 14:24:52

Previous topic - Next topic

Hardcoal

I dont know if theirs a better way to do it.. but i needed to draw on a Transparent  texture with the mouse..

So the trick i used is using two transparent textures.. and two polygons on top of one other
one for the mouse cursor display.. (without imprinting itself on the texture)

and the other texture is the actual texture i want to paint on..

In order to wipe each time the trace the cursor leaves i could not use CLS i had to use write pixel with special pixel value..
also wiping the whole texture is too slow so i only wiped the area were the cursor was before.. on each cycle..

anyway here is the code , and its working.

Its written In XORS but can easily be translated to blitz3d

[the blue sphere on the image is a 3d sphere , just for testing]


xAppWindowFrame (False)

xGraphics3D (1200, 800, 32)

xPhysicsDebugRender(63)

Const PNGEmptySpace_Value = 16777215

Local cam = xCreateCamera()
xPositionEntity(cam, 0, 0, -20)

Local lt = xCreateLight()
xPositionEntity(lt, 0, 0, -10)

Global spr = xCreateSphere()
xPositionEntity(spr, 3, 1, 0.5)
xEntityColor(spr, 10, 100, 112)

Global TXW = 800
Global TXH = 800

Global CrntPoly
Global PolyTexture

CreatePaintablePoly()

Global CubeCover = xcreatepoly(0)
xScaleEntity(CubeCover, 3, 3, 3)
Global CubeCoverTxt = xcreatetexture(TXW, TXH, 3)
xEntityTexture(CubeCover, CubeCoverTxt)

Global PRvX:Float
Global PRVY:Float

Global pntr = xLoadImage("media/Mainline.png")

xHidePointer()

Global KeepMX:Float = 200
Global KeepMY:Float = 200

Global SDX:Float
Global SDY:Float

While Not xKeyHit(1) Or xWinMessage("WM_CLOSE")
Local Size = 10

xRenderWorld(5)
xUpdateWorld(1)

xSetBuffer(xTextureBuffer(CubeCoverTxt))

   'Frame
xRect(2, 2, TXW - 4, TXH - 4)

SDX = KeepMX
SDY = KeepMY

   'Clear
    If SDX - Size < 0 Then SDX = Size
If SDX + Size >= xTextureWidth(CubeCoverTxt) Then SDX = xTextureWidth(CubeCoverTxt) - Size - 1
    If SDY - Size < 0 Then SDY = Size
If SDY + Size >= xTextureHeight(CubeCoverTxt) Then SDY = xTextureHeight(CubeCoverTxt) - Size - 1

Local I, J
xLockBuffer(xTextureBuffer(CubeCoverTxt))
For I = SDX - Size To SDX + Size
For J = SDY - Size To SDY + Size
xWritePixelFast(I, J, PNGEmptySpace_Value, xTextureBuffer(CubeCoverTxt))
Next
Next
xUnlockBuffer(xTextureBuffer(CubeCoverTxt))

KeepMX = KeepMX + xMouseXSpeed()
KeepMY = KeepMY + xMouseYSpeed()

       'Draw
    xDrawImage(pntr, KeepMX, KeepMY)


   'paint
If xMouseDown(1) Then

xSetBuffer(xTextureBuffer(PolyTexture))

xColor(255, 255, 255)
xOval(KeepMX, KeepMY, 5, 5, 1)
PRvX = xMouseX()
PRVY = xMouseY()

End If

xSetBuffer(xBackBuffer())

xText (100, 100, "X:" + KeepMX + " Y:" + KeepMY)

xMoveMouse(500, 500)

Delay(0.1)
xFlip()

Wend



Code