Prince Sultan University College of Computer & Information Sciences Department of Computer Science Practice Sheet 3 Using Triangle Fans to Build Solid Shapes Objective: Use triangle fans to build a solid cone: Step 1: start at the top by including the required libraries and declare the global variables using OpenTK.Graphics.OpenGL; // Rotation amounts static float xRot = 0.0f; static float yRot = 0.0f; // Flags for effects bool iCull = false; bool iOutline = true; bool iDepth = false; 1 Step 2: Build the RenderCone() method as follows: private void RenderCone() { double x, y, angle; int iPivot = 1; // Storage for coordinates and angles // Used to flag alternating colors // Clear the window and the depth buffer GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); // Turn culling on if flag is set if (iCull) GL.Enable(EnableCap.CullFace); else GL.Disable(EnableCap.CullFace); // Enable depth testing if flag is set if (iDepth) GL.Enable(EnableCap.DepthTest); else GL.Disable(EnableCap.DepthTest); // Draw back side as a polygon only, if flag is set if (iOutline) GL.PolygonMode(MaterialFace.Back, PolygonMode.Line); else GL.PolygonMode(MaterialFace.Back, PolygonMode.Line); // Save matrix state and do the rotation GL.PushMatrix(); GL.Rotate(xRot, 1.0f, 0.0f, 0.0f); GL.Rotate(yRot, 0.0f, 1.0f, 0.0f); // Begin a triangle fan GL.Begin(BeginMode.TriangleFan); // Pinnacle of cone is shared vertex for fan, moved up Z axis // to produce a cone instead of a circle GL.Vertex3(0.0f, 0.0f, 75.0f); // Loop around in a circle and specify even points along the circle // as the vertices of the triangle fan for (angle = 0.0; angle <= (2.0 * Math.PI)+0.001; angle += (Math.PI / 8.0)) { // Calculate x and y position of the next vertex x = 50.0f * Math.Sin(angle); y = 50.0f * Math.Cos(angle); // Alternate color between red and green if ((iPivot % 2) == 0) 2 GL.Color3(0.0f, 1.0f, 0.0f); else GL.Color3(1.0f, 0.0f, 0.0f); // Increment pivot to change color next time iPivot++; // Specify the next vertex for the triangle fan GL.Vertex2(x, y); } // Done drawing fan for cone GL.End(); // Note That this part is to cover the bottom of the cone // Begin a new triangle fan to cover the bottom GL.Begin(BeginMode.TriangleFan); // Center of fan is at the origin GL.Vertex2(0.0f, 0.0f); for (angle = 0.0f; angle <= (2.0f * Math.PI)+0.001; angle += (Math.PI / 8.0f)) { // Calculate x and y position of the next vertex x = 50.0f * Math.Sin(angle); y = 50.0f * Math.Cos(angle); // Alternate color between red and green if ((iPivot % 2) == 0) GL.Color3(0.0f, 1.0f, 0.0f); else GL.Color3(1.0f, 0.0f, 0.0f); // Increment pivot to change color next time iPivot++; // Specify the next vertex for the triangle fan GL.Vertex2(x, y); } // Done drawing the fan that covers the bottom GL.End(); // Restore transformations GL.PopMatrix(); glControl1.SwapBuffers(); } 3 Step 3: Implement the Paint event as follows: private void glControl1_Paint(object sender, PaintEventArgs e) { //DrawLine(); RenderCone(); } Step 4: Implement the KeyDown event as follows: private void glControl1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.PageUp) { xRot -= 5.0f; } if (e.KeyCode == Keys.F2) xRot += 5.0f; if (e.KeyCode == Keys.F3) yRot -= 5.0f; if (e.KeyCode == Keys.F4) yRot += 5.0f; if (xRot > 356.0f) xRot = 0.0f; if (xRot < -1.0f) xRot = 355.0f; if (yRot > 356.0f) yRot = 0.0f; if (yRot < -1.0f) yRot = 355.0f; // Refresh the Window glControl1.Invalidate(); } 4 Step 5: Implement the Resize() event as follows: private void glControl1_Resize(object sender, EventArgs e) { float nRange = 100.0f; int w = glControl1.Width; int h = glControl1.Height; // Prevent a divide by zero if (h == 0) h = 1; // Set Viewport to window dimensions GL.Viewport(0, 0, w, h); // Reset projection matrix stack GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far) if (w <= h) GL.Ortho(-nRange, nRange, -nRange * h / w, nRange * h / w, -nRange, nRange); else GL.Ortho(-nRange * w / h, nRange * w / h, -nRange, nRange, -nRange, nRange); // Reset Model view matrix stack GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); glControl1.Invalidate(); } 5