OpenGL-GLUT tutorial (2) glut Initialization

Source: Internet
Author: User

 GLUTTutorial

Initialization settings

 

In this section, we will create a main function in our program, which will complete the necessary initialization and enable the event processing loop. All glut functions have the glut prefix, and those functions that complete initialization have the gluinit prefix. The first thing you need to do is call the function gluinit ().

Void gluinit (int * argc, char ** argv );

Parameters:

Argc: A pointer pointing to the unchanged argc variable passed from the main () function.

Argv: A pointer pointing to the unchanged argv variable passed from the main () function.

After the glut is initialized, we start to define our window. First, determine the window position (which defaults to the upper-left corner of the screen). We use the function: gluinitwindowposition ().

Void gluinitwindowpositon (int x, int y );

Parameters:

X: number of rows to the left of the screen. -1 is the default value, which means that the window manager determines where the window appears. If you do not use the default value, you can set it yourself.

Y: The number of workers from the top of the screen. Same as X.

Note that the parameter is only a suggestion for the window manager. Although you have carefully set the window location, the window may return different positions. If you set it, you will generally get the desired result. Next, we will set the window size and use the function maid ().

Void gluinitwindowsize (INT width, int height );

Parameters:

Width: The window width.

Height: the height of the window.

 

Similarly, width and height are just a reference number. Avoid using negative numbers.

 

Next. You should use the function maid () to define the display mode.

Void gluinitdisplaymode (unsighed int Mode)

Parameters:

Mode-you can specify the following display modes.

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

The predefined constants of the specified color mode are:

1: glu_rgba or glu_rgb. Specifies an rgba window, which is a default color mode.

2: glu_index. Specify the color index mode.

This display mode also allows you to select a single buffer or dual buffer window.

1: glu_single. Single buffer window.

2: glu_buffer. Dual buffer window, which is required to generate a smooth animation.

You can also specify more. If you want to specify a special set of buffers, use the following variables:

1: The cumulative buffer.

2: Map buffer.

3: glu_depth. Deep buffer.

 

Suppose you want an RGB window with a single buffer and a deep buffer, you use the "or" (|) operator to create the display mode you want.

................

Gluinitdisplaymode (glu_rgb | glu_single | glut | depth );

................

 

After the above steps, you can call the function glucreatewindow () to create a window.

Int fig (char * Title );

Parameters:

Title: Set the title of the window.

The returned value of gluetreewindow () is a window identifier. You can use this identifier in glut later, but this is beyond the scope of this section.

 

Now we have some code to complete all initialization operations.

# Include <Gl/glut. h>

Void main (INT argc, char ** argv)

{

Gluinit (& argc, argv );

Fig );

Gluinitwindowposition (100,100 );

Gluinitwindowsize (320,320 );

Glucreatewindow ("glut tutorial ");

}

If you run the above Code, you will get an empty black Console window without OpenGL window. And the console window will soon disappear.

There are two more things to deal with before we render something. First, tell glut which function is responsible for rendering. We create a simple rendering function. The following function clears the color buffer and draws a triangle.

...

void renderScene(void) {

         glClear(GL_COLOR_BUFFER_BIT);

         glBegin(GL_TRIANGLES);

                 glVertex
  
   3f
  (-0.5,-0.5,0.0);

                 glVertex
  
   3f
  (0.5,0.0,0.0);

                 glVertex
  
   3f
  (0.0,0.5,0.0);

         glEnd();

         glFlush();

}

You can set the name of the above function. Now you must tell glut to use the above function for rendering. This is called the storage callback .. Let's tell glut that renderscene should be used. When you need to re-draw a function, Glut will call a function that only transmits one function name parameter (a function named as a parameter.

Void gludisplayfunc (void (* func) (void ));

Parameters:

FUNC: the name of the called function to be re-painted in the window. Note that using null as a real parameter is incorrect.

The last thing is to tell glut that we are going to enter the application event processing loop. GLUT provides a function that allows the program to enter an endless loop. Wait until processing the next event. The function is glumainloop ().

Void glumainloop (void)

All code so far is listed below. If you run the code, you will get a console window and an OpenGL window drawn with a white triangle, which appears at the position you set and has the size you set.

#include <GL/glut.h>

 

 

void renderScene(void) {

         glClear(GL_COLOR_BUFFER_BIT);

         glBegin(GL_TRIANGLES);

                 glVertex
  
   3f
  (-0.5,-0.5,0.0);

                 glVertex
  
   3f
  (0.5,0.0,0.0);

                 glVertex
  
   3f
  (0.0,0.5,0.0);

         glEnd();

         glFlush();

}

 

void main(int argc, char **argv) {

         glutInit(&argc, argv);

         glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);

         glutInitWindowPosition(100,100);

         glutInitWindowSize(320,320);

         glutCreateWindow("3D Tech- GLUT Tutorial");

         glutDisplayFunc(renderScene);

         glutMainLoop();

}

 

 

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.