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.lightmanager; 30 31 import derelict.opengl.gl; 32 33 import dlib.core.memory; 34 import dlib.math.vector; 35 import dlib.image.color; 36 import dlib.container.array; 37 38 import dgl.core.interfaces; 39 import dgl.graphics.object3d; 40 41 public import dgl.graphics.light; 42 43 class LightManager: Modifier3D, Drawable 44 { 45 DynamicArray!Light lights; 46 uint maxLightsPerObject = 4; 47 bool lightsVisible = false; 48 bool lightsOn = true; 49 50 Light addLight(Light light) 51 { 52 lights.append(light); 53 return light; 54 } 55 56 Light addPointLight(Vector3f position) 57 { 58 Light light = pointLight( 59 position, 60 Color4f(1.0f, 1.0f, 1.0f, 1.0f), 61 Color4f(0.1f, 0.1f, 0.1f, 1.0f)); 62 lights.append(light); 63 return light; 64 } 65 66 void bind(Object3D obj, double dt) 67 { 68 glEnable(GL_LIGHTING); 69 apply(obj.getPosition); 70 } 71 72 void unbind(Object3D obj) 73 { 74 foreach(i; 0..maxLightsPerObject) 75 glDisable(GL_LIGHT0 + i); 76 glDisable(GL_LIGHTING); 77 } 78 79 void calcBrightness(Light light, Vector3f objPos) 80 { 81 if (!light.enabled && !light.forceOn) 82 { 83 light.brightness = 0.0f; 84 } 85 else 86 { 87 Vector3f d = (light.position.xyz - objPos); 88 float quadraticAttenuation = d.lengthsqr; 89 //if (quadraticAttenuation > 100.0f) 90 // light.brightness = 0.0f; 91 //else 92 light.brightness = 1.0f / quadraticAttenuation; 93 } 94 } 95 96 void sortLights() 97 { 98 size_t j = 0; 99 Light tmp; 100 101 auto ldata = lights.data; 102 103 foreach(i, v; ldata) 104 { 105 j = i; 106 size_t k = i; 107 108 while (k < ldata.length) 109 { 110 float b1 = ldata[j].brightness; 111 float b2 = ldata[k].brightness; 112 if (b2 > b1) 113 j = k; 114 k++; 115 } 116 117 tmp = ldata[i]; 118 ldata[i] = ldata[j]; 119 ldata[j] = tmp; 120 } 121 } 122 123 void apply(Vector3f objPos) 124 { 125 auto ldata = lights.data; 126 127 foreach(light; ldata) 128 if (lightsOn || light.forceOn) 129 calcBrightness(light, objPos); 130 131 sortLights(); 132 133 foreach(i; 0..maxLightsPerObject) 134 if (i < lights.length) 135 { 136 auto light = ldata[i]; 137 if (light.enabled && (lightsOn || light.forceOn)) 138 { 139 glEnable(GL_LIGHT0 + i); 140 glLightfv(GL_LIGHT0 + i, GL_POSITION, light.position.arrayof.ptr); 141 glLightfv(GL_LIGHT0 + i, GL_SPECULAR, light.diffuseColor.arrayof.ptr); 142 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, light.diffuseColor.arrayof.ptr); 143 glLightfv(GL_LIGHT0 + i, GL_AMBIENT, light.ambientColor.arrayof.ptr); 144 glLightf( GL_LIGHT0 + i, GL_CONSTANT_ATTENUATION, light.constantAttenuation); 145 glLightf( GL_LIGHT0 + i, GL_LINEAR_ATTENUATION, light.linearAttenuation); 146 glLightf( GL_LIGHT0 + i, GL_QUADRATIC_ATTENUATION, light.quadraticAttenuation); 147 } 148 else 149 { 150 Vector4f p = Vector4f(0, 0, 0, 2); 151 glLightfv(GL_LIGHT0 + i, GL_POSITION, p.arrayof.ptr); 152 } 153 } 154 } 155 156 void unapply() 157 { 158 foreach(i; 0..maxLightsPerObject) 159 glDisable(GL_LIGHT0 + i); 160 } 161 162 void draw(double dt) 163 { 164 // Draw lights 165 if (lightsVisible) 166 { 167 glPointSize(5.0f); 168 foreach(light; lights.data) 169 if (light.debugDraw) 170 { 171 glColor4fv(light.diffuseColor.arrayof.ptr); 172 glBegin(GL_POINTS); 173 glVertex3fv(light.position.arrayof.ptr); 174 glEnd(); 175 } 176 glPointSize(1.0f); 177 } 178 } 179 180 void free() 181 { 182 Delete(this); 183 } 184 185 ~this() 186 { 187 foreach(light; lights.data) 188 light.free(); 189 lights.free(); 190 } 191 }