OpenGL environment configuration and HelloWorld program

It is recommended to start learning OpenGL from 32-bit, because some details of 64-bit may require special processing, so beginners can temporarily ignore these special situations and start simple to reduce unnecessary trouble.

1. Rendering mode: immediate rendering mode and core mode

Immediate rendering mode (Immediate mode, that is, fixed rendering pipeline), drawing graphics in this mode is very convenient. The immediate rendering mode is indeed easy to use and understand, but it is too inefficient and is now obsolete.
The core model forces us to use modern functions. The advantages of modern functions are greater flexibility and efficiency, but they are also more difficult to learn.

2. State machine

OpenGL itself is a huge State Machine: a series of variables describe how OpenGL should behave at the moment. The state of OpenGL is usually called OpenGL context (Context). We usually use the following methods to change the OpenGL state: setting options, operating buffers. Finally, we use the current OpenGL context to render.

3. Object

Some abstraction layers were introduced during OpenGL development. "Object" is one of them. An object in OpenGL refers to a collection of options, which represents a subset of the OpenGL state. When we want to draw any of these models, we only need to bind an object containing the corresponding model data (of course, we need to create and set the options of the object first).

4. Use vs2015 to build opengl+glfw3+glad environment

4.1 opengl

The opengl library comes with the SDK of vs2015 and does not need to be installed

4.2 glfw3

The project address of glfw3: https://www.glfw.org/download.html
There are ready-made library files at this website. As a beginner, I recommend compiling it by yourself.
How to compile? Answer: Use CMake (you won’t find a few articles to read it, you can learn it in less than 10 minutes)
(1) Download the source code, and unzip it
(2) Open cmd, enter the source directory, and execute: cmake.
(3 ) After the previous step is executed successfully, the visual studio project will be generated (specifically, vs2015 or 2017, cmake will automatically configure it for you according to your development environment), enter the source code directory, open FLFW.sln, if you are learning, compile the 32-bit Release version enough.
(4) Copy include and glfw3.lib (static library) for use.

4.3 glad

This is the simplest, you can configure it directly, the configuration address: https://glad.dav1d.de/
Just fill in a gl version (confirm according to your own graphics card, if you can’t confirm it, choose version 3.3, other new versions are based on 3.3 It's expanded, the core has not changed and is compatible), finally click GENERATE, and then directly download glad.zip, there is an include and a glad.c, the include is loaded into the vs2015 project, and glad.c is directly added to your opengl project as the source code.

5. The first opengl+glfw3+glad project

Create a new empty console project, the console can be printed with printf or cout for easy debugging.
First add glad.c to the project
. Add the include of glfw3 and glad to the additional directory of the project
. Add 2 new ones in the dependent library: opengl32.lib and glfw3.lib.
Create a new file HelloWorld.cpp, copy the source code below to enter Compile and then run the program. The code comes from the opengl learning website, I added a little comment myself.


#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <iostream>

#define GL_PRINT(S) (printf("%s\n",#S),S)

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main()
{
    // glfw: initialize and configure
    // ------------------------------
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

    // glfw window creation
    // --------------------
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);//将该窗口作为当前线程的主上下文
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);//注册回调函数,这里是窗口大小改变时候调用framebuffer_size_callback,该函数还可以注册其他相关硬件的回调函数

    // glad: load all OpenGL function pointers
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))//初始化glad库,只要用glad库都这么写,没什么可说的,glfwGetProcAddress这个函数返回指定的OpenGL或OpenGL ES核心或扩展函数的地址,如果它被当前上下文所支持的话。
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    // render loop
    // -----------
    while (!glfwWindowShouldClose(window))//不阻塞,循环会一直执行
    {
        // input
        // -----
        processInput(window);

        // render
        // ------
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        // -------------------------------------------------------------------------------
        glfwSwapBuffers(window);//交换缓冲区,这里用了双缓冲技术,用两个内存区域来保存数据,分为前缓冲区和后缓冲区,前缓冲区用于展示屏幕上的内容,而后缓冲区就用来绘制,然后每一帧开始的时候,将两个缓冲区交换,这样后缓冲区又可以画新的内容
        glfwPollEvents();//轮询事件(如: 鼠标键盘事件)
    }

    // glfw: terminate, clearing all previously allocated GLFW resources.
    // ------------------------------------------------------------------
    glfwTerminate();
    return 0;
}

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    // make sure the viewport matches the new window dimensions; note that width and 
    // height will be significantly larger than specified on retina displays.
    glViewport(0, 0, width, height);
    GL_PRINT("Window Resize!");
}

Guess you like

Origin blog.51cto.com/14207158/2536415