1 /*
2 Copyright (c) 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.light;
30 
31 import derelict.opengl.gl;
32 import dlib.core.memory;
33 import dlib.math.vector;
34 import dlib.image.color;
35 import dgl.graphics.object3d;
36 
37 class Light: Drawable3D
38 {
39     Vector4f position;
40     Color4f diffuseColor;
41     Color4f ambientColor;
42     float constantAttenuation;
43     float linearAttenuation;
44     float quadraticAttenuation;
45     float brightness;
46     bool enabled = true;
47     bool debugDraw = false;
48     bool forceOn = false;
49 
50     this(
51         Vector4f position,
52         Color4f diffuseColor,
53         Color4f ambientColor,
54         float constantAttenuation,
55         float linearAttenuation,
56         float quadraticAttenuation)
57     {
58         this.position = position;
59         this.diffuseColor = diffuseColor;
60         this.ambientColor = ambientColor;
61         this.constantAttenuation = constantAttenuation;
62         this.linearAttenuation = linearAttenuation;
63         this.quadraticAttenuation = quadraticAttenuation;
64     }
65 
66     void draw(double dt)
67     {
68         if (debugDraw)
69         {
70             glDisable(GL_LIGHTING);
71             glColor4fv(diffuseColor.arrayof.ptr);
72             glPointSize(5.0f);
73             glBegin(GL_POINTS);
74             glVertex3f(0, 0, 0);
75             glEnd();
76             glPointSize(1.0f);
77             glEnable(GL_LIGHTING);
78         }
79     }
80 
81     void draw(Object3D obj, double dt)
82     {
83         position = Vector4f(obj.getPosition);
84         position.w = 1.0f;
85         draw(dt);
86     }
87 
88     override void free()
89     {
90         Delete(this);
91     }
92 }
93 
94 Light pointLight(
95     Vector3f pos,
96     Color4f diffuseColor,
97     Color4f ambientColor,
98     float constantAttenuation = 1.0f,
99     float linearAttenuation = 0.0f,
100     float quadraticAttenuation = 0.2f)
101 {
102     return New!Light(
103         Vector4f(pos.x, pos.y, pos.z, 1.0f),
104         diffuseColor, ambientColor,
105         constantAttenuation, linearAttenuation,
106         quadraticAttenuation);
107 }