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.
|
/*
*This code is property of Vasily Tserekh
*if you like it you can visit my personal dev blog
*http://vasilydev.blogspot.com
*/
using System;
using System.Collections.Generic;
using System.Text;
using Tao.OpenGl;
namespace SistemaSolar
{
class Star
{
List<Position> stars = new List<Position>();
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)
{
stars.Add(p);
count++;
}
}
}
public void Draw()
{
Gl.glBegin(Gl.GL_POINTS);
Gl.glColor3f(1, 1, 1);//color of the stars
Gl.glPointSize(3);
foreach (var item in stars)
{
Gl.glVertex3f(item.x, item.y, item.z);
}
Gl.glEnd();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
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 www.vasilydev.blogspot.com. My real passion is 3D game programming and playing guitar. I've programmed stuff in C#, python, Delphi, PHP, C++, JS, QT and others...