📜 ⬆️ ⬇️

Meet OpenGL

Opengl


Getting to know OpenGL needs to begin with the fact that OpenGL is a specification . Those. OpenGL only defines a set of required features. The implementation depends on the specific platform.
OpenGL is a cross-platform, independent programming language API for working with graphics. OpenGL is a low-level API, so to work with it, it is nice to have some idea about graphics in general and know the basics of linear algebra.

Naming


Let's say a few words about the naming of functions in OpenGL. First, the names of all functions provided directly by OpenGL begin with the prefix gl . Secondly, functions defining some parameter characterized by a set of numbers (for example, a coordinate or a color) have a suffix of the form [number of parameters + type of parameters + representation of parameters].

Example: glVertex3iv specifies the coordinate of a vertex consisting of three integers, passed as a pointer to an array.

Graphics


All graphic objects in OpenGL are a collection of points, lines, and polygons. There are 10 different primitives, with which all objects are built. Both 2D and 3D. All primitives, in turn, are defined by points - vertices.

To set the primitive, the glBegin (primitive_type) ... glEnd () construction is used. Vertices are set by glVertex * . Vertices are set counterclockwise. Coordinates are set from the upper left corner of the window. The vertex color is set with the glColor * command. The color is specified as RGB or RGBA. The glColor * command acts on all vertices that go after until another glColor * command is encountered, or all, if there are no other glColor * commands.
Here is the code drawing a square with multi-colored vertices:
  1. glBegin ( GL_QUADS ) ;
  2. glColor3f ( 1.0 , 1.0 , 1.0 ) ;
  3. glVertex2i ( 250 , 450 ) ;
  4. glColor3f ( 0.0 , 0.0 , 1.0 ) ;
  5. glVertex2i ( 250 , 150 ) ;
  6. glColor3f ( 0.0 , 1.0 , 0.0 ) ;
  7. glVertex2i ( 550 , 150 ) ;
  8. glColor3f ( 1.0 , 0.0 , 0.0 ) ;
  9. glVertex2i ( 550 , 450 ) ;
  10. glEnd ( ) ;

')

OpenGL Program Basics


For platform-independent work with windows, you can use the library GLUT . GLUT makes it easy to work with OpenGL.
To initialize GLUT at the beginning of the program, you must call glutInit (& argc, argv) . To set the display mode, call glutInitDisplayMode (mode) , where the mode can take the following values:

To select multiple modes at the same time you need to use bitwise OR '|'. For example: glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH) includes double buffering, Z-buffer and four-component color. Window sizes are set by glutInitWindowSize (width, height) . Its position is glutInitWindowPosition (x, y) . A window is created using the glutCreateWindow function (window_title) .
GLUT implements an event-driven mechanism. Those. there is a main loop that starts after initialization, and all declared events are already processed in it. For example, pressing a key on the keyboard or moving the mouse cursor, etc. You can register event handler functions using the following commands:

Then you can run the main loop glutMainLoop () .

First program


Now we know the basics of working with OpenGL. You can write a simple program to consolidate knowledge.
To begin with, you need to include the GLUT header file:
  1. #if defined (linux) || defined (_WIN32)
  2. #include <GL / glut.h> / * For Linux and Windows * /
  3. #else
  4. #include <GLUT / GLUT.h> / * For Mac OS * /
  5. #endif

Now we already know what to write in main. We register two handlers: for drawing the contents of the window and processing its resizing. These two handlers are essentially used in any program that uses OpenGL and GLUT.
  1. int main ( int argc , char * argv [ ] )
  2. {
  3. glutInit ( & argc , argv ) ;
  4. glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGBA ) ; / * Include double buffering and four-component color * /
  5. glutInitWindowSize ( 800 , 600 ) ;
  6. glutCreateWindow ( "OpenGL lesson 1" ) ;
  7. glutReshapeFunc ( reshape ) ;
  8. glutDisplayFunc ( display ) ;
  9. glutMainLoop ( ) ;
  10. return 0 ;
  11. }

Now you need to write a function to handle window resizing. Set the display area to the size of the entire window using the glViewport command (x, y, width, height) . Then we load the projection matrix glMatrixMode (GL_PROJECTION) , replace it with the unit glLoadIdentity () and set the orthogonal projection. Finally, let's load the glMatrixMode model- view matrix (GL_MODELVIEW) and replace it with the one.
As a result, we get:
  1. void reshape ( int w , int h )
  2. {
  3. glViewport ( 0 , 0 , w , h ) ;
  4. glMatrixMode ( GL_PROJECTION ) ;
  5. glLoadIdentity ( ) ;
  6. gluOrtho2D ( 0 , w , 0 , h ) ;
  7. glMatrixMode ( GL_MODELVIEW ) ;
  8. glLoadIdentity ( ) ;
  9. }

It remains only to write a function to draw the contents of the window. We will draw the square that I gave above as an example. Add have quite a bit of code. First, before drawing, you need to clear the various buffers with glClear (mode) . Used as well as glutInitDisplayMode. Possible values:

In our case, you need to clear only the color buffer, since we do not use others. Secondly, after drawing, you need to ask OpenGL to change screen buffers with glutSwapBuffers () , because we have double buffering enabled. Everything is drawn on a hidden buffer from the user and then the buffers are changed. This is done to get a smooth animation and to avoid the screen flicker effect.
We get:
  1. void display ( )
  2. {
  3. glClear ( GL_COLOR_BUFFER_BIT ) ;
  4. glBegin ( GL_QUADS ) ;
  5. glColor3f ( 1.0 , 1.0 , 1.0 ) ;
  6. glVertex2i ( 250 , 450 ) ;
  7. glColor3f ( 0.0 , 0.0 , 1.0 ) ;
  8. glVertex2i ( 250 , 150 ) ;
  9. glColor3f ( 0.0 , 1.0 , 0.0 ) ;
  10. glVertex2i ( 550 , 150 ) ;
  11. glColor3f ( 1.0 , 0.0 , 0.0 ) ;
  12. glVertex2i ( 550 , 450 ) ;
  13. glEnd ( ) ;
  14. glutSwapBuffers ( ) ;
  15. }


Total


Everything! You can compile. It should get something like this:
screen
The entire code is complete (for those who have not mastered the article).
In principle, there is nothing difficult in this, at least if you have already encountered graphics before.

OpenGL is a handy tool for creating cross-platform applications using graphics. OpenGL is easy to use with the programming language that is more convenient for you. There are bindings to OpenGL for many popular languages, such as C, C ++, C #, Java, Python, Perl, VB, and others. View information about them on the official website of OpenGL .

Source: https://habr.com/ru/post/111175/


All Articles