Thursday, November 25, 2010

Displaying functions in a MetaSL shader

In the previous blog entries I talked about the procedural generation of lines, squares and spheres. Next I want to talk about the possibility to visualize 1 dimensional functions in your shader as a plotted graph. Depending on your expertise you might never ever want to do that. I remember that I always wondered at school what function graphs are useful for. It wasn't until I started 3D rendering, animation and shader writing that I saw a need for creating my own functions or try to understand what a certain function looks like. Now that has changed and I check regularly the look and shape of my functions.
I will not discuss WHY you would want to do this. You will find out at the moment when you need it. Just make sure to remember this blog entry. 

Displaying a function graph on a plane

Sometimes it can be really useful to try to understand a function and what it does in a graphical way. I will present a simple framework for displaying your graph inside a shader.

Using texture coordinates as our coordinate system
First we will start by displaying a typical function graph on a plane. As in the previous blogs I will be using texture_space[0] as my coordinate system. In mental mill, if you switch to the plane preview geometry object texture space 0 aligns perfectly with the quad. This means, it covers the UV coordinates which range from 0 to 1 linearly.


Changing the range of the coordinate system
Now that we have a suitable coordinate system at hand, we take a closer look: The UV coordinates range from 0 to 1 which might be fine in some cases, however, before we start plotting a function, we should add a function that allows us to change the range of our coordinate system. I will call the function change_range. It maps the interval that lies between old_min and old_max so that the interval will be remapped to lie between new_min and new_max.

Example: If old_min and old_max are 0 and 1 and new_min and new_max is 10 and 20 a value that was 0.5 in the 'old' range will be mapped to 15 in the new range.

Here is the MetaSL code for the function:

// changes the range of a given coordinate system.
// Values that varied between 'old_min' and 'old_max' 
// will be mapped so that they vary between 
// 'new_min' and 'new_max'
float change_range(float old_min, 
          float old_max,
          float new_min,
          float new_max,
          float x)
{
    return ((x-old_min)/(old_max - old_min))*(new_max - new_min) + new_min;
}


If we pass the UV coordinates and choose 0 and 1 for OldMin and Oldmax we can choose any arbitrary values for NewMin and NewMax. See the following image where the coordinate space is remapped using the function mentioned above: 

You can see that the shader debugger clearly shows the new
range of the texture coordinates.
Outputting a function graph
Now that we have met all our prerequisites, we can start thinking about plotting our function graph. For each value on the x-axis we compute a value on the y-axis that we will output.
So this task is now straight-forward: We write whatever function we want to calculate and feed the x-coordinate (of our remapped texture space) as our function argument. For example if we just take a square function it would look like this:

float x_square(float x)
{
  return x*x;
}

We will pass the x-coordinate to our function so that it will be processed by the function to obtain the new value. Now that we have computed the new value, how can we output a graph? In older blog posts I mentioned that inside a shader we can not ask to draw lines or polygons, because a shader is executed for a given pixel when lines and polygons are already being rasterized. So instead of asking "draw here" or "draw there" we ask: "Where are we and what information we have currently available?" and based in this information we can decide in which color we want to output that pixel/fragment that is being rasterized.
So we know the current coordinates and we have also calculated the new value. How can we put that into a graph? As you know, we want to show the resulting value in the y-axis of the coordinate system. As we can not explicitely ask to draw a point at a certain position, instead we test if the value that our function returned is greater or smaller than the value of the y-coordinate of our coordinate system.
If it is smaller, we assign the 'lower' color, otherwise the 'upper' color.
Displaying the function y = x*x using the remapped texture coordinates
as our coordinate system
Of course you can add more functionality to your shader by adding input parameters that allow you to change other parameters of your function: In the image below, the power function is used and the user may provide different values for the pow-function.

Check out the code sample here, feel free to extend and reuse it for your own projects!


