OpenGL 创建窗口笔记

原教程地址

link.

头文件

包含glad和glfw:
#include
#include
包含 glad.h 和 glad.c

GLFW:针对OpenGL的C语言库
GLAD:简化获取函数位置的库,即管理函数指针


创建窗口

初始化窗口
		glfwInit();
		//指定OpenGL的版本号
    		glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    		glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
		//指定模式
    		glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
		//Mac OS X系统需要加上下面这行
    		//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
创建窗口对象
		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);
GLAD初始化
		if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
		{
		    std::cout << "Failed to initialize GLAD" << std::endl;
		    return -1;
		}
设置视口
		//视口就是渲染的窗口大小
		glViewport(0,0,800,600);
注册回调函数
		//回调函数注册在窗口创建后,渲染开始前
		glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);//窗口大小被调整时调用回调函数
		
		//会传window进来
		void framebuffer_size_callback(GLFWwindow* window, int width, int height)
		{
		    glViewport(0, 0, width, height);
		}
渲染
		//渲染循环(Render Loop)
		while(!glfwWindowShouldClose(window))
		{
		    processInput(window);//自己定义的函数,控制输入

		    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);//设置清屏的颜色
		    glClear(GL_COLOR_BUFFER_BIT);//清空缓冲,这里是颜色缓冲。清空缓存后,用设置的颜色填充
	
		    glfwSwapBuffers(window);	//交换颜色缓冲,双缓冲机制
		    glfwPollEvents();    //检查触发事件,然后调用回调函数
		}
		
	    
		void processInput(GLFWwindow *window)
		{
		    if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
		        glfwSetWindowShouldClose(window, true);
		}
释放资源
		glfwTerminate();

代码

原文代码地址:link

#include 
#include 

#include 

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); // uncomment this statement to fix compilation on OS X
#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);

    // glad: load all OpenGL function pointers
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        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);
}

你可能感兴趣的:(OpenGL)