Skip to content

Commit

Permalink
1.Model Load Completed
Browse files Browse the repository at this point in the history
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
  • Loading branch information
0kk470 committed Aug 7, 2020
1 parent b9a594d commit c370d70
Show file tree
Hide file tree
Showing 46 changed files with 61,185 additions and 489 deletions.
Binary file modified Debug/LearnOpenGL.exe
Binary file not shown.
File renamed without changes.
Binary file added Debug/zlibd1.dll
Binary file not shown.
1 change: 0 additions & 1 deletion HelloTexture.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once
#include "Painter.h"
#include "SOIL.h"
#include "Resource.h"

class HelloTexture :public Painter
Expand Down
7 changes: 6 additions & 1 deletion LearnOpenGL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,12 @@
<ClCompile Include="include\imgui\imgui_impl_glfw.cpp" />
<ClCompile Include="include\imgui\imgui_impl_opengl3.cpp" />
<ClCompile Include="HelloLightCasters.cpp" />
<ClCompile Include="include\stb_image\stb_image.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="HelloShader.cpp" />
<ClCompile Include="Mesh.cpp" />
<ClCompile Include="Model.cpp" />
<ClCompile Include="ModelPainter.cpp" />
<ClCompile Include="Mouse.cpp" />
<ClCompile Include="Resource.cpp" />
<ClCompile Include="Shader.cpp" />
Expand Down Expand Up @@ -260,9 +263,11 @@
<ClInclude Include="include\imgui\imstb_textedit.h" />
<ClInclude Include="include\khrplatform.h" />
<ClInclude Include="HelloShader.h" />
<ClInclude Include="include\SOIL.h" />
<ClInclude Include="HelloLightCasters.h" />
<ClInclude Include="include\stb_image\stb_image.h" />
<ClInclude Include="Mesh.h" />
<ClInclude Include="Model.h" />
<ClInclude Include="ModelPainter.h" />
<ClInclude Include="Mouse.h" />
<ClInclude Include="Painter.h" />
<ClInclude Include="Resource.h" />
Expand Down
27 changes: 24 additions & 3 deletions LearnOpenGL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
<Filter Include="12_Mesh">
<UniqueIdentifier>{4b80d85b-704c-40f8-961d-59db6514584d}</UniqueIdentifier>
</Filter>
<Filter Include="13_Model">
<UniqueIdentifier>{a6695adb-8455-4d8c-9fe1-8fcc7011372d}</UniqueIdentifier>
</Filter>
<Filter Include="lib\stb_image">
<UniqueIdentifier>{1a0dc422-6439-4f39-b825-6d7c05cc9632}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="HelloShader.cpp">
Expand Down Expand Up @@ -114,6 +120,15 @@
<ClCompile Include="Mesh.cpp">
<Filter>12_Mesh</Filter>
</ClCompile>
<ClCompile Include="Model.cpp">
<Filter>13_Model</Filter>
</ClCompile>
<ClCompile Include="ModelPainter.cpp">
<Filter>13_Model</Filter>
</ClCompile>
<ClCompile Include="include\stb_image\stb_image.cpp">
<Filter>lib\stb_image</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="HelloShader.h">
Expand All @@ -134,9 +149,6 @@
<ClInclude Include="include\GL\glxew.h">
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="include\SOIL.h">
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="HelloTexture.h">
<Filter>3_HelloTexture</Filter>
</ClInclude>
Expand Down Expand Up @@ -429,6 +441,15 @@
<ClInclude Include="Mesh.h">
<Filter>12_Mesh</Filter>
</ClInclude>
<ClInclude Include="Model.h">
<Filter>13_Model</Filter>
</ClInclude>
<ClInclude Include="ModelPainter.h">
<Filter>13_Model</Filter>
</ClInclude>
<ClInclude Include="include\stb_image\stb_image.h">
<Filter>lib\stb_image</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="include\assimp\vector3.inl">
Expand Down
Binary file added Libs/assimp-vc142-mt.dll
Binary file not shown.
98 changes: 58 additions & 40 deletions Mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "Mesh.h"

const char* DIFFUSE_TEX_NAME = "texture_diffuse";
const char* SPECULAR_TEX_NAME = "texture_specular";
const char* NORMAL_TEX_NAME = "texture_normal";
const char* HEIGHT_TEX_NAME = "texture_height";

Mesh::Mesh(vector<Vertex> vertices, vector<GLuint> indices, vector<Texture> textures)
{
this->vertices = vertices;
Expand All @@ -11,60 +16,73 @@ Mesh::Mesh(vector<Vertex> vertices, vector<GLuint> indices, vector<Texture> text

Mesh::~Mesh()
{
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glDeleteVertexArrays(1, &VAO);
//glDeleteBuffers(1, &VBO);
//glDeleteBuffers(1, &EBO);
//glDeleteVertexArrays(1, &VAO);
}

void Mesh::Draw(Shader& shader)
{
GLuint diffuseNr = 1;
GLuint specularNr = 1;
for (size_t i = 0; i < textures.size(); ++i)
{
glActiveTexture(GL_TEXTURE0 + i);
string number;
string name = textures[i].type;
if (name == DIFFUSE_TEX_NAME)
number = std::to_string(diffuseNr++);
else if (name == SPECULAR_TEX_NAME)
number = std::to_string(specularNr++);
unsigned int diffuseNr = 1;
unsigned int specularNr = 1;
unsigned int normalNr = 1;
unsigned int heightNr = 1;
for (unsigned int i = 0; i < textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i); // active proper texture unit before binding
// retrieve texture number (the N in diffuse_textureN)
string number;
string name = textures[i].type;
if (name == DIFFUSE_TEX_NAME)
number = std::to_string(diffuseNr++);
else if (name == SPECULAR_TEX_NAME)
number = std::to_string(specularNr++); // transfer unsigned int to stream
else if (name == NORMAL_TEX_NAME)
number = std::to_string(normalNr++); // transfer unsigned int to stream
else if (name == HEIGHT_TEX_NAME)
number = std::to_string(heightNr++); // transfer unsigned int to stream

shader.setFloat("material" + name + number, i);
glBindTexture(GL_TEXTURE_2D, textures[i].id);
}
glActiveTexture(GL_TEXTURE0);
shader.setInt((name + number).c_str(), i);
glBindTexture(GL_TEXTURE_2D, textures[i].id);
}
glActiveTexture(GL_TEXTURE0);

//Draw
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}

void Mesh::SetUpMesh()
{
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);

glBindVertexArray(VAO);

glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);

glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);

//Position
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex), (void*)0);
//Normal
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, false, sizeof(Vertex), (void*)offsetof(Vertex, Normal) );
//Texture Coordinates
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, false, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
// vertex Positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
// vertex normals
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
// vertex texture coords
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
// vertex tangent
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Tangent));
// vertex bitangent
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Bitangent));

glBindVertexArray(0);
glBindVertexArray(0);

}
8 changes: 4 additions & 4 deletions Mesh.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "glheader.h"
#include <assimp/types.h>

using std::vector;
using std::string;
Expand All @@ -9,18 +10,17 @@ struct Vertex
glm::vec3 Position;
glm::vec3 Normal;
glm::vec2 TexCoords;
glm::vec3 Tangent;
glm::vec3 Bitangent;
};

struct Texture
{
GLuint id;
std::string type;
aiString Path;
};


const char* DIFFUSE_TEX_NAME = "texture_diffuse";
const char* SPECULAR_TEX_NAME = "texture_specular";

class Mesh
{
public:
Expand Down
116 changes: 116 additions & 0 deletions Model.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include "Model.h"

void Model::Draw(Shader& shader)
{
for (auto mesh : meshes)
{
mesh.Draw(shader);
}
}

void Model::LoadModel(string path)
{
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path, aiProcess_FlipUVs | aiProcess_Triangulate);
if (scene == nullptr || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
std::cout << "ERROR::ASSIMP::" << importer.GetErrorString() << std::endl;
return;
}
directory = path.substr(0, path.find_last_of('/'));
ProcessNode(scene->mRootNode, scene);
}

void Model::ProcessNode(aiNode* node, const aiScene* scene)
{
for (auto i = 0; i < node->mNumMeshes; ++i)
{
auto mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(ProcessMesh(mesh, scene));
}

for (auto i = 0; i < node->mNumChildren; ++i)
{
ProcessNode(node->mChildren[i], scene);
}
}

Mesh Model::ProcessMesh(aiMesh* mesh, const aiScene* scene)
{
vector<Vertex> vertices;
vector<GLuint> indices;
vector<Texture> textures;

//Position
for (auto i = 0; i < mesh->mNumVertices; ++i)
{
Vertex vertex;
glm::vec3 vec(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z);
vertex.Position = vec;

vec.x = mesh->mNormals[i].x;
vec.y = mesh->mNormals[i].y;
vec.z = mesh->mNormals[i].z;
vertex.Normal = vec;

if (mesh->mTextureCoords[0])
{
vertex.TexCoords = glm::vec2(mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y);
}
else
{
vertex.TexCoords = glm::vec2(0);
}

vertices.push_back(vertex);
}

//Indices
for (unsigned int i = 0; i < mesh->mNumFaces; i++)
{
aiFace face = mesh->mFaces[i];
for (unsigned int j = 0; j < face.mNumIndices; j++)
indices.push_back(face.mIndices[j]);
}

//Material
if (mesh->mMaterialIndex >= 0)
{
auto material = scene->mMaterials[mesh->mMaterialIndex];
vector<Texture> diffuseMaps = LoadMaterialTextures(material,
aiTextureType_DIFFUSE, "texture_diffuse");
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
vector<Texture> specularMaps = LoadMaterialTextures(material,
aiTextureType_SPECULAR, "texture_specular");
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
}
return Mesh(vertices, indices, textures);
}

vector<Texture> Model::LoadMaterialTextures(aiMaterial* mat, aiTextureType type, string typeName)
{
vector<Texture> textures;

for (auto i = 0; i < mat->GetTextureCount(type); ++i)
{
aiString name;
mat->GetTexture(type, i, &name);
auto toFind = textures_loaded.find(name.C_Str());
if (toFind != textures_loaded.end())
{
textures.push_back(toFind->second);
}
else
{
Texture tex;
auto filePath = StringUtil::Format("%s/%s", directory.c_str(), name.C_Str());
tex.id = Resource::LoadTexture(filePath.c_str(), GL_REPEAT, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR);
tex.type = typeName;
tex.Path = name;
textures.push_back(tex);
textures_loaded[name.C_Str()] = tex;
}
}

return textures;
}
36 changes: 36 additions & 0 deletions Model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include "glheader.h"
#include "Mesh.h"

#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include<map>

using std::vector;
using std::string;
using std::map;

class Model
{
public:
/* º¯Êý */
Model(const char* path)
{
LoadModel(path);
}
void Draw(Shader& shader);
private:
/* Ä£ÐÍÊý¾Ý */
vector<Mesh> meshes;
string directory;
map<string, Texture> textures_loaded;

void LoadModel(string path);
void ProcessNode(aiNode* node, const aiScene* scene);
Mesh ProcessMesh(aiMesh* mesh, const aiScene* scene);
vector<Texture> LoadMaterialTextures(aiMaterial* mat, aiTextureType type,
string typeName);
};

Loading

0 comments on commit c370d70

Please sign in to comment.