Qbasicnews.com
February 26, 2020, 12:03:06 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 2 [3] 4
  Print  
Author Topic: Triangle routine gone haywire  (Read 10518 times)
Ninkazu
Been there, done that
*****
Posts: 1169



WWW
« Reply #30 on: May 30, 2003, 02:56:02 PM »

No, I've seen a realtime raytracing engine that doesn't use triangles, vertices, or anything. I'll post the link when I have time. Right now I have to go.
Logged

am an asshole. Get used to it.
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #31 on: May 30, 2003, 05:14:20 PM »

Raytracing doesn't use triangles.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Ninkazu
Been there, done that
*****
Posts: 1169



WWW
« Reply #32 on: May 30, 2003, 08:54:36 PM »

Quote from: "na_th_an"
Raytracing doesn't use triangles.


Oh well, it's still cool http://www.realstorm.de
Logged

am an asshole. Get used to it.
Ninkazu
Been there, done that
*****
Posts: 1169



WWW
« Reply #33 on: May 31, 2003, 04:42:20 PM »

Thanks, Rel. I looked through your flat shading program and so far I've been able to do this:

Code:
DEFINT A-Z
DECLARE SUB CalcNormals (Model() AS ANY, ModelConnect() AS ANY, V() AS ANY)
DECLARE SUB FlatTri (x1%, y1%, x2%, y2%, x3%, y3%, Col%)
DECLARE SUB DrawCube (Model() AS ANY, Vector() AS ANY)
DECLARE SUB LoadCube (Model() AS ANY, Vector() AS ANY)
DECLARE SUB RotateAndProject (Model() AS ANY, AngleX%, AngleY%, AngleZ%)
DECLARE SUB RotNormals (V() AS ANY, V2() AS ANY, AngleX%, AngleY%, AngleZ%)
DECLARE SUB BLine (x1, y1, x2, y2, colour)
'$DYNAMIC
DIM SHARED buffer(32001)
'$STATIC
DIM SHARED lutsegy(199) AS LONG

CONST pi180 = 3.141592654# / 180

FOR N& = 0 TO 199
 lutsegy(N&) = N& * 320 + 4
NEXT

TYPE Point3d
x AS SINGLE 'Normal 3d coords
y AS SINGLE
Z AS SINGLE
Xr AS SINGLE
Yr AS SINGLE
Zr AS SINGLE
ScrX AS INTEGER 'Translated and projected
ScrY AS INTEGER '2d Coords
END TYPE

TYPE PolyType
p1 AS INTEGER
p2 AS INTEGER
p3 AS INTEGER
Clr1 AS INTEGER
Clr2 AS INTEGER
Clr3 AS INTEGER
END TYPE

TYPE VectorType
x AS SINGLE
y AS SINGLE
Z AS SINGLE
END TYPE


CONST LENS = 256 'Z
CONST XCENTER = 160 '??
CONST YCENTER = 100 '??


CONST PI = 3.14151693#

DIM SHARED LCOS(359) AS SINGLE
DIM SHARED LSIN(359) AS SINGLE

REDIM SHARED CubeModel(1) AS Point3d
REDIM SHARED CubePoly(1) AS PolyType
REDIM SHARED CubeNormal(1) AS VectorType
REDIM SHARED CubeNormal2(1) AS VectorType
DIM SHARED ThetaX, ThetaY, ThetaZ
DIM SHARED Zcenter, CamX, CamY
DIM SHARED LightNormal AS VectorType

'PreCalc sin and cos lookuptable

FOR I = 0 TO 359
A! = I * PI / 180
LCOS(I) = COS(A!)
LSIN(I) = SIN(A!)
NEXT I

LightNormal.x = 0 'Light is from the camera
LightNormal.y = 0
LightNormal.Z = -1

LoadCube CubeModel(), CubePoly()
RotateAndProject CubeModel(), ThetaX, ThetaY, ThetaZ
CalcNormals CubeModel(), CubePoly(), CubeNormal()

CLS
SCREEN 13
RANDOMIZE TIMER

'Grey Scale the Palette
FOR I = 0 TO 255
OUT &H3C8, I
OUT &H3C9, (I \ 4)
OUT &H3C9, (I \ 4) * .9
OUT &H3C9, (I \ 4) * .1
NEXT I


ThetaX = 0
ThetaY = 0
ThetaZ = 0
Zcenter = LENS


buffer(0) = 2560: buffer(1) = 200
DEF SEG = VARSEG(buffer(0))

t# = TIMER
DO


ThetaX = (ThetaX + 1) MOD 360
ThetaY = (ThetaY + 1) MOD 360
ThetaZ = (ThetaZ + 1) MOD 360


RotateAndProject CubeModel(), ThetaX, ThetaY, ThetaZ
RotNormals CubeNormal(), CubeNormal2(), ThetaX, ThetaY, ThetaZ
REDIM buffer(32001)
buffer(0) = 2560: buffer(1) = 200
DrawCube CubeModel(), CubePoly()
PUT (0, 0), buffer, PSET
frames& = frames& + 1
LOOP UNTIL INKEY$ <> ""
COLOR 63
PRINT frames& / (TIMER - t#)
DO: LOOP UNTIL LEN(INKEY$)

END

'numPoints
NumPoints:
DATA 8


'vertices of Cube
VertexData:
DATA -50,50,50
DATA 50,50,50
DATA 50,50,-50
DATA -50,50,-50
DATA -50,-50,50
DATA 50,-50,50
DATA 50,-50,-50
DATA -50,-50,-50

NumPoly:
DATA 12
ConnectData:
DATA 5,4,0, 5,0,1
DATA 6,2,3, 3,7,6
DATA 6,5,1, 6,1,2
DATA 7,0,4, 7,3,0
DATA 6,7,4, 6,4,5
DATA 0,3,2, 1,0,2

SUB BLine (x1, y1, x2, y2, colour)
DIM addr AS LONG
  x = x1: xinc = 1
  y = y1: yinc = 1: ainc = 320

  dy = y2 - y1 'Determine delta y
  dx = x2 - x1 'Determine delta x

  IF dy < 0 THEN dy = -dy: yinc = -1: ainc = -320 'Correct negative delta y
  IF dx < 0 THEN dx = -dx: xinc = -1                     'Correct negative delta x

  addr = lutsegy(y1) + x1 'Determine starting location

  IF (x >= 0) AND (x <= 319) AND (y >= 0) AND (y <= 199) THEN POKE addr, colour

  IF dx > dy THEN
    d = dy + dy - dx       'Determine initial "check" value
    d0 = dy + dy           'Determine constant y increment
    d1 = 2 * (dy - dx)     'Determine variable y increment
    DO WHILE x <> x2       'Draw line if line not a point
      x = x + xinc         'Increment x value
      addr = addr + xinc   'Increment address value accordingly
      IF d < 0 THEN
        d = d + d0
      ELSE
        d = d + d1
        y = y + yinc
        addr = addr + ainc
      END IF
      IF (x >= 0) AND (x <= 319) AND (y >= 0) AND (y <= 199) THEN POKE addr, colour
    LOOP
  ELSE
    d = dx + dx - dy     'Determine initial 'check' value
    d0 = dx + dx         'Determine constant x increment
    d1 = 2 * (dx - dy)   'Determine variable x increment
    DO WHILE y <> y2     'Draw line if line not a point
      y = y + yinc       'Increment y value
      addr = addr + ainc 'Increment address value accordingly
      IF d < 0 THEN
        d = d + d0
      ELSE
        d = d + d1
        x = x + xinc
        addr = addr + xinc
      END IF
      IF (x >= 0) AND (x <= 319) AND (y >= 0) AND (y <= 199) THEN POKE addr, colour
    LOOP
  END IF

END SUB

SUB CalcNormals (Model() AS Point3d, ModelConnect() AS PolyType, V() AS VectorType)

FOR I = 1 TO UBOUND(V)

p1 = ModelConnect(I).p1
p2 = ModelConnect(I).p2
p3 = ModelConnect(I).p3
x1 = Model(p1).x
x2 = Model(p2).x
x3 = Model(p3).x
y1 = Model(p1).y
y2 = Model(p2).y
y3 = Model(p3).y
Z1 = Model(p1).Z
Z2 = Model(p2).Z
Z3 = Model(p3).Z

ax! = x2 - x1
bx! = x3 - x2
ay! = y2 - y1
by! = y3 - y2
az! = Z2 - Z1
bz! = Z3 - Z2

'Cross product
xnormal! = ay! * bz! - az! * by!
ynormal! = az! * bx! - ax! * bz!
Znormal! = ax! * by! - ay! * bx!

'Normalize
Mag! = SQR(xnormal! ^ 2 + ynormal! ^ 2 + Znormal! ^ 2)
IF Mag! <> 0 THEN
xnormal! = xnormal! / Mag!
ynormal! = ynormal! / Mag!
Znormal! = Znormal! / Mag!
END IF

IF Znormal! < -1 THEN
Znormal! = -1
ELSEIF Znormal! > 1 THEN
Znormal! = 1
END IF

IF xnormal! < -1 THEN
xnormal! = -1
ELSEIF xnormal! > 1 THEN
xnormal! = 1
END IF

IF ynormal! < -1 THEN
yxnormal! = -1
ELSEIF ynormal! > 1 THEN
ynormal! = 1
END IF

V(I).x = xnormal!
V(I).y = ynormal!
V(I).Z = Znormal!

NEXT I
END SUB

SUB DrawCube (Model() AS Point3d, Poly() AS PolyType) STATIC

FOR I = 1 TO UBOUND(Poly)
x1 = Model(Poly(I).p1).ScrX 'Get triangles from "projected"
x2 = Model(Poly(I).p2).ScrX 'X and Y coords since Znormal
x3 = Model(Poly(I).p3).ScrX 'Does not require a Z coord
y1 = Model(Poly(I).p1).ScrY 'V1= Point1 connected to V2 then
y2 = Model(Poly(I).p2).ScrY 'V2 to V3 and so on...
y3 = Model(Poly(I).p3).ScrY


'Use the Znormal,the Ray perpendicular(Orthogonal) to the XY plane
'Defined by the Triangle (X1,Y1,X2,Y2,X3,Y3)
'if Less(<) 0 then its facing in the opposite direction so
'don't plot. If =>0 then its facing towards you so Plot.
Znormal = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)
IF Znormal < 0 THEN
nx! = CubeNormal2(I).x
ny! = CubeNormal2(I).y
nz! = CubeNormal2(I).Z
lx! = LightNormal.x
ly! = LightNormal.y
lz! = LightNormal.Z
Dot! = (nx! * lx!) + (ny! * ly!) + (nz! * lz!)
IF Dot! < 0 OR Dot! > 1 THEN
Dot! = 0
END IF
Clr = Dot! * 255
C1 = Poly(I).Clr1
C2 = Poly(I).Clr2
C3 = Poly(I).Clr3
FlatTri x1, y1, x2, y2, x3, y3, Clr
END IF

