View Full Version : Stars

11-16-2000, 08:51 AM
I am trying to create a space scene (planets, stars... etc), everything is great except the stars, I can't seem to get the right visuals (like the ones you see in space simulators). I tried a texture mapped sphere, looked horrible. tried a list (or array) of small bitmapped squares, No luck, can't even see them. I'm new to openGL so this alone might be the problem.

Does anyone know of a good tutorial on the subject or maybe some source code I can learn from. Any help is appreciated, thanks

Michael Steinberg
11-16-2000, 08:56 AM
Probably I shouldn't post an answer, because I don't know resources. But, if you search the web for "skybox" or something similar, you'll most probably get a tutorial for that technique.
A Skybox is simply a box, textured with 6 textures, that are somehow wrapped to make the impression of a real 360 degrees panorama.

11-16-2000, 10:40 AM
I've had the same problem. I tried doing a "skydome" thing with a low-resolution gluSphere but mine looked like crap too. Even when I used REALLY high-res texutres (1024x1024) and each star was just one to four pixels in size. So I know what you mean. I guess you'd have to do a "skybox" thing and then tile your textures on each side of the cube.

11-16-2000, 12:40 PM
maybe use inward-facing, blended, textured quads? you could make them neat shapes, that way. and making stars bigger than one another would be easy, too. you could use an oop approach as well, and then dump all the stars into a display list and forget about them.

what doesn't go right when using the texture map? too blurry or what?

11-16-2000, 12:42 PM
What about using billboarded squares with a texturemap of a star on it?


11-16-2000, 03:03 PM
The textured skybox/skydome is too blurry and I don't wanna use bitmaps that are too large and tiling looks fake (unless someone has a cool idea). It seemed to me that the OOP approach was the best but the problem is for some reason I cannot see any stars (remember I am novice when it comes to openGL) when I try that. the other problem is figuring out which way their Normals should point (actually I got it!! normalize then inverse the world coord of the star). but then I would have to scale them in such a way that if the camera gets gets closer they won't look bigger (guess I would solve that if the camera is the centre of the "stardome"... Too much work for something that should be simple.. Gonna try a few more things..

Does anyone know what approach is used in games like Starlancer or Wing Commander..

11-16-2000, 03:34 PM
I'd say they use GL_POINTS in a star sphere for 'normal' stars, then use quads for bigger stars.

11-16-2000, 05:03 PM
At the risk of sounding stupid.. (new in openGL) how would you use GL_POINTS? an example would be great.. Thanks :P

Oh yeah and is there an opengl.hlp file anywhere?

[This message has been edited by monson (edited 11-16-2000).]

11-16-2000, 05:10 PM
The problem, at least with mine, isn't a problem with using large stars and smaller stars... I think the OOP approach would be unneccissarily slow since, if you wanted a reasonably dense starfield, you'd have to use WAY too many quads or billboards (how do you use billboards in OpenGL anyway?) so I think the most reasonable idea is the skybox or skydome. Only problem with that is that you have to use too large of a texture or tile the stars which looks fake. I've implemented a skydome (inward facing gluSphere) using a REALLY large texture (1024x1024) and it still looks hokey. The stars still look way too big and too blurry and it's just plain wierd looking. I can't describe it. I'm wanting a very basic starfield effect as seen in just about any 3D space combat game. I'm not even asking for big nebulas (although that would be cool) or anything like that.

11-16-2000, 05:50 PM
glBegin ( GL_POINTS );
glVertex3f ( 0.0, 0.0, -2.0 );
glEnd ( );Simple as that! You can draw and lots of points very quickly as long as texturing and lighting is turned off. Mix a few random GL_QUADs with nice star textures (don't make them huge, small ones will do), maybe add a Homeworld style untextured nebula in the background - very nice!

11-16-2000, 05:54 PM
Originally posted by Pauly:

glBegin ( GL_POINTS );
glVertex3f ( 0.0, 0.0, -2.0 );
glEnd ( );Simple as that! You can draw lots and lots of points very quickly as long as texturing and lighting is turned off. Mix a few random GL_QUADs with nice star textures (don't make them huge, small ones will do), maybe add a Homeworld style untextured nebula in the background - very nice!

11-16-2000, 05:58 PM
Umm, now that really wasn't supposed to happen http://www.opengl.org/discussion_boards/ubb/rolleyes.gif

11-16-2000, 07:00 PM
Thanks Pauly! (sorry about the stupid question), So I tried it with 100 stars (only GL_POINTS, no Quads), my FPS went down from 60 to 50-55, even lower at times (30FPS). I looks good but the performance hit is high especially for only 100 points. Still it's better than nothing for now. Thanks a lot, appreciate it http://www.opengl.org/discussion_boards/ubb/biggrin.gif

11-16-2000, 07:42 PM
What 3d card are you using?

Remember to make sure to call glPointSize ( 1.0 ); and have point smoothing off for optimal performance.

Try 1000 stars and see if it slows down anymore...

11-17-2000, 05:48 AM
How's the best way to plot those points "around" your view? Basically, how's the best way to quickly place them in a "sphere" around the camera?

11-17-2000, 07:14 AM
I have a RIVA TNT2 16MB, I think the GL_SMOOTH is on what the points are drawing.I thinks I still have to clean and optimize the code some more. I'm at work now, but I'll try what you suggest and let you know.
Punchey, the stars don't have to be in a sphere, as long as they are always relative to where the camera is. This is sort of what I am using now, I still have to clean my code and maybe use linked lists. Let me know if this helps, maybe I'll arrange to put an exe file on a site when I'm done http://www.opengl.org/discussion_boards/ubb/smile.gif It would be cool each other's work.

void InitStars()
for (int i=0; i<100; i++)
{ // this results in a number between -7 and 7
star[i].x = ((float)(rand()%14000)/1000)-7;
star[i].y = ((float)(rand()%14000)/1000)-7;
star[i].z = ((float)(rand()%14000)/1000)-7;
void DrawStars()
DisableAll(); //Disables texture, lights, (gonna add smooth ... Everything, as pauly suggests)
glColor3f(1,1,1); //set star color to white
for (int i=0; i<100; i++)
glVertex3f(star[i].x,star[i].y, star[i].z);
EnableAll; //Enables everything

Rotate Cameraview
TRanslate to Planet coordinates

11-17-2000, 08:58 AM
Thanks for the code! I havn't tried your code yet but wouldn't it sometimes place a star between you and the planet since the coordinate generated could be something between -7 and 7? Since the viewpoint sits at 0,0,0 isn't it possible the it could place a star at, say, 0,0,1? And if the edge of a planet was at 0,0,2 then you'd see the star in front of the planet?

11-17-2000, 09:23 AM
The best way to do it is use some trig. to randomly generate your stars (or read them from a file if you're feeling fancy and want real star data) in big sphere. Compile this into a display list.

Whereever you move you viewpoint, move the stars so you never catch up with them.

11-17-2000, 10:11 AM
Actually I admit it is a poor algo for generating stars, but at the time I just wanted to see how they would visually look. another way is to do this.

radius=radius of the stardome (for example, 50)
alpha = random number between 0 and 360 degrees
beta = another random number 0-360 deg; //elevation angle

This uses the point on sphere representation using 2 angles and a radius (as I remember it, I'll have to check that one)

11-19-2000, 05:21 PM
For some reason, I can't get any points to display. I'm using the same code (fixed up a bit) as was posted in this thread. I'm disableing texturing and lighting and then doing glBegin(GL_POINTS)and then glVertex3f(...) but to no avail! I've had this problem before and I never resolved it then either... So I've basically never gotten points to work for me. Anyone know why?

11-19-2000, 05:47 PM
did you make sure you set the color to white (or whatever). use glColor3f(1,1,1); just before glBegin(GL_POINTS), If you can post the code for your "drawStars" function..
Oh Yeah, and make sure that the stars are not outside the view frustrum, start with a stardome radius of 10 or something.. Hope this helps http://www.opengl.org/discussion_boards/ubb/smile.gif

[This message has been edited by monson (edited 11-19-2000).]

11-20-2000, 05:48 AM
Thanks! I got it working... After I got it working, all the stars look so bright and all looked the same so I figured I'd store a color with each star as well. So I did something like this:
float color = 1/((rand()%3) + 1); // gets either a 1 or .5
stars[i].color[0] = color; // indexes of color are r(0) g(1) and b(2)
stars[i].color[1] = color;
stars[i].color[2] = color;

then I render the stars like:
for(i=0; i<NUM_STARS; i++)

But then all my stars dissapear! If I just put a glColor3f(1,1,1) at the beginning, it works. But if I try and use the generated values, nothing shows up. I can't see why my generated values would come out at anything other than 1 or .5 and even if it sometimes came out at 0 (which it won't) there should still be a few visible stars wouldn't you think? Maybe I just can't see the forrest for the trees.

Michael Steinberg
11-20-2000, 06:11 AM
That is, because the 1 in the 1/(something) string indicates the compiler to create an integer calculation, wich always ends in 0.0f. Maybe use 1.0f instead like:

float color = 1.0f/((rand()%3) + 1);

Uhmm I think that is the bug, but I don't know the rand() syntax for sure anymore, since I don't use it very frequently.

11-20-2000, 06:27 AM
Thanks! Duh! I should have caught that one. I'll try it now and see if that was it. I'll bet you're right though.

11-20-2000, 06:32 PM
DA*N!!!! for some reason I cannot get the GL_QUADS to always face the camera (no matter from which angle I am looking) it is always at an angle!! I am trying to use textured quads for the sun and nebulas and stuff... Anyone got any ideas? I know it should be easy, especially since the camera is always at the centre of the star-dome.

Also how can I get the projected 2D screen coordinates of the centre of the quad, in openGL?

[This message has been edited by monson (edited 11-20-2000).]

[This message has been edited by monson (edited 11-20-2000).]

The Wolf
11-20-2000, 09:03 PM
I would be interested to know too

11-21-2000, 07:40 AM
Try searching this message board with the keyword "billboard". I rann across some billboard stuff on this message board last night but was looking for something else so I didn't pay attention. You should be able to find the info. On another note, when I implemented your spherical placement of stars algo, I noticed that it actually places the stars in a virtical cylinder. So what I wound up doing was generating random vecors and then normalizing the vectors and multiplying them by the radius. That seems to have worked well with one exception: there is increased star density where the axes' + and - quadrants converge. It's hard to explain. But I seriously doubt anyone would ever notice this fact unless you used an EXREMELY dense starfield. Let me know if you find that billboard info and I'd REALLY like to know if and when you find that screen coordinate info. Thanks for all your help!

11-21-2000, 11:01 AM
Now that I think of it, you should be able to precalculate (better performance than render-time calculation for a billboard) the coordinates for a quad that points towards the center of your starsphere. You could, for example, use the method I mentioned for star placement only instead of placing stars, you're placing coordinates for a quad. Generate four vectors that point out from the center of your starsphere in a rectangular fashion and then simply multiply those coords by the radius of your starsphere and BAM! It should place the quad out along the circumference of your starsphere pointing directly at the center of it (where you will always be). If having the quad too close to other stars is a problem, try adjusting the radius for your quads so they are slightly offset from the stars. Save all of these coords and stars from your initilization, then you're ready to rock and there's no "run-time" calculation required like for billboarding. (I know, the init phase is technically run-time also, but it's done at the start and at a non-time-critical point of the app - not while you're trying to render so it won't effect your framerate... only your load time.) I'm sure it wouldn't be all that hard to write an algo for generating a "rectangular" set of vectors. I'd write one here and now (and probably will when I get home) but it depends on the radius you're using and consequently, the resolution about the center you desire.

11-21-2000, 05:01 PM
Actually I was working on that one and it was not working, turned out to be a simple syntax mistake (coding after work has its consequences). But yeah, you arrived at the same idea (great minds think alike), and guess what, IT WORKS!!!, what I did though I just calculated 1 point (center of quad) then drew the quad around it, it works for quads that are on the z-axis, but I'll need to implement your idea for any quad.
About the algo for drawing stars, yes actually that algo seems to draw them in a cylindrical fashion, but if you use 3 different angles it solves the problem (you tend to see a bit more stars at the 0 angles, like you mentioned). I like the algo you presented, i'm thinking of using a variation of that, or simply load a precalculated starmap (maybe an true-life accurate one, it I find one). As for finding the coord of a projected object on screen, no luck yet, lemme know if you find something (I don't know why openGL is so unfriendly when it comes to that, DX seem to handle this aspect better). Thanks again http://www.opengl.org/discussion_boards/ubb/smile.gif

11-21-2000, 06:51 PM
Sounds great! Hey, e-mail me at aerotechts@spamsucks.usa.net
(remove the "spamsucks." from the address. I do this so I won't get spammed by messageboard crawlers)
I'd like to talk with you more on this stuff 'cause it sounds like we're doing some of the same stuff and, like you said, good minds think alike so it wouldn't hurt for us to put those minds together for some of this stuff. This way, we won't clog up the message board (29 posts on this thread so far!). Later!

11-22-2000, 12:49 PM
No.. keep spamming. I'm learning here, too! http://www.opengl.org/discussion_boards/ubb/smile.gif

11-22-2000, 02:47 PM

This will cause problems, i've tried something very similar once before and i ended up with some rather large holes at two ends. Tell me if you figure it out though.


11-22-2000, 06:35 PM
I haven't exactly been keeping up with this topic and am also new to OpenGL but inorder to plot the stars randomly, have any of you guys tried using the "GL_POINTS" but with plotting a fractal type function? Maybe that might help to generate the stars a little better...I could however be very wrong but it's just a thought! http://www.opengl.org/discussion_boards/ubb/smile.gif Take Care...and Merry Christmas if you celebrate it or Happy New Year! http://www.opengl.org/discussion_boards/ubb/smile.gif

11-22-2000, 09:17 PM
Hey wire, actually if you use 3 different angles it solves the problem (well almost, you'll find a few more stars at the 0 angles but it still looks good), ie,:
z=(float)radius*sin(theta); //diff angle
or you can also go with Punchey's suggestion which is great too (above), actually I'll probably switch to that idea too.
I suggest you guys read the whole post if you are doing this kind of stuff (space simulation), some very good ideas are presented. feel free to ask if you have more questions http://www.opengl.org/discussion_boards/ubb/smile.gif
yoda, your fractal function idea is interesting. Oh yeah, and Merry Xmas and Happy New year http://www.opengl.org/discussion_boards/ubb/wink.gif

11-25-2000, 08:48 PM
the simple way to generate a regular distribution of random sphere is to generate them into a cubic space, then discard them if they're outside the enclosed sphere.
then you normalize, and you're done, like this:

#define SQUARED_LEN_3(v) (v.x*v.x+v.y*v.y*v.z*v.z)

Tdmyvert3f p;
float dd;

do {

// this will generate a point into a 2x2x2 cube, centered at 0,0,0
p.x= rand()*2.0/RAND_MAX-1.0;
p.y= rand()*2.0/RAND_MAX-1.0;
p.z= rand()*2.0/RAND_MAX-1.0;

dd= SQUARED_LEN_3(p)

} while( dd>1.0 );

// normalize
dd= 1.0/sqrt(dd);

p.x*= dd;
p.y*= dd;
p.z*= dd;

now we have a point on the surface of the unit sphere.

say we generate 20000 points, maybe in three different chunks: one for the faintest stars, one for the normal and the other for the brightest, to say.

you can render them by the use of glVertex.

i found that a fast way (i think the fastest, since this way you can see if a star is visible with only 3 mults and 2 adds, and in case it is not visible, you cull it away before sending it to the pipeline) is to do this:

float mv[16];


for(<all stars into db> ) {
Tdmyvert3f v;
v.z= rotate_solve_for_z // dot product of star location and 3rd column of mv matrix (3 mults, 2 adds)
if(v.z>=THRESHOLD) continue; // gl have negative z running into the screen
v.x= rotate_solve_for_x
v.y= rotate_solve_for_y
glVertex((float *)&amp;v);

THRESHOLD is a value wich depends on your current fov and aspect ratio.
but don't bother about the math, set it to -0.7 and you should be quite good.


11-25-2000, 09:29 PM
oh, i tried also another way, similar to the skybox approach.

let's talk about heavy gear 2: there are some missions on the asteroids, where you can clearly see stars surrounding the scene.

i liked very much that star glittering when you smoothly change the view: a common aliasing artifact, but wich in this case looks good.

so i tried to recreate that.
the fact is that if i enclosed the scene into a skybox, then i would have to build a texture wich resolution is very high, compared to the projected size of a pixel (to produce aliasing)

so i tried to tile it: bad!
the skybox is a box, not a sphere, so near the corners of the box can be seen dense clusters of stars (with aliasing, ok) but near the center... no aliasing at all.

i would need a way to keep aliasing mostly constant over the surface of the quads.

so, i splitted every face of the box into more quads (actually 6, if i remember) and assigned directly texture coords to the new verts created.

the texture coords function must map the 3D vertex position to a 2D location on the surface of a sphere (approx works good the same)

i implemented it in a small demo, available here:

earth demo (ftp://demos:demos@

sorry, but the demo is not compilable, unless i share all the developement system... quite big.

however, the skybox code is all there.

email me if it doesn't work!


[This message has been edited by dmy (edited 11-26-2000).]

Michael Steinberg
11-26-2000, 01:31 AM
Just in case: I got that aliasing effect already in a 512*512 texture, where the stars still are not only one pixel great, at a resolution of 640x480. Uhmmm, so if you make the stars a bit smaller 4*4 or even 2*2 texels, it should work for higher resolutions as well.

11-26-2000, 03:59 AM
yep, but you still have aliasing incoherency between the center and the corners of the quads... the stars don't glitter the same in any direction, is what i mean, and the nature of the skybox is revealed, wich is bad for the observer imo.

also, a grayscale 8-bit 512x512 texture is 256k, and depending how you want to make the background appear, you'll need 2 to 6 of them.

instead, by using a single tile (seamless is best) of say, 256x256 pixels, you obtain a fairly good background for the scene, with only 64k of texture space.
also you don't have to switch textures.

then, you could add some other bits of graphics to increase the quality, but still you have that nice glittering.

but as always, it's a matter of taste http://www.opengl.org/discussion_boards/ubb/smile.gif


11-27-2000, 09:20 AM
That's a good idea you've got, dmy. I'll have to try it out. BTW, I know this is a stupid question that I should already know, but using C++, I'm trying to seed the rand() function so that I get the same starfield every time. I don't use rand() often and I've forgotten how to seed it properly. I've done what my documentation says to do and used srand() but when I use it, all my points are placed in the same place. This would make sense if my seed value was set each time before my rand() call, but srand() is only called once and then I call rand() several times after that. Anyone know why I'd be getting the same results every time rand() is called? Anyway, I also like the fractal idea. You could easily combine the box to sphere method with the fractal method by replacing the rand() calls with afractal algo.

[This message has been edited by Punchey (edited 11-27-2000).]

11-27-2000, 10:17 AM
Pass a time value to the srand() so that you seed the random number generator differently each time based on the current time. There are a couple of ways you could do this..

srand(time(NULL)); // Must #include <time.h>

Or in Windows..

srand(timeGetTime()); // Must #include <mmsystem.h>

11-27-2000, 11:19 AM
Actually, that's the exact opposite of what I want and doesn't solve the problem I'm having anyway. I want the same random sequence every time - not a different one. Here's what I'm doing:

srand(58); //just a number, it doesn't matter what
for(i=0; i<NUM_STARS; i++)
z = rand();

Now each rand() call should generate a different number (basically). But it doesn't. What I wind up with is what looks to be a single star because it generates the exact same number each and every time it's called in the loop and therefore it places each and every star in the exact same place. I could understand this behaviour if I was calling srand(58) within the loop, but this is not the case.

11-27-2000, 11:25 AM
Well, dmy for your 1st post I can see the other possible applications for such an algo, but for a stars algo the distance from the camera to the stars is always the same so you don't need to cull if you make sure your algo generate stars that are only within certain distance . You're second post definitely got me interested since placing a skybox would probably give better performance. I'll check out your demo when I get home. Thank http://www.opengl.org/discussion_boards/ubb/smile.gif

11-27-2000, 11:44 AM
Yes, I'll also have to check out your demo since my skybox attempt totally sucked! It looked like crap!

11-27-2000, 12:01 PM
DMY I downloaded your demo but cannot run the .exe. It requires IJL 1.1 and I have IJL 1.5. I went to Intel's website to find the 1.1 version of the DLL but it is not available. I will attempt to compile it with IJL 1.5 tonight if I get the time.

Thanks for the effort you put into this.

Paul Leopard

11-27-2000, 01:35 PM
Originally posted by Punchey:
Actually, that's the exact opposite of what I want and doesn't solve the problem I'm having anyway. I want the same random sequence every time - not a different one.

Oops.. guess I didn't read your post close enough. http://www.opengl.org/discussion_boards/ubb/tongue.gif

That is strange that it's giving you the same number every time. Not sure what to tell you about that except that your code looks right.

Are you just using the value returned by rand() directly the way your code shows it? I typically use it mod the range of numbers I want to use.
(i.e. for a random number between 1 and 10, (rand() % 10) + 1) http://www.opengl.org/discussion_boards/ubb/wink.gif.

How do you know it's giving the same value every time? Do you print out what it returns? Do you use it in any calculations first? For instance if you use it in a division and you are expecting a float value to be returned but you don't properly cast your values so that it does an integer division instead, you could always end up with 0.

You probably know all that already, but I figured I'd throw out some possible ideas to look at.

[This message has been edited by Deiussum (edited 11-27-2000).]

11-27-2000, 02:50 PM
Actually, I didn't type my code exactly the way I have it, I am, in fact, using % just like you mentioned. The way I can tell is I stepped through the code at execution and put watches on some test variables. And for some reason, it assigned the same value every single time. Very weird. Thanks for your suggestions.

11-28-2000, 08:19 AM
monson, i didn't understood your point when you talk about my 1st post.
could you explain better?

you're right about ijl 1.1, i forgot that.
i'll include the dll ASAP.


11-28-2000, 11:43 AM
dmy, your idea for the star-placement algo works beautifully! No strange concentrations in certain regions! Just good ol' random distribution of points. Thanks!

11-28-2000, 04:05 PM
dmy, actually upon reading your post again i see that you have already done what I proposed, and bonus, the algo can also be used for other applications, I remember once using a similar algo (not in openGL) to create a space minefield. http://www.opengl.org/discussion_boards/ubb/smile.gif Way to go http://www.opengl.org/discussion_boards/ubb/smile.gif . Do you have some good lens flare ideas by any chance, I am having a problem billboarding my lens flare textures?

[This message has been edited by monson (edited 11-28-2000).]

11-29-2000, 05:25 AM
I don't know much about OpenGL programming. I am very good at C, but have just recently moved on from the world of engineering programming to the pretty world of graphics. If you are an engineer or scientist you know what I'm talking about:) Anyway, over on flipcode.com they have a skybox tutorial if anyone is trying to figure out the skybox thing. I'm not sure how useful it will be to all of you, but it is worth a look at. Actually the entire site is worth a look at. Right now I'm trying to learn how to make a portal engine from the tutorials on their site. Great site!!! Lots of luck!

Michael Steinberg
11-29-2000, 07:22 AM
Couldn't one switch off Perspective correction to make it glittering?
Oh, I currently only have 1 skybox (background) texture, that displays start. The other textures are smaller, since they only show nebulars. (?)

11-29-2000, 08:02 AM
monson, i can include the lensflare class into the earth demo,
so you can see how i do it, either lensflare and billboarding.

i don't know if this is the best way... so maybe you can look around and test other approaches.
basically i make it work this way:

a billboard, you know it, is a image wich is rendered always facing to the viewer.

so, what is a easy way to say opengl to render, say, a quad in such a way?

by setting the modelview matrix to identity and sending to gl the quad's verts with equal z component.

other issues like "how to use a texture" is not important here:
in fact, if you're starting experimenting billboards, i suggest to turn on wireframe rendering with a call to
glPolygonMode... and disabling textures, just to see if geometry is in the right place.

the easiest way to handle billboards is imo to build a routine wich, given the billboard location in eye space,
the one wich features the camera at 0,0,0 and looking toward negative z axis,
draws your quad using the location you pass as the center point.

something like this:

void draw_quad( Tdmyvert3f loc, float size ) {

size *=0.5;

Tdmyvert3f v;
Tdmytex2f t;

v.z =loc.z; // keep depth constant
v.x =loc.x-size; // lower left corner of quad
v.y =loc.y-size;
t.s =0; // lower left corner of texture
t.t =0;

glTexCoord2fv((float *)&amp;t);
glVertex3fv((float *)&amp;v);

v.x =loc.x+size;
t.s =1
glTexCoord2fv((float *)&amp;t);
glVertex3fv((float *)&amp;v);

v.y =loc.y+size;
t.t =1
glTexCoord2fv((float *)&amp;t);
glVertex3fv((float *)&amp;v);

v.x =loc.x-size;
t.s =0
glTexCoord2fv((float *)&amp;t);
glVertex3fv((float *)&amp;v);

to finally render, you can do this:



for(<all lens flare elements> ) {
draw_quad( <flare location in eye space>, <flare size> );


i draw billboards for lens flare, particles and the likes this way.
in the code you'll download is a bit more complex, since i need more control, but that's the idea.

to render this fx you have to think 2D:
-take the flare generator location in world space.
-transform it to eye space and project it to a plane.
-render a billboard across the line made by the center of the viewport and the flare generator.

you have a point in world-space, how you can transform it to eye-space?
-obtain the gl modelview matrix with a call to glGetFloatv...
-multiply your point with this matrix.
problem solved.

how to project to 2D a 3D point?
-apply this formula:
<new.x> = <plane depth> * [list=1] / [list=1]
<new.y> = <plane depth> * [list=1] / [list=1]
<new.z> = <plane depth>
this is a simple planar projection. i suggest to use a plane depth of -100 or so.
why -100? because gl have the eye negative z axis running into the viewport.
why with this projection to 2D, you still have a 3D component, z?
because you are projecting to a plane, and x & y is about that plane yes,
but how can you identify a plane of constant depth in 3D if you don't specify its z?

how to render across the line?
-apply this:
<new.x> = [list=1] * <offset>
<new.y> = [list=1] * <offset>

when offset = 0, you draw at the viewport center.
when offset = 1, you draw at the flare generator.
when offset = -1, you draw at the opposite point of the generator, in respect to the viewport center.

that's all.
as always an example is better than a thousand words (spoken or written) so take a look at the code.


[This message has been edited by dmy (edited 11-29-2000).]

11-29-2000, 07:40 PM
Thanks dmy!! I had a similar algo going but had forgotten to place my lens flares in world coordinates (I was placing them in Screen coordinates, so they were outside the view frustrum), your post and the demo helped me realize that, Thanks very much. BTW, great looking demo http://www.opengl.org/discussion_boards/ubb/smile.gif

11-29-2000, 07:54 PM