summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/src/Plugins/RenderingEngines/OpenGL/GLT/material.cpp
blob: ff3c9932f2ac727f044f581f4d91210a4b4cda98 (plain)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include "glt_material.h"

/*! \file 
    \ingroup GLT
    
    $Id: material.cpp,v 1.10 2002/10/09 15:09:38 nigels Exp $
    
    $Log: material.cpp,v $
    Revision 1.10  2002/10/09 15:09:38  nigels
    Added RCS Id and Log tags


*/

#include "glt_error.h"

#include <cassert>
#include <cstring>
#include <iostream>
using namespace std;

GltMaterial::GltMaterial(const GLenum face,const bool getIt)
: _face(face)
{
	assert(_face==GL_FRONT_AND_BACK || _face==GL_FRONT || _face==GL_BACK);

	// TODO - OpenGL Defaults
	
	_shininess = 0.0;
	memset(_colorIndex,0,sizeof(_colorIndex));

	if (getIt)
		get();
}

GltMaterial::GltMaterial
(
	const GltColor &ambient,
	const GltColor &diffuse,
	const GltColor &specular,
	const GltColor &emission,
	const GLfloat   shininess,
	const GLenum face
)
: 
	_face(face),
	_ambient(ambient),
	_diffuse(diffuse),
	_specular(specular),
	_emission(emission),
	_shininess(shininess)
{
	assert(_face==GL_FRONT_AND_BACK || _face==GL_FRONT || _face==GL_BACK);

	memset(_colorIndex,0,sizeof(_colorIndex));
}

GltMaterial::GltMaterial(const std::string &name,const GLenum face)
: _face(face)
{
	assert(_face==GL_FRONT_AND_BACK || _face==GL_FRONT || _face==GL_BACK);

	// Do a binary search through the list of names

	int i=0;
	int j=_matSize;

	for (;;)
	{
		int k = ((j-i)>>1)+i;
		const int res = strcmp(name.c_str(),_matName[k]);

		if (res<0)
			j = k;
		else
			if (res>0)
				i = k+1;
			else
			{
				operator=(*_matValue[k]);
				return;
			}

		if (i==k && j==k || i==_matSize)
		{
			assert(!"Material not found");
			operator=(GltMaterial());
			return;
		}
	}
}

GltMaterial::~GltMaterial()
{
}

void 
GltMaterial::get()
{
	// Can't query front and back at same time - beware
	const GLenum face = (_face==GL_FRONT_AND_BACK ? GL_FRONT : _face);

	GLERROR(std::cerr);

	_ambient. glGetMaterial(face,GL_AMBIENT);
	_diffuse. glGetMaterial(face,GL_DIFFUSE);
	_specular.glGetMaterial(face,GL_SPECULAR);
	_emission.glGetMaterial(face,GL_EMISSION);

	glGetMaterialfv(face, GL_SHININESS, &_shininess);

	GLERROR(std::cerr);
}

void 
GltMaterial::set() const
{
	GLERROR(std::cerr);

	_ambient. glMaterial(_face,GL_AMBIENT);
	_diffuse. glMaterial(_face,GL_DIFFUSE);
	_specular.glMaterial(_face,GL_SPECULAR);
	_emission.glMaterial(_face,GL_EMISSION);

	glMaterialfv(_face, GL_SHININESS, &_shininess);

	GLERROR(std::cerr);
}

GLenum   &GltMaterial::face()       { return _face;       }
GltColor &GltMaterial::ambient()    { return _ambient;    }
GltColor &GltMaterial::diffuse()    { return _diffuse;    }
GltColor &GltMaterial::specular()   { return _specular;   }
GltColor &GltMaterial::emission()   { return _emission;   }
GLfloat  &GltMaterial::shininess()  { return _shininess;  }
GLint    *GltMaterial::colorIndex() { return _colorIndex; }

const GLenum   &GltMaterial::face()       const { return _face;       }
const GltColor &GltMaterial::ambient()    const { return _ambient;    }
const GltColor &GltMaterial::diffuse()    const { return _diffuse;    }
const GltColor &GltMaterial::specular()   const { return _specular;   }
const GltColor &GltMaterial::emission()   const { return _emission;   }
const GLfloat  &GltMaterial::shininess()  const { return _shininess;  }
const GLint    *GltMaterial::colorIndex() const { return _colorIndex; }

//
// Adapted from 
//	http://www.cs.utk.edu/~kuck/materials_ogl.htm
//	http://devernay.free.fr/cours/opengl/materials.html
//  http://www.sgi.com/software/opengl/advanced98/notes/node119.html
//  