NEXT I

END SUB

SUB FlatTri (x1%, y1%, x2%, y2%, x3%, y3%, C%)
DIM addr AS LONG
'DRAWS TRIANGLE WITH FLAT SHADING
'
'reorder coords so y1<y2<y3
'
BLine x1%, y1%, x2%, y2%, C% ' triangle outline
BLine x2%, y2%, x3%, y3%, C% '
BLine x3%, y3%, x1%, y1%, C% '

IF y1% > y2% THEN SWAP y1%, y2%: SWAP x1%, x2%
IF y1% > y3% THEN SWAP y1%, y3%: SWAP x1%, x3%
IF y2% > y3% THEN SWAP y2%, y3%: SWAP x2%, x3%

' need to check for divide by zero..

ydiffa% = y2% - y1%
IF ydiffa% THEN d1! = ((x2% - x1%)) / ydiffa%

ydiffb% = y3% - y2%
IF ydiffb% THEN d2! = ((x3% - x2%)) / ydiffb%

ydiffc% = 1 + y3% - y1%
IF ydiffc% THEN d3! = ((x3% - x1%)) / ydiffc%

lx! = x1%
rx! = x1%

FOR y% = y1% TO y2% - 1
BLine INT(lx!), y%, INT(rx!), y%, C%
lx! = lx! + d1!
rx! = rx! + d3!
NEXT

lx! = x2%

FOR y% = y2% TO y3%
BLine INT(lx!), y%, INT(rx!), y%, C%
lx! = lx! + d2!
rx! = rx! + d3!
NEXT y%
END SUB

SUB LoadCube (Model() AS Point3d, Tri() AS PolyType) STATIC

RESTORE NumPoints
READ MaxVertex
REDIM Model(1 TO MaxVertex) AS Point3d


RESTORE VertexData
FOR V = 1 TO UBOUND(Model)
READ Xt, Yt, Zt
Model(V).x = Xt
Model(V).y = Yt
Model(V).Z = Zt
NEXT V

RESTORE NumPoly
READ MaxPoly
REDIM Tri(MaxPoly) AS PolyType

RESTORE ConnectData

FOR V = 1 TO UBOUND(Tri)
READ T1, T2, T3
Tri(V).p1 = T1 + 1
Tri(V).p2 = T2 + 1
Tri(V).p3 = T3 + 1
Tri(V).Clr1 = 50 + INT(RND * 100)
NEXT V


REDIM CubeNormal(1 TO UBOUND(Tri)) AS VectorType
REDIM CubeNormal2(1 TO UBOUND(Tri)) AS VectorType




END SUB

SUB RotateAndProject (Model() AS Point3d, AngleX, AngleY, AngleZ) STATIC

'Precalculate the SIN and COS of each angle
CX! = LCOS(AngleX)
SX! = LSIN(AngleX)
CY! = LCOS(AngleY)
SY! = LSIN(AngleY)
CZ! = LCOS(AngleZ)
SZ! = LSIN(AngleZ)

'Transformation matrix formula
'This is actually 16(or 12) equations but I pared it down to 9
'since TX4=0,TY4=0,TZ4=0,13 to 16th =0,0,0,1 (yes Doom!!!)

TX1! = CY! * CZ!
TX2! = CY! * SZ!
TX3! = -SY!
TY1! = CX! * -SZ! + SX! * SY! * CZ!
TY2! = CX! * CZ! + SX! * SY! * SZ!
TY3! = SX! * CY!
TZ1! = -SX! * -SZ! + CX! * SY! * CZ!
TZ2! = -SX! * CZ! + CZ! * SY! * SZ!
TZ3! = CX! * CY!


