/// <summary> /// Draws the text. /// </summary> /// <param name="gl">The gl.</param> /// <param name="x">The x.</param> /// <param name="y">The y.</param> /// <param name="r">The r.</param> /// <param name="g">The g.</param> /// <param name="b">The b.</param> /// <param name="faceName">Name of the face.</param> /// <param name="fontSize">Size of the font.</param> /// <param name="text">The text.</param> public void DrawText(OpenGL gl, int x, int y, float r, float g, float b, string faceName, float fontSize, string text) { // Get the font size in pixels. var fontHeight = (int)(fontSize * (16.0f / 12.0f)); // Do we have a font bitmap entry for this OpenGL instance and face name? var result = (from fbe in fontBitmapEntries where fbe.HDC == gl.RenderContextProvider.DeviceContextHandle && fbe.HRC == gl.RenderContextProvider.RenderContextHandle && String.Compare(fbe.FaceName, faceName, StringComparison.OrdinalIgnoreCase) == 0 && fbe.Height == fontHeight select fbe).ToList(); // Get the FBE or null. var fontBitmapEntry = result.FirstOrDefault(); // If we don't have the FBE, we must create it. if (fontBitmapEntry == null) fontBitmapEntry = CreateFontBitmapEntry(gl, faceName, fontHeight); double width = gl.RenderContextProvider.Width; double height = gl.RenderContextProvider.Height; // Create the appropriate projection matrix. gl.MatrixMode(OpenGL.GL_PROJECTION); gl.PushMatrix(); gl.LoadIdentity(); int[] viewport = new int[4]; gl.GetInteger(OpenGL.GL_VIEWPORT, viewport); gl.Ortho(0, width, 0, height, -1, 1); // Create the appropriate modelview matrix. gl.MatrixMode(OpenGL.GL_MODELVIEW); gl.PushMatrix(); gl.LoadIdentity(); gl.Color(r, g, b); gl.RasterPos(x, y); gl.PushAttrib(OpenGL.GL_LIST_BIT | OpenGL.GL_CURRENT_BIT | OpenGL.GL_ENABLE_BIT | OpenGL.GL_TRANSFORM_BIT); gl.Color(r, g, b); gl.Disable(OpenGL.GL_LIGHTING); gl.Disable(OpenGL.GL_TEXTURE_2D); gl.Disable(OpenGL.GL_DEPTH_TEST); gl.RasterPos(x, y); // Set the list base. gl.ListBase(fontBitmapEntry.ListBase); // Create an array of lists for the glyphs. var lists = text.Select(c => (byte) c).ToArray(); // Call the lists for the string. gl.CallLists(lists.Length, lists); gl.Flush(); // Reset the list bit. gl.PopAttrib(); // Pop the modelview. gl.PopMatrix(); // back to the projection and pop it, then back to the model view. gl.MatrixMode(OpenGL.GL_PROJECTION); gl.PopMatrix(); gl.MatrixMode(OpenGL.GL_MODELVIEW); }
/// <summary> /// Creates the display list. This function draws the /// geometry as well as compiling it. /// </summary> void CreateDisplayList(SharpGL.OpenGL gl) { // Create the display list. this.displayList = new DisplayList(); // Generate the display list and this.displayList.Generate(gl); this.displayList.New(gl, DisplayList.DisplayListMode.CompileAndExecute); // Push attributes, set the color. gl.PushAttrib(OpenGL.GL_CURRENT_BIT | OpenGL.GL_ENABLE_BIT | OpenGL.GL_LINE_BIT); gl.Disable(OpenGL.GL_LIGHTING); gl.Disable(OpenGL.GL_TEXTURE_2D); gl.LineWidth(1.0f); // Draw the grid lines. gl.Begin(SharpGL.OpenGL.GL_LINES); for (int i = -10; i <= 10; i++) { float fcol = ((i % 10) == 0) ? 0.3f : 0.15f; gl.Color(fcol, fcol, fcol); gl.Vertex(i, -10, 0); gl.Vertex(i, 10, 0); gl.Vertex(-10, i, 0); gl.Vertex(10, i, 0); } gl.End(); // Restore attributes. gl.PopAttrib(); // End the display list. this.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); 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); }
void drawTree(SharpGL.OpenGL gl, double dx, double dy, double dz, int index)//树的函数 { gl.Enable(OpenGL.GL_BLEND); gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA); gl.Enable(OpenGL.GL_ALPHA_TEST); gl.Enable(OpenGL.GL_TEXTURE_2D); gl.PushMatrix(); //强制刷新 gl.Translate(dx, dy, mydem.high[Convert.ToInt32(mydem.m - (dy + 50) / 0.5 - 1), Convert.ToInt32((dx + 50) / 0.5)]); texture[treeId[index]].Bind(gl); gl.Color(1f, 1f, 1f, 1f); gl.Begin(OpenGL.GL_QUADS); { gl.TexCoord(0, 1); gl.Vertex(-0.5f, 0f, 0f); gl.TexCoord(1, 1); gl.Vertex(0.5f, 0f, 0f); gl.TexCoord(1, 0); gl.Vertex(0.5f, 0f, 3.0f); gl.TexCoord(0, 0); gl.Vertex(-0.5f, 0.0f, 3.0f); } gl.End(); gl.Rotate(0f, 0f, 90); gl.Begin(OpenGL.GL_QUADS); { gl.TexCoord(0, 1); gl.Vertex(-0.5f, 0f, 0f); gl.TexCoord(1, 1); gl.Vertex(0.5f, 0f, 0f); gl.TexCoord(1, 0); gl.Vertex(0.5f, 0f, 3.0f); gl.TexCoord(0, 0); gl.Vertex(-0.5f, 0.0f, 3.0f); } gl.End(); gl.PopMatrix(); gl.Disable(OpenGL.GL_TEXTURE_2D); gl.Disable(OpenGL.GL_BLEND); gl.Disable(OpenGL.GL_ALPHA); }
void IRenderable.Render(OpenGL gl, RenderMode renderMode) { if (positionBuffer != null && colorBuffer != null && radiusBuffer != null) { if (this.shaderProgram == null) { this.shaderProgram = InitShader(gl, renderMode); } if (this.vertexArrayObject == null) { CreateVertexArrayObject(gl, renderMode); } BeforeRendering(gl, renderMode); if (this.RenderGrid && this.vertexArrayObject != null) { gl.Enable(OpenGL.GL_BLEND); gl.BlendFunc(SharpGL.Enumerations.BlendingSourceFactor.SourceAlpha, SharpGL.Enumerations.BlendingDestinationFactor.OneMinusSourceAlpha); gl.BindVertexArray(this.vertexArrayObject[0]); gl.DrawArrays(OpenGL.GL_POINTS, 0, count); gl.BindVertexArray(0); gl.Disable(OpenGL.GL_BLEND); } AfterRendering(gl, renderMode); } }
public override void Draw(SharpGL.OpenGL gl) { gl.PushMatrix(); gl.PushAttrib(AttributeMask.All); gl.PushClientAttrib(OpenGL.GL_CLIENT_ALL_ATTRIB_BITS); gl.VertexPointer(3, 0, vertexs); gl.NormalPointer(OpenGL.GL_FLOAT, 0, normals); gl.TexCoordPointer(2, OpenGL.GL_FLOAT, 0, texCoord); gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY); //gl.EnableClientState(OpenGL.GL_COLOR_ARRAY); gl.EnableClientState(OpenGL.GL_NORMAL_ARRAY); gl.EnableClientState(OpenGL.GL_TEXTURE_COORD_ARRAY); gl.Translate(position.x, position.y + sizeY / 2, position.z); gl.Color(color.GetInArrWithAlpha()); if (texture != null) { gl.Enable(OpenGL.GL_TEXTURE_2D); texture.Bind(gl); } else { gl.Disable(OpenGL.GL_TEXTURE_2D); } gl.DrawArrays(OpenGL.GL_QUADS, 0, 24); gl.PopClientAttrib(); gl.PopAttrib(); gl.PopMatrix(); }
public void RenderWithStruct(SharpGL.OpenGL gl, SharpGL.SceneGraph.Core.RenderMode renderMode) { //if (!this.IsRenderable()) //return; ShaderProgram shader = this.shader; //GetShader(gl, renderMode); shader.Bind(gl); // 用VAO+EBO进行渲染。 // Bind the out vertex array. //gl.BindVertexArray(vao[0]); gl.BindVertexArray(vertexArrayObject); // Draw the square. //gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, ebo[0]); gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, this.triangleBufferObject); // 启用Primitive restart gl.Enable(OpenGL.GL_PRIMITIVE_RESTART); gl.PrimitiveRestartIndex(uint.MaxValue);// 截断图元(四边形带、三角形带等)的索引值。 gl.DrawElements(this.primitiveMode, this.triangleIndexCount, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero); // Unbind our vertex array and shader. gl.BindVertexArray(0); gl.Disable(OpenGL.GL_PRIMITIVE_RESTART); shader.Unbind(gl); }
private void openGLControl1_OpenGLDraw(object sender, SharpGL.RenderEventArgs args) { SharpGL.OpenGL gl = this.openGLControl1.OpenGL; gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); gl.LoadIdentity(); gl.LookAt(0, 20, 30, 0, 0, 0, 0, 1, 0); //desenha o piso gl.Enable(OpenGL.GL_TEXTURE_2D); grass.Bind(gl); gl.Begin(OpenGL.GL_QUADS); gl.Color(0.0f, 1.0f, 0.0f); gl.TexCoord(0.0f, 0.0f); gl.Vertex(-100.0f, 0.0f, -100.0f); gl.TexCoord(100.0f, 0.0f); gl.Vertex(100.0f, 0.0f, -100.0f); gl.TexCoord(100.0f, 100.0f); gl.Vertex(100.0f, 0.0f, 100.0f); gl.TexCoord(0.0f, 100.0f); gl.Vertex(-100.0f, 0.0f, 100.0f); gl.End(); gl.Disable(OpenGL.GL_TEXTURE_2D); foreach (Polygon polygon in polygons) { polygon.PushObjectSpace(gl); polygon.Render(gl, SharpGL.SceneGraph.Core.RenderMode.Render); polygon.PopObjectSpace(gl); } gl.Flush(); }
public void Render(SharpGL.OpenGL gl, SharpGL.SceneGraph.Core.RenderMode renderMode) { // update matrix and bind shader program mat4 projectionMatrix = camera.GetProjectionMat4(); mat4 viewMatrix = camera.GetViewMat4(); mat4 modelMatrix = glm.scale(mat4.identity(), new vec3(1, 1, this.ZAxisScale)); shaderProgram.Bind(gl); shaderProgram.SetUniformMatrix4(gl, strprojectionMatrix, projectionMatrix.to_array()); shaderProgram.SetUniformMatrix4(gl, strviewMatrix, viewMatrix.to_array()); shaderProgram.SetUniformMatrix4(gl, strmodelMatrix, modelMatrix.to_array()); gl.BindVertexArray(vao[0]); // 启用Primitive restart gl.Enable(OpenGL.GL_PRIMITIVE_RESTART); gl.PrimitiveRestartIndex(uint.MaxValue);// 截断图元(四边形带、三角形带等)的索引值。 //GL.DrawArrays(primitiveMode, 0, vertexCount); gl.DrawElements((uint)primitiveMode, vertexCount + (this.pipe.Count - 1) * 2, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero); gl.BindVertexArray(0); gl.Disable(OpenGL.GL_PRIMITIVE_RESTART); // unbind shader program shaderProgram.Unbind(gl); }
private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs e) { // Get the OpenGL object, for quick access. SharpGL.OpenGL gl = this.openGLControl1.OpenGL; gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); gl.Color(1.0f, 1.0f, 1.0f); gl.MatrixMode(OpenGL.GL_PROJECTION); gl.LoadIdentity(); cam.Look(); gl.Disable(OpenGL.GL_TEXTURE_2D); PlaneSurfaceRenderer psr = new PlaneSurfaceRenderer(16); psr.render(gl); texture.Bind(gl); gl.Enable(OpenGL.GL_TEXTURE_2D); foreach (ShapeRenderer shape in listShape) { shape.render(gl); } }
public static void LocalAxis(SharpGL.OpenGL gl) { double[] Reddish = { 0.9, 0.3, 0.4 }; double[] Greenish = { 0.1, 0.8, 0.3 }; double[] Bluish = { 0.1, 0.2, 0.8 }; gl.PushAttrib(SharpGL.OpenGL.GL_LIGHTING); gl.Disable(SharpGL.OpenGL.GL_LIGHTING); gl.LineWidth(3); gl.Color(Reddish); gl.Begin(SharpGL.Enumerations.BeginMode.LineLoop); gl.Vertex(0f, 0f, 0f); gl.Vertex(1f, 0f, 0f); gl.End( ); gl.Color(Greenish); gl.Begin(SharpGL.Enumerations.BeginMode.LineLoop); gl.Vertex(0f, 0f, 0f); gl.Vertex(0f, 1f, 0f); gl.End( ); gl.Color(Bluish); gl.Begin(SharpGL.Enumerations.BeginMode.LineLoop); gl.Vertex(0f, 0f, 0f); gl.Vertex(0f, 0f, 1f); gl.End( ); gl.PopAttrib( ); }
//设置抗锯齿开关(实验性) //开启后在画圆时会出问题 public void setSmooth(bool value) { if (value == true) { gl.Enable(OpenGL.GL_BLEND); gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA); gl.Enable(OpenGL.GL_POINT_SMOOTH); gl.Enable(OpenGL.GL_LINE_SMOOTH); gl.Enable(OpenGL.GL_POLYGON_SMOOTH); } else { gl.Disable(OpenGL.GL_BLEND); gl.Disable(OpenGL.GL_POINT_SMOOTH); gl.Disable(OpenGL.GL_LINE_SMOOTH); gl.Disable(OpenGL.GL_POLYGON_SMOOTH); } }
/// <summary> /// Render to the provided instance of OpenGL. /// </summary> /// <param name="gl">The OpenGL instance.</param> /// <param name="renderMode">The render mode.</param> public void Render(OpenGL gl, RenderMode renderMode) { // Push attributes, disable lighting. gl.PushAttrib(OpenGL.GL_CURRENT_BIT | OpenGL.GL_ENABLE_BIT | OpenGL.GL_LINE_BIT | OpenGL.GL_POLYGON_BIT); gl.Disable(OpenGL.GL_LIGHTING); gl.Disable(OpenGL.GL_TEXTURE_2D); gl.LineWidth(1.0f); gl.Color(1f, 0.2f, 0.2f, 0.6f); gl.PolygonMode(OpenGL.GL_FRONT_AND_BACK, renderMode == RenderMode.HitTest ? (uint)PolygonMode.Filled : (uint)PolygonMode.Lines); gl.Begin(OpenGL.GL_QUADS); // Draw The Cube Using quads gl.Vertex(hhl); // Top Right Of The Quad (Top) gl.Vertex(lhl); // Top Left Of The Quad (Top) gl.Vertex(lhh); // Bottom Left Of The Quad (Top) gl.Vertex(hhh); // Bottom Right Of The Quad (Top) gl.Vertex(hlh); // Top Right Of The Quad (Bottom) gl.Vertex(llh); // Top Left Of The Quad (Bottom) gl.Vertex(lll); // Bottom Left Of The Quad (Bottom) gl.Vertex(hll); // Bottom Right Of The Quad (Bottom) gl.Vertex(hhh); // Top Right Of The Quad (Front) gl.Vertex(lhh); // Top Left Of The Quad (Front) gl.Vertex(llh); // Bottom Left Of The Quad (Front) gl.Vertex(hlh); // Bottom Right Of The Quad (Front) gl.Vertex(hll); // Top Right Of The Quad (Back) gl.Vertex(lll); // Top Left Of The Quad (Back) gl.Vertex(lhl); // Bottom Left Of The Quad (Back) gl.Vertex(hhl); // Bottom Right Of The Quad (Back) gl.Vertex(lhh); // Top Right Of The Quad (Left) gl.Vertex(lhl); // Top Left Of The Quad (Left) gl.Vertex(lll); // Bottom Left Of The Quad (Left) gl.Vertex(llh); // Bottom Right Of The Quad (Left) gl.Vertex(hhl); // Top Right Of The Quad (Right) gl.Vertex(hhh); // Top Left Of The Quad (Right) gl.Vertex(hlh); // Bottom Left Of The Quad (Right) gl.Vertex(hll); // Bottom Right Of The Quad (Right) gl.End(); // End Drawing The Cube // Pop attributes. gl.PopAttrib(); }
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 cbLightEnabled_CheckedChanged(object sender, EventArgs e) { SharpGL.OpenGL gl = this.openGLControl1.OpenGL; if (cbLightEnabled.Checked) { gl.Enable(OpenGL.GL_LIGHT0); } else { gl.Disable(OpenGL.GL_LIGHT0); } }
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); }
/// <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> /// This function sets all of the lights parameters into OpenGL. /// </summary> /// <param name="gl">The OpenGL instance.</param> public virtual void Bind(OpenGL gl) { if (on) { // Enable this light. gl.Enable(OpenGL.GL_LIGHTING); gl.Enable(glCode); // The light is on, so set it's properties. gl.Light(glCode, OpenGL.GL_AMBIENT, ambient); gl.Light(glCode, OpenGL.GL_DIFFUSE, diffuse); gl.Light(glCode, OpenGL.GL_SPECULAR, specular); gl.Light(glCode, OpenGL.GL_POSITION, new float[] { position.X, position.Y, position.Z, 1.0f }); // 180 degree cutoff gives an omnidirectional light. gl.Light(GLCode, OpenGL.GL_SPOT_CUTOFF, 180.0f); } else { gl.Disable(glCode); } }
/// <summary> /// Draws the text. /// </summary> /// <param name="gl">The gl.</param> /// <param name="x">The x.</param> /// <param name="y">The y.</param> /// <param name="r">The r.</param> /// <param name="g">The g.</param> /// <param name="b">The b.</param> /// <param name="faceName">Name of the face.</param> /// <param name="fontSize">Size of the font.</param> /// <param name="text">The text.</param> /// <param name="resWidth">Horizontal resolution.</param> /// <param name="resHeight">Vertical resolution.</param> public void DrawText(OpenGL gl, int x, int y, float r, float g, float b, string faceName, float fontSize, string text, double resWidth, double resHeight) { // Get the font size in pixels. var fontHeight = (int)(fontSize * (16.0f / 12.0f)); // Do we have a font bitmap entry for this OpenGL instance and face name? var result = (from fbe in fontBitmapEntries where fbe.HDC == gl.RenderContextProvider.DeviceContextHandle && fbe.HRC == gl.RenderContextProvider.RenderContextHandle && String.Compare(fbe.FaceName, faceName, StringComparison.OrdinalIgnoreCase) == 0 && fbe.Height == fontHeight select fbe).ToList(); // Get the FBE or null. var fontBitmapEntry = result.FirstOrDefault(); // If we don't have the FBE, we must create it. if (fontBitmapEntry == null) { fontBitmapEntry = CreateFontBitmapEntry(gl, faceName, fontHeight); } // Create the appropriate projection matrix. gl.MatrixMode(OpenGL.GL_PROJECTION); gl.PushMatrix(); gl.LoadIdentity(); gl.Ortho(0, resWidth, 0, resHeight, -1, 1); // Create the appropriate modelview matrix. gl.MatrixMode(OpenGL.GL_MODELVIEW); gl.PushMatrix(); gl.LoadIdentity(); gl.Color(r, g, b); gl.RasterPos(x, y); gl.PushAttrib(OpenGL.GL_LIST_BIT | OpenGL.GL_CURRENT_BIT | OpenGL.GL_ENABLE_BIT | OpenGL.GL_TRANSFORM_BIT); gl.Color(r, g, b); gl.Disable(OpenGL.GL_LIGHTING); gl.Disable(OpenGL.GL_TEXTURE_2D); gl.Disable(OpenGL.GL_DEPTH_TEST); gl.RasterPos(x, y); // Set the list base. gl.ListBase(fontBitmapEntry.ListBase); // Create an array of lists for the glyphs. var lists = text.Select(c => (byte)c).ToArray(); // Call the lists for the string. gl.CallLists(lists.Length, lists); gl.Flush(); // Reset the list bit. gl.PopAttrib(); // Pop the modelview. gl.PopMatrix(); // back to the projection and pop it, then back to the model view. gl.MatrixMode(OpenGL.GL_PROJECTION); gl.PopMatrix(); gl.MatrixMode(OpenGL.GL_MODELVIEW); }
public override void Draw(ref OpenGL gl) { if (!TexturesInitialised) { InitializeTexture(ref gl); } // Clear the color and depth buffer. gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); // Load the identity matrix. gl.LoadIdentity(); gl.Enable(OpenGL.GL_TEXTURE_2D); gl.Enable(OpenGL.GL_LIGHTING); gl.Enable(OpenGL.GL_LIGHT0); gl.Enable(OpenGL.GL_DEPTH_TEST); gl.Enable(OpenGL.GL_NORMALIZE); gl.BindTexture(OpenGL.GL_TEXTURE_2D, gtexture[0]); gl.Color(1.0f, 1.0f, 1.0f, 0.1f); gl.Begin(OpenGL.GL_QUADS); gl.FrontFace(OpenGL.GL_FRONT_FACE); gl.TexCoord(1.0f, 1.0f); gl.Vertex(gImage1.Width, gImage1.Height, 1.0f); gl.TexCoord(0.0f, 1.0f); gl.Vertex(0.0f, gImage1.Height, 1.0f); gl.TexCoord(0.0f, 0.0f); gl.Vertex(0.0f, 0.0f, 1.0f); gl.TexCoord(1.0f, 0.0f); gl.Vertex(gImage1.Width, 0.0f, 1.0f); gl.End(); gl.Disable(OpenGL.GL_TEXTURE_2D); gl.MatrixMode(OpenGL.GL_PROJECTION); gl.LoadIdentity(); gl.Ortho(0.0, (double)gImage1.Width, (double)gImage1.Height, 0.0, -1.0, 1.0); gl.MatrixMode(OpenGL.GL_MODELVIEW); gl.Disable(OpenGL.GL_DEPTH_TEST); }
//Handles the Resized event of the openGLControl1 control private void Resized(ref OpenGL gl, double Width, double Height) { // Set the projection matrix. gl.MatrixMode(OpenGL.GL_PROJECTION); // Load the identity. gl.LoadIdentity(); if (!TexturesInitialised) { gl.Ortho(-1, 1, -1, 1, -1, 1); } else { gl.Ortho(0, gImage1.Width, gImage1.Height, 0, -1, 1); } gl.MatrixMode(OpenGL.GL_MODELVIEW); gl.Disable(OpenGL.GL_DEPTH_TEST); // Create a perspective transformation. gl.Perspective(45.0f, (double)Width / (double)Height, 1.0, 1000.0); gl.Viewport(0, 0, (int)Width, (int)Height); // Use the 'look at' helper function to position and aim the camera. gl.LookAt(-5, 5, -5, 0, 0, 0, 0, 1, 0); // Set the modelview matrix. gl.MatrixMode(OpenGL.GL_MODELVIEW); }
private void RecursiveRender(Assimp.Scene scene, Node node, ref OpenGL gl) { Matrix4 m = FromMatrix(node.Transform); m.Transpose(); gl.PushMatrix(); gl.MultMatrix(FloatFromMatrix(m)); if (node.HasMeshes) { foreach (int index in node.MeshIndices) { Mesh mesh = scene.Meshes[index]; ApplyMaterial(m_model.Materials[mesh.MaterialIndex], ref gl); if (mesh.HasNormals) { gl.Enable(OpenGL.GL_LIGHTING); } else { gl.Disable(OpenGL.GL_LIGHTING); } bool hasColors = mesh.HasVertexColors(0); if (hasColors) { gl.Enable(OpenGL.GL_COLOR_MATERIAL); } else { gl.Disable(OpenGL.GL_COLOR_MATERIAL); } bool hasTexCoords = mesh.HasTextureCoords(0); foreach (Assimp.Face face in mesh.Faces) { BeginMode faceMode; switch (face.IndexCount) { case 1: faceMode = BeginMode.Points; break; case 2: faceMode = BeginMode.Lines; break; case 3: faceMode = BeginMode.Triangles; break; default: faceMode = BeginMode.Polygon; break; } gl.Begin(faceMode); for (int i = 0; i < face.IndexCount; i++) { int indice = face.Indices[i]; if (hasColors) { Color4 vertColor = FromColor(mesh.VertexColorChannels[0][indice]); if (mesh.HasNormals) { Vector3 normal = FromVector(mesh.Normals[indice]); gl.Normal(normal.X, normal.Y, normal.Z); } if (hasTexCoords) { Vector3 uvw = FromVector(mesh.TextureCoordinateChannels[0][indice]); gl.TexCoord(uvw.X, 1 - uvw.Y); } Vector3 pos = FromVector(mesh.Vertices[indice]); gl.Vertex(pos.X, pos.Y, pos.Z); } gl.End(); } } } for (int i = 0; i < node.ChildCount; i++) { RecursiveRender(m_model, node.Children[i], ref gl); } } }
/// <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(); } }
void IRenderable.Render(OpenGL gl, RenderMode renderMode) { if (positionBuffer == null || colorBuffer == null) { return; } if (this.shaderProgram == null) { this.shaderProgram = InitShader(gl, renderMode); } if (this.vertexArrayObject == null) { CreateVertexArrayObject(gl, renderMode); } BeforeRendering(gl, renderMode); if (this.RenderGridWireframe && this.vertexArrayObject != null) { //if (wireframeIndexBuffer != null) if (positionBuffer != null && colorBuffer != null && indexBuffer != null) { shaderProgram.SetUniform1(gl, "renderingWireframe", 1.0f);// shader一律上白色。 gl.Disable(OpenGL.GL_LINE_STIPPLE); gl.Disable(OpenGL.GL_POLYGON_STIPPLE); gl.Enable(OpenGL.GL_LINE_SMOOTH); gl.Enable(OpenGL.GL_POLYGON_SMOOTH); gl.ShadeModel(SharpGL.Enumerations.ShadeModel.Smooth); gl.Hint(SharpGL.Enumerations.HintTarget.LineSmooth, SharpGL.Enumerations.HintMode.Nicest); gl.Hint(SharpGL.Enumerations.HintTarget.PolygonSmooth, SharpGL.Enumerations.HintMode.Nicest); gl.PolygonMode(SharpGL.Enumerations.FaceMode.FrontAndBack, SharpGL.Enumerations.PolygonMode.Lines); gl.Enable(OpenGL.GL_PRIMITIVE_RESTART); gl.PrimitiveRestartIndex(uint.MaxValue); gl.Enable(OpenGL.GL_BLEND); gl.BlendFunc(SharpGL.Enumerations.BlendingSourceFactor.SourceAlpha, SharpGL.Enumerations.BlendingDestinationFactor.OneMinusSourceAlpha); gl.BindVertexArray(this.vertexArrayObject[0]); gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, indexBuffer[0]); gl.DrawElements(OpenGL.GL_QUAD_STRIP, this.indexBufferLength, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero); gl.BindVertexArray(0); gl.Disable(OpenGL.GL_BLEND); gl.Disable(OpenGL.GL_PRIMITIVE_RESTART); gl.PolygonMode(SharpGL.Enumerations.FaceMode.FrontAndBack, SharpGL.Enumerations.PolygonMode.Filled); gl.Disable(OpenGL.GL_POLYGON_SMOOTH); } } if (this.RenderGrid && this.vertexArrayObject != null) { if (positionBuffer != null && colorBuffer != null && indexBuffer != null) { shaderProgram.SetUniform1(gl, "renderingWireframe", 0.0f);// shader根据uv buffer来上色。 gl.Enable(OpenGL.GL_PRIMITIVE_RESTART); gl.PrimitiveRestartIndex(uint.MaxValue); gl.Enable(OpenGL.GL_BLEND); gl.BlendFunc(SharpGL.Enumerations.BlendingSourceFactor.SourceAlpha, SharpGL.Enumerations.BlendingDestinationFactor.OneMinusSourceAlpha); gl.BindVertexArray(this.vertexArrayObject[0]); gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, indexBuffer[0]); gl.DrawElements(OpenGL.GL_QUAD_STRIP, this.indexBufferLength, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero); gl.BindVertexArray(0); gl.Disable(OpenGL.GL_BLEND); gl.Disable(OpenGL.GL_PRIMITIVE_RESTART); } } AfterRendering(gl, renderMode); }
private void openGLControl1_OpenGLDraw(object sender, SharpGL.RenderEventArgs args) { SharpGL.OpenGL gl = this.openGLControl.OpenGL; setCamera(gl); //清除深度缓存 gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); //重置当前指定的矩阵为单位矩阵,将当前的用户坐标系的原点移到了屏幕中心 gl.LoadIdentity(); // 光照 // gl.Enable(OpenGL.GL_LIGHTING); //地形 mydem.DrawLand(gl, texture[8]); //把当前坐标系右移3个单位,注意此时是相对上面(-1.5, 0, -6)点定位 gl.Translate(0f, 0f, 0f); gl.Enable(OpenGL.GL_TEXTURE_2D); texture[1].Bind(gl); gl.Color(1f, 1f, 1f, 1f); gl.Begin(OpenGL.GL_QUADS); { //地形 //gl.TexCoord(0, 0); //gl.Vertex(-50.0f, 50.0f, 0.0f); //gl.TexCoord(0, 1); //gl.Vertex(-50.0f, -50.0f, 0.0f); //gl.TexCoord(1, 1); //gl.Vertex(50.0f, -50.0f, 0.0f); //gl.TexCoord(1, 0); //gl.Vertex(50.0f, 50.0f, 0.0f); } gl.End(); gl.Disable(OpenGL.GL_TEXTURE_2D); //水面 gl.Enable(OpenGL.GL_TEXTURE_2D); texture[3].Bind(gl); gl.Color(1f, 1f, 1f, 1f); gl.Begin(OpenGL.GL_QUADS); { gl.TexCoord(0, 0); gl.Vertex(-50.0f, 50.0f, a); gl.TexCoord(0, 1); gl.Vertex(-50.0f, -50.0f, a); gl.TexCoord(1, 1); gl.Vertex(50.0f, -50.0f, a); gl.TexCoord(1, 0); gl.Vertex(50.0f, 50.0f, a); } gl.End(); gl.Disable(OpenGL.GL_TEXTURE_2D); //天空 gl.Enable(OpenGL.GL_TEXTURE_2D); texture[2].Bind(gl); gl.Color(1f, 1f, 1f, 1f); gl.Begin(OpenGL.GL_QUADS); { gl.TexCoord(0, 0); gl.Vertex(-50.0f, 50.0f, 40.00f); gl.TexCoord(0, 1); gl.Vertex(-50.0f, -50.0f, 40f); gl.TexCoord(1, 1); gl.Vertex(50.0f, -50.0f, 40f); gl.TexCoord(1, 0); gl.Vertex(50.0f, 50.0f, 40f); } gl.End(); gl.Disable(OpenGL.GL_TEXTURE_2D); //01 gl.Enable(OpenGL.GL_TEXTURE_2D); texture[4].Bind(gl); gl.Color(1f, 1f, 1f, 1f); gl.Begin(OpenGL.GL_QUADS); { gl.TexCoord(0, 0); gl.Vertex(-50.0f, 50.0f, -1f); gl.TexCoord(0, 1); gl.Vertex(-50.0f, 50.0f, 40f); gl.TexCoord(1, 1); gl.Vertex(-50.0f, -50.0f, 40f); gl.TexCoord(1, 0); gl.Vertex(-50.0f, -50.0f, -1f); } gl.End(); gl.Disable(OpenGL.GL_TEXTURE_2D); //02 gl.Enable(OpenGL.GL_TEXTURE_2D); texture[5].Bind(gl); gl.Color(1f, 1f, 1f, 1f); gl.Begin(OpenGL.GL_QUADS); { gl.TexCoord(0, 0); gl.Vertex(50.0f, 50.0f, -1f); gl.TexCoord(0, 1); gl.Vertex(50.0f, 50.0f, 40f); gl.TexCoord(1, 1); gl.Vertex(50.0f, -50.0f, 40f); gl.TexCoord(1, 0); gl.Vertex(50.0f, -50.0f, -1f); } //03 gl.End(); gl.Disable(OpenGL.GL_TEXTURE_2D); gl.Enable(OpenGL.GL_TEXTURE_2D); texture[6].Bind(gl); gl.Color(1f, 1f, 1f, 1f); gl.Begin(OpenGL.GL_QUADS); { gl.TexCoord(0, 0); gl.Vertex(-50.0f, 50.0f, -1f); gl.TexCoord(0, 1); gl.Vertex(-50.0f, 50.0f, 40f); gl.TexCoord(1, 1); gl.Vertex(50.0f, 50.0f, 40f); gl.TexCoord(1, 0); gl.Vertex(50.0f, 50.0f, -1f); } gl.End(); gl.Disable(OpenGL.GL_TEXTURE_2D); gl.Enable(OpenGL.GL_TEXTURE_2D); texture[7].Bind(gl); gl.Color(1f, 1f, 1f, 1f); gl.Begin(OpenGL.GL_QUADS); { gl.TexCoord(0, 0); gl.Vertex(-50.0f, -50.0f, -1f); gl.TexCoord(0, 1); gl.Vertex(-50.0f, -50.0f, 40f); gl.TexCoord(1, 1); gl.Vertex(50.0f, -50.0f, 40f); gl.TexCoord(1, 0); gl.Vertex(50.0f, -50.0f, -1f); } gl.End(); gl.Disable(OpenGL.GL_TEXTURE_2D); //画树 for (int i = 0; i < treePos.Count; i++) { Vector3d vPos = treePos[i]; drawTree(gl, vPos.dx, vPos.dy, vPos.dz, i); } gl.Flush(); //强制刷新 }
/// <summary> /// 渲染基质 /// </summary> /// <param name="gl"></param> /// <param name="renderMode"></param> private void DoRenderMatrix(OpenGL gl, RenderMode renderMode) { if (this.positionBuffer == null || this.colorBuffer == null) { return; } if (this.RenderGrid && this.matrixVertexArrayObject != null) { shaderProgram.SetUniform1(gl, "renderingWireframe", 0.0f); shaderProgram.SetUniform1(gl, "opacity", this.Opacity); gl.Enable(OpenGL.GL_POLYGON_OFFSET_FILL); gl.PolygonOffset(1.0f, 1.0f); gl.Enable(OpenGL.GL_BLEND); gl.BlendFunc(SharpGL.Enumerations.BlendingSourceFactor.SourceAlpha, SharpGL.Enumerations.BlendingDestinationFactor.OneMinusSourceAlpha); gl.BindVertexArray(matrixVertexArrayObject[0]); switch (this.MatrixType) { case MatrixFormat.Triangle: gl.DrawArrays(this.matrixRenderMode, 0, this.MatrixVertexOrIndexCount); break; case MatrixFormat.Tetrahedron: gl.Enable(OpenGL.GL_PRIMITIVE_RESTART); gl.PrimitiveRestartIndex(uint.MaxValue); gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, this.matrixIndexBuffer[0]); gl.DrawElements(this.matrixRenderMode, this.MatrixVertexOrIndexCount, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero); gl.Disable(OpenGL.GL_PRIMITIVE_RESTART); break; case MatrixFormat.TriangularPrism: // 先渲染三棱柱的上下三角形 gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, this.MatrixVertexOrIndexCount); // 再渲染三棱柱的三个侧面 gl.Enable(OpenGL.GL_PRIMITIVE_RESTART); gl.PrimitiveRestartIndex(uint.MaxValue); gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, this.matrixIndexBuffer[0]); gl.DrawElements(this.matrixRenderMode, this.MatrixVertexOrIndexCount, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero); gl.Disable(OpenGL.GL_PRIMITIVE_RESTART); break; default: break; } gl.BindVertexArray(0); gl.Disable(OpenGL.GL_BLEND); gl.Disable(OpenGL.GL_POLYGON_OFFSET_FILL); } if (this.RenderGridWireframe && this.matrixVertexArrayObject != null) { shaderProgram.SetUniform1(gl, "renderingWireframe", 1.0f); shaderProgram.SetUniform1(gl, "opacity", this.Opacity); gl.PolygonMode(SharpGL.Enumerations.FaceMode.FrontAndBack, SharpGL.Enumerations.PolygonMode.Lines); gl.Enable(OpenGL.GL_BLEND); gl.BlendFunc(SharpGL.Enumerations.BlendingSourceFactor.SourceAlpha, SharpGL.Enumerations.BlendingDestinationFactor.OneMinusSourceAlpha); gl.BindVertexArray(matrixVertexArrayObject[0]); switch (this.MatrixType) { case MatrixFormat.Triangle: gl.DrawArrays(this.matrixRenderMode, 0, this.MatrixVertexOrIndexCount); break; case MatrixFormat.Tetrahedron: gl.Enable(OpenGL.GL_PRIMITIVE_RESTART); gl.PrimitiveRestartIndex(uint.MaxValue); gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, this.matrixIndexBuffer[0]); gl.DrawElements(this.matrixRenderMode, this.MatrixVertexOrIndexCount, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero); gl.Disable(OpenGL.GL_PRIMITIVE_RESTART); break; case MatrixFormat.TriangularPrism: // 先渲染三棱柱的上下三角形 gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, this.MatrixVertexOrIndexCount / 9 * 6); // 再渲染三棱柱的三个侧面 gl.Enable(OpenGL.GL_PRIMITIVE_RESTART); gl.PrimitiveRestartIndex(uint.MaxValue); gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, this.matrixIndexBuffer[0]); gl.DrawElements(this.matrixRenderMode, this.MatrixVertexOrIndexCount, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero); gl.Disable(OpenGL.GL_PRIMITIVE_RESTART); break; default: break; } gl.PolygonMode(SharpGL.Enumerations.FaceMode.FrontAndBack, SharpGL.Enumerations.PolygonMode.Filled); gl.BindVertexArray(0); gl.Disable(OpenGL.GL_BLEND); } }
protected void AfterRendering(OpenGL gl, RenderMode renderMode) { shaderProgram.Unbind(gl); gl.Disable(OpenGL.GL_BLEND); gl.Disable(OpenGL.GL_VERTEX_PROGRAM_POINT_SIZE); gl.Disable(OpenGL.GL_POINT_SPRITE_ARB); gl.Disable(OpenGL.GL_POINT_SMOOTH); gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0); }
/// <summary> /// Render to the provided instance of OpenGL. /// </summary> /// <param name="gl">The OpenGL instance.</param> /// <param name="renderMode">The render mode.</param> public virtual void Render(OpenGL gl, RenderMode renderMode) { // If we're frozen, use the helper. if (freezableHelper.IsFrozen) { freezableHelper.Render(gl); return; } // Go through each face. foreach (Face face in faces) { // If the face has its own material, push it. if (face.Material != null) { face.Material.Push(gl); } // Begin drawing a polygon. if (face.Indices.Count == 2) { gl.Begin(OpenGL.GL_LINES); } else { gl.Begin(OpenGL.GL_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(); // todo use auto smoothing instead // Add it to the normals, setting the index for next time. normals.Add(vNormal); index.Normal = normals.Count - 1; gl.Normal(vNormal); } } // Set the vertex. gl.Vertex(vertices[index.Vertex]); } gl.End(); // If the face has its own material, pop it. if (face.Material != null) { face.Material.Pop(gl); } } // Draw normals if we have to. if (drawNormals) { // Set the colour to red. gl.PushAttrib(OpenGL.GL_ALL_ATTRIB_BITS); gl.Color(1, 0, 0, 1); gl.Disable(OpenGL.GL_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. Vertex normal = normals[index.Normal]; Vertex vertex2 = vertex + normal; gl.Begin(OpenGL.GL_LINES); gl.Vertex(vertex); gl.Vertex(vertex2); gl.End(); } } } // Restore the attributes. 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(); }
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> /// 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> /// 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 override void OnRender(OpenGL gl) { gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); gl.ClearColor(0.137f, 0.121f, 0.125f, 0f); _graph.Render(); gl.Disable(OpenGL.GL_DEPTH_TEST); using (new Bind(_shader)) { gl.UniformMatrix4(_modelUniform, 1, false, Mat4.Identity.ToColumnMajorArray()); gl.PointSize(10f); gl.Begin(BeginMode.Points); gl.Vertex(6, 0); gl.End(); } gl.PointSize(5f); _points.Render(); gl.Enable(OpenGL.GL_DEPTH_TEST); }
private void DoRenderFraction(OpenGL gl, RenderMode renderMode) { if (this.fractionPositionBuffer == null || this.fractionUVBuffer == null) { return; } if (this.RenderFraction && this.fractionVertexArrayObject != null) { shaderProgram.SetUniform1(gl, "renderingWireframe", 0.0f); gl.Enable(OpenGL.GL_POLYGON_OFFSET_FILL); gl.PolygonOffset(1.0f, 1.0f); gl.BindVertexArray(fractionVertexArrayObject[0]); switch (this.FractionType) { case FractureFormat.Line: float[] originalWidth = new float[1]; gl.GetFloat(SharpGL.Enumerations.GetTarget.LineWidth, originalWidth); gl.LineWidth(this.FractionLineWidth); gl.DrawArrays(OpenGL.GL_LINES, 0, this.FractionVertexCount); gl.LineWidth(originalWidth[0]); break; case FractureFormat.Triange: gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, this.FractionVertexCount); break; case FractureFormat.Quad: gl.DrawArrays(OpenGL.GL_QUADS, 0, this.FractionVertexCount); break; default: throw new NotImplementedException(); //break; } gl.BindVertexArray(0); gl.Disable(OpenGL.GL_POLYGON_OFFSET_FILL); } if (this.renderFractionWireframe && this.fractionVertexArrayObject != null) { shaderProgram.SetUniform1(gl, "renderingWireframe", 1.0f); gl.BindVertexArray(fractionVertexArrayObject[0]); { gl.PolygonMode(SharpGL.Enumerations.FaceMode.FrontAndBack, SharpGL.Enumerations.PolygonMode.Lines); switch (this.FractionType) { case FractureFormat.Line: gl.DrawArrays(OpenGL.GL_LINES, 0, this.FractionVertexCount); break; case FractureFormat.Triange: gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, this.FractionVertexCount); break; case FractureFormat.Quad: gl.DrawArrays(OpenGL.GL_QUADS, 0, this.FractionVertexCount); break; default: break; } gl.PolygonMode(SharpGL.Enumerations.FaceMode.FrontAndBack, SharpGL.Enumerations.PolygonMode.Filled); } gl.BindVertexArray(0); } }
protected void AfterRendering(OpenGL gl, RenderMode renderMode) { gl.Disable(OpenGL.GL_POLYGON_SMOOTH); shaderProgram.Unbind(gl); gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0); gl.Disable(OpenGL.GL_TEXTURE_2D); }
/// <summary> /// Draw a mesh. /// </summary> /// <param name="gl">OpenGL handler.</param> /// <param name="buildingObj">The mesh.</param>BuildingObjectLib3DS _object, private void DrawMesh(OpenGL gl, Lib3dsMesh thisMesh, Hashtable textures, List<Lib3dsMaterial> matrials, DrawType type) { if (thisMesh == null || thisMesh.nfaces == 0) return; // Draw all the faces in this mesh. for (int j = 0; j < thisMesh.faces.Count; j++) { Lib3dsFace thisFace = thisMesh.faces[j]; float transparency = matrials[thisFace.material].transparency; //float[] fogColor = new float[4] { 0.5f, 0.5f, 0.5f, 1.0f }; //float[] LightAmbient = new float[4] { 0.5f, 0.5f, 0.5f, 1.0f }; //float[] LightDiffuse = new float[4] { 1.0f, 1.0f, 1.0f, 1.0f }; //float[] LightPosition = new float[4] { 0.0f, 0.0f, 2.0f, 1.0f }; switch (type) { case DrawType.WireFrame: gl.PolygonMode(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_LINE); IsDrawTexture = false; gl.Color(0.5f, 0.5f, 0.5f, 0.5f); gl.LineWidth(1.5f); break; case DrawType.Full: IsDrawTexture = BindTexture(gl, textures, matrials[thisFace.material].name); gl.PolygonMode(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_FILL); break; case DrawType.Face: gl.PolygonMode(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_FILL); IsDrawTexture = false; break; case DrawType.Translucent: IsDrawTexture = BindTexture(gl, textures, matrials[thisFace.material].name); gl.PolygonMode(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_FILL); gl.Enable(OpenGL.GL_BLEND); gl.BlendFunc(OpenGL.GL_SRC_ALPHA,OpenGL.GL_ONE_MINUS_SRC_ALPHA); //gl.Enable(OpenGL.GL_TEXTURE_2D); // Enable Texture Mapping //gl.ShadeModel(OpenGL.GL_SMOOTH); // Enable Smooth Shading //gl.ClearColor(0.5f,0.5f,0.5f,1.0f); // We'll Clear To The Color Of The Fog //gl.ClearDepth(1.0f); // Depth Buffer Setup //gl.Enable(OpenGL.GL_DEPTH_TEST); // Enables Depth Testing //gl.DepthFunc(OpenGL.GL_LEQUAL); // The Type Of Depth Testing To Do //gl.Hint(OpenGL.GL_PERSPECTIVE_CORRECTION_HINT, OpenGL.GL_NICEST); // Really Nice Perspective Calculations //gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_AMBIENT, LightAmbient); // Setup The Ambient Light //gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light //gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_POSITION,LightPosition); // Position The Light //gl.Enable(OpenGL.GL_LIGHT1); //gl.Fog(OpenGL.GL_FOG_COLOR, fogColor);//设置雾颜色,f是一个指定颜色的数组float f[4] //gl.Fog(OpenGL.GL_FOG_DENSITY, 0.85f); // 设置雾的密度 //gl.Hint(OpenGL.GL_FOG_HINT, OpenGL.GL_DONT_CARE); // 设置系统如何计算雾气 //gl.Fog(OpenGL.GL_FOG_START, 0.01f);//设置雾从多远开始 //gl.Fog(OpenGL.GL_FOG_END, 100.0f);//设置雾从多远结束 //gl.Fog(OpenGL.GL_FOG_MODE, OpenGL.GL_LINEAR);//设置使用哪种雾,共有三中雾化模式 //gl.Enable(OpenGL.GL_FOG);//打开雾效果 transparency = 0.2f; break; default: gl.PolygonMode(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_FILL); IsDrawTexture = false; break; } if (type != DrawType.WireFrame) { gl.Color(matrials[thisFace.material].diffuse[0], matrials[thisFace.material].diffuse[1], matrials[thisFace.material].diffuse[2], matrials[thisFace.material].transparency); } gl.Begin(OpenGL.GL_TRIANGLES); for (int k = 0; k != 3; ++k) { int index = thisFace.index[k]; if (IsDrawTexture) gl.TexCoord(thisMesh.texcos[index].s, thisMesh.texcos[index].t); gl.Vertex(thisMesh.vertices[index].x / 20, thisMesh.vertices[index].z / 20, -thisMesh.vertices[index].y / 20); } gl.End(); if(type == DrawType.Translucent) gl.Disable(OpenGL.GL_BLEND); } }
/// <summary> /// Creates the display list. This function draws the /// geometry as well as compiling it. /// </summary> public void CreateDisplayList(OpenGL gl, int x_largura, int y_profundidade, int z_altura) { //if (x_largura == 0 && y_profundidade == 0 && z_altura == 0) //{ // x_largura = 10; // y_profundidade = 7; // z_altura = 4; //} // Create the display list. displayList = new DisplayList(); // Generate the display list and displayList.Generate(gl); displayList.New(gl, DisplayList.DisplayListMode.CompileAndExecute); // Push attributes, set the color. gl.PushAttrib(OpenGL.GL_CURRENT_BIT | OpenGL.GL_ENABLE_BIT | OpenGL.GL_LINE_BIT); gl.Disable(OpenGL.GL_LIGHTING); gl.Disable(OpenGL.GL_TEXTURE_2D); gl.LineWidth(1.4f); // Draw the grid lines. gl.Begin(OpenGL.GL_LINES); float x_inicial = x_largura / (float)2.0; float y_inicial = y_profundidade / (float)2.0; if (true) { //Base vertical for (int i = -(int)x_inicial; i <= x_largura - x_inicial; i++) { try { float fcol = ((i % (x_largura / (float)2.0)) == 0) ? 0.3f : 0.15f; gl.Color(fcol, fcol, fcol); } catch (Exception) { } gl.Vertex(i, -y_inicial, 0); gl.Vertex(i, y_profundidade - y_inicial, 0); } //Base horizontal for (int i = -(int)y_inicial; i <= y_profundidade - y_inicial; i++) { try { float fcol = ((i % (x_largura / (float)2.0)) == 0) ? 0.3f : 0.15f; gl.Color(fcol, fcol, fcol); } catch (Exception) { } gl.Vertex(-x_inicial, i, 0); gl.Vertex(x_largura - x_inicial, i, 0); } //Parede direita horizontal for (int i = 0; i <= z_altura; i++) { try { float fcol = ((i % (z_altura / (float)2.0)) == 0) ? 0.3f : 0.15f; gl.Color(fcol, fcol, fcol); } catch (Exception) { } gl.Vertex(x_largura - x_inicial, -y_inicial, i); gl.Vertex(x_largura - x_inicial, y_profundidade - y_inicial, i); } //Parede esquerda horizontal for (int i = 0; i <= z_altura; i++) { try { float fcol = ((i % (z_altura / (float)2.0)) == 0) ? 0.3f : 0.15f; gl.Color(fcol, fcol, fcol); } catch (Exception) { } gl.Vertex(-x_inicial, y_profundidade - y_inicial, i); gl.Vertex(x_largura - x_inicial, y_profundidade - y_inicial, i); } //Parede direita vertical for (int i = -(int)y_inicial; i <= y_profundidade - y_inicial; i++) { try { float fcol = ((i % (x_largura / (float)2.0)) == 0) ? 0.3f : 0.15f; gl.Color(fcol, fcol, fcol); } catch (Exception) { } gl.Vertex(x_largura - x_inicial, i, 0); gl.Vertex(x_largura - x_inicial, i, z_altura); } //Parede esquerda vertical for (int i = -(int)x_inicial; i <= x_largura - x_inicial; i++) { try { float fcol = ((i % (x_largura / (float)2.0)) == 0) ? 0.3f : 0.15f; gl.Color(fcol, fcol, fcol); } catch (Exception) { } gl.Vertex(i, y_profundidade - y_inicial, 0); gl.Vertex(i, y_profundidade - y_inicial, z_altura); } } else { int minimum_base_value = -7; int maximum_base_value = 7; int minimum_height_value = 0; int maximum_height_value = 4; for (int i = minimum_base_value; i <= maximum_base_value; i++) { float fcol = ((i % maximum_base_value) == 0) ? 0.3f : 0.15f; gl.Color(fcol, fcol, fcol); gl.Vertex(i, minimum_base_value, 0); gl.Vertex(i, maximum_base_value, 0); gl.Vertex(minimum_base_value, i, 0); gl.Vertex(maximum_base_value, i, 0); } for (int i = minimum_height_value; i <= maximum_base_value + maximum_base_value; i++) { float fcol = ((i % maximum_base_value) == 0) ? 0.3f : 0.15f; gl.Color(fcol, fcol, fcol); gl.Vertex(maximum_base_value, i - maximum_base_value, minimum_height_value); gl.Vertex(maximum_base_value, i - maximum_base_value, maximum_height_value); if (i <= maximum_height_value) { gl.Vertex(maximum_base_value, minimum_base_value, i); gl.Vertex(maximum_base_value, maximum_base_value, i); } } for (int i = minimum_height_value; i <= maximum_base_value + maximum_base_value; i++) { float fcol = ((i % maximum_base_value) == 0) ? 0.3f : 0.15f; gl.Color(fcol, fcol, fcol); gl.Vertex(i - maximum_base_value, maximum_base_value, minimum_height_value); gl.Vertex(i - maximum_base_value, maximum_base_value, maximum_height_value); if (i <= maximum_height_value) { gl.Vertex(minimum_base_value, maximum_base_value, i); gl.Vertex(maximum_base_value, maximum_base_value, i); } } } gl.End(); // Restore attributes. gl.PopAttrib(); // End the display list. displayList.End(gl); }
/// <summary> /// Create device and renderbuffer, initialize NV_DX_interop and start rendering. /// </summary> private void StartRendering() { _gl = new OpenGL(); _hwnd = new HwndSource(0, 0, 0, 0, 0, "test", IntPtr.Zero).Handle; _gl.Create(SharpGL.Version.OpenGLVersion.OpenGL2_1, RenderContextType.HiddenWindow, 1, 1, 32, _hwnd); // Set the most basic OpenGL styles. _gl.ShadeModel(OpenGL.GL_SMOOTH); _gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f); _gl.ClearDepth(1.0f); _gl.Enable(OpenGL.GL_BLEND); _gl.Disable(OpenGL.GL_DEPTH_TEST); _gl.Enable(OpenGL.GL_TEXTURE_2D); _gl.TexEnv(OpenGL.GL_TEXTURE_ENV, OpenGL.GL_TEXTURE_ENV_MODE, OpenGL.GL_REPLACE); _gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA); //_gl.Enable(OpenGL.GL_MULTISAMPLE); //_gl.Enable(OpenGL.GL_MULTISAMPLE_ARB); _gl.Hint(0x8534, OpenGL.GL_FASTEST); ResizeRendering(); // leverage the Rendering event of WPF's composition target to // update the custom D3D scene CompositionTarget.Rendering += OnRenderOpenGL; if (GLInitialize != null) GLInitialize(this, new EventArgs()); }
/// <summary> /// Draws the text. /// </summary> /// <param name="gl">The gl.</param> /// <param name="x">The x.</param> /// <param name="y">The y.</param> /// <param name="r">The r.</param> /// <param name="g">The g.</param> /// <param name="b">The b.</param> /// <param name="faceName">Name of the face.</param> /// <param name="fontSize">Size of the font.</param> /// <param name="text">The text.</param> public void DrawText(OpenGL gl, int x, int y, float r, float g, float b, string faceName, float fontSize, string text) { // Get the font size in pixels. int fontHeight = (int)(fontSize * (16.0f / 12.0f)); // Do we have a font bitmap entry for this OpenGL instance and face name? var result = from fbe in fontBitmapEntries where fbe.HDC == gl.RenderContextProvider.DeviceContextHandle && fbe.HRC == gl.RenderContextProvider.RenderContextHandle && string.Compare(fbe.FaceName, faceName, true) == 0 && fbe.Height == fontHeight select fbe; // Get the FBE or null. FontBitmapEntry fontBitmapEntry = result == null || result.Count() == 0 ? null : result.First(); // If we don't have the FBE, we must create it. if (fontBitmapEntry == null) fontBitmapEntry = CreateFontBitmapEntry(gl, faceName, fontHeight); double width = gl.RenderContextProvider.Width; double height = gl.RenderContextProvider.Height; double aspect_ratio = width / height; // Create the appropriate projection matrix. gl.MatrixMode(OpenGL.GL_PROJECTION); gl.PushMatrix(); gl.LoadIdentity(); int[] viewport = new int[4]; gl.GetInteger(OpenGL.GL_VIEWPORT, viewport); //gl.Ortho2D(viewport[0], viewport[2], viewport[1], viewport[3]); gl.Ortho(0, width, 0, height, -1, 1); // Create the appropriate modelview matrix. gl.MatrixMode(OpenGL.GL_MODELVIEW); gl.PushMatrix(); gl.LoadIdentity(); //gl.Translate(0, 0, -5); //gl.RasterPos(x, y); gl.Color(r, g, b); gl.RasterPos(x, y); gl.PushAttrib(OpenGL.GL_LIST_BIT | OpenGL.GL_CURRENT_BIT | OpenGL.GL_ENABLE_BIT | OpenGL.GL_TRANSFORM_BIT); gl.Color(r, g, b); gl.Disable(OpenGL.GL_LIGHTING); gl.Disable(OpenGL.GL_TEXTURE_2D); gl.Disable(OpenGL.GL_DEPTH_TEST); gl.RasterPos(x, y); /** gl.Enable(OpenGL.GL_BLEND); gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA); */ // Set the list base. gl.ListBase(fontBitmapEntry.ListBase); // Create an array of lists for the glyphs. List<byte> lists = new List<byte>(); foreach(char c in text) lists.Add((byte)c); // Call the lists for the string. gl.CallLists(lists.Count, lists.ToArray()); gl.Flush(); // Reset the list bit. gl.PopAttrib(); // Pop the modelview. gl.PopMatrix(); // back to the projection and pop it, then back to the model view. gl.MatrixMode(OpenGL.GL_PROJECTION); gl.PopMatrix(); gl.MatrixMode(OpenGL.GL_MODELVIEW); }
public static void MyGrid(SharpGL.OpenGL gl, int iLo, int iHi, int jLo, int jHi) { const String ColumnAnnotationFont = "Times New Roman"; const int Ilo = 0; const int Ihi = 10; const int Jlo = 0; const int Jhi = 10; gl.PushAttrib(SharpGL.OpenGL.GL_CURRENT_BIT | SharpGL.OpenGL.GL_ENABLE_BIT | SharpGL.OpenGL.GL_LINE_BIT); gl.Disable(SharpGL.OpenGL.GL_LIGHTING); gl.Disable(SharpGL.OpenGL.GL_TEXTURE_2D); gl.LineWidth(1.0f); // Draw the grid lines. gl.Begin(SharpGL.OpenGL.GL_LINES); for (int i = iLo; i <= iHi; i++) { float fcol = ((i % 10) == 0) ? 0.3f : 0.15f; gl.Color(fcol, fcol, fcol); gl.Vertex(i, jLo, 0); gl.Vertex(i, jHi, 0); } for (int j = jLo; j <= jHi; j++) { float fcol = ((j % 10) == 0) ? 0.3f : 0.15f; gl.Vertex(iLo, j, 0); gl.Vertex(iHi, j, 0); } gl.End( ); // Draw Spreadsheet boarders for (int i = iLo; i < iHi; i++) { gl.PushMatrix( ); gl.Translate(i + 0.25, jLo - 0.5, 0); gl.Scale(0.5d, 0.5d, 0.5d); String Annotation = String.Format("{0,2} ", i); gl.DrawText3D(ColumnAnnotationFont, 8.0f, 0.0f, 0.1f, Annotation); gl.PopMatrix( ); } for (int i = iLo; i < iHi; i++) { gl.PushMatrix( ); gl.Translate(i + .25, jHi + 0.5, 0); gl.Scale(0.5d, 0.5d, 0.5d); String Annotation = String.Format("{0,2} ", i); gl.DrawText3D(ColumnAnnotationFont, 8.0f, 0.0f, 0.1f, Annotation); gl.PopMatrix( ); } for (int j = jLo; j < jHi; j++) { gl.PushMatrix( ); gl.Translate(iLo - 0.5, j + 0.25, 0); gl.Scale(0.5d, 0.5d, 0.5d); String Annotation = String.Format("{0,2} ", j + 1); gl.DrawText3D(ColumnAnnotationFont, 8.0f, 0.0f, 0.1f, Annotation); gl.PopMatrix( ); } for (int j = jLo; j < jHi; j++) { gl.PushMatrix( ); gl.Translate(iHi + 0.5, j + 0.25, 0); gl.Scale(0.5d, 0.5d, 0.5d); String Annotation = String.Format("{0,2} ", j + 1); gl.DrawText3D(ColumnAnnotationFont, 8.0f, 0.0f, 0.1f, Annotation); gl.PopMatrix( ); } gl.LineWidth(2); gl.Color(.5, .2, .1); gl.Begin(SharpGL.OpenGL.GL_LINE_LOOP); gl.Vertex(Jlo, Ilo); gl.Vertex(Jlo, Ihi + 1); gl.Vertex(Jhi + 1, Ihi + 1); gl.Vertex(Jhi + 1, Ilo); gl.End( ); gl.PopAttrib( ); }