C++/OpenGL 入门(1):关于VS2017 中OpenGL部分安装过程

  1. 参考书目:Computer Graphics Programming in OpenGL Using C++ by V Scott Gordon John L Clevenger (z-lib.org)
    下载方式: zlibrary 网站(https://zh.ng1lib.org/)中搜索书名

  2. OpenGL 官方帮助手册,网址:https://www.opengl.org/sdk/docs/man

  3. 书中的例子需要 GLM 库,在附录有教安装步骤,GLM 库 的文档,网址:
    https://glm.g-truc.net/0.9.9/index.html

  4. SOLI 库:用来加载图片纹理图像,网址: https://github.com/SpartanJ/soil2

  5. 3D 图形变成相关的书籍推荐:
    • (Sellers et al.) OpenGL SuperBible [SW15]
    • (Kessenich et al.) OpenGL Programming Guide [KS16] (the “red book”)
    • (Wolff) OpenGL 4 Shading Language Cookbook [WO13]
    • (Angel and Shreiner) Interactive Computer Graphics [AS14]
    • (Luna) Introduction to 3D Game Programming with DirectX 12 [LU16]

  6. 如何查找电脑的显卡能否支持什么版本的OpenGL
    ① 计算机管理 -> 设备管理器 -> 显示适配器 -> Interl® HD Graphics 620

  7. 用测试工具GPU_Caps_Viewer查看显卡支持的OpenGL的版本号,可能是 4.1.书中说最好支持4.3以上,所以不确定是否有影响
    在这里插入图片描述

  8. 下载了 GLview 免费软件,查看版本号,图中显示,可以支持 4.4的版本,应该能满足书中的要求,GLview 的压缩包,解压打开直接用即可,不需要安装
    在这里插入图片描述

  9. 确保 OpenGL 的版本号之后,需要编译 GLFW, 编译之前,需要安装 cmake,cmake 网址在:https://cmake.org
    cmake 下载, 网址:https://cmake.org/download/
    公众号安装教程: https://mp.weixin.qq.com/s/6Pu9qQgwoaT2WvX37uK_aw

下载 GLFW

 下载 glfw-3.3.6,网址是:https://www.glfw.org/
位置:E:\pg\code\220124 computer graphic\glfw-3.3.6
 下载 cmake,网址是:https://cmake.org/download/
位置:F:\cmake\cmake-3.22.1-windows-x86_64\bin
貌似直接打开 cmake-gui.exe 就能用,不需要安装,但是在电脑 cmd 输入 cmake –version看不到 cmake的信息。以下是 cmake 的界面
在这里插入图片描述

 打开cmake, 用 cmake 编译 glfw 文件夹,具体步骤是,
①“where is the source code”输入 glfw 文件夹所在位置“E:/pg/code/220124 computer graphic/glfw-3.3.6/glfw-3.3.6”
②“where to build the binaries”输入 cmake生成文件想要存储的位置“E:/pg/code/220124 computer graphic/CmakeGLFW”
③点击 “configure”,由于电脑装的是 vs2017, 注意跳出的窗口要选择 VS2017
④ 编译成功后,选择“generate”,在生成的文件夹中可以看到 “GLFW.sln”,用vs2017打开
⑤ VS2017 打开 “GLFW.sln”后,直接点击“本地 Windows 编译器”,即可直接编译
在这里插入图片描述
图 cmake 界面
在这里插入图片描述图 点击 configure 后弹窗,需要选择电脑安装的VS版本,电脑安装2017VS,所以选择2017
在这里插入图片描述
图 cmake 编译 GLFW 后生成的文件夹,其中 GLFW.sln 文件需要用 VS2017打开
在这里插入图片描述
图 VS2017 成功打开 GLFW.sln 后,直接点击 “本地 Windows 编译器”,VS开始编译
 结果:上述步骤,主要生成了三个文件,在后续工作中会用到
① 库 “glfw3.lib”位置 “E:\pg\code\220124 computer graphic\CmakeGLFW\src\Debug”,就是在cmake 编译 glfw 文件后生成的文件夹 CmakeGLFW 中
② 两个头文件,在 GLFW 文件夹中,位置是: 就是在,网上下载的 glfw 的文件夹 include 中,有 GLFW文件夹
E:\pg\code\220124 computer graphic\glfw-3.3.6\glfw-3.3.6\include\GLFW

下载 GLEW

 网址:http://glew.sourceforge.net/
① 注意下载的 window 版本下的,而不是普通的 zip
在这里插入图片描述

② 位置:E:\pg\code\220124 computer graphic\glew-2.1.0-win32
③ 需要的文件
文件 glew32.dll 位置: bin 文件夹 -> Release 文件夹 -> x64 文件夹
库 glew32.lib 位置: lib 文件夹 -> Release 文件夹 -> x64 文件夹
文件夹 GL 位置: include 文件夹 -> GL 文件夹

下载 GLM

 网址:https://github.com/g-truc/glm/releases/tag/0.9.8.0
 版本:0.9.8.0
 Csdn 参考教程: https://blog.csdn.net/Wonz5130/article/details/83116009
教程过程:
步骤是,在 VS 创建的项目所在文件夹中,将下载的 glm-0.9.8.0 复制进去,在 VS 界面中,将这个项目的属性中,“添加包括的目录 Additional Include Directories ”添加 glm 的文件夹,然后在项目的源代码里面输入头文件:

//glm
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> //需要什么变换,就导入什么文件,具体可以去官网看
#include <glm/gtc/type_ptr.hpp>

 需要的文件:压缩包解压后,glm文件夹

下载 SOIL2 库

 首先下载 premake 工具,需要用 premake5.exe
 Premake 下载网址 : https://premake.github.io/download
 SOIL2 下载网址: https://github.com/SpartanJ/soil2
 SOLIL2 和 premake 下载后的压缩包存储位置: E:\pg\code\220124 computer graphic
 将 premake5.exe 复制到 SOIL2 文件夹,然后在 SOIL2 文件夹的路径上 更改为 cmd,调出在这个路径下的命令窗口:
在这里插入图片描述
图 如何在 SOIL2 文件夹中,调出该路径下的命令窗口

 在 SOIL2 库所在路径下的cmd 窗口 输入:premake5 vs2017
因为电脑安装的是VS2017
 打开 SOIL2 文件夹 -> make 文件夹 -> windows 文件夹 ,位置为:
E:\pg\code\220124 computer graphic\SOIL2-master\SOIL2-master\make\windows
右键用 VS2017 打开 SOIL2.sln
图 右键用 VS2017 打开 SOIL2.sln
 打开 VS2017后,右键点击左边栏 “soil2-static-lib”-> “生成”
图 打开 VS2017后,右键点击左边栏 “soil2-static-lib”-> “生成”
 如果报错:
MSB8036 找不到 Windows SDK 版本 8.1。请安装所需版本的 Windows SDK,或者在项目属性页中或通过右键单击解决方案并选择“重定解决方案目标”来更改 SDK 版本。
解决方法:
参考网址:
https://blog.csdn.net/weixin_44981971/article/details/122035658?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_aa&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_aa&utm_relevant_index=2
实际步骤:
① “项目” -> “重定解决方案目标”
图 项目 -> 重定解决方案目标
② 在弹出窗口,默认选项,直接按确定
图 重定解决方案目标弹窗,选择确定
③ 右键选定 “soil2-static-lib”,选择“重定向项目”,弹窗中按照默认选项,选择确认
图 针对 soil2-static-lib 右键选择“重定向项目”
图 按照默认,直接点击确认
 ④ 重复报错前的那一步,右键点击左边栏 “soil2-static-lib”-> “生成”,此时就没有报错了

建立自己的 lib 库 和 include 库

 在自己喜欢的电脑储存位置上,建立一个文件夹,这里命名为 0126 template, 文件夹下再建立两个文件夹,分别命名 “lib”和“include”。另外,将 glew32.dll 文件复制进该文件夹,复制来源:
glew32.lib: E:\pg\code\220124 computer graphic\glew-2.1.0-win32\glew-2.1.0\bin\Release\Win32
图 新建好的 “220126 template ”文件夹里有两个子文件夹“include”和 “lib”
 “lib”文件夹中,三个文件:glew32.lib , glfw3.lib, soil2-debug.lib, 位置分别在:
glew32.lib:E:\pg\code\220124 computer graphic\glew-2.1.0-win32\glew-2.1.0\lib\Release\x64
glfw3.lib:E:\pg\code\220124 computer graphic\CmakeGLFW\src\Debug
soil2-debug.lib: E:\pg\code\220124 computer graphic\SOIL2-master\SOIL2-master\lib\windows
 “include”文件夹中,四个文件夹,GL, GLFW, glm, SOIL2, 位置分别在:
GL: E:\pg\code\220124 computer graphic\glew-2.1.0-win32\glew-2.1.0\include
GLFW: E:\pg\code\220124 computer graphic\glfw-3.3.6\glfw-3.3.6\include
glm: E:\pg\code\220124 computer graphic\glm-0.9.8.0
SOIL2: E:\pg\code\220124 computer graphic\SOIL2-master\SOIL2-master\src
 将所有文件复制完后的结构:
在这里插入图片描述
 以上为 PDF 370 – PDF 374 的内容,是附录A的内容

调试pdf 29 页的程序

 新建项目后,如何将现有文件夹加入vs项目中
参考教程:https://blog.csdn.net/weixin_44378800/article/details/106697612
 如何将现有 lib 库导入项目,右键项目,选择“添加”-> 现有项,将 lib 导入
在这里插入图片描述
 VS2017出现C4996 ‘fopen’: This function or variable may be unsafe. Consider using fopen_s instead.错误
参考教程:https://blog.csdn.net/qq_28114615/article/details/85683550
解决办法:
项目——属性——C/C++——预处理器——预处理器定义——右侧下拉框中“编辑”——在第一个编辑框中添加_CRT_SECURE_NO_WARNINGS——大功告成

 错误“main 函数已经在main.obj 文件中定义过”,解决办法:右键选中 main, 选择“查找所有引用”,查到 dummy.cpp 文件中也有一个 main 函数定义,由于 dummy.cpp 文件中没有任何有用信息,所以更改 里面的mian 函数名称为 main_dummy,重新编译解决问题
 编译报错,定义的 display 函数中调用了 glClearColor 和 glClear 函数,“无法解析的外部符号”,所以在项目属性中,配置属性 -> 链接器 -> 输入 -> 附加依赖项中,增加一个 OpenGL32.lib -> 确定 -> 确定
在这里插入图片描述
 需要XXX.lib或XXX.dll库。手动添加,项目->属性->配置属性->链接器->输入 然后在附件依赖项添加XXX.lib,再生成第一个无法解析的外部符号错误消失了。

 增加 OpenGL32.lib 附加依赖项之后,运行报错,vs运行时提示:应用程序无法正常启动(oxc000007b)。请单击"确定"关闭应用程序
打开生成的 “220126 template.exe”,位置是:
E:\pg\code\220124 computer graphic\220126 template\220126 template\Debug
这个exe 文件无法顺利打开,报错是“找不到 glew32.dll”
在这里插入图片描述
注意,这里是将 win32 文件夹中的 glew32.dll 放入电脑的 SysWOW64 文件夹中,也就是将32位的 dll 文件放入 系统对应存储64位的文件夹里。
就是从 E:\pg\code\220124 computer graphic\glew-2.1.0-win32\glew-2.1.0\bin\Release\Win32 文件夹中,复制 “glew32.dll” ,在C:\Windows\SysWOW64 文件夹中,粘贴刚刚复制的 dll 文件。
这样就可以顺利生成,结果如下:
在这里插入图片描述

分析2.1代码

#include "include\GL\glew.h"
#include "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) {
	if (!glfwInit()) { exit(EXIT_FAILURE); }
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	GLFWwindow* window = glfwCreateWindow(600, 600, "Chapter2 - program1", NULL, NULL);
	glfwMakeContextCurrent(window);
	if (glewInit() != GLEW_OK) { exit(EXIT_FAILURE); }
	glfwSwapInterval(1);
	init(window);
	while (!glfwWindowShouldClose(window)) {
		display(window, glfwGetTime());
		glfwSwapBuffers(window);
		glfwPollEvents();
	}
	glfwDestroyWindow(window);
	glfwTerminate();
	exit(EXIT_SUCCESS);

glClearColor(1.0, 0.0, 0.0, 1.0); // 四个数 (1,0,0,1)是 RGBA的格式
GLFWGLEW 库激活函数:

glfwInit()
glewInit()

glfwCreateWindow() : 用来创造 GLFW 窗口,长,宽,顶部的标题和 对应的内容
 window hints 窗口提示:提示机制必须兼容 openGL 4.3,就是代码中的:
 major = 4
 minor = 3
glfwSwapInterval() and glfwSwapBuffers() : 用来垂直同步?
glfwMakeContextCurrent() : 用来使得窗口的内容显示出来
display() : 循环函数
glfwSwapBuffers() : 涂屏幕窗口 paint the screen
glfwPollEvents() : 对类似键盘被按下的事件做出反应
glfwGetTime() : 返回GLFW 初始化的时间
glClear(GL_COLOR_BUFFER_BIT); 意思是,将颜色缓存器的颜色清理掉,重新提供预设好的颜色
glClearColor() 就是在清理颜色缓存器之前,要提供一个颜色,以便清理缓存器后提供
glfwDestroyWindow() and glfwTerminate() 结束这个窗口并停止
PDF31

 补充知识点:

vs中,include 其他文件夹的头文件时候,需要用 双引号 而不是<>,同时,创建名为test的解决方案项目时候,有一个文件夹 test,里面有 test.sln 和 test 文件夹,需要引用的头文件在子文件夹 test 中,那么就写作 include “GL/xxx.h”,其中 GL文件夹就在 test 子文件夹中。
在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值