FOR I = 1 TO UBOUND(Model)

x! = Model(I).x 'Load Original model
y! = Model(I).y
Z! = Model(I).Z

RotX! = (x! * TX1! + y! * TY1! + Z! * TZ1!)
RotY! = (x! * TX2! + y! * TY2! + Z! * TZ2!)
RotZ! = (x! * TX3! + y! * TY3! + Z! * TZ3!)

Model(I).Xr = RotX!
Model(I).Yr = RotY!
Model(I).Zr = RotZ!


'Project
Distance% = (LENS - RotZ!)
IF Distance% THEN
Model(I).ScrX = (Zcenter * RotX! / Distance%) + XCENTER + CamX
Model(I).ScrY = -(Zcenter * RotY! / Distance%) + YCENTER + CamY
ELSE
END IF
NEXT I

END SUB

SUB RotNormals (V() AS VectorType, V2() AS VectorType, AngleX, AngleY, AngleZ)

'Precalculate the SIN and COS of each angle
CX! = LCOS(AngleX)
SX! = LSIN(AngleX)
CY! = LCOS(AngleY)
SY! = LSIN(AngleY)
CZ! = LCOS(AngleZ)
SZ! = LSIN(AngleZ)

'Transformation matrix formula
'This is actually 16(or 12) equations but I pared it down to 9
'since TX4=0,TY4=0,TZ4=0,13 to 16th =0,0,0,1 (yes Doom!!!)

TX1! = CY! * CZ!
TX2! = CY! * SZ!
TX3! = -SY!
TY1! = CX! * -SZ! + SX! * SY! * CZ!
TY2! = CX! * CZ! + SX! * SY! * SZ!
TY3! = SX! * CY!
TZ1! = -SX! * -SZ! + CX! * SY! * CZ!
TZ2! = -SX! * CZ! + CZ! * SY! * SZ!
TZ3! = CX! * CY!


FOR I = 1 TO UBOUND(V)

x! = V(I).x 'Load Original vector
y! = V(I).y
Z! = V(I).Z

RotX! = (x! * TX1! + y! * TY1! + Z! * TZ1!)
RotY! = (x! * TX2! + y! * TY2! + Z! * TZ2!)
RotZ! = (x! * TX3! + y! * TY3! + Z! * TZ3!)

IF RotZ! < -1 THEN
RotZ! = -1
ELSEIF RotZ! > 1 THEN
RotZ! = 1
END IF

IF RotX! < -1 THEN
RotX! = -1
ELSEIF RotX! > 1 THEN
RotX! = 1
END IF

IF RotY! < -1 THEN
RotY! = -1
ELSEIF RotY! > 1 THEN
RotY! = 1
END IF


V2(I).x = RotX!
V2(I).y = RotY!
V2(I).Z = RotZ!
NEXT I

END SUB


The thing is, I'd like to have an inline scanline drawer. I've tried to use Antoni's, but that gave me a whole bunch of black pixels around the triangles' outlines, which this code takes out a bit (still some on the top if you look) Please help me out with this.
Logged

am an asshole. Get used to it.
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #34 on: June 02, 2003, 12:06:18 AM »

No prob. I'll look it up but we still have no electricity in the area I live. ;*(
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #35 on: June 05, 2003, 01:52:42 AM »

You know I posted a scanline based trifill routine here but it somehow disappeared. Huh?

BTW, if you didn't get it just shout and I'll repost again. ;*)
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
Ninkazu
Been there, done that
*****
Posts: 1169



WWW
« Reply #36 on: June 05, 2003, 09:07:17 AM »

I don't think I got that one, no.
Logged

am an asshole. Get used to it.
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #37 on: June 05, 2003, 11:42:31 PM »

Here:

Code:

DEFINT A-Z
DECLARE SUB Hline (xx1%, xx2%, y%, Colour%)
DECLARE SUB CalcNormals (Model() AS ANY, ModelConnect() AS ANY, V() AS ANY)
DECLARE SUB FlatTri (x1%, y1%, x2%, y2%, x3%, y3%, Col%)
DECLARE SUB DrawCube (Model() AS ANY, Vector() AS ANY)
DECLARE SUB LoadCube (Model() AS ANY, Vector() AS ANY)
DECLARE SUB RotateAndProject (Model() AS ANY, AngleX%, AngleY%, AngleZ%)
DECLARE SUB RotNormals (V() AS ANY, V2() AS ANY, AngleX%, AngleY%, AngleZ%)
DECLARE SUB BLine (x1, y1, x2, y2, Colour)
'$DYNAMIC
DIM SHARED buffer(32001)
'$STATIC
DIM SHARED lutsegy(199) AS LONG

CONST pi180 = 3.141592654# / 180

FOR N& = 0 TO 199
lutsegy(N&) = N& * 320 + 4
NEXT

