(a) Platform construction and OpenGL's Hello World
OpenGL is the 3d drawing API, Microsoft pin and it competes to launch D3D, which is the 3d function in the most common Directorx component when playing the game. So don't expect Windows to provide the latest support for OpenGL.
The development environment of C # also does not encapsulate OpenGL components, and OpenGL's official DLL is for c,c++ development. To use it in C # you have to import DLL functions yourself. But I'm sure you won't do that, or it's convenient to introduce a third-party OpenGL library.
SHARPGL is the OpenGL library used in C #, the reason why it is selected, because it last updated more recent, online C + + tutorials are also the most, and this library is a successful commercial development application, can rest assured that use.
Source:
Http://www.oschina.net/p/sharpgl
What about OpenGL? For bloggers, learn it is out of need, want to do 3D simulation, perhaps you are very young, have a lot of spare time, then you can use it to write a CS to Shuangshuang. But bloggers still think that learning things to economic efficiency and work need two forces as the driving force, otherwise do not learn, in this knowledge of the age of explosion, in fact, it is necessary to promote saving time, as far as possible to use the blade.
We use VS2010 C # to build a blank Windows Forms application
First, you can see 4 controls on the panel by introducing the 3 DLLs (except Serialization.dll) in the downloaded SHARPGL into C #, and then selecting the option SharpGL.Winforms.dll. One of the Openglcontrol can play it on the form. It is equivalent to OpenGL's artboard.
If you drag and drop the Openglcontrol onto the form, the following picture is unfortunate.
Well, mostly because of the vs2010 you're using, the default framework is. NET Framework 4 Client profile, so switch to. NET Framework 4.
The source code is as follows: explained in detail
1 usingSystem;2 usingSystem.Windows.Forms;3 usingSHARPGL;4 5 namespaceblanktest6 {7 Public Partial classForm1:form8 {9 Ten PublicForm1 () One { A InitializeComponent (); - - } the - Private voidForm1_Load (Objectsender, EventArgs e) - { - + } - + Private voidOpenglcontrol1_openglinitialized (Objectsender, EventArgs e) A { atOpenGL gl =Openglcontrol1.opengl; -Gl. Clearcolor (0,0,0,0); - } - - Private voidOpenglcontrol1_resize (Objectsender, EventArgs e) - { inOpenGL gl =Openglcontrol1.opengl; - to //sets the current matrix mode, applying subsequent matrix operations to the projection matrix + GL. Matrixmode (opengl.gl_projection); - the //resets the current specified matrix to the unit matrix, moving the origin of the current user coordinate system to the center of the screen * GL. Loadidentity (); $ Panax Notoginseng //Create a Perspective projection transformation -Gl. Perspective (30.0f, (Double) Width/(Double) Height,5,100.0); the + //Viewpoint Transformation AGl. LookAt (-5,5, -5,0,0,0,0,1,0); the + //sets the current matrix as the Model view matrix - GL. Matrixmode (Opengl.gl_modelview); $ } $ - Private voidOpenglcontrol1_opengldraw (Objectsender, PaintEventArgs e) - { theSharpgl.opengl gl = This. Openglcontrol1.opengl; - //clear the depth cacheWuyiGl. Clear (Opengl.gl_color_buffer_bit |opengl.gl_depth_buffer_bit); the - //resets the current specified matrix to the unit matrix, moving the origin of the current user coordinate system to the center of the screen Wu GL. Loadidentity (); - About //Axis transform position to ( -1.5,0,-6) $Gl. Translate (-1.5f, 0f,-6f); - - GL. Begin (opengl.gl_triangles); - { A //Vertex +Gl. Vertex (0.0f,1.0f,0.0f); the //Left Endpoint -Gl. Vertex (-1.0f, -1.0f,0.0f); $ //Right Endpoint theGl. Vertex (1.0f, -1.0f,0.0f); the } the GL. End (); the - //move the current coordinate system to the right by 3 units, note that this is relative to the top ( -1.5,0,-6) point positioning in GL. Translate (3f, 0f, 0f); the the GL. Begin (opengl.gl_quads); About { theGl. Vertex (-1.0f,1.0f,0.0f); theGl. Vertex (1.0f,1.0f,0.0f); theGl. Vertex (1.0f, -1.0f,0.0f); +Gl. Vertex (-1.0f, -1.0f,0.0f); - } the GL. End ();BayiGl. Flush ();//Force Refresh the the } - } -}
Openglcontrol1_openglinitialized,
Openglcontrol1_resiz,
Openglcontrol1_opengldraw
These three are all standard events of the control openGLControl1.
The effect is like this.
The whole lot of code, but see to draw this thing, is not some small disappointment? Don't know what's different from GDI +, right?
In fact, the difference is that GDI + is pure 2D, and the above two basic graphics is not a single, but in 3D space, although it is only a piece, but can also be rotated in X, Y and z direction arbitrary OH.
This example is simple, or attach the source code bar! So as not to affect the mood. ^_^
This section source code download
SHARPGL Learning Notes (i) Platform build and OpenGL's Hello World