Import materials from files

This commit is contained in:
Dane Johnson 2021-01-22 18:44:43 -06:00
parent 1ee5ed3e88
commit 1d97f6d855
22 changed files with 4427 additions and 44 deletions

View File

@ -39,8 +39,10 @@ Texture Texture::FromFile(const char *filename) {
}
Material::Material() {
usesColor = false;
usesTex = false;
shininess = 8;
alphaScissor = 0.0f;
unshaded = false;
cullBack = true;

View File

@ -22,10 +22,15 @@ public:
};
struct Material {
Color color;
bool usesColor;
Texture tex;
bool usesTex;
Color ambient;
Color diffuse;
Color specular;
int shininess;
cfloat alphaScissor;
bool unshaded;
bool cullBack;

View File

@ -81,10 +81,13 @@ Mesh* Mesh::FromFile(const char *filename) {
}
aiNode *root = scene->mRootNode;
if (root->mNumChildren == 1) {
root = root->mChildren[0];
}
Mesh *my_mesh = new Mesh();
for (int i = 0; i < root->mNumMeshes; i++) {
aiMesh *mesh_to_import = scene->mMeshes[root->mMeshes[i]];
my_mesh->submeshes.push_back(aiMesh2SubMesh(mesh_to_import));
my_mesh->submeshes.push_back(aiMesh2SubMesh(mesh_to_import, scene->mMaterials[mesh_to_import->mMaterialIndex]));
}
my_mesh->SetupMesh();
@ -92,7 +95,7 @@ Mesh* Mesh::FromFile(const char *filename) {
return my_mesh;
}
SubMesh *Mesh::aiMesh2SubMesh(aiMesh *aimesh){
SubMesh *Mesh::aiMesh2SubMesh(aiMesh *aimesh, aiMaterial* material){
SubMesh *sub = new SubMesh();
for (int i = 0; i < aimesh->mNumVertices; i++) {
aiVector3D aiPosition = aimesh->mVertices[i];
@ -109,9 +112,35 @@ SubMesh *Mesh::aiMesh2SubMesh(aiMesh *aimesh){
Index index(face[0], face[1], face[2]);
sub->indices.push_back(index);
}
// Get material properties
aiColor3D ambient;
if (AI_SUCCESS == material->Get(AI_MATKEY_COLOR_AMBIENT, ambient))
sub->material.ambient = aiColor3D2Color(ambient);
aiColor3D diffuse;
if (AI_SUCCESS == material->Get(AI_MATKEY_COLOR_DIFFUSE, diffuse))
sub->material.diffuse = aiColor3D2Color(diffuse);
aiColor3D specular;
if (AI_SUCCESS == material->Get(AI_MATKEY_COLOR_SPECULAR, specular))
sub->material.specular = aiColor3D2Color(specular);
float shininess;
if(AI_SUCCESS == material->Get(AI_MATKEY_SHININESS, shininess))
sub->material.shininess = (int) shininess;
return sub;
}
Color Mesh::aiColor3D2Color(aiColor3D aicolor) {
Color color;
color.r = aicolor.r;
color.g = aicolor.g;
color.b = aicolor.b;
return color;
}
void Mesh::Draw(Shader *shader) {
for (SubMesh *sub : submeshes) {
sub->Draw(shader);

View File

@ -48,7 +48,8 @@ protected:
SubMeshList submeshes;
virtual void SetupMesh();
private:
static SubMesh *aiMesh2SubMesh(aiMesh *mesh);
static SubMesh *aiMesh2SubMesh(aiMesh *mesh, aiMaterial *material);
static Color aiColor3D2Color(aiColor3D aicolor);
};
typedef std::list<Mesh*> MeshList;

View File

@ -54,15 +54,16 @@ void Shader::UpdateProjection(Matrix projection) {
}
void Shader::UpdateMaterial(Material material) {
glUniform1i(glGetUniformLocation(id, "material.usesColor"), (int) material.usesColor);
glUniform3f(glGetUniformLocation(id, "material.color"),
material.color.r,
material.color.g,
material.color.b);
glUniform1i(glGetUniformLocation(id, "material.usesTex"), (int) material.usesTex);
if (material.usesTex) {
glBindTexture(GL_TEXTURE_2D, material.tex.id);
}
glUniform3fv(glGetUniformLocation(id, "material.ambient"), 1, (cfloat*) &material.ambient);
glUniform3fv(glGetUniformLocation(id, "material.diffuse"), 1, (cfloat*) &material.diffuse);
glUniform3fv(glGetUniformLocation(id, "material.specular"), 1, (cfloat*) &material.specular);
glUniform1i(glGetUniformLocation(id, "material.shininess"), material.shininess);
glUniform1f(glGetUniformLocation(id, "material.alphaScissor"), material.alphaScissor);
glUniform1i(glGetUniformLocation(id, "material.unshaded"), (int) material.unshaded);
glUniform1i(glGetUniformLocation(id, "material.cullBack"), (int) material.cullBack);

Binary file not shown.

12
demo/ball.mtl Normal file
View File

@ -0,0 +1,12 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl Material.001
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2

4105
demo/ball.obj Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

10
demo/cube.mtl Normal file
View File

@ -0,0 +1,10 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl None
Ns 500
Ka 0.8 0.8 0.8
Kd 0.8 0.8 0.8
Ks 0.8 0.8 0.8
d 1
illum 2

40
demo/cube.obj Normal file
View File

@ -0,0 +1,40 @@
# Blender v2.91.0 OBJ File: ''
# www.blender.org
mtllib cube.mtl
o Cube_Cube.001
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
vt 0.375000 0.000000
vt 0.625000 0.000000
vt 0.625000 0.250000
vt 0.375000 0.250000
vt 0.625000 0.500000
vt 0.375000 0.500000
vt 0.625000 0.750000
vt 0.375000 0.750000
vt 0.625000 1.000000
vt 0.375000 1.000000
vt 0.125000 0.500000
vt 0.125000 0.750000
vt 0.875000 0.500000
vt 0.875000 0.750000
vn -1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 0.0000
usemtl None
s off
f 1/1/1 2/2/1 4/3/1 3/4/1
f 3/4/2 4/3/2 8/5/2 7/6/2
f 7/6/3 8/5/3 6/7/3 5/8/3
f 5/8/4 6/7/4 2/9/4 1/10/4
f 3/11/5 7/6/5 5/8/5 1/12/5
f 8/5/6 4/13/6 2/14/6 6/7/6

View File

@ -14,6 +14,7 @@ local cam_rot_y = 0.0
local SPEED = 30
local WHITE = couch.Color(1.0, 1.0, 1.0)
local RED = couch.Color(1.0, 0.0, 0.0)
local BLUE = couch.Color(0.0, 0.0, 1.0)
@ -34,15 +35,15 @@ function init()
light.specular = 0.01
couch.Node.GetRoot().children:Append(light)
ball = couch.Mesh.FromFile("cube.glb")
material = couch.Material()
material.color = RED
material.usesColor = true
ball = couch.Mesh.FromFile("cube.obj")
material = ball:GetMaterial(0)
material.ambient = WHITE
ball:SetMaterial(0, material)
couch.Node.GetRoot().children:Append(ball)
ball1 = couch.Mesh.FromFile("ball.glb")
material = couch.Material()
ball1 = couch.Mesh.FromFile("ball.obj")
print(material.diffuse.b)
material = ball1:GetMaterial(0)
material.tex = couch.Texture.FromFile("container.png")
material.usesTex = true
ball1:SetMaterial(0, material)
@ -50,12 +51,12 @@ function init()
ball1.transform:Translate(0.0, 3.0, 0.0)
trough = couch.TexturedMesh("trough.glb", "wood_lowres.png")
trough = couch.TexturedMesh("trough.obj", "wood_lowres.png")
couch.Node.GetRoot().children:Append(trough)
trough.transform:Translate(10.0, 0.0, 0.0)
trough.transform.scale = trough.transform.scale * 3.0
scaffold = couch.TexturedMesh("scaffold.glb", "grate_floor_lowres.png", "railing.png")
scaffold = couch.TexturedMesh("scaffold.obj", "grate_floor_lowres.png", "railing.png")
material = scaffold:GetMaterial(0)
material.alphaScissor = 0.9

Binary file not shown.

22
demo/scaffold.mtl Normal file
View File

@ -0,0 +1,22 @@
# Blender MTL File: 'scaffold.blend'
# Material Count: 2
newmtl grate
Ns 323.999994
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
newmtl railing
Ns 323.999994
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2

52
demo/scaffold.obj Normal file
View File

@ -0,0 +1,52 @@
# Blender v2.91.0 OBJ File: 'scaffold.blend'
# www.blender.org
mtllib scaffold.mtl
o Cube
v 1.934654 0.106465 -3.000000
v 1.934654 0.010658 -3.000000
v 1.934654 0.106465 3.000000
v 1.934654 0.010658 3.000000
v -1.956436 0.106465 -3.000000
v -1.956436 0.010658 -3.000000
v -1.956436 0.106465 3.000000
v -1.956436 0.010658 3.000000
v 1.934654 2.092319 -3.000000
v 1.934654 2.092319 3.000000
vt 0.003780 1.006212
vt 0.003780 0.005796
vt 0.994847 0.005796
vt 0.994847 1.006212
vt 0.426341 0.196745
vt 0.393727 0.196745
vt 0.393727 0.000079
vt 0.426341 0.000079
vt 0.426341 0.999921
vt 0.393727 0.999921
vt 0.393727 0.696666
vt 0.426341 0.696666
vt 1.986373 0.005796
vt 1.986373 1.006211
vt 0.995306 1.006212
vt 0.995306 0.005796
vt 0.426341 0.500001
vt 0.393727 0.500001
vt 0.000072 0.000073
vt 0.999928 0.000072
vt 0.999928 0.998269
vt 0.000072 0.998269
vn 0.0000 1.0000 0.0000
vn 0.0000 0.0000 1.0000
vn -1.0000 0.0000 0.0000
vn 0.0000 -1.0000 0.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
usemtl grate
s off
f 1/1/1 5/2/1 7/3/1 3/4/1
f 4/5/2 3/6/2 7/7/2 8/8/2
f 8/9/3 7/10/3 5/11/3 6/12/3
f 6/13/4 2/14/4 4/15/4 8/16/4
f 2/17/5 1/18/5 3/6/5 4/5/5
f 6/12/6 5/11/6 1/18/6 2/17/6
usemtl railing
f 3/19/5 1/20/5 9/21/5 10/22/5

Binary file not shown.

12
demo/trough.mtl Normal file
View File

@ -0,0 +1,12 @@
# Blender MTL File: 'trough.blend'
# Material Count: 1
newmtl Material
Ns 323.999994
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2

80
demo/trough.obj Normal file
View File

@ -0,0 +1,80 @@
# Blender v2.91.0 OBJ File: 'trough.blend'
# www.blender.org
mtllib trough.mtl
o Cube
v 0.559649 0.176655 1.457162
v 0.559649 0.176655 -1.457162
v -0.559649 0.176655 1.457162
v -0.559649 0.176655 -1.457162
v 0.768938 1.259425 2.002092
v 0.615018 0.029390 1.601327
v 0.768938 1.259425 -2.002092
v 0.615018 0.029390 -1.601327
v -0.768938 1.259425 2.002092
v -0.615018 0.029390 1.601327
v -0.768938 1.259425 -2.002092
v -0.615018 0.029390 -1.601327
v 0.695141 1.259425 -1.809946
v 0.695141 1.259425 1.809946
v -0.695141 1.259425 -1.809946
v -0.695141 1.259425 1.809946
vt 0.686031 0.303372
vt 0.701491 0.673315
vt 0.962419 0.711595
vt 0.971650 0.340317
vt -0.300445 0.304089
vt -0.298284 0.709022
vt 0.099821 0.696156
vt 0.134610 0.334567
vt 0.599830 0.530280
vt 0.599830 0.643491
vt 0.894598 0.643491
vt 0.894598 0.530280
vt 0.272975 0.302381
vt 0.272886 0.674376
vt 0.599830 0.000171
vt 0.607295 0.019606
vt 0.747924 0.019606
vt 0.747925 0.385756
vt 0.755388 0.405188
vt 0.755388 0.000170
vt -0.066641 0.235731
vt -0.225881 0.232306
vt -0.204799 0.717963
vt -0.061225 0.757944
vt 1.229174 0.237805
vt 0.663827 0.254265
vt 0.728179 0.721933
vt 1.245199 0.723908
vt 0.923761 0.405529
vt 0.599830 0.405529
vt 0.599830 0.529940
vt 0.923761 0.529940
vt 0.471728 0.284766
vt 0.491305 0.770590
vt 0.599832 0.405188
vt 0.607295 0.385755
vn 0.0000 0.3098 0.9508
vn 0.9923 0.1242 0.0000
vn 0.0000 1.0000 0.0000
vn -0.9923 0.1242 0.0000
vn 0.0000 0.3098 -0.9508
vn 0.0000 -0.3098 -0.9508
vn -0.9923 -0.1242 0.0000
vn 0.0000 -1.0000 0.0000
vn 0.9923 -0.1242 0.0000
vn 0.0000 -0.3098 0.9508
usemtl Material
s off
f 2/1/1 13/2/1 15/3/1 4/4/1
f 4/5/2 15/6/2 16/7/2 3/8/2
f 3/9/3 1/10/3 2/11/3 4/12/3
f 1/13/4 14/14/4 13/2/4 2/1/4
f 3/8/5 16/7/5 14/14/5 1/13/5
f 5/15/3 14/16/3 16/17/3 15/18/3 11/19/3 9/20/3
f 8/21/6 12/22/6 11/23/6 7/24/6
f 12/25/7 10/26/7 9/27/7 11/28/7
f 10/29/8 12/30/8 8/31/8 6/32/8
f 6/33/9 8/21/9 7/24/9 5/34/9
f 10/26/10 6/33/10 5/34/10 9/27/10
f 14/16/3 5/15/3 7/35/3 11/19/3 15/18/3 13/36/3

View File

@ -4,7 +4,7 @@
function couch.TexturedMesh(meshfile, ...)
local mesh = couch.Mesh.FromFile(meshfile)
for i, texturefile in ipairs({...}) do
local material = couch.Material()
local material = mesh:GetMaterial(i - 1)
material.usesTex = true
material.tex = couch.Texture.FromFile(texturefile)
mesh:SetMaterial(i - 1, material)

View File

@ -3,17 +3,21 @@
noperspective in vec2 UV;
in vec3 NORMAL;
flat in vec3 AMBIENT;
flat in vec3 DIFFUSE;
flat in vec3 SPECULAR;
in vec3 AMBIENT;
in vec3 DIFFUSE;
in vec3 SPECULAR;
out vec4 FragColor;
struct Material {
vec3 color;
bool usesColor;
sampler2D tex;
bool usesTex;
vec3 ambient;
vec3 diffuse;
vec3 specular;
int shininess;
float alphaScissor;
bool unshaded;
bool cullBack;
@ -22,14 +26,10 @@ struct Material {
uniform Material material;
void main() {
FragColor = vec4(0.0);
if (material.usesColor) {
FragColor += vec4(material.color, 1.0);
}
FragColor = vec4(AMBIENT + DIFFUSE + SPECULAR, 1.0);
if (material.usesTex) {
FragColor += texture(material.tex, UV);
FragColor *= texture(material.tex, UV);
}
if (FragColor.w < material.alphaScissor) {
@ -39,8 +39,4 @@ void main() {
if (material.cullBack && !gl_FrontFacing) {
discard;
}
if (!material.unshaded) {
FragColor *= vec4(AMBIENT + DIFFUSE + SPECULAR, 1.0);
}
}

View File

@ -11,9 +11,9 @@ uniform mat4 PROJECTION;
noperspective out vec2 UV; // PSX use affine texture mapping
out vec3 NORMAL;
flat out vec3 AMBIENT;
flat out vec3 DIFFUSE;
flat out vec3 SPECULAR;
out vec3 AMBIENT;
out vec3 DIFFUSE;
out vec3 SPECULAR;
struct DirectionalLight {
vec3 direction;
@ -23,7 +23,22 @@ struct DirectionalLight {
float specular;
};
struct Material {
sampler2D tex;
bool usesTex;
vec3 ambient;
vec3 diffuse;
vec3 specular;
int shininess;
float alphaScissor;
bool unshaded;
bool cullBack;
};
uniform DirectionalLight directionalLight;
uniform Material material;
void main() {
vec4 vertex = PROJECTION * VIEW * MODEL * vec4(pos, 1.0);
@ -34,17 +49,17 @@ void main() {
NORMAL = (VIEW * MODEL * vec4(normal, 0.0)).xyz;
// Flat shading, we compute light per vertex
AMBIENT = directionalLight.ambient * directionalLight.color;
AMBIENT = directionalLight.ambient * directionalLight.color * material.ambient;
vec3 direction = -(VIEW * vec4(directionalLight.direction, 0.0)).xyz;
float diff = dot(normalize(direction), normalize(NORMAL));
diff = max(diff, 0.0);
DIFFUSE = directionalLight.diffuse * diff * directionalLight.color;
DIFFUSE = directionalLight.diffuse * diff * directionalLight.color * material.diffuse;
vec3 viewDir = (VIEW * MODEL * vec4(pos, 1.0)).xyz;
vec3 viewDir = normalize((VIEW * MODEL * vec4(pos, 1.0)).xyz);
vec3 reflectionDir = reflect(normalize(direction), normalize(NORMAL));
float spec = dot(viewDir, reflectionDir);
spec = max(spec, 0.0);
spec = pow(spec, 2);
SPECULAR = directionalLight.specular * spec * directionalLight.color;
spec = pow(spec, material.shininess);
SPECULAR = directionalLight.color * (spec * material.specular);
}

View File

@ -9,7 +9,7 @@ target_include_directories(couchlua
## ASSIMP
set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT FALSE CACHE BOOL "Turn off all assimp importers")
set(ASSIMP_BUILD_GLTF_IMPORTER ON CACHE BOOL "Turn on gltf importer")
set(ASSIMP_BUILD_OBJ_IMPORTER ON CACHE BOOL "Turn on wavefront importer")
add_subdirectory(assimp)
target_link_libraries(couch assimp::assimp)
target_link_libraries(couchlua assimp::assimp)