Road OpenGL learning (2) - Window

  The actual OpenGL library developers usually graphics producers. OpenGL version of the card you buy are supported by this series of graphics cards specially developed. You can use OpenGL Extensions View mentioned last time to see the OpenGL version, me is OpenGL4.3.

GLAD's choice before time kernel mode selection of OpenGL (Core-profile) to develop high efficiency, flexible, and developers can learn about specific operations of OpenGL.

The original rendering immediate mode (Immediate mode, which is fixed rendering pipeline ) easy to use though inefficient and inflexible.

 

 1. State Machine --OpenGL state is generally referred context (Context)

Using a series of variables describing how OpenGL to work.

    Change the OpenGL context state variables change - setting options, operating cushion - using the current OpenGL rendering context.

 

A subset of some set of options, which represents the state of OpenGL - 2. Object

unsigned int objectId = 0;// 创建对象
glGenObject(1, &objectId);
glBindObject(GL_WINDOW_TARGET, objectId);
// 绑定对象至上下文// 设置当前绑定到 GL_WINDOW_TARGET 的对象的一些选项
glSetObjectOption(GL_WINDOW_TARGET, GL_OPTION_WINDOW_WIDTH, 800);
glSetObjectOption(GL_WINDOW_TARGET, GL_OPTION_WINDOW_HEIGHT, 600);
// 将上下文对象设为默认
glBindObject(GL_WINDOW_TARGET, 0);

 

Start creating the window

1) Examples of the window GLFW

    glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);

glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //OpenGL版本4.3

//使用的是核心模式(Core-profile)

    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

 

2) Create a window

GLFWwindow* window = 

glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);

if (window == NULL)

{

std::cout << "Failed to create GLFW window" << std::endl;

glfwTerminate();

return -1;

}

glfwMakeContextCurrent(window);

// create a finished window we can set our notice GLFW the context of the main window context for the current thread

 

3) Initialization GLAD - Management function pointer OpenGL

if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}

function pointer address OpenGL functions related to system load. GLFW to us is glfwGetProcAddressthat it defines the correct function According to our compilation system.

 

4) the viewport (the Viewport)

The location coordinates in OpenGL as your screen coordinates. For example, in the OpenGL coordinates (-0.5, 0.5) it is possible to (eventually) is mapped to the screen coordinates (200,450). Note that, treated only OpenGL coordinate range -1 to 1, so we will actually map coordinates (-1 to 1) to (0, 800), and (0, 600).  

glViewport(0, 0, 800, 600);

Window to register a callback function (Callback Function), it will be called every time the window was resized

framebuffer_size_callback(GLFWwindow* window, int width, int height);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

5) render loop (Render Loop)

while(!glfwWindowShouldClose(window))
{

// Render command

    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

 

   // 检查并调用事件,交换缓冲

glfwSwapBuffers(window);

 glfwPollEvents();

}

 

 

 

Guess you like

Origin www.cnblogs.com/AmyBKLP/p/11876011.html