something similar to GL_POINT_SPRITE

Started by
6 comments, last by _WeirdCat_ 9 years, 10 months ago

Hi i am trying to draw a spiral galaxy without usage of point sprites, so far i was able to draw billboards, but in order to display galaxy properly i need to change their size in realtime, with glpointsprites i could just define where i want to put a sprite and set its maximum and minimum size (the whole sizing thing was automatic), like in this video.

Main problem is that i set point sprite size with pixels, and with billboards i use numbers.

So far i set a fixed size:

maybebeybe.jpg

But it only looks good from this distance.

Anyway, i am now trying to write a sizing function for these billboards, where minimum size is 1 pixel (less than 100.0f dist from a billboard) and maximum is 35 pixels. (more than 3500.0f dist from a billboard)

BUT the problem is how do i calculate size of a billboard to a coressponding size in pixels?

I tried to do some perspective calculations but i am far from getting it done, any help? tongue.png

and pls dont ask why i dont want to use point sprites, cuz i won't run my program on a different computer after every build, and mine (that i work on) does not support such thing. Oh and i cant use shaders aswell.

But maybe my GL_POINT_SPRITE implementation lacks of something since my cards supports them but i cant make these point textured.

Advertisement

Here is my slower, but good enough version to do this:


	vec4 projsize = matproj * vec4(in_data.xx, position.zw);
	gl_PointSize = max(2.0, in_data.x / 64.0 * sizemult.x / projsize.z);

sizemult is screensize in pixels

in_data.x is from the particle vertex structure, and is the size of the particle

so, to get the pointsize we transform (size, size, Z, W) with the projection matrix

the rest from there is really up to you

Translated to make any sense:


vec4 position = matview * vec4(in_vertex.xyz, 1.0);
vec4 projsize = matproj * vec4(size, size, position.zw);
gl_PointSize = size * screensize.x / projsize.x;

... which means that all of this can be done with fewer calculations

i'm just not any good at math to begin with

Also, to texture your particles just use GL_TEXTURE_2D_ARRAY. Simple and works.

i am very very pleased, however i dont quite understand this maybe i am a lil bit drunk (ok i am) but could you describe 'so, to get the pointsize we transform (size, size, Z, W) with the projection matrix' and this second code, it looks like a fragment shader, i am not aware of multiplying a matrix with 4d point.

matview (viewmatrix / inverse camera transformation matrix) and matproj (projection matrix) are both mat4x4's.

to transform a vector, the vector must be 4D (x, y, z, w) where w is the homogenous coordinate, usually 1.0. if it's 0.0, the transformed vector only has direction, no position

anyways,

all of my code (above) is only in the vertex program. gl_PointSize must be set in the vertex program.

furthermore, to give your stars or particles each own sprite, or tile, we can use a 2D texture array in the fragment shader like this:

vec4 color = texture2DArray(stars, vec3(gl_PointCoord.xy, tileID)).rgba;

where the tileID is part of the vertex structure of the particle, and is transferred from the vertex program to the shader program.

if our stars texture contains tiles which you can multiply with a color, we can use additional uniqueness data from the vertex shader to give each star its own color:

color.rgb *= star_color.rgb;

Cool effect: you can also emulate stars being obscured by entities in space by applying a hash function on the star-coordinates and the current time, output as a random number (0.0 - 1.0 not included) and use wave function to make your stars blink

Hope it helps

also, the reason your galaxy doesn't look as good from afar could be because you aren't emulating gas clouds (including the missing max(minsize, ...size...))

I have few questions:

First of all will vector * matrix will look similar to this? : MatVecMult01.gif

where right [] is my vector ;]

second thing is about your code:

vec4 position = matview * vec4(in_vertex.xyz, 1.0);
vec4 projsize = matproj * vec4(size, size, position.zw); <- whats this size?
gl_PointSize = size * screensize.x / projsize.x;

maybe i didnt precise my needs: i have a billboard and i must make it to be 32 pixels big when i am 3500 units away from it. ;]

I believe i found a solution, had to use pencil and ruler ;]

take a look at this there will be an explanation under it:

sol.jpg

So we have a viewport, we know how far a particle is from our camera.

we also know FOV, and we know what screen_width we have (the opengl window width in pixels ;])

so now ze math: we calculate C from distance_particle_camera / (cosine of FOV/2)

then we calculate a from C*C - distance_particle_camera*distance_particle_camera

now we know that 2*a is related to our screen_width (that opengl window - in pixels)

as we know what size in pixels should particle be (depending on the distance from camera) we can calculate:

x = (dest_particle_size_in_pixels / screen_width_in_pixels) and then

2*a*x is our numeric particle width, tommorow i will check it ;f

I'll try it, hold on for edit

EDIT: I couldn't make it work

This is what I did:


	float size = in_data.x / screensize.x;
	gl_PointSize = max(2.0, 2.0 * size * nearPlaneHalfSize.x);

Unless I misunderstood you, it didn't work.

I did find another solution that worked almost as good as my first response.

I tested it, and the particles disappear a little too early, but it's not a huge deal for me. I think maybe I'll keep it:


	float angle = asin(size / length(view_position));
	gl_PointSize = max(2.0, angle / (61.0 / 3.14159) * screensize.y);

I hardcoded the FOV in radians (61 / PI)

Anyways, after some testing, my first response has the one that works best, so I'm using that.

yep it works, however i have another question:

det.jpg

On what distance particles go to their max size?

the code looks like this:


 float sizeFromDistance(float dist,float maxsize)
 {

  if (dist >= 8000.0f) return maxsize;

 return      (dist / 8000.0f) * maxsize;

//  return 1.0f;
 }



elsefunc()
{
sizecoeff = cos(45.0f*imopi);  where 45.0f is FOV /2.0f and const float imopi           = 0.017453292519943295769236907684886;   //PI / 180

float dst = n3ddistance(particlepos, campos); // just a distance between 2 points
	float C = 	dst / sizecoeff;
	float x = sizeFromDistance(dst,35.0f) / 1024.0f; //where 1024 is my screen width in pixels
	float a = C*C - dst*dst;
	a = sqrt(a);
	float pointsize = 2.0f*a*x;


}

This topic is closed to new replies.

Advertisement