3d sonogram with GLSL shader

Arthur Kuhn's icon

Hello !

I'm trying to create a 3d sonogram that would display sound as a plane of concentric circles.

I'm now trying to create a matrix containing my amplitude values as polar coordinates, with a jit.gl.slab and a GLSL shader.
I found a snippet of code there which does just that and I adapted it, but I get the errors :
ERROR: 2:24 : " : Illegal non-ASCII character(0x96)
ERROR: 2:24 : 'Center' : Syntax error syntax error
And the shader can't compile.

Here is the code of my shader:

<jittershader name="default">
    <description>Default Slab </description>
    <param name="scale" type="float" default="1.0" />
    <param name="tex0" type="int" default="0" />
    <param name="modelViewProjectionMatrix" type="mat4" state="MODELVIEW_PROJECTION_MATRIX" />
    <param name="textureMatrix0" type="mat4" state="TEXTURE0_MATRIX" />
    <param name="position" type="vec3" state="POSITION" />
    <param name="texcoord" type="vec2" state="TEXCOORD" />

    <language name="glsl" version="1.5">

        <bind param="scale" program="fp" />
        <bind param="tex0" program="fp" />
        <bind param="modelViewProjectionMatrix" program="vp" />
        <bind param="textureMatrix0" program="vp" />
        <bind param="position" program="vp" />
        <bind param="texcoord" program="vp" />

        <program name="vp" type="vertex"  >
        <![CDATA[
            #version 330 core

in vec3 position;
in vec2 texcoord;
out jit_PerVertex {
vec2 texcoord;
            } jit_out;
out vec2 jit_textCoord;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 textureMatrix0;

void main(void) {
gl_Position = modelViewProjectionMatrix*vec4(position, 1.);
                jit_out.texcoord = vec2(textureMatrix0*vec4(texcoord, 0., 1.));
                jit_textCoord = jit_out.texcoord;
            }
        ]]>
        </program>

        <program name="fp" type="fragment"  >
        <![CDATA[
            #version 330 core

in vec2 jit_textCoord;

// Control inputs
uniform float Angle; // range 2pi / 100000.0 to 1.0 (rounded down), exponential
uniform float AngleMin; // range -3.2 to 3.2
uniform float AngleWidth; // range 0.0 to 6.4
uniform float Radius; // range -10000.0 to 1.0
uniform float RadiusMin; // range 0.0 to 2.0
uniform float RadiusWidth; // range 0.0 to 2.0
uniform vec2 Center; // range: -1.0 to 3.0

layout (location = 0) out vec4 outColor;

// Texture input
uniform sampler2D Texture;

void main()
{
// Normalised texture coords
vec2 texCoord = jit_textCoord.xy;
// Shift origin to texture centre (with offset)
vec2 normCoord;
normCoord.x = 2.0 * texCoord.x – Center.x;
normCoord.y = 2.0 * texCoord.y – Center.y;
// Convert Cartesian to Polar coords
float r = length(normCoord);
float theta = atan(normCoord.y, normCoord.x);

// The actual effect
r = (r < RadiusMin) ? r : (r > RadiusMin + RadiusWidth) ? r : ceil(r / Radius) * Radius;
theta = (theta < AngleMin) ? theta : (theta > AngleMin + AngleWidth) ? theta : floor(theta / Angle) * Angle;

// Convert Polar back to Cartesian coords
normCoord.x = r * cos(theta);
normCoord.y = r * sin(theta);
// Shift origin back to bottom-left (taking offset into account)
texCoord.x = normCoord.x / 2.0 + (Center.x / 2.0);
texCoord.y = normCoord.y / 2.0 + (Center.y / 2.0);

// Output
outColor = texture2D(Texture, texCoord);
}
        ]]>
        </program>
    </language>
</jittershader>

Has anyone ever encountered those errors ? How can I correct them ?

Thanks !

Rob Ramirez's icon

this ERROR: 2:24 : " : Illegal non-ASCII character(0x96) error indicates that there is some non-standard characters copied in from the web site. This was tricky to track down, but eventually I figured out that dash being used in the following 2 lines is not the proper subtraction dash, but instead a longer dash (- vs –):
normCoord.x = 2.0 * texCoord.x - Center.x;
normCoord.y = 2.0 * texCoord.y - Center.y;

replacing that dash with the proper subtraction character will get rid of the illegal characters error. you also need to use texture instead of texture2d for gl3 compatible shaders.

Arthur Kuhn's icon

Thanks a lot for your answer ! It worked and no the errors are gone.

But... The effect doesn't seem to happen... All I get if I connect my jit.gl.slab to a pwindow is the same image , but in red, because I shifted it from a one dimensional matrice to a tri-dimensional matrice prior to the shader. The math of the cartesian/polar transformation are quite mysterious to me but from what I gathered it seems like the formula should work, so I guess I'm missing something from the GLSL programming side ?

Edit : I got it... I forgot to actually give any of the variables values, it is working now !

Also, as the ultimate goal is to displace vertices along the y axis according to the result of this shader, what do you think is the best way to go : Keep it as a texture and use it as a heightmap, or try to feed the texture to another shader that will use it in a vertex program ?

Arthur Kuhn's icon

It is alive !

I managed to make my project work, and will now focus on improving it and understand it better.
I'm attaching a zip file containing the patch and shader, all feedback will be appreciated !

Thanks !

A.KUHN - GL3_3dSonogram.zip
application/x-zip-compressed 8.17 KB

Rob Ramirez's icon

nice, thanks for sharing!

I'm assuming you are running on Windows and using gl3 engine? On Mac the shader fails to compile due to using texture2DRect instead of texture. Easy fix. I'm guessing windows is more permissive here...