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.
Code archives/3D Graphics - Effects/2d alpha rotation and scaling realtime for b3d only sry blitz+ :(
This code has been declared by its author to be Public Domain code.
Download source code
| 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 |
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 r\rot# NTGsetalpha r\lif/255.0 DrawNTG r\ent.NTGsprite,r\x#,r\y# r\lif = r\lif - 1 r\rot# = r\rot# + r\rotspeed# r\x# = r\x# + r\vx# r\y# = r\y# + r\vy# r\vy# = r\vy# + .07 If r\y# > 480 Then r\vy# = - r\vy#*Rnd#(.5,.9) r\y# = 480 EndIf If r\lif = 0 Then FreeEntity r\ent\entity Delete r\ent.NTGsprite Delete r.rects EndIf Next If MouseDown(1) Then r.rects = New rects r\x# = MouseX() r\y# = MouseY() r\rot# = Rnd(360) r\rotspeed# = Rnd#(-10,10) r\vx# = Rnd#(-3,3) r\vy# = Rnd#(-3,4) r\lif = 255 r\ent.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 hand\entity hand\active = True PositionEntity hand\entity,NTGox# + x#*NTGpixelwidth#,0,NTGoz# + y#*NTGpixelheight RotateEntity hand\entity,0,NTGrotation#,0 ScaleEntity hand\entity, NTGscalex#,1,NTGscaley# EntityAlpha hand\entity, NTGalpha# EntityBlend hand\entity, NTGblend End Function Function UpdateNTG() For s.NTGsprite = Each NTGsprite s\active = False HideEntity s\entity 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 tmp\entity = CreateMesh() tmpsurf = CreateSurface(tmp\entity) 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 tmp\entity,r,g,b UpdateNormals tmp\entity HideEntity tmp\entity 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
Like FastImage and SpriteCandy :)
You anew invent bicycle...
You anew invent bicycle...
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.
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.
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.
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.
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 hand\entity hand\active = True PositionEntity hand\entity,NTGox# + x#*NTGpixelwidth#,0,NTGoz# + y#*NTGpixelheight RotateEntity hand\entity,0,NTGrotation#,0 ScaleEntity hand\entity, NTGscalex#,1,NTGscaley# EntityAlpha hand\entity, NTGalpha# EntityBlend hand\entity, NTGblend End Function Function UpdateNTG() For s.NTGsprite = Each NTGsprite s\active = False HideEntity s\entity 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 tmp\entity = CreateMesh() tmpsurf = CreateSurface(tmp\entity) 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 tmp\entity,r,g,b UpdateNormals tmp\entity EntityTexture tmp\entity, tmptex HideEntity tmp\entity 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 tmp\entity = CreateMesh() tmpsurf = CreateSurface(tmp\entity) 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 tmp\entity,r,g,b UpdateNormals tmp\entity EntityTexture tmp\entity, tmptex HideEntity tmp\entity 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.
Here is the final file... the file that you should include if you use it.
for a sample download the following 3 files sorry I couldn't zip them. run the one called test.bb
http://files.filefront.com/NTG+imagebb/;12232060;/fileinfo.html
http://files.filefront.com/shippng/;12232059;/fileinfo.html
http://files.filefront.com/testbb/;12232057;/fileinfo.html
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 hand\entity hand\active = True PositionEntity hand\entity,NTGox# + x#*NTGpixelwidth#,0,NTGoz# + y#*NTGpixelheight RotateEntity hand\entity,0,NTGrotation#,0 ScaleEntity hand\entity, NTGscalex#,1,NTGscaley# EntityAlpha hand\entity, NTGalpha# EntityBlend hand\entity, NTGblend EntityColor hand\entity, NTGRed, NTGGreen, NTGBlue EntityTexture hand\entity,hand\tex,frm EntityOrder hand\entity,NTGorder NTGorder = NTGorder - 1 End Function Function UpdateNTG() For s.NTGsprite = Each NTGsprite s\active = False HideEntity s\entity 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 tmp\entity = CreateMesh() tmpsurf = CreateSurface(tmp\entity) 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 tmp\entity,r,g,b UpdateNormals tmp\entity EntityTexture tmp\entity, tmptex tmp\tex = tmptex HideEntity tmp\entity 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 tmp\entity = CreateMesh() tmp\tex = tmptex tmpsurf = CreateSurface(tmp\entity) 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 tmp\entity,r,g,b UpdateNormals tmp\entity EntityTexture tmp\entity, tmptex HideEntity tmp\entity 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 tmp\entity = CreateMesh() tmp\tex = tmptex tmpsurf = CreateSurface(tmp\entity) 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 tmp\entity,r,g,b UpdateNormals tmp\entity EntityTexture tmp\entity, tmptex HideEntity tmp\entity 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 n\entity 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.bb
http://files.filefront.com/NTG+imagebb/;12232060;/fileinfo.html
http://files.filefront.com/shippng/;12232059;/fileinfo.html
http://files.filefront.com/testbb/;12232057;/fileinfo.html