1. help!

hey could some one send me some stuff on how to display 3-d points on the 2d
screen?
also can some one give me help on a raytracer?
It's good to be back
Jesse Kint

new topic     » topic index » view message » categorize

2. Re: help!

Hi,
I'm working on a wire-frame 3-D program as well. I found this on the
Internet somewhere:

                   ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
                   ³ Perspective Transforms ³
                   ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

        By Andre Yew (andrey at gluttony.ugcs.caltech.edu)


    This is how I learned perspective transforms --- it was
intuitive and understandable to me, so perhaps it'll be to
others as well.  It does require knowledge of matrix math
and homogeneous coordinates.  IMO, if you want to write a
serious renderer, you need to know both.

   First, let's look at what we're trying to do:
               S (screen)
               |    * P (y, z)
               |   /|
               |  / |
               | /  |
               |/   |
               * R  |
             / |    |
            /  |    |
           /   |    |
   E (eye)/    |    | W
---------*-----|----*-------------
         <- d -><-z->

   E is the eye, P is the point we're trying to project, and
R is its projected position on the screen S (this is the point
you want to draw on your monitor).  Z goes into the monitor (left-
handed coordinates), with X and Y being the width and height of the
screen.  So let's find where R is:

    R = (xs, ys)

    Using similar triangles (ERS and EPW)

    xs/d = x/(z + d)
    ys/d = y/(z + d)
    (Use similar triangles to determine this)

    So,

    xs = x*d/(z + d)
    ys = y*d/(z + d)

    Express this homogeneously:

    R = (xs, ys, zs, ws).

    Make xs = x*d
         ys = y*d
         zs = 0 (the screen is a flat plane)
         ws = z + d

    and express this as a vector transformed by a matrix:

    [x y z 1][ d 0 0 0 ]
             [ 0 d 0 0 ]    =  R
             [ 0 0 0 1 ]
             [ 0 0 0 d ]

    The matrix on the right side can be called a perspective transform.
But we aren't done yet.  See the zero in the 3rd column, 3rd row of
the matrix?  Make it a 1 so we retain the z value (perhaps for some
kind of Z-buffer).  Also, this isn't exactly what we want since we'd
also like to have the eye at the origin and we'd like to specify some
kind of field-of-view.  So, let's translate the matrix (we'll call
it M) by -d to move the eye to the origin:

    [ 1 0 0  0 ][ d 0 0 0 ]
    [ 0 1 0  0 ][ 0 d 0 0 ]
    [ 0 0 1  0 ][ 0 0 1 1 ]  <--- Remember, we put a 1 in (3,3) to
    [ 0 0 -d 1 ][ 0 0 0 d ]       retain the z part of the vector.

    And we get:

    [ d 0 0  0 ]
    [ 0 d 0  0 ]
    [ 0 0 1  1 ]
    [ 0 0 -d 0 ]

    Now parametrize d by the angle PEW, which is half the field-of-view
(FOV/2).  So we now want to pick a d such that ys = 1 always and we get
a nice relationship:

    d = cot( FOV/2 )

    Or, to put it another way, using this formula, ys = 1 always.

    Replace all the d's in the last perspective matrix and multiply
through by sin's:

    [ cos 0   0    0   ]
    [ 0   cos 0    0   ]
    [ 0   0   sin  sin ]
    [ 0   0   -cos 0   ]

    With all the trig functions taking FOV/2 as their arguments.
Let's refine this a little further and add near and far Z-clipping
planes.  Look at the lower right 2x2 matrix:

   [ sin sin ]
   [-cos 0   ]

   and replace the first column by a and b:

   [ a sin ]
   [ b 0   ]
   [ b 0   ]

   Transform out near and far boundaries represented homogeneously
as (zn, 1), (zf, 1), respectively and we get:

   (zn*a + b, zn*sin) and (zf*a + b, zf*sin).

   We want the transformed boundaries to map to 0 and 1, respectively,
so divide out the homogeneous parts to get normal coordinates and equate:

    (zn*a + b)/(zn*sin) = 0 (near plane)
    (zf*a + b)/(zf*sin) = 1 (far plane)

   Now solve for a and b and we get:

   a = (zf*sin)/(zf - zn)
     = sin/(1 - zn/zf)
   b = -a*zn
   b = -a*zn

   At last we have the familiar looking perspective transform matrix:

   [ cos( FOV/2 ) 0                        0            0 ]
   [ 0            cos( FOV/2 )             0            0 ]
   [ 0            0 sin( FOV/2 )/(1 - zn/zf) sin( FOV/2 ) ]
   [ 0            0                    -a*zn            0 ]

   There are some pretty neat properties of the matrix.  Perhaps
the most interesting is how it transforms objects that go through
the camera plane, and how coupled with a clipper set up the right
way, it does everything correctly.  What's interesting about this
is how it warps space into something called Moebius space, which
is kind of like a fortune-cookie except the folds pass through
each other to connect the lower folds --- you really have to see
it to understand it.  Try feeding it some vectors that go off to
infinity in various directions (ws = 0) and see where they come
out.

