Qbasicnews.com

Full Version: I need some help regarding 3D
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a bunch of 3D example programs, and i have even written my own, but my problem is that i cant get it to exclude the stuff that is beeing covered. IE. in a box you cant see the inside or the back, only the front, side and top/bottom

I've tried just about everything, and I keep getting my transaparent box, or worse =(.

Anything that could remotely help me in the right direction will be greatly appreciated.

Please note that i'm not talking textures, I only want wireframe models =)

Thanx
[offtopic]
Hey Z!re you are back! Welcome! Its been a long time after I have seen you on *any* forum =P
[/offtopic]
uhh... theres a formula which can decide wether a point should be hidden or not, but i dont know it off the top of my head. Maybe one of the other guys could help ya with this.
BackFace culling:

http://rel.betterwebber.com/junk.php?id=19

DrawTorusT

Check out the line with this

"Znormal"

If Z normal>0 then
end if
Big Grin
Well, you can visit this website and it has a finished 3D wire-framed box program with instructions.

It is at http://qb4all.com then goto Tutorials, then under Advanced Tutorials is the 3D

This is a copy of their code (some lines have been cut):
Code:
DEFINT A-Z

TYPE pnt 'type for each 3D point
  x AS INTEGER 'x coord (horizontal)
  y AS INTEGER 'y coord (vertical)
  Z AS INTEGER 'z coord (into the screen)
  p AS INTEGER 'dist from center of object
END TYPE

numLines = 12 - 1

DIM lO(numLines, 1) AS pnt        'Original line coords
DIM lR(numLines, 1) AS pnt        'Rotated coords
DIM scrX(numLines, 1)             'screen x coord
DIM scrY(numLines, 1)             'screen y coord
DIM oldX(numLines, 1)             'old x coord for erasing
DIM oldY(numLines, 1)             'old y coord for erasing
DIM s!(359)                       'trig tables

DIM c!(359)

CONST PI = 3.141592

FOR i = 0 TO 359                  'create sine and cosine
    S!(i) = SIN(i * (PI / 180))     'look up tables to speed up
    C!(i) = COS(i * (PI / 180))     'the math
NEXT

FOR i = 0 TO numLines             'Read two points instead of one.
  READ lO(i, 0).x, lO(i, 0).y, lO(i, 0).Z, lO(i, 0).p
  READ lO(i, 1).x, lO(i, 1).y, lO(i, 1).Z, lO(i, 1).p
NEXT

SCREEN 13

CLS

xCenter = 160: yCenter = 100: zCenter = 256
theta = 0: phi = 0
thetaRot = 2: phiRot = 2
justStarted = 1

DO
FOR i = 0 TO numLines
' Save the old values of x and y so we can erase the balls later.
oldX(i, 0) = scrX(i, 0): oldY(i, 0) = scrY(i, 0)
oldX(i, 1) = scrX(i, 1): oldY(i, 1) = scrY(i, 1)

' Rotate both points on each axis.
lR(i, 0).x = -lO(i, 0).x * s!(theta) + lO(i, 0).y * c!(theta)

lR(i, 0).y = -lO(i, 0).x * c!(theta) * s!(phi) - lO(i, 0).y * s!(theta)* s!(phi) - lO(i, 0).Z * c!(phi) + lO(i, 0).p

lR(i, 0).Z = -lO(i, 0).x * c!(theta) * c!(phi) - lO(i, 0).y * s!(theta)* c!(phi) + lO(i, 0).Z * s!(phi)

lR(i, 1).x = -lO(i, 1).x * s!(theta) + lO(i, 1).y * c!(theta)

lR(i, 1).y = -lO(i, 1).x * c!(theta) * s!(phi) - lO(i, 1).y * s!(theta)* s!(phi) - lO(i, 1).Z * c!(phi) + lO(i, 1).p

lR(i, 1).Z = -lO(i, 1).x * c!(theta) * c!(phi) - lO(i, 1).y * s!(theta)* c!(phi) + lO(i, 1).Z * s!(phi)

' Translate both points from 3D to 2D.

IF (lR(i, 0).Z + zCenter) <> 0 THEN
    scrX(i, 0) = 256 * (lR(i, 0).x / (lR(i, 0).Z + zCenter)) + xCenter
    scrY(i, 0) = 256 * (lR(i, 0).y / (lR(i, 0).Z + zCenter)) + yCenter
END IF

IF (lR(i, 1).Z + zCenter) <> 0 THEN
    scrX(i, 1) = 256 * (lR(i, 1).x / (lR(i, 1).Z + zCenter)) + xCenter
    scrY(i, 1) = 256 * (lR(i, 1).y / (lR(i, 1).Z + zCenter)) + yCenter
END IF

NEXT i

' Erase the old lines.

