在此记录一下SharpGL最初创建的程序
在此记录一下SharpGL最初创建的程序。完整工程在此。
1 /// <summary> 2 /// The main form class. 3 /// </summary> 4 public partial class SharpGLForm : Form 5 { 6 /// <summary> 7 /// Initializes a new instance of the <see cref="SharpGLForm"/> class. 8 /// </summary> 9 public SharpGLForm() 10 { 11 InitializeComponent(); 12 } 13 14 /// <summary> 15 /// Handles the OpenGLDraw event of the openGLControl control. 16 /// </summary> 17 /// <param name="sender">The source of the event.</param> 18 /// <param name="e">The <see cref="System.Windows.Forms.PaintEventArgs"/> instance containing the event data.</param> 19 private void openGLControl_OpenGLDraw(object sender, PaintEventArgs e) 20 { 21 // Get the OpenGL object. 22 OpenGL gl = openGLControl.OpenGL; 23 24 // Clear the color and depth buffer. 25 gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); 26 27 // Load the identity matrix. 28 gl.LoadIdentity(); 29 30 // Rotate around the Y axis. 31 gl.Rotate(rotation, 0.0f, 1.0f, 0.0f); 32 33 // Draw a coloured pyramid. 34 gl.Begin(OpenGL.GL_TRIANGLES); 35 gl.Color(1.0f, 0.0f, 0.0f); 36 gl.Vertex(0.0f, 1.0f, 0.0f); 37 gl.Color(0.0f, 1.0f, 0.0f); 38 gl.Vertex(-1.0f, -1.0f, 1.0f); 39 gl.Color(0.0f, 0.0f, 1.0f); 40 gl.Vertex(1.0f, -1.0f, 1.0f); 41 gl.Color(1.0f, 0.0f, 0.0f); 42 gl.Vertex(0.0f, 1.0f, 0.0f); 43 gl.Color(0.0f, 0.0f, 1.0f); 44 gl.Vertex(1.0f, -1.0f, 1.0f); 45 gl.Color(0.0f, 1.0f, 0.0f); 46 gl.Vertex(1.0f, -1.0f, -1.0f); 47 gl.Color(1.0f, 0.0f, 0.0f); 48 gl.Vertex(0.0f, 1.0f, 0.0f); 49 gl.Color(0.0f, 1.0f, 0.0f); 50 gl.Vertex(1.0f, -1.0f, -1.0f); 51 gl.Color(0.0f, 0.0f, 1.0f); 52 gl.Vertex(-1.0f, -1.0f, -1.0f); 53 gl.Color(1.0f, 0.0f, 0.0f); 54 gl.Vertex(0.0f, 1.0f, 0.0f); 55 gl.Color(0.0f, 0.0f, 1.0f); 56 gl.Vertex(-1.0f, -1.0f, -1.0f); 57 gl.Color(0.0f, 1.0f, 0.0f); 58 gl.Vertex(-1.0f, -1.0f, 1.0f); 59 gl.End(); 60 61 // Nudge the rotation. 62 rotation += 3.0f; 63 } 64 65 66 67 /// <summary> 68 /// Handles the OpenGLInitialized event of the openGLControl control. 69 /// </summary> 70 /// <param name="sender">The source of the event.</param> 71 /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 72 private void openGLControl_OpenGLInitialized(object sender, EventArgs e) 73 { 74 // TODO: Initialise OpenGL here. 75 76 // Get the OpenGL object. 77 OpenGL gl = openGLControl.OpenGL; 78 79 // Set the clear color. 80 gl.ClearColor(0, 0, 0, 0); 81 } 82 83 /// <summary> 84 /// Handles the Resized event of the openGLControl control. 85 /// </summary> 86 /// <param name="sender">The source of the event.</param> 87 /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 88 private void openGLControl_Resized(object sender, EventArgs e) 89 { 90 // TODO: Set the projection matrix here. 91 92 // Get the OpenGL object. 93 OpenGL gl = openGLControl.OpenGL; 94 95 // Set the projection matrix. 96 gl.MatrixMode(OpenGL.GL_PROJECTION); 97 98 // Load the identity. 99 gl.LoadIdentity(); 100 101 // Create a perspective transformation. 102 gl.Perspective(60.0f, (double)Width / (double)Height, 0.01, 100.0); 103 104 // Use the 'look at' helper function to position and aim the camera. 105 gl.LookAt(-5, 5, -5, 0, 0, 0, 0, 1, 0); 106 107 // Set the modelview matrix. 108 gl.MatrixMode(OpenGL.GL_MODELVIEW); 109 } 110 111 /// <summary> 112 /// The current rotation. 113 /// </summary> 114 private float rotation = 0.0f; 115 }
微信扫码,自愿捐赠。天涯同道,共谱新篇。
微信捐赠不显示捐赠者个人信息,如需要,请注明联系方式。 |