![](/i/fill.gif) |
![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
From: Vincent Le Chevalier
Subject: Re: 99 lines of C++ for an unbiased ray tracer
Date: 14 Jan 2010 14:07:41
Message: <4b4f6b7d$1@news.povray.org>
|
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Darren New wrote:
>> Line 79 means each row of pixels is computed in parallel, right?
>
> Got me. It looks like someone is asking the compiler to do that if it can.
>
It's using the OpenMP directives
(http://en.wikipedia.org/wiki/OpenMP)... I discovered that last year,
can be pretty useful and makes it quite easy to take advantage of
multiple cores when the algorithms are already mostly suited to being
run in parallel.
--
Vincent
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
From: Orchid XP v8
Subject: Re: 99 lines of C++ for an unbiased ray tracer
Date: 14 Jan 2010 14:18:53
Message: <4b4f6e1d@news.povray.org>
|
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
>> I don't find it really crazy, actually I was expecting far worse
>> before opening the file...
>
> Me too, honestly. You can see what it's doing, and you could probably
> translate it by hand into some other OO imperative language without even
> knowing what it's trying to do. It's not obfuscated as much as terse.
It's just shoved together into a very small space, so it's a big awkward
to read.
What I've probably done is got a plus sign instead of a minus somewhere,
or something equally trivial, which has none the less completely screwed
up the math. And damn, it's going to take a while to find it! :-/
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
>> 44: double n=sizeof(spheres)/sizeof(Sphere);
>>
>> Well... that's... one way to figure out how big an array is. :-.
>
> That's the usual way to figure out how big an array is in C and C++.
It is surprising to me that this actually works. I was under the
impression that C does not actually distinguish between pointers and
arrays (and integers and booleans and...)
>> Evidently my C++ is weak, but... how is
>> 53: Vec nl=n.dot(r.d)<0?n:n*-1;
>> different from "nl = -abs(n.dot(r.d))"?
>
> In the lack of needing to define the abs() function?
No, I misread the code. It's actually deciding whether to negate n based
on a function of n, not n itself.
>> Also, where THE HELL is "Xi" defined? I can see it *used* in several
>> places, but I can't find a definitions.
>
> Line 48. It's an argument to the function.
No, it's an argument in the radiance() function, I meant where is it
defined in main().
> Note that text search will
> usually answer that sort of question on a C program, and often on a C++
> program too.
>
> Not always, mind, but usually.
Yeah, I tried that too...
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Tim Cook <z99### [at] gmail com> wrote:
> scott wrote:
> >> 53: Vec nl=n.dot(r.d)<0?n:n*-1;
> >>
> >> different from "nl = -abs(n.dot(r.d))"?
> >
> > a?b:c evaluates to b if a is true, or c otherwise.
> LPC might be a little different, but can't you negate a variable by
> saying a=-a;? Saves a little CPU cost.
Yes, but since 'nl' is of type struct Vec, he would have to define
operator-() for it for that to work. Maybe he wanted to save one line
of code.
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Vincent Le Chevalier <gal### [at] libertyallsurfsp amfr> wrote:
> Darren New wrote:
> >> Line 79 means each row of pixels is computed in parallel, right?
> >
> > Got me. It looks like someone is asking the compiler to do that if it can.
> >
> It's using the OpenMP directives
> (http://en.wikipedia.org/wiki/OpenMP)... I discovered that last year,
> can be pretty useful and makes it quite easy to take advantage of
> multiple cores when the algorithms are already mostly suited to being
> run in parallel.
The advantage of OpenMP is that the routine will run in parallel if
the compiler supports it, but if the compiler doesn't support OpenMP,
it doesn't matter: The program will still compile and work properly
(it will just be single-threaded). Compilers ignore #pragmas they don't
recognize (per the standard), so the addition #pragma openmp's don't
matter.
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Darren New <dne### [at] san rr com> wrote:
> http://www.kevinbeason.com/smallpt/
A small pitty that it's not 100% standard C++, as erand48() is not part of
the standard (it's a glibc extension). OTOH, better use erand48() than rand(),
as the latter is just plain horrible in quality.
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Orchid XP v8 <voi### [at] dev null> wrote:
> >> 44: double n=sizeof(spheres)/sizeof(Sphere);
> >>
> >> Well... that's... one way to figure out how big an array is. :-.
> >
> > That's the usual way to figure out how big an array is in C and C++.
> It is surprising to me that this actually works. I was under the
> impression that C does not actually distinguish between pointers and
> arrays (and integers and booleans and...)
There is a big difference between a pointer and an array in C (and hence
in C++).
Basically each array is a type of its own, defined by the element type
and the number of elements. What makes an array distinctly a type of its
own is that sizeof() always returns the number of bytes that a type
requires when allocated in memory, and an array is no different: sizeof()
for an array returns the amount of bytes taken by the entire array. This
is drastically different from a sizeof() of a pointer, which will always
return 4 or 8 (depending on whether you are compiling a 32-bit or 64-bit
program).
Since sizeof() of an array returns the total amount of bytes taken by
an array, if you divide it by the amount of bytes of one element, you get
the total number of elements.
For example if in C++ you instantiate a template using an array as a
template parameter, a distinct instanstiation will be made for each specified
type and size of array.
Of course what makes this confusing is that an array implicitly casts to
a pointer of the element type. In other words, wherever a pointer of the
element type is required, you can give an array of that type instead (there
will be an implicity cast to the pointer type).
Also the syntax of indexing an array and indexing memory using a pointer
is identical, adding to the confusion.
It can sometimes be difficult to distinguish between an array and a
const pointer (a pointer which cannot be changed to point somewhere else).
sizeof() is one thing which makes the distinction.
> >> Also, where THE HELL is "Xi" defined? I can see it *used* in several
> >> places, but I can't find a definitions.
> >
> > Line 48. It's an argument to the function.
> No, it's an argument in the radiance() function, I meant where is it
> defined in main().
Line 82.
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
From: Orchid XP v8
Subject: Re: 99 lines of C++ for an unbiased ray tracer
Date: 14 Jan 2010 15:18:04
Message: <4b4f7bfc@news.povray.org>
|
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Warp wrote:
> There is a big difference between a pointer and an array in C (and hence
> in C++).
Really?
> Since sizeof() of an array returns the total amount of bytes taken by
> an array, if you divide it by the amount of bytes of one element, you get
> the total number of elements.
This is plain. What surprises me is that sizeof() can give you an
accurate size in the first place.
> Of course what makes this confusing is that an array implicitly casts to
> a pointer of the element type. In other words, wherever a pointer of the
> element type is required, you can give an array of that type instead (there
> will be an implicity cast to the pointer type).
Ah. I didn't know that. All the books I've seen claim that they are *the
same type*.
This makes more sense now...
>>>> Also, where THE HELL is "Xi" defined? I can see it *used* in several
>>>> places, but I can't find a definitions.
>>> Line 48. It's an argument to the function.
>
>> No, it's an argument in the radiance() function, I meant where is it
>> defined in main().
>
> Line 82.
Yes, we've established that now. I wasn't aware that you could actually
define more than one variable in a loop initialiser...
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Orchid XP v8 <voi### [at] dev null> wrote:
> > Since sizeof() of an array returns the total amount of bytes taken by
> > an array, if you divide it by the amount of bytes of one element, you get
> > the total number of elements.
> This is plain. What surprises me is that sizeof() can give you an
> accurate size in the first place.
Why wouldn't it? The compiler has to know the size of the array if it
wants to allocate it in memory.
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Warp wrote:
> Also the syntax of indexing an array and indexing memory using a pointer
> is identical, adding to the confusion.
Plus, you can't pass an array to a function, at least in C.
--
Darren New, San Diego CA, USA (PST)
Forget "focus follows mouse." When do
I get "focus follows gaze"?
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |