/// <summary> /// This function draws the polygon. /// </summary> /// <param name="gl">OpenGL.</param> public override void Draw(OpenGL gl) { if (DoPreDraw(gl)) { polyAttributes.Set(gl); foreach (Face face in faces) { // Begin drawing a polygon. if (face.Indices.Count == 2) { gl.Begin(OpenGL.LINES); } else { gl.Begin(OpenGL.POLYGON); } foreach (Index index in face.Indices) { // Set a texture coord (if any). if (index.UV != -1) { gl.TexCoord(uvs[index.UV]); } // Set a normal, or generate one. if (index.Normal != -1) { gl.Normal(normals[index.Normal]); } else { // Do we have enough vertices for a normal? if (face.Indices.Count >= 3) { // Create a normal. Vertex vNormal = face.GetSurfaceNormal(this); vNormal.UnitLength(); // Add it to the normals, setting the index for next time. index.Normal = normals.Add(new Normal(vNormal)); gl.Normal(vNormal); } } // Set the vertex. gl.Vertex(vertices[index.Vertex]); } gl.End(); } // If the current context is edit vertices, draw the vertices. if (currentContext == Context.EditVertices) { // Push all the attributes. gl.PushAttrib(OpenGL.ALL_ATTRIB_BITS); // Set the colour to red, large points, no lighting. gl.Color(1, 0, 0, 1); gl.PointSize(5); gl.Disable(OpenGL.LIGHTING); // Draw the control points. for (int point = 0; point < vertices.Count; point++) { // Name the point, then draw it. gl.PushName((uint)point); gl.Begin(OpenGL.POINTS); gl.Vertex(vertices[point]); gl.End(); gl.PopName(); } // Restore the attributes. gl.PopAttrib(); } // If the current context is edit normals, draw the normals. if (currentContext == Context.EditNormals) { // We're going to disable lighting. Attributes.Lighting lighting = new Attributes.Lighting(); lighting.Enable = false; lighting.Set(gl); gl.Color(1, 0, 0); gl.Begin(OpenGL.LINES); // Draw the normals points. foreach (Face face in faces) { foreach (Index index in face.Indices) { if (index.Normal == -1) { continue; } // Translate to that vertex. Vertex v = vertices[index.Vertex]; gl.PushName((uint)index.Normal); gl.Vertex(v); gl.Vertex(v + normals[index.Normal]); gl.PopName(); } } gl.End(); lighting.Restore(gl); } if (currentContext == Context.EditFaces) { for (int i = 0; i < Faces.Count; i++) { // Push the face name. gl.PushName((uint)i); // Set the parent poly. faces[i].parentpoly = this; // Draw it. ((IInteractable)faces[i]).DrawPick(gl); // Pop the name. gl.PopName(); } } // Draw normals if we have to. if (drawNormals) { // Set the colour to red. gl.PushAttrib(OpenGL.ALL_ATTRIB_BITS); gl.Color(1, 0, 0, 1); gl.Disable(OpenGL.LIGHTING); // Go through each face. foreach (Face face in faces) { // Go though each index. foreach (Index index in face.Indices) { // Make sure it's got a normal, and a vertex. if (index.Normal != -1 && index.Vertex != -1) { // Get the vertex. Vertex vertex = vertices[index.Vertex]; // Get the normal vertex. Normal normal = normals[index.Normal]; Vertex vertex2 = vertex + normal; gl.Begin(OpenGL.LINES); gl.Vertex(vertex); gl.Vertex(vertex2); gl.End(); } } } // Restore the attributes. gl.PopAttrib(); } polyAttributes.Restore(gl); DoPostDraw(gl); } }
/// <summary> /// This function draws the polygon. /// </summary> /// <param name="gl">OpenGL.</param> public override void Draw(OpenGL gl) { if(DoPreDraw(gl)) { polyAttributes.Set(gl); foreach(Face face in faces) { // Begin drawing a polygon. if(face.Indices.Count == 2) gl.Begin(OpenGL.LINES); else gl.Begin(OpenGL.POLYGON); foreach(Index index in face.Indices) { // Set a texture coord (if any). if(index.UV != -1) gl.TexCoord(uvs[index.UV]); // Set a normal, or generate one. if(index.Normal != -1) gl.Normal(normals[index.Normal]); else { // Do we have enough vertices for a normal? if(face.Indices.Count >= 3) { // Create a normal. Vertex vNormal = face.GetSurfaceNormal(this); vNormal.UnitLength(); // Add it to the normals, setting the index for next time. index.Normal = normals.Add(new Normal(vNormal)); gl.Normal(vNormal); } } // Set the vertex. gl.Vertex(vertices[index.Vertex]); } gl.End(); } // If the current context is edit vertices, draw the vertices. if(currentContext == Context.EditVertices) { // Push all the attributes. gl.PushAttrib(OpenGL.ALL_ATTRIB_BITS); // Set the colour to red, large points, no lighting. gl.Color(1, 0, 0, 1); gl.PointSize(5); gl.Disable(OpenGL.LIGHTING); // Draw the control points. for(int point = 0; point < vertices.Count; point++) { // Name the point, then draw it. gl.PushName((uint)point); gl.Begin(OpenGL.POINTS); gl.Vertex(vertices[point]); gl.End(); gl.PopName(); } // Restore the attributes. gl.PopAttrib(); } // If the current context is edit normals, draw the normals. if(currentContext == Context.EditNormals) { // We're going to disable lighting. Attributes.Lighting lighting = new Attributes.Lighting(); lighting.Enable = false; lighting.Set(gl); gl.Color(1, 0, 0); gl.Begin(OpenGL.LINES); // Draw the normals points. foreach(Face face in faces) { foreach(Index index in face.Indices) { if(index.Normal == -1) continue; // Translate to that vertex. Vertex v = vertices[index.Vertex]; gl.PushName((uint)index.Normal); gl.Vertex(v); gl.Vertex(v + normals[index.Normal]); gl.PopName(); } } gl.End(); lighting.Restore(gl); } if(currentContext == Context.EditFaces) { for(int i = 0; i<Faces.Count; i++) { // Push the face name. gl.PushName((uint)i); // Set the parent poly. faces[i].parentpoly = this; // Draw it. ((IInteractable)faces[i]).DrawPick(gl); // Pop the name. gl.PopName(); } } // Draw normals if we have to. if(drawNormals) { // Set the colour to red. gl.PushAttrib(OpenGL.ALL_ATTRIB_BITS); gl.Color(1, 0, 0, 1); gl.Disable(OpenGL.LIGHTING); // Go through each face. foreach(Face face in faces) { // Go though each index. foreach(Index index in face.Indices) { // Make sure it's got a normal, and a vertex. if(index.Normal != -1 && index.Vertex != -1) { // Get the vertex. Vertex vertex = vertices[index.Vertex]; // Get the normal vertex. Normal normal = normals[index.Normal]; Vertex vertex2 = vertex + normal; gl.Begin(OpenGL.LINES); gl.Vertex(vertex); gl.Vertex(vertex2); gl.End(); } } } // Restore the attributes. gl.PopAttrib(); } polyAttributes.Restore(gl); DoPostDraw(gl); } }