Summary of the first introduction to OpenGL drawing and simple operations - drawing lines and setting transparency

Recent image algorithm work requires the use of OpenGL, and most of them have been using Opencv before, so I will make a simple summary of recent learning to provide other students who are just getting started with OpenGL with a simple reference and understanding of the OpenGL workflow. As for OpenGL Please refer to my previous blog for configuration.

The drawing process of OpenGL is a process of converting three-dimensional objects into two-dimensional objects. If you want to understand the drawing process of OpenGL, you must first understand the coordinate system in OpenGL. The local coordinate system constructed by OpenGL from the model is finally rendered to screen coordinates through a series of processes. There are 6 coordinate systems in this process, namely:

World Coordinates,
Object Coordinates,
Eye Coordinates or Camera Coordinates,
Clip Coordinates
, Normalized Device Coordinates (NDC),
Screen Coordinates)

Because this time we only introduce the OpenGL drawing process based on the sample code (the complete code will be put at the end), the OpenGL drawing process will involve a series of coordinate transformation mathematical processes that I will not go into here. There are many online Blog about the process.

First, let’s talk about the basic process of OpenGL drawing, which are: clearing the buffer, setting color, drawing primitives, and output graphics.

clear buffer

The function to clear the color buffer is glClearColor(). It has four floating-point parameters, which respectively represent the values ​​of the R, G, B, and A components of the cleared value. The default values ​​​​are all 0. The fourth parameter A represents the Alpha value, which is mainly used for fusion. It is generally set to 0. The function to set the depth buffer clear value is glClearDepth(). It has only one parameter, the depth clear value. The value range is [0.01,1.0], and the default value is 1.0. The function used to clear the buffer is glClear(), which has only one parameter, indicating the type of buffer to be cleared. The possible values ​​are GL_COLOR_BUFFER_BIT (color buffer), GL_DEPTH_BUFFER_BIT (depth buffer), GL_ACCUM_BUFFER_BIT (accumulation buffer), GL_STENCIL_BUFFER_BIT (stencil buffer). These values ​​can also be combined using logical OR to clear multiple buffers at once.

Color settings

Sets the color of the current drawing target. After the color is set to the current color, all subsequent objects will be drawn with that color until the setting is changed. The function to set the color is glColor().

Draw primitives

There are many OpenGL drawing functions. OpenGL drawing must be completed between the glBegin() and glEnd() functions, otherwise it may cause compilation errors. Here is a brief introduction to the basic geometric drawing functions (that is, the geometric model can be filled in glBegin()). as follows:

GL_POINTS: a single vertex set
GL_LINES: multiple sets of double-vertex line segments
GL_POLYGON: a single simple filled convex polygon
GL_TRAINGLES: multiple sets of independent filled triangles GL_QUADS
: multiple sets of independent filled quadrilaterals
GL_LINE_STRIP: unclosed polyline GL_LINE_LOOP:
closed polyline
GL_TRAINGLE_STRIP: linear continuous filled triangle string
GL_TRAINGLE_FAN: Fan-shaped continuous filled triangle string
GL_QUAD_STRIP: Continuously filled quadrilateral string

The glBegin() and glEnd() functions must be used in pairs, otherwise unpredictable errors may occur.

Output graphics

The final drawing output uses the glFlush() function. According to the official documentation, the glFlush() function is used in OpenGL to force the buffer to be refreshed to ensure that the drawing command will be executed instead of being stored in the buffer waiting for other OpenGL commands .

Sample code

The following sample code completes the drawing of a straight line

//初始化
void init(void){

	glClearColor(0.0, 0.0, 0.0, 0.0);     //设置窗体将被清除成黑色
	glMatrixMode(GL_PROJECTION);           //投影矩阵
	glLoadIdentity();                      //把当前矩阵设为单位矩阵
	glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);//视景体坐标系统 glOrtho(Xmin,Xmax,Ymin,Ymax,Zmin,Zmax);   glOrtho(left, right, bottom, top, near, far)


}




void LineDrawing() {
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT);

	glLineWidth(10.0f);
    //glViewport(100, 50, 200, 150);
	glBegin(GL_LINES);
	glColor4f(1.0, 1.0, 0.0, 0.7); glVertex3f(-0.15f, -0.5f, 0.0f);
	glColor4f(1.0, 1.0, 0.0, 0.7); glVertex3f(-0.5f, 0.5f, 0.0f);
    glColor4f(1.0, 1.0, 0.0, 0.7); glVertex3f(-0.05f, -0.5f, 0.0f);
    glColor4f(1.0, 1.0, 0.0, 0.7); glVertex3f(-0.05f, 0.5f, 0.0f);
	glEnd();
	glFlush();
}



int main(int argc, char** argv)
{
	//初始化gult
	glutInit(&argc, argv);

	//指定使用RGBA模式还是颜色索引模式。

	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

	//指定窗体大小(像素为单位)
	glutInitWindowSize(750, 750);

	//窗体在电脑屏幕的左上角位置
	glutInitWindowPosition(100, 100);

	//创建窗体,glutMainLoop()之后才会显示
	glutCreateWindow("OpenGL");

	//初始化一次
	init();

	glutDisplayFunc(LineDrawing);

	//启动程序。
	glutMainLoop();
	//return 0;
   
}

Running the code successfully yields the following output:

 You can see that the diagonal lines are jagged, and we can improve it by modifying the initialization function used for drawing; the code is modified as follows:

void init(void){

	glClearColor(0.0, 0.0, 0.0, 0.0);     //设置窗体将被清除成黑色
	glMatrixMode(GL_PROJECTION);           //将当前矩阵指定为投影矩阵
	glLoadIdentity();                      //把当前矩阵设为单位矩阵
	glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);//指定绘制图像时使用的坐标系统 glOrtho(Xmin,Xmax,Ymin,Ymax,Zmin,Zmax);   glOrtho(left, right, bottom, top, near, far)
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA);
    glEnable(GL_LINE_SMOOTH);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);


}

After successful operation, the following output is obtained, and you can see that the aliasing has been improved.

 Transparency can be set through the function glColor4f:

void LineDrawing() {
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT);

	glLineWidth(10.0f);
	glBegin(GL_LINES);
	glColor4f(1.0, 1.0, 0.0, 0.5); glVertex3f(-0.15f, -0.5f, 0.0f);
	glColor4f(1.0, 1.0, 0.0, 0.5); glVertex3f(-0.5f, 0.5f, 0.0f);
    glColor4f(1.0, 1.0, 0.0, 0.5); glVertex3f(-0.05f, -0.5f, 0.0f);
    glColor4f(1.0, 1.0, 0.0, 0.5); glVertex3f(-0.05f, 0.5f, 0.0f);
	glEnd();
	glFlush();
}

You can see that the transparency of the image has changed:

 

Guess you like

Origin blog.csdn.net/weixin_46568899/article/details/129057388