1 /*
2 Copyright (c) 2013-2015 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module dgl.graphics.material;
30 
31 import std.string;
32 
33 import derelict.opengl.gl;
34 import derelict.opengl.glext;
35 
36 import dlib.core.memory;
37 import dlib.image.color;
38 
39 import dgl.core.interfaces;
40 import dgl.graphics.texture;
41 import dgl.graphics.shader;
42 
43 enum TextureCombinerMode: ushort
44 {
45     Blend = 0,
46     Modulate = 1,
47     Add = 2,
48     Subtract = 3,
49     Dot3 = 4,
50     Dot3Alpha = 5
51 }
52 
53 // TODO: VERY dirty hack, use class for this!
54 bool useDimLight = false;
55 
56 class Material: Modifier
57 {
58     int id;
59     string name;
60 
61     Color4f ambientColor;
62     Color4f diffuseColor;
63     Color4f specularColor;
64     Color4f emissionColor;
65     float shininess;
66     Shader shader;
67     Texture[8] textures;
68     ushort[8] texBlendMode;
69     bool shadeless = false;
70     bool useTextures = true;
71     bool additiveBlending = false;
72 
73     this()
74     {
75         ambientColor = Color4f(0.7f, 0.7f, 0.7f, 1.0f);
76         diffuseColor = Color4f(0.8f, 0.8f, 0.8f, 1.0f);
77         specularColor = Color4f(1.0f, 1.0f, 1.0f, 1.0f);
78         emissionColor = Color4f(0.0f, 0.0f, 0.0f, 0.0f);
79         shininess = 64.0f;
80     }
81 
82     @property uint numTextures()
83     {
84         uint res = 0;
85         foreach(t; textures)
86             if (t !is null) res++;
87         return res;
88     }
89 
90     void bind(double dt)
91     {
92         glDisable(GL_TEXTURE_2D);
93         
94         glEnable(GL_LIGHTING);
95         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambientColor.arrayof.ptr);
96         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuseColor.arrayof.ptr);
97         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specularColor.arrayof.ptr);
98         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emissionColor.arrayof.ptr);
99         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &shininess);
100 
101         if (additiveBlending)
102             glBlendFunc(GL_ONE, GL_ONE);
103 
104         glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
105         if (shadeless && !useDimLight)
106         {
107             glDisable(GL_LIGHTING);
108             glColor4f(diffuseColor.r, diffuseColor.g, diffuseColor.b, diffuseColor.a);
109         }
110 
111         if (useTextures)
112         foreach(i, tex; textures)
113         {
114             if (tex !is null)
115             {
116                 glActiveTextureARB(GL_TEXTURE0_ARB + cast(uint)i);
117                 tex.bind(dt);
118             }
119         }
120 
121         if (shader && !useDimLight)
122             shader.bind(dt);
123     }
124 
125     void unbind()
126     {
127         if (shader && !useDimLight)
128             shader.unbind();
129 
130         if (useTextures)
131         foreach(i, tex; textures)
132         {
133             if (tex !is null)
134             {
135                 glActiveTextureARB(GL_TEXTURE0_ARB + cast(uint)i);
136                 tex.unbind();
137             }
138         }
139 
140         glActiveTextureARB(GL_TEXTURE0_ARB);
141 
142         if (shadeless && !useDimLight)
143             glEnable(GL_LIGHTING);
144 
145         if (additiveBlending)
146             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
147     }
148 
149     override string toString()
150     {
151         return format(
152             "id = %s\n"
153             "name = %s\n"
154             "ambientColor = %s\n"
155             "diffuseColor = %s\n"
156             "specularColor = %s\n"
157             "emissionColor = %s",
158             id,
159             name,
160             ambientColor,
161             diffuseColor,
162             specularColor,
163             emissionColor
164         );
165     }
166 
167     void free()
168     {
169         //if (name.length)
170         //    Delete(name);
171         Delete(this);
172     }
173 }