OpenGL 学习系列-Hello Word

我的电脑是windows,在电脑上学习Opengl,推荐开发工具 Clion
推荐一个很好的opengl学习网站 https://learnopengl-cn.readthedocs.io/zh/latest/01%20Getting%20started/03%20Hello%20Window/

话不多说,先创建一个Hello word 工程

1.先下载几个库
OpenGL 学习系列-Hello Word_第1张图片
需要的三方库.png
  1. freeglut 这个库最初由MarkKilgard编写,从OpenGL Redbook(红宝书)第二版起就用来作为示例程序的支持环境 GLUT因为其简单、可用性广、可移植性强,被广泛应用于各种OpenGL实际应用中
    https://www.transmissionzero.co.uk/software/freeglut-devel/
  2. glad 也是用来访问OpenGL规范接口的第三方库
    https://glad.dav1d.de/
  3. glfw 是一个专门针对OpenGL的C语言库,它提供了一些渲染物体所需的最低限度的接口。它允许用户创建OpenGL上下文,定义窗口参数以及处理用户输入
    http://www.glfw.org/download.html
  4. glew (OpenGL Extension Wrangler Library) 也是opengl额外的库,这个库封装了opengl复杂的操作,简化的我们opengl的API调用
    http://glew.sourceforge.net/

2.Clion 创建工程

OpenGL 学习系列-Hello Word_第2张图片
工程.png
  1. 本工程采用glad glew glfw 三个库
  2. 分别创建inclue lib src三个目录
  3. inclue主要是头文件,lib主要是需要链接的库
  4. src下面就一个glad源文件
3.CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(OpenglTest2)
# 指定支持的c++版本
set(CMAKE_CXX_STANDARD 14)
# 指定编译后的输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin)
# 指定include 和 链接lib
include_directories(include)
link_directories(lib)
add_executable(OpenglTest2 main.cpp src/glad.c)
# 把glfw 和 glew链接进来
target_link_libraries(OpenglTest2 libglfw3.a glew32s.lib)
4.测试文件
#include 

#include "glad/glad.h"
#include "GLFW/glfw3.h"
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);
}
5.运行结果如下
OpenGL 学习系列-Hello Word_第3张图片
程序.png

1.有用VS开发的配置工程会更简单一些,不过安装VS要电脑要牛逼点
2.Clion 开发工具学习OpenGL足够了

欲乘东风,先把工具环境齐全了,有疑问请 @我 [email protected] !!!

你可能感兴趣的:(OpenGL 学习系列-Hello Word)