private void CreateDisplayList(OpenGL gl) { displayList = new DisplayList(); displayList.Generate(gl); displayList.New(gl, DisplayList.DisplayListMode.CompileAndExecute); //Push all attributes, disable lighting and depth testing. gl.PushAttrib(OpenGL.GL_CURRENT_BIT | OpenGL.GL_ENABLE_BIT | OpenGL.GL_LINE_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); gl.Disable(OpenGL.GL_LIGHTING); gl.Disable(OpenGL.GL_TEXTURE_2D); gl.DepthFunc(OpenGL.GL_ALWAYS); //Set line width. gl.LineWidth(lineWidth); //Draw the line. gl.Begin(OpenGL.GL_LINES); gl.Color(Convert.ColorToGLColor(c1)); gl.Vertex(v1.X, v1.Y, v1.Z); gl.Color(Convert.ColorToGLColor(c2)); gl.Vertex(v2.X, v2.Y, v2.Z); gl.End(); // Restore attributes. gl.PopAttrib(); displayList.End(gl); }
/// <summary> /// Sets the attributes. /// </summary> /// <param name="gl">The OpenGL instance.</param> public override void SetAttributes(OpenGL gl) { if (enableDepthTest.HasValue) { gl.EnableIf(OpenGL.GL_DEPTH_TEST, enableDepthTest.Value); } if (depthFunction.HasValue) { gl.DepthFunc(depthFunction.Value); } if (depthClearValue.HasValue) { gl.ClearDepth(depthClearValue.Value); } if (enableDepthWritemask.HasValue) { gl.EnableIf(OpenGL.GL_DEPTH_WRITEMASK, enableDepthWritemask.Value); } }
private void CreateDisplayList(OpenGL gl) { displayList = new DisplayList(); displayList.Generate(gl); displayList.New(gl, DisplayList.DisplayListMode.CompileAndExecute); //Push all attributes, disable lighting and depth testing. gl.PushAttrib(OpenGL.GL_CURRENT_BIT | OpenGL.GL_ENABLE_BIT | OpenGL.GL_LINE_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); gl.Disable(OpenGL.GL_LIGHTING); gl.Disable(OpenGL.GL_TEXTURE_2D); gl.DepthFunc(OpenGL.GL_ALWAYS); //Set line width. gl.LineWidth(lineWidth); //Draw the line. gl.Begin(OpenGL.GL_LINES); for (float i = (size * -1); i < size; i+=0.4f) { float add = 0.2f; if (i > 0) add = 0.4f; if (i < 0 && i > -0.2f) add = 0.4f; gl.Color(1f, 0f, 0f, 1f); gl.Vertex(i, 0, 0); gl.Vertex(i + add, 0, 0); gl.Color(0f, 1f, 0f, 1f); gl.Vertex(0, i, 0); gl.Vertex(0, i + add, 0); gl.Color(0f, 0f, 1f, 1f); gl.Vertex(0, 0, i); gl.Vertex(0, 0, i + add); } gl.End(); // Restore attributes. gl.PopAttrib(); displayList.End(gl); }
private void CreateDisplayList(OpenGL gl) { displayList = new DisplayList(); displayList.Generate(gl); displayList.New(gl, DisplayList.DisplayListMode.CompileAndExecute); //Push all attributes, disable lighting and depth testing. gl.PushAttrib(OpenGL.GL_CURRENT_BIT | OpenGL.GL_ENABLE_BIT | OpenGL.GL_LINE_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); gl.Disable(OpenGL.GL_LIGHTING); gl.Disable(OpenGL.GL_TEXTURE_2D); gl.DepthFunc(OpenGL.GL_ALWAYS); //Set line width. gl.LineWidth(lineWidth); //Draw the line. gl.Begin(OpenGL.GL_LINES); for (int i = (size * -1); i <= size; i++) { if (i != 0) { if ((i % 4) == 0) gl.Color(Convert.ColorToGLColor(darkColor)); else gl.Color(Convert.ColorToGLColor(lightColor)); gl.Vertex(i, (size * -1), 0); gl.Vertex(i, size, 0); gl.Vertex((size * -1), i, 0); gl.Vertex(size, i, 0); } } gl.End(); // Restore attributes. gl.PopAttrib(); displayList.End(gl); }
public void Initialize(OpenGL gl) { this.gl = gl; gl.Enable(OpenGL.GL_DEPTH_TEST); gl.DepthFunc(DepthFunction.LessThanOrEqual); gl.ClearDepth(1.0); gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.Hint(HintTarget.PerspectiveCorrection, HintMode.Nicest); gl.Hint(HintTarget.LineSmooth, HintMode.Nicest); gl.Hint(HintTarget.PointSmooth, HintMode.Nicest); gl.Hint(HintTarget.PolygonSmooth, HintMode.Nicest); gl.Enable(OpenGL.GL_SMOOTH); gl.Enable(OpenGL.GL_LINE_SMOOTH); gl.Enable(OpenGL.GL_POINT_SMOOTH); gl.Enable(OpenGL.GL_MULTISAMPLE); gl.MinSampleShading(4.0f); }
/// <summary> /// Creates the display list. This function draws the /// geometry as well as compiling it. /// </summary> private void CreateDisplayList(OpenGL gl) { // Create the display list. displayList = new DisplayList(); // Generate the display list and displayList.Generate(gl); displayList.New(gl, DisplayList.DisplayListMode.CompileAndExecute); // Push all attributes, disable lighting and depth testing. gl.PushAttrib(OpenGL.GL_CURRENT_BIT | OpenGL.GL_ENABLE_BIT | OpenGL.GL_LINE_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); gl.Disable(OpenGL.GL_LIGHTING); gl.Disable(OpenGL.GL_TEXTURE_2D); gl.DepthFunc(OpenGL.GL_ALWAYS); // Set a nice fat line width. gl.LineWidth(1.50f); // Draw the axies. gl.Begin(OpenGL.GL_LINES); gl.Color(1f, 0f, 0f, 1f); gl.Vertex(0, 0, 0); gl.Vertex(3, 0, 0); gl.Color(0f, 1f, 0f, 1f); gl.Vertex(0, 0, 0); gl.Vertex(0, 3, 0); gl.Color(0f, 0f, 1f, 1f); gl.Vertex(0, 0, 0); gl.Vertex(0, 0, 3); gl.End(); // Restore attributes. gl.PopAttrib(); // End the display list. displayList.End(gl); }
/// <summary> /// This is called when a face is interactable, so highlight it. /// </summary> void IInteractable.DrawPick(OpenGL gl) { // Save all the attributes. gl.PushAttrib(OpenGL.ALL_ATTRIB_BITS); // Disable lighting, set colour to red etc. gl.Disable(OpenGL.LIGHTING); gl.DepthFunc(OpenGL.LEQUAL); // Now draw it with a faint red. gl.Enable(OpenGL.BLEND); gl.BlendFunc(OpenGL.SRC_ALPHA, OpenGL.ONE_MINUS_SRC_ALPHA); gl.Color(1, 0, 0, 0.2f); // Draw the face. gl.Begin(OpenGL.POLYGON); foreach (Index index in indices) { gl.Vertex(parentpoly.Vertices[index.Vertex]); } gl.End(); gl.Disable(OpenGL.BLEND); // Draw the face. gl.Begin(OpenGL.LINE_LOOP); for (int i = 0; i < indices.Count - 1; i++) { gl.Vertex(parentpoly.Vertices[indices[i].Vertex]); } gl.End(); gl.PopAttrib(); }
/// <summary> /// Use this to draw the vertex grid. /// </summary> /// <param name="gl">OpenGL object.</param> /// <param name="points">Draw each individual vertex (with selection names).</param> /// <param name="lines">Draw the lines connecting the points.</param> public virtual void Draw(OpenGL gl, bool points, bool lines) { // Save the attributes. gl.PushAttrib(OpenGL.GL_ALL_ATTRIB_BITS); gl.Disable(OpenGL.GL_LIGHTING); gl.Color(1, 0, 0, 1); if (points) { int name = 0; gl.PointSize(5); // Add a new name (the vertex name). gl.PushName(0); foreach (Vertex v in vertices) { // Set the name, draw the vertex. gl.LoadName((uint)name++); //todo draw vertex //((IInteractable)v).DrawPick(gl); } // Pop the name. gl.PopName(); } if (lines) { // Draw lines along each row, then along each column. gl.DepthFunc(OpenGL.GL_ALWAYS); gl.LineWidth(1); gl.Disable(OpenGL.GL_LINE_SMOOTH); for (int col = 0; col < y; col++) { for (int row = 0; row < x; row++) { // Create vertex indicies. int nTopLeft = (col * x) + row; int nBottomLeft = ((col + 1) * x) + row; gl.Begin(OpenGL.GL_LINES); if (row < (x - 1)) { gl.Vertex(vertices[nTopLeft]); gl.Vertex(vertices[nTopLeft + 1]); } if (col < (y - 1)) { gl.Vertex(vertices[nTopLeft]); gl.Vertex(vertices[nBottomLeft]); } gl.End(); } } gl.DepthFunc(OpenGL.GL_LESS); } gl.PopAttrib(); }
public static void MyGlobalAxis(SharpGL.OpenGL gl, int AxesLength = 20, int LineWidth = 2, int Pointsize = 3, Boolean DoMinusTicks = true, Boolean TagOrigin = true, Boolean DoXYZAnnotation = true, Boolean DoMinusXYZAnnotation = true, Boolean DoUnitTicks = true, Boolean DoAnnotateZTicks = true, Boolean DoAnnotateYTicks = true, Boolean DoAnnotateXTicks = true, Boolean DoPlusTicks = true, float tick_annotation_scale = 0.4f, Boolean Draw_Minus_Z_Axis_Leg = true, Boolean Draw_Minus_Y_Axis_Leg = true, Boolean Draw_Minus_X_Axis_Leg = true) { gl.PushMatrix( ); gl.PushAttrib(SharpGL.OpenGL.GL_CURRENT_BIT | SharpGL.OpenGL.GL_ENABLE_BIT | SharpGL.OpenGL.GL_LINE_BIT | SharpGL.OpenGL.GL_DEPTH_BUFFER_BIT); gl.Disable(SharpGL.OpenGL.GL_LIGHTING); gl.Disable(SharpGL.OpenGL.GL_TEXTURE_2D); gl.DepthFunc(SharpGL.OpenGL.GL_ALWAYS); gl.LineWidth(LineWidth); // Draw the axis and annotate the ends. if (TagOrigin) { gl.Color(White); gl.PointSize(Pointsize); gl.Vertex(Origin); gl.PushMatrix( ); gl.DrawText3D(AxisLabelFont, 10.0f, 0.0f, 0.2f, "0"); gl.PopMatrix( ); } gl.Color(Red); gl.Begin(SharpGL.OpenGL.GL_LINES); gl.Vertex(Origin); gl.Vertex(AxesLength, 0, 0); gl.End( ); if (Draw_Minus_X_Axis_Leg) { gl.Color(Red); gl.Begin(SharpGL.OpenGL.GL_LINES); gl.Vertex(Origin); gl.Vertex(-AxesLength, 0, 0); gl.End( ); } if (DoXYZAnnotation) { gl.PushMatrix( ); gl.Translate(AxesLength, 0, 0); gl.DrawText3D(AxisLabelFont, 10.0f, 0.0f, 0.2f, "+X"); gl.PopMatrix( ); } if (DoMinusXYZAnnotation) { gl.PushMatrix( ); gl.Translate(-AxesLength, 0, 0); gl.DrawText3D(AxisLabelFont, 10.0f, 0.0f, 0.2f, "-X"); gl.PopMatrix( ); } gl.Color(Green); gl.Begin(SharpGL.OpenGL.GL_LINES); gl.Vertex(Origin); gl.Vertex(0, AxesLength, 0); gl.End( ); if (Draw_Minus_Y_Axis_Leg) { gl.Color(Green); gl.Begin(SharpGL.OpenGL.GL_LINES); gl.Vertex(Origin); gl.Vertex(0, -AxesLength, 0); gl.End( ); } if (DoXYZAnnotation) { gl.PushMatrix( ); gl.Translate(0, AxesLength, 0); gl.DrawText3D(AxisLabelFont, 10.0f, 0.0f, 0.1f, "+Y"); gl.PopMatrix( ); } if (DoMinusXYZAnnotation) { gl.PushMatrix( ); gl.Translate(0, -AxesLength, 0); gl.DrawText3D(AxisLabelFont, 10.0f, 0.0f, 0.1f, "-Y"); gl.PopMatrix( ); } if (true) { gl.Color(Blue); gl.Begin(SharpGL.OpenGL.GL_LINES); gl.Vertex(Origin); gl.Vertex(0, 0, AxesLength); gl.End( ); } if (Draw_Minus_Z_Axis_Leg) { gl.Color(Blue); gl.Begin(SharpGL.OpenGL.GL_LINES); gl.Vertex(Origin); gl.Vertex(0, 0, -AxesLength); gl.End( ); } if (DoXYZAnnotation) { gl.PushMatrix( ); gl.Translate(0, 0, AxesLength); gl.DrawText3D(AxisLabelFont, 10.0f, 0.0f, 0.05f, "+Z"); gl.PopMatrix( ); } if (DoMinusXYZAnnotation && Draw_Minus_Z_Axis_Leg) { gl.PushMatrix( ); gl.Translate(0, 0, -AxesLength); gl.DrawText3D(AxisLabelFont, 10.0f, 0.0f, 0.05f, "-Z"); gl.PopMatrix( ); } if (DoUnitTicks) { gl.PointSize(Pointsize); gl.Color(White); for (int i = 0; i < AxesLength; i++) { if (DoPlusTicks) { gl.Begin(SharpGL.Enumerations.BeginMode.Points); gl.Vertex(i, 0, 0); gl.Vertex(0, i, 0); gl.Vertex(0, 0, i); gl.End( ); } if (DoMinusTicks) { gl.Begin(SharpGL.Enumerations.BeginMode.Points); gl.Vertex(-i, 0, 0); gl.Vertex(0, -i, 0); if (Draw_Minus_Z_Axis_Leg) { gl.Vertex(0, 0, -i); } gl.End( ); } if (DoAnnotateZTicks) { gl.PushMatrix( ); gl.Translate(0, 0, i); gl.Scale(tick_annotation_scale, tick_annotation_scale, tick_annotation_scale); gl.DrawText3D(faceName: AxisLabelFont, fontSize: .1f, deviation: 0.0f, extrusion: 0.05f, text: i.ToString( )); gl.PopMatrix( ); } if (DoMinusTicks && Draw_Minus_Z_Axis_Leg) { gl.PushMatrix( ); gl.Translate(0, 0, -i); gl.Scale(tick_annotation_scale, tick_annotation_scale, tick_annotation_scale); gl.DrawText3D(faceName: AxisLabelFont, fontSize: .1f, deviation: 0.0f, extrusion: 0.05f, text: (-i).ToString( )); gl.PopMatrix( ); } if (DoAnnotateYTicks) { gl.PushMatrix( ); gl.Translate(0, i, 0); gl.Scale(tick_annotation_scale, tick_annotation_scale, tick_annotation_scale); gl.DrawText3D(faceName: AxisLabelFont, fontSize: .1f, deviation: 0.0f, extrusion: 0.05f, text: i.ToString( )); gl.PopMatrix( ); } if (DoMinusTicks) { gl.PushMatrix( ); gl.Translate(0, -i, 0); gl.Scale(tick_annotation_scale, tick_annotation_scale, tick_annotation_scale); gl.DrawText3D(faceName: AxisLabelFont, fontSize: .1f, deviation: 0.0f, extrusion: 0.05f, text: (-i).ToString( )); gl.PopMatrix( ); } if (DoAnnotateXTicks) { gl.PushMatrix( ); gl.Translate(i, 0, 0); gl.Scale(tick_annotation_scale, tick_annotation_scale, tick_annotation_scale); gl.DrawText3D(faceName: AxisLabelFont, fontSize: .1f, deviation: 0.0f, extrusion: 0.05f, text: i.ToString( )); gl.PopMatrix( ); } if (DoMinusTicks) { gl.PushMatrix( ); gl.Translate(-i, 0, 0); gl.Scale(tick_annotation_scale, tick_annotation_scale, tick_annotation_scale); gl.DrawText3D(faceName: AxisLabelFont, fontSize: .1f, deviation: 0.0f, extrusion: 0.05f, text: (-i).ToString( )); gl.PopMatrix( ); } } } // Restore attributes. gl.PopAttrib( ); gl.PopMatrix( ); }
/// <summary> /// Casts a real time 3D shadow. /// </summary> /// <param name="gl">The OpenGL object.</param> /// <param name="lights">The lights.</param> private void CastShadow(OpenGL gl) { // Set the connectivity, (calculate the neighbours of each face). SetConnectivity(); // Get the lights in the scene. var lights = TraverseToRootElement().Traverse <Light>(l => l.IsEnabled && l.On && l.CastShadow); // Get some useful references. var faces = ParentPolygon.Faces; // Go through every light in the scene. foreach (var light in lights) { // Every face will have a visibility setting. bool[] facesVisible = new bool[faces.Count]; // Get the light position relative to the polygon. Vertex lightPos = light.Position; lightPos = lightPos - ParentPolygon.Transformation.TranslationVertex; // Go through every face, finding out whether it's visible to the light. for (int nFace = 0; nFace < faces.Count; nFace++) { // Get a reference to the face. Face face = faces[nFace]; // Is this face facing the light? float[] planeEquation = face.GetPlaneEquation(ParentPolygon); float side = planeEquation[0] * lightPos.X + planeEquation[1] * lightPos.Y + planeEquation[2] * lightPos.Z + planeEquation[3]; facesVisible[nFace] = (side > 0) ? true : false; } // Save all the attributes. gl.PushAttrib(OpenGL.GL_ALL_ATTRIB_BITS); // Turn off lighting. gl.Disable(OpenGL.GL_LIGHTING); // Turn off writing to the depth mask. gl.DepthMask(0); gl.DepthFunc(OpenGL.GL_LEQUAL); // Turn on stencil buffer testing. gl.Enable(OpenGL.GL_STENCIL_TEST); // Translate our shadow volumes. ParentPolygon.PushObjectSpace(gl); // Don't draw to the color buffer. gl.ColorMask(0, 0, 0, 0); gl.StencilFunc(OpenGL.GL_ALWAYS, 1, 0xFFFFFFFF); gl.Enable(OpenGL.GL_CULL_FACE); // First Pass. Increase Stencil Value In The Shadow gl.FrontFace(OpenGL.GL_CCW); gl.StencilOp(OpenGL.GL_KEEP, OpenGL.GL_KEEP, OpenGL.GL_INCR); DoShadowPass(gl, lightPos, facesVisible); // Second Pass. Decrease Stencil Value In The Shadow gl.FrontFace(OpenGL.GL_CW); gl.StencilOp(OpenGL.GL_KEEP, OpenGL.GL_KEEP, OpenGL.GL_DECR); DoShadowPass(gl, lightPos, facesVisible); gl.FrontFace(OpenGL.GL_CCW); ParentPolygon.PopObjectSpace(gl); // Enable writing to the color buffer. gl.ColorMask(1, 1, 1, 1); // Draw A Shadowing Rectangle Covering The Entire Screen gl.Color(light.ShadowColor); gl.Enable(OpenGL.GL_BLEND); gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA); gl.StencilFunc(OpenGL.GL_NOTEQUAL, 0, 0xFFFFFFF); gl.StencilOp(OpenGL.GL_KEEP, OpenGL.GL_KEEP, OpenGL.GL_KEEP); Sphere shadow = new Sphere(); shadow.Transformation.ScaleX = shadowSize; shadow.Transformation.ScaleY = shadowSize; shadow.Transformation.ScaleZ = shadowSize; shadow.Render(gl, RenderMode.Design); gl.PopAttrib(); } }
/// <summary> /// Use this to draw the vertex grid. /// </summary> /// <param name="gl">OpenGL object.</param> /// <param name="points">Draw each individual vertex (with selection names).</param> /// <param name="lines">Draw the lines connecting the points.</param> public virtual void Draw(OpenGL gl, bool points, bool lines) { // Save the attributes. gl.PushAttrib(OpenGL.ALL_ATTRIB_BITS); gl.Disable(OpenGL.LIGHTING); gl.Color(1, 0, 0, 1); if(points) { int name = 0; gl.PointSize(5); // Add a new name (the vertex name). gl.PushName(0); foreach(Vertex v in vertices) { // Set the name, draw the vertex. gl.LoadName((uint)name++); ((IInteractable)v).DrawPick(gl); } // Pop the name. gl.PopName(); } if(lines) { // Draw lines along each row, then along each column. gl.DepthFunc(OpenGL.ALWAYS); gl.LineWidth(1); gl.Disable(OpenGL.LINE_SMOOTH); for(int col=0; col < y; col++) { for(int row=0; row < x; row++) { // Create vertex indicies. int nTopLeft = (col * x) + row; int nBottomLeft = ((col + 1) * x) + row; gl.Begin(OpenGL.LINES); if(row < (x-1)) { gl.Vertex(vertices[nTopLeft]); gl.Vertex(vertices[nTopLeft + 1]); } if(col < (y-1)) { gl.Vertex(vertices[nTopLeft]); gl.Vertex(vertices[nBottomLeft]); } gl.End(); } } gl.DepthFunc(OpenGL.LESS); } gl.PopAttrib(); }
/// <summary> /// This is called when a face is interactable, so highlight it. /// </summary> void IInteractable.DrawPick(OpenGL gl) { // Save all the attributes. gl.PushAttrib(OpenGL.ALL_ATTRIB_BITS); // Disable lighting, set colour to red etc. gl.Disable(OpenGL.LIGHTING); gl.DepthFunc(OpenGL.LEQUAL); // Now draw it with a faint red. gl.Enable(OpenGL.BLEND); gl.BlendFunc(OpenGL.SRC_ALPHA, OpenGL.ONE_MINUS_SRC_ALPHA); gl.Color(1, 0, 0, 0.2f); // Draw the face. gl.Begin(OpenGL.POLYGON); foreach(Index index in indices) gl.Vertex(parentpoly.Vertices[index.Vertex]); gl.End(); gl.Disable(OpenGL.BLEND); // Draw the face. gl.Begin(OpenGL.LINE_LOOP); for(int i = 0; i < indices.Count - 1; i++) gl.Vertex(parentpoly.Vertices[indices[i].Vertex]); gl.End(); gl.PopAttrib(); }