// This method renders our scene. // We could optimize it in any of several ways, including factoring out the repeated OpenGL initialization calls and // hanging onto the GLU quadric object, but the details of how it's implemented aren't important here. // The main thing to note is that we've factored the drawing code out of the NSView subclass so that // the full-screen and non-fullscreen views share the same states for rendering // (and MainController can use it when rendering in full-screen mode on pre-10.6 systems). public void render () { GL.Enable (EnableCap.DepthTest); GL.Enable (EnableCap.CullFace); GL.Enable (EnableCap.Lighting); GL.Enable (EnableCap.Light0); GL.Enable (EnableCap.Texture2D); GL.ClearColor (0, 0, 0, 0); GL.Clear (ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); // Upload the texture // Since we are sharing OpenGL objects between the full-screen and non-fullscreen contexts, we only need to do this once if (textureName == 0) { var path = NSBundle.MainBundle.PathForResource ("Earth", "jpg"); texture = new Texture (path); textureName = texture.TextureName; } // Set up texturing parameters GL.BindTexture (TextureTarget.Texture2D, textureName); GL.TexEnv (TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (float)All.Modulate); lightDirection[0] = (float)Math.Cos (degreesToRadians (SunAngle)); lightDirection[2] = (float)Math.Sin (degreesToRadians (SunAngle)); GL.Light (LightName.Light0, LightParameter.Position, lightDirection); GL.PushMatrix (); // Back the camera off a bit GL.Translate (0, 0, -1.5f); // Draw the Earth! if (wireFrame) GL.PolygonMode (MaterialFace.FrontAndBack, PolygonMode.Line); else GL.PolygonMode (MaterialFace.FrontAndBack, PolygonMode.Fill); GL.Material (MaterialFace.Front, MaterialParameter.Ambient, materialAmbient); GL.Material (MaterialFace.Front, MaterialParameter.Diffuse, materialDiffuse); GL.Rotate (RollAngle, 1, 0, 0); // Earth's axial tilt is 23.45 degrees from the plane of the ecliptic GL.Rotate (-23.45, 0, 0, 1); GL.Rotate ((animationPhase * 360), 0, 1, 0); GL.Rotate (-90, 1, 0, 0); sphere.Draw (); GL.PopMatrix (); GL.BindTexture (TextureTarget.Texture2D, 0); GL.Flush (); }
// This method renders our scene. // We could optimize it in any of several ways, including factoring out the repeated OpenGL initialization calls and // hanging onto the GLU quadric object, but the details of how it's implemented aren't important here. // The main thing to note is that we've factored the drawing code out of the NSView subclass so that // the full-screen and non-fullscreen views share the same states for rendering // (and MainController can use it when rendering in full-screen mode on pre-10.6 systems). public void Render() { IntPtr quadric = IntPtr.Zero; GL.glEnable (GL.GL_DEPTH_TEST); GL.glEnable (GL.GL_CULL_FACE); GL.glEnable (GL.GL_LIGHTING); GL.glEnable (GL.GL_LIGHT0); GL.glEnable (GL.GL_TEXTURE_2D); GL.glClearColor (0, 0, 0, 0); GL.glClear (GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Upload the texture // Since we are sharing OpenGL objects between the full-screen and non-fullscreen contexts, we only need to do this once if (this.texture == null) { NSString path = NSBundle.MainBundle.PathForResourceOfType ("Earth", "jpg"); this.texture = new Texture (path); this.textureName = this.texture.TextureName; } // Set up texturing parameters GL.glBindTexture (GL.GL_TEXTURE_2D, this.textureName); GL.glTexEnvf (GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE); // Set up our single directional light (the Sun!) lightDirection[0] = (float)Math.Cos (sunAngle * Math.PI / 180.0); lightDirection[2] = (float)Math.Sin (sunAngle * Math.PI / 180.0); GL.glLightfv (GL.GL_LIGHT0, GL.GL_POSITION, lightDirection); GL.glPushMatrix (); // Back the camera off a bit GL.glTranslatef (0.0f, 0.0f, -1.5f); // Draw the Earth! quadric = GL.gluNewQuadric (); if (wireframe) { GL.gluQuadricDrawStyle (quadric, GL.GLU_LINE); } GL.gluQuadricTexture (quadric, (byte)GL.GL_TRUE); GL.glMaterialfv (GL.GL_FRONT, GL.GL_AMBIENT, materialAmbient); GL.glMaterialfv (GL.GL_FRONT, GL.GL_DIFFUSE, materialDiffuse); GL.glRotatef (rollAngle, 1.0f, 0.0f, 0.0f); GL.glRotatef (-23.45f, 0.0f, 0.0f, 1.0f); // Earth's axial tilt is 23.45 degrees from the plane of the ecliptic GL.glRotatef (animationPhase * 360.0f, 0.0f, 1.0f, 0.0f); GL.glRotatef (-90.0f, 1.0f, 0.0f, 0.0f); GL.gluSphere (quadric, radius, 48, 24); GL.gluDeleteQuadric (quadric); quadric = IntPtr.Zero; GL.glPopMatrix (); GL.glBindTexture (GL.GL_TEXTURE_2D, 0); }