Logo by kameelian - Contribute your own Logo!

END OF AN ERA, FRACTALFORUMS.COM IS CONTINUED ON FRACTALFORUMS.ORG

it was a great time but no longer maintainable by c.Kleinhuis contact him for any data retrieval,
thanks and see you perhaps in 10 years again

this forum will stay online for reference
News: Visit the official fractalforums.com Youtube Channel
 
*
Welcome, Guest. Please login or register. March 29, 2024, 04:32:24 PM


Login with username, password and session length


The All New FractalForums is now in Public Beta Testing! Visit FractalForums.org and check it out!


Pages: [1]   Go Down
  Print  
Share this topic on DiggShare this topic on FacebookShare this topic on GoogleShare this topic on RedditShare this topic on StumbleUponShare this topic on Twitter
Author Topic: Quick question about initializing variables  (Read 879 times)
0 Members and 1 Guest are viewing this topic.
Patryk Kizny
Global Moderator
Fractal Fertilizer
******
Posts: 372



kizny
WWW
« on: August 24, 2015, 12:50:52 PM »

Hey,
In most of the included frags there's a structure that passes init() function down to the main scene file and also #providesinit statement.

What is a practical difference between initializing variables and doing some 'free' calculations 'inline' vs within init() function? Is there anything that really matters besides convenience having this in one place in the code?

For examples take this:
Code:
qtrot = rotationMatrix3(normalize(qtRotVector), qtRotAngle);
which defines a rotation matrix for the quaternion fractal.
It could be done inline or it coud sit within init() function.

Thanks
Logged

Visual Artist, Director & Cinematographer specialized in emerging imaging techniques.
_revers_
Conqueror
*******
Posts: 138



« Reply #1 on: August 24, 2015, 02:47:16 PM »

The init() function is called only once per shader invocation. If you would put initialization for example in DE() function it would be called many times.
Consider Mandelbulb.frag: init() is called only once in the main() function in 3D.frag, but DE() function from Mandelbulb.frag is called in the DEF() function in DE-Raytracer.frag, which is called in many loops in the same file.
Logged
Patryk Kizny
Global Moderator
Fractal Fertilizer
******
Posts: 372



kizny
WWW
« Reply #2 on: August 24, 2015, 03:29:52 PM »

Yeah, but the question is what would be the difference between putting "float myVariable = 3.14529" within init() or outside of it as a 'free' line in the code, so outside of the body of any functions.
Logged

Visual Artist, Director & Cinematographer specialized in emerging imaging techniques.
_revers_
Conqueror
*******
Posts: 138



« Reply #3 on: August 24, 2015, 04:02:31 PM »

Well I think it is due to a fact that you can do more in functions than in global scope. Consider the following:
Code:
struct MyStruct {
vec3 pos;
vec3 col;
};
MyStruct array[2];

// This won't work in global scope:
array[0].pos = vec3(0.0);
array[0].col = vec3(1.0);
array[1].pos = vec3(0.0);
array[1].col = vec3(1.0);

// This will work:
void init() {
    array[0].pos = vec3(0.0);
    array[0].col = vec3(1.0);
    array[1].pos = vec3(0.0);
    array[1].col = vec3(1.0);
}

You can do a workaround:
Code:
MyStruct array[] = MyStruct[2](MyStruct(vec3(0.0), vec3(1.0)), MyStruct(vec3(0.0), vec3(1.0)));
But for larger arrays it might be more readable to put initialization in the init() function. Apart from above example I don't think there is much difference.
« Last Edit: August 24, 2015, 04:09:08 PM by _revers_ » Logged
Patryk Kizny
Global Moderator
Fractal Fertilizer
******
Posts: 372



kizny
WWW
« Reply #4 on: August 24, 2015, 07:50:12 PM »

Thanks, makes sense.
Logged

Visual Artist, Director & Cinematographer specialized in emerging imaging techniques.
Pages: [1]   Go Down
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
A quick hi! Meet & Greet Joost 2 1009 Last post September 27, 2010, 01:56:25 PM
by Nahee_Enterprises
Quick test question Mathematics David Makin 6 4483 Last post February 01, 2011, 04:34:11 AM
by David Makin
Quick Hello from fractal mad guy Meet & Greet simon.snake 1 845 Last post February 13, 2012, 02:29:10 AM
by David Makin
Quick formula question Mandelbulb 3d Glitchraptor 8 1603 Last post March 20, 2015, 10:51:44 PM
by DarkBeam
how to get the sum of 2 formulas with variables Mandelbulb 3d dyol 0 806 Last post May 02, 2016, 08:44:11 PM
by dyol

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM
Page created in 0.521 seconds with 25 queries. (Pretty URLs adds 0.058s, 2q)