1. ## Data Types

Hello everybody

So I'm trying to learn how to use OSL and its not working too well. I know programming and the syntax and everything, but I can't seem to do anything more then just play around and make minor changes to the math to get minor changes in the result.

The thing is I just don't get what the math really is to try and manipulate it. I get what a color is and I get what a point is but I don't get what they are together and how to make things do much of anything.

For example I want to make a dot at point, seems easy enough, but I can't figure out how to make it work, I know pi*r^2 should probably have something to do with it some how but I have no idea where to even start.

Can anybody explain how they would go about making a dot at a point?

2. Hi Captain,

You can down load an example blend file from: http://www.pasteall.org/blend/23264

The screen shot below is taken from the example blend file - which shows how dots can be drawn that:
- Stick to the Global common co-ordinated space (the red dot)
- Stick to the object being shaded (the green dot)
- Stick to the camera screen (the blue dot)

Generally to draw anything in OSL use the mindset:

"My shader is being called right now to shade point P. Shall I shade this point or not - and what color should I shade it?"

This typically involves working out the distance form the point P being shaded to some other point of interest and then deciding on the color of the point based on that distance calculation.

In this sense - it's a little backwards from the way a coder usually draws a picture, by running a loop, deciding which x,y,z point to plot, choosing a colour and then plotting it.

Your shader will most likely be called millions of times for every render frame (at least once for every point P being shaded). You then have to decide whether to 'plot' something at this time. Not having access to variables between shader calls - and not knowing the order that the x,y,z points in the scene will be presented to the shader (if at all) just adds to the fun!

Here is the example dot shader source code from the example file link above.

Code:
```// For this shader example,
//  - Create a plane (in the x-y plane at the origin).
//  - Point the camera directly at the plane
// ie:  CTRL C to center the cursor
//      SHIFT A -> MESH -> ADD PLANE    to add the plane in the x-y plane
//
//      Right CLick camera to select the camera
//      ALT G   to move the cam to the origin
//      ALT R   to point it at the x-y plane
//      GZ      to grab the camera and lift it up with the mouse - so the camera can see the plane.
//

vector myPointPosition = vector(.5,.5,0),
float dotSize = 0.03,
output color outColor = 0)
{

// *** Common co-ordinates example. RED dot ***
// P is the Point being shaded in "common" , i.e Blender Global world co-ordinates
// P is an OSL global variable.
point pCommon = P;

// calculate the distance from the point being shaded to the desired position.
float distGlobal = distance(pCommon,myPointPosition);

// If the distance between the point being shaded (in common co-ordinates) and the desired dot position (myPointPosition)
// is less than the specified dotSize then draw a red pixel at this shader position.
if (distGlobal < dotSize) outColor = color(1,0,0);

// Note that the red dot does not 'stick' to the object if you move the object around.
// It sticks to the world co-ordinates defined be myPoint
// Change 'myPoint' to try this. It will move the red dot.
// Try moving the plane (in the x-y plane)

// *** Object co-ordinates example. GREEN dot ***
// transform P into object coordinates.
// (-1,-1,0) = bottom left of a plane that lies in the x-y plane.
// (1,1,0) = top right. This is irregardless of the plane size.
point pObject = transform("object",P);
float distObject= distance(pObject,myPointPosition);

// If the distance between the point being shaded (in object co-ordinates) and the desired dot position (myPointPosition)
// is less than the specified dotSize then draw a green  pixel at this shader position.
// Note that the green dot now sticks to the object if you move the object or camera around.
// This is generally what you want to do if you want your shader textures to stick to the object being shaded.
if (distObject < dotSize) outColor += color(0,1,0);

// *** Normalized devie coordinates (NDC) example. BLUE dot  ***
// transform P into NDC coordinates.
// (0, 0, 0) = bottom left the camera screen plane.
// (0.5, 0.5, 0) = center screen.
// (1, 1, 0) = top right. This is irregardless of the camera resolution or aspect ration.
point pCam= transform("NDC",P);

// set the z coordinate to 0 - as we don't want the distance from the camera to myPoint to be evaluated.
pCam[2] = 0;
float distCam= distance(pCam,myPointPosition);

// If the distance between the point being shaded (in object co-ordinates) and the desired dot position (myPointPosition)
// is less than the specified dotSize then draw a blue  pixel at this shader position.
// Move or rotate the camera. The blue dot sticks to the screen (as long as the object you are shading is within view).
// Note - you may have to do an actual render to see this effect - as the live 3D window in RENDER mode is effectively the camera.
// Camera or NDC co-ordiants are what you generally want to use if you are superimposing some effect over a plane parented to the camera.
if (distCam < dotSize) outColor += color(0,0,1);

// To find more coordinate spaces in OSL search for "geometric spaces" in the OSL language spec pdf
//
// Also check out: Thomas Dinges' examples: http://www.openshading.com/tutorial-3-basic-lines/
//
// There is alot of great OSL info on Michel J. Anders site too: http://blenderthings.blogspot.com.au/

}```

I hope this helps.

Cheers

Dylan

3. Thanks a lot for that. That was a very thorough and helpful explanation.
This is far simpler and cleaner then what I came up with it. I did make a dot with two vectors and the dot function although it didn't work too well.
After reading all the documentation at first I didn't see why there was such an emphasize on spaces and transformations, it just really didn't fit into what I thought I would need to do, but this clarifies why and how this work.

Again thank you very much for your time iReboot, its very appreciated.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•