Added the materials system; Any model can have any number of materials with different textures and properties

This commit is contained in:
grayson committed 2021-09-09 23:18:42 -05:00
1 parent 70318b28fd
commit 39e0c8e808
37 files changed
+1616 -1327

No files matched your search

@@ -1,46 +0,0 @@
# Blender v2.93.3 OBJ File: ''
# www.blender.org
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.380883 0.000961
vt 0.630883 0.250961
vt 0.380883 0.250961
vt 0.875500 0.748039
vt 0.625500 0.498039
vt 0.875500 0.498039
vt 0.625500 0.748039
vt 0.375500 0.498039
vt 0.375500 0.748039
vt 0.125500 0.498039
vt 0.375500 0.248039
vt 0.625500 0.248039
vt 0.625500 0.998039
vt 0.630883 0.000961
vt 0.125500 0.748039
vt 0.375500 0.998039
vn 0.0000 0.0000 -1.0000
vn 1.0000 0.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 0.0000 1.0000 0.0000
s off
f 2/1/1 3/2/1 1/3/1
f 4/4/2 7/5/2 3/6/2
f 8/7/3 5/8/3 7/5/3
f 6/9/4 1/10/4 5/8/4
f 7/5/5 1/11/5 3/12/5
f 4/13/6 6/9/6 8/7/6
f 2/1/1 4/14/1 3/2/1
f 4/4/2 8/7/2 7/5/2
f 8/7/3 6/9/3 5/8/3
f 6/9/4 2/15/4 1/10/4
f 7/5/5 5/8/5 1/11/5
f 4/13/6 2/16/6 6/9/6
@@ -0,0 +1,14 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl Material.001
Ns 750.283502
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
map_Kd diff.png
map_Ks spec.png
@@ -0,0 +1,46 @@
# Blender v2.93.3 OBJ File: ''
# www.blender.org
mtllib spec.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 -2.000000 -1.000000
vt -1.000000 0.000000
vt -2.000000 0.000000
vt -1.000000 -1.000000
vt 0.000000 0.000000
vt 0.000000 -1.000000
vt 1.000000 0.000000
vt 1.000000 -1.000000
vt 2.000000 -0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vt 0.000000 -2.000000
vt 2.000000 -1.000000
vt 1.000000 -2.000000
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 Material.001
s off
f 2/1/1 3/2/1 1/3/1
f 4/4/2 7/5/2 3/2/2
f 8/6/3 5/7/3 7/5/3
f 6/8/4 1/9/4 5/7/4
f 7/5/5 1/10/5 3/11/5
f 4/12/6 6/8/6 8/6/6
f 2/1/1 4/4/1 3/2/1
f 4/4/2 8/6/2 7/5/2
f 8/6/3 6/8/3 5/7/3
f 6/8/4 2/13/4 1/9/4
f 7/5/5 5/7/5 1/10/5
f 4/12/6 2/14/6 6/8/6
Binary file not shown.

After

Width:  |  Height:  |  Size: 457 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.9 KiB

@@ -0,0 +1,10 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl None
Ns 100
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
@@ -1,5 +1,6 @@
# Blender v2.93.3 OBJ File: ''
# www.blender.org
mtllib cone.mtl
o Cone
v 0.000000 -1.000000 -1.000000
v 0.195090 -1.000000 -0.980785
@@ -133,6 +134,7 @@ vn -0.4969 0.4472 -0.7437
vn -0.3423 0.4472 -0.8263
vn 0.0000 -1.0000 0.0000
vn -0.1745 0.4472 -0.8772
usemtl None
s 1
f 1/1/1 33/2/2 2/3/3
f 2/3/3 33/2/2 3/4/4
@@ -0,0 +1,10 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl None
Ns 100
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
@@ -1,6 +1,7 @@
# Blender v2.93.3 OBJ File: ''
# www.blender.org
o Cube_Cube.002
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
@@ -29,6 +30,7 @@ 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 2/1/1 3/2/1 1/3/1
f 4/4/2 7/5/2 3/2/2
@@ -0,0 +1,10 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl None
Ns 100
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
@@ -1,6 +1,7 @@
# Blender v2.93.3 OBJ File: ''
# www.blender.org
o Cylinder
mtllib cylinder.mtl
o Cylinder_Cylinder.001
v 0.000000 -1.000000 -1.000000
v 0.000000 1.000000 -1.000000
v 0.195090 -1.000000 -0.980785
@@ -229,6 +230,7 @@ vn -0.3827 0.0000 -0.9239
vn 0.0000 1.0000 -0.0000
vn -0.1951 0.0000 -0.9808
vn 0.0000 -1.0000 0.0000
usemtl None
s 1
f 2/1/1 3/2/2 1/3/1
f 4/4/2 5/5/3 3/2/2
@@ -0,0 +1,10 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl None
Ns 100
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
@@ -1,5 +1,6 @@
# Blender v2.93.3 OBJ File: ''
# www.blender.org
mtllib plane.mtl
o Plane
v -1.000000 0.000000 1.000000
v 1.000000 0.000000 1.000000
@@ -10,6 +11,7 @@ vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 1.000000
vn 0.0000 1.0000 0.0000
usemtl None
s off
f 2/1/1 3/2/1 1/3/1
f 2/1/1 4/4/1 3/2/1
@@ -0,0 +1,10 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl None
Ns 100
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
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,10 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl None
Ns 100
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
@@ -1,5 +1,6 @@
# Blender v2.93.3 OBJ File: ''
# www.blender.org
mtllib torus.mtl
o Torus
v 1.250000 0.000000 0.000000
v 1.216506 0.125000 0.000000
@@ -1742,6 +1743,7 @@ vn 0.5036 -0.8614 0.0663
vn 0.8612 -0.4954 0.1134
vn -0.8559 0.5047 -0.1127
vn -0.4876 -0.8707 -0.0642
usemtl None
s 1
f 13/1/1 2/2/2 1/3/3
f 2/2/2 15/4/4 3/5/5
@@ -9,8 +9,12 @@ struct Camera {
};
struct Material {
float shininess;
//Do I want a specular color here?
bool hasDiffuseTex;
sampler2D diffuseTexture;
vec3 diffuseColor;
bool hasSpecTex;
sampler2D specularTexture;
float specPower;
};
struct Light {
@@ -21,7 +25,6 @@ struct Light {
float strength;
};
uniform sampler2D modelTexture;
uniform Camera camera;
uniform Material material;
uniform Light light[100];
@@ -30,13 +33,21 @@ uniform int numberOfLights;
out vec4 outColor;
void main() {
vec4 texColor = texture(modelTexture, texCoord);
vec3 color = vec3(0.0);
vec3 matDiff;
if (material.hasDiffuseTex)
matDiff = texture(material.diffuseTexture, texCoord).rgb;
else
matDiff = material.diffuseColor;
vec3 matSpec = vec3(1.0);
if(material.hasSpecTex)
matSpec = texture(material.specularTexture, texCoord).rgb;
for (int i = 0; i < numberOfLights; i++) {
float ambientStrength = 0.2f;
vec3 ambient = ambientStrength * texColor.rgb;
vec3 ambient = ambientStrength * matDiff;
if (i == numberOfLights - 1 && numberOfLights == 1) {
color += ambient;
break;
@@ -46,12 +57,12 @@ void main() {
vec3 lightDir = normalize(-light[i].pos);
vec3 norm = normalize(normals);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light[i].color * (diff * texColor.rgb) * light[i].strength;
vec3 diffuse = light[i].color * (diff * matDiff) * (light[i].strength / 2.0f);
vec3 viewDir = normalize(camera.pos - fragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess * 32.0f);
vec3 specular = light[i].color * spec * light[i].strength;
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.specPower);
vec3 specular = light[i].color * spec * matSpec * (light[i].strength / 2.0f);
color += (ambient + diffuse + specular);
continue;
@@ -60,12 +71,12 @@ void main() {
vec3 lightDir = normalize(light[i].pos - fragPos);
vec3 norm = normalize(normals);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light[i].color * (diff * texColor.rgb) * light[i].strength;
vec3 diffuse = light[i].color * (diff * matDiff) * (light[i].strength / 2.0f);
vec3 viewDir = normalize(camera.pos - fragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess * 32.0f);
vec3 specular = light[i].color * spec * (light[i].strength) / 8.0f;
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.specPower);
vec3 specular = light[i].color * spec * matSpec * (light[i].strength / 2.0f);
float length = length(light[i].pos - fragPos);
float att = clamp(10.0 / length, 0.0, 1.0) * light[i].strength;
@@ -74,5 +85,5 @@ void main() {
continue;
}
}
outColor = vec4(color, texColor.a);
outColor = vec4(color, 1.0);
}
+53 -1
View File
@@ -64,6 +64,22 @@ void writeFile(const std::string& filename, const std::string& in, bool encrypte
out.close();
}
void getNeededImages(std::string mtl, std::set<std::string>& set) {
while (mtl.size()) {
unsigned int pos = mtl.find("map_");
if (pos == std::string::npos)
break;
mtl = mtl.substr(pos + 7);
std::stringstream ss(mtl);
std::string temp;
ss >> temp;
unsigned int pos2 = temp.find_last_of("/\\");
if (pos2 != std::string::npos)
temp = temp.substr(pos2 + 1);
set.insert(temp);
}
}
int main(int argc, char* argv[]) {
Log("Starting up");
@@ -84,6 +100,7 @@ int main(int argc, char* argv[]) {
continue;
std::string filename = currDir.path().filename().string().append(".nfpack");
Log("Creating pack \"" + filename + (std::string)"\"");
std::string currFileExtension;
std::string currFileContents;
std::stringstream out;
unsigned int fileCount = 0;
@@ -91,11 +108,46 @@ int main(int argc, char* argv[]) {
if (curr.is_directory())
continue;
std::filesystem::path relative = std::filesystem::relative(curr, currDir);
if (extensions.find(relative.extension().string().substr(1)) == extensions.end())
currFileExtension = relative.extension().string().substr(1);
if (currFileExtension == "mtl")
continue;
if (extensions.find(currFileExtension) == extensions.end())
Error("File \"" + relative.string() + (std::string)"\" is not of supported type!");
Log("Current file: " + relative.string());
currFileContents = readFile(curr.path().string());
if (currFileExtension == "obj") {
std::filesystem::path mtlPath;
for (const auto& curr2 : std::filesystem::recursive_directory_iterator(curr.path().parent_path())) {
if (curr2.is_directory())
continue;
if (curr2.path().extension() != ".mtl")
continue;
std::string mtlFile = relative.filename().string().substr(0, relative.filename().string().size() - 4) + (std::string)".mtl";
if (curr2.path().filename().string() == mtlFile) {
mtlPath = curr2.path();
break;
}
}
if (mtlPath.empty())
Error("No mtl file found for " + relative.filename().string() + (std::string)"!");
Log("Found mtl file for " + relative.filename().string());
std::set<std::string> neededImages;
std::string mtlContents = readFile(mtlPath.string());
getNeededImages(mtlContents, neededImages);
if (!neededImages.empty()) {
currFileContents.insert(0, "\n");
for(std::string curr : neededImages) {
currFileContents.insert(0, curr);
currFileContents.insert(0, " ");
}
currFileContents = currFileContents.substr(1);
}
else {
currFileContents.insert(0, "none\n");
}
currFileContents += '\n' + mtlContents;
}
if (out.rdbuf()->in_avail() != 0)
out << "\n";
out << "#NFASSET " + curr.path().filename().string();