New kind of maze/world?

Discuss whatever you want here--both QB and non-QB related. Anything from the DEF INT command to the meaning of life!

Moderators: Pete, Mods

User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

New kind of maze/world?

Post by Mentat »

As some of you might know, I'm working of Wire V.2 . Well, I've been fiddeling around with the Z axis, and on one of my check runs, another bug popped up (well...it wasn't a bug in the sense that the code was bad. I just changed one of my initial variables). But was in the form of looking down a hall, like a maze.

So...
For any grievances posted above, I blame whoever is in charge . . .
User avatar
Stoves
Veteran
Posts: 101
Joined: Fri Feb 10, 2006 12:24 am
Location: Nashville, TN

Post by Stoves »

Your posting privileges ... revoked for 5 minutes!
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

Huh?
For any grievances posted above, I blame whoever is in charge . . .
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

How do I fill in an arbitrary polygon bounded by lines?
For any grievances posted above, I blame whoever is in charge . . .
Joe
Coder
Posts: 20
Joined: Wed Aug 15, 2007 3:11 pm
Contact:

Post by Joe »

Mentat wrote:How do I fill in an arbitrary polygon bounded by lines?
EDIT: oops, gave some wrong advice here. Dismiss it.
Last edited by Joe on Wed Aug 22, 2007 1:10 pm, edited 2 times in total.
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

I mean color. I need a quick way to fill in already made polygons for my RPG. Many polygons.
For any grievances posted above, I blame whoever is in charge . . .
k7
Coder
Posts: 41
Joined: Wed Aug 01, 2007 7:38 am
Location: Tasmania, Australia
Contact:

Post by k7 »

I would use paint, the position where the paint is targeted is the average of the two x values (x1, x2), and the average of the y values (y1, y2). That paints in the center of the shape.
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

So it fills the entire polygon up? :)
For any grievances posted above, I blame whoever is in charge . . .
Joe
Coder
Posts: 20
Joined: Wed Aug 15, 2007 3:11 pm
Contact:

Post by Joe »

Not a good idea. I've tried that before and the paint will miss the center in certain cases. And it also won't fill the entire triangle all the time (the corners will block the paint fill from reaching the empty pixels).

What you want to do is make your own triangle-drawing algorithm. What you need to do is calculate the slope between the vertices and then fill it in from top to bottom using LINE statements.

There are some tutorials on it somewhere. I can't find any for basic though. I'll make another post with examples when I have more time.
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

Thanks. :D
For any grievances posted above, I blame whoever is in charge . . .
Joe
Coder
Posts: 20
Joined: Wed Aug 15, 2007 3:11 pm
Contact:

Post by Joe »

Okay, I managed to code an example. I have a FB version if you want that too. It's commented, so it should be fairly easy to follow. All you need to have is an understanding of slopes or interpolation. Also, check out relsoft's tutorial on this: http://rel.betterwebber.com/mytutes/3dt ... apter3.htm; about halfway down you'll find the tutorial on polygon filling. It'll help fill in the gaps if you don't quite get how this example works. He has a whole series on 3D that's a very good read. I recommend it. You can find it here.

Code: Select all

'- Triangle fill example (QB Version)
'- by Joe King
'- 2007-08-22

TYPE tVertex
    
    x AS INTEGER
    y AS INTEGER
    
END TYPE

DECLARE SUB WireTriangle (v1 AS tVertex, v2 AS tVertex, v3 AS tVertex, col AS INTEGER)
DECLARE SUB FillTriangle (v1 AS tVertex, v2 AS tVertex, v3 AS tVertex, col AS INTEGER)
DECLARE SUB SortVertices (v1 AS tVertex, v2 AS tVertex, v3 AS tVertex)

'OPTION EXPLICIT

'- gfx resolution data
DIM resX AS INTEGER, resY AS INTEGER
resX = 320: resY = 200

SCREEN 13

'- vertex data
DIM v1 AS tVertex
DIM v2 AS tVertex
DIM v3 AS tVertex

'- generation data
DIM numTriangles AS INTEGER
DIM fillcol AS INTEGER
DIM bordcol AS INTEGER
DIM n AS INTEGER
    
numTriangles = 500
    
'- cycle through and draw all the triangles
FOR n = 1 TO numTriangles

    '- randomly generate the triangle vertex coordinates
    v1.x = RND(1) * resX: v1.y = RND(1) * resY
    v2.x = RND(1) * resX: v2.y = RND(1) * resY
    v3.x = RND(1) * resX: v3.y = RND(1) * resY
    '- randomly generate the fill and border colors
    fillcol = RND(1) * 8 + 7
    bordcol = fillcol - 8
    
    '- draw the filled triangle
    FillTriangle v1, v2, v3, fillcol
    '- draw the border (the wireframe version) to verify that the fill
    '- is working correctly
    WireTriangle v1, v2, v3, bordcol
                                
    WAIT &H3DA, 8 '- delay to let the viewer see what's happening
    
NEXT n
    
SLEEP
END

'- SUB FillTriangle
'-
'- Draws a solid triangle with the given vertices and color
'-
'-\
SUB FillTriangle (v1 AS tVertex, v2 AS tVertex, v3 AS tVertex, col AS INTEGER)
    
    CONST ZERO = 1E-32

    DIM x1 AS SINGLE, x2 AS SINGLE
    DIM slope12 AS SINGLE
    DIM slope13 AS SINGLE
    DIM slope23 AS SINGLE
    DIM stp AS INTEGER
    DIM y AS INTEGER
    
    '- vertices must be sorted before filling the triangle
    SortVertices v1, v2, v3
    
    '- calculate slopes
    '- the numbers appended at the end of the slope variables designate which
    '- line it is referring to (ex. slope12 mean the slope of the line between
    '- v1 and v2)
    '- the "+ ZERO" adds the dividend by a very small number to avoid division
    '- by zero erros
    slope12 = (v1.x - v2.x) / ((v1.y - v2.y) + ZERO)
    slope13 = (v1.x - v3.x) / ((v1.y - v3.y) + ZERO)
    slope23 = (v2.x - v3.x) / ((v2.y - v3.y) + ZERO)
    
    '- get starting sides
    x1 = v1.x
    x2 = v1.x
    
    '- determine which way we will be traveling while filling the triangle,
    '- either up or down
    IF v1.y <= v2.y THEN stp = 1 ELSE stp = -1
    
    '- draw first half
    FOR y = v1.y TO v2.y - 1 STEP stp
    
        LINE (x1, y)-(x2, y), col
    
        x1 = x1 + slope12
        x2 = x2 + slope13
    
    NEXT y
    
    x1 = v2.x
    
    '- draw second half
    FOR y = v2.y TO v3.y STEP stp
    
        LINE (x1, y)-(x2, y), col
    
        x1 = x1 + slope23
        x2 = x2 + slope13
    
    NEXT y
    
END SUB

'- SortVertices
'-
'- Sorts the given vertices by their y-value
'-
'-\
SUB SortVertices (v1 AS tVertex, v2 AS tVertex, v3 AS tVertex)
    
    DIM vTop AS tVertex
    DIM vMid AS tVertex
    DIM vBtm AS tVertex
    
    IF v1.y <= v2.y AND v1.y <= v3.y THEN
        
        vTop.x = v1.x: vTop.y = v1.y
        
        IF v2.y <= v3.y THEN
            vMid.x = v2.x: vMid.y = v2.y
            vBtm.x = v3.x: vBtm.y = v3.y
        ELSE
            vMid.x = v3.x: vMid.y = v3.y
            vBtm.x = v2.x: vBtm.y = v2.y
        END IF
        
    ELSEIF v2.y <= v1.y AND v2.y <= v3.y THEN
        
        vTop.x = v2.x: vTop.y = v2.y
        
        IF v1.y <= v3.y THEN
            vMid.x = v1.x: vMid.y = v1.y
            vBtm.x = v3.x: vBtm.y = v3.y
        ELSE
            vMid.x = v3.x: vMid.y = v3.y
            vBtm.x = v1.x: vBtm.y = v1.y
        END IF
        
    ELSE
        
        vTop.x = v3.x: vTop.y = v3.y
        
        IF v1.y <= v2.y THEN
            vMid.x = v1.x: vMid.y = v1.y
            vBtm.x = v2.x: vBtm.y = v2.y
        ELSE
            vMid.x = v2.x: vMid.y = v2.y
            vBtm.x = v1.x: vBtm.y = v1.y
        END IF
        
    END IF

    v1.x = vTop.x: v1.y = vTop.y
    v2.x = vMid.x: v2.y = vMid.y
    v3.x = vBtm.x: v3.y = vBtm.y
    
END SUB

'- SUB WireTriangle
'-
'- Draw a wireframe triangle with the given vertices and color
'-
'-\
SUB WireTriangle (v1 AS tVertex, v2 AS tVertex, v3 AS tVertex, col AS INTEGER)
    
    LINE (v1.x, v1.y)-(v2.x, v2.y), col
    LINE (v2.x, v2.y)-(v3.x, v3.y), col
    LINE (v3.x, v3.y)-(v1.x, v1.y), col
    
END SUB
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

Nice example. Worked great here :-)....
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
Lachie Dazdarian
Veteran
Posts: 202
Joined: Mon Aug 30, 2004 6:18 am
Location: Croatia
Contact:

Post by Lachie Dazdarian »

FB version, please.
Lachie Dazdarian - The Maker Of Stuff
Joe
Coder
Posts: 20
Joined: Wed Aug 15, 2007 3:11 pm
Contact:

Post by Joe »

Code: Select all

'- Triangle fill example (FB Version)
'- by Joe King
'- 2007-08-22

TYPE tVertex
    
    x AS INTEGER
    y AS INTEGER
    
END TYPE

DECLARE SUB WireTriangle (v1 AS tVertex, v2 AS tVertex, v3 AS tVertex, col AS INTEGER)
DECLARE SUB FillTriangle (v1 AS tVertex, v2 AS tVertex, v3 AS tVertex, col AS INTEGER)
DECLARE SUB SortVertices (v1 AS tVertex, v2 AS tVertex, v3 AS tVertex)

OPTION EXPLICIT

'- gfx resolution data
DIM resX AS INTEGER, resY AS INTEGER
resX = 320: resY = 200

SCREENRES resX, resY

'- vertex data
DIM v1 AS tVertex
DIM v2 AS tVertex
DIM v3 AS tVertex

'- generation data
DIM numTriangles AS INTEGER
DIM fillcol AS INTEGER
DIM bordcol AS INTEGER
DIM n AS INTEGER
    
numTriangles = 500
    
'- cycle through and draw all the triangles
FOR n = 1 TO numTriangles

    '- randomly generate the triangle vertex coordinates
    v1.x = RND(1) * resX: v1.y = RND(1) * resY
    v2.x = RND(1) * resX: v2.y = RND(1) * resY
    v3.x = RND(1) * resX: v3.y = RND(1) * resY
    '- randomly generate the fill and border colors
    fillcol = RND(1) * 8 + 7
    bordcol = fillcol - 8
    
    '- draw the filled triangle
    FillTriangle v1, v2, v3, fillcol
    '- draw the border (the wireframe version) to verify that the fill
    '- is working correctly
    WireTriangle v1, v2, v3, bordcol
                                
    SCREENSYNC  '- delay to let the viewer see what's happening
    
NEXT n
    
SLEEP
END

'- SUB WireTriangle
'-
'- Draw a wireframe triangle with the given vertices and color
'-
'-\
SUB WireTriangle (v1 AS tVertex, v2 AS tVertex, v3 AS tVertex, col AS INTEGER)
    
    LINE (v1.x, v1.y) - (v2.x, v2.y), col
    LINE (v2.x, v2.y) - (v3.x, v3.y), col
    LINE (v3.x, v3.y) - (v1.x, v1.y), col
    
END SUB

'- SUB FillTriangle
'-
'- Draws a solid triangle with the given vertices and color
'-
'-\
SUB FillTriangle (v1 AS tVertex, v2 AS tVertex, v3 AS tVertex, col AS INTEGER)
    
    DIM x1 AS SINGLE, x2 AS SINGLE
    DIM slope12 AS SINGLE
    DIM slope13 AS SINGLE
    DIM slope23 AS SINGLE
    DIM stp AS INTEGER
    DIM y AS INTEGER
    
    '- vertices must be sorted before filling the triangle
    SortVertices v1, v2, v3
    
    '- calculate slopes
    '- the numbers appended at the end of the slope variables designate which
    '- line it is referring to (ex. slope12 mean the slope of the line between
    '- v1 and v2)
    slope12 = (v1.x - v2.x) / (v1.y - v2.y)
    slope13 = (v1.x - v3.x) / (v1.y - v3.y)
    slope23 = (v2.x - v3.x) / (v2.y - v3.y)
    
    '- get starting sides
    x1 = v1.x
    x2 = v1.x
    
    '- determine which way we will be traveling while filling the triangle,
    '- either up or down
    IF v1.y <= v2.y THEN stp = 1 ELSE stp = -1
    
    '- draw first half
    FOR y = v1.y TO v2.y - 1 STEP stp
    
        LINE (x1, y) - (x2, y), col
    
        x1 = x1 + slope12
        x2 = x2 + slope13
    
    NEXT y
    
    x1 = v2.x
    
    '- draw second half
    FOR y = v2.y TO v3.y STEP stp
    
        LINE (x1, y) - (x2, y), col
    
        x1 = x1 + slope23
        x2 = x2 + slope13
    
    NEXT y
    
