Here I go again, I came back on my shape drawing with lwjgl.
But this time no triangulor, I try to use shader instead. My main point is to have antialiasing and a “good” speed. I use an Intel G45 for my test (I was surprised to see that this card has shaders !!!)
To do so, I fill a lookup texture with all segment for one polygone. Then I use a shader to see if the point is in the polygon.
This is my first time with a real shader but everything go right. There is still a bug and the antialiasing is wrong. But this is far slower than expected :-. I have tried with an “empty” shader, I have around 150fps. With the shader, 5 fps… With some googling, someone suggested to remove all “if” branch, but it don’t give mush speed increase.
Is it pointless to try to make it run on a G45 or is there something wrong with my way of doing it ?
The shader for a simple even-odd filling polygone :
varying vec2 coord;
varying vec4 color;
uniform sampler2D shape;
vec2 p = vec2(0,0);
float getPoint(float i)
{
p.x = floor(mod(i+0.0,1024.0))/1024.0;
p.y = floor((i+0.0)/1024.0)/8.0;
vec4 c = texture2D(shape,p.xy);
float a1 = floor(0.5 + c.x * 255.0);
float a2 = floor(0.5 + c.y * 255.0);
float a3 = floor(0.5 + c.z * 255.0);
float a4 = floor(0.5 + c.w * 255.0);
return (a4>=255.0)? -1.0 : (((((a4*256.0)+a3)*256.0+a2)*256.0)+a1)/1024.0;
}
void main()
{
vec2 n = vec2(0,0);
float alpha = 0.0;
int compt = 0;
float x1;
float y1;
float x2;
float y2;
float a1,a2,a3,a4;
for(float i=0.0;i<1024.0*4.0;i+=4.0)
{
x1 = getPoint(i);
if(x1<0.0) { break; }
y1 = getPoint(i+1.0);
x2 = getPoint(i+2.0);
y2 = getPoint(i+3.0);
n.x = (x2-x1);
n.y = (y2-y1);
float dx = (coord.x-x1);
float dy = (coord.y-y1);
float l = sqrt(n.x*n.x+n.y*n.y);
n = n/l;
float tan = dx*n.y-dy*n.x;
float nor = dx*n.x+dy*n.y;
tan = (tan<0.0) ? -tan:tan;
float sig = (y2-y1);
float c1 = (coord.x-x1)*sig;
float c2 = (x2-x1) * (coord.y-y1);
int d1 = ( c1 >= c2 )? 1-compt : compt;
int d2 = ( c1 <= c2 )? 1-compt : compt;
int temp = ( sig >= 0.0 )? d1 : d2;
compt = ((coord.y>=min(y1,y2))&&(coord.y<=max(y1,y2))) ? temp : compt;
float al = ((nor>=0.0)&&(nor<=l)) ? max(1.0-tan,0.0) : 0.0 ;
alpha = max(al,alpha);
}
alpha = (compt>0) ? 1.0 : alpha;
gl_FragColor = vec4(color.r,color.g,color.b,alpha);
}