OpenGL 1. Configure the environment and create a window

Reference video
LearnOpenGL

Configure GLFW

First create a C++ empty project with VS, and download GLFW from the official website. GLFW is a C language library specifically for OpenGL, which provides some minimal interfaces required for rendering objects. Download the precompiled binaries from the Download page of the official website. If there is an incompatibility problem, refer to the method in LearnOpenGL, first download the source code package, and then use CMAKE to generate the file.
insert image description here
insert image description here
To decompress the file, you need two folders include and lib-vc2019. I installed VS2019, and select the corresponding folder for other versions.
Create a new Dependencies folder in the root directory of the project, indicating all dependent files, create a new GLFW folder at the lower level, copy include and lib-vc2019, and the directory structure is as follows. The dll files in
insert image description here
lib-vc2019 do not need to be deleted
insert image description here

Right-click the solution in VS – Properties, select all configurations, and select x64 or Win32 as the platform.
Add $(SolutionDir)Dependencies\GLFW\include in C/C++ - General - Additional Include Directories, where $(SolutionDir) represents the root directory of the project.
insert image description here

Then add $(SolutionDir)Dependencies\GLFW\lib-vc2019 in Linker – General – Additional Library Directory

insert image description here
Add the two libraries glfw3.lib and opengl32.lib in the linker - input - additional dependencies, pay attention to the semicolon.

insert image description here

create window

Create a new src folder in the project, and add a script to write the code, copy the code under the Documentation page of the GLFW official website, right-click the solution, generate (Build) compile and run.
Application.cpp

#include <GLFW/glfw3.h>

int main(void)
{
    
    
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
    
    
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
    
    
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

Finally, finally saw the window drawn with OpenGL.

draw triangle

Application.cpp

#include <GLFW/glfw3.h>
int main(void)
{
    
    
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
    
    
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
    
    
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

		//绘制三角形,即时模式
        glBegin(GL_TRIANGLES);
        glVertex2f(-0.5f, -0.5f);
        glVertex2f(0.0f, 0.5f);
        glVertex2f(0.5f, -0.5f);
        glEnd();

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

insert image description here

Configure GLEW

GLEW is a cross-platform open source C/C++ extension loading library. After downloading from the official website, extract it to the previous Dependencies folder, and change the name to GLEW for clarity.
insert image description here
The configuration process is similar to GLFW, except that a preprocessor definition GLEW_STATIC is added.
insert image description here
Application.cpp

//需要先引用GL/glew.h,不然会报错
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

int main(void)
{
    
    
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
    
    
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    //输出版本号
    std::cout << glGetString(GL_VERSION) << std::endl;

    //必须在创建上下文之后,初始化glew
    GLenum err = glewInit();
    if (GLEW_OK != err)
        std::cout << err << std::endl;

    //......

    return 0;
}

Guess you like

Origin blog.csdn.net/sinat_34014668/article/details/126506513