This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#26 2007-11-03 22:21:27

Canthiar
Scratcher
Registered: 2007-05-16
Posts: 100+

Re: How my Raytracer works

Everything that I have covered shouldn't require  you to know any advance mathematics prior to reading the posts.  If I didn't cover anything sufficiently for you to understand please let me know.

Offline

 

#27 2007-11-04 09:50:21

Oldschooler2
Scratcher
Registered: 2007-08-28
Posts: 100+

Re: How my Raytracer works

Hey Canthiar, how exactly would I set up the raytracer to do a collision calculation? That is, how should I set up a raytracer to have it only put the pen color and shade to something other than black? Sorry if my question is confusing. By the by, do you know how I could set up a raytracer that draws say, a pyramid? And what is the Attenuation distance variable supposed to be used for?

Offline

 

#28 2007-11-04 09:53:11

Oldschooler2
Scratcher
Registered: 2007-08-28
Posts: 100+

Re: How my Raytracer works

My question is basically asking, how would I set up the ray tracer so it only changes from a black pen color to a different color and shade based on the distance to the object center variables.

Offline

 

#29 2007-11-04 11:01:10

Canthiar
Scratcher
Registered: 2007-05-16
Posts: 100+

Re: How my Raytracer works

Oldschooler2 wrote:

Hey Canthiar, how exactly would I set up the raytracer to do a collision calculation? That is, how should I set up a raytracer to have it only put the pen color and shade to something other than black?

For the sphere example I derived the collision with a ray to be this:

time = dot_product - sqrt( dot_product^2 - to_center^2 - radius^2 )

Since you can't have a rational value taking the square root of a negative number if dot_product^2 - to_center^2 - radius^2 is negative there is no collision and you don't update the time value.

If the entire equation comes up with a negative value then the surface of the object that we are looking for is behind the ray.  In that case you also don't update the time value.

In the cases where the time value is not set you will set your pen shade to 0.  In the other cases you will set your pen shade to some other value.  You can set the pen shade to a value like 50 until you learn how to get lighting to work.

Oldschooler2 wrote:

By the by, do you know how I could set up a raytracer that draws say, a pyramid?

Of the top of my head I couldn't tell you.  One way to do it is to use Constructive Solid Geometry where you start off with something like a plane or a cube and carve out pieces that you don't want.  Having the ability to do a CSG raytracer in Scratch would require too much work since it would require keeping up with the entry and exit times of all of the CSG objects the ray intersects rather than just the minimum time.

An infinite pyramid can be graphed using this function:  0 = -( abs(x+y) + abs(x-y) + z ).  Currently I don't have time to derive the intersection equation for a pyramid, but I'm sure that it is possible.

Oldschooler2 wrote:

And what is the Attenuation distance variable supposed to be used for?

I did a brief description of attenuation in the Illumination section.  When you have a light source the amount of light that it casts goes down the further away you get.  This decrease in light is called attenuation.  For modeling light sources for rendered images it is often easier to specify how far you would like the light to travel before its light contribution is less than ambient.  For my sample ray tracer I named this to be the attenuation distance.  This is basically the point at which the light from the light source has no affect on the object.

Offline

 

#30 2007-11-04 11:30:43

andresmh
Scratch Team at MIT
Registered: 2007-03-05
Posts: 1000+

Re: How my Raytracer works

Gigabyte123 wrote:

only bieng grade 7, and matrices and that stuff is grade 10, errrr, I know a little about matrices anyway. But I am not a university graduate for maths or something crazy like that.

Based on the black hole project you made I would say that understanding matrices should be pretty simple for you  smile


Andres Monroy-Hernandez | Scratch Team at the MIT Media Lab
on identi.ca and  twitter

Offline

 

#31 2007-11-17 11:54:27

Canthiar
Scratcher
Registered: 2007-05-16
Posts: 100+

Re: How my Raytracer works

I've been playing around with Scratch 1.2 and I think going forward I will use it for the rest of the tutorials.  So I will probably wait until it has been released to post the final set of projects.  I'm hoping to demonstrate multiple objects, occlusion, and reflection. 

I would really also like to do a super simple constructive solid geometry demonstration.

Offline

 

#32 2007-11-27 15:04:30

Gigabyte123
Scratcher
Registered: 2007-10-15
Posts: 100

Re: How my Raytracer works

I had a go at raytracing, but it only turned out be light and reflection, and embarasing!
I am not even sure it is real!


gather your confidence- reach for the skies... and always use deoderant!

Offline

 

#33 2007-11-28 13:59:40

Canthiar
Scratcher
Registered: 2007-05-16
Posts: 100+

Re: How my Raytracer works

I looked through the code that you posted, and even though it is not ray tracer, it is an interesting approach to rendering a simple scene.

Offline

 

#34 2007-11-29 17:25:13

jimi123
Scratcher
Registered: 2007-11-29
Posts: 1

Re: How my Raytracer works

is there a simple way to use scratch to run some of its own code and then to run a separate piece of compiled C code (or anything else)?

Offline

 

#35 2007-11-29 22:25:55

Canthiar
Scratcher
Registered: 2007-05-16
Posts: 100+

