Lavender's Blog

浩瀚众星,皆降为尘

此刻,让心安静下来

准备工作

#学习网站

LearnOpenGL CN: https://learnopengl-cn.github.io/

#依赖开源项目

  1. Assimp:https://github.com/assimp/assimp (注:这个需要编译,后续会说,直接下载源文件不管用)

下载Assimp: http://assimp.org/index.php/downloads

  1. GLFW: https://github.com/glfw/glfw (注:这个因为需要lib文件,所以需要官网下载相应的库文件等)

下载GLFW: https://www.glfw.org/download.html

  1. GLM: https://github.com/g-truc/glm

  2. STB: https://github.com/nothings/stb (注:其实只用到一个关键文件 stb_image.h ,直接手动添加头文件也可)

  3. GLAD下载: https://glad.dav1d.de/

#IDE

Visual Studio 16 2019 (VS 2019)

(注:一定要选定好统一的调试模式,例如 Debug 模式 / x64,后续的Assimp编译模式也要与此统一:Debug 模式 / x64,以及GLFW的相应文件下载也是x64的)

另外,本人习惯:所有后续的include文件(.h 包含目录)、lib文件(.lib 库目录)都是使用的VS 2019默认的目录,应该是 Microsoft Visual Studio(VS2019安装目录)\2019\Community\VC\Tools\MSVC\14.28.29910 里面的 include 文件夹和 lib 文件夹,查看方法:打开菜单->项目->(项目)属性->VC ++目录->包含目录->编辑,在下方计算的值即可看到目录位置;也可以自定义相应的目录,在上述位置中修改添加等。

#软件

Cmake: https://cmake.org/download/

需要cmake对assimp进行编译

当快捷键 win + R 输入 cmd ,输入 cmake --help 显示

Usage
  cmake [options] <path-to-source>
  cmake [options] <path-to-existing-build>
  cmake [options] -S <path-to-source> -B <path-to-build>
Specify a source directory to (re-)generate a build system for it in the
current working directory.  Specify an existing build directory to
re-generate its build system.
                                   · · · · · ·

类似如上,即成功

正式开始

#1、新建项目

打开VS 2019,新建一个普通的控制台项目即可;

以下以LearnOpenGL CN上项目为例:

打开https://learnopengl.com/code_viewer_gh.php?code=src/3.model_loading/1.model_loading/model_loading.cpp

将源码复制到对应工程的源文件中,此时会有很多报错,不要急,因为缺少相应的头文件等

#2、配置导入头文件(重要)

#(1) GLAD

打开网站:https://glad.dav1d.de/,在API里选择对应的opengl版本下载即可,本项目以opengl版本为4.1为例,最后点击下方的 Generate 下载;

下载 glad.zip ,解压后找到 glad/src 下的 glad.c ,添加到项目->源文件中,找到 glad/include/glad 文件夹,将其复制到VS 2019对应的包含目录中(开头已经说明)。

#(2) GLFW

打开网站:https://www.glfw.org/download.html,我是Windows平台,64位,所以点击 64-bit Windows binaries 下载,解压后打开 lib-vc2019 ,将里面的lib文件复制到 VS 2019 对应的库目录 x64 文件夹中;

接下来,打开菜单->项目->属性,找见链接器->输入->附加依赖项->编辑,输入 glfw3.lib ,确定;

下载https://github.com/glfw/glfw/tree/master/include里的GLFW文件夹,解压后复制GLFW文件夹至VS 2019对应的包含目录中。

#(3) GLM

下载https://github.com/g-truc/glm,复制里面的glm文件夹至VS 2019对应的包含目录中即可。

#(4) LearnOpenGL头文件问题

打开https://learnopengl.com/code_viewer_gh.php?code=src/3.model_loading/1.model_loading/model_loading.cpp

鼠标移动至 #include <learnopengl/shader_m.h> 中,点击,即可跳转至shader_m.h文件,在项目中头文件右键,添加新建项,命名为shader_m.h,复制代码至文件中;

后续的 camera.h 和 model.h 添加方法相同,都是新建头文件,复制代码进去;对用网站地址:

shader_m.h: https://learnopengl.com/code_viewer_gh.php?code=includes/learnopengl/shader.h

camera.h: https://learnopengl.com/code_viewer_gh.php?code=includes/learnopengl/camera.h

model.h: https://learnopengl.com/code_viewer_gh.php?code=includes/learnopengl/model.h

然后,将头文件由(例) #include <learnopengl/shader_m.h> 改成 #include "shader_m.h" ,后面的camera.h和model.h相同

#(5) model.h头文件问题

此时shader_m.h文件和camera.h文件都没有错误,但model.h文件还需要配置一些头文件,包括stb_image.h文件、assimp库、mesh.h和shader.h,下面先说明mesh.h和shader.h

和(4)一样,都是新建头文件,复制对应的代码进去;对用网站地址:

mesh.h: https://learnopengl.com/code_viewer_gh.php?code=includes/learnopengl/mesh.h

shader.h: https://learnopengl.com/code_viewer_gh.php?code=includes/learnopengl/shader.h

然后,将头文件由(例) #include <learnopengl/mesh.h> 改成 #include "mesh.h" ,后面的shader.h相同

注:mesh.h中也引入了shader.h,也需要改一下

#(6) stb_image.h头文件问题

项目头文件新建.h文件命名为stb_image.h

LearnOpenGL网站地址如下:https://learnopengl-cn.github.io/01 Getting started/06 Textures/#stb_imageh,可作为了解参考

代码地址:https://github.com/nothings/stb/blob/master/stb_image.h,复制代码至新建的头文件中,即可,头文件改成 #include "stb_image.h"

重要⭐⭐⭐:这时直接引用会报错,代码stb_image.h里有如下说明:

Do this:
   #define STB_IMAGE_IMPLEMENTATION
before you include this file in *one* C or C++ file to create the implementation.

因此,要在 #include "stb_image.h" 引用前加一行,如下:

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

#(7) assimp库(核心)

下载Assimp: http://assimp.org/index.php/downloads,选择 Get the sourcecode 或者直接访问https://github.com/assimp/assimp/releases/tag/v5.0.1下载 Source code (zip) (以这个5.0.1版本为例)

解压assimp-5.0.1,新建文件夹build,按住 Shift 键,右键,选择 在此处打开Powershell窗口 ,输入以下命令:

cmake .. -G "Visual Studio 16 2019"

回车键,如图生成以下文件即成功:

image-20210410225638071

打开.sln(Solution文件),注意编译环境是Debug / x64,选择ALL_BUILD,生成

image-20210410234255092

等待生成成功,输出窗口出现以下即为成功:

image-20210410230354912

复制 assimp-5.0.1\build\include\assimp 文件夹下的 config.h 到 assimp-5.0.1\include\assimp 文件夹里

复制下载的 assimp-5.0.1\include 文件夹里的 assimp 文件夹至VS 2019对应的包含目录中。

复制 assimp-5.0.1\build\code\Debug 里的除.dll文件到 VS 2019 对应的库目录 x64 文件夹中。

注意:此时运行可能会出现如下问题:

image-20210410231413293

解决方法:

将 assimp-5.0.1\build\code\Debug 里的 assimp-vc142-mtd.dll 复制到 项目文件夹/x64/Debug 文件夹下

,或者复制到系统的 C:/Windows/System32 文件夹下(因为是x64下)

接下来,打开菜单->项目->属性,找见链接器->输入->附加依赖项->编辑,输入 assimp-vc142-mtd.dll ,确定;

#(8) 模型问题

打开主函数文件,大概在80行代码处有以下代码

	// build and compile shaders
    // -------------------------
    Shader ourShader("1.model_loading.vs", "1.model_loading.fs");
    // load models
    // -----------
    Model ourModel(FileSystem::getPath("resources/objects/backpack/backpack.obj"));

其中 1.model_loading.vs 和 1.model_loading.fs 都是shader文件, backpack.obj 是OBJ格式的模型文件

接下来,先搞定shader文件,分别在项目源文件新建文件,分别命名为 1.model_loading.vs 和 1.model_loading.fs ,

访问网站,粘贴代码,网站和代码如下:

1.model_loading.vs

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
out vec2 TexCoords;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
    TexCoords = aTexCoords;    
    gl_Position = projection * view * model * vec4(aPos, 1.0);
}

1.model_loading.fs

#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D texture_diffuse1;
void main()
{    
    FragColor = texture(texture_diffuse1, TexCoords);
}

接下来搞定obj模型文件,按照 resources/objects/backpack/backpack.obj 新建文件夹也可,自己定义路径也可,我放在与.sln同一级文件夹下新建的resources/objects/backpack目录下,即 ../resources/objects/backpack/

下载模型nanosuit.rar: https://learnopengl-cn.github.io/data/nanosuit.rar

然后解压,复制所以文件至 ../resources/objects/backpack/ 下,对于我这种代码改成

// load models
    // -----------
    Model ourModel(FileSystem::getPath("../resources/objects/backpack/nanosuit.obj"));

最后,上方代码会报错,是关于FileSystem相关的,改成如下就正确了

    // build and compile shaders
    // -------------------------
    Shader ourShader("1.model_loading.vs", "1.model_loading.fs");
    // load models
    // -----------
    Model ourModel("../resources/objects/backpack/nanosuit.obj");

最终成果

运行如下:

image-20210410233446712

成功!

更新于 阅读次数 … 次

请我喝[茶]~( ̄▽ ̄)~*

Lavender 微信支付

微信支付

Lavender 支付宝

支付宝