# Sierpinski mandalas from GLSL to OSL

(quollism) #1

Allo all,

I’m trying to port https://www.shadertoy.com/view/4djGW1 into OSL but I’m hitting a snag in the “fractal” function.

The original GLSL code has the lines:

``````mat2 m = mat2(c, s, -s, c);
vec2 p = vec2(1.0, 0.0), r = p;
``````

and later

``````p = m*p;
``````

The vec2 p line is particularly intriguing. I’m not sure what that’s doing at all.

OSL doesn’t appear to do 2x2 matrices (only 4x4) and the spec says OSL can’t multiply matrices and vectors. I’m not hugely knowledgeable about linear algebra but I don’t mind learning. Any advice? This should be possible in OSL, right?

(varkenvarken) #2

the mat2 line defines a rotation matrix, presumably its preceded bij code like

``````
c = cos(phi); s=sin(phi);

``````

the matrix multiplication later on in your example simply rotates the vector p. in OSL you may use the rotate() function for that.

vectors (and points and normals) in OSL are all 3D. you can either set the z component to zero ( p[2]=0; ) or define a new type (OSL has structs) with corresponding functions but that is an awfull lot of work.

The vec2 p line in your example defines two vectors: p , which is initialized to the unit x vector, and r. So one of the ways to write out your example in OSL would be:

``````
vector p=vector(1,0,0);
vector r=p;

p = rotate(phi, p, vector(0,0,0), vector(0,0,1)); // check docs for rotate()

``````

does this help?

cheers,

– michel.

(quollism) #3

Hi Michel,

Still a bit lost here, sorry for taking so long to get back to this thread after your prompt response.

c and s are defined as:

``````float c = cos(1.0/float(SIDES)*TAU);
float s = sin(1.0/float(SIDES)*TAU);
``````

There’s some sort of rotation going on (guessing that’s what the deal is with putting pi/tau into trigonometric functions, bit of a newbie at matrices); if I don’t miss my guess it has something to do with axially repeating the pattern by however many SIDES are specified. So if there’s six sides, the pattern is repeated six different ways.

Beyond that, I have trouble following the maths. I’ll stick with easier to understand things and do more background study for now. This was much trickier than I first thought to understand.