-----------------------------------------

Hope it helps :)

--PatRat (Thomas Parslow)
--               ()___()
--                (o o)
--                =\O/=
--             Rat Software
-- http://free.prohosting.com/~rats/ratsoft/

-----Original Message-----
From: jesse kint <jk2000 at PA.NET>
To: EUPHORIA at LISTSERV.MUOHIO.EDU <EUPHORIA at LISTSERV.MUOHIO.EDU>
Date: 29 November 1998 20:08
Subject: help!


>hey could some one send me some stuff on how to display 3-d points on the
2d
>screen?
>also can some one give me help on a raytracer?
>It's good to be back
>Jesse Kint
>

new topic     » goto parent     » topic index » view message » categorize

3. help!

I found out there are no functions for trimming white space(like LTRIM$
and RTRIM$ in BASIC), so i tried to make one. then i found out there are
no functions that can scan variables(like MID$ in BASIC). So, i found a
bbbbbbbiiiiiiiggggggggg disadvantage in Euphoria.

new topic     » goto parent     » topic index » view message » categorize

4. Re: help!

>I found out there are no functions for trimming white space(like LTRIM$
>and RTRIM$ in BASIC), so i tried to make one. then i found out there are
>no functions that can scan variables(like MID$ in BASIC). So, i found a
>bbbbbbbiiiiiiiggggggggg disadvantage in Euphoria.


A function to scan ?
A function to trim ?
This all sounds like string specific functions to me.. in the euphoriotic
way of thinking strings do not exist.

But see here how easily, it is achieved with the general purpose sequence
manipulators:

-- 100% safe, works with any kind of sequence
-- (is not recursive.. only the upper sequence gets cut)
function ltrim (sequence s)
    if not length(s) then return {} end if
    while not compare (s[1],' ')  do -- safer than s[1] = ' '
        s = s[2..length(s)]
    end while
    return s
end function

-- 100% safe, works with any kind of sequence
-- (is not recursive.. only the upper sequence gets cut)
function rtrim (sequence s)
    if not length(s) then return {} end if
    while not compare (s[length(s)],' ')  do -- safer than s[end] = ' '
        s = s[1..length(s)-1]
    end while
    return s
end function

-- And the combination of both (again 100% safe, non-recursive)
function trim (sequence s)
    return ltrim(rtrim(s))
end function

-- And this function shows how we mids () in euphoria.. off course the
euphoric way is much more cleaner than the basic way, but if you want to do
it the basic way, you could use this function --

function mids (sequence s, integer start, integer len)
    return s[start..start+len]
end function

Seen the light already ?

Ralf Nieuwenhuijsen
nieuwen at xs4all.nl

new topic     » goto parent     » topic index » view message » categorize

5. Re: help!

uh, ya

_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

new topic     » goto parent     » topic index » view message » categorize

6. Re: help!

Matt just wrote:

>I found out there are no functions for trimming white space(like LTRIM$
>and RTRIM$ in BASIC), so i tried to make one. then i found out there are
>no functions that can scan variables(like MID$ in BASIC). So, i found a
>bbbbbbbiiiiiiiggggggggg disadvantage in Euphoria.


You have to have a pretty big mouth to shout such big words...

The really big advantage of Euphoria is you can roll your own function
of this kind quite easily, and they are invariably zillion times
faster than equivalents in your beloved, crummy BASIC:

function ltrim(sequence s)
    -- 'short-circuit' optimized left trim
    integer c,i,len
    len=length(s)
    i=1
    while i<=len do
        c=s[i]
        if c=32 then i=i+1
        elsif c=10 then i=i+1
        elsif c=13 then i=i+1
        elsif c=9 then i=i+1
        else exit end if
    end while
    return s[i..len]
end function

function rtrim(sequence s)
    -- 'short-circuit' optimized right trim
    integer c,i
    i=length(s)
    while i do
        c=s[i]
        if c=32 then i=i-1
        elsif c=10 then i=i-1
        elsif c=13 then i=i-1
        elsif c=9 then i=i-1
        else exit end if
    end while
    return s[1..i]
end function

function trim(sequence s)
    -- trim 'white space' from both ends of string s
    -- 'short-circuit' optimized
    integer c,i,j,len
    len=length(s)
    i=1
    while i<=len do
        c=s[i]
        if c=32 then i=i+1
        elsif c=10 then i=i+1
        elsif c=13 then i=i+1
        elsif c=9 then i=i+1
        else exit end if
    end while
    j=len
    while j>=i do
        c=s[j]
        if c=32 then j=j-1
        elsif c=10 then j=j-1
        elsif c=13 then j=j-1
        elsif c=9 then j=j-1
        else exit end if
    end while
    return s[i..j]
end function

Enjoy. jiri

new topic     » goto parent     » topic index » view message » categorize

7. Re: help!

hey, i was calling the functions what they were. I didn't shout

_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

new topic     » goto parent     » topic index » view message » categorize

8. Re: help!

From the ignorant:  What does this mean?

Snortboy

Matt Z Nunyabidness wrote:

> I found out there are no functions for trimming white space(like LTRIM$
> and RTRIM$ in BASIC), so i tried to make one. then i found out there are
> no functions that can scan variables(like MID$ in BASIC). So, i found a
> bbbbbbbiiiiiiiggggggggg disadvantage in Euphoria.
>

new topic     » goto parent     » topic index » view message » categorize

9. help!

I have been working all night trying to define a global sequence
that changes a variable.
        The first time I tried a global anything many months ago,  It didn't
work, so I gave up and concentrated on other things.
        Now I am working with Rob Craig's sched.e, and have written a global
procedure intended to check the keyboard and change a variable or two,
I can get the procedure to run,  but not to actually change the variable.
        It will recognize a key is pressed,  print the key number, print
"hi" etc,  but will not act on the keypress.
        I have tried the abort(1),  a puts statement,  none of them work
inside of the elsif.
        The procedure is as follows,  any help?  The global function I tried
failed totally, so I am concentrating on global procedure first, since that
was what Rob had in there to begin with.

Start of io.e
------------------------------------------------------------

--try subtracting something
procedure subtract()
                lemx = lemx - 5
    puts(1,"hi")
end procedure



--main
global procedure task_keyb()
a = 1
a=a+1
while 1 do
    key = get_key()
         if key != -1 then



         ?key

         elsif key = 27 then
            --what to do if esc pressed
                gameover = TRUE
         elsif key = 331 then
            --what to do if left arrow
         subtract()--tried to use an in include procedure, it didn't work ither.
         elsif key = 333 then
            --what to do if right arrow
                lemx = lemx + 5
                ?lemx
         elsif key = 336 then
                lemy = lemy + 5
            --down arrow
         elsif key = 328 then
                lemy = lemy - 5
            --up arrow
         end if

        G_lempos = {lemx,lemy}

        exit
    end while
    --return G_lempos
    --return G_lempos--a variable to return to ???
end procedure

--end.

I have G_lempos as a global sequence, lemx and lemy as global atoms, all
defined in a global var.e file and made lemx and lemy = to a global constant
defined in the included variable file.
let lemx and lemy be equal to that constant right before the main procedure
in my main ex program, which I don't think has anything to do with this
part,  it is only the timing and scheduling stuff which has been made to
call only this global procedure so far.

        Anything else I can send, but can't understand why it will print an
amount, but not modify a variable.
        Anybody able to help???
Please....??????
Thanks Monty

new topic     » goto parent     » topic index » view message » categorize

10. Re: help!

Monty King writes:
> It will recognize a key is pressed,  print the key number, ...
> but will not act on the keypress.

>while 1 do
>   key = get_key()
>   if key != -1 then
>         ?key

>   elsif key = 27 then
>          --what to do if esc pressed
>          gameover = TRUE

>   elsif key = 331 then
>         --what to do if left arrow
>         subtract()--tried to use an in include procedure, it didn't work
 ither.

>   elsif key = 333 then
>         --what to do if right arrow
>         lemx = lemx + 5
>         ?lemx

>   elsif key = 336 then
>         lemy = lemy + 5
>         --down arrow

>   elsif key = 328 then
>         lemy = lemy - 5
>         --up arrow

>   end if
>end while

I think the root cause of all your difficulties is the
following:
>   key = get_key()
>   if key != -1 then
>         ? key

get_key() returns -1 when no key has been pressed.
This means that any key that is pressed will cause you
to execute "? key". And then
the entire if statement is *finished*. You will never test
for key = 331, or any other key. At most one alternative
can be chosen from a chain of if-elsif-elsif-end if

Try changing it to something like:
     if key != -1 then
          ? key
     end if

     if key = 27 then
        ...etc.

     elsif key = 331 then
        ...etc.

     elsif key = ...etc.

     end if


Your concerns about global procedures etc. are probably
due to this "if" problem.

Regards,
    Rob Craig
    Rapid Deployment Software

new topic     » goto parent     » topic index » view message » categorize

11. Re: help!

Robert Craig wrote:

> >         ?key

For some stupid reason I included this when it actually was removed
right before I sent that note, (no matter because I see how it works):


> Try changing it to something like:
>      if key != -1 then
>           ? key
>      end if
>
>      if key = 27 then
>         ...etc.
>
>      elsif key = 331 then
>         ...etc.
>
>      elsif key = ...etc.
>
>      end if


worked great.  Thank you it actually works now :)

Could you answer one more thing for me though?
(as long as I have your attention.I'll make it quick.

When you are defining a global procedure and it looks like:

global procedure my_procedure(integer x, sequence seq)

are these being passed to AND from the program/procedure?  I think I
understand that in a function it only returns whatever you type RETURN
VARIABLE as a successful or unsuccesfull function,  but does it pass the
variables in the
global function myfunction(integer x, sequ or whatever x)
back to the program or just straight to the function and then modifying
whatever global variables and returning a successful or unsuccesful
value with the Return statement.
        These aren't real well doc'd at least that I have seen.
It can be real confusing.
Thanks for making my program work,  am looking forward to learning more
about sched.e

Monty King

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu