Assimp texture/material loading assignment

This commit is contained in:
Hugo Willaume 2019-03-26 14:04:51 +09:00
parent 1f558e3f1c
commit 47c8e8a72e
66 changed files with 484 additions and 101 deletions

5
.gitattributes vendored Normal file
View File

@ -0,0 +1,5 @@
*.obj
*.jpg
*.png
*.mtl
*.tga

View File

@ -174,7 +174,6 @@
<ClInclude Include="imgui\stb_image.h" /> <ClInclude Include="imgui\stb_image.h" />
<ClInclude Include="Light.h" /> <ClInclude Include="Light.h" />
<ClInclude Include="Loader.h" /> <ClInclude Include="Loader.h" />
<ClInclude Include="Material.h" />
<ClInclude Include="Models\Mesh.h" /> <ClInclude Include="Models\Mesh.h" />
<ClInclude Include="ModelView.h" /> <ClInclude Include="ModelView.h" />
<ClInclude Include="MyGLWindow.h" /> <ClInclude Include="MyGLWindow.h" />

View File

@ -119,9 +119,6 @@
<ClInclude Include="SceneContext.h"> <ClInclude Include="SceneContext.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Material.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="imgui\stb_image.h"> <ClInclude Include="imgui\stb_image.h">
<Filter>imgui</Filter> <Filter>imgui</Filter>
</ClInclude> </ClInclude>

View File

@ -15,6 +15,8 @@ Light::Light(glm::vec3 intensity_, glm::vec4 location_, float spot_cutoff_, floa
direction = lookAtPosition_; direction = lookAtPosition_;
} }
Light::Light() : location(glm::vec4(0)), intensity(glm::vec3(1))
{}
Light::~Light() Light::~Light()
{ {

View File

@ -11,7 +11,7 @@ public:
BASE, BASE,
SPOT SPOT
}; };
Light();
Light(glm::vec3 intensity, glm::vec4 location); Light(glm::vec3 intensity, glm::vec4 location);
Light(glm::vec3 intensity, glm::vec4 location, Light(glm::vec3 intensity, glm::vec4 location,
float spot_cutoff, float spot_inner_cutoff, float spot_cutoff, float spot_inner_cutoff,

View File

@ -1,11 +0,0 @@
#pragma once
#include <glm/vec3.hpp>
struct Material
{
glm::vec3 ka;
glm::vec3 kd;
glm::vec3 ks;
float shininess;
};

View File

@ -93,7 +93,7 @@ Mesh::MeshEntry::MeshEntry(Dataset &set, Mesh *m)
} }
Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m) Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m, std::string dirname)
{ {
renderType = OBJ; renderType = OBJ;
parent = m; parent = m;
@ -121,8 +121,8 @@ Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m)
glBindBuffer(GL_ARRAY_BUFFER, vbo[VERTEX_BUFFER]); glBindBuffer(GL_ARRAY_BUFFER, vbo[VERTEX_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, 3 * mesh->mNumVertices * sizeof(GLfloat), vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, 3 * mesh->mNumVertices * sizeof(GLfloat), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); glVertexAttribPointer(VERTEX_BUFFER, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(VERTEX_BUFFER);
delete[] vertices; delete[] vertices;
} }
@ -139,8 +139,8 @@ Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m)
glBindBuffer(GL_ARRAY_BUFFER, vbo[NORMAL_BUFFER]); glBindBuffer(GL_ARRAY_BUFFER, vbo[NORMAL_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, 3 * mesh->mNumVertices * sizeof(GLfloat), normals, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, 3 * mesh->mNumVertices * sizeof(GLfloat), normals, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); glVertexAttribPointer(NORMAL_BUFFER, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(NORMAL_BUFFER);
delete[] normals; delete[] normals;
} }
@ -156,13 +156,12 @@ Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m)
glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER]); glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, 2 * mesh->mNumVertices * sizeof(GLfloat), texCoords, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, 2 * mesh->mNumVertices * sizeof(GLfloat), texCoords, GL_STATIC_DRAW);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, NULL); glVertexAttribPointer(TEXCOORD_BUFFER, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(2); glEnableVertexAttribArray(TEXCOORD_BUFFER);
delete[] texCoords; delete[] texCoords;
} }
if (mesh->HasFaces()) { if (mesh->HasFaces()) {
unsigned int *indices = new unsigned int[mesh->mNumFaces * 3]; unsigned int *indices = new unsigned int[mesh->mNumFaces * 3];
for (unsigned int i = 0; i < mesh->mNumFaces; ++i) { for (unsigned int i = 0; i < mesh->mNumFaces; ++i) {
@ -175,8 +174,8 @@ Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[INDEX_BUFFER]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[INDEX_BUFFER]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * mesh->mNumFaces * sizeof(GLuint), indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * mesh->mNumFaces * sizeof(GLuint), indices, GL_STATIC_DRAW);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, NULL); glVertexAttribPointer(INDEX_BUFFER, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(3); glEnableVertexAttribArray(INDEX_BUFFER);
delete[] indices; delete[] indices;
} }
@ -188,8 +187,6 @@ Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m)
material->Get(AI_MATKEY_COLOR_DIFFUSE, dcolor); material->Get(AI_MATKEY_COLOR_DIFFUSE, dcolor);
material->Get(AI_MATKEY_COLOR_AMBIENT, acolor); material->Get(AI_MATKEY_COLOR_AMBIENT, acolor);
material->Get(AI_MATKEY_COLOR_SPECULAR, scolor); material->Get(AI_MATKEY_COLOR_SPECULAR, scolor);
} }
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
@ -230,14 +227,15 @@ void Mesh::MeshEntry::render(SceneContext &ctx, Shader *shd) {
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
for (GLuint i = 0; i < shd->textures.size(); i++) for (GLuint i = 0; i < parent->textures.size(); i++)
{ {
glActiveTexture(GL_TEXTURE0 + i); glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, shd->textures[i].tex_ref); glBindTexture(GL_TEXTURE_2D, parent->textures[i].tex_ref);
shd->applyTextureMaterial(parent->textures[i]);
shd->addUniform("tex[" + std::to_string(i) + "]", (int)i); shd->addUniform("tex[" + std::to_string(i) + "]", (int)i);
} }
if (shd->textures.size() > 0) if (parent->textures.size() > 0)
shd->addUniform("TexCount", (int)shd->textures.size()); shd->addUniform("TexCount", (int)parent->textures.size());
glBindVertexArray(vao); glBindVertexArray(vao);
if (renderType == NO_INDEX) if (renderType == NO_INDEX)
@ -263,6 +261,8 @@ Mesh::Mesh(const char *filename, Shader *sh)
std::string fullname; std::string fullname;
fullname = std::string("./Models/")+ std::string(filename); fullname = std::string("./Models/")+ std::string(filename);
std::string dirname = fullname;
dirname.resize(fullname.find_last_of('/'));
Assimp::Importer importer; //aiProcessPreset_TargetRealtime_Fast Assimp::Importer importer; //aiProcessPreset_TargetRealtime_Fast
const aiScene* scene = importer.ReadFile(fullname.c_str(), aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenSmoothNormals | aiProcess_OptimizeMeshes); const aiScene* scene = importer.ReadFile(fullname.c_str(), aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenSmoothNormals | aiProcess_OptimizeMeshes);
@ -274,8 +274,34 @@ Mesh::Mesh(const char *filename, Shader *sh)
return; return;
} }
for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
const aiMaterial* pMaterial = scene->mMaterials[i];
if (pMaterial->GetTextureCount(aiTextureType_DIFFUSE) > 0) { //we only care diffuse texture
aiString Path;
if (pMaterial->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL,
NULL, NULL) == AI_SUCCESS) {
std::string FullPath = dirname + "/" + Path.data; //texture file
aiColor3D diffuse;
aiColor3D specular;
aiColor3D ambient;
pMaterial->Get(AI_MATKEY_COLOR_DIFFUSE, diffuse);
pMaterial->Get(AI_MATKEY_COLOR_SPECULAR, specular);
pMaterial->Get(AI_MATKEY_COLOR_AMBIENT, ambient);
textures.push_back(Texture(FullPath));
textures.back().mat.enabled = true;
textures.back().mat.ka = glm::vec3(ambient.r, ambient.g, ambient.b);
textures.back().mat.kd = glm::vec3(diffuse.r, diffuse.g, diffuse.b);
textures.back().mat.ks = glm::vec3(specular.r, specular.g, specular.b);
}
}
}
for (unsigned int i = 0; i < scene->mNumMeshes; ++i) { for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
meshEntries.push_back(new Mesh::MeshEntry(scene->mMeshes[i], scene, this)); meshEntries.push_back(new Mesh::MeshEntry(scene->mMeshes[i], scene, this, dirname));
} }
} }
@ -370,6 +396,11 @@ void Mesh::draw(SceneContext ctx) {
shader->disable(); shader->disable();
} }
void Mesh::assignTexture(Texture &texture)
{
textures.emplace_back(texture);
}
void Mesh::effectTransformations() void Mesh::effectTransformations()
{ {
for (auto pair : _transformations) for (auto pair : _transformations)

View File

@ -52,9 +52,10 @@ public:
aiColor3D dcolor; aiColor3D dcolor;
aiColor3D acolor; aiColor3D acolor;
aiColor3D scolor; aiColor3D scolor;
std::vector<Texture> textures;
float shininessStrength; float shininessStrength;
MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m); MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m, std::string dirname);
MeshEntry(Dataset &set, Mesh *m); MeshEntry(Dataset &set, Mesh *m);
~MeshEntry(); ~MeshEntry();
Mesh * parent; Mesh * parent;
@ -66,6 +67,7 @@ public:
Mesh(Dataset &dataset, Shader *sh); Mesh(Dataset &dataset, Shader *sh);
~Mesh(void); ~Mesh(void);
std::vector<MeshEntry*> meshEntries; std::vector<MeshEntry*> meshEntries;
std::vector<Texture> textures;
private: private:
std::vector<std::pair<glm::vec4, Transformation>> _transformations; std::vector<std::pair<glm::vec4, Transformation>> _transformations;
@ -86,4 +88,5 @@ public:
Shader *shader; Shader *shader;
void draw(SceneContext ctx); void draw(SceneContext ctx);
void assignTexture(Texture &texture);
}; };

