Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 3D Code Help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SNIPERDUDE




PostPosted: Thu Aug 28, 2008 8:35 am   Post subject: 3D Code Help

Ok, so I have the basics of 3D. I think it's right...
Anyways I have hit another snag, how do I make it so I can fill in
faces or make it so some lines are hidden (when behind another face)?

Here's what I have so far:

Turing:

View.Set ("graphics")
View.Set ("offscreenonly")

colorback (120)

const depth := 10

fcn divi (x, y : real) : real
    if y = 0 then
        result 9999999
    else
        result (x / y)
    end if
end divi

proc drawdot3d (x, y, z : real, c : int)
    drawdot (round (divi (x, z / depth) + maxx / 2) - 1, round (divi (y, z / depth) + maxy / 2) - 1, c)
end drawdot3d

proc drawline3d (x1, y1, z1, x2, y2, z2 : real, c : int)
    drawline (round (divi (x1, z1 / depth) + maxx / 2), round (divi (y1, z1 / depth) + maxy / 2), round (divi (x2, z2 / depth) + maxx / 2), round (divi (y2, z2 / depth) + maxy / 2), c)
end drawline3d

var h : real := -10
var xe : int := 100
var ye : int := 100
var w : int := 50
var key : array char of boolean

type Vert :
    record
        X, Y : int
        Z : real
    end record
   
type LineProp :
    record
        P1, P2, Clr : int
    end record

var Point : array 1 .. 100 of Vert
var Line : array 1 .. 100 of LineProp

for i : 1 .. 100
    Point(i).X := 0
    Point(i).Y := 0
    Point(i).Z := 0
    Line(i).P1 := 0
    Line(i).P2 := 0
end for



%% House Properties

Point(1).X := 0
Point(1).Y := 0
Point(1).Z := 0
Point(2).X := 0
Point(2).Y := 50
Point(2).Z := 0
Point(3).X := 50
Point(3).Y := 50
Point(3).Z := 0
Point(4).X := 50
Point(4).Y := 0
Point(4).Z := 0
Point(5).X := 0
Point(5).Y := 0
Point(5).Z := 1
Point(6).X := 0
Point(6).Y := 50
Point(6).Z := 1
Point(7).X := 50
Point(7).Y := 50
Point(7).Z := 1
Point(8).X := 50
Point(8).Y := 0
Point(8).Z := 1
Point(9).X := 25
Point(9).Y := 0
Point(9).Z := 1.2
Point(10).X := 25
Point(10).Y := 50
Point(10).Z := 1.2
Point(11).X := 0
Point(11).Y := 30
Point(11).Z := 0.5
Point(12).X := 0
Point(12).Y := 20
Point(12).Z := 0.5
Point(13).X := 0
Point(13).Y := 30
Point(13).Z := 0
Point(14).X := 0
Point(14).Y := 20
Point(14).Z := 0
Line(1).P1 := 1
Line(1).P2 := 4
Line(1).Clr := 0
Line(2).P1 := 1
Line(2).P2 := 2
Line(2).Clr := 0
Line(3).P1 := 2
Line(3).P2 := 3
Line(3).Clr := 0
Line(4).P1 := 3
Line(4).P2 := 4
Line(4).Clr := 0
Line(5).P1 := 5
Line(5).P2 := 6
Line(5).Clr := 0
Line(6).P1 := 6
Line(6).P2 := 10
Line(6).Clr := 0
Line(7).P1 := 10
Line(7).P2 := 7
Line(7).Clr := 0
Line(8).P1 := 7
Line(8).P2 := 8
Line(8).Clr := 0
Line(9).P1 := 8
Line(9).P2 := 9
Line(9).Clr := 0
Line(10).P1 := 9
Line(10).P2 := 5
Line(10).Clr := 0
Line(11).P1 := 9
Line(11).P2 := 10
Line(11).Clr := 0
Line(12).P1 := 1
Line(12).P2 := 5
Line(12).Clr := 0
Line(13).P1 := 2
Line(13).P2 := 6
Line(13).Clr := 0
Line(14).P1 := 3
Line(14).P2 := 7
Line(14).Clr := 0
Line(15).P1 := 4
Line(15).P2 := 8
Line(15).Clr := 0
Line(16).P1 := 11
Line(16).P2 := 12
Line(16).Clr := brightred
Line(17).P1 := 12
Line(17).P2 := 14
Line(17).Clr := brightred
Line(18).P1 := 13
Line(18).P2 := 11
Line(18).Clr := brightred