Related links:
RGBA's past and future intros This video is a seminar that was held during Breakpoint '07. In this video clip, IQ talks about how he and his demo group are creating their demos. It has a strong realtime aspect and I recommend watching this video especially to those users who are interested in realtime shaders and those who like the demo scene in general. 
Check out the section at minute 14. Here you can see how function can be (ab)used to generate procedural textures. Very inspiring! (Note: Don't be confused: The file info says that the file  1kb only, but this will be the full video file!)

Monday, November 15, 2010

Rendering lines and squares

How to render lines in a MetaSL shader
If you haven't read the previous post, I recommend to do so as well. However, I will repeat some things: In a shader you can't ask the graphics engine to draw lines or triangles to the screen because the shader is executed already during that process.
Instead it has access to a variety of data that is passed to the shader. This data includes texture coordinates, normals, position in space and so on.
When rendering a pixel we need to determine whether we shall color the pixel because it is close to a line or whether we want to output the background color. So much for the theory. The simplest thing that we can do is to draw a straight line, let's say along the x-axis:

We can determine how far the pixel is away from it and apply a smoothstep function to generate a clamped transition value that fades from 0 to 1 within that range. This allows us to set the line width using a shader parameter:

I will let you figure out how that shader would look, it's really trivial. So next I want to extend the line drawing so that I am able to draw lines in any arbitrary angle. To define a line, we need 2 points, pt_a and pt_b.
There are plenty of explanations for calculating the distance between a point and a line, check out this link on Wolfram Mathworld, for example.

Using the function that calculates the distance of a point to the line, we can now draw lines with arbitrary slopes:
Check out the source code for the shader here

Rendering a wire/solid square in a MetaSL shader
Since we already covered how to calculate the distance from a line/circle, we are finally missing the squares and rectangles. Without further ado, the formula for the distance of a point from a rectangle is:

distance = max(abs(rect_center.x - coord.x) - rect_size.x,
           abs(rect_center.y - coord.y) - rect_size.y);


float2 rect_center - The center position of the rectangle
float2 coord - The coordinate to test against
float2 rect_size - The size of the rectangle

The debugger in mental mill allows you to take a look at the result of the distance function.
You can activate the debugger by holding down the "Ctrl" key and dragging with your mouse
in one of the debug variable viewports.


Drawing shapes in mental mill

This post should have been originally part of a much much longer entry, but it turned out to be way too elaborate, so I will start very small. The post today is all about drawing simple geometric shapes in mental mill, using the texture space as a coordinate sytem.

"So why would I ever want to do that!?" you may ask next. This is a good and valid question. It will become more obvious when I have one of my next blog entries ready that implements a simple 2 dimensional ray marcher in a shader. For now, just assume that you would like to render simple geometric shapes such as spheres and squares.

Rendering procedural 2D shapes in a shader
In a shader you can't just say "Draw a line from A to B" - this is because a shader is executed when the triangles and lines are being rasterized, so a shader can not issue drawing calls. But the shader has access to certain information that is passed along like texture coordinates, surface normal etc.
If we want a shader to draw procedural shapes, we need to change the process of generating procedural shapes. Instead of asking our program to draw something (which we can not do) we ask it instead: Is the (current) pixel that we are about to display near the line? If yes, it will receive the line color, otherwise we will use some background color.


Here we will use the texture coordinates as our coordinate system. This is also perfect to inspect in mental mill on the 'plane' geometry. Check out the shader code here.


This shader provides some control over the size and the position of the circle.

Rendering an outlined circle
However, we can change our shader to render a circle with a line. We do this by testing how far the current point is away from the boundary of the circle. If it is within a given range, we apply a smoothstep function which outputs a value from 0 to 1 when the length varies betwen 0 and "line_width".


The result of the shader can be seen here:

This is just the beginning. In the next post I will show how to render lines and squares, so stay tuned for more! Feel free to ask questions if you need more explanations. Shade safe and have a nice day! :)

Wednesday, November 10, 2010

Get inpired, check out the KoddeShader v2.0

These days one of our mental mill power users released his latest version of a versatile and powerful shader, the KoddeShader on the mental mill. This is already release 2.0 of Kogi's shader, so there are a couple of new features that you can check out.
It is always great to see how users create shaders using mental mill, export them and release their work to the public. I hope to see more great work coming! Also don't forget to check out the .pdf documentation, as it is written really carefully, containing lots and lots of illustrations.

Wednesday, November 3, 2010

Things you should never do

Today I read an interesting article about the consequences of writing software over and over again. While this can have a major impact in big companies, this also affects programmers who work on their own private project. Trying to start from scratch is something that happens to all of us who are writing code, no matter if it is an entire program or a shader. But you will learn those details in the article, so check it out.
I hope you feel as inspired as I did and remember: Good code never gets rusty, no matter how old it is!