Re: How my Raytracer works

No, there is no way to run compiled code from Scratch.

Offline

 

#36 2007-12-11 21:24:52

Canthiar
Scratcher
Registered: 2007-05-16
Posts: 100+

Re: How my Raytracer works

Multiple Objects

Doing multiple objects is fairly easy.  When the ray is being cast into the scene each object that it encounters has a time value calculated for it.  This time value is the distance the surface of the object is from the start of the object.  If multiple objects generate a time value then the one that is closest to the start of the ray is the one that will be visible.

To do this in Scratch I created multiple sprites to represent each object.  The ray and collision variables were set to be shared by all sprites. Then the render loop would send a broadcast message that each sprite would get. 

If an object had a collision with the ray then it would check to see if it was closer than any other object.  If it is then it sets the time along the ray, the material color, and the direction that the surface is pointing.  When all scripts have run then the final results are for the closest object.

Example code:  http://scratch.mit.edu/projects/Canthiar/62288

Offline

 

#37 2007-12-13 12:30:01

Canthiar
Scratcher
Registered: 2007-05-16
Posts: 100+

Re: How my Raytracer works

Light Occlusion

Light occlusion is the term used for casting shadows because light is calculated from the surface of the object rather than from the light source and the reverse check looks for objects that occlude visibility to the light.  After the ray has been cast into the scene and the closest object has been found then the light for the surface has to be calculated.  Before it is calculated there has to be a check to see light is actually shining on the surface.

To do this a ray has to be created to test to see if the light is visible from the surface.  The start of the ray is at the intersection point on the surface and the direction is from the surface to the light to check with.

When this check is done the location of the collision and the material color where the collision occurs don't matter.  To do this I created a variable that indicated that an occlusion check was being done so that the material color and normal variables didn't get overridden.

Example code: http://scratch.mit.edu/projects/Canthiar/62289

Offline

 

#38 2008-01-02 18:45:36

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Re: How my Raytracer works

So in scalar points in vectors, to get the vector of two points, you do this formula: x of A minus x of B plus y of a minus y of b? Because how you do it you do not add  the first total too the second.


http://i.imgur.com/WBkM2QQ.png

Offline

 

#39 2008-01-02 18:48:50

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

Re: How my Raytracer works

Canthiar wrote:

This post is directed toward Oldschooler2 in regards to my raytracer project http://scratch.mit.edu/projects/Canthiar/5347.

when i go on that link, it says no such page. did you take the project offline?


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#40 2008-01-02 19:29:10

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: How my Raytracer works

Lucario621, I don't understand your question at all.

Vector addition and subtraction is done separately on the x and y parts:
   (xa,ya) + (xb,yb) = (xa+xb, ya+yb)
The result is another vector, not a scalar.

Offline

 

#41 2008-01-03 18:25:42

Canthiar
Scratcher
Registered: 2007-05-16
Posts: 100+

Re: How my Raytracer works

funkymonkey wrote:

Canthiar wrote:

This post is directed toward Oldschooler2 in regards to my raytracer project http://scratch.mit.edu/projects/Canthiar/5347.

when i go on that link, it says no such page. did you take the project offline?

The period was throwing things off.  I've removed it from the original post.

Offline

 

#42 2008-01-08 12:47:55

Canthiar
Scratcher
Registered: 2007-05-16
Posts: 100+

Re: How my Raytracer works

Reflection

Things in the real world that have a glossy or polished surface act like mirrors, they reflect the light coming off of the objects around them where you can still see the detail of the objects.  Modeling this using a ray tracer is fairly easy, you just cast a ray from the surface of the object and any color that comes out of that ray cast contributes to the color of the original object.

The reflection vector cast into the scene is similar to the reflection vector used for calculating lighting.  The only difference is that you reflect the main ray instead of the light ray.

In a normal ray tracer you can model two reflective surfaces, the light shining off of the glossy part of the surface and the light shining off of the pigment of the surface.  These would normally be calculated as final_color = color + specular_color * specular_reflection * reflection_color + diffuse_color * diffuse_reflection * reflection_color, where each color has a red, green, and blue component.

In Scratch there is no way to set individual red, green, or blue components.  To get around this when the material color of a surface was generated it also generated a likely intensity for that surface.  On the reflective pass that material intensity was multiplied by the reflectivity and added to the intensity of the surface on the original object.

Sample project here: http://scratch.mit.edu/projects/Canthiar/62290

Offline

 

#43 2008-03-23 12:40:32

Ty101
Scratcher
Registered: 2008-03-23
Posts: 19

Re: How my Raytracer works

YYYYYYYYYYAAAAAAAAAAAAAYYYYYYYYYYYYYY!

Offline

 

#44 2008-04-09 02:15:07

MasterOfMac
Scratcher
Registered: 2008-02-01
Posts: 62

Re: How my Raytracer works

This is hella-awesome...
I think scratch needs a "fill" block. That way, the fastpen 3D method could be solid; rather than wireframe...

Anyway, thank you very much for the technical vocab explanation... Now I know how raytracers render well; not just that they're 'better'  smile


*sarcasm*

Offline

 

Board footer