Computer Science Canada

More on .RAW

Author:  RaPsCaLLioN [ Mon Aug 16, 2004 10:11 am ]
Post subject:  More on .RAW

Update of .raw import. Try it with some big raw files.. I'll include one I found somewhere on the board. This should be pretty self explanatory but post any questions/comments/glitches.

Author:  Delta [ Mon Aug 16, 2004 11:18 am ]
Post subject: 

that's awesome man! I love it when you render it very nice work. Teach me the ways! Anyways... one thing... wireframe do that instead of the triangles and lil lines...

Author:  DanShadow [ Mon Aug 16, 2004 3:26 pm ]
Post subject: 

Shocked OMFG! That is awesome!! Keep up the great work!!
+bits

Author:  templest [ Wed Aug 18, 2004 10:18 am ]
Post subject: 

whaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaat.... Shocked

+20 Bits

Author:  RaPsCaLLioN [ Wed Aug 18, 2004 1:50 pm ]
Post subject: 

Delta wrote:
that's awesome man! I love it when you render it very nice work. Teach me the ways! Anyways... one thing... wireframe do that instead of the triangles and lil lines...


I only do the lil lines if the .RAW file is really large... if u load a smaller .RAW file like uh.... ship.raw or plane2.raw... then it will use full wireframe. U can find those somewhere on the board too

Author:  templest [ Wed Aug 18, 2004 4:47 pm ]
Post subject: 

I seriously doubt that you wrote those coordinate files by hand. Is this a MAYA or 3dsmax file you wrote a program to read and render, or did you really write all that out by hand? Confused

Author:  RaPsCaLLioN [ Wed Aug 18, 2004 6:25 pm ]
Post subject: 

templest wrote:
I seriously doubt that you wrote those coordinate files by hand. Is this a MAYA or 3dsmax file you wrote a program to read and render, or did you really write all that out by hand? Confused

Do you know anything about .RAW files? and I never claimed I wrote all those coordinates, are u crazy?

Author:  templest [ Wed Aug 18, 2004 6:33 pm ]
Post subject: 

Nope, I have no idea what a .RAW file is.
Nope, I'm not crazy.
Nope, I never suggested that you claimed you wrote those RAW files. Since it's your program, I assumed that was yours as well. Why don't know enlighten me, wtf is a RAW file?

Author:  Mazer [ Wed Aug 18, 2004 8:44 pm ]
Post subject: 

My understanding of them is that a .raw file is simply a format for 3D meshes that simply stores coordinates for the vertices.
So, one line of the file would mean one vertex and would look like
code:

X Y Z

(using actual values for the coords)

Also, and I'm not too sure about this one since I don't use this format, they may or may not store a normal vector for each face. I dunno.

It's pretty simple to write some script to export a .raw file (or another file with a similar format) using most popular 3D packages (Blender anyone?). The challenge lies in making a renderer for it in turing.

Kudos to you RaPs, kudos.

Author:  RaPsCaLLioN [ Thu Aug 19, 2004 9:12 am ]
Post subject: 

No normal stored in .raw file. Each line is stored as one vertice..
code:
X1 Y1 Z1 X2 Y2 Z2 X3 Y3 Z3

It's just strored as plain text format. I use a program called Deep Exploration to export to .raw format. I think Catalyst suggested that.

Author:  Mazer [ Thu Aug 19, 2004 2:19 pm ]
Post subject: 

RaPsCaLLioN wrote:
No normal stored in .raw file. Each line is stored as one vertice..
code:
X1 Y1 Z1 X2 Y2 Z2 X3 Y3 Z3

Er, don't you mean one triangle? Wink

Author:  RaPsCaLLioN [ Thu Aug 19, 2004 4:07 pm ]
Post subject: 

Well... Yes whatever... One triagle/ 3 vertices Smile

Author:  TheZsterBunny [ Thu Aug 19, 2004 11:07 pm ]
Post subject: 

Incredible.

Wow.

l33t.

I've wondered how one can make program such as that. can you perchance post your source. especially the 'render' button.

wow.

-Z

+93 bits

Author:  RaPsCaLLioN [ Fri Aug 20, 2004 2:09 am ]
Post subject: 

I'll post the code when I'm done tweaking.
I just noticed I didn't give any info on PgUP/PgDown - use those buttons for a better zoom... U should only use the Zoom buttons for .raw files that are coded really tiny... So yea, try PgUP/PgDown.

Author:  TheZsterBunny [ Fri Aug 20, 2004 8:45 am ]
Post subject: 

Before you post the code, I'd like to take my own shot at this.

2 problems I've encountered in the past were:

-the interpretation of the Z coord
-the rotation of the shape.

How do you get around these problems?

What formulas are used for the rotation and z-interpretation?

-Z

Author:  TheZsterBunny [ Fri Aug 20, 2004 4:15 pm ]
Post subject: 

I give up. I've tried all day, but I cannot figure out what i'm doing wrong.

I would very much like to learn how that program was done

-Z

Author:  RaPsCaLLioN [ Fri Aug 20, 2004 6:07 pm ]
Post subject: 

Do you know the equation for rotation? Just make an array that stores 3 coordinates for each point (X, Y, Z) - rotate each accordingly - same equation for any axis. And just draw the 'screen' x/y coordinates.

I don't make any sense...
I'll post the code but it doesn't make much more sense than I do. Let me play around w/ it a bit more first.

Author:  Mazer [ Fri Aug 20, 2004 6:11 pm ]
Post subject: 

I'm a bit curious, how is the Z sorting done? Just taking the average Z coord for each face and sorting them based on that?

Author:  RaPsCaLLioN [ Sat Aug 21, 2004 7:44 am ]
Post subject: 

Yep. So there are some cases where the display would be wrong because the avg of a polygon that should be located further away may be closer than the avg of an up front polygon.
code:
                  __        __
                _I__I______I  I___
               I__________ I  I___I
                 I  I      I  I
                 I  I      I  I
                 I  I      I  I
                 I  I      I  I
                 I  I      I  I
                _I  I______I__I__
               I_I  I____________I
                 I__I      I__I 
                                                 

This is another example of polygons that wouldn't be displayed right.

Author:  TheZsterBunny [ Sat Aug 21, 2004 7:45 am ]
Post subject: 

WHat is the rotation code?

I have a nifty way of keeping the shape stored, but rotation is giving me difficulties.

code:

type poly :
    record
        x : array 1 .. 3 of real
        y : array 1 .. 3 of real
        z : array 1 .. 3 of real
    end record
var shape : flexible array 1 .. 1 of poly
var input : int
var filename : string
put "Enter File Name followed by .raw: " ..
get filename
assert length (filename) > 4 and filename (* -3 .. *) = ".raw" and File.Exists (filename)
% filename := "spacestation2.raw"
open : input, filename, get
var count : int := 1
put "Reading File..."
loop
    for i : 1 .. 3
        get : input, shape (count).x (i)
        get : input, shape (count).y (i)
        get : input, shape (count).z (i)
    end for
    count += 1
    exit when eof (input)
    new shape, count
end loop
close : input
put upper (shape), " Polygons Read."



The answer is probably something very simple, and I'm missing it completely. I dug up some formulas for rotation, but I didn't understand them. Its from some computer science university class. Open with PPT.

I have to go,

-Z

Author:  RaPsCaLLioN [ Sat Aug 21, 2004 7:58 am ]
Post subject: 

This is the procedure for rotation. iTheta is the angle whether +/-. The big problem I encountered initially was during display. It took me forever to realize to use a temp variable to store the rounded value to display. I first rounded each stored point which caused distortion in shape.

code:
procedure Rotate (iAxisA, iAxisB, iTheta : int)

    var iU, iV : real

    for i : 1 .. iNumPolys
        for j : 1 .. 3
            iU := arPolygons (i, j, iAxisA)
            iV := arPolygons (i, j, iAxisB)
            arPolygons (i, j, iAxisA) := (iU * cosd (iTheta) + iV * sind (iTheta))
            arPolygons (i, j, iAxisB) := (-iU * sind (iTheta) + iV * cosd (iTheta))
        end for
    end for


end Rotate

Author:  TheZsterBunny [ Sat Aug 21, 2004 8:42 am ]
Post subject: 

Crap. I've seen that thing before too.

This
code:

var tempx, tempy, tempz : array 1 .. 3 of int
for i : 1 .. upper (shape)
    for j : 1 .. 3
        tempx (j) := round (shape (i).x (j)) + xoffset
        tempy (j) := round (shape (i).y (j)) + yoffset
        tempz (j) := round (shape (i).z (j)) % + ???????
    end for
    drawpolygon (tempx, tempy, 3, black)
end for

is what I've used to draw my shapes.

Can you please explain to me what the iAxis variables are?

Thanks

-Z

--edit--

and what arPolygons are

--edit--

I get it. Thanks.

-Z

-Z

Author:  TheZsterBunny [ Sat Aug 21, 2004 11:32 am ]
Post subject: 

Ok, got it.


'cept I'm having trouble with the z-sort.


...and shapes with over 10 000 polygons.


meh.

-Z

code:

var dispx, dispy : array 1 .. 3 of int
type poly :
    record
        xyz : array 1 .. 3, 1 .. 3 of real
    end record
var shape : flexible array 1 .. 1 of poly
var xoffset : int := maxx div 2
var yoffset : int := maxy div 2
var input : int
var filename : string
put "Enter File Name followed by .raw: " ..
get filename
assert length (filename) > 4 and filename (* -3 .. *) = ".raw" and File.Exists (filename)
open : input, filename, get
var count : int := 1
put "Reading File..."
loop
    for i : 1 .. 3
        get : input, shape (count).xyz (1, i)
        get : input, shape (count).xyz (2, i)
        get : input, shape (count).xyz (3, i)
    end for
    count += 1
    exit when eof (input)
    new shape, count
end loop
close : input
var unsort : array 1 .. upper (shape) of real
var sort : array 1 .. upper (shape), 1 .. 2 of real
var mx, my, mb : int
var yangle, xangle : int := 0
put upper (shape), " Polygons Read."
setscreen ("offscreenonly")
proc rotate (axis1, axis2, ang : int)
    var iu, iv : real
    for i : 1 .. upper (shape)
        for j : 1 .. 3
            iu := shape (i).xyz (axis1, j)
            iv := shape (i).xyz (axis2, j)
            shape (i).xyz (axis1, j) := (iu * cosd (ang) + iv * sind (ang))
            shape (i).xyz (axis2, j) := (-iu * sind (ang) + iv * cosd (ang))
        end for
    end for
end rotate
proc draw (axis1, axis2 : int)
    cls
    for i : 1 .. upper (shape)
        for j : 1 .. 3
            dispx (j) := round (shape (i).xyz (axis1, j)) + xoffset
            dispy (j) := round (shape (i).xyz (axis2, j)) + yoffset
        end for
        drawpolygon (dispx, dispy, 3, black)
    end for
    drawline (0, maxy div 2, maxx, maxy div 2, blue)
    drawline (maxx div 2, 0, maxx div 2, maxy, red)
    View.Update
end draw
var alicia : array char of boolean
loop
    Input.KeyDown (alicia)
    if alicia (KEY_LEFT_ARROW) and xoffset - 10 > 0 then
        xoffset -= 10
    end if
    if alicia (KEY_RIGHT_ARROW) and xoffset + 10 < maxx then
        xoffset += 10
    end if
    if alicia (KEY_UP_ARROW) and yoffset + 10 < maxy then
        yoffset += 10
    end if
    if alicia (KEY_DOWN_ARROW) and yoffset - 10 > 0 then
        yoffset -= 10
    end if
    mousewhere (mx, my, mb)
    if mb = 1 then
        if my > 0 and my < maxy then
            yangle := round (((my + yoffset) div 2 / maxy) * (10)) - 5
        else
            yangle := 0
        end if
        if yangle not= 0 then
            rotate (2, 3, yangle)
        end if
        if mx > 0 and mx < maxx then
            xangle := round (((mx + xoffset) div 2 / maxx) * (10)) - 5
        else
            xangle := 0
        end if
        if xangle not= 0 then
            rotate (1, 3, xangle)
        end if
    end if
    if xangle not= 0 or yangle not= 0 then
        draw (1, 2)
    end if
end loop

Author:  RaPsCaLLioN [ Sat Aug 21, 2004 7:42 pm ]
Post subject: 

When I used ur code I didn't have problems with .raw files larger than 10k.. What's happening?

Author:  TheZsterBunny [ Sun Aug 22, 2004 9:32 pm ]
Post subject: 

No problems with polygon numbers, but it becomes a bit slow.

My Z-Sort is quite slow, and there are occasional problems with overlap.

-Z