TYPE Point3d
x AS SINGLE 'Normal 3d coords
y AS SINGLE
Z AS SINGLE
Xr AS SINGLE
Yr AS SINGLE
Zr AS SINGLE
ScrX AS INTEGER 'Translated and projected
ScrY AS INTEGER '2d Coords
END TYPE

TYPE PolyType
p1 AS INTEGER
p2 AS INTEGER
p3 AS INTEGER
Clr1 AS INTEGER
Clr2 AS INTEGER
Clr3 AS INTEGER
END TYPE

TYPE VectorType
x AS SINGLE
y AS SINGLE
Z AS SINGLE
END TYPE


CONST LENS = 256 'Z
CONST XCENTER = 160 '??
CONST YCENTER = 100 '??


CONST PI = 3.14151693#

DIM SHARED LCOS(359) AS SINGLE
DIM SHARED LSIN(359) AS SINGLE

REDIM SHARED CubeModel(1) AS Point3d
REDIM SHARED CubePoly(1) AS PolyType
REDIM SHARED CubeNormal(1) AS VectorType
REDIM SHARED CubeNormal2(1) AS VectorType
DIM SHARED ThetaX, ThetaY, ThetaZ
DIM SHARED Zcenter, CamX, CamY
DIM SHARED LightNormal AS VectorType

'PreCalc sin and cos lookuptable

FOR I = 0 TO 359
A! = I * PI / 180
LCOS(I) = COS(A!)
LSIN(I) = SIN(A!)
NEXT I

LightNormal.x = 0 'Light is from the camera
LightNormal.y = 0
LightNormal.Z = -1

LoadCube CubeModel(), CubePoly()
RotateAndProject CubeModel(), ThetaX, ThetaY, ThetaZ
CalcNormals CubeModel(), CubePoly(), CubeNormal()

CLS
SCREEN 13
RANDOMIZE TIMER

'Grey Scale the Palette
FOR I = 0 TO 255
OUT &H3C8, I
OUT &H3C9, (I \ 4)
OUT &H3C9, (I \ 4) * .9
OUT &H3C9, (I \ 4) * .1
NEXT I


ThetaX = 0
ThetaY = 0
ThetaZ = 0
Zcenter = LENS


buffer(0) = 2560: buffer(1) = 200
DEF SEG = VARSEG(buffer(0))

t# = TIMER
DO


ThetaX = (ThetaX + 1) MOD 360
ThetaY = (ThetaY + 1) MOD 360
ThetaZ = (ThetaZ + 1) MOD 360


