PDA

View Full Version : Point sprites get smaller when really close



Cloov
08-24-2011, 06:47 AM
Hi all,

I'm having a problem with point sprites.

I'm working in GLES 2.0 on iOS. I can't seem to arrive at a good equation for scaling point primitives.

I've used an approach that most sites I've seen recommend, but what I don't quite understand is that my sprites get larger and larger as my camera approaches them, but then they get a bit smaller again before my camera passes by them.

'position' is my attribute for the point sprite's XYZ coords

vec4 newPosition = projection * modelview * position;
gl_Position = newPosition;

float distX = sqrt((newPosition.x-position.x)*(newPosition.x-position.x));
float distY = sqrt((newPosition.y-position.y)*(newPosition.y-position.y));
float distZ = sqrt((newPosition.z-position.z)*(newPosition.z-position.z));
totalDistance = distX + distY + distZ;

It may help to note that the totalDistance value works fine for distance fog, when I pass it through my fragment to shader, but I did think it was a little strange as it's not using the coords of the camera and the initial written coords of the vertex, it's using the calculated coords of the vertex and the initial written coords of the vertex.


Now onto gl_PointSize. The algorithm I used was from here: http://www.two-kings.de/tutorials/dxgraphics/dxgraphics17.html

But I thought I'd get this working, albeit not quite perfectly, without as much as a quadratic attenuation. So I've tried quadratic and linear attentuation too, but I always seem to get this issue with the sprites appearing smaller again when I get as close as I possibly can. It almost feels like the point sprites I'm seeing as smaller are actually behind the camera, but I can't work out why.

Below, I use 320 because that's my screen height at the moment.

float customPointSize = 0.5;
gl_PointSize = 320.0 * customPointSize * ( 1.0 / (2.0*distFromEye) );

I've also tried other equations, like:

gl_PointSize = 200.0 * (customPointSize / totalDistance);


Can anyone help with this?

Thanks

zeoverlord
08-24-2011, 02:06 PM
What is your z-near value? if it's negative then that could cause that effect.

also



vec4 newPosition = projection * modelview * position;
gl_Position = newPosition;

float distX = sqrt((newPosition.x-position.x)*(newPosition.x-position.x));
float distY = sqrt((newPosition.y-position.y)*(newPosition.y-position.y));
float distZ = sqrt((newPosition.z-position.z)*(newPosition.z-position.z));
totalDistance = distX + distY + distZ;

It may help to note that the totalDistance value works fine for distance fog, when I pass it through my fragment to shader, but I did think it was a little strange as it's not using the coords of the camera and the initial written coords of the vertex, it's using the calculated coords of the vertex and the initial written coords of the vertex.

Your senses serve you well, it is strange, in fact for all i know this could be the cause of the problem.
Subtracting camera space coordinates with untransformed model space coordinates and then calculating the length in what is a truly unorthodox way can only have unforeseen effects (including a small increase in distance given really close up). all this when you actually have the value your looking for right there in the first line, newPosition.z will do pretty much the same thing as it's the distance from the camera to the vertex in question

Cloov
08-25-2011, 02:20 AM
Thanks for this! Your description of what might happen when subtracting these values is exactly what I'm seeing with my particle emitter.

I did try and rewrite the totalDistance calculation, using the camera model space coordinates and the current vertex's model space coordinates, but it didn't seem to work. Does that sound right to you? It is, after all, how I'd expect to calculate the distance outside of the shader program.