Tutorial1
Tutorial1
{pkw23, wangsc23}@mails.tsinghua.edu.cn
The introduction of OpenGL
OpenGL
lObjects
Ø A collection of options represents a subset of OpenGL's state
Ø How to use
lCreate an object and store a reference to it as an id
lBind the object to the target location of the context
lSet the window options
lUn-bind the object
How to create a window
GLFW (Graphics Library Framework)
lDeployment
Ø Create a new project using Visual Studio
Ø Link GLFW library with the project
l Project properties -> Configuration properties -> VC++
Directories
l Add your own directories to Include Directories
and Library Directories
Setting up for Windows
lDeployment
Ø Create a new project using Visual Studio
Ø Link GLFW library with the project
l Project properties -> Configuration properties -> Linker ->
Input
l Add glfw3.lib in Additional Dependencies
Setting up for Windows
lCreate CMakeLists.txt.
lRun the following commands.
cmake ./
make
./main
Code
l Initialize GLAD
if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << “Failed to initialize GLAD” << std::endl;
return -1;
}
We pass GLAD the function to load the address of the OpenGL function
pointers which is OS-specific.
glfwGetProcAddress:
Defines the correct function based on which OS we're compiling for.
Code
lViewport
Ø Tell OpenGL the size of the rendering window via glViewport
glViewport(0, 0, 800, 600);
p The first two parameters set the location of the lower left corner of the window.
p The third and fourth parameters set the width and height of the rendering window
in pixels, which we set equal to GLFW's window size.
p If values of viewport dimensions are smaller than GLFW‘s dimensions, the OpenGL
rendering will be in a smaller window. This could be used for displaying multiple
viewports in a GLFW’s window
Code
lRender loop
Ø Keep drawing images and handling user input until the program is told to
stop
while(!glfwWindowShouldClose(window)){
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwWindowShouldClose:
Checks at the start of each loop iteration if GLFW has been instructed to close
glfwPollEvents: Checks if any events are triggered, updates the window state, and calls the
corresponding functions (Set via callback methods)
glfwSwapBuffers:
Swap the color buffer that is used to draw during this iteration and display it on the screen as
output
Code
lCallback function
Ø Respond to events
Ø Window resizing, keyboard input, mouse movement and so on
lExample: window resizing callback function
lDefine the callback function: If the window is resized, the viewport should
be adjusted.
void framebuffer_size_callback(GLFWwindow* window, int width, int height){
glViewport(0, 0, width, height);
}
lRegister the callback function, tell GLFW to call this function each time the
window is resized:
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
Code
lBuffer
p Single buffer
Ø Output image not drawn in an instant, but drawn pixel by pixel
Ø Displayed while still being rendered, may contain artifacts, display
flickering issues
p Double buffer
Ø front buffer: contains the final output image shown at the screen
Ø back buffer: all the rendering commands are drawn on the back buffer
Ø When all the rendering commands are finished, swap the back buffer
to the front buffer
Code
l Render
Ø Place all the rendering commands in the render loop
while(!glfwWindowShouldClose(window)) {
glfwPollEvents(); // trigger event
... // rendering commands
glfwSwapBuffers(window); // swap buffers
}
Code
l Render
Ø Example
Ø Clear the screen with a color
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);