/// Black Plastic Material

const GltMaterial blackPlasticMaterial
(
	GltColor(0.00, 0.00, 0.00),	// Ambient
	GltColor(0.01, 0.01, 0.01),	// Diffuse
	GltColor(0.50, 0.50, 0.50), // Specular
	GltColor(0.00, 0.00, 0.00),	// Emission
	32.0f						// Shininess
);

/// Black Rubber Material

const GltMaterial blackRubberMaterial
(
	GltColor(0.02, 0.02, 0.02),	// Ambient
	GltColor(0.01, 0.01, 0.01),	// Diffuse
	GltColor(0.40, 0.40, 0.40), // Specular
	GltColor(0.00, 0.00, 0.00),	// Emission
	10.0f						// Shininess
);

/// Brass Material

const GltMaterial brassMaterial
(
	GltColor(0.329412, 0.223529, 0.027451),	// Ambient
	GltColor(0.780392, 0.568627, 0.113725),	// Diffuse
	GltColor(0.992157, 0.941176, 0.807843), // Specular
	GltColor(0.0,0.0,0.0),					// Emission
	27.8974f								// Shininess
);

/// Bronze Material

const GltMaterial bronzeMaterial
(
	GltColor(0.2125  , 0.1275  , 0.054   ),	// Ambient
	GltColor(0.714   , 0.4284  , 0.18144 ),	// Diffuse
	GltColor(0.393548, 0.271906, 0.166721), // Specular
	GltColor(0.0,0.0,0.0),					// Emission
	25.6f									// Shininess
);

/// Chrome Material

const GltMaterial chromeMaterial
(
	GltColor(0.25,     0.25,     0.25    ),	// Ambient
	GltColor(0.4,      0.4,      0.4     ),	// Diffuse
	GltColor(0.774597, 0.774597, 0.774597), // Specular
	GltColor(0.0,0.0,0.0),					// Emission
	76.8f									// Shininess
);

/// Copper Material

const GltMaterial copperMaterial
(
	GltColor(0.19125,  0.0735,   0.0225  ),	// Ambient
	GltColor(0.7038,   0.27048,  0.0828  ),	// Diffuse
	GltColor(0.256777, 0.137622, 0.086014),	// Specular
	GltColor(0.00,0.00,0.00),				// Emission
	12.8f									// Shininess
);

/// Emerald Material

const GltMaterial emeraldMaterial
(
	GltColor(0.0215,  0.1745,   0.0215 ),	// Ambient
	GltColor(0.07568, 0.61424,  0.07568),	// Diffuse
	GltColor(0.633,   0.727811, 0.633  ),	// Specular
	GltColor(0.00,0.00,0.00),				// Emission
	76.8f									// Shininess
);

/// Gold Material

const GltMaterial goldMaterial
(
	GltColor(0.24725,  0.1995,   0.0745),	// Ambient
	GltColor(0.75164,  0.60648,  0.22648),	// Diffuse
	GltColor(0.628281, 0.555802, 0.366065),	// Specular
	GltColor(0.00,0.00,0.00),				// Emission
	51.2f									// Shininess
);

/// Jade Material

const GltMaterial jadeMaterial
(
	GltColor(0.135,    0.2225,   0.1575  ),	// Ambient
	GltColor(0.135,    0.2225,   0.1575  ),	// Diffuse
	GltColor(0.316228, 0.316228, 0.316228),	// Specular
	GltColor(0.00,0.00,0.00),				// Emission
	12.8f									// Shininess
);

/// Obsidian Material

const GltMaterial obsidianMaterial
(
	GltColor(0.05375,  0.05,     0.06625 ),	// Ambient
	GltColor(0.18275,  0.17,     0.22525 ),	// Diffuse
	GltColor(0.332741, 0.328634, 0.346435),	// Specular
	GltColor(0.00,0.00,0.00),				// Emission
	38.4f									// Shininess
);

/// Pearl Material

const GltMaterial pearlMaterial
(
	GltColor(0.25,     0.20725,  0.20725 ),	// Ambient
	GltColor(1.0,      0.829,    0.829   ),	// Diffuse
	GltColor(0.296648, 0.296648, 0.296648),	// Specular
	GltColor(0.00,0.00,0.00),				// Emission
	11.264f									// Shininess
);

/// Pewter Material

const GltMaterial pewterMaterial
(
	GltColor(0.105882, 0.058824, 0.113725),	// Ambient
	GltColor(0.427451, 0.470588, 0.541176),	// Diffuse
	GltColor(0.333333, 0.333333, 0.521569),	// Specular
	GltColor(0.00,0.00,0.00),				// Emission
	9.84615f								// Shininess
);

/// Polished Bronze Material

const GltMaterial polishedBronzeMaterial
(
	GltColor(0.25    , 0.148   , 0.06475 ),	// Ambient
	GltColor(0.4     , 0.2368  , 0.1036  ),	// Diffuse
	GltColor(0.774597, 0.458561, 0.200621), // Specular
	GltColor(0.0,0.0,0.0),					// Emission
	76.8f									// Shininess
);

/// Polished Copper Material

const GltMaterial polishedCopperMaterial
(
	GltColor(0.2295,   0.08825,  0.0275   ),	// Ambient
	GltColor(0.5508,   0.2118,   0.066    ),	// Diffuse
	GltColor(0.580594, 0.223257, 0.0695701),	// Specular
	GltColor(0.0,0.0,0.0),						// Emission
	51.2f										// Shininess
);

/// Polished Gold Material

const GltMaterial polishedGoldMaterial
(
	GltColor(0.24725,  0.2245,   0.0645  ),	// Ambient
	GltColor(0.34615,  0.3143,   0.0903  ),	// Diffuse
	GltColor(0.797357, 0.723991, 0.208006),	// Specular
	GltColor(0.0,0.0,0.0),					// Emission
	83.2f									// Shininess
);

/// Polished Silver Material

const GltMaterial polishedSilverMaterial
(
	GltColor(0.23125, 0.23125, 0.23125),	// Ambient
	GltColor(0.2775,  0.2775,  0.2775),		// Diffuse
	GltColor(0.773911, 0.773911, 0.773911),	// Specular
	GltColor(0.0,0.0,0.0),					// Emission
	89.6f									// Shininess
);

/// Ruby Material

const GltMaterial rubyMaterial
(
	GltColor(0.1745,   0.01175,  0.01175),	// Ambient
	GltColor(0.61424,  0.04136,  0.04136),	// Diffuse
	GltColor(0.727811, 0.626959, 0.626959),	// Specular
	GltColor(0.00,0.00,0.00),				// Emission
	76.8f									// Shininess
);

/// Silver Material

const GltMaterial silverMaterial
(
	GltColor(0.19225,  0.19225,  0.19225 ),	// Ambient
	GltColor(0.50754,  0.50754,  0.50754 ),	// Diffuse
	GltColor(0.508273, 0.508273, 0.508273),	// Specular
	GltColor(0.00,0.00,0.00),				// Emission
	51.2f									// Shininess
);

/// Turquoise Material

const GltMaterial turquoiseMaterial
(
	GltColor(0.1,      0.18725, 0.1745  ),	// Ambient
	GltColor(0.396,    0.74151, 0.69102 ),	// Diffuse
	GltColor(0.297254, 0.30829, 0.306678),	// Specular
	GltColor(0.00,0.00,0.00),				// Emission
	12.8f									// Shininess
);

/// White Plastic Material

const GltMaterial whitePlasticMaterial
(
	GltColor(0.00, 0.00, 0.00),	// Ambient
	GltColor(0.55, 0.55, 0.55),	// Diffuse
	GltColor(0.70, 0.70, 0.70), // Specular
	GltColor(0.00, 0.00, 0.00),	// Emission
	32.0f						// Shininess
);

//

const int   GltMaterial::_matSize = 20;

const char *GltMaterial::_matName[20] =
{
	"blackPlastic",
	"blackRubber",
	"brass",
	"bronze",
	"chrome",
	"copper",
	"emerald",
	"gold",
	"jade",
	"obsidian",
	"pearl",
	"pewter",
	"polishedBronze",
	"polishedCopper",
	"polishedGold",
	"polishedSilver",
	"ruby",
	"silver",
	"turquoise",
	"whitePlastic"
};

const GltMaterial *GltMaterial::_matValue[20] =
{
	&::blackPlasticMaterial,
	&::blackRubberMaterial,
	&::brassMaterial,
	&::bronzeMaterial,
	&::chromeMaterial,
	&::copperMaterial,
	&::emeraldMaterial,
	&::goldMaterial,
	&::jadeMaterial,
	&::obsidianMaterial,
	&::pearlMaterial,
	&::pewterMaterial,
	&::polishedBronzeMaterial,
	&::polishedCopperMaterial,
	&::polishedGoldMaterial,
	&::polishedSilverMaterial,
	&::rubyMaterial,
	&::silverMaterial,
	&::turquoiseMaterial,
	&::whitePlasticMaterial
};