Hello, I’m trying to develop a fragment shader, in order to get the real projected areas in pixels for every triangle. Thus, first I render the scene into a texture. Every triangle has a different colour. Then I render a point with the colour of the triangle and a fragment shader look up in all the texture, counting every texel that has the same colour of the point. So the result will be all points have changed their colour. And the colour is their area.
Well my fragment shader is:
uniform sampler2D tex;
uniform int width, height;
void getRGBA(in int number, out int r, out int g, out int b, out int a) {
int base = 256, quotient, rest, dividend = number, n = 0;
r = 0;
g = 0;
b = 0;
a = 0;
while (dividend >= base) {
quotient = dividend / base;
rest = dividend - (quotient * base);
if (n==0) r = rest;
if (n==1) g = rest;
if (n==2) b = rest;
if (n==3) a = rest;
dividend = quotient;
n++;
}
if (n==0) r = dividend;
if (n==1) g = dividend;
if (n==2) b = dividend;
if (n==3) a = dividend;
}
void main()
{
vec4 texel;
int i, n = 0, numPixels = height * width,
r, g, b, a;
float pixelw = 1.0 / float(width),
pixelh = 1.0 / float(height),
x = 0.0, y = 0.0;
for (i=0; i < numPixels; i++) {
texel = texture2D(tex, vec2(x, y));
if (all(equal(texel, gl_Color)))
n++;
if (x < (1.0 - pixelw))
{
x += pixelw;
} else {
x = 0.0;
y += pixelh;
}
}
getRGBA(n, r, g, b, a);
gl_FragColor = vec4( float(r) / 256.0, float(g) / 256.0, float(b) / 256.0, float(a) / 256.0);
}
Can I go throuth all the texture?. Any idea of what’s going wrong?.
Thanks in advance,
Pascual.