]> git.tdb.fi Git - libs/gl.git/blob - source/material.cpp
Support for loading materials from datafiles
[libs/gl.git] / source / material.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "material.h"
9
10 namespace Msp {
11 namespace GL {
12
13 Material::Material():
14         ambient(0.2),
15         diffuse(0.8),
16         specular(0),
17         emission(0),
18         shininess(0)
19 { }
20
21 void Material::set_ambient(const Color &a)
22 {
23         ambient=a;
24 }
25
26 void Material::set_diffuse(const Color &d)
27 {
28         diffuse=d;
29 }
30
31 void Material::set_specular(const Color &s)
32 {
33         specular=s;
34 }
35
36 void Material::set_emission(const Color &e)
37 {
38         emission=e;
39 }
40
41 void Material::set_shininess(float s)
42 {
43         shininess=s;
44 }
45
46 void Material::apply() const
47 {
48         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r);
49         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r);
50         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r);
51         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, &emission.r);
52         glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
53 }
54
55
56 Material::Loader::Loader(Material &m):
57         mat(m)
58 {
59         add("ambient",   &Loader::ambient);
60         add("diffuse",   &Loader::diffuse);
61         add("specular",  &Loader::specular);
62         add("emission",  &Loader::emission);
63         add("shininess", &Material::shininess);
64 }
65
66 void Material::Loader::ambient(float r, float g, float b, float a)
67 {
68         mat.ambient=GL::Color(r, g, b, a);
69 }
70
71 void Material::Loader::diffuse(float r, float g, float b, float a)
72 {
73         mat.diffuse=GL::Color(r, g, b, a);
74 }
75
76 void Material::Loader::specular(float r, float g, float b, float a)
77 {
78         mat.specular=GL::Color(r, g, b, a);
79 }
80
81 void Material::Loader::emission(float r, float g, float b, float a)
82 {
83         mat.emission=GL::Color(r, g, b, a);
84 }
85
86 } // namespace GL
87 } // namespace Msp