using OhioState.Graphics; using Tao.OpenGl; namespace OhioState.Graphics.OpenGL { /// /// A simple sphere approximation that knows how to render itself using OpenGL /// public class SphereGL : IDrawable { #region Constructors /// /// Default constructor. /// public SphereGL() : this(1.0f) { } /// /// Constructor. /// /// The radius of the sphere in world-space public SphereGL(float radius) : this(radius, 300, false) { } /// /// Constructor. /// /// The radius of the sphere in world-space public SphereGL(float radius, int numberOfLongitudeSlices, bool wireframe) { this.radius = radius; this.NumberOfLongitudeSlices = numberOfLongitudeSlices; this.Wireframe = wireframe; } #endregion #region Properties /// /// Get or set the number of subdivision along the equator. /// public int NumberOfLongitudeSlices { get; set; } /// /// Get or set whether the sphere should be displayed as a wireframe model. /// public bool Wireframe { get; set; } #endregion #region IDrawable /// /// Render the object to the specified render panel. /// /// The render panel that object /// should be rendered to. public void Render(IRenderPanel panel) { material.MakeActive(panel); RenderSphere(); material.Deactivate(panel); } #endregion #region Public Properties /// /// Get or set object's material. /// public IMaterial Material { get { return material; } set { material = value; } } /// /// Get the bounding volume. /// public IBoundingVolume BoundingVolume { get { return null; } } #endregion #region Implementation private void RenderSphere() { if (!created) { // Create a new display list. created = true; displayList = Gl.glGenLists(1); Gl.glNewList(displayList, Gl.GL_COMPILE); // use the glu quadric functions to create the sphere. Glu.GLUquadric q; q = Glu.gluNewQuadric(); if( this.Wireframe ) Glu.gluQuadricDrawStyle(q, Glu.GLU_LINE); else Glu.gluQuadricDrawStyle(q, Glu.GLU_FILL); Glu.gluQuadricNormals(q, Glu.GLU_SMOOTH); Glu.gluQuadricTexture(q, Glu.GLU_TRUE); Glu.gluSphere(q, radius, this.NumberOfLongitudeSlices, this.NumberOfLongitudeSlices/2); Gl.glEndList(); } // Once created, we can just call its display list. Gl.glCallList(displayList); } #endregion #region Member variables private float radius; private IMaterial material; private bool created = false; private int displayList = 0; #endregion } }