Convert x, y, z coordinates into shader coordinates (-1, 1)

edited May 2014 in GLSL / Shaders

Hi all,

I'm trying to use shaders to draw points. I figured out that into shaders all the coordinates have values between -1 and 1, so I have to convert my PVector coordinates. For the X (0, width) and Y (0, height) values the operation is simple. How can I convert the Z coordinate?

Thanks. paolofuse

Answers

  • Would screenX() and screenY() help? http://processing.org/reference/screenX_.html You can give them an x, y, z point and they will map it to the screen coordinates. Then you only need to normalize the values... or did I get it wrong?

  • No, I need all 3 coordinates. Shader accepts 3 coordinates like PVector (x, y, z) but with values between -1 and 1. It's simple to convert X value and Y value, but I don't know to convert Z value. Maybe is the normalize operation you are talking about?

  • Ok, I guess then you have to decide which Z depth corresponds to 0 and which corresponds to 1. You said with x and y it's simple, but if x is -20, it can still be seen in the screen, for certain z values (as things get smaller further away). Or?

    I'm not sure if I get it, but I guess you have to decide to map a certain volume of space into the range 0 .. 1 (that's what I meant with normalization).

    So for instance:

    newX = map(x, 0, width, 0, 1);
    newY = map(y, 0, height, 0, 1);
    newZ = map(z, 0, (width+height)/2, 0, 1);
    

    Here I just randomly decided to grab the z range between 0 and the (width+height)/2. You could also use

    newZ = map(z, -(width+height)/4, (width+height)/4, 0, 1);
    

    or any other space region you like. But I'm saying all this only half understanding the problem :)

  • If the desired range is 0 to 1, we can use norm() instead of map(): :ar!
    http://processing.org/reference/norm_.html

  • For clarify this is what I do:

    xShader = (x - width / 2) / (width / 2);
    yShader = - (y - height / 2) / (height / 2;
    

    xShader goes from -1 (when X is 0) to 1 (when X is width)

    yShader goes from -1 (when Y is height) to 1 (when Y is 0)

    I want to do the same with Z coordinate, without have to choose arbitrary values. Thanks

  • We got width & height already. But I wonder how much depth there'd be?... :-??

  • edited May 2014

    Well, depth is related to the View Frustum (the area between the Near and Far planes of the camera). z0 is the near plane, z1 is the far plane. Things close to the near plane appear to be bigger and anything outside the view frustum is cropped.

Sign In or Register to comment.