RotateAndProject CubeModel(), ThetaX, ThetaY, ThetaZ
RotNormals CubeNormal(), CubeNormal2(), ThetaX, ThetaY, ThetaZ
REDIM buffer(32001)
buffer(0) = 2560: buffer(1) = 200
DrawCube CubeModel(), CubePoly()
PUT (0, 0), buffer, PSET
frames& = frames& + 1
LOOP UNTIL INKEY$ <> ""
COLOR 63
PRINT frames& / (TIMER - t#)
DO: LOOP UNTIL LEN(INKEY$)

END

'numPoints
NumPoints:
DATA 8


'vertices of Cube
VertexData:
DATA -50,50,50
DATA 50,50,50
DATA 50,50,-50
DATA -50,50,-50
DATA -50,-50,50
DATA 50,-50,50
DATA 50,-50,-50
DATA -50,-50,-50

NumPoly:
DATA 12
ConnectData:
DATA 5,4,0, 5,0,1
DATA 6,2,3, 3,7,6
DATA 6,5,1, 6,1,2
DATA 7,0,4, 7,3,0
DATA 6,7,4, 6,4,5
DATA 0,3,2, 1,0,2

SUB BLine (x1, y1, x2, y2, Colour)
DIM addr AS LONG
  x = x1: xinc = 1
  y = y1: yinc = 1: ainc = 320

  dy = y2 - y1 'Determine delta y
  dx = x2 - x1 'Determine delta x

  IF dy < 0 THEN dy = -dy: yinc = -1: ainc = -320 'Correct negative delta y
  IF dx < 0 THEN dx = -dx: xinc = -1                     'Correct negative delta x

  addr = lutsegy(y1) + x1 'Determine starting location

  IF (x >= 0) AND (x <= 319) AND (y >= 0) AND (y <= 199) THEN POKE addr, Colour

  IF dx > dy THEN
    d = dy + dy - dx       'Determine initial "check" value
    d0 = dy + dy           'Determine constant y increment
    d1 = 2 * (dy - dx)     'Determine variable y increment
    DO WHILE x <> x2       'Draw line if line not a point
      x = x + xinc         'Increment x value
      addr = addr + xinc   'Increment address value accordingly
      IF d < 0 THEN
        d = d + d0
      ELSE
        d = d + d1
        y = y + yinc
        addr = addr + ainc
      END IF
      IF (x >= 0) AND (x <= 319) AND (y >= 0) AND (y <= 199) THEN POKE addr, Colour
    LOOP
  ELSE
    d = dx + dx - dy     'Determine initial 'check' value
    d0 = dx + dx         'Determine constant x increment
    d1 = 2 * (dx - dy)   'Determine variable x increment
    DO WHILE y <> y2     'Draw line if line not a point
      y = y + yinc       'Increment y value
      addr = addr + ainc 'Increment address value accordingly
      IF d < 0 THEN
        d = d + d0
      ELSE
        d = d + d1
        x = x + xinc
        addr = addr + xinc
      END IF
      IF (x >= 0) AND (x <= 319) AND (y >= 0) AND (y <= 199) THEN POKE addr, Colour
    LOOP
  END IF

END SUB

SUB CalcNormals (Model() AS Point3d, ModelConnect() AS PolyType, V() AS VectorType)

FOR I = 1 TO UBOUND(V)

p1 = ModelConnect(I).p1
p2 = ModelConnect(I).p2
p3 = ModelConnect(I).p3
x1 = Model(p1).x
x2 = Model(p2).x
x3 = Model(p3).x
y1 = Model(p1).y
y2 = Model(p2).y
y3 = Model(p3).y
Z1 = Model(p1).Z
Z2 = Model(p2).Z
Z3 = Model(p3).Z

ax! = x2 - x1
bx! = x3 - x2
ay! = y2 - y1
by! = y3 - y2
az! = Z2 - Z1
bz! = Z3 - Z2

'Cross product
xnormal! = ay! * bz! - az! * by!
ynormal! = az! * bx! - ax! * bz!
Znormal! = ax! * by! - ay! * bx!

'Normalize
Mag! = SQR(xnormal! ^ 2 + ynormal! ^ 2 + Znormal! ^ 2)
IF Mag! <> 0 THEN
xnormal! = xnormal! / Mag!
ynormal! = ynormal! / Mag!
Znormal! = Znormal! / Mag!
END IF

IF Znormal! < -1 THEN
Znormal! = -1
ELSEIF Znormal! > 1 THEN
Znormal! = 1
END IF

IF xnormal! < -1 THEN
xnormal! = -1
ELSEIF xnormal! > 1 THEN
xnormal! = 1
END IF

IF ynormal! < -1 THEN
yxnormal! = -1
ELSEIF ynormal! > 1 THEN
ynormal! = 1
END IF

V(I).x = xnormal!
V(I).y = ynormal!
V(I).Z = Znormal!

NEXT I
END SUB

SUB DrawCube (Model() AS Point3d, Poly() AS PolyType) STATIC

FOR I = 1 TO UBOUND(Poly)
x1 = Model(Poly(I).p1).ScrX 'Get triangles from "projected"
x2 = Model(Poly(I).p2).ScrX 'X and Y coords since Znormal
x3 = Model(Poly(I).p3).ScrX 'Does not require a Z coord
y1 = Model(Poly(I).p1).ScrY 'V1= Point1 connected to V2 then
y2 = Model(Poly(I).p2).ScrY 'V2 to V3 and so on...
y3 = Model(Poly(I).p3).ScrY


'Use the Znormal,the Ray perpendicular(Orthogonal) to the XY plane
'Defined by the Triangle (X1,Y1,X2,Y2,X3,Y3)
'if Less(<) 0 then its facing in the opposite direction so
'don't plot. If =>0 then its facing towards you so Plot.
Znormal = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)
IF Znormal < 0 THEN
nx! = CubeNormal2(I).x
ny! = CubeNormal2(I).y
nz! = CubeNormal2(I).Z
lx! = LightNormal.x
ly! = LightNormal.y
lz! = LightNormal.Z
Dot! = (nx! * lx!) + (ny! * ly!) + (nz! * lz!)
IF Dot! < 0 OR Dot! > 1 THEN
Dot! = 0
END IF
Clr = Dot! * 255
C1 = Poly(I).Clr1
C2 = Poly(I).Clr2
C3 = Poly(I).Clr3
FlatTri x1, y1, x2, y2, x3, y3, Clr
END IF

NEXT I

END SUB

SUB FlatTri (x1%, y1%, x2%, y2%, x3%, y3%, c%)
DIM addr AS LONG
'DRAWS TRIANGLE WITH FLAT SHADING
'
'reorder coords so y1<y2<y3
'
BLine x1%, y1%, x2%, y2%, c% ' triangle outline
BLine x2%, y2%, x3%, y3%, c% '
BLine x3%, y3%, x1%, y1%, c% '

IF y1% > y2% THEN SWAP y1%, y2%: SWAP x1%, x2%
IF y1% > y3% THEN SWAP y1%, y3%: SWAP x1%, x3%
IF y2% > y3% THEN SWAP y2%, y3%: SWAP x2%, x3%

' need to check for divide by zero..

ydiffa% = y2% - y1%
IF ydiffa% THEN d1! = ((x2% - x1%)) / ydiffa%

