SharpGL learning notes (1) platform construction and Opengl hello World and sharpgl learning notes

Source: Internet
Author: User

SharpGL learning notes (1) platform construction and Opengl hello World and sharpgl learning notes

 

(1) platform construction and Opengl hello World

 

OpenGL is the 3d drawing API. Microsoft and it compete to launch D3D, which is the most common 3d function in DirectorX components when playing games. Therefore, do not expect windows to provide the latest support for Opengl.

C #'s development environment does not encapsulate Opengl components. Opengl's official dll is for C and C ++ development. To use it in C #, you must import the DLL function yourself. But I believe you will not do this. It is convenient to introduce a third-party Opengl library.

 

SharpGL is the OpenGL library used by C #. The reason for choosing SharpGL is that it is updated in a short time and has the most C ++ tutorials on the Internet, in addition, this database is successfully developed for commercial applications and can be safely used.

Source code:

Http://www.oschina.net/p/sharpgl

 

What can OpenGL do? For bloggers, they want to do 3D simulation out of their needs. Maybe you are young and have a lot of spare time, so you can use it to write a CS. However, bloggers still believe that learning things should be driven by economic benefits and work needs. Otherwise, they should not learn things. In this age of explosive knowledge, it is necessary to advocate time saving, try to use time on the cutting edge.

 

We use vs2010 C # To build a blank windows form application.

 

First, put the three dll files in the downloaded SharpGL (except for serialization. dll) Introduce C #, and then select SharpGL for "select items. winforms. dll, You can see four controls on the panel. Here, OpenGLControl can play it to the Form. It is equivalent to Opengl canvas.

 

 

If you drag and drop OpenGLControl to Form, the following figure appears unfortunately.

Most of the reason is that the default Framework of vs2010 is. Net Framework 4 Client profile. Switch to. Net Framework 4.

 

The source code is as follows:

1 using System; 2 using System. windows. forms; 3 using SharpGL; 4 5 namespace blankTest 6 {7 public partial class Form1: Form 8 {9 10 public Form1 () 11 {12 InitializeComponent (); 13 14} 15 16 private void Form1_Load (object sender, EventArgs e) 17 {18 19} 20 21 private void openGLControl1_OpenGLInitialized (object sender, EventArgs e) 22 {23 OpenGL gl = openGLControl1.OpenGL; 24 gl. clearColor (0, 0, 0, 0); 25} 26 27 private void openGLControl1_Resize (object sender, EventArgs e) 28 {29 OpenGL gl = openGLControl1.OpenGL; 30 31 // set the current matrix mode and apply the subsequent matrix operations to the projection matrix 32 gl. matrixMode (OpenGL. GL_PROJECTION); 33 34 // reset the current specified matrix as the unit matrix, and move the origin of the current user coordinate system to the center of the screen 35 gl. loadIdentity (); 36 37 // creates a pivot projection transformation 38 gl. perspective (30366f, (double) Width/(double) Height, 5,100.0); 39 40 // viewpoint transformation 41 gl. lookAt (-5, 5,-5, 0, 0, 0, 0, 1, 0); 42 43 // set the current matrix to model view matrix 44 gl. matrixMode (OpenGL. GL_MODELVIEW); 45} 46 47 private void openGLControl1_OpenGLDraw (object sender, PaintEventArgs e) 48 {49 SharpGL. openGL gl = this. openGLControl1.OpenGL; 50 // clear depth cache 51 gl. clear (OpenGL. GL_COLOR_BUFFER_BIT | OpenGL. GL_DEPTH_BUFFER_BIT); 52 53 // reset the current specified matrix as the unit matrix and move the origin of the current user coordinate system to the center of the screen 54 gl. loadIdentity (); 55 56 // coordinate axis to (-1.5, 0,-6) 57 gl. translate (-1.5f, 0f,-6f); 58 59 gl. begin (OpenGL. GL_TRIANGLES); 60 {61 // vertex 62 gl. vertex (0.0f, 1.0f, 0.0f); 63 // left endpoint 64 gl. vertex (-1.0f,-1.0f, 0.0f); 65 // right endpoint 66 gl. vertex (1.0f,-1.0f, 0.0f); 67} 68 gl. end (); 69 70 // shifts the current coordinate system to three units to the right. Note that the position is 71 gl relative to the above (-1.5, 0,-6. translate (3f, 0f, 0f); 72 73 gl. begin (OpenGL. GL_QUADS); 74 {75 gl. vertex (-1.0f, 1.0f, 0.0f); 76 gl. vertex (1.0f, 1.0f, 0.0f); 77 gl. vertex (1.0f,-1.0f, 0.0f); 78 gl. vertex (-1.0f,-1.0f, 0.0f); 79} 80 gl. end (); 81 gl. flush (); // force refresh 82 83} 84} 85}

 

openGLControl1_OpenGLInitialized,
openGLControl1_Resiz,
OpenGLControl1_OpenGLDraw

All three are the built-in standard events of the openGLControl1 control.


The effect is like this.

 

Is it a little disappointing to draw this code? I don't know what the difference is from GDI +, right?

In fact, the difference is that GDI + is purely 2D, and the above two basic images are not 2D, but in 3D space. Although it is just a piece, it can also be in x, y and z rotate freely.

 

 

Although this example is simple, add the source code! Otherwise, it may affect your mood. Pai_^

 

Download the source code in this section

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.