ecere/gfx/drivers/GL3: Fixed missing precision for WebGL
[sdk] / ecere / src / gfx / drivers / gl3 / defaultShader.ec
1 import "shaders"
2
3 #include "gl123es.h"
4
5 namespace gfx::drivers;
6
7 // **********   Default Shader   **********
8 public enum LightMode { off, dir, pos, posAtt, posSpot, posSpotAtt };
9 public enum SwizzleMode { off, alpha, red };
10
11 public class DefaultShaderBits : uint64
12 {
13 public:
14    bool lighting:1;
15    bool nonLocalViewer:1;
16    bool twoSided:1;
17    bool specular:1;
18    bool separateSpecular:1;
19    bool blinnSpecular:1;
20    uint lightBits:24;
21    bool perVertexColor:1;
22    SwizzleMode swizzle:2;
23    bool textureMatrix:1;
24    bool texturing:1;
25    bool cubeMap:1;
26    bool modelView:1;
27    bool fog:1;
28    bool normalsMapping:1;
29    bool specularMapping:1;
30    bool environmentMapping:1;
31    bool reflection:1;
32    bool reflectionMap:1;
33    bool refraction:1;
34    bool debugging:1;
35 };
36
37 public class CompiledDefaultShader : CompiledShader
38 {
39 public:
40    // Uniforms
41    int uPrjMatrix;
42    int uMVMatrix;
43    int uTextureMatrix;
44    int uNormalsMatrix;
45    int uFogDensity;
46    int uFogColor;
47    int uGlobalAmbient;
48    int uNearPlane;
49    int uLightsPos[8];
50    int uLightsDiffuse[8];
51    int uLightsAmbient[8];
52    int uLightsSpecular[8];
53    int uLightsAtt[8];
54    int uLightsSpotDir[8];
55    int uLightsSpotCutOffCos[8];
56    int uLightsSpotExp[8];
57    int uMatDiffuse;
58    int uMatAmbient;
59    int uMatSpecular;
60    int uMatEmissive;
61    int uMatPower;
62    int uMatOpacity;
63    int uDiffuseTex;
64    int uBumpTex;
65    int uSpecularTex;
66    int uEnvTex;
67    int uReflectTex;
68    int uRefractionETA;
69    int uMatReflectivity;
70    int uCubeMapMatrix;
71
72    void registerUniforms(int program, DefaultShaderBits state)
73    {
74 #if ENABLE_GL_SHADERS
75       uPrjMatrix        = glGetUniformLocation(program, "projection_matrix");
76       uMatDiffuse       = glGetUniformLocation(program, "matDiffuse");
77
78       if(state.modelView)
79          uMVMatrix         = glGetUniformLocation(program, "modelview_matrix");
80
81       if(state.lighting)
82       {
83          int i;
84
85          for(i = 0; i < 8; i++)
86          {
87             char name[100];
88
89             sprintf(name, "lightsPos[%d]", i);
90             uLightsPos[i] = glGetUniformLocation(program, name);
91
92             sprintf(name, "lightsDiffuse[%d]", i);
93             uLightsDiffuse[i] = glGetUniformLocation(program, name);
94
95             sprintf(name, "lightsAmbient[%d]", i);
96             uLightsAmbient[i] = glGetUniformLocation(program, name);
97
98             sprintf(name, "lightsSpecular[%d]", i);
99             uLightsSpecular[i] = glGetUniformLocation(program, name);
100
101             sprintf(name, "lightsAtt[%d]", i);
102             uLightsAtt[i] = glGetUniformLocation(program, name);
103
104             sprintf(name, "lightsSpotDir[%d]", i);
105             uLightsSpotDir[i] = glGetUniformLocation(program, name);
106
107             sprintf(name, "lightsSpotCutOffCos[%d]", i);
108             uLightsSpotCutOffCos[i] = glGetUniformLocation(program, name);
109
110             sprintf(name, "lightsSpotExp[%d]", i);
111             uLightsSpotExp[i] = glGetUniformLocation(program, name);
112          }
113
114          uNearPlane        = glGetUniformLocation(program, "nearPlane");
115          uNormalsMatrix    = glGetUniformLocation(program, "normals_matrix");
116          uGlobalAmbient    = glGetUniformLocation(program, "globalAmbient");
117          uMatAmbient       = glGetUniformLocation(program, "matAmbient");
118          uMatEmissive      = glGetUniformLocation(program, "matEmissive");
119          if(state.specular)
120          {
121             uMatSpecular      = glGetUniformLocation(program, "matSpecular");
122             uMatPower         = glGetUniformLocation(program, "matPower");
123          }
124       }
125
126       if((state.specularMapping || state.normalsMapping || state.texturing || state.reflectionMap || state.cubeMap) && state.textureMatrix)
127          uTextureMatrix    = glGetUniformLocation(program, "texture_matrix");
128       if(state.texturing || state.cubeMap)
129          uDiffuseTex    = glGetUniformLocation(program, "diffuseTex");
130       if(state.normalsMapping)
131          uBumpTex       = glGetUniformLocation(program, "bumpTex");
132       if(state.specularMapping)
133          uSpecularTex   = glGetUniformLocation(program, "specularTex");
134       if(state.environmentMapping)
135       {
136          uEnvTex = glGetUniformLocation(program, "envTex");
137          uCubeMapMatrix = glGetUniformLocation(program, "cubemap_matrix");
138
139          if(state.refraction)
140             uRefractionETA = glGetUniformLocation(program, "matRefractionETA");
141          if(state.reflection)
142             uMatReflectivity = glGetUniformLocation(program, "matReflectivity");
143          if(state.reflectionMap)
144             uReflectTex  = glGetUniformLocation(program, "reflectTex");
145       }
146
147       if(state.fog)
148       {
149          uFogDensity       = glGetUniformLocation(program, "fogDensity");
150          uFogColor         = glGetUniformLocation(program, "fogColor");
151       }
152 #endif
153    }
154 };
155
156 public class DefaultShader : Shader
157 {
158    vertexShaderFile   = "<:ecere>shaders/default.vert";
159    fragmentShaderFile = "<:ecere>shaders/default.frag";
160
161 public:
162    float modelView[16];
163    float projection[16];
164    float matTexture[16];
165    float cubemap_matrix[9];
166
167    float normalsMatrix[9];
168    float nearPlane;
169    float globalAmbient[3];
170
171    // Lights
172    float lDiffuse [8][4];
173    float lAmbient [8][3];
174    float lSpecular[8][3];
175    float lPosition[8][3];
176    float lAtt[8][3];
177    float lCutOffCos[8];
178    float lSpotDir[8][3];
179    float lSpotExp[8];
180
181    // Material
182    float diffuse[4];
183    float ambient[3];
184    float specular[3];
185    float emissive[3];
186    float power;
187    float refractionETA, reflectivity;
188
189    bool fogOn;
190    uint lightBits;
191    float fogDensity;
192    float fogColor[3];
193
194    float color[4];
195
196    DefaultShader()
197    {
198       color[0] = 1, color[1] = 1, color[2] = 1, color[3] = 1;
199       diffuse[0] = 1, diffuse[1] = 1, diffuse[2] = 1, diffuse[3] = 1;
200       ambient[0] = 1, ambient[1] = 1, ambient[2] = 1;
201
202       // state = DefaultShaderBits { blinnSpecular = true };
203    }
204
205    void bindAttribs(int program)
206    {
207 #if ENABLE_GL_SHADERS
208       DefaultShaderBits state = this.state;
209       glBindAttribLocation(program, GLBufferContents::vertex,    "vertex");
210       if(state.perVertexColor)
211          glBindAttribLocation(program, GLBufferContents::color,     "color");
212       if(state.texturing || state.normalsMapping || state.specularMapping || state.reflectionMap)
213          glBindAttribLocation(program, GLBufferContents::texCoord,  "texCoord");
214       if(state.lighting)
215          glBindAttribLocation(program, GLBufferContents::normal,    "normal");
216       if(state.normalsMapping)
217       {
218          glBindAttribLocation(program, GLBufferContents::tangent1,  "tangent1");
219          glBindAttribLocation(program, GLBufferContents::tangent2,  "tangent2");
220       }
221 #endif
222    }
223
224    CompiledDefaultShader registerShader(int program, uint64 state)
225    {
226       CompiledDefaultShader shader { };
227       shader.registerUniforms(program, state);
228       return shader;
229    }
230
231    ZString getDefinitions(uint64 _state)
232    {
233       DefaultShaderBits state = _state;
234       ZString defs { allocType = heap, minSize = 5000 };
235       int i;
236
237 #ifdef _DEBUG
238        //PrintLn("Compiling shader for state: ", state);
239 #endif
240
241 #if defined(__EMSCRIPTEN__)
242       defs.concatf("#version 100\n");
243       defs.concatf("#define GLSL_FLOAT_PRECISION   1\n");
244 #else
245       defs.concatf("#version 110\n");
246       defs.concatf("#define GLSL_FLOAT_PRECISION   0\n");
247 #endif
248       defs.concatf("\n#define NUM_LIGHTS %d",               8);
249       defs.concatf("\n#define MODELVIEW %d",                state.modelView          ? 1 : 0);
250       defs.concatf("\n#define PER_VERTEX_COLOR %d",         state.perVertexColor     ? 1 : 0);
251       defs.concatf("\n#define LIGHTING_ON %d",              state.lighting           ? 1 : 0);
252       defs.concatf("\n#define NON_LOCAL_VIEWER %d",         state.nonLocalViewer     ? 1 : 0);
253       defs.concatf("\n#define MAT_TWOSIDED %d",             state.twoSided           ? 1 : 0);
254       defs.concatf("\n#define MAT_SPECULAR %d",             state.specular           ? 1 : 0);
255       defs.concatf("\n#define MAT_SEPARATE_SPECULAR %d",    state.separateSpecular   ? 1 : 0);
256       defs.concatf("\n#define SPECULAR_MAPPING %d",         state.specularMapping    ? 1 : 0);
257       defs.concatf("\n#define ENVIRONMENT_MAPPING %d",      state.environmentMapping ? 1 : 0);
258       defs.concatf("\n#define ENVIRONMENT_REFLECTION %d",   state.reflection         ? 1 : 0);
259       defs.concatf("\n#define ENVIRONMENT_REFRACTION %d",   state.refraction         ? 1 : 0);
260       defs.concatf("\n#define REFLECTION_MAP %d",           state.reflectionMap      ? 1 : 0);
261       defs.concatf("\n#define NORMALS_MAPPING %d",          state.normalsMapping     ? 1 : 0);
262       defs.concatf("\n#define CUBEMAP_ON %d",               state.cubeMap            ? 1 : 0);
263       defs.concatf("\n#define LIGHTING_SPECULAR_BLINN %d",  state.blinnSpecular      ? 1 : 0);
264       defs.concatf("\n#define TEXTURE_ON %d",               state.texturing          ? 1 : 0);
265       defs.concatf("\n#define TEXTURE_MATRIX %d",           state.textureMatrix      ? 1 : 0);
266       defs.concatf("\n#define SWIZZLE_ALPHA %d",            state.swizzle == alpha   ? 1 : 0);
267       defs.concatf("\n#define SWIZZLE_RED %d",              state.swizzle == red     ? 1 : 0);
268       defs.concatf("\n#define FOG_ON %d\n",                 state.fog                ? 1 : 0);
269       defs.concatf("\n#define DEBUGGING %d\n",              state.debugging          ? 1 : 0);
270       for(i = 0; i < 8; i++)
271       {
272          LightMode mode = (LightMode)((state.lightBits & (0x7 << (3*i))) >> (3*i));
273          defs.concatf("\n#define LIGHT%d_ON %d",         i, mode                                 ? 1 : 0);
274          defs.concatf("\n#define LIGHT%d_POSITIONAL %d", i, mode >= pos                          ? 1 : 0);
275          defs.concatf("\n#define LIGHT%d_SPOT %d",       i, mode >= posSpot                      ? 1 : 0);
276          defs.concatf("\n#define LIGHT%d_ATT %d\n",      i, mode == posAtt || mode == posSpotAtt ? 1 : 0);
277       }
278       defs.concatf("\n\n#line 0\n");
279
280 #ifdef _DEBUG
281       //puts((String)defs._string);
282 #endif
283       return defs;
284    }
285
286    void uploadUniforms(CompiledDefaultShader shader)
287    {
288 #if ENABLE_GL_SHADERS
289       DefaultShaderBits state = this.state;
290
291       // Upload Uniforms
292
293 /*
294       if(prjViewModified)
295       {
296          Matrix * mv = &mvMatrix;
297          Matrix * prj = &prjMatrix;
298          Matrix mat;
299          mat.Multiply(mv, prj);
300          {
301             float m[16] =
302             {
303                (float)mat.m[0][0], (float)mat.m[0][1], (float)mat.m[0][2], (float)mat.m[0][3],
304                (float)mat.m[1][0], (float)mat.m[1][1], (float)mat.m[1][2], (float)mat.m[1][3],
305                (float)mat.m[2][0], (float)mat.m[2][1], (float)mat.m[2][2], (float)mat.m[2][3],
306                (float)mat.m[3][0], (float)mat.m[3][1], (float)mat.m[3][2], (float)mat.m[3][3]
307             };
308             float mvz[4] = { (float)mv->m[0][2], (float)mv->m[1][2], (float)mv->m[2][2], (float)mv->m[3][2] };
309             glUniformMatrix4fv(shader.uPrjMatrix, 1, GL_FALSE, m);
310             if(mvModified)
311                glUniform3fv(uMVZ, 1, mvz);
312          }
313       }
314 */
315
316       glUniformMatrix4fv(shader.uPrjMatrix, 1, GL_FALSE, projection);
317
318       if(state.modelView)
319          glUniformMatrix4fv(shader.uMVMatrix, 1, GL_FALSE, modelView);
320
321       if(state.lighting)
322       {
323          // Lights
324          int i;
325          for(i = 0; i < 8; i++)
326          {
327             LightMode mode = (LightMode)((state.lightBits & (0x7 << (3*i))) >> (3*i));
328             if(mode)
329             {
330                if(mode == posSpot || mode == posSpotAtt)
331                {
332                   glUniform3fv(shader.uLightsSpotDir[i], 1, lSpotDir[i]);
333                   glUniform1f(shader.uLightsSpotCutOffCos[i], lCutOffCos[i]);
334                   glUniform1f(shader.uLightsSpotExp[i], lSpotExp[i]);
335                }
336                if(mode == posAtt || mode == posSpotAtt)
337                   glUniform3fv(shader.uLightsAtt[i], 1, lAtt[i]);
338                if(state.specular)
339                   glUniform3fv(shader.uLightsSpecular[i], 1, lSpecular[i]);
340                glUniform3fv(shader.uLightsPos[i], 1, lPosition[i]);
341                glUniform3fv(shader.uLightsDiffuse[i], 1, lDiffuse[i]);
342                glUniform3fv(shader.uLightsAmbient[i], 1, lAmbient[i]);
343             }
344          }
345
346          // Material
347          glUniform4fv(shader.uMatDiffuse,  1, diffuse);
348          glUniform3fv(shader.uMatAmbient,  1, ambient);
349          glUniform3fv(shader.uMatEmissive, 1, emissive);
350          if(state.specular)
351          {
352             glUniform3fv(shader.uMatSpecular, 1, specular);
353             glUniform1f(shader.uMatPower, state.blinnSpecular ? power : power / 4.0f);
354          }
355
356          glUniform1f(shader.uNearPlane, nearPlane);
357          glUniform3fv(shader.uGlobalAmbient, 1, globalAmbient);
358
359          glUniformMatrix3fv(shader.uNormalsMatrix, 1, GL_FALSE, normalsMatrix);
360
361          if(state.normalsMapping)
362             glUniform1i(shader.uBumpTex, 1);
363
364          if(state.specularMapping)
365             glUniform1i(shader.uSpecularTex, 2);
366       }
367       else
368          glUniform4fv(shader.uMatDiffuse, 1, color);
369
370       if(state.environmentMapping)
371       {
372          glUniform1i(shader.uEnvTex, 3);
373          glUniformMatrix3fv(shader.uCubeMapMatrix, 1, GL_FALSE, cubemap_matrix);
374          if(state.reflection)
375             glUniform1f(shader.uMatReflectivity, reflectivity);
376          if(state.refraction)
377             glUniform1f(shader.uRefractionETA, refractionETA);
378          if(state.reflectionMap)
379             glUniform1i(shader.uReflectTex, 4);
380       }
381
382       if(state.texturing || state.cubeMap)
383          glUniform1i(shader.uDiffuseTex, 0);
384       if((state.texturing || state.normalsMapping || state.specularMapping || state.reflectionMap || state.cubeMap) && state.textureMatrix)
385          glUniformMatrix4fv(shader.uTextureMatrix, 1, GL_FALSE, matTexture);
386
387       if(state.fog)
388       {
389          glUniform1f(shader.uFogDensity, fogDensity);
390          glUniform3fv(shader.uFogColor, 1, fogColor);
391       }
392 #endif
393    }
394
395    void setCamera(Camera camera)
396    {
397       double * c = camera.inverseTranspose.array;
398       float m[9] =
399       {
400          (float) c[0], (float) c[1], (float)-c[2],
401          (float)-c[4], (float) c[5], (float) c[6],
402          (float)-c[8], (float) c[9], (float) c[10]
403       };
404       memcpy(cubemap_matrix, m, 9 * sizeof(float));
405       uniformsModified = true;
406    }
407
408    void updateMatrix(MatrixMode mode, Matrix matrix, bool isIdentity)
409    {
410       float m[16] =
411       {
412          (float)matrix.m[0][0], (float)matrix.m[0][1], (float)matrix.m[0][2], (float)matrix.m[0][3],
413          (float)matrix.m[1][0], (float)matrix.m[1][1], (float)matrix.m[1][2], (float)matrix.m[1][3],
414          (float)matrix.m[2][0], (float)matrix.m[2][1], (float)matrix.m[2][2], (float)matrix.m[2][3],
415          (float)matrix.m[3][0], (float)matrix.m[3][1], (float)matrix.m[3][2], (float)matrix.m[3][3]
416       };
417       if(mode == projection)
418       {
419          memcpy(projection, m, 16 * sizeof(float));
420          nearPlane = (float)::nearPlane;
421          uniformsModified = true;
422       }
423       else if(mode == modelView)
424       {
425          Matrix t, inv;
426          double * i = inv.array;
427
428          memcpy(modelView, m, 16 * sizeof(float));
429          ((DefaultShaderBits)state).modelView = !isIdentity;
430
431          //mvModified = true;
432          //prjViewModified = true;
433
434          inv = matrix;
435          inv.Scale(1, -1, 1);
436          t.Transpose(inv);
437          inv.Inverse(t);
438          {
439             float m[9] =
440             {
441                (float)i[0],(float)i[1],(float)i[2],
442                (float)i[4],(float)i[5],(float)i[6],
443                (float)i[8],(float)i[9],(float)i[10]
444             };
445             memcpy(normalsMatrix, m, 9 * sizeof(float));
446          }
447
448          uniformsModified = true;
449       }
450       else if(mode == texture)
451       {
452          memcpy(matTexture, m, 16 * sizeof(float));
453          ((DefaultShaderBits)state).textureMatrix = !isIdentity;
454          if(((DefaultShaderBits)state).texturing || ((DefaultShaderBits)state).normalsMapping || ((DefaultShaderBits)state).specularMapping ||
455             ((DefaultShaderBits)state).reflectionMap || ((DefaultShaderBits)state).cubeMap)
456             uniformsModified = true;
457       }
458    }
459
460    void setGlobalAmbient(float r, float g, float b, float a)
461    {
462       globalAmbient[0] = r, globalAmbient[1] = g, globalAmbient[2] = b;
463       if(((DefaultShaderBits)state).lighting)
464          uniformsModified = true;
465    }
466
467    void setColor(float r, float g, float b, float a)
468    {
469       color[0] = r, color[1] = g, color[2] = b, color[3] = a;
470       uniformsModified = true;
471    }
472
473    void lighting(bool on)
474    {
475       if(((DefaultShaderBits)state).lighting != on)
476       {
477          ((DefaultShaderBits)state).lighting = on;
478          uniformsModified = true;
479          if(!on)
480             state &= ~DefaultShaderBits
481             {
482                nonLocalViewer = true;
483                twoSided = true;
484                specular = true;
485                separateSpecular = true;
486                blinnSpecular = true;
487                lightBits = 0xFFFFFF;
488             };
489          else
490             state |= DefaultShaderBits { separateSpecular = true };
491       }
492    }
493
494    void fog(bool on)
495    {
496       if(fogOn != on)
497       {
498          fogOn = on;
499          ((DefaultShaderBits)state).fog = fogOn && fogDensity;
500          uniformsModified = true;
501       }
502    }
503
504    void setFogDensity(float density)
505    {
506       if(fogDensity != density)
507       {
508          fogDensity = density;
509          ((DefaultShaderBits)state).fog = fogOn && fogDensity;
510          if(fogOn)
511             uniformsModified = true;
512       }
513    }
514
515    void setFogColor(float r, float g, float b)
516    {
517       fogColor[0] = r, fogColor[1] = g, fogColor[2] = b;
518       if(fogOn)
519          uniformsModified = true;
520    }
521
522    void texturing(bool on)
523    {
524       if(((DefaultShaderBits)state).texturing != on)
525       {
526          ((DefaultShaderBits)state).texturing = on;
527          if(!on)
528             state &= ~DefaultShaderBits { swizzle = (SwizzleMode)0x3 };
529          if(!on && !((DefaultShaderBits)state).normalsMapping && !((DefaultShaderBits)state).specularMapping && !((DefaultShaderBits)state).reflectionMap && !((DefaultShaderBits)state).cubeMap)
530             state &= ~DefaultShaderBits { textureMatrix = true };
531          uniformsModified = true;
532       }
533    }
534
535    void debugging(bool on)
536    {
537       if(((DefaultShaderBits)state).debugging != on)
538       {
539          ((DefaultShaderBits)state).debugging = on;
540          uniformsModified = true;
541       }
542    }
543
544    void swizzle(SwizzleMode swizzle)
545    {
546 #ifdef _DEBUG
547       if(swizzle && !((DefaultShaderBits)state).texturing && !((DefaultShaderBits)state).cubeMap)
548          printf("swizzle() with texturing off\n");
549 #endif
550       if(((DefaultShaderBits)state).swizzle != swizzle)
551       {
552          ((DefaultShaderBits)state).swizzle = swizzle;
553          if(((DefaultShaderBits)state).texturing || ((DefaultShaderBits)state).cubeMap)
554             uniformsModified = true;
555       }
556    }
557
558    void setSimpleMaterial(ColorAlpha color, bool twoSided)
559    {
560       float r = color.color.r / 255.0f;
561       float g = color.color.g / 255.0f;
562       float b = color.color.b / 255.0f;
563 #ifdef _DEBUG
564       if(!((DefaultShaderBits)state).lighting)
565          printf("setSimpleMaterial() with lighting off\n");
566 #endif
567       diffuse[0] = r, diffuse[1] = g, diffuse[2] = b, diffuse[3] = color.a / 255.0f;
568       ambient[0] = r, ambient[1] = g, ambient[2] = b;
569       emissive[0] = 0, emissive[1] = 0, emissive[2] = 0;
570       ((DefaultShaderBits)state).perVertexColor = false;
571       ((DefaultShaderBits)state).normalsMapping = false;
572       ((DefaultShaderBits)state).environmentMapping = false;
573       ((DefaultShaderBits)state).reflectionMap = false;
574       ((DefaultShaderBits)state).specularMapping = false;
575       ((DefaultShaderBits)state).reflection = false;
576       ((DefaultShaderBits)state).refraction = false;
577       ((DefaultShaderBits)state).texturing = false;
578       ((DefaultShaderBits)state).cubeMap = false;
579       ((DefaultShaderBits)state).specular = false;
580       ((DefaultShaderBits)state).twoSided = twoSided;
581       uniformsModified = true;
582    }
583
584    void setPerVertexColor(bool perVertexColor)
585    {
586       if(((DefaultShaderBits)state).perVertexColor != perVertexColor)
587       {
588          ((DefaultShaderBits)state).perVertexColor = perVertexColor;
589          uniformsModified = true;
590       }
591    }
592
593    #if !defined(ECERE_NO3D)
594    void setMaterial(Material material, MeshFeatures flags)
595    {
596 #if ENABLE_GL_SHADERS
597       if(material)
598       {
599          ((DefaultShaderBits)state).specular = material.power && (material.specular.r || material.specular.g || material.specular.b);
600          ((DefaultShaderBits)state).perVertexColor = flags.colors;
601          ((DefaultShaderBits)state).separateSpecular = ((DefaultShaderBits)state).specular && material.flags.separateSpecular;
602          ((DefaultShaderBits)state).cubeMap = material && material.flags.cubeMap && material.baseMap;
603          ((DefaultShaderBits)state).twoSided = material.flags.doubleSided && !material.flags.singleSideLight;
604          ((DefaultShaderBits)state).lightBits = material.flags.noLighting ? 0 : lightBits;
605          ((DefaultShaderBits)state).lighting = (!material.flags.noLighting && lightBits) ? true : false;
606          color[0] = 1, color[1] = 1, color[2] = 1, color[3] = material.opacity;
607          diffuse[0]  = material.diffuse.r,  diffuse[1]  = material.diffuse.g,  diffuse[2]  = material.diffuse.b, diffuse[3] = material.opacity;
608          ambient[0]  = material.ambient.r,  ambient[1]  = material.ambient.g,  ambient[2]  = material.ambient.b;
609          specular[0] = material.specular.r, specular[1] = material.specular.g, specular[2] = material.specular.b;
610          emissive[0] = material.emissive.r, emissive[1] = material.emissive.g, emissive[2] = material.emissive.b;
611          power = material.power;
612       }
613       else
614       {
615          ((DefaultShaderBits)state).cubeMap = false;
616          ((DefaultShaderBits)state).perVertexColor = false;
617          ((DefaultShaderBits)state).separateSpecular = false;
618          ((DefaultShaderBits)state).twoSided = false;
619          ((DefaultShaderBits)state).textureMatrix = false;
620       }
621
622       if(material && material.bumpMap && flags.tangents)
623       {
624          glActiveTexture(GL_TEXTURE1);
625          glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)material.bumpMap.driverData);
626          if(material.flags.tile)
627          {
628             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
629             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
630          }
631          else
632          {
633             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
634             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
635          }
636
637          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, material.bumpMap.mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
638          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
639          glActiveTexture(GL_TEXTURE0);
640          ((DefaultShaderBits)state).normalsMapping = true;
641       }
642       else
643          ((DefaultShaderBits)state).normalsMapping = false;
644
645       if(material && material.envMap && (material.refractiveIndex || material.refractiveIndexContainer || material.reflectivity || material.reflectMap))
646       {
647          CubeMap cube = material.envMap;
648          glActiveTexture(GL_TEXTURE3);
649          glBindTexture(GL_TEXTURE_CUBE_MAP, (GLuint)(uintptr)cube.driverData);
650          glActiveTexture(GL_TEXTURE0);
651          ((DefaultShaderBits)state).environmentMapping = true;
652
653          if(material.refractiveIndex || material.refractiveIndexContainer)
654          {
655             refractionETA =
656                (material.refractiveIndexContainer ? material.refractiveIndexContainer : 1.0) /
657                (material.refractiveIndex ? material.refractiveIndex : 1.0);
658
659             ((DefaultShaderBits)state).refraction = true;
660          }
661
662          if(material.reflectivity || material.reflectMap)
663          {
664             reflectivity = material.reflectivity;
665             if(!reflectivity && material.reflectMap) reflectivity = 1.0;
666
667             if(material.reflectMap)
668             {
669                glActiveTexture(GL_TEXTURE4);
670                glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)material.reflectMap.driverData);
671                if(material.flags.tile)
672                {
673                   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
674                   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
675                }
676                else
677                {
678                   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
679                   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
680                }
681
682                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, material.reflectMap.mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
683                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
684                glActiveTexture(GL_TEXTURE0);
685
686                ((DefaultShaderBits)state).reflectionMap = true;
687             }
688             else
689                ((DefaultShaderBits)state).reflectionMap = false;
690             ((DefaultShaderBits)state).reflection = true;
691          }
692       }
693       else
694       {
695          ((DefaultShaderBits)state).environmentMapping = false;
696          ((DefaultShaderBits)state).refraction = false;
697          ((DefaultShaderBits)state).reflection = false;
698          ((DefaultShaderBits)state).reflectionMap = false;
699       }
700
701       if(material && material.specularMap)
702       {
703          glActiveTexture(GL_TEXTURE2);
704          glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)material.specularMap.driverData);
705          if(material.flags.tile)
706          {
707             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
708             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
709          }
710          else
711          {
712             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
713             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
714          }
715
716          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, material.specularMap.mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
717          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
718          glActiveTexture(GL_TEXTURE0);
719          ((DefaultShaderBits)state).specularMapping = true;
720       }
721       else
722          ((DefaultShaderBits)state).specularMapping = false;
723
724       uniformsModified = true;
725 #endif
726    }
727
728    void setLight(Display display, uint id, Light light)
729    {
730       int lShift = 3*id;
731       bool lightOn = light && !light.flags.off;
732       LightMode mode = (LightMode)((((DefaultShaderBits)state).lightBits & (0x7 << lShift)) >> lShift);
733
734       if(lightOn && !((DefaultShaderBits)state).lighting)
735          ((DefaultShaderBits)state).lighting = true;
736
737       if(lightOn || (lightOn != (mode != off)))
738          uniformsModified = true;
739
740       if(lightOn)
741       {
742          Object lightObject = light.lightObject;
743          float multiplier = light.multiplier;
744          Matrix m = matrixStack[0][matrixIndex[0]];
745          Vector3D l;
746          m.Scale(::nearPlane, ::nearPlane, ::nearPlane);
747
748          if(!multiplier) multiplier = 1.0f;
749
750          lDiffuse[id][0] = light.diffuse.r * multiplier;
751          lDiffuse[id][1] = light.diffuse.g * multiplier;
752          lDiffuse[id][2] = light.diffuse.b * multiplier;
753
754          lAmbient[id][0] = light.ambient.r * multiplier;
755          lAmbient[id][1] = light.ambient.g * multiplier;
756          lAmbient[id][2] = light.ambient.b * multiplier;
757
758          lSpecular[id][0] = light.specular.r * multiplier;
759          lSpecular[id][1] = light.specular.g * multiplier;
760          lSpecular[id][2] = light.specular.b * multiplier;
761
762          if(lightObject)
763          {
764             // Positional Lights (Including Spot Lights and Omni with flags.spot not set)
765             Matrix * mat = &lightObject.matrix;
766             Vector3D positionVector { mat->m[3][0], mat->m[3][1], mat->m[3][2] };
767             if(display.display3D.camera)
768                positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
769
770             l.MultMatrix(positionVector, m);
771             lPosition[id][0] = (float)l.x, lPosition[id][1] = (float)-l.y, lPosition[id][2] = (float)l.z;
772
773             // Display Light Position
774             /*
775             GLSetupLighting(false);
776             glDisable(GL_DEPTH_TEST);
777             GLColor3f(
778                light.diffuse.r * multiplier,
779                light.diffuse.g * multiplier,
780                light.diffuse.b * multiplier);
781             glPointSize(10);
782             GLBegin(GL_POINTS);
783             GLVertex3dv((double *)&positionVector);
784             GLEnd();
785             glEnable(GL_DEPTH_TEST);
786             GLSetupLighting(true);
787             */
788
789             // Display Target
790             /*
791             if(light.target)
792             {
793                Vector3D positionVector;
794                if(lightObject.flags.root || !lightObject.parent)
795                {
796                   positionVector = light.target.transform.position;
797                   positionVector.Subtract(positionVector, display.camera.cPosition);
798                }
799                else
800                {
801                   positionVector.MultMatrix(light.target.transform.position,
802                      lightObject.light.target.parent.matrix);
803                   positionVector.Subtract(positionVector, display.camera.cPosition);
804                }
805                GLSetupLighting(false);
806                glDisable(GL_DEPTH_TEST);
807                GLColor3f(1,1,0);
808                glPointSize(10);
809                GLBegin(GL_POINTS);
810                GLVertex3dv((double *)&positionVector);
811                GLEnd();
812                glEnable(GL_DEPTH_TEST);
813                GLSetupLighting(false);
814             }
815             */
816
817             if(light.flags.attenuation)
818             {
819                lAtt[id][0] = light.Kc;
820                lAtt[id][1] = light.Kl;
821                lAtt[id][2] = light.Kq;
822             }
823
824             if((light.flags.spot && light.fallOff < 360) || (lightObject && (light.direction.x || light.direction.y || light.direction.z)))
825             {
826                #define MAXLIGHT  0.9
827                // Figure out exponent out of the hot spot
828                float exponent = light.flags.spot ? (float)(log(MAXLIGHT) / log(cos(light.hotSpot / 2))) : 1;
829                Degrees cutOff = light.flags.spot ? light.fallOff/2 : 90;
830                Vector3D l, n;
831                l.MultMatrix(light.direction, m);
832                n.Normalize(l);
833                n.Scale(n, -1);
834
835                lSpotDir[id][0] = (float)n.x, lSpotDir[id][1] = (float)-n.y, lSpotDir[id][2] = (float)n.z;
836                lCutOffCos[id] = (float)cos(Degrees { cutOff });
837                lSpotExp[id] = exponent;
838
839                mode = light.flags.attenuation ? posSpotAtt : posSpot;
840             }
841             else
842                mode = light.flags.attenuation ? posAtt : pos;
843          }
844          else
845          {
846             // Directional Light
847             Vector3D vector { 0,0,-1 };
848             Vector3D direction, v;
849             Matrix mat;
850             mat.RotationQuaternion(light.orientation);
851             direction.MultMatrix(vector, mat);
852             v.Normalize(direction);
853             l.MultMatrix(v, m);
854
855             lPosition[id][0] = (float)l.x, lPosition[id][1] = (float)-l.y, lPosition[id][2] = (float)l.z;
856             mode = dir;
857          }
858       }
859       else
860          mode = off;
861       lightBits = (lightBits & ~(0x7 << lShift)) | (mode << lShift);
862       ((DefaultShaderBits)state).lightBits = lightBits;
863    }
864    #endif
865
866    DefaultShader ::shader() { return defaultShader; }
867 }
868
869 DefaultShader defaultShader { };