Click here to Skip to main content
15,301,624 members
Articles / Multimedia / OpenGL
Posted 17 Apr 2013

Tagged as


63 bookmarked

3D Solar System with OpenGL and C#

Rate me:
Please Sign up or sign in to vote.
4.91/5 (55 votes)
17 Apr 2013CPOL6 min read
A demo of a solar system programmed in OpenGL and C#
In this article, you will see a simple 3D solar system implementation with OpenGL and C#. In the demo, you will learn how OpenGL rotation works and how to rotate a mesh around an arbitrary axis. You will also make use of the main OpenGL primitives: Point, Line and Triangles.

Image 1


This is a 3D solar system implementation with OpenGL and C#. I tried to keep it simple because this demo is only for educational purposes. It contains the sun, the planets, our moon, the planet’s orbit and some stars. It is programmed in Visual Studio 2008 and I have upgraded it to Visual Studio 2010 without any kind of issue. For this demo, I used the TAO namespace which is an interop between the OpenGL DLL and the .NET Framework. I also used Shadowengine, a small graphic framework developed by me to rid you of the tedium of hard coding the loading of textures, the initialization of the graphic context among others.

A Solar System Viewed form a 3D Programmer Point of View

Well, what does a solar system contain? Planets, Sun Satellites, the universe, the stars on the background, etc. As a 3D programmer, you should think about how you will translate those entities to a programming environment. For example, the universe is all black. With having a black background, you will solve that problem. OpenGL already has that function Gl.glClearColor(0, 0, 0, 1);//red green blue alpha which will set the background color to black. About the stars, they are just bright dots, and then you could make use of the OpenGL primitives that handle the drawing of points. You can make use of random functions to generate a lot of stars if you are too lazy to place them one by one, you just have to make sure they don’t fall inside the solar system. The planets are just spheres with textures; they also have orbit and a rotation on its own axis so you have to keep track of those using variables and update them because they change over the time. If you don’t want to make a sphere in 3D max, you may use OpenGL quadrics because it defines a set of basic trigonometric shapes and also defines texture coordinates for them. Satellites are the same as planets, the only difference is that the axis of their rotation is located on a planet not on the sun.

Using the Code

References in the project include those to ShadowEngine and TAO.OpenGL. I would like to point out that I don’t create a graphic context in a standalone window Like XNA, GLUT, etc. My graphic context is created in a common .NET WinForm. This is very convenient because you can draw 3D content in any window mixing it with 2D components. Later on, you will see that you can draw 3D content in almost any 2D component. The OpenGL initialization function only needs a valid component handler to start drawing 3D.

Here is the list of project classes:


This is a classic FPS (First Person Shooter) camera. The explanation of how a FPS works goes beyond the scope of this article. They work in the following way:

  • The mouse is centered on the middle of the screen.
  • When the user moves the mouse, a delta X and Delta Y are calculated from the beginning point.
  • Those Delta X and Delta Y are translated into angles and how the camera it’s rotated.
  • When you wish to move forward or backward, the camera will move in the direction in which the angles are pointing.
  • You may take a look at public void Update(int pressedButton) at the camera class to have a better understanding.


This class name is self explanatory, it is the main and only form of the project. It contains the call to the texture loading, the 3D context initialization, the drawing of the 3D content, among others. It also handles the user key and mouse input. Because the 3D content requires at least 30 frames per second to be drawn, I used a timer and placed all the drawing code inside it. One point of interest would be that I start a 3D context on a panel, so I can set the panel in any position I want inside the form. Here is the code of the 3D initialization on the project:

hdc = (uint)pnlViewPort.Handle;
string error = "";
OpenGLControl.OpenGLInit(ref hdc, pnlViewPort.Width, pnlViewPort.Height, ref error);

Here is the code to load the textures into OpenGL memory:


My small engine takes care to load all the textures located on that folder, the texture format accepted is TGA JPG and BMP. The textures may not be NPOT (Non Power Of Two) and still will load correctly.

Here is the code that draws all the scenes:

private void tmrPaint_Tick(object sender, EventArgs e)
    // clears OpenGL
    //updates the camera
    //draws the scene
    //swaps buffers
    //finish drawing operations


A planet contains the following variables:

  • Position
  • Texture
  • Orbit (current distance from the sun)
  • Current rotation angle
  • Current orbit rotation angle
  • Current orbit speed

I used OpenGL quadrics to draw the planets sphere. Quadrics are OpenGL predefined shapes to help in small drawing tasks. Quadrics come, for example, with texture coordinates, so I don't have to use a 3D editor like 3D Max to correctly apply texture to each planet. In each frame, the planet moves through its orbit according to its orbit speed. Also there is a bool variable called hasMoon to specify if you want to draw a moon for that planet. I have only our moon but if you like, for example, to draw mars moons Phobos and Deimos, you can use that code. Another interesting function that contains the planet class is the one used to draw its orbit. First, I generate the points with a sin function and then I connect them using GL_LINE_STRIP. Here is the code:

public void DrawOrbit()
    for (int i = 0; i < 361; i++)
        Gl.glVertex3f(p.x * (float)Math.Sin(i * Math.PI / 180), 
                      0, p.x * (float)Math.Cos(i * Math.PI / 180));

Note that the planets almost always have an elliptical orbit. This is a circular orbit. The two angle variables that hold the planets class are used to maintain the rotation of a planet around its axis and to maintain the rotation around the sun.


A satellite contains everything that a planet does. The only difference is that its rotation point is not the sun but the planet that contains it. So anytime it draws, it has to receive the position of its containing planet. You will note it on its draw function.


This is the class that contains the list of planets, stars and satellites. It only creates and draws them. The planets are saved into a list and when I call DrawScene() from the main form, it makes a foreach loop invoking the Draw method on the planets.


This is the class the draws the stars. The stars are single GL_POINTS which are generated in random positions. This is the function that generates them:

public void CreateStars(int amount)
    Random r = new Random();
    int count = 0;

    while (count != amount)
        Position p = default(Position);
        p.x = (r.Next(110)) * (float)Math.Pow(-1, r.Next());
        p.z = (r.Next(110)) * (float)Math.Pow(-1, r.Next());
        p.y = (r.Next(110)) * (float)Math.Pow(-1, r.Next());
        if (Math.Pow(Math.Pow(p.x, 2) + Math.Pow(p.y, 2) + Math.Pow(p.z, 2), 1 / 3f) > 15)

What this code does is to generate a random point and calculate its distance to the sun, and if the distance is less that a predefined value, discard the point. In this case, the predefined value is twice the radius of the solar system. This operation will be repeated until it reaches the desired amount of stars.


The sun class is the most simple. It’s like the planet class, only it has no orbit. It has only a rotation around its axis. The sun is drawn at the OpenGL 3D coordinates of (0,0,0).

Points of Interest

In this demo, you learn how OpenGL rotation works and how to rotate a mesh around an arbitrary axis. Also, you will make use of the main OpenGL primitives: Point, Line and Triangles. Well, these are all the classes involved in this project. I hope it is useful and that it will encourage developers to start programming in 3D. Feel free to play with the code and to ask any questions you want.


  • 17th April, 2013: First version of the demo


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Vasily Tserekh
Software Developer
United States United States
Born on 86, had my first computer at the age of 8, wrote my first code at the age of 15(pascal), began to study software engineering at 2005 and graduated in 2010. I have a dev blog My real passion is 3D game programming and playing guitar. I've programmed stuff in C#, python, Delphi, PHP, C++, JS, QT and others...

Comments and Discussions

QuestionColor of planets Pin
ganesh199618-Feb-18 11:32
Memberganesh199618-Feb-18 11:32 
QuestionGreat Project Pin
Syaiful Maulana Abbidin31-Dec-16 20:22
MemberSyaiful Maulana Abbidin31-Dec-16 20:22 
QuestionBut the earth is flat, not a sphere. Yeah, I know, ...but research it on youtube to awaken. Pin
Member 899937626-Nov-16 4:56
MemberMember 899937626-Nov-16 4:56 
GeneralMy vote of 5 Pin
Will Liou 10-Nov-16 23:02
MemberWill Liou 10-Nov-16 23:02 
PraiseMy Vote of 5 Pin
Member 1175433524-Oct-16 22:15
MemberMember 1175433524-Oct-16 22:15 
Questionasking about shadowengine.dll and tao.opengl (solar system program) Pin
Member 121749462-Dec-15 21:34
MemberMember 121749462-Dec-15 21:34 
AnswerRe: asking about shadowengine.dll and tao.opengl (solar system program) Pin
Vasily Tserekh3-Dec-15 6:26
MemberVasily Tserekh3-Dec-15 6:26 
GeneralRequsting for complete project Pin
Member 114302065-Feb-15 5:52
MemberMember 114302065-Feb-15 5:52 
GeneralRe: Requsting for complete project Pin
Vasily Tserekh5-Feb-15 6:49
MemberVasily Tserekh5-Feb-15 6:49 
QuestionMy vote of 5 Pin
charles92231-Dec-14 18:35
Membercharles92231-Dec-14 18:35 
AnswerRe: My vote of 5 Pin
Vasily Tserekh9-Jan-15 13:21
MemberVasily Tserekh9-Jan-15 13:21 
QuestionContentManager.cs not found Pin
Member 1077177515-Dec-14 3:52
MemberMember 1077177515-Dec-14 3:52 
AnswerRe: ContentManager.cs not found Pin
Vasily Tserekh9-Jan-15 13:24
MemberVasily Tserekh9-Jan-15 13:24 
Questionclose and reload the MainForm problem Pin
dakeen14-Sep-14 20:37
Memberdakeen14-Sep-14 20:37 
AnswerRe: close and reload the MainForm problem Pin
Vasily Tserekh15-Sep-14 4:41
MemberVasily Tserekh15-Sep-14 4:41 
GeneralNice! My vote of 5! Pin
Sharath C V12-Aug-14 20:41
professionalSharath C V12-Aug-14 20:41 
QuestionReally nice Pin
Maya Dang25-Dec-13 21:09
professionalMaya Dang25-Dec-13 21:09 
Questiondll hell Pin
loise8511-Jul-13 1:22
Memberloise8511-Jul-13 1:22 
AnswerRe: dll hell Pin
Vasily Tserekh11-Jul-13 3:04
MemberVasily Tserekh11-Jul-13 3:04 
GeneralRe: dll hell Pin
loise8511-Jul-13 8:29
Memberloise8511-Jul-13 8:29 
GeneralRe: dll hell Pin
loise8511-Jul-13 9:55
Memberloise8511-Jul-13 9:55 
GeneralRe: dll hell Pin
Vasily Tserekh11-Jul-13 10:28
MemberVasily Tserekh11-Jul-13 10:28 
GeneralRe: dll hell Pin
loise8511-Jul-13 10:53
Memberloise8511-Jul-13 10:53 
GeneralMy vote of 5 Pin
Sarah salem11-May-13 1:27
MemberSarah salem11-May-13 1:27 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA10-May-13 18:39
professionalȘtefan-Mihai MOGA10-May-13 18:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.