Binary file not shown.

After

Width:  |  Height:  |  Size: 628 KiB

View File

@ -0,0 +1,21 @@
# Blender MTL File: 'mount.blend1.blend'
# Material Count: 2
newmtl Material
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.050977 0.050977 0.050977
Ks 0.001641 0.001641 0.001641
Ni 1.000000
d 1.000000
illum 2
newmtl Material.001
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 1
map_Kd ground_grass_3264_4062_Small.jpg

View File

@ -0,0 +1,306 @@
# Blender MTL File: 'None'
# Material Count: 25
newmtl Material__25
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/lion.tga
map_Disp textures/lion_ddn.tga
map_Ka textures/lion.tga
newmtl Material__298
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/background.tga
map_Disp textures/background_ddn.tga
map_Ka textures/background.tga
newmtl Material__47
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
newmtl Material__57
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd textures/vase_plant.tga
map_d textures/vase_plant_mask.tga
map_Ka textures/vase_plant.tga
newmtl arch
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/sponza_arch_diff.tga
map_Ka textures/sponza_arch_diff.tga
map_Disp textures/sponza_arch_ddn.tga
newmtl bricks
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/spnza_bricks_a_diff.tga
map_Disp textures/spnza_bricks_a_ddn.tga
map_Ka textures/spnza_bricks_a_diff.tga
newmtl ceiling
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/sponza_ceiling_a_diff.tga
map_Ka textures/sponza_ceiling_a_diff.tga
map_Disp textures/sponza_ceiling_a_ddn.tga
newmtl chain
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd textures/chain_texture.tga
map_d textures/chain_texture_mask.tga
map_Disp textures/chain_texture_ddn.tga
map_Ka textures/chain_texture.tga
newmtl column_a
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/sponza_column_a_diff.tga
map_Disp textures/sponza_column_a_ddn.tga
map_Ka textures/sponza_column_a_diff.tga
newmtl column_b
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/sponza_column_b_diff.tga
map_Disp textures/sponza_column_b_ddn.tga
map_Ka textures/sponza_column_b_diff.tga
newmtl column_c
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/sponza_column_c_diff.tga
map_Disp textures/sponza_column_c_ddn.tga
map_Ka textures/sponza_column_c_diff.tga
newmtl details
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/sponza_details_diff.tga
map_Ka textures/sponza_details_diff.tga
map_Disp textures/sponza_details_ddn.tga
newmtl fabric_a
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/sponza_fabric_diff.tga
map_Ka textures/sponza_fabric_diff.tga
map_Disp textures/sponza_fabric_ddn.tga
newmtl fabric_c
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/sponza_curtain_diff.tga
map_Ka textures/sponza_curtain_diff.tga
map_Disp textures/sponza_curtain_ddn.tga
newmtl fabric_d
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/sponza_fabric_blue_diff.tga
map_Ka textures/sponza_fabric_blue_diff.tga
map_Disp textures/sponza_fabric_ddn.tga
newmtl fabric_e
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/sponza_fabric_green_diff.tga
map_Ka textures/sponza_fabric_green_diff.tga
map_Disp textures/sponza_fabric_ddn.tga
newmtl fabric_f
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/sponza_curtain_green_diff.tga
map_Ka textures/sponza_curtain_green_diff.tga
map_Disp textures/sponza_curtain_ddn.tga
newmtl fabric_g
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/sponza_curtain_blue_diff.tga
map_Ka textures/sponza_curtain_blue_diff.tga
map_Disp textures/sponza_curtain_ddn.tga
newmtl flagpole
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/sponza_flagpole_diff.tga
map_Ka textures/sponza_flagpole_diff.tga
map_Disp textures/sponza_flagpole_ddn.tga
newmtl floor
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/sponza_floor_a_diff.tga
map_Ka textures/sponza_floor_a_diff.tga
map_Disp textures/sponza_floor_a_ddn.tga
newmtl leaf
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd textures/sponza_thorn_diff.tga
map_d textures/sponza_thorn_mask.tga
map_Disp textures/sponza_thorn_ddn.tga
map_Ka textures/sponza_thorn_diff.tga
newmtl roof
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/sponza_roof_diff.tga
map_Ka textures/sponza_roof_diff.tga
map_Ka textures/sponza_roof_ddn.tga
newmtl vase
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/vase_dif.tga
map_Ka textures/vase_dif.tga
map_Disp textures/vase_ddn.tga
newmtl vase_hanging
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/vase_hanging.tga
map_Ka textures/vase_hanging.tga
map_Disp textures/vase_hanging_ddn.tga
newmtl vase_round
Ns 7.843137
Ka 0.000000 0.000000 0.000000
Kd 0.470400 0.470400 0.470400
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 0.000000
illum 2
map_Kd textures/vase_round.tga
map_Disp textures/vase_round_ddn.tga
map_Ka textures/vase_round.tga

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 768 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

View File

@ -62,9 +62,9 @@ void MyGlWindow::textureSetup()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
_scnctx.textures.emplace("BrickTex", Texture("brick1.jpg", GL_RGB)); _scnctx.textures.emplace("BrickTex", Texture("brick1.jpg"));
_scnctx.textures.emplace("MossTex", Texture(std::string("moss.png"), GL_RGBA)); _scnctx.textures.emplace("MossTex", Texture("moss.png"));
_scnctx.textures.emplace("EarthTex", Texture("earth.jpg", GL_RGB)); _scnctx.textures.emplace("EarthTex", Texture("earth.jpg"));
} }
void MyGlWindow::shaderSetup() void MyGlWindow::shaderSetup()
@ -75,19 +75,13 @@ void MyGlWindow::shaderSetup()
shaders["BaseLight"] = new Shader("base_light.vert", "base_light.frag"); shaders["BaseLight"] = new Shader("base_light.vert", "base_light.frag");
shaders["Fog"] = new Shader("fog.vert", "fog.frag"); shaders["Fog"] = new Shader("fog.vert", "fog.frag");
shaders["BrickBaseLight"] = new Shader("tex_base_light.vert", "tex_base_light.frag"); shaders["TexBaseLight"] = new Shader("tex_base_light.vert", "tex_base_light.frag");
shaders["BrickBaseLight"]->assignTexture(_scnctx.textures["BrickTex"]);
shaders["BrickBaseLight"]->assignTexture(_scnctx.textures["MossTex"]);
shaders["EarthBaseLight"] = new Shader("tex_base_light.vert", "tex_base_light.frag");
shaders["EarthBaseLight"]->assignTexture(_scnctx.textures["EarthTex"]);
shaders["SpotLight"] = new Shader("spotlight.vert", "spotlight.frag"); shaders["SpotLight"] = new Shader("spotlight.vert", "spotlight.frag");
shaders["SpotLight"]->light_type = Light::LightType::SPOT; shaders["SpotLight"]->light_type = Light::LightType::SPOT;
shaders["TexSpotLight"] = new Shader("tex_spotlight.vert", "tex_spotlight.frag"); shaders["TexSpotLight"] = new Shader("tex_spotlight.vert", "tex_spotlight.frag");
shaders["TexSpotLight"]->light_type = Light::LightType::SPOT; shaders["TexSpotLight"]->light_type = Light::LightType::SPOT;
shaders["Silhouette"] = new Shader("silhouette.vert", "silhouette.frag"); shaders["Silhouette"] = new Shader("silhouette.vert", "silhouette.frag");
shaders["Silhouette"]->uniformFlags = ShaderFlags::MVP_FLAG; shaders["Silhouette"]->uniformFlags = ShaderFlags::MVP_FLAG;
shaders["Silhouette"]->addUniform("fColor", glm::vec3(237 / 255, 229 / 255, 194 / 255)); shaders["Silhouette"]->addUniform("fColor", glm::vec3(237 / 255, 229 / 255, 194 / 255));
@ -102,15 +96,15 @@ void MyGlWindow::shaderSetup()
void MyGlWindow::lightSetup() void MyGlWindow::lightSetup()
{ {
//Showcase lights //Showcase lights
_scnctx.lights.emplace("Spotlight1", Light(glm::vec3(0.8f), glm::vec4(3, 10, 5, 1), _scnctx.lights.emplace("Spotlight1", Light(glm::vec3(0.8f), glm::vec4(0, 11, 4.3, 1)));
24, 12, 2, glm::vec4(0, 1, 0, 1))); // 24, 12, 2, glm::vec4(0, 1, 0, 1)));
//Party lights //Party lights
/*_scnctx.lights.emplace("Light1", Light(glm::vec3(0.0f, 0.5f, 0.5f), glm::vec4(10, 10, 0, 1))); //_scnctx.lights.emplace("Light1", Light(glm::vec3(0.0f, 0.5f, 0.5f), glm::vec4(10, 10, 0, 1)));
_scnctx.lights.emplace("Light2", Light(glm::vec3(0.0f, 0.0f, 0.5f), glm::vec4(3.09, 10, 9.51, 1))); //_scnctx.lights.emplace("Light2", Light(glm::vec3(0.0f, 0.0f, 0.5f), glm::vec4(3.09, 10, 9.51, 1)));
_scnctx.lights.emplace("Light3", Light(glm::vec3(0.5f, 0.0f, 0.0f), glm::vec4(-8.09, 10, 5.87, 1))); //_scnctx.lights.emplace("Light3", Light(glm::vec3(0.5f, 0.0f, 0.0f), glm::vec4(-8.09, 10, 5.87, 1)));
_scnctx.lights.emplace("Light4", Light(glm::vec3(0.0f, 0.5f, 0.0f), glm::vec4(-8.09, 10, -5.87, 1))); //_scnctx.lights.emplace("Light4", Light(glm::vec3(0.0f, 0.5f, 0.0f), glm::vec4(-8.09, 10, -5.87, 1)));
_scnctx.lights.emplace("Light5", Light(glm::vec3(0.5f, 0.5f, 0.5f), glm::vec4(3.09, 10, -9.51, 1)));*/ //_scnctx.lights.emplace("Light5", Light(glm::vec3(0.5f, 0.5f, 0.5f), glm::vec4(3.09, 10, -9.51, 1)));
} }
void MyGlWindow::setup() void MyGlWindow::setup()
@ -119,26 +113,46 @@ void MyGlWindow::setup()
shaderSetup(); shaderSetup();
lightSetup(); lightSetup();
/*
meshes.emplace("Buddha", new Mesh("buddha.obj", shaders["BaseLight"]));
meshes["Buddha"]->addTranslation(glm::vec4(-4, 3, -4, 0));
meshes["Buddha"]->addScaling(glm::vec4(4, 4, 4, 0));
meshes["Buddha"]->addRotation(glm::vec4(0, 1, 0, 180));
Dataset moddata; Dataset moddata;
moddata.checkeredFloor(20, 20, glm::vec3(0.1, 0.1, 0.1), glm::vec3(0.7, 0.7, 0.7)); moddata.simpleCube();
meshes.emplace("Floor", new Mesh(moddata, shaders["SpotLight"])); meshes.emplace("Cube", new Mesh(moddata, shaders["TexBaseLight"]));
meshes["Floor"]->addTranslation(glm::vec4(0, -0.05, 0, 1)); meshes["Cube"]->assignTexture(_scnctx.textures["BrickTex"]);
meshes["Floor"]->cullMode = BACK; meshes["Cube"]->assignTexture(_scnctx.textures["MossTex"]);
meshes["Cube"]->addTranslation(glm::vec4(4, 3, -4, 0));
/*moddata.simpleCube(); meshes.emplace("Mountain", new Mesh("mountain/mount.blend1.obj", shaders["TexBaseLight"])); */
meshes.emplace("Cube", new Mesh(moddata, shaders["BrickBaseLight"])); meshes.emplace("Sponza", new Mesh("sponza/sponza.obj", shaders["TexBaseLight"]));
meshes["Cube"]->addTranslation(glm::vec4(0, 1, 0, 1)); meshes["Sponza"]->addScaling(glm::vec4(0.2, 0.2, 0.2, 1));
meshes["Sponza"]->cullMode = CullFace::BACK;
moddata.sphere(1, 100, 100);
meshes.emplace("Sphere", new Mesh(moddata, shaders["EarthBaseLight"]));
meshes["Sphere"]->addTranslation(glm::vec4(-5, 1, -3, 1));
meshes.emplace("TeapotSilhouette", new Mesh("teapot.obj", shaders["Silhouette"]));
meshes["TeapotSilhouette"]->addTranslation(glm::vec4(5, 0 ,3, 1)); //moddata.checkeredFloor(20, 20, glm::vec3(0.1, 0.1, 0.1), glm::vec3(0.7, 0.7, 0.7));
meshes["TeapotSilhouette"]->cullMode = FRONT; //meshes.emplace("Floor", new Mesh(moddata, shaders["BaseLight"]));
meshes.emplace("Teapot", new Mesh("teapot.obj", shaders["Toon"])); //meshes["Floor"]->addTranslation(glm::vec4(0, -0.05, 0, 1));
meshes["Teapot"]->addTranslation(glm::vec4(5, 0, 3, 1)); //meshes["Floor"]->cullMode = BACK;
meshes["Teapot"]->cullMode = BACK;*/
//moddata.simpleCube();
//meshes.emplace("Cube", new Mesh(moddata, shaders["BrickBaseLight"]));
//meshes["Cube"]->addTranslation(glm::vec4(0, 1, 0, 1));
//moddata.sphere(1, 100, 100);
//meshes.emplace("Sphere", new Mesh(moddata, shaders["TexBaseLight"]));
//meshes["Sphere"]->assignTexture(_scnctx.textures["EarthTex"]);
//meshes["Sphere"]->addTranslation(glm::vec4(-5, 1, -3, 1));
//meshes.emplace("TeapotSilhouette", new Mesh("teapot.obj", shaders["Silhouette"]));
//meshes["TeapotSilhouette"]->addTranslation(glm::vec4(5, 0 ,3, 1));
//meshes["TeapotSilhouette"]->cullMode = FRONT;
//meshes.emplace("Teapot", new Mesh("teapot.obj", shaders["Toon"]));
//meshes["Teapot"]->addTranslation(glm::vec4(5, 0, 3, 1));
//meshes["Teapot"]->cullMode = BACK;
} }
@ -159,7 +173,7 @@ void MyGlWindow::draw()
_scnctx.viewMatrix = view; _scnctx.viewMatrix = view;
_scnctx.projectionMatrix = projection; _scnctx.projectionMatrix = projection;
_scnctx.adjustSpots(); //_scnctx.adjustSpots();
for (auto it = meshes.begin(); it != meshes.end(); it++) for (auto it = meshes.begin(); it != meshes.end(); it++)
(*it).second->draw(_scnctx); (*it).second->draw(_scnctx);
} }

View File

@ -31,8 +31,8 @@ public:
void resize(int w, int h); void resize(int w, int h);
Viewer viewer; Viewer viewer;
private:
SceneContext _scnctx; SceneContext _scnctx;
private:
int m_width; int m_width;
int m_height; int m_height;

View File

@ -2,6 +2,7 @@
#include <map> #include <map>
#include <glm/mat3x3.hpp> #include <glm/mat3x3.hpp>
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include <glm/vec3.hpp>
#include "Texture.h" #include "Texture.h"

View File

@ -64,8 +64,7 @@ void Shader::setLights(SceneContext ctx)
i++; i++;
} }
} }
_program.addUniform("LightCount"); glUniform1i(_program.addUniform("LightCount"), i);
glUniform1i(_program.uniform("LightCount"), i);
} }
void Shader::setUniforms(SceneContext ctx) void Shader::setUniforms(SceneContext ctx)

View File

@ -6,11 +6,10 @@
#include <functional> #include <functional>
#include "glm/gtc/type_ptr.hpp" #include "glm/gtc/type_ptr.hpp"
#include "Texture.h"
#include "Material.h"
#include "Light.h" #include "Light.h"
#include "SceneContext.h" #include "SceneContext.h"
#include "Loader.h" #include "Loader.h"
#include "Texture.h"
enum ShaderFlags enum ShaderFlags
{ {
@ -32,10 +31,9 @@ public:
Shader(const std::string vtx_name, const std::string frag_name); Shader(const std::string vtx_name, const std::string frag_name);
~Shader(); ~Shader();
std::vector<Texture> textures;
int uniformFlags = KD_FLAG | KA_FLAG | KS_FLAG | SHINE_FLAG | MVP_FLAG | NORMAL_MAT_FLAG | MODELVIEW_FLAG | LIGHTS_FLAG; int uniformFlags = KD_FLAG | KA_FLAG | KS_FLAG | SHINE_FLAG | MVP_FLAG | NORMAL_MAT_FLAG | MODELVIEW_FLAG | LIGHTS_FLAG;
Material mat;
Light::LightType light_type = Light::LightType::BASE; Light::LightType light_type = Light::LightType::BASE;
Material mat;
void enable(); void enable();
void disable(); void disable();
@ -56,9 +54,14 @@ public:
_program.use(); glUniform1fv(_program.addUniform(name), 1, &data); _program.use(); glUniform1fv(_program.addUniform(name), 1, &data);
} }
void assignTexture(Texture &texture) void applyTextureMaterial(Texture &tex)
{ {
textures.emplace_back(texture); if (mat.enabled)
{
addUniform("Ka", tex.mat.ka);
addUniform("Kd", tex.mat.kd);
addUniform("Ks", tex.mat.ks);
}
} }
private: private:
@ -69,4 +72,3 @@ private:
private: private:
ShaderProgram _program; ShaderProgram _program;
}; };

