Granted, it doesn't sound like much at first glance... however, there is everything going on under the surface. What the UpdateFrame node does is report an event whenever there is a render step in the Shader window. In order to do that, there needs to be a Shader window, so UpdateFrame will create one.
So what is going on in a Shader window? You can specify that by double-clicking on the UpdateFrame node. When you do that for this example, something like this will show up:
This is the shader collection editor. What it does is specify what shaders are running in the window. What are shaders exactly? Shaders are little programs that tell your graphics card what should be drawn on the screen. In the old times, you were able to directly write to your screen as a matrix of pixels. Nowadays, it is very rare that you are able to do this, both because it is excruciatingly slow, and also because it is unsafe to take complete and direct control of your screen in user applications (think viruses, etc...).
So what you do is write little programs that run massively parallel in your graphics card whenever there is a Draw command. Bonsai uses OpenGL to do this, which uses shader programs written in GLSL. You can read a bit about GLSL shaders here, but there is a whole world of tutorials and discussion forums about them as they form a huge chunk of game development pipelines; or we can just keep exploring the space of possible examples in Bonsai.
#version 400
uniform vec2 scale = vec2(1, 1);
uniform vec2 shift;
in vec2 vp;
in vec2 vt;
out vec2 tex_coord;
void main()
{
gl_Position = vec4(vp * scale + shift, 0.0, 1.0);
tex_coord = vt;
}
#version 400
uniform float time;
uniform float frequency = 100;
uniform float speed = 0.1;
in vec2 tex_coord;
out vec4 frag_colour;
void main()
{
float index = tex_coord.x + time * speed;
float value = 0.5 * sin(index * frequency) + 0.5;
frag_colour = vec4(value,value,value,1);
}
Hopefully you can get a feeling for how these colors are interpolated across the surface for each intermediate pixel in the triangle.
4) fragment shader: this is a program very much like the vertex shader, but now mapping every pixel data in the triangle to a color. The final color is specified by the output variable frag_colour. This program also runs in parallel for all pixels in the surface.
Ok, so if all this pipeline makes sense, we can get back to your original questions:
1) where does the vertex data come from in the first place?
2) how does it get mapped in the vertex shader?
The answer to 1) is that it either comes pre-loaded in one of Bonsai's built-in shader configurations or you have to pass it in explicitly using the UpdateVertexBuffer node. There are currently four built-in shader configurations: ShaderConfiguration; PointSprite; TexturedQuad; and TexturedModel. Of these, only the last two actually pre-load vertex data for you.
The TexturedQuad configuration I've used draws a rectangle (or quad) at coordinates (-1,1), (1,1), (1,-1), (-1,-1). Also as the name indicates, the quad is textured (i.e. it will map a bitmap across its surface; useful when you want to display an image for example) so the vertex data also includes texture coordinates indicating which parts of the bitmap are mapped to which vertex (read more about texture coordinates here).
#version 400
uniform sampler2D tex;
in vec2 tex_coord;
out vec4 frag_colour;
uniform float radius = 1;
void main()
{
vec2 pixelpoint = tex_coord * 2 - 1;
float d = length(pixelpoint) < radius ? 1 : 0;
frag_colour = vec4(d,d,d,1);
}
Is there a way to get the shader to draw the circle shader inside the embedded workflow while its still running and have it draw blankShader outside of the embedded workflow?
Hi,I wanted to run the sine grating example using shaders. I think the files you posted are from an old version of bonsai? I re-created it in the current version but I am only getting a static grating. what setting do I have to set to make it move? I understand that the time variable is used to reset the position of the grating in each frame. I am guessing my shader gets executed only once?
--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/4ed1f168-0528-4932-9fef-e3559561b937%40googlegroups.com.