-
Notifications
You must be signed in to change notification settings - Fork 2
/
Material.cpp
222 lines (178 loc) · 5.48 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// @Begin License@
// This file is part of Coldest.
//
// Coldest is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Coldest is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Coldest. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2008-2012 Ben Nemec
// @End License@
#include "Material.h"
#include "util.h"
int Material::nummats = 0;
#ifndef DEDICATED
Material::Material(string filename, TextureManager& tm, Shader& s) : diffuse(4, 0.f), ambient(4, 0.f), specular(4, 0.f), shininess(0.f),
texid(8, 0), texfilename(8, ""), shader(""), shaderid(0), texman(&tm), shaderhand(&s), id(nummats), cullface(true), doalphatest(false),
alphatocoverage(false), additive(false), depthtest(true), depthwrite(true), noshadowcull(false), alphatest(0.f)
{
ifstream check(filename.c_str());
if (check.fail())
{
logout << "Failed to load material " << filename << endl;
filename = "materials/default";
}
NTreeReader reader(filename);
for (int i = 0; i < 4; ++i)
reader.Read(diffuse[i], "Diffuse", i);
for (int i = 0; i < 4; ++i)
reader.Read(ambient[i], "Ambient", i);
for (int i = 0; i < 4; ++i)
reader.Read(specular[i], "Specular", i);
reader.Read(shininess, "Shininess");
bool repeat = false;
reader.Read(repeat, "Repeat");
for (int i = 0; i < 8; ++i)
{
string name = "Texture";
name += ToString(i);
reader.Read(texfilename[i], name);
if (texfilename[i] != "")
texid[i] = texman->LoadTexture(texfilename[i]);
if (repeat)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
}
reader.Read(shader, "Shader");
shaderhand->LoadShader(shader);
reader.Read(cullface, "CullFace");
reader.Read(noshadowcull, "NoShadowCull");
reader.Read(alphatest, "AlphaTest");
if (alphatest > 1e-6)
doalphatest = true;
reader.Read(additive, "Additive");
reader.Read(alphatocoverage, "AlphaToCoverage");
reader.Read(depthtest, "DepthTest");
reader.Read(depthwrite, "DepthWrite");
++nummats;
}
#else
Material::Material(){}
#endif
void Material::Use() const
{
#ifndef DEDICATED
for (int i = 0; i < 6; ++i) // At this time, textures 7 and 8 are reserved for shadowmaps
{
if (texid[i])
{
texman->texhand->ActiveTexture(i);
texman->texhand->BindTexture(texid[i]);
}
}
texman->texhand->ActiveTexture(0);
shaderhand->UseShader(shader);
if (cullface)
{
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
}
else glDisable(GL_CULL_FACE);
if (depthtest)
glEnable(GL_DEPTH_TEST);
else glDisable(GL_DEPTH_TEST);
if (depthwrite)
glDepthMask(GL_TRUE);
else glDepthMask(GL_FALSE);
if (doalphatest)
{
glAlphaFunc(GL_GREATER, alphatest);
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ZERO);
}
else
{
if (additive)
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
else
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_ALPHA_TEST);
}
if (alphatocoverage)
{
// Not entirely happy with the way this looks, but it could be worse
glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
glSampleCoverage(1.f, GL_FALSE);
glDisable(GL_BLEND);
}
else
{
glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
glEnable(GL_BLEND);
}
//glDisable(GL_COLOR_MATERIAL); // TODO: This should probably be removed at some point
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, &ambient[0]);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse[0]);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &specular[0]);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
#endif
}
void Material::UseTextureOnly() const
{
#ifndef DEDICATED
texman->texhand->BindTexture(texid[0]);
// Also do face culling so we can render only back faces for shadowing
if (cullface && !noshadowcull)
{
glEnable(GL_CULL_FACE);
}
else glDisable(GL_CULL_FACE);
#endif
}
void Material::SetTexture(int texunit, GLuint tex)
{
if (texfilename[texunit] != "")
logout << "Warning: Possible memory leak in Material::SetTexture" << endl;
texfilename[texunit] = "";
texid[texunit] = tex;
}
void Material::SetTexture(int texunit, string tex)
{
#ifndef DEDICATED
if (texfilename[texunit] != "")
logout << "Warning: Possible memory leak in Material::SetTexture" << endl;
texfilename[texunit] = tex;
texid[texunit] = texman->LoadTexture(texfilename[texunit]);
#endif
}
GLuint Material::GetTexture(int texunit)
{
return texid[texunit];
}
// Define an ordering on materials so that Mesh can sort by material
bool Material::operator<(const Material& m) const
{
return id < m.id;
}
void Material::Release()
{
#ifndef DEDICATED
for (int i = 0; i < 6; ++i)
{
if (texfilename[i] != "")
texman->DeleteTexture(texfilename[i]);
// Textures in texid are not our problem because they should come from FBO's.
}
#endif
}