View File

@ -175,10 +175,14 @@ int loop(GLFWwindow *window)
{ {
ImGui::SetWindowPos(ImVec2(20, 20)); ImGui::SetWindowPos(ImVec2(20, 20));
static float bgColor[3] = { .0f, .0f, .1f }; static float bgColor[3] = { 0.6f, 0.6f, 0.9f };
ImGui::ColorEdit3("Background", bgColor, 0); ImGui::ColorEdit3("Background", bgColor, 0);
glWin.setBgColor(bgColor); glWin.setBgColor(bgColor);
ImGui::SliderFloat("Light X", &(glWin._scnctx.lights["Spotlight1"].location.x), -20, 20);
ImGui::SliderFloat("Light Y", &(glWin._scnctx.lights["Spotlight1"].location.y), -20, 20);
ImGui::SliderFloat("Light Z", &(glWin._scnctx.lights["Spotlight1"].location.z), -20, 20);
ImGui::End(); ImGui::End();
} }

View File

@ -1,6 +1,7 @@
#include "Texture.h" #include "Shader.h"
#include "imgui/stb_image.h"
Texture::Texture(std::string file_name, GLuint enum_rgb) Texture::Texture(std::string file_name)
{ {
int width, height, channel; int width, height, channel;
unsigned char * image; unsigned char * image;
@ -10,6 +11,8 @@ Texture::Texture(std::string file_name, GLuint enum_rgb)
image = stbi_load(file_name.c_str(), &width, &height, &channel, 0); image = stbi_load(file_name.c_str(), &width, &height, &channel, 0);
GLuint enum_rgb = GL_RGB + (channel == 4);
glTexImage2D(GL_TEXTURE_2D, 0, enum_rgb, width, height, 0, enum_rgb, glTexImage2D(GL_TEXTURE_2D, 0, enum_rgb, width, height, 0, enum_rgb,
GL_UNSIGNED_BYTE, image); GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
@ -23,6 +26,7 @@ Texture::Texture(const Texture &other)
tex_ref = other.tex_ref; tex_ref = other.tex_ref;
} }
Texture::~Texture() Texture::~Texture()
{ {
} }

View File

@ -1,19 +1,25 @@
#pragma once #pragma once
#include <GL/glew.h>
#include <string> #include <string>
#include <map> #include <glm/vec3.hpp>
#include "imgui/stb_image.h"
struct Material
{
glm::vec3 ka;
glm::vec3 kd;
glm::vec3 ks;
float shininess;
bool enabled = false;
};
class Texture class Texture
{ {
public: public:
Material mat;
GLuint tex_ref; GLuint tex_ref;
Texture(std::string file_name, GLuint enum_rgb); Texture(std::string file_name);
Texture(const Texture &other); Texture(const Texture &other);
Texture() {}; Texture() {};
~Texture(); ~Texture();
void sendUniform(GLuint uni);
}; };

BIN
BaseGLProject/default.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 266 B

View File

@ -45,6 +45,6 @@ Collapsed=0
[Window][First Window] [Window][First Window]
Pos=20,20 Pos=20,20
Size=322,75 Size=321,139
Collapsed=0 Collapsed=0