0
\$\begingroup\$

I am pretty new to Graphics Programming. I am currently using OpenGL. I have got some hands-on in last few days. I have knowledge of Texture Mapping, Cubemap, Multitexturing. But somehow I am not able to bridge the gap between my understanding and actual coding

What I am trying to do is create a demo of a stars coming out of screen. It should look realistic.

Now, my questions are:
1. Is Cubemap capable of doing this?
2. Do I need to write particle engine for this?(Really, don't want to at this stage)
3. This is kinda most needed for me. May I See some code..? In some tutorial / just code would also do.. but i think if its similar to my requirement then only it will Help

\$\endgroup\$
5
  • \$\begingroup\$ If you put two spaces at the end of each line a new line will be created Markdown help - Line breaks \$\endgroup\$
    – user25712
    Mar 4, 2013 at 13:13
  • \$\begingroup\$ Try to avoid putting "Thanks in Advance" at the end of your question, see this Should 'Hi', 'thanks,' taglines, and salutations be removed from posts? \$\endgroup\$
    – user25712
    Mar 4, 2013 at 13:26
  • \$\begingroup\$ Where to get started questions are best suited for a discussion oriented site, like those listed in the FAQ. \$\endgroup\$
    – House
    Mar 4, 2013 at 15:57
  • \$\begingroup\$ You really shouldn't put questions like this here but to give you a nudge in the right direction I would look at #2 first assuming you are trying for the 'flying through stars' look. I cant imagine that you would have to write a particle engine from scratch however as there are packages out there that will do this for you and you simply have to set up the parameters for your particles. \$\endgroup\$
    – RobCurr
    Mar 4, 2013 at 16:37
  • 1
    \$\begingroup\$ I'm not sure how stars coming out of the screen can look "realistic". If you want it to look like star trek or the famous screen saver, that's not realistic. While no one really knows what it would look like, you can imagine it would be pretty warped, just look what it looks like when you're getting close to the speed of light: youtube.com/watch?v=9lMYhhCkPXE \$\endgroup\$
    – House
    Mar 4, 2013 at 18:01

1 Answer 1

0
\$\begingroup\$

What you're asking for is sometimes called the starfield effect, named after the infamous screensaver. That should give you a nice starting point for searching.

A lot of tutorials recommend "faking" the effect in 2D, that is, start by putting a dot near the centre of the screen, then moving them away from the centre and towards the edge. Sometimes an acceleration is included to simulate the perceived motion of stars that are moving almost directly at the camera. This is indeed the "easiest" way, but it is not realistic, and I prefer another:

Use 3D particles

Represent each star as a 3D point, starting from some location in front of the camera, and moving in the general direction of the camera. That is, if the z-axis was forward/backward with respect to the camera, you would generate stars with an initial z coordinate, and random x/y coordinates, and animate the stars by subtracting a constant speed from the z coordinate.

Here's some code to get you started (illustrative purposes only, does not compile, you didn't specify the language, not using best practices):

class Star
{
    float x, y, z;
    void Move() { z -= STAR_SPEED; }
    void Draw();
    bool IsVisible();
};

main()
{
    list<Star> stars;
    for (int i = 0; i < MAX_FRAMES; i++)
    {
        if (i % MAKE_STAR_PERIOD == 0)
        {
            stars.append(Star(random(), random(), INITIAL_Z));
        }
        for (star : stars)
        {
            star.Draw();
            star.Move();
            if (!star.IsVisible())
            {
                stars.remove(star);
            }
        }
    }
}

The advantages of this approach are:

  • Realistic 3D motion (2D approximations suffer from weird motion for very-far-away or very-close stars)
  • Realistic distance modelling (finding the distance to the star is trivial, so you can modify the luminosity of stars based on distance etc)
  • As with all particle systems, you can choose any arbitrary drawing method for each star. Draw nebulae, galaxies!

Finally, to answer your specific questions:

  1. Is Cubemap capable of doing this?
    • I assume you mean using textured surfaces. Yes you can do this, but it won't look good because unless you have many layers of transparent textures, viewers will quickly discern the surfaces themselves, plus it's making the problem harder than it needs to be
  2. Do I need to write particle engine for this?(Really, don't want to at this stage)
    • I recommed the 3D particle approach. Why not? An "engine" is as hard as a string is long.
  3. This is kinda most needed for me. May I See some code..? In some tutorial / just code would also do.. but i think if its similar to my requirement then only it will Help
    • I gave you some code, you can search for more tutorials, really it's just points in 3D space flying in the same axis.
\$\endgroup\$
1
  • \$\begingroup\$ It would be good to mention that drawing the stars involve perspective projecting, which in the simplest form is screenX = screenSize / 2 + x * constant / z; and same for y. \$\endgroup\$
    – msell
    Mar 5, 2013 at 6:14

Not the answer you're looking for? Browse other questions tagged .