ydiffb% = y3% - y2%
IF ydiffb% THEN d2! = ((x3% - x2%)) / ydiffb%

ydiffc% = 1 + y3% - y1%
IF ydiffc% THEN d3! = ((x3% - x1%)) / ydiffc%

lx! = x1%
rx! = x1%

FOR y% = y1% TO y2% - 1
'''''BLine INT(lx!), y%, INT(rx!), y%, c%
Hline INT(lx!), INT(rx!), y%, c%
lx! = lx! + d1!
rx! = rx! + d3!
NEXT

lx! = x2%

FOR y% = y2% TO y3%
'''''''''BLine INT(lx!), y%, INT(rx!), y%, c%
Hline INT(lx!), INT(rx!), y%, c%
lx! = lx! + d2!
rx! = rx! + d3!
NEXT y%
END SUB

SUB Hline (xx1, xx2, y, Colour)

'This is faster as this only uses scanline Hpoke
x1 = xx1
x2 = xx2
y1 = yy1
y2 = yy2

IF x1 > x2 THEN SWAP x1, x2
IF y1 > y2 THEN SWAP y1, y2

IF x1 < 0 THEN
    xxd = x1 + (x2 - x1) + 1
    IF xxd < 0 THEN EXIT SUB
    x1 = 0
ELSEIF x1 > 319 THEN
    EXIT SUB
END IF

IF x2 > 319 THEN
    xxd = x2 - ((x2 - x1) + 1)
    IF xxd > 319 THEN EXIT SUB
    x2 = 319
ELSEIF x2 < 0 THEN
    EXIT SUB
END IF


IF y < 0 OR y > 199 THEN EXIT SUB

addr& = lutsegy(y) + x1

sx = 0
FOR xx = x1 TO x2
    POKE addr& + sx, Colour
    sx = sx + 1
NEXT xx


END SUB

SUB LoadCube (Model() AS Point3d, Tri() AS PolyType) STATIC

RESTORE NumPoints
READ MaxVertex
REDIM Model(1 TO MaxVertex) AS Point3d


RESTORE VertexData
FOR V = 1 TO UBOUND(Model)
READ Xt, Yt, Zt
Model(V).x = Xt
Model(V).y = Yt
Model(V).Z = Zt
NEXT V

RESTORE NumPoly
READ MaxPoly
REDIM Tri(MaxPoly) AS PolyType

RESTORE ConnectData

FOR V = 1 TO UBOUND(Tri)
READ T1, T2, T3
Tri(V).p1 = T1 + 1
Tri(V).p2 = T2 + 1
Tri(V).p3 = T3 + 1
Tri(V).Clr1 = 50 + INT(RND * 100)
NEXT V


REDIM CubeNormal(1 TO UBOUND(Tri)) AS VectorType
REDIM CubeNormal2(1 TO UBOUND(Tri)) AS VectorType




END SUB

SUB RotateAndProject (Model() AS Point3d, AngleX, AngleY, AngleZ) STATIC

'Precalculate the SIN and COS of each angle
CX! = LCOS(AngleX)
sx! = LSIN(AngleX)
CY! = LCOS(AngleY)
SY! = LSIN(AngleY)
CZ! = LCOS(AngleZ)
SZ! = LSIN(AngleZ)

'Transformation matrix formula
'This is actually 16(or 12) equations but I pared it down to 9
'since TX4=0,TY4=0,TZ4=0,13 to 16th =0,0,0,1 (yes Doom!!!)

TX1! = CY! * CZ!
TX2! = CY! * SZ!
TX3! = -SY!
TY1! = CX! * -SZ! + sx! * SY! * CZ!
TY2! = CX! * CZ! + sx! * SY! * SZ!
TY3! = sx! * CY!
TZ1! = -sx! * -SZ! + CX! * SY! * CZ!
TZ2! = -sx! * CZ! + CZ! * SY! * SZ!
TZ3! = CX! * CY!


FOR I = 1 TO UBOUND(Model)

x! = Model(I).x 'Load Original model
y! = Model(I).y
Z! = Model(I).Z

RotX! = (x! * TX1! + y! * TY1! + Z! * TZ1!)
RotY! = (x! * TX2! + y! * TY2! + Z! * TZ2!)
RotZ! = (x! * TX3! + y! * TY3! + Z! * TZ3!)

Model(I).Xr = RotX!
Model(I).Yr = RotY!
Model(I).Zr = RotZ!


'Project
Distance% = (LENS - RotZ!)
IF Distance% THEN
Model(I).ScrX = (Zcenter * RotX! / Distance%) + XCENTER + CamX
Model(I).ScrY = -(Zcenter * RotY! / Distance%) + YCENTER + CamY
ELSE
END IF
NEXT I

END SUB

SUB RotNormals (V() AS VectorType, V2() AS VectorType, AngleX, AngleY, AngleZ)

