GomesAndres
GomesAndres

Reputation: 13

How can i place a vertex in a fixed pixel?

Im trying to make some calculations using the gpu parallel capacities in Webgl.

In the program i am making, i have to divide the bounding box in a grid.

Then in the vertex shader for every vertex, i have to determine the cell of the vertex.

Having the cell of the vertex i have to determine the 2D position so i can save the result as a color in the pixel shader.

Reading the graphics pipeline, and how the viewport works, i have in some way achieved this, but i think im not placing the vertex in the pixel it correspond.

I have a mesh that has 47 vertex and i have make the calculations by hand, and the result its this.

Manual

But, the output of the shaders its this:

With Shader

Maybe i have missed some steps or calculations from the pipeline that are altering the result.

Vertex Shaders:

<script id="VertexRTT" type="x-shader/x-vertex">
precision highp float;

uniform vec3 max; //Vertex with the Max values
uniform vec3 min; //Vertex with the Min values
uniform float Dim; //Number of Cells for axis
uniform float RTDim; //Dimension of the Frambuffer/Texture/Viewport
//RTDim = Math.floor(Math.sqrt(ownCubic(Dim)));

attribute vec3 VertexPos;

varying vec3 Color;


void main(void) {
    vec3 pos;

    //First i take the vertex from the min-max range to the 0-1 range
    float X = floor((VertexPos.x - min.x)*(Dim)/(max.x - min.x));

    //Because i can have a vertex in the index Dim i have to subtract 1 (because the indexes go from 0 to Dim-1)

    if( X == Dim)X=X-1.0;

    float Y = floor((VertexPos.y-min.y)*(Dim)/(max.y - min.y));

    if( Y == Dim)Y=Y-1.0;

    Y = Y * Dim;
    float Z = floor((VertexPos.z-min.z)*(Dim)/(max.z - min.z));

    if( Z == Dim)Z=Z-1.0;

    Z = Z*Dim*Dim;
    //Make the 3D index a 1D index
    float temp = X + Y + Z;

    //Make the 1D index a 2D index
    pos.y = floor(temp/RTDim);
    pos.x = temp - (pos.y * RTDim);
    pos.z = 0.1;
    // it seems that the vertex with index 0 are begin culled
    pos.x = pos.x +1.0;
    pos.y = pos.y +1.0;
    //Take from the 0-RTDim range to the -1 - 1 Range
    pos.x = ((pos.x / RTDim)*2.0) - 1.0;
    pos.y = ((pos.y / RTDim)*2.0) - 1.0;

    //Right now the Color is fixed
    Color = vec3(1.0,1.0,1.0);

    gl_Position = vec4(pos, 1.0);

    //I am drawing as POINTS
    gl_PointSize = 1.0;
}

Viewport : gl.viewport(0, 0, RTDim, RTDim);

What can i be overlooking?

Upvotes: 1

Views: 82

Answers (1)

Swifter
Swifter

Reputation: 211

These lines may be the problem:

//Take from the 0-RTDim range to the -1 - 1 Range
pos.x = ((pos.x / RTDim)*2.0) - 1.0;
pos.y = ((pos.y / RTDim)*2.0) - 1.0;

To write to an individual pixel, you need to place the viewport coordinate (gl_Position) directly at the pixel centre.

Consider writing to a 2 x 2 texture with a viewport ranging from (-1, 1)

You should map the texels to viewport coordinates as follows

(0, 0) to (-0.5, -0.5)

(0, 1) to (-0.5, 0.5)

(1, 0) to (0.5, -0.5)

(1, 1) to (0.5, 0.5)

The following code maps texels to viewport cooridinates

//the width of the texture, in pixels
float RTDim; 
//the texel in range (0, RTDim - 1)
vec2 texel;

vec2 viewportCoords = ((texel + 0.5) / RTDim) * 2.0 - 1.0;

Upvotes: 2

Related Questions