Hello, csgl

Source: Internet
Author: User

There has been very little information about csgl in China. Recently, I have been paying attention to this aspect. Just write something and it should be a learning record.
For beginners like me, I cannot demand too much. If there is any mistake, I hope you can point it out.

-0-about csgl
Csgl is the packaging of OpenGL in. net.
Download: http://csgl.sourceforge.net/on SF. net/
Currently, the development of csgl has basically stopped. The official website says that csgl is stable enough for most applications. At present, the csgl development work is basically concentrated on writing example programs:
See http://www.randyridge.com/Tao/Default.aspx for details

-1-Hello, csgl
First, we will implement a simple csgl application.Program: A rotatingCubeBody.
The steps below are basically
Http://www.c-sharpcorner.com/Code/2002/Oct/SimpleOpenGL.asp
But it is not a one-to-one translation.

First, create a C # windows application. Add csgl. DLL to reference.

First, create a new thread in the main window to update the window.
Introduce namespace
Using system. Threading;
Declare the following private members in the main window class:
Private Static thread thrdraw;
Then define a private class member function
Private void opengl_start ()
{
While (true)
{
This. View. Refresh ();
}
}
Add the following in the form_load method:Code, Start thread:
Threadstart start = new threadstart (opengl_start );
Thrdraw = new thread (start );
Thrdraw. Start ();
Exit the thread in the dispose method of the Main Window
Thrdraw. Abort ();

Create a class inherited from openglcontrol: myview
Using csgl. OpenGL;

Public class myview: openglcontrol
{
Public myview (): Base (){}

/// <Summary>
/// Execute OpenGL Initialization
/// </Summary>
Protected override void initglcontext ()
{
Base. initglcontext ();

Gl. glshademodel (GL. gl_smooth); // enable smooth Shading
Gl. glclearcolor (0.0f, 0.0f, 0.0f, 0.5f); // black background
Gl. glcleardepth (1.0f); // depth buffer setup
Gl. glable (GL. gl_depth_test); // enables depth Testing
Gl. gldepthfunc (GL. gl_lequal); // The type of depth testing to do
Gl. glhint (GL. gl_perspective_correction_hint, GL. gl_nicest );

// Todo: add other initialization actions here, such as creating a display list
}

/// <Summary>
/// When the window size changes
/// </Summary>
Protected override void onsizechanged (eventargs E)
{
Base. onsizechanged (E );

Size S = size;
// Calculate the aspect ratio of the window
Double aspect_ratio = (double) S. width/(double) S. height;

Gl. glmatrixmode (GL. gl_projection); // you can specify the current projection matrix.
Gl. glloadidentity (); // reset the Projection Matrix

Gl. gluperspective (45.0f, aspect_ratio, 0.1f, 100366f );

Gl. glmatrixmode (GL. gl_modelview); // you can specify the current model view matrix.
Gl. glloadidentity (); // reset the Model View Matrix
}
}

Step 3: Create the cube class to be painted
Using csgl. OpenGL;

Public class cube
{
Private const int cube = 1;
Private float frotate;

Public void cube (){}

/// <Summary>
/// Create a display list
/// </Summary>
Public void glconstructlist ()
{
Gl. glnewlist (cube, GL. gl_compile); // start with the box display list

Gl. glbegin (GL. gl_quads); // start drawing quads
// Bottom face
Gl. glvertex3f (-1.0f,-1.0f,-1.0f );
Gl. glvertex3f (1.0f,-1.0f,-1.0f );
Gl. glvertex3f (1.0f,-1.0f, 1.0f );
Gl. glvertex3f (-1.0f,-1.0f, 1.0f );

// front face
GL. glvertex3f (-1.0f,-1.0f, 1.0f);
GL. glvertex3f (1.0f,-1.0f, 1.0f);
GL. glvertex3f (1.0f, 1.0f, 1.0f);
GL. glvertex3f (-1.0f, 1.0f, 1.0f);

// back face
GL. glvertex3f (-1.0f,-1.0f,-1.0f);
GL. glvertex3f (-1.0f, 1.0f,-1.0f);
GL. glvertex3f (1.0f, 1.0f,-1.0f);
GL. glvertex3f (1.0f,-1.0f,-1.0f);

// right face
GL. glvertex3f (1.0f,-1.0f,-1.0f);
GL. glvertex3f (1.0f, 1.0f,-1.0f);
GL. glvertex3f (1.0f, 1.0f, 1.0f);
GL. glvertex3f (1.0f,-1.0f, 1.0f);

// left face
GL. glvertex3f (-1.0f,-1.0f,-1.0f);
GL. glvertex3f (-1.0f,-1.0f, 1.0f);
GL. glvertex3f (-1.0f, 1.0f, 1.0f);
GL. glvertex3f (-1.0f, 1.0f,-1.0f);

Gl. glend (); // done drawing quads
Gl. gldlist (); // done building the box display list
}

///


// draw a box
///
Public void gldraw ()
{< br> GL. gltranslatef (0.0f, 0.0f, 6.0f);
GL. glrotatef (90f, 1.0f, 1.0f, 1.0f);

Gl. glrotatef (frotate, 1.0f, 1.0f, 1.0f );
Frotate + = 0.5f;

Gl. glcalllist (cube );
}
}

OK, so far, the work is basically finished, and the cube is displayed as follows:
Add a private domain member to the myview class:
Private cube;
Initialize it in the constructor:
Cube = new cube ();
Initglcontext () adds a row to create a display list:
Cube. glconstructlist ();
Finally, an overload member function is added to draw the Code:
Public override void gldraw ()
{
Gl. glclear (GL. gl_color_buffer_bit | Gl. gl_depth_buffer_bit); // clear windows and deep Cache
Gl. glloadidentity (); // reset the current model view Matrix

// Drawing
Cube. gldrawcube ();
This. swapbuffer (); // exchange Cache
}

The last step is to add the private member variable in the main window class.
Private myview view;
Add view initialization to initializecomponent:
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. view = new myview ();
This. suspendlayout ();
//
// View
//
This. View. Dock = system. Windows. Forms. dockstyle. Fill;
This. View. Location = new system. Drawing. Point (0, 0 );
This. View. Name = "View ";
This. View. size = new system. Drawing. Size (292,266 );
This. View. tabindex = 0;
//
// Mainwindow
//
This. autoscalebasesize = new system. Drawing. Size (5, 13 );
This. clientsize = new system. Drawing. Size (292,266 );
This. Controls. Add (this. View );
This. Name = "mainwindow ";
This. Text = "simple csgl app ";
This. Load + = new system. eventhandler (this. mainwindow_load );
This. resumelayout (false );
}

Success.

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.