阅读《计算机图形学编程(使用OpenGL和C++)》 - 清屏

本书介绍如何在C++中使用OpenGL(Open Graphics Library开放图像库)进行3D图形编程
Windows操作系统使用开发工具和库:
Visual Studio 2015
窗口管理 GLFW
扩展库 GLEW
数学库 GLM
纹理管理 SOIL2

硬件方面:GLSL(OpenGL着色语言)进行部分编程,写着色器代码,需要显卡GPU
软件方面:OpenGL的API是C语言编写,直接兼容C和C++。C++/OpenGL应用程序的一个重要任务是将GLSL代码安装到GPU上。C++/OpenGL应用发送图形数据到顶点着色器,随着管线处理(顶点着色器、曲面细分着色器、几何着色器、光栅化、片段着色器、像素操作),最终生成在显示器上显示的像素点。

GLFW库让我们可以在GLFWwindow类上进行3D场景绘制。我们先尝试创建一个GLFWwindow实例并为其设置背景色。
操作流程:
1、 初始化GLFW库
2、 实例化GLFWwindow
3、 初始化GLEW库
4、 调用一次init()函数(每个应用程序的初始化任务都放init()中)
5、 重复调用display()函数(绘制GLFWwindow的代码都放在display()中)

 其中,glClearColor()命令指定了清除背景时用的颜色值——这里(1,0,0,1)代表RGB值中的红色,末尾的1表示不透明度。使用OpenGL调用glClear(GL_COLOR_BUFFER_BIT),实际使用红色对颜色缓冲区进行填充。

代码如下

main.cpp

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

using namespace std;

void init(GLFWwindow* window){}

void display(GLFWwindow* window, double currentTime)
{
    glClearColor(1.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
}

int main(void)
{
    GLFWwindow* window;

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

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(400, 300, "Hello World", NULL, NULL); //没用到的参数分别用来允许全屏显示以及资源共享
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

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

    GLenum err = glewInit();
    if (err != GLEW_OK)
    {
        std::cout << "Error: " << glewGetErrorString(err) << std::endl;
    }
    glfwSwapInterval(1); // 交互缓冲区间隔设为1,即每帧更新一次,交换间隔表示交换缓冲区之前等待的帧数,通常称为Vsync垂直同步
    init(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        display(window, glfwGetTime()); // glfwGetTime()返回GLFW初始化之后经过的时间

        /* Swap front and back buffers */
        glfwSwapBuffers(window); // GLFW默认使用两个缓冲区

        /* Poll for and process events */
        glfwPollEvents(); // 处理窗口相关事件(如按键事件)
    }

    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

上图的框架是在下载GLFW库时官网展示的示例代码及加入一些书本内容。

OpenGL有很多预定义的常量,其中很多是枚举常量。

执行结果:

 解决方案使用的是 debug x86

依赖库下载的是

glew-2.1.0

glfw-3.3.6.bin.WIN32

glm-0.9.9.8

SOIL2-master

Computer Graphics Programming in OpenGL with C++ by V. Scott Gordon, John L. Clevenger English | 2019 | ISBN: 1683922216 | 384 Pages | EPUB | 29 MB This book provides step-by-step instruction on modern 3D graphics shader programming in OpenGL with C++, along with its theoretical foundations. It is appropriate both for computer science graphics courses and for professionals interested in mastering 3D graphics skills. It has been designed in a 4-color, “teach-yourself” format with numerous examples and detailed explanations. Every shader stage is explored, starting with the basics of modeling, lighting, textures, etc., up through advanced techniques such as tessellation, soft shadows, and generating realistic materials and environments. The book includes companion files with all of the source code, models, textures, skyboxes and normal maps used in the book. Features: Covers modern OpenGL 4.0+ shader programming in C++, with instructions for both PC/Windows and Macintosh. Illustrates every technique with running code examples. Everything needed to install the libraries, and complete source code for each example is provided and fully explained. Includes step-by-step instruction for using each GLSL programmable pipeline stage (vertex, tessellation, geometry, and fragment). Explores practical examples for modeling, lighting and shadows (including soft shadows), terrain, and 3D materials such as wood and marble. Explains how to optimize code for performance, and use modern development tools such as the NVIDIA® Nsight™ debugger. Includes companion files with all of the code, object models, figures, textures, skyboxes and skydomes, height and normal maps used throughout the book.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值