Skip to content

Commit c370d70

Browse files
committed
1.Model Load Completed
2.use stb_image.h to load texture instead of SOIL.h Why? GL_EXTENSIONS has been deprecated as a paremeter to glGetString. All calls to glGetString( GL_EXTENSIONS ) fail under OpenGL 3+ with error GL_INVALID_ENUM. This breaks all query_x_capability functions... which breaks most of SOIL. Check kbranigan/Simple-OpenGL-Image-Library#8
1 parent b9a594d commit c370d70

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+61185
-489
lines changed

Diff for: Debug/LearnOpenGL.exe

126 KB
Binary file not shown.
File renamed without changes.

Diff for: Debug/zlibd1.dll

154 KB
Binary file not shown.

Diff for: HelloTexture.h

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#pragma once
22
#include "Painter.h"
3-
#include "SOIL.h"
43
#include "Resource.h"
54

65
class HelloTexture :public Painter

Diff for: LearnOpenGL.vcxproj

+6-1
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,12 @@
155155
<ClCompile Include="include\imgui\imgui_impl_glfw.cpp" />
156156
<ClCompile Include="include\imgui\imgui_impl_opengl3.cpp" />
157157
<ClCompile Include="HelloLightCasters.cpp" />
158+
<ClCompile Include="include\stb_image\stb_image.cpp" />
158159
<ClCompile Include="main.cpp" />
159160
<ClCompile Include="HelloShader.cpp" />
160161
<ClCompile Include="Mesh.cpp" />
162+
<ClCompile Include="Model.cpp" />
163+
<ClCompile Include="ModelPainter.cpp" />
161164
<ClCompile Include="Mouse.cpp" />
162165
<ClCompile Include="Resource.cpp" />
163166
<ClCompile Include="Shader.cpp" />
@@ -260,9 +263,11 @@
260263
<ClInclude Include="include\imgui\imstb_textedit.h" />
261264
<ClInclude Include="include\khrplatform.h" />
262265
<ClInclude Include="HelloShader.h" />
263-
<ClInclude Include="include\SOIL.h" />
264266
<ClInclude Include="HelloLightCasters.h" />
267+
<ClInclude Include="include\stb_image\stb_image.h" />
265268
<ClInclude Include="Mesh.h" />
269+
<ClInclude Include="Model.h" />
270+
<ClInclude Include="ModelPainter.h" />
266271
<ClInclude Include="Mouse.h" />
267272
<ClInclude Include="Painter.h" />
268273
<ClInclude Include="Resource.h" />

Diff for: LearnOpenGL.vcxproj.filters

+24-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949
<Filter Include="12_Mesh">
5050
<UniqueIdentifier>{4b80d85b-704c-40f8-961d-59db6514584d}</UniqueIdentifier>
5151
</Filter>
52+
<Filter Include="13_Model">
53+
<UniqueIdentifier>{a6695adb-8455-4d8c-9fe1-8fcc7011372d}</UniqueIdentifier>
54+
</Filter>
55+
<Filter Include="lib\stb_image">
56+
<UniqueIdentifier>{1a0dc422-6439-4f39-b825-6d7c05cc9632}</UniqueIdentifier>
57+
</Filter>
5258
</ItemGroup>
5359
<ItemGroup>
5460
<ClCompile Include="HelloShader.cpp">
@@ -114,6 +120,15 @@
114120
<ClCompile Include="Mesh.cpp">
115121
<Filter>12_Mesh</Filter>
116122
</ClCompile>
123+
<ClCompile Include="Model.cpp">
124+
<Filter>13_Model</Filter>
125+
</ClCompile>
126+
<ClCompile Include="ModelPainter.cpp">
127+
<Filter>13_Model</Filter>
128+
</ClCompile>
129+
<ClCompile Include="include\stb_image\stb_image.cpp">
130+
<Filter>lib\stb_image</Filter>
131+
</ClCompile>
117132
</ItemGroup>
118133
<ItemGroup>
119134
<ClInclude Include="HelloShader.h">
@@ -134,9 +149,6 @@
134149
<ClInclude Include="include\GL\glxew.h">
135150
<Filter>lib</Filter>
136151
</ClInclude>
137-
<ClInclude Include="include\SOIL.h">
138-
<Filter>lib</Filter>
139-
</ClInclude>
140152
<ClInclude Include="HelloTexture.h">
141153
<Filter>3_HelloTexture</Filter>
142154
</ClInclude>
@@ -429,6 +441,15 @@
429441
<ClInclude Include="Mesh.h">
430442
<Filter>12_Mesh</Filter>
431443
</ClInclude>
444+
<ClInclude Include="Model.h">
445+
<Filter>13_Model</Filter>
446+
</ClInclude>
447+
<ClInclude Include="ModelPainter.h">
448+
<Filter>13_Model</Filter>
449+
</ClInclude>
450+
<ClInclude Include="include\stb_image\stb_image.h">
451+
<Filter>lib\stb_image</Filter>
452+
</ClInclude>
432453
</ItemGroup>
433454
<ItemGroup>
434455
<None Include="include\assimp\vector3.inl">

Diff for: Libs/assimp-vc142-mt.dll

16.7 MB
Binary file not shown.

Diff for: Mesh.cpp

+58-40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#include "Mesh.h"
22

3+
const char* DIFFUSE_TEX_NAME = "texture_diffuse";
4+
const char* SPECULAR_TEX_NAME = "texture_specular";
5+
const char* NORMAL_TEX_NAME = "texture_normal";
6+
const char* HEIGHT_TEX_NAME = "texture_height";
7+
38
Mesh::Mesh(vector<Vertex> vertices, vector<GLuint> indices, vector<Texture> textures)
49
{
510
this->vertices = vertices;
@@ -11,60 +16,73 @@ Mesh::Mesh(vector<Vertex> vertices, vector<GLuint> indices, vector<Texture> text
1116

1217
Mesh::~Mesh()
1318
{
14-
glDeleteBuffers(1, &VBO);
15-
glDeleteBuffers(1, &EBO);
16-
glDeleteVertexArrays(1, &VAO);
19+
//glDeleteBuffers(1, &VBO);
20+
//glDeleteBuffers(1, &EBO);
21+
//glDeleteVertexArrays(1, &VAO);
1722
}
1823

1924
void Mesh::Draw(Shader& shader)
2025
{
21-
GLuint diffuseNr = 1;
22-
GLuint specularNr = 1;
23-
for (size_t i = 0; i < textures.size(); ++i)
24-
{
25-
glActiveTexture(GL_TEXTURE0 + i);
26-
string number;
27-
string name = textures[i].type;
28-
if (name == DIFFUSE_TEX_NAME)
29-
number = std::to_string(diffuseNr++);
30-
else if (name == SPECULAR_TEX_NAME)
31-
number = std::to_string(specularNr++);
26+
unsigned int diffuseNr = 1;
27+
unsigned int specularNr = 1;
28+
unsigned int normalNr = 1;
29+
unsigned int heightNr = 1;
30+
for (unsigned int i = 0; i < textures.size(); i++)
31+
{
32+
glActiveTexture(GL_TEXTURE0 + i); // active proper texture unit before binding
33+
// retrieve texture number (the N in diffuse_textureN)
34+
string number;
35+
string name = textures[i].type;
36+
if (name == DIFFUSE_TEX_NAME)
37+
number = std::to_string(diffuseNr++);
38+
else if (name == SPECULAR_TEX_NAME)
39+
number = std::to_string(specularNr++); // transfer unsigned int to stream
40+
else if (name == NORMAL_TEX_NAME)
41+
number = std::to_string(normalNr++); // transfer unsigned int to stream
42+
else if (name == HEIGHT_TEX_NAME)
43+
number = std::to_string(heightNr++); // transfer unsigned int to stream
3244

33-
shader.setFloat("material" + name + number, i);
34-
glBindTexture(GL_TEXTURE_2D, textures[i].id);
35-
}
36-
glActiveTexture(GL_TEXTURE0);
45+
shader.setInt((name + number).c_str(), i);
46+
glBindTexture(GL_TEXTURE_2D, textures[i].id);
47+
}
48+
glActiveTexture(GL_TEXTURE0);
3749

38-
//Draw
39-
glBindVertexArray(VAO);
40-
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
41-
glBindVertexArray(0);
50+
glBindVertexArray(VAO);
51+
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
52+
glBindVertexArray(0);
4253
}
4354

4455
void Mesh::SetUpMesh()
4556
{
46-
glGenVertexArrays(1, &VAO);
47-
glGenBuffers(1, &VBO);
48-
glGenBuffers(1, &EBO);
57+
glGenVertexArrays(1, &VAO);
58+
glGenBuffers(1, &VBO);
59+
glGenBuffers(1, &EBO);
60+
61+
glBindVertexArray(VAO);
4962

50-
glBindVertexArray(VAO);
51-
glBindBuffer(GL_ARRAY_BUFFER, VBO);
63+
glBindBuffer(GL_ARRAY_BUFFER, VBO);
5264

53-
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
65+
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
5466

55-
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
56-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
67+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
68+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
5769

58-
//Position
59-
glEnableVertexAttribArray(0);
60-
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex), (void*)0);
61-
//Normal
62-
glEnableVertexAttribArray(1);
63-
glVertexAttribPointer(1, 3, GL_FLOAT, false, sizeof(Vertex), (void*)offsetof(Vertex, Normal) );
64-
//Texture Coordinates
65-
glEnableVertexAttribArray(2);
66-
glVertexAttribPointer(2, 2, GL_FLOAT, false, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
70+
// vertex Positions
71+
glEnableVertexAttribArray(0);
72+
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
73+
// vertex normals
74+
glEnableVertexAttribArray(1);
75+
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
76+
// vertex texture coords
77+
glEnableVertexAttribArray(2);
78+
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
79+
// vertex tangent
80+
glEnableVertexAttribArray(3);
81+
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Tangent));
82+
// vertex bitangent
83+
glEnableVertexAttribArray(4);
84+
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Bitangent));
6785