'Precalculate the SIN and COS of each angle
CX! = LCOS(AngleX)
sx! = LSIN(AngleX)
CY! = LCOS(AngleY)
SY! = LSIN(AngleY)
CZ! = LCOS(AngleZ)
SZ! = LSIN(AngleZ)

'Transformation matrix formula
'This is actually 16(or 12) equations but I pared it down to 9
'since TX4=0,TY4=0,TZ4=0,13 to 16th =0,0,0,1 (yes Doom!!!)

TX1! = CY! * CZ!
TX2! = CY! * SZ!
TX3! = -SY!
TY1! = CX! * -SZ! + sx! * SY! * CZ!
TY2! = CX! * CZ! + sx! * SY! * SZ!
TY3! = sx! * CY!
TZ1! = -sx! * -SZ! + CX! * SY! * CZ!
TZ2! = -sx! * CZ! + CZ! * SY! * SZ!
TZ3! = CX! * CY!


FOR I = 1 TO UBOUND(V)

x! = V(I).x 'Load Original vector
y! = V(I).y
Z! = V(I).Z

RotX! = (x! * TX1! + y! * TY1! + Z! * TZ1!)
RotY! = (x! * TX2! + y! * TY2! + Z! * TZ2!)
RotZ! = (x! * TX3! + y! * TY3! + Z! * TZ3!)

IF RotZ! < -1 THEN
RotZ! = -1
ELSEIF RotZ! > 1 THEN
RotZ! = 1
END IF

IF RotX! < -1 THEN
RotX! = -1
ELSEIF RotX! > 1 THEN
RotX! = 1
END IF

IF RotY! < -1 THEN
RotY! = -1
ELSEIF RotY! > 1 THEN
RotY! = 1
END IF


V2(I).x = RotX!
V2(I).y = RotY!
V2(I).Z = RotZ!
NEXT I

END SUB

Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
Ninkazu
Been there, done that
*****
Posts: 1169



WWW
« Reply #38 on: June 06, 2003, 01:26:17 AM »

Thanks man, I still don't know what's up with that top part. If you look closely when the upper right front corner is almost to the full right and top.... wow... it's really hard to describe that.
Logged

am an asshole. Get used to it.
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #39 on: June 07, 2003, 03:05:47 AM »

Yeah, you prolly need to add 1 to the length of each scanline. ;*)
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
Ninkazu
Been there, done that
*****
Posts: 1169



WWW
« Reply #40 on: June 07, 2003, 02:14:51 PM »

I've tried that, but it just looked totally messed up.
Logged

am an asshole. Get used to it.
Blitz
I hold this place together
*****
Posts: 853



WWW
« Reply #41 on: June 07, 2003, 09:14:08 PM »

ugh...
sometimes you guys make me wonder. First of all what's the point of optimizing such a routine in qb. No matter how much you try it will be greatly slowed down by the sub call of qb. And using 32 bit variables in qb? Thats really is nuts. And 3 divisions where in somecases only 2 are needed. I could go on a mile long list. But you dudes don't care anyway.
Logged

oship me and i will give you lots of guurrls and beeea
Ninkazu
Been there, done that
*****
Posts: 1169



WWW
« Reply #42 on: June 07, 2003, 09:22:57 PM »

Quote from: "Blitz"
And 3 divisions where in somecases only 2 are needed. I could go on a mile long list. But you dudes don't care anyway.


Please go on, I'm really interested in this. I have a book on programming 3D games in DOS, but it doesn't really go over a lot of the optimizing aspects, and the source that came with the book uses his custom library of whose source ISN'T enclosed. That really pisses me off. (**off topic: I just got a phone call of whom the caller just hung up... I hope those christians aren't after me**) I think you guys know the book, because it's written by the moth himself... LaMothe (ya, gay joke, but I'm having dizzy spells) It's the Black Art of Programming 3D Games. I just ordered his latest book. It's over 1700 pages and it's for windows and how to program your own software rastering routines. I'm in total anticipation.
Logged

am an asshole. Get used to it.
Blitz
I hold this place together
*****
Posts: 853



WWW
« Reply #43 on: June 08, 2003, 07:14:11 AM »

Those books don't show you how to optimize, they only show you how to do stuff. Optimizing is an art, which takes a while to learn. It's not rules you follow. It's a way of thinking. And don't buy those books dude. Most of the books on 3d and game dev out there are by people without the right compotence. Of that 1700 pages i garantue you 1500 will be just source listings.

I mean if that dude Liquidex can get a book published, go figure.
Logged

oship me and i will give you lots of guurrls and beeea
Blitz
I hold this place together
*****
Posts: 853



WWW
« Reply #44 on: June 08, 2003, 07:16:21 AM »

My tip is to write the polygon filler in C. And using a totaly different approach. Read FATMAP (the first one, not the second one) to see how a good tri filler should be.
Logged

oship me and i will give you lots of guurrls and beeea
Pages: 1 2 [3] 4
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!