END SUB

'- SortVertices
'-
'- Sorts the given vertices by their y-value
'-
'-\
SUB SortVertices (v1 AS tVertex, v2 AS tVertex, v3 AS tVertex)
    
    DIM vTop AS tVertex
    DIM vMid AS tVertex
    DIM vBtm AS tVertex
    
    IF v1.y <= v2.y AND v1.y <= v3.y THEN
        
        vTop.x = v1.x: vTop.y = v1.y
        
        IF v2.y <= v3.y THEN
            vMid.x = v2.x: vMid.y = v2.y
            vBtm.x = v3.x: vBtm.y = v3.y
        ELSE
            vMid.x = v3.x: vMid.y = v3.y
            vBtm.x = v2.x: vBtm.y = v2.y
        END IF
        
    ELSEIF v2.y <= v1.y AND v2.y <= v3.y THEN
        
        vTop.x = v2.x: vTop.y = v2.y
        
        IF v1.y <= v3.y THEN
            vMid.x = v1.x: vMid.y = v1.y
            vBtm.x = v3.x: vBtm.y = v3.y
        ELSE
            vMid.x = v3.x: vMid.y = v3.y
            vBtm.x = v1.x: vBtm.y = v1.y
        END IF
        
    ELSE
        
        vTop.x = v3.x: vTop.y = v3.y
        
        IF v1.y <= v2.y THEN
            vMid.x = v1.x: vMid.y = v1.y
            vBtm.x = v2.x: vBtm.y = v2.y
        ELSE
            vMid.x = v2.x: vMid.y = v2.y
            vBtm.x = v1.x: vBtm.y = v1.y
        END IF
        
    END IF

    v1.x = vTop.x: v1.y = vTop.y
    v2.x = vMid.x: v2.y = vMid.y
    v3.x = vBtm.x: v3.y = vBtm.y
    
END SUB
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

Could raycasting be used to fill wireframes?
For any grievances posted above, I blame whoever is in charge . . .
Joe
Coder
Posts: 20
Joined: Wed Aug 15, 2007 3:11 pm
Contact:

Post by Joe »

Do you mean raytracing? There's a big difference between the two. Raytracing can fill polygons, but it's really processor intensive and generally slow even on today's computers. You'd have a lot of trouble trying to do real-time graphics with that method. But if you aren't and only care to produce pre-rendered images then raytracing is worth learning.

Raycasting is the "flat" form of raytracing, in which you only cast rays on a 2d plane.
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

I mean throwing rays at auxilery lines and painting around them, thus not "missing". Like this:
+-----+
|......./|
|...../..|
|.../....|
|./......|
+-----+
Where the "/" is the aux line and the dots are paint, and the boder is a square or something.
For any grievances posted above, I blame whoever is in charge . . .
Joe
Coder
Posts: 20
Joined: Wed Aug 15, 2007 3:11 pm
Contact:

Post by Joe »

I don't quite see what you're talking about. :?

Could you explain it in a little more detail?
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

Ok. Imagine you have a cube. One auxilary line (I made the name up) just connects one pair of diagonal vertices for each face. Then, later on paint both sides of the same color of the Aux line (but not the edges! Otherwise the computer will make a mistake and paint all over the place :) ). However, have the aux lines painted BEFORE the edges, so very thin sides might be ignored and thus avoiding missing. Once that's done, paint on both sides of the aux lines. Of course, you can do other neat things with the auxs.
For any grievances posted above, I blame whoever is in charge . . .
Joe
Coder
Posts: 20
Joined: Wed Aug 15, 2007 3:11 pm
Contact:

Post by Joe »

Hmm, I don't know. If you mean splitting up the polygon into triangles and painting them then that won't fix anything. When I said that using paint wasn't a good idea to fill polygons, I was referring to any arbitrary polygon including triangles. Just set up your own triangle filler using paint and try many different triangle shapes and you'll see what I mean. It won't only mess up some of the painting, but you won't be able to paint triangles on top of other triangles. Anyway, if I still don't understand your idea, then it's best just to try it out for yourself and see if it works.

Some examples of what I'm talking about:

Triangle not filled all the way:
http://z8.invisionfree.com/Delta_Code/i ... id=2497881

Can't paint triangles on top of other triangles:
http://z8.invisionfree.com/Delta_Code/i ... id=2497883
Post Reply