Author:  TheZsterBunny [ Sun Aug 22, 2004 9:58 pm ]
Post subject: 

code:

var dispx, dispy : array 1 .. 3 of int
var colorthing : int := 12
type poly :
    record
        xyz : array 1 .. 3, 1 .. 3 of real
    end record
var shape : flexible array 1 .. 1 of poly
var xoffset : int := maxx div 2
var yoffset : int := maxy div 2
var input : int
var filename : string
put "Enter File Name followed by .raw: " ..
get filename
assert length (filename) > 4 and filename (* -3 .. *) = ".raw" and File.Exists (filename)
open : input, filename, get
var count : int := 1
put "Reading File..."
loop
    for i : 1 .. 3
        get : input, shape (count).xyz (1, i)
        get : input, shape (count).xyz (2, i)
        get : input, shape (count).xyz (3, i)
    end for
    count += 1
    exit when eof (input)
    new shape, count
end loop
close : input
var unsort : array 1 .. upper (shape) of real
var sort : array 1 .. upper (shape), 1 .. 2 of real
var mx, my, mb : int
var yangle, xangle : int := 0
put upper (shape), " Polygons Read."
setscreen ("offscreenonly")
proc rotate (axis1, axis2, ang : int)
    var iu, iv : real
    for i : 1 .. upper (shape)
        for j : 1 .. 3
            iu := shape (i).xyz (axis1, j)
            iv := shape (i).xyz (axis2, j)
            shape (i).xyz (axis1, j) := (iu * cosd (ang) + iv * sind (ang))
            shape (i).xyz (axis2, j) := (-iu * sind (ang) + iv * cosd (ang))
        end for
    end for
end rotate
proc draw (axis1, axis2 : int)
    cls
    drawline (0, maxy div 2, maxx, maxy div 2, blue)
    drawline (maxx div 2, 0, maxx div 2, maxy, red)
    for decreasing i : upper (shape) .. 1
        for j : 1 .. 3
            dispx (j) := round (shape (round (sort (i, 1))).xyz (axis1, j)) + xoffset
            dispy (j) := round (shape (round (sort (i, 1))).xyz (axis2, j)) + yoffset
        end for
        colorthing := RGB.AddColor ((1 - (sort (i, 2) / 240)), (1 - (sort (i, 2) / 240)), (1 - (sort (i, 2) / 240)))
        drawfillpolygon (dispx, dispy, 3, colorthing)
        drawpolygon (dispx, dispy, 3, black)
    end for
    View.Update
end draw
proc zsort
    for qiy : 1 .. upper (shape)
        unsort (qiy) := (shape (qiy).xyz (3, 1) + shape (qiy).xyz (3, 2) + shape (qiy).xyz (3, 3)) / 3
        for yiq : 1 .. 2
            sort (qiy, yiq) := -999999
        end for
    end for
    for zing : 1 .. upper (shape)
        var templow : real := 999999
        var whichlow : int
        for zing2 : 1 .. upper (shape)
            if unsort (zing2) < templow then
                templow := unsort (zing2)
                whichlow := zing2
            end if
        end for
        sort (zing, 1) := whichlow
        sort (zing, 2) := templow
        unsort (round (whichlow)) := 99999
    end for
end zsort
var alicia : array char of boolean
loop
    Input.KeyDown (alicia)
    if alicia (KEY_LEFT_ARROW) and xoffset - 10 > 0 then
        xoffset -= 10
    end if
    if alicia (KEY_RIGHT_ARROW) and xoffset + 10 < maxx then
        xoffset += 10
    end if
    if alicia (KEY_UP_ARROW) and yoffset + 10 < maxy then
        yoffset += 10
    end if
    if alicia (KEY_DOWN_ARROW) and yoffset - 10 > 0 then
        yoffset -= 10
    end if
    mousewhere (mx, my, mb)
    if mb = 1 then
        if my > 0 and my < maxy then
            yangle := round ((my / maxy) * (10)) - 5
        else
            yangle := 0
        end if
        if yangle not= 0 then
            rotate (2, 3, yangle)
        end if
        if mx > 0 and mx < maxx then
            xangle := round ((mx / maxx) * (10)) - 5
        else
            xangle := 0
        end if
        if xangle not= 0 then
            rotate (1, 3, xangle)
        end if
    end if
    if xangle not= 0 or yangle not= 0 then
        zsort
        draw (1, 2)
    end if
end loop


Tried my hand at the Z-Sort and distance shading parts of the engine. Still having trouble with both.

Fair warning with this version. THe fewer polygons, the better. I've tested this with plane2.raw and it has been pretty smooth.

Except for the overlapping polygons.

-Z

Author:  RaPsCaLLioN [ Mon Aug 23, 2004 11:37 am ]
Post subject: 

I'll check that code out when I get home and Edit this reply with comments.

EDIT
I see what u mean with the z-sort. Look into a Quick sort method. I can help u there if u need it. Also, try culling your polygons and that should fix some of the display problems. That should also speed up the system quite a bit.

I never liked distance shading. Confused

Author:  TheZsterBunny [ Tue Aug 24, 2004 5:50 pm ]
Post subject: 

Thanks.

I've spent the last 2 days trying to remember what I learned in compsci class. the teacher showed us an incredible sorting procedure which sorted huge arrays almost instantly, but I cannot remember it.

Also, what is the polygon culling?

--edit--

well I found your qsort procedures and am trying to decipher them.

I'm just curious as to one part. The variables 'lo' and 'hi', are they the actual values or the unsorted positions of?

-Z

Author:  RaPsCaLLioN [ Wed Aug 25, 2004 9:50 am ]
Post subject: 

'lo' and 'hi' are the array positions of the section to sort.
Polygon culling is removing background polygons (which aren't seen anyway) from being drawn to speed up the process.

Author:  TheZsterBunny [ Wed Aug 25, 2004 3:10 pm ]
Post subject: 

so to sort the entire array

code:

var unsort : array 1..5 of int

1 would be 'lo' and 5 would be 'hi'?

There is also palce for 2 arrays. A is the output, B is the unsorted data?

-Z

Author:  RaPsCaLLioN [ Thu Aug 26, 2004 6:57 pm ]
Post subject: 

a is the centroid (depth value) of each polygon which to sort by and b is the associated location of that polygon in the 'master' array.

Author:  TheZsterBunny [ Thu Aug 26, 2004 9:20 pm ]
Post subject: 

Cool. That helped very much.

Now, if you don't mind, another question.

How does one go about culling polygons? I think it may have to do with calculating the area of a polygon and seeing if it overlaps any polygons entirely, but I believe this would be less efficient than drawing every polygon.

-Z

p.s. got distance shading done!

Author:  Mazer [ Fri Aug 27, 2004 5:56 am ]
Post subject: 

You could use the vertices of a triangle and get a normal vector for the face, then check the Z component of it to see if it goes into the screen or out of the screen. This would involve 'cross product', which you learn in grade 12 algebra, but I'm too lazy to explain it well enough right now. Check this site for more info.

Author:  RaPsCaLLioN [ Fri Aug 27, 2004 9:33 am ]
Post subject: 

Yea like that. U should also use that method for shading.

Author:  TheZsterBunny [ Fri Aug 27, 2004 5:54 pm ]
Post subject: 

...

This is the method I use for shading.

code:

        calclr := (abs (sort (1)) + sort (i)) / (abs (sort (1)) + sort (upper (shape)))
        if calclr > 1 then
            calclr := 1
        end if
        RGB.SetColor (clr, calclr, calclr, calclr)


where sort is my average Z array from lowest to highest.


--edit--

Mazer, I just finished struggling through grade 10 white math. It'll be a while before I learn this in class. If you get the time...

-Z

Author:  Mazer [ Fri Aug 27, 2004 7:43 pm ]
Post subject: 

I thought the site in my link explained it pretty well, but ok. It's really easy, actually, and in my opinion Mr. Caldwell's method is much simpler. Thus, it's the method I'll use. Just give me a minute to make some pretty illustrations.

Author:  Mazer [ Fri Aug 27, 2004 8:08 pm ]
Post subject: 

I hope this helps.

If you want to get the vectors from a triangle, pick an point and subtract it from the other two points. One more thing: order matters. If C = A crossed with B, then A crossed with b = -C.

Right hand rule:
Find vector A. Put your right hand out at A with your fingers stretched in the direction of the vector. Now curl your fingers toward vector B. Sticking your thumb out should show you the direction of vector C. I'm not sure how you would handle this. Are all of the triangles supposed to have their vertices listed in the same order?


: