SyntaxBomb - Indie Coders

Languages & Coding => Blitz Code Archives => 3D Graphics - Effects => Topic started by: BlitzBot on June 29, 2017, 00:28:40

Title: [bb] 2d alpha rotation and scaling realtime for b3d only sry blitz+ :( by Nate the Great [ 1+ years ago ]
Post by: BlitzBot on June 29, 2017, 00:28:40
Title : 2d alpha rotation and scaling realtime for b3d only sry blitz+ :(
Author : Nate the Great
Posted : 1+ years ago

Description : This is a free 2d alpha engine only in 3d mode.  It uses multi layered rendering and 3d rectangles (not blitz sprites) I am working on incorperating textures in at the moment. Here is a list of the functions. (NTG stands for Nate the Great) :)

DrawNTG
UpdateNTG
NTGsetblend
NTGsetrotation
NTGsetscale
NTGsetalpha
NTGCreateRect
NTGinit2dAlpha


Code :
Code (blitzbasic) Select
Graphics3D 640,480,0,2

lit = CreateLight(2)

MoveEntity lit,0,-101,0

SetBuffer BackBuffer()

Global NTGox#
Global NTGoz#
Global NTGpixelwidth#
Global NTGpixelheight#
Global NTGinit = False
Global NTGplane
Global NTGcam
Global NTGheight#
Global NTGrotation#
Global NTGscalex# = 1
Global NTGscaley# = 1
Global NTGalpha# = 1
Global NTGblend = 3

Type NTGsprite
Field entity,active
End Type


NTGinit2dAlpha()
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||





Type rects
Field x#,y#,rot#,rotspeed#,vx#,vy#,lif,ent.NTGsprite
End Type



rect1.NTGsprite = NTGcreateRect.NTGsprite(40,40)

cnt# = 1

timer = MilliSecs()
While Not KeyDown(1)
Cls

NTGsetrotation cnt#
NTGsetscale Sin(cnt#),Sin(cnt#)
NTGsetalpha Sin(cnt#/2)/2 + .5
DrawNTG rect1.NTGsprite,MouseX(),MouseY()

NTGsetscale 1,1

For r.rects = Each rects
NTGsetrotation rot#
NTGsetalpha rlif/255.0
DrawNTG rent.NTGsprite,rx#,ry#
rlif = rlif - 1
rot# = rot# + rotspeed#
rx# = rx# + rvx#
ry# = ry# + rvy#
rvy# = rvy# + .07
If ry# > 480 Then
rvy# = - rvy#*Rnd#(.5,.9)
ry# = 480
EndIf

If rlif = 0 Then
FreeEntity rententity
Delete rent.NTGsprite
Delete r.rects
EndIf
Next

If MouseDown(1) Then
r.rects = New rects
rx# = MouseX()
ry# = MouseY()
rot# = Rnd(360)
rotspeed# = Rnd#(-10,10)
rvx# = Rnd#(-3,3)
rvy# = Rnd#(-3,4)
rlif = 255
rent.NTGsprite = NTGCreateRect.NTGsprite(40,40,255,0,255)
EndIf

cnt = cnt + 1
If cnt = 720 Then cnt = 0

UpdateWorld()
RenderWorld()
UpdateNTG()

.stt1
If MilliSecs()-timer > 0 Then
.stt
x = 1000/(MilliSecs()-timer)
If x > 58 Then Goto stt
Text 1,1,x
Else
Goto stt1
EndIf
timer = MilliSecs()

Flip
Wend


Flip
WaitKey()









;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||










Function DrawNTG(hand.NTGsprite,x#,y#)

ShowEntity handentity
handactive = True
PositionEntity handentity,NTGox# + x#*NTGpixelwidth#,0,NTGoz# + y#*NTGpixelheight
RotateEntity handentity,0,NTGrotation#,0
ScaleEntity handentity, NTGscalex#,1,NTGscaley#
EntityAlpha handentity, NTGalpha#
EntityBlend handentity, NTGblend

End Function


Function UpdateNTG()

For s.NTGsprite = Each NTGsprite
sactive = False
HideEntity sentity
Next

End Function

Function NTGSetBlend(bld)
NTGblend = bld
End Function

Function NTGSetRotation(Rot# = 0)
NTGrotation = rot#
End Function

Function NTGsetscale(sclx# = 1,scly# = 1)
NTGscalex# = sclx#
NTGscaley# = scly#
End Function

Function NTGsetalpha(alph# = 1)
NTGalpha# = alph#
End Function


Function NTGCreateRect.NTGsprite(width#,height#,r = 255,g = 255,b = 255)

tmp.NTGsprite = New NTGsprite
tmpentity = CreateMesh()
tmpsurf = CreateSurface(tmpentity)
width# = width# / 2
height# = height# / 2
v1 = AddVertex(tmpsurf,0-(width#*NTGpixelwidth),NTGheight#,0-(height#*NTGpixelheight#))
v2 = AddVertex(tmpsurf,(width#*NTGpixelwidth),NTGheight#,0-(height#*NTGpixelheight#))
v3 = AddVertex(tmpsurf,(width#*NTGpixelwidth),NTGheight#,(height#*NTGpixelheight#))
v4 = AddVertex(tmpsurf,0-(width#*NTGpixelwidth),NTGheight#,(height#*NTGpixelheight#))


AddTriangle(tmpsurf,v3,v4,v2)
AddTriangle(tmpsurf,v2,v4,v1)

EntityColor tmpentity,r,g,b

UpdateNormals tmpentity

HideEntity tmpentity

Return tmp.NTGsprite

End Function









Function NTGinit2dAlpha(maxrng# = 1000)

NTGcam = CreateCamera()
NTGinit = True

If NTGcam = 0 Then RuntimeError("Camera does not exist.")

NTGplane = CreatePlane()

TurnEntity NTGplane,-90,0,0
MoveEntity NTGplane,0,-2,0
EntityParent NTGplane,NTGcam
EntityAlpha NTGplane,0
EntityPickMode NTGplane,2

CameraRange NTGcam,.01,maxrng#
TurnEntity NTGcam,90,0,0
MoveEntity NTGcam,0,0,100
CameraZoom NTGcam,3
CameraClsMode NTGcam,0,1

CameraPick NTGcam,1,1

x1# = PickedX()
z1# = PickedZ()
NTGheight# = PickedY()

CameraPick NTGcam,GraphicsWidth(),GraphicsHeight()

x2# = PickedX()
z2# = PickedZ()


NTGox# = x1#
NTGoz# = z1#

NTGpixelwidth# = (x2#-x1#)/GraphicsWidth()
NTGpixelheight# = (z2#-z1#)/GraphicsHeight()

End Function


Comments :


Nate the Great(Posted 1+ years ago)

 I guess what I am trying to do here is write something that makes blitz 3d more like blitz max.  One thing I didn't mention before is that you can just make a normal camera before you call the init function and it will act like normal on your game. and I think it eliminates the alpha problem between the camera for the 2d fx and the normal camera so your sprites will never look like they are behind the water wich they are obviously not behind.  I also have locked it at 58 fps because it keeps it from leaping between 58 and 1000 fps on my machine... it is very unstable.


MikhailV(Posted 1+ years ago)

 Like FastImage and SpriteCandy :)You anew invent bicycle...


Nate the Great(Posted 1+ years ago)

 yeah... it was just for the learning experience. I got the idea from fastimage because I like to know how every part of my game works... I even made a small 3d engine for blitzbasic when it first came out. I wish I had known about this blog earlier :)P.S. I know it isn't as fast as fastimage but it can handle 200 sprites easily which serves my purpose especially since I have added a load2dsprite function for images.


Nate the Great(Posted 1+ years ago)

 ok I added NTGloadimage now this will be more useful... here is the code.P.S. you have to replace the name of my image with the name of your own image for this to work.after you move the mouse around a little hold the space bar down and move the mouse around some more.



Graphics3D 640,480,0,2

lit = CreateLight(2)

MoveEntity lit,0,-101,0

SetBuffer BackBuffer()

Global NTGox#
Global NTGoz#
Global NTGpixelwidth#
Global NTGpixelheight#
Global NTGinit = False
Global NTGplane
Global NTGcam
Global NTGheight#
Global NTGrotation#
Global NTGscalex# = 1
Global NTGscaley# = 1
Global NTGalpha# = 1
Global NTGblend = 3

Type NTGsprite
Field entity,active
End Type


NTGinit2dAlpha()
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||


img1.NTGsprite = NTGLoadImage.NTGsprite("img1.jpg",256) ;PUT YOUR PICTURE HERE :) HOLD DOWN THE SPACE BAR AFTER YOU RUN THIS ONE

While Not KeyDown(1)
Cls

DrawNTG img1.NTGsprite,MouseX(),MouseY()

If KeyDown(57) Then
NTGSetRotation MouseX()
NTGsetalpha MouseY()/480.0
EndIf

UpdateWorld()
RenderWorld()
UpdateNTG()

.stt1
If MilliSecs()-timer > 0 Then
.stt
x = 1000/(MilliSecs()-timer)
If x > 58 Then Goto stt
Text 1,1,x
Else
Goto stt1
EndIf
timer = MilliSecs()

Flip
Wend


Flip
WaitKey()









;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||










Function DrawNTG(hand.NTGsprite,x#,y#)

ShowEntity handentity
handactive = True
PositionEntity handentity,NTGox# + x#*NTGpixelwidth#,0,NTGoz# + y#*NTGpixelheight
RotateEntity handentity,0,NTGrotation#,0
ScaleEntity handentity, NTGscalex#,1,NTGscaley#
EntityAlpha handentity, NTGalpha#
EntityBlend handentity, NTGblend

End Function


Function UpdateNTG()

For s.NTGsprite = Each NTGsprite
sactive = False
HideEntity sentity
Next

End Function

Function NTGSetBlend(bld)
NTGblend = bld
End Function

Function NTGSetRotation(Rot# = 0)
NTGrotation = rot#
End Function

Function NTGsetscale(sclx# = 1,scly# = 1)
NTGscalex# = sclx#
NTGscaley# = scly#
End Function

Function NTGsetalpha(alph# = 1)
NTGalpha# = alph#
End Function


Function NTGCreateRect.NTGsprite(width#,height#,r = 255,g = 255,b = 255)

tmptex = CreateTexture(64,64)
SetBuffer TextureBuffer(tmptex)

Color r,g,b
Rect 0,0,65,65,1

SetBuffer BackBuffer()

tmp.NTGsprite = New NTGsprite
tmpentity = CreateMesh()

tmpsurf = CreateSurface(tmpentity)
width# = width# / 2
height# = height# / 2
v1 = AddVertex(tmpsurf,0-(width#*NTGpixelwidth),NTGheight#,0-(height#*NTGpixelheight#),0,0)
v2 = AddVertex(tmpsurf,(width#*NTGpixelwidth),NTGheight#,0-(height#*NTGpixelheight#),1,0)
v3 = AddVertex(tmpsurf,(width#*NTGpixelwidth),NTGheight#,(height#*NTGpixelheight#),1,1)
v4 = AddVertex(tmpsurf,0-(width#*NTGpixelwidth),NTGheight#,(height#*NTGpixelheight#),0,1)


AddTriangle(tmpsurf,v3,v4,v2)
AddTriangle(tmpsurf,v2,v4,v1)

;EntityColor tmpentity,r,g,b

UpdateNormals tmpentity

EntityTexture tmpentity, tmptex

HideEntity tmpentity

Return tmp.NTGsprite

End Function



Function NTGLoadImage.NTGsprite(imageext$,texw = 64)

tmpimg = LoadImage(imageext$)
width# = ImageWidth(tmpimg)
height# = ImageHeight(tmpimg)

ResizeImage tmpimg,texw,texw

tmptex = CreateTexture(texw,texw)
SetBuffer TextureBuffer(tmptex)

DrawImage tmpimg,1,1

SetBuffer BackBuffer()

tmp.NTGsprite = New NTGsprite
tmpentity = CreateMesh()

tmpsurf = CreateSurface(tmpentity)
width# = width# / 2
height# = height# / 2
v1 = AddVertex(tmpsurf,0-(width#*NTGpixelwidth),NTGheight#,0-(height#*NTGpixelheight#),0,0)
v2 = AddVertex(tmpsurf,(width#*NTGpixelwidth),NTGheight#,0-(height#*NTGpixelheight#),1,0)
v3 = AddVertex(tmpsurf,(width#*NTGpixelwidth),NTGheight#,(height#*NTGpixelheight#),1,1)
v4 = AddVertex(tmpsurf,0-(width#*NTGpixelwidth),NTGheight#,(height#*NTGpixelheight#),0,1)


AddTriangle(tmpsurf,v3,v4,v2)
AddTriangle(tmpsurf,v2,v4,v1)

;EntityColor tmpentity,r,g,b

UpdateNormals tmpentity

EntityTexture tmpentity, tmptex

HideEntity tmpentity

Return tmp.NTGsprite



End Function






Function NTGinit2dAlpha(maxrng# = 1000)

NTGcam = CreateCamera()
NTGinit = True

If NTGcam = 0 Then RuntimeError("Camera does not exist.")

NTGplane = CreatePlane()

TurnEntity NTGplane,-90,0,0
MoveEntity NTGplane,0,-2,0
EntityParent NTGplane,NTGcam
EntityAlpha NTGplane,0
EntityPickMode NTGplane,2

CameraRange NTGcam,.01,maxrng#
TurnEntity NTGcam,90,0,0
MoveEntity NTGcam,0,0,100
CameraZoom NTGcam,3
CameraClsMode NTGcam,0,1

CameraPick NTGcam,1,1

x1# = PickedX()
z1# = PickedZ()
NTGheight# = PickedY()

CameraPick NTGcam,GraphicsWidth(),GraphicsHeight()

x2# = PickedX()
z2# = PickedZ()


NTGox# = x1#
NTGoz# = z1#

NTGpixelwidth# = (x2#-x1#)/GraphicsWidth()
NTGpixelheight# = (z2#-z1#)/GraphicsHeight()

End Function
Oh... and By the way... I would recomend loading all sprites before the game not ingame because they use resize image which causes major slowdown.If anyone has anything they want me to add please tell me. I love a challenge.


Nate the Great(Posted 1+ years ago)

 Here is the final file... the file that you should include if you use it.Global NTGox#
Global NTGoz#
Global NTGpixelwidth#
Global NTGpixelheight#
Global NTGinit = False
Global NTGplane
Global NTGcam
Global NTGheight#
Global NTGrotation#
Global NTGscalex# = 1
Global NTGscaley# = 1
Global NTGalpha# = 1
Global NTGblend = 1
Global NTGBlue = 255
Global NTGGreen = 255
Global NTGRed = 255
Global NTGorder = 0

Type NTGsprite
Field entity,active,tex
End Type






Function DrawNTG(hand.NTGsprite,x#,y#,frm = 0)

ShowEntity handentity
handactive = True
PositionEntity handentity,NTGox# + x#*NTGpixelwidth#,0,NTGoz# + y#*NTGpixelheight
RotateEntity handentity,0,NTGrotation#,0
ScaleEntity handentity, NTGscalex#,1,NTGscaley#
EntityAlpha handentity, NTGalpha#
EntityBlend handentity, NTGblend
EntityColor handentity, NTGRed, NTGGreen, NTGBlue
EntityTexture handentity,hand ex,frm
EntityOrder handentity,NTGorder
NTGorder = NTGorder - 1

End Function


Function UpdateNTG()

For s.NTGsprite = Each NTGsprite
sactive = False
HideEntity sentity
Next

NTGorder = 0

End Function

Function NTGsetcolor(r,g,b)

NTGred = r
NTGgreen = g
NTGblue = b

End Function

Function NTGSetBlend(bld)
NTGblend = bld
End Function

Function NTGSetRotation(Rot# = 0)
NTGrotation = rot#
End Function

Function NTGsetscale(sclx# = 1,scly# = 1)
NTGscalex# = sclx#
NTGscaley# = scly#
End Function

Function NTGsetalpha(alph# = 1)
NTGalpha# = alph#
End Function








Function NTGCreateRect.NTGsprite(width#,height#,r = 255,g = 255,b = 255)

tmptex = CreateTexture(64,64)
SetBuffer TextureBuffer(tmptex)

Color r,g,b
Rect 0,0,65,65,1

SetBuffer BackBuffer()

tmp.NTGsprite = New NTGsprite
tmpentity = CreateMesh()

tmpsurf = CreateSurface(tmpentity)
width# = width# / 2
height# = height# / 2
v1 = AddVertex(tmpsurf,0-(width#*NTGpixelwidth),NTGheight#,0-(height#*NTGpixelheight#),0,0)
v2 = AddVertex(tmpsurf,(width#*NTGpixelwidth),NTGheight#,0-(height#*NTGpixelheight#),1,0)
v3 = AddVertex(tmpsurf,(width#*NTGpixelwidth),NTGheight#,(height#*NTGpixelheight#),1,1)
v4 = AddVertex(tmpsurf,0-(width#*NTGpixelwidth),NTGheight#,(height#*NTGpixelheight#),0,1)


AddTriangle(tmpsurf,v3,v4,v2)
AddTriangle(tmpsurf,v2,v4,v1)

;EntityColor tmpentity,r,g,b

UpdateNormals tmpentity

EntityTexture tmpentity, tmptex
tmp ex = tmptex

HideEntity tmpentity

Return tmp.NTGsprite

End Function













Function NTGLoadImage.NTGsprite(imageext$)

tmpimg = LoadImage(imageext$)
width# = ImageWidth(tmpimg)
height# = ImageHeight(tmpimg)


tmptex = LoadTexture(imageext$,4)

tmp.NTGsprite = New NTGsprite
tmpentity = CreateMesh()
tmp ex = tmptex
tmpsurf = CreateSurface(tmpentity)
width# = width# / 2
height# = height# / 2
v1 = AddVertex(tmpsurf,0-(width#*NTGpixelwidth),NTGheight#,0-(height#*NTGpixelheight#),0,0)
v2 = AddVertex(tmpsurf,(width#*NTGpixelwidth),NTGheight#,0-(height#*NTGpixelheight#),1,0)
v3 = AddVertex(tmpsurf,(width#*NTGpixelwidth),NTGheight#,(height#*NTGpixelheight#),1,1)
v4 = AddVertex(tmpsurf,0-(width#*NTGpixelwidth),NTGheight#,(height#*NTGpixelheight#),0,1)


AddTriangle(tmpsurf,v3,v4,v2)
AddTriangle(tmpsurf,v2,v4,v1)

;EntityColor tmpentity,r,g,b

UpdateNormals tmpentity

EntityTexture tmpentity, tmptex

HideEntity tmpentity

Return tmp.NTGsprite



End Function










Function NTGLoadAnimImage.NTGsprite(imageext$,w,h,f,c)

tmpimg = LoadImage(imageext$)
width# = ImageWidth(tmpimg)
height# = ImageHeight(tmpimg)


tmptex = LoadAnimTexture(imageext$,4,w,h,f,c)

tmp.NTGsprite = New NTGsprite
tmpentity = CreateMesh()
tmp ex = tmptex

tmpsurf = CreateSurface(tmpentity)
width# = width# / 2
height# = height# / 2
v1 = AddVertex(tmpsurf,0-(width#*NTGpixelwidth),NTGheight#,0-(height#*NTGpixelheight#),0,0)
v2 = AddVertex(tmpsurf,(width#*NTGpixelwidth),NTGheight#,0-(height#*NTGpixelheight#),1,0)
v3 = AddVertex(tmpsurf,(width#*NTGpixelwidth),NTGheight#,(height#*NTGpixelheight#),1,1)
v4 = AddVertex(tmpsurf,0-(width#*NTGpixelwidth),NTGheight#,(height#*NTGpixelheight#),0,1)


AddTriangle(tmpsurf,v3,v4,v2)
AddTriangle(tmpsurf,v2,v4,v1)

;EntityColor tmpentity,r,g,b

UpdateNormals tmpentity

EntityTexture tmpentity, tmptex

HideEntity tmpentity

Return tmp.NTGsprite



End Function











Function NTGinit2dAlpha(maxrng# = 1000)

NTGcam = CreateCamera()
NTGinit = True

If NTGcam = 0 Then RuntimeError("Camera does not exist.")

NTGplane = CreatePlane()

TurnEntity NTGplane,-90,0,0
MoveEntity NTGplane,0,-2,0
EntityParent NTGplane,NTGcam
EntityAlpha NTGplane,0
EntityPickMode NTGplane,2

CameraRange NTGcam,.01,maxrng#
TurnEntity NTGcam,90,0,0
MoveEntity NTGcam,0,0,100
CameraZoom NTGcam,3
CameraClsMode NTGcam,0,1

CameraPick NTGcam,1,1

x1# = PickedX()
z1# = PickedZ()
NTGheight# = PickedY()

CameraPick NTGcam,GraphicsWidth(),GraphicsHeight()

x2# = PickedX()
z2# = PickedZ()


NTGox# = x1#
NTGoz# = z1#

NTGpixelwidth# = (x2#-x1#)/GraphicsWidth()
NTGpixelheight# = (z2#-z1#)/GraphicsHeight()


lit = CreateLight(3)

MoveEntity lit,0,-101,0
TurnEntity lit,90,0,0

End Function






Function EndNTG()

For n.NTGsprite = Each NTGsprite
FreeEntity nentity
Delete n.NTGsprite
Next

End Function






Function ScaleImageFast(SrcImage, ScaleX#, ScaleY#, ExactSize=False)

Local SrcWidth,  SrcHeight
Local DestWidth, DestHeight
Local ScratchImage, DestImage
Local SrcBuffer, ScratchBuffer, DestBuffer
Local X1, Y1, X2, Y2

; Get the width and height of the source image.
SrcWidth  = ImageWidth(SrcImage)
SrcHeight = ImageHeight(SrcImage)

; Calculate the width and height of the dest image, or the scale.
If ExactSize = False

DestWidth  = Floor(SrcWidth  * ScaleX#)
DestHeight = Floor(SrcHeight * ScaleY#)

Else

DestWidth  = ScaleX#
DestHeight = ScaleY#

ScaleX# = Float(DestWidth)  / Float(SrcWidth)
ScaleY# = Float(DestHeight) / Float(SrcHeight)

EndIf

; If the image does not need to be scaled, just copy the image and exit the function.
If (SrcWidth = DestWidth) And (SrcHeight = DestHeight) Then Return CopyImage(SrcImage)

; Create a scratch image that is as tall as the source image, and as wide as the destination image.
ScratchImage = CreateImage(DestWidth, SrcHeight)

; Create the destination image.
DestImage = CreateImage(DestWidth, DestHeight)

; Get pointers to the image buffers.
SrcBuffer     = ImageBuffer(SrcImage)
ScratchBuffer = ImageBuffer(ScratchImage)
DestBuffer    = ImageBuffer(DestImage)

; Duplicate columns from source image to scratch image.
For X2 = 0 To DestWidth-1
X1 = Floor(X2 / ScaleX#)
CopyRect X1, 0, 1, SrcHeight, X2, 0, SrcBuffer, ScratchBuffer
Next

; Duplicate rows from scratch image to destination image.
For Y2 = 0 To DestHeight-1
Y1 = Floor(Y2 / ScaleY#)
CopyRect 0, Y1, DestWidth, 1, 0, Y2, ScratchBuffer, DestBuffer
Next

; Free the scratch image.
FreeImage ScratchImage

; Return the new image.
Return DestImage

End Function
for a sample download the following 3 files sorry I couldn't zip them.  run the one called test.bbhttp://files.filefront.com/NTG+imagebb/;12232060;/fileinfo.htmlhttp://files.filefront.com/shippng/;12232059;/fileinfo.htmlhttp://files.filefront.com/testbb/;12232057;/fileinfo.html [/i]