WAIT &H3DA, 8

IF justStarted = 0 THEN
  FOR i = 0 TO numLines
    LINE (oldX(i, 0), oldY(i, 0))-(oldX(i, 1), oldY(i, 1)), 0
  NEXT i
END IF

' Draw the new lines.

FOR i = 0 TO numLines
  LINE (scrX(i, 0), scrY(i, 0))-(scrX(i, 1), scrY(i, 1)), 11
NEXT i

theta = (theta + thetaRot) MOD 360
phi = (phi + phiRot) MOD 360

justStarted = 0
LOOP UNTIL INKEY$ = CHR$(27)

' Lines are stored in format (X1,Y1,Z1,p1)-(X2,Y2,Z2,p2)

DATA -50,50,50,1,50,50,50,1
DATA 50,-50,50,1,50,50,50,1
DATA 50,50,-50,1,50,50,50,1
DATA -50,-50,50,1,-50,50,50,1
DATA -50,50,-50,1,-50,50,50,1
DATA -50,-50,50,1,50,-50,50,1
DATA -50,50,-50,1,50,50,-50,1
DATA -50,-50,-50,1,50,-50,-50,1
DATA -50,-50,-50,1,-50,50,-50,1
DATA 50,-50,-50,1,50,-50,50,1
DATA 50,-50,-50,1,50,50,-50,1
DATA -50,-50,-50,1,-50,-50,50,1
Thanks for the late reply, I figured it out though.

I needed pollygon filling (or triangle filling if you like)

Here's the routine to fill a triangle by giving the coordinates x1,y1,x2,y2,x3,y3

Code:
SUB FillPoly (xx1, yy1, xx2, yy2, xx3, yy3, col)
DIM X(3) AS LONG, y(3) AS LONG
DIM left AS DOUBLE, right AS DOUBLE, cleft AS DOUBLE, cright AS DOUBLE
X(0) = xx1
X(1) = xx2
X(2) = xx3
y(0) = yy1
y(1) = yy2
y(2) = yy3
IF y(1) > y(0) THEN SWAP y(0), y(1): SWAP X(0), X(1)
IF y(2) > y(1) THEN SWAP y(2), y(1): SWAP X(2), X(1)
IF y(1) > y(0) THEN SWAP y(0), y(1): SWAP X(0), X(1)
a! = (X(2) - X(0))
b! = (y(1) - y(0))
c! = (y(2) - y(0))
IF a! = 0 THEN a! = 1D-16
IF b! = 0 THEN b! = 1D-16
IF c! = 0 THEN c! = 1D-16
X(3) = a! * b! / c! + X(0)
y(3) = y(1)
height = y(2) - y(1)
IF ABS(height) > 1 THEN
left = (X(3) - X(2)) / height
right = (X(1) - X(2)) / height
cleft = X(2)
cright = X(2)
FOR y = y(2) TO y(1)
  DrawLine cleft, cright, y, col  'my own function, it just draaws a horisontal line
  cleft = cleft - left
  cright = cright - right
NEXT
END IF
height = y(1) - y(0)
IF ABS(height) > 1 THEN
left = (X(3) - X(0)) / height
right = (X(1) - X(0)) / height
cleft = X(0)
cright = X(0)
FOR y = y(0) TO y(1) STEP -1
  DrawLine cleft, cright, y, col  'my own function, it just draaws a horisontal line
  cleft = cleft - left
  cright = cright - right
NEXT
END IF
END SUB

I use the DIM's in the code because I use DEFINT A-Z too speed things up :lol:

I got some excelent 3D programming tips here: http://www.sbdev.pwp.blueyonder.co.uk/intro.htm

Also thank you relsoft, I had to modify your torclean.bas (in torus.zip) I got a Div by 0 error, fixed it by adding 0.0001 (I know, I know, but I just wanted to see what it looked like :lol: )

I'm currently working on a way to remve flicker, I work in Screen 12 (for the res, can't go lower and don't need more colours) so I'm working on a doublle buffering routine, something with updating the top of the screen first and the bottom after that.

I will post more info on the D-Buff in screen 12 in another thread later on.
Code:
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<title>Introduction</title>
</head>

Now see, that's why I never find these! :roll:

Rel, if your still watching this...
I got that one-line backface culling to work! Big Grin I'm not too familiar with this yet, but it requires counter-clockwise coordinates, right? I had to readjust the vertex orders to make it work. I think it's counter-clockwise, but I haven't really looked at everything yet.
Dr. D: You can use any order you want(be it counter or clockwise) as long as they are in order. :*)

It has to do with the fact that the cross-product is "exclusive" to 3d. :*) There is no such thing as a right order. If you see the otherside when rendering, try:
If znormal<0 or znormal>0. :*)

You have mail. :*)