68-
glBindVertexArray(0);
86+
glBindVertexArray(0);
6987

7088
}

Diff for: Mesh.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include "glheader.h"
3+
#include <assimp/types.h>
34

45
using std::vector;
56
using std::string;
@@ -9,18 +10,17 @@ struct Vertex
910
glm::vec3 Position;
1011
glm::vec3 Normal;
1112
glm::vec2 TexCoords;
13+
glm::vec3 Tangent;
14+
glm::vec3 Bitangent;
1215
};
1316

1417
struct Texture
1518
{
1619
GLuint id;
1720
std::string type;
21+
aiString Path;
1822
};
1923

20-
21-
const char* DIFFUSE_TEX_NAME = "texture_diffuse";
22-
const char* SPECULAR_TEX_NAME = "texture_specular";
23-
2424
class Mesh
2525
{
2626
public:

Diff for: Model.cpp

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include "Model.h"
2+
3+
void Model::Draw(Shader& shader)
4+
{
5+
for (auto mesh : meshes)
6+
{
7+
mesh.Draw(shader);
8+
}
9+
}
10+
11+
void Model::LoadModel(string path)
12+
{
13+
Assimp::Importer importer;
14+
const aiScene* scene = importer.ReadFile(path, aiProcess_FlipUVs | aiProcess_Triangulate);
15+
if (scene == nullptr || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
16+
{
17+
std::cout << "ERROR::ASSIMP::" << importer.GetErrorString() << std::endl;
18+
return;
19+
}
20+
directory = path.substr(0, path.find_last_of('/'));
21+
ProcessNode(scene->mRootNode, scene);
22+
}
23+
24+
void Model::ProcessNode(aiNode* node, const aiScene* scene)
25+
{
26+
for (auto i = 0; i < node->mNumMeshes; ++i)
27+
{
28+
auto mesh = scene->mMeshes[node->mMeshes[i]];
29+
meshes.push_back(ProcessMesh(mesh, scene));
30+
}
31+
32+
for (auto i = 0; i < node->mNumChildren; ++i)
33+
{
34+
ProcessNode(node->mChildren[i], scene);
35+
}
36+
}
37+
38+
Mesh Model::ProcessMesh(aiMesh* mesh, const aiScene* scene)
39+
{
40+
vector<Vertex> vertices;
41+
vector<GLuint> indices;
42+
vector<Texture> textures;
43+
44+
//Position
45+
for (auto i = 0; i < mesh->mNumVertices; ++i)
46+
{
47+
Vertex vertex;
48+
glm::vec3 vec(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z);
49+
vertex.Position = vec;
50+
51+
vec.x = mesh->mNormals[i].x;
52+
vec.y = mesh->mNormals[i].y;
53+
vec.z = mesh->mNormals[i].z;
54+
vertex.Normal = vec;
55+
56+
if (mesh->mTextureCoords[0])
57+
{
58+
vertex.TexCoords = glm::vec2(mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y);
59+
}
60+
else
61+
{
62+
vertex.TexCoords = glm::vec2(0);
63+
}
64+
65+
vertices.push_back(vertex);
66+
}
67+
68+
//Indices
69+
for (unsigned int i = 0; i < mesh->mNumFaces; i++)
70+
{
71+
aiFace face = mesh->mFaces[i];
72+
for (unsigned int j = 0; j < face.mNumIndices; j++)
73+
indices.push_back(face.mIndices[j]);
74+
}
75+
76+
//Material
77+
if (mesh->mMaterialIndex >= 0)
78+
{
79+
auto material = scene->mMaterials[mesh->mMaterialIndex];
80+
vector<Texture> diffuseMaps = LoadMaterialTextures(material,
81+
aiTextureType_DIFFUSE, "texture_diffuse");
82+
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
83+
vector<Texture> specularMaps = LoadMaterialTextures(material,
84+
aiTextureType_SPECULAR, "texture_specular");
85+
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
86+
}
87+
return Mesh(vertices, indices, textures);
88+
}
89+
90+
vector<Texture> Model::LoadMaterialTextures(aiMaterial* mat, aiTextureType type, string typeName)
91+
{
92+
vector<Texture> textures;
93+
94+
for (auto i = 0; i < mat->GetTextureCount(type); ++i)
95+
{
96+
aiString name;
97+
mat->GetTexture(type, i, &name);
98+
auto toFind = textures_loaded.find(name.C_Str());
99+
if (toFind != textures_loaded.end())
100+
{
101+
textures.push_back(toFind->second);
102+
}
103+
else
104+
{
105+
Texture tex;
106+
auto filePath = StringUtil::Format("%s/%s", directory.c_str(), name.C_Str());
107+
tex.id = Resource::LoadTexture(filePath.c_str(), GL_REPEAT, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR);
108+
tex.type = typeName;
109+
tex.Path = name;
110+
textures.push_back(tex);
111+
textures_loaded[name.C_Str()] = tex;
112+
}
113+
}
114+
115+
return textures;
116+
}

Diff for: Model.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include "glheader.h"
4+
#include "Mesh.h"
5+
6+
#include <assimp/Importer.hpp>
7+
#include <assimp/scene.h>
8+
#include <assimp/postprocess.h>
9+
#include<map>
10+
11+
using std::vector;
12+
using std::string;
13+
using std::map;
14+
15+
class Model
16+
{
17+
public:
18+
/* º¯Êý */
19+
Model(const char* path)
20+
{
21+
LoadModel(path);
22+
}
23+
void Draw(Shader& shader);
24+
private:
25+
/* Ä£ÐÍÊý¾Ý */
26+
vector<Mesh> meshes;
27+
string directory;
28+
map<string, Texture> textures_loaded;
29+
30+
void LoadModel(string path);
31+
void ProcessNode(aiNode* node, const aiScene* scene);
32+
Mesh ProcessMesh(aiMesh* mesh, const aiScene* scene);
33+
vector<Texture> LoadMaterialTextures(aiMaterial* mat, aiTextureType type,
34+
string typeName);
35+
};
36+

0 commit comments

Comments
 (0)