proc draw (xe : int)
for i : 1 .. 100
    if Line(i).P1 = 0 and Line(i).P2 = 0 then
        exit
    else
        drawline3d (xe + Point(Line(i).P1).X, ye + Point(Line(i).P1).Y, h + Point(Line(i).P1).Z, xe + Point(Line(i).P2).X, ye + Point(Line(i).P2).Y, h + Point(Line(i).P2).Z, Line(i).Clr)
    end if
end for
end draw

loop
    Input.KeyDown (key)
   
    if key (KEY_UP_ARROW) then
        ye += 3
    elsif key (KEY_DOWN_ARROW) then
        ye -= 3
    end if
   
    if key (KEY_RIGHT_ARROW) then
        xe += 3
    elsif key (KEY_LEFT_ARROW) then
        xe -= 3
    end if
   
    if key ('w') then
        h += 0.1
    elsif key ('s') then
        h -= 0.1
    end if
   
    drawfillbox (0, 0, maxx, maxy, 7)
    draw (xe)

    View.Update
    delay(10)
    exit when key (KEY_ESC)
end loop


Use the arrow keys to move the house around, the 'W' key to zoom in, and the 'S' key to zoom out.
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Thu Aug 28, 2008 9:33 am   Post subject: RE:3D Code Help

10 million isn't exactly a very good estimate of x/0, I suggest you go with something bigger, like maxint.
Zren




PostPosted: Thu Aug 28, 2008 12:09 pm   Post subject: Re: 3D Code Help

Your either looking for:

Back-Face Culling: Ignore drawing the polygons that aren't seen.
http://www.gamedev.net/reference/articles/article1088.asp
Posted Image, might have been reduced in size. Click Image to view fullscreen.

Painter's Algorithm: Set each polygon to a certain depth, sort that list, then draw each polygon. The Java link is to the basic list generation, you get many errors with that much with polygons that cover each other. So there are some imperfect tests to run in order to slightly fix things.
http://www.javaworld.com/javaworld/jw-07-1997/jw-07-howto.html?page=1
Posted Image, might have been reduced in size. Click Image to view fullscreen.
http://www.siggraph.org/education/materials/HyperGraph/scanline/visibility/painter.htm

Personally I haven't done either before. I tried working with the Painter's algorithm once though. It kinda failed which is why Virus was old-school vector display styled.
Zylum's 3D Engine has both of these in it and is good to look at in order to understand concepts.
http://compsci.ca/v3/viewtopic.php?t=9639
SNIPERDUDE




PostPosted: Thu Aug 28, 2008 5:02 pm   Post subject: RE:3D Code Help

Thanks guys, no time to look at it now...
I'll let you know how it goes when I have the time to look at it...
CodeMonkey2000




PostPosted: Thu Aug 28, 2008 11:04 pm   Post subject: RE:3D Code Help

I think that using the z-buffer algorithm might be easier and the most correct, but since you can't use a graphics accelerator it will be VERY slow.

Striahgt from gamedev:
code:
Clear Z-Buffer to a certain value

For every polygon
    For every scanline of that polygon
        For every pixel
            If this is nearest pixel {
                Write Pixel
                Write Z into zbuffer
            }
        End
    End
End
SNIPERDUDE




PostPosted: Fri Aug 29, 2008 6:47 am   Post subject: RE:3D Code Help

Thought about that one.
Would it run at any good speed in C++?
CodeMonkey2000




PostPosted: Fri Aug 29, 2008 10:33 am   Post subject: RE:3D Code Help

Well if you are using openGL, it does it itself, just enable GL_DEPTH_TEST and make sure you clear the depth buffer. OpenGL can do it faster too since it uses hardware acceleration, you don't need to use the painter's algorithm in openGL, since it's not that much faster anyway.
SNIPERDUDE




PostPosted: Fri Aug 29, 2008 6:44 pm   Post subject: RE:3D Code Help

Meh, first I'll think I'll try learning the painter's algorithm before I try learning other languages...
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: