OpenGL learning Process (3) First lesson: Initializing a form

Source: Internet
Author: User
Tags set background

This section is the first lesson in OpenGL learning, and the following describes how to initialize a form:

(1) A form with a blue background is displayed:

#include <GL/glut.h>#include<stdlib.h>voidDisplayvoid){   /*Clear all pixels*/glclear (gl_color_buffer_bit); Glflush ();}intMainintargcChar**argv) {Glutinit (&argc, argv); Glutinitdisplaymode (Glut_single|Glut_rgb); Glutinitwindowsize ( -, -); Glutinitwindowposition ( -, -); Glutcreatewindow (the blue background form"); Glclearcolor (0.0,0.0,1.0,0.0);    Glutdisplayfunc (display);   Glutmainloop (); return 0;}

Code Explanation:

Several routines perform tasks necessary for initializing a window:

1) glutinit (int *argc, char **argv)

Initializes GLUT and processes any command,line arguments (for X, this would is options such as-display and-geometry). Glutinit () should is called before any other GLUT routine.
2) glutinitdisplaymode (unsigned int mode)

Specifies whether to use an RGBA or Color-index color model. You can also specify whether want a single-or double-buffered window. (If you ' re working-ColorIndex mode, you'll want to load certain colors into the color map; use Glutsetcolor () to do thi S.) Finally, you can use this routine to indicate so want the window to a associated depth, stencil, Multisamp Ling, and/or accumulation buffer. For example, if want a window with double buffering, the RGBA color model, and a depth buffer, you might call Glutinit DisplayMode (glut_double | Glut_rgba | glut_depth).

3) glutinitwindowposition (int x, int y)

Specifies the screen location for the Upper-left corner of your window.

4) glutinitwindowsize (int width, int height)

Specifies the size, in pixels, of your window.

5) glutinitcontextversion (int majorversion, int minorversion)

Specifies which version of OpenGL want to use. (This is a new addition available if using Freeglut, and was introduced with OpenGL Version 3.0. See "OpenGL Contexts" on page, more details on OpenGL contexts and versions.)

6) glutinitcontextflags (int flags)

Specifes the type of OpenGL context you want to use. For normal OpenGL operation, you can omit the from your program. However, if you want to use a forward-compatible OpenGL context, you'll need to the this routine. (This is also a new addition available only in Freeglut, and were introduced with OpenGL Version 3.0.) See "OpenGL Contexts" on page, more details on the types of OpenGL contexts.)

7) int Glutcreatewindow (char *string)

Creates a window with an OpenGL context. It returns a unique identifier for the new window. Be Warned:until Glutmainloop () are called, the window is not yet displayed.

The low-level Win32 API Initializes a form that is more complex, and the glut library simplifies the process.

callback function:

That is, the Win32 information System.

1) Glutdisplayfunc calls a function that is constantly being called.

2) Glutmainloop is used to start the program and keep the program running, without exiting, that is, entering the message loop.

3) Glclearcolor is used to clear the buffer color and reset the new color.

4)Guflush refreshes the OpenGL command queue, and if you do not call this method, the redrawn graphic cannot be displayed.

(2) Displays a form with a white rectangular black background:

//The function of the program is to draw a white rectangle in the center of a black window. #include <GL/glut.h>//Glut header file ( originally OpenGL program should also include <GL/gl.h> and <GL/glu.h>  , but the GLUT header file already contains the two files automatically and does not have to be included again. )
//note the case of the function namevoidMydisplay ()// This GL start function is the standard function of OpenGL {glclear (gl_color_buffer_bit); 9. Clear the operation. Gl_color_buffer_bit indicates that the color is cleared. Glclear can also be used to clear other things. GLRECTF (-0.5,-0.5f,0.5f,0.5f); 10. Draw a rectangle. The four parameters represent the horizontal ordinate of two points at the diagonal.                     Glflush (); 11. Ensure that the previous OpenGL command executes immediately, rather than waiting in the buffer. (function similar to fflush (stdout))}intMainintargcChar*argv[])// This function that starts with glut is the function {Glutinit provided by the GLUT Toolkit (&argc, argv); 1. Initialize the glut, this function must be called once before other glut are used. Glutinitdisplaymode (Glut_rgb|glut_single); 2. Set the display mode. Glut_rgb means using an RGB color, glut_index means using index (indexed color), glut_single means using single buffering, glut_double (using double buffering). Glutinitwindowposition ( -, -); 3. Set the position of the window in the screen. 4.glutInitWindowSize sets the size of the window Glutcreatewindow ("White rectangle with black background"); 5. Create a window based on the previous information, with the caption set as the parameter. Glutdisplayfunc (&mydisplay); 6. This function is called when drawing is required.                  ( Register callback ) Glutmainloop (); 7. When the window is created, it is not immediately displayed on the screen. This function has the effect of displaying the window on the screen. A message loop is made and the function returns when the window is closed. return 0;} 8. When drawing is required, we set the "Call Mydisplay function when drawing is required" in Glutdisplayfunc (). Mydisplay is used for drawing, the functions in Mydisplay are described below.

(3) Detailed:

1) Glutinitdisplaymode () function:

function is: set the display mode;

The function prototype is: void Glutinitdisplaymode (unsigned int mode);

The 1.mode parameter is a possible Boolean combination that is predefined in a glut library. You can use mode to specify the color mode , number , and buffer type .

The following are all possible values for mode:

2. Pixel colors are stored in the graphics hardware in two ways: Rgba and index (pixel index).

RGB color mode: is a color standard of the industry, is through the red (R), Green (G), Blue (B) three color channel changes and they overlap each other to get a variety of colors, RGB is the red, green, blue three channels of color, This standard almost includes all the colors that human vision can perceive, and is one of the most widely used color systems. RGBA has more parameters to control alpha transparency on the basis of RGB. Above R, G, b three parameters, the positive integer value range is: 0-255. The value of the percentage value range is: 0%-100%. Values that are out of range will be up to their nearest value limit. Not all browsers support the use of percent values. A parameter, the value between 0~1, not negative.

Index color mode: stores and indexes colors in an image using a color table. If a color in the original image does not appear in the speeding table, the program selects the most similar color in the existing color or simulates the color with an existing color. Can reduce file size while maintaining visual quality unchanged.

3. Set a single buffer or double buffer window:

Glut_single: Single buffer window, when user interaction is not required to use a single buffer, user interaction needs to use double buffering .

glut_double: Double buffer window, which is required to produce a smooth animation. Glut using double buffering only needs to replace glflush () with function glutswapbuffers () in the display callback function.

2) Glclear () and Glclearcolor () functions:

glclear function prototype: void Glclear (Glbitfield mask);

Glbitfield: You can use the | operator to combine different buffer flag bits to indicate which buffers need to be purged.

(e.g. Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit) indicates to clear the color buffer and depth buffer)

1. Type of buffer: (Function of buffer: http://blog.sina.com.cn/s/blog_5dc7bbf801010fdr.html)

The   buffer                  name color           buffer    gl_color_buffer_bit          // is the frame buffer (frame_buffer), The scene you need to render will eventually be written to the buffer for each pixel, and then it shows the depth buffer           gl_depth_buffer_bit // corresponds to the frame buffer on the render to screen to record the depth of each pixel above Values, through depth buffers, we can conduct deep testing to determine the occlusion of pixels and ensure that rendering is correct. the cumulative buffer           gl_accum_buffer_bit // Cumulative cache also stores RGBA color data, combining a series of images into an image. The template buffer           // is the same size as the depth buffer, and by setting the stencil to buffer the value of each pixel, we can specify that only some pixels are rendered at render time, which can achieve some special effects

Functions of the 2.glclear () function:

Clears the last displayed result, which must be called before each drawing.

Glclearcolor (0.0,0.0,0.0,0.0); Set the clear color to black. (Just set the color, and do not use this color to clear any area) glclear (gl_color_buffer_bit);      Actually completed the task of clearing the whole window to black. Pixel inspection, crop inspection, jitter, and write masking of the cache affect the operation of the Glclear, where the clipping range limits the cleared area, while the glclear command ignores alpha functions, fused functions, logical operations, templates, texture mappings, and z-caches;

OpenGL starts at each frame to call Glclearcolor to set the background color, which is equivalent to the color of the canvas. It must be emphasized that Glclearcolor only acts as a set and does not clear anything.

3) Glflush () function:

ensure that the drawing command will be executed instead of being stored in the buffer waiting for other OpenGL commands. is forced refresh.

(OpenGL is a linear processing command using a rendering pipeline, in general, the instructions we put to OpenGL are not immediately sent to the driver to execute, but put into a buffer, and so the buffer is full once again sent to the driver to execute; Most of the time only a few instructions are filling the buffer, which means that these instructions are not sent to the driver, so we have to call Glflush to force these instructions to be sent to the driver for processing. )

4) Draw a form with a red background and a blue rectangle:(test glclear function and Glclearcolor function)

#include <GL/glut.h>voidMydisplay (void) {glclear (gl_color_buffer_bit); //Glclearcolor (0.0,0.0,1.0,0.0);//Re -set the fill color to blue, which is the window background colorGLCOLOR3F (0.0f,0.0f,1.0f);//set the color of the brush to blueGLRECTF (-0.5f,-0.5f,0.5f,0.5f); Glflush ();}intMainintargcChar*argv[]) {Glutinit (&argc, argv); Glutinitdisplaymode (Glut_single|Glut_rgb); Glutinitwindowposition ( -, -); Glutinitwindowsize ( -, $); Glutcreatewindow ("My Exercise 1: Blue rectangle with red background"); Glclearcolor (1.0,0.0,0.0,0.0);//Set background fill color to redGlutdisplayfunc (&mydisplay);//After testing, the hosting callback process is correct with no address. Glutmainloop ();}

OpenGL learning Process (3) First lesson: Initializing a form

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.