Qbasicnews.com

Full Version: 3d programming
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
I've been trying to understand the 3d stuff I try to learn and
for example I use the formula for 3d->2d coords from the
qbasicnews 3d tutorial:
Code:
X3 = 256 * (x2 / (Z2 + zCenter)) + xCenter
Y3 = 256 * (y2 / (Z2 + zCenter)) + yCenter

Now I wonder:

1) Is the x/y/zcenter supposed to be camera position or center
of perspective

2)I implemented it as cam coords (ie: keys up/down: inc/dec ycenter etc.)
but I didn't get any perspective based changes of the coords
ie: In gta2 the rooftops move more than the ground when you walk
because they are higher up...
The xCenter/yCenter is for the camera, it makes the view centered in screen; although its not required if you have a moving camera. So you can use it as a camera, but will require other transformations to move the camera at any angle.

What kind of model are you using to test with? I'm not quite sure why you wouldn't get the perspective difference, unless you don't have a differring x/z or y/z value.
And adding the z to zcenter makes your projection a left-handed system. ;*)

Most books uses the right-handed system. ie. Looking at negative z.

when increased:

x = right
y = up
z = into you from screen

Don't worry 2 months from now, QBCM will feature a 3d series from the basics to the advanced. Including a PSx algo in sorting which Blitz just taught me. ;*)
i can get you a better 2d to 3d conversion method using elipses.......i thought of this all by myself (which is y its kinda crazy to understand......

this isn't done, but it gives you the general idea
Code:
TYPE AxisType
  x  AS SINGLE
  y  AS SINGLE
  z  AS SINGLE
END TYPE

TYPE CoordType
axis AS AxisType ' X,Y,Z coordinates
  rx  AS SINGLE   ' X radius
  ry  AS SINGLE   ' Y radius
  a   AS SINGLE   ' last recorded angle
END TYPE

TYPE ObjectType
  centerL  AS AxisType   'Left
  centerR  AS AxisType   'Right
  centerC  AS AxisType   'Center
END TYPE

DIM CObject AS ObjectType
DIM Coord(1 TO 2, 1 TO 20) AS CoordType

DECLARE FUNCTION x2d! (x AS SINGLE, z AS SINGLE)
DECLARE FUNCTION y2d! (y AS SINGLE, z AS SINGLE)


SCREEN 12

DIM Sine(360) AS SINGLE, Cosine(360) AS SINGLE

a = 0
FOR i! = 0 TO 6.28 STEP 6.28 / 360
Sine(a) = SIN(i!)
Cosine(a) = COS(i!)
a = a + 1
NEXT

DIM Shape AS INTEGER
DIM Speed AS AxisType
DIM angle(1 TO 2) AS SINGLE
DIM hilight AS INTEGER
DIM keys AS STRING

'Clear screen
CLS
'Create Interface
LINE (3, 3)-(640, 290), 15, BF
LINE (10, 10)-(630, 280), 0, BF
LOCATE 1, 5: PRINT "ÝOutput ShapeÞ"

LINE (3, 307)-(640, 450), 15, BF
LINE (10, 317)-(630, 440), 0, BF
LOCATE 20, 5: PRINT "ÝInput DataÞ"

'Initialize Center
' The Center
CObject.centerC.x = 640 \ 2
CObject.centerC.y = 480 \ 4
CObject.centerC.z = 10 \ 2
' The Left Side
CObject.centerL.x = CObject.centerC.x + 160 * Sine(270)
CObject.centerL.y = CObject.centerC.y + 80 * Cosine(270)
CObject.centerL.z = 10 \ 2
' The Right Side
CObject.centerR.x = CObject.centerC.x + 160 * Sine(90)
CObject.centerR.y = CObject.centerC.y + 80 * Cosine(90)
CObject.centerR.z = 10 \ 2 - 2

'Initialize Shape
Shape = 5

'Initialize Radius
FOR nn = 1 TO 2
FOR n = 1 TO Shape
  Coord(nn, n).rx = 30
  Coord(nn, n).ry = 30
NEXT
NEXT
angle = 90

DO
keys = INKEY$

'Erase Previous
CIRCLE (CObject.centerL.x, CObject.centerL.y), 2, 0
LINE (CObject.centerC.x, CObject.centerC.y)-(CObject.centerR.x, CObject.centerR.y), 0
CIRCLE (CObject.centerR.x, CObject.centerR.y), 2, 0
LINE (CObject.centerC.x, CObject.centerC.y)-(CObject.centerL.x, CObject.centerL.y), 0
CIRCLE (CObject.centerL.x, CObject.centerL.y), 2, 0

angle(1) = angle(1) + .01
IF angle(1) > 360 THEN angle(1) = 0

'Center Correction
' The Right Side
CObject.centerR.z = 10 \ 2
CObject.centerL.x = x2d(CObject.centerR.x, CObject.centerR.z)
CObject.centerL.y = x2d(CObject.centerR.y, CObject.centerR.z)
CObject.centerR.x = CObject.centerC.x + 160 * Sine(angle(1))
CObject.centerR.y = CObject.centerC.y + 80 * Cosine(angle(1))
' The Left Side
angle(2) = angle(1) + 180
IF angle(2) > 360 THEN angle(2) = angle(2) - 360
CObject.centerL.z = 10 \ 2
CObject.centerL.x = x2d(CObject.centerL.x, CObject.centerL.z)
CObject.centerL.y = x2d(CObject.centerL.y, CObject.centerL.z)
CObject.centerL.x = CObject.centerC.x + 160 * Sine(angle(2))
CObject.centerL.y = CObject.centerC.y + 80 * Cosine(angle(2))

'Draw Center
CIRCLE (CObject.centerC.x, CObject.centerC.y), 2, 15
CIRCLE (CObject.centerL.x, CObject.centerL.y), 2, 15
LINE (CObject.centerC.x, CObject.centerC.y)-(CObject.centerR.x, CObject.centerR.y), 1
CIRCLE (CObject.centerR.x, CObject.centerR.y), 2, 15
LINE (CObject.centerC.x, CObject.centerC.y)-(CObject.centerL.x, CObject.centerL.y), 1
CIRCLE (CObject.centerL.x, CObject.centerL.y), 2, 15

'Erase Previous PSETs
FOR sp = 1 TO Shape
PSET (Coord(1, sp).axis.x, Coord(1, sp).axis.y), 0
NEXT
FOR sp = 1 TO Shape
PSET (Coord(2, sp).axis.x, Coord(2, sp).axis.y), 0
NEXT

'Erase Connection lines
FOR sp = 1 TO Shape
init = sp
endd = sp + 1
IF endd > Shape THEN endd = endd - Shape
LINE (x2d(Coord(1, init).axis.x, Coord(1, init).axis.z), y2d(Coord(1, init).axis.y, Coord(1, init).axis.z))-(x2d(Coord(1, endd).axis.x, Coord(1, endd).axis.z), y2d(Coord(1, endd).axis.y, Coord(1, endd).axis.z)), 0
NEXT



'Calculate voxel
'Left Side
FOR sp = 1 TO Shape
Coord(1, sp).a = sp * (360 \ Shape) - 1
Coord(1, sp).axis.x = CObject.centerL.x + Coord(1, sp).rx * Sine(Coord(1, sp).a)
Coord(1, sp).axis.y = CObject.centerL.y + Coord(1, sp).ry * Cosine(Coord(1, sp).a)
Coord(1, sp).axis.z = ((CObject.centerC.y - Coord(1, sp).axis.x) / 30) * 10
Coord(1, sp).axis.x = x2d(Coord(1, sp).axis.x, Coord(1, sp).axis.z)
Coord(1, sp).axis.y = y2d(Coord(1, sp).axis.y, Coord(1, sp).axis.z)
NEXT sp
'Right Side
FOR sp = 1 TO Shape
Coord(2, sp).a = sp * (360 \ Shape) - 1
Coord(2, sp).axis.x = CObject.centerR.x + Coord(2, sp).rx * Sine(Coord(2, sp).a)
Coord(2, sp).axis.y = CObject.centerR.y + Coord(2, sp).ry * Cosine(Coord(2, sp).a)
Coord(2, sp).axis.z = ((CObject.centerC.y - Coord(2, sp).axis.x) / 30) * 10
Coord(2, sp).axis.x = x2d(Coord(2, sp).axis.x, Coord(2, sp).axis.z)
Coord(2, sp).axis.y = y2d(Coord(2, sp).axis.y, Coord(2, sp).axis.z)
NEXT sp

'Lines Connecting
FOR sp = 1 TO Shape
init = sp
endd = sp + 1
IF endd > Shape THEN endd = endd - Shape
LINE (Coord(1, init).axis.x, Coord(1, init).axis.y)-(Coord(1, endd).axis.x, Coord(1, endd).axis.y), 2
NEXT

FOR sp = 1 TO Shape
PSET (Coord(1, sp).axis.x, Coord(1, sp).axis.y), 2
NEXT
FOR sp = 1 TO Shape
PSET (Coord(2, sp).axis.x, Coord(2, sp).axis.y), 2
NEXT

LOOP UNTIL keys = CHR$(27)
'CLS
'SCREEN 9
'LOCATE 1, 1: PRINT "End of 3D example (v1.0)"
'SYSTEM

FUNCTION x2d! (x AS SINGLE, z AS SINGLE)
IF x = 0 OR z = 0 THEN
x = -1
EXIT FUNCTION
END IF
x2d = (((x) / z) + (640 \ 2)) - 320
END FUNCTION

FUNCTION y2d! (y AS SINGLE, z AS SINGLE)
IF y = 0 OR z = 0 THEN
y = -1
EXIT FUNCTION
END IF
y2d = (((y) / z) + (480 \ 2)) - 240
END FUNCTION
Simple, eh?
eh?

Code:
FUNCTION x2d! (x AS SINGLE, z AS SINGLE)
IF x = 0 OR z = 0 THEN
x = -1
EXIT FUNCTION
END IF
x2d = (((x) / z) + (640 \ 2)) - 320
END FUNCTION

FUNCTION y2d! (y AS SINGLE, z AS SINGLE)
IF y = 0 OR z = 0 THEN
y = -1
EXIT FUNCTION
END IF
y2d = (((y) / z) + (480 \ 2)) - 240
END FUNCTION

Where's the ellipse?
the elipse is in the code.......
Code:
FOR sp = 1 TO Shape
Coord(1, sp).a = sp * (360 \ Shape) - 1
Coord(1, sp).axis.x = CObject.centerL.x + Coord(1, sp).rx * Sine(Coord(1, sp).a)
Coord(1, sp).axis.y = CObject.centerL.y + Coord(1, sp).ry * Cosine(Coord(1, sp).a)
Coord(1, sp).axis.z = ((CObject.centerC.y - Coord(1, sp).axis.x) / 30) * 10
Coord(1, sp).axis.x = x2d(Coord(1, sp).axis.x, Coord(1, sp).axis.z)
Coord(1, sp).axis.y = y2d(Coord(1, sp).axis.y, Coord(1, sp).axis.z)
NEXT sp
this calculates all the points on the elipse
the drawing is just a bit furture in the code (this is in the main module)

Oz
as a seperate note, I flipped my axis like this

z y
| /
| /
| /
--------|---------- x
/ |
/ |

This way, the main axis are x and y still........a little easier to work with....i dunno

Oz
Quote:
Code:
as a seperate note, I flipped my axis like this

           z     y
           |   /
           |  /
           | /
----------|---------- x
         / |
        /  |

This way, the main axis are x and y still........a little easier to work with....i dunno

Oz
thnx for the correction

Oz
Quote:i can get you a better 2d to 3d conversion method using elipses.......i thought of this all by myself (which is y its kinda crazy to understand......

You said 2d to 3d. And your ellipse code does not convert. And the right term for that is "Polar to cartesian"

x=cos(theta)
y=sin(theta)

for real 3d, you use the "spherical" coordinate system

x=r sin(phi) cos(theta)
y=r sin(phi) sin(theta)
z=r cos(phi)
Pages: 1 2 3