Title: OpenGL (2) the window

date: 2020-06-28 10:07

Category: graphics

tags: opengl

Link: OpenGL (2) the window

1. The viewport

One important thing to do before rendering is to tell OpenGL the size of the rendering window, called the Viewport. So OpenGL knows how to display data and coordinates according to the size of the window, and how to set the dimensions of the window through the glViewPort function, right

glViewPort(0.0.800.600);
Copy the code

The first two arguments to the glViewPort function control the position of the lower left corner of the window, and the third and fourth arguments control the width and height (pixels) of the render window.

OpenGL uses the position and width and height defined in glViewport to convert 2D coordinates. The position coordinates in OpenGL are converted to screen coordinates. If the coordinates in OpenGL (-0.5, 0.5) are mapped to screen coordinates (200,450), OpenGL coordinates are only processed in the range -1 to 1, essentially mapping coordinates in the range -1, 1 to (0,800) and (0,600)

2. Apply colours to a drawing

2.1 Render Loop

If you don’t want your application to exit and close the window immediately after drawing only one image, and you want to keep drawing images and accepting user input before actively closing it, you need to add a while loop to your application to keep the window running until it exits.

while(! glfwWindowShouldClose(window)){ glfwSwapBuffers(windo); glfwPollEvents(); }Copy the code
  • GlfwWindowShouldClose: Check at the start of each loop to see if GLFW is required to exit, return true if so, then render loop ends, and close the application.

  • GlfwSwapBuffers: Exchange color buffers to draw and display as output to the screen

  • GlfwPollEvents: Checks what events are triggered, updates the window state, and invokes the corresponding callback function.

2.2 process

Put all render operations in the render loop so that render instructions can be executed on each iteration of the render loop.


while(! glfwWindowShouldClose(window)){/ / input
	processInput(window);

	// Render instructions

	// Check and call events, swap buffers
	glfwPollEvents();
	glfwSwapBuffers(window);
}

Copy the code

Set clear screen colors and clear color buffers: To speed up rendering and avoid seeing the results of the last iteration; GL_DEPTH_BUFFER_BIT: GL_STENCIL_BUFFER_BIT: GL_STENCIL_BUFFER_BIT: Template buffer GL_COLOR_BUFFER_BIT: color buffer

The glClearColor function sets the color used to clear the screen, and the entire color buffer is filled with the color set in glClearColor. State setting function

glClearColor(0.3 f.0.2 f.0.3 f.1.0 f);
glClear(GL_COLOR_BUFFER_BIT);
Copy the code