OpenGL simple graphics program

Source: Internet
Author: User

1. Tool: codeblock 10.05

2. Code:

# Include <windows. h> # include <Gl/GL. h> # include <math. h> # define PI 3.1415926535898 glubyte fly [] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x01, 0xc0, 0x06, 0xc0, 0x03, 0x60, 0x04, 0x60, 0x06, 0x20, 0x04, 0x30, 0x0c, 0x20, 0x04, 0x18, 0x18, 0x20, 0x04, 0x0c, 0x30, 0x20, 0x04, 0x06, 0x60, 0x20, 0x44, 0x03, 0xc0, 0x22, 0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x8 0, 0x22, 0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22, 0x66, 0x01, 0x80, 0x66, 0x33, 0x01, 0x80, 0xcc, 0x19, 0x81, 0x81, 0x98, 0x0c, 0xc1, 0x83, 0x30, 0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0, 0x03, 0x31, 0x8c, 0xc0, 0x03, 0x33, 0xcc, 0xc0, 0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30, 0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08, 0x10, 0x63, 0xc6, 0x08, 0x10, 0 X30, 0x0c, 0x08, 0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08}; void draw_fly () {// glbegin (gl_points); glcolor3f (0.0, 1.0, 0.0); glable (gl_polygon_stipple); glpolygonstipple (FLY); glrectf (0.4, 0.4, 0.4, 0.4); gldisable (gl_polygon_stipple); glend ();} void draw_point () {glpointsize (8.0); // you can specify the vertex size, no more glbegin (gl_points); glcolor3f (0.0, 1.0, 0.0); glvertex2f (-0.4,-0.4); glcolor3f (1.0, 0. 0, 1.0); glvertex2f (-0.4,-0.3); glcolor3f (0.0, 1.0, 1.0); glvertex2f (-0.4,-0.2); glcolor3f (1.0, 1.0, 0.0); glvertex2f (0-0.4,-0.1); glend ();} void draw_line_loop () {glbegin (gl_line_loop); // The mode is gl_line_loop glvertex2f (-0.3, -0.3); glvertex2f (-0.3, 0.5); glvertex2f (0.0, 0.4); glvertex2f (0.5, 0.5); glvertex2f (0.5,-0.5); glend ();} void draw_line_strip () {glcolor3f (1.0, 0.1, 0.1); // draw a rectangular glbegin (gl_line_strip); // Mode: gl_line_strip glvertex2f (-0.1,-0.1); glvertex2f (-0.2, 0.22); glvertex2f (0.3, 0.15); glvertex2f (0.2,-0.3 ); glend ();} void draw_rec () {glbegin (gl_line_loop); // The mode is gl_line_loop glvertex2f (-0.5,-0.5); glvertex2f (-0.5, 0.5 ); glvertex2f (0.5, 0.5); glvertex2f (0.5,-0.5); glend ();} void draw_elipse () {int I; glfloat angle; glint circle_points = 100; glbegin (gl_line_loop); for (I = 0; I <circle_poin TS; I ++) {angle = 2 * pI * I/circle_points; glvertex2f (COS (angle), sin (angle);} glend ();} void draw_line () {glbegin (gl_lines); glvertex3f (250000f, 160.0f, 0.0f); random (2250000f, 160.0f, 0.0f); glend ();} void random () {glcolor4f (0.0, 0.0, 0.0, 0.5); // draw the glrectf (-0.2,-0.2, 0.2, 0.2); glend () ;}lresult callback windowproc (hwnd, uint, wparam, lparam ); void enableopengl (hwnd, HDC *, hglrc *); void dis Ableopengl (hwnd, HDC, hglrc); int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow) {wndclassex wcex; hwnd; HDC; hglrc HRC; MSG; bool bquit = false; float Theta = 0.5f;/* Register window class */wcex. cbsize = sizeof (wndclassex); wcex. style = cs_owndc; wcex. lpfnwndproc = windowproc; wcex. cbclsextra = 0; wcex. cbwndextra = 0; wcex. hinstance = Hi Nstance; wcex. hicon = loadicon (null, idi_application); wcex. hcursor = loadcursor (null, idc_arrow); wcex. hbrbackground = (hbrush) getstockobject (black_brush); wcex. lpszmenuname = NULL; wcex. lpszclassname = "glsample"; wcex. hiconsm = loadicon (null, idi_application); If (! Registerclassex (& wcex) return 0;/* Create main window */hwnd = create1_wex (0, "glsample", "OpenGL test", ws_overlappedwindow, cw_usedefault, cw_usedefault, 600,600, null, null, hinstance, null); showwindow (hwnd, ncmdshow);/* enable OpenGL for the window */enableopengl (hwnd, & HDC, & HRC ); // glortho (-300,300,300,300, 0,500);/* Program Main Loop */while (! Bquit) {/* Check for messages */If (peekmessage (& MSG, null, 0, 0, pm_remove) {/* handle or dispatch messages */If (MSG. message = wm_quit) {bquit = true;} else {translatemessage (& MSG); dispatchmessage (& MSG );}} else {/* OpenGL animation code goes here */glclearcolor (1.0f, 1.0f, 1.0f, 1.0f); glclear (gl_color_buffer_bit); glfloat cursizeline = 2; gllinewidth (cursizeline ); glpushmatrix (); // glrotatef (Theta, 1.0f, 1.0f, 1.0f); glcolor3f (0.5f, 0.5f, 0.1f); draw_elipse (); draw_rec (); draw_line (); // draw_rect (); draw_fly (); draw_line_loop (); lag (); draw_point (); // glpolygonstipple (FLY); // glpolygonstipple (halftone); glend (); glpopmatrix (); swapbuffers (HDC); Theta + = 4.0f; sleep (1) ;}/ * shutdown OpenGL */disableopengl (hwnd, HDC, HRC ); /* destroy the window explicitly */destroywindow (hwnd); Return MSG. wparam;} lresult callback windowproc (hwnd, uint umsg, wparam, lparam) {Switch (umsg) {Case wm_close: postquitmessage (0); break; Case wm_destroy: return 0; Case wm_keydown: {Switch (wparam) {Case vk_escape: postquitmessage (0); break ;}} break; default: Return defwindowproc (hwnd, umsg, wparam, lparam);} return 0;} void enableopengl (hwnd, HDC * HDC, hglrc * HRC) {pixelformatdescriptor PFD; int iformat;/* get the device context (DC) */* HDC = getdc (hwnd);/* set the pixel format for the DC */zeromemory (& PFD, sizeof (PFD); PFD. nsize = sizeof (PFD); PFD. nversion = 1; PFD. dwflags = pfd_draw_to_window | pfd_support_opengl | pfd_doublebuffer; PFD. ipixeltype = pfd_type_rgba; PFD. ccolorbits = 24; PFD. cdepthbits = 32; PFD. ilayertype = pfd_main_plane; iformat = choosepixelformat (* HDC, & PFD); setpixelformat (* HDC, iformat, & PFD);/* Create and enable the render context (RC) */* HRC = wglcreatecontext (* HDC); wglmakecurrent (* HDC, * HRC);} void disableopengl (hwnd, HDC, hglrc HRC) {wglmakecurrent (null, null); wgldeletecontext (HRC); releasedc (hwnd, HDC );}

3. Outputs

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.