Poking an image buffer - Blue-ness

New to FreeBASIC? Post your questions here.
TheMG
Posts: 376
Joined: Feb 08, 2006 16:58

Poking an image buffer - Blue-ness

Postby TheMG » Aug 09, 2007 12:28

I am making a voxel engine for a CNC remake, and heres the beginnings. I got it to draw a flat texture (at a slight angle for tests). But this was very slow, so I tried Poking it right onto the screen... With this code:

Code: Select all

Poke ScreenPtr+((Core.XYZtoY(X+texX,Y+texY,Z+texX/6)*640)+Core.XYZtoX(X+texX,Y+texY,Z))*4, Point (texX, texY, ImageBuffer)


Getting rid of confusing bits:

Code: Select all

Poke ScreenPtr+(Y*640)+X)*4, PixelX


The buffer is drawn all blue-ified, can someone tell me why? BTW, this code is in Isometric.DrawImage_PlaneXY(), and you will have to get your own 600*600 texture if you want to run it... You might want to find a 150*150 one and stretch by 4, it seems to scale wierdly.

Code: Select all

ScreenRes 640, 480, 32, 1

Type VoxelCamera
   
    Dim X as Integer
    Dim Y as Integer
    Dim Z as Integer
   
    Dim W as Integer
    Dim H as Integer
   
End Type

Type VoxelEngine
   
    Dim Camera as VoxelCamera
   
End Type

Dim Shared Voxel as VoxelEngine

Voxel.Camera.W = 640
Voxel.Camera.H = 480

Namespace Core
   
    Function XYZtoY (tdPosX as Integer, tdPosY as Integer, tdPosZ as Integer) as Integer
       
        ''Converts an XYZ coordinate (array) to Y coordinates (screen)
       
        Dim xdxX as Integer = -tdPosX/4
        Dim xdxY as Integer = tdPosY/4
        Dim xdxZ as Integer = tdPosZ/2
       
        Return -((xdxX+xdxY)/2+xdxZ)-Voxel.Camera.Y+Voxel.Camera.H/2
       
    End Function
    Function XYZtoX (tdPosX as Integer, tdPosY as Integer, tdPosZ as Integer) as Integer
       
        ''Converts an XYZ coordinate (array) to X coordinates (screen)
       
        Dim xdxX as Integer = tdPosX/2
        Dim xdxY as Integer = tdPosY/2
       
        Return (xdxX+xdxY)/2-Voxel.Camera.X+Voxel.Camera.W/2
       
    End Function
   
End Namespace

Namespace Isometric
   
    Sub DrawLine(X1 as Integer, Y1 as Integer, Z1 as Integer, X2 as Integer, Y2 as Integer, Z2 as Integer, C as Integer)
       
        Line (Core.XYZtoX(X1,Y1,Z1), Core.XYZtoY(X1,Y1,Z1))-(Core.XYZtoX(X2,Y2,Z2), Core.XYZtoY(X2,Y2,Z2)), C
       
    End Sub
   
    Sub DrawImage_XYplane (X as Integer, Y as Integer, Z as integer, W as Integer, L as Integer, ImageBuffer as Any Ptr)
       
        Dim texX as Integer
        Dim texY as Integer
       
        For texX = 0 to W-1
           
            For texY = 0 to L-1
               
                ''PSet (Core.XYZtoX(X+texX,Y+texY,Z),Core.XYZtoY(X+texX,Y+texY,Z+texX/6)),Point (texX,texY, ImageBuffer)
               
                Poke ScreenPtr+((Core.XYZtoY(X+texX,Y+texY,Z+texX/6)*640)+Core.XYZtoX(X+texX,Y+texY,Z))*4, Point (texX, texY, ImageBuffer)
               
            Next
           
        Next
       
    End Sub
   
End Namespace

Dim Shared MX as Integer
Dim Shared MY as Integer

Dim Shared TextureDirt As Any Ptr
TextureDirt = ImageCreate(600,600)
BLoad "dirttexture.bmp", TextureDirt

''(480-MY-240)*2 = mouse y to coords

ScreenLock

Isometric.DrawImage_XYplane (0,0,0, 600,600, TextureDirt)

Isometric.DrawLine(-1000,0,0, 1000,0,0, RGB(255,255,255))
Isometric.DrawLine(0,-1000,0, 0,1000,0, RGB(255,255,255))
Isometric.DrawLine(0,0,-1000, 0,0,1000, RGB(255,255,255))

ScreenUnlock

Sleep
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Aug 09, 2007 12:29

Ya couldn't narrow it down for us, eh? I'll check it out :p

EDIT: This is because poke defaults to a ubyte ptr. You're going to have a specify the type of pointer your screen pointer is, which varies by bit depth. In your case, a uInteger ptr.
Last edited by anonymous1337 on Aug 09, 2007 12:31, edited 1 time in total.
TheMG
Posts: 376
Joined: Feb 08, 2006 16:58

Postby TheMG » Aug 09, 2007 12:31

I like QB stuff. Especially peek and poke. But anyway, can you think why this is happening?

EDIT: Okay, thanks (to his edit).
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Aug 09, 2007 12:32

TheMG wrote:I like QB stuff. Especially peek and poke. But anyway, can you think why this is happening?

EDIT: Okay, thanks (to his edit).
Sheesh you work fast :p
TheMG
Posts: 376
Joined: Feb 08, 2006 16:58

Postby TheMG » Aug 09, 2007 12:34

Cool, got it working :).
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » Aug 10, 2007 23:49

As far a speed goes, if you need to perform this operation numerous times, consider making a Look-Up-Table for all your poke values...

Dim Shared LUT(639,479) as Integer
Dim X as Integer
Dim Y as Integer

For Y=0 to 639
For X=0 to 479
LUT(X,Y)=(Y*640)+X)*4
Next X
Next Y

You do these calculations one time at the beginning of your program. Then you just need to access the LUT to get the proper offset for any pixel location...

ie: Offset = LUT (250, 190)

Much faster than performing two multiplications and an addition 307,200 times for each texture....

-Vince
counting_pine
Site Admin
Posts: 6190
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Aug 11, 2007 1:32

vdecampo wrote:You do these calculations one time at the beginning of your program. Then you just need to access the LUT to get the proper offset for any pixel location...

ie: Offset = LUT (250, 190)

Much faster than performing two multiplications and an addition 307,200 times for each texture....

-Vince
The problem with your suggestion is, if you're working with a 2-dimensional array, you will actually need to perform a similar set of additions/multiplications just to find the memory location of an element in it.
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » Aug 11, 2007 4:47

It's all relative. The more complex the calculations, the more benefit you will get using LUTs. I agree this is a relatively simple calculation. But the idea of using LUTs is a sound programming practice.

-Vince

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 6 guests