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