Greetings to you Habrayuzer! Today in my first article I will talk about programming on
OpenGl .
// Note: You can also use glut instead of freeglut (an open alternative to glut).
First, I’ll write a little about what you need to download and install in order to start coding on
opengl (freeglut'e) .
')
For drawing on opengl, we will use the
freeglut library (OpenGL Utility Toolkit - a wrapper for opengl, so to speak). Freeglut files can be found
here . There are also instructions on how to install it on linux and windows (MSVC and MinGW). On linux, freeglut is placed in several commands, and in windows in a few clicks. If you have any problems with the installation, you can write to me, I will write in more detail how to install
freeglut .
More under the cut.
And so in the beginning let's get acquainted with the initial initialization of the code.
Usually any freeglut project contains the following code:
#include <GL/freeglut.h>// void display() { /* */ } int main(int argc, char **argv) { glutInit(&argc, argv);// glutInitDisplayMode(GLUT_DOUBLE);// glutInitWindowSize(400,400);// glutInitWindowPosition(200, 200);// // glutInit...?, , -. glutCreateWindow("Window");// glClearColor(1, 1, 1, 0);// // , - . glMatrixMode(GL_PROJECTION);// glLoadIdentity();// glOrtho(-100, 100, -100, 100, -100, 100);//c , 3D , 200 . glutDisplayFunc(display);// . glutMainLoop();// , - main, glut' - main. return 0; }
Let us examine some of the above functions in more detail:
The
ffl glutInitDisplayMode () can take different parameters, but for now we will use only some of them:
GLUT_RGB - Using the RGB mode (red | green | blue - red | green | blue - respectively). Used by default.
GLUT_SINGLE - single buffer window. This mode is also used by default.
GLUT_DOUBLE - double buffer window.
A buffer is an area in which data is temporarily stored.
The question arises, how does a single buffer differ from a double buffer? The double buffer works as follows: There are two buffers while the first buffer is drawn on the screen, the second buffer is prepared and drawn after the entire first buffer is drawn. Then in the same way, only buffers are swapped. Those. buffers are drawn one by one. Why do you need it? In order to avoid flickering on the screen. I think from the above written, you can understand what a single buffer means.
F.
glOrtho (left, right, bottom, top, zNear, zFar) - sets the coordinate system
the left parameter indicates how many units we move to the left along the oX axis
the parameter right indicates how many units move to the right along the axis oX
the bottom parameter means how many units we move down along the oY axis
the top parameter indicates how many units we move up along the oY axis
the parameter zNear means how many units we move inwards along the oZ axis
the parameter zFar means how many units we move forward along the oZ axis
For an easier understanding of the picture dashed:
Programming
opengl primitives:
Let's proceed directly to the coding.
Small code that draws the line:
#include <GL/freeglut.h> void display() { glClear(GL_COLOR_BUFFER_BIT);// glColor3f(1.0f,0.0f,0.0f);// glLineWidth(3);// glBegin(GL_LINES);// glVertex2d(-1, 0);// (x,y) glVertex2d( 1, 0);// glEnd();// glutSwapBuffers();// } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE); glutInitWindowSize(200, 200); glutInitWindowPosition(200, 200); glutCreateWindow("Window"); glClearColor(1, 1, 1, 0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-10, 10, -10, 10, 0, 0); glutDisplayFunc(display); glutMainLoop(); return 0; }

In fbl
glBegin () we set what we want to start drawing, it can take such parameters as:
GL_POINTS - tells the program that we will draw a point
GL_LINES - we will draw a line
GL_TRIANGLES - we will draw a triangle
GL_QUADS - we will draw a rectangle
...
and other parameters with which you can combine lines, draw groups of triangles, quadrangles and polygons.
The
f -I
glEnd () completes the drawing of the object (s)
Now let's use
glBegin (GL_POINTS),
glBegin (GL_LINES),
glBegin (GL_TRIANGLES),
glBegin (GL_QUADS) to draw a point, line, triangle and rectangle.
#include <GL/freeglut.h> void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPointSize(6); glColor3f(0.0f,0.0f,0.0f); glBegin(GL_POINTS);// glVertex2f(-0.5f, 0.2f); glEnd(); glLineWidth(3); glColor3f(1.0f,0.0f,0.0f); glBegin(GL_LINES);// glVertex2d(-1, 1); glVertex2d( 1, -1); glEnd(); glColor3f(0.0f, 1.0f,0.0f); glBegin(GL_TRIANGLES);// glVertex2d(0, 0); glVertex2f(0.5, 1); glVertex2d(1, 0); glEnd(); glColor3f(0.0f, 0.0f, 1.0f);// glBegin(GL_QUADS); glVertex2d(-1, -1); glVertex2d(-1, 0); glVertex2d( 0, 0); glVertex2d( 0, -1); glEnd(); glutSwapBuffers();// } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE); glutInitWindowSize(200, 200); glutInitWindowPosition(200, 200); glutCreateWindow("Window"); glClearColor(1, 1, 1, 0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1, 1, -1, 1, 0, 0); glutDisplayFunc(display); glutMainLoop(); return 0; }

I think it's time to finish this, I will describe the rest in the following articles. If you have any comments, questions, write to me in a personal or in the comments. If you find any error in the description or in the code, please let me know.