-
Notifications
You must be signed in to change notification settings - Fork 0
/
Material.cpp
executable file
·99 lines (64 loc) · 2.33 KB
/
Material.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "Material.h"
#include <iostream>
using namespace std;
////////////////////////////////////////////////
// TEXTURE //
////////////////////////////////////////////////
Texture::Texture(string filename) {
if (filename.find(".jpg") != string::npos)
image = FreeImage_Load(FIF_JPEG, filename.c_str(), JPEG_ACCURATE);
else if (filename.find(".png") != string::npos)
image = FreeImage_Load(FIF_PNG, filename.c_str(), PNG_IGNOREGAMMA);
else {
cout << endl;
cerr << "Error: Unsupported texture format in " << filename << endl;
exit(1);
}
width = FreeImage_GetWidth(image);
height = FreeImage_GetHeight(image);
}
rgb Texture::getColor(const vec2& uvCoord) {
unsigned int i = ((unsigned int)(uvCoord[0] * width)) % width;
unsigned int j = ((unsigned int)(uvCoord[1] * height)) % height;
RGBQUAD color;
FreeImage_GetPixelColor(image, i, j, &color);
return rgb(color.rgbRed / 255.0, color.rgbGreen / 255.0, color.rgbBlue / 255.0);
}
////////////////////////////////////////////////
// MATERIAL //
////////////////////////////////////////////////
/* Constructors */
Material::Material() {
myReflectance.kA = myReflectance.kD = myReflectance.kR =
myReflectance.kS = rgb();
myReflectance.pExp = 0;
}
Material::Material(Reflectance reflec) {
myReflectance = reflec;
}
/* Instance methods */
Reflectance Material::getReflectance(const vec3& point, Shape* shape) {
return myReflectance;
}
////////////////////////////////////////////////
// TEXTUREDMATERIAL //
////////////////////////////////////////////////
TexturedMaterial::TexturedMaterial(Reflectance reflec, Texture* texMap, bool rough)
: Material(reflec) {
texture = texMap;
this->rough = rough;
}
Reflectance TexturedMaterial::getReflectance(const vec3& point, Shape* shape) {
rgb texColor = texture->getColor(shape->getTextureCoordinate(point));
return multiply(Material::getReflectance(point, shape), texColor, rough);
}
Reflectance TexturedMaterial::multiply(const Reflectance& ref, const rgb& color, bool rough) {
Reflectance toReturn = ref;
toReturn.kA = toReturn.kA * color;
toReturn.kD = toReturn.kD * color;
if (rough) {
toReturn.kS = toReturn.kS * color;
toReturn.kR = toReturn.kR * color;
}
return toReturn;
}