Determining distance from center of a triangle

Lets say I have 3*N vertices in a VBO representing N triangles.

In a fragment shader I want to know how far I am from the center of the triangle.

I think the best way of doing this is to use texture coordinates (or any custom varying) such that

vertex1, vertex2 and vertex3 of all N triangles have texture coordinates t1, t2 and t3 respectively.

How to I specify this without the massive redundancy of specifying a VBO of 3*N texture coordinates?

only via geom.shaders

Unless Im mistaken,
surely u only need 1 float number ( ie U dont need to pass each of the 3 corners per vertex)

the float, contains the distance from that point to the center
in the shader just query that number, no calculations needed

I’d probably do something foolish like send the center to the shader and take a direct reading from there, but then I’m a fool.

As Ilian suggested I used geometry shaders as follows:

Vertex shader

[b]
#version 120
attribute float radius;
varying float vRadius;
void main () {
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    gl_FrontColor = gl_Color;
    vRadius = radius;
}
 [/b]
[/b][/QUOTE]


Geometry shader


#version 120
#extension GL_EXT_geometry_shader4 : enable
varying in float[] vRadius;
varying out vec2 vLocalCoord;
vec2[3] cLocalCoords = vec2[3](vec2(0, 2), vec2(sqrt(3), -1), vec2(-sqrt(3), -1)); //how many times initialized?
void main() {
for(int i = 0; i < 3; ++i) {
gl_FrontColor = gl_FrontColorIn[0];
gl_Position = gl_PositionIn[0] + gl_ModelViewProjectionMatrix * vec4(vRadius[0] *cLocalCoords[i], 0.0, 0.0);
vLocalCoord = cLocalCoords[i];
EmitVertex();
}
EndPrimitive();
}

[/b][/QUOTE]

Fragment shader

[b]
#version 120
varying vec2 vLocalCoord;
void main () {
    float distanceFromLocalOrigin2 = vLocalCoord.x * vLocalCoord.x + vLocalCoord.y * vLocalCoord.y;
    if (distanceFromLocalOrigin2 &gt; 1.0)
        discard;
    gl_FragColor = gl_Color * (1.0 - distanceFromLocalOrigin2);
}
 [/b]
[/b][/QUOTE]

Program (Note I am converting a GL_POINT into a GL_TRIANGLE)

> [b]
>         program = new Program(vertexShader, geometryShader, fragmentShader);
>         program.setProgramParameteriEXT(GL.GL_GEOMETRY_INPUT_TYPE_EXT, GL.GL_POINTS);
>         program.setProgramParameteriEXT(GL.GL_GEOMETRY_OUTPUT_TYPE_EXT, GL.GL_TRIANGLE_STRIP);
>         program.setProgramParameteriEXT(GL.GL_GEOMETRY_VERTICES_OUT_EXT, 3);
>  [/b]
> [/b]


I would have liked to make the cLocalCoords array constant but "error C7516: OpenGL does not allow constant arrays"...

Don’t worry about not using “const”. The compiler will make it const.
(it’s very eye-opening to see the asm listing that cgc.exe or your nVidia drivers produce [with nVemulate])

I’d probably do something foolish like send the center to the shader and take a direct reading from there, but then I’m a fool.

I cant see how thats follish, that like the method I suggested should both work + IMO are simpler than the geometry shader method.
I must of misunderstood the question?

Well, he wants to avoid "VBO of 3N texture coordinates", so that’s why the only solution is geom.shader ; or slower tricks with boundable-uniforms/VTF.
Still, considering how much geometry needs to be “flat”-shaded (edgy stairs, walls etc) and thus go with NumVerts=3
NumTris, your approach of just adding one float/vertex is the optimal solution in those cases.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.