/
MStandardRenderer.cpp
2425 lines (1963 loc) · 67 KB
/
MStandardRenderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Maratis
// MStandardRenderer.cpp
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//========================================================================
// Copyright (c) 2003-2014 Anael Seghezzi <www.maratis3d.com>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
#include <tinyutf8.h>
#include <MEngine.h>
#include <MLog.h>
#include "MStandardShaders.h"
#include "MStandardRenderer.h"
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// LIGHT SPACE PERSPECTIVE SHADOW MAPS
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// This programn is a demonstration of the Light Space Perspective Shadow Mapping
// algorithm described in the respective paper. Additional information and sample
// images are available at
//
// http://www.cg.tuwien.ac.at/research/vr/lispsm/
//
// Copyright and Disclaimer:
//
// This code is copyright Vienna University of Technology, 2004.
//
//
// Please feel FREE to COPY and USE the code to include it in your own work,
// provided you include this copyright notice.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// Authors:
//
// Daniel Scherzer (scherzer@cg.tuwien.ac.at)
// Michael Wimmer (wimmer@cg.tuwien.ac.at)
//
// Date: October 14, 2004
// 2014: adapted for Maratis
static void createOrthoView(MMatrix4x4 * matrix, float left, float right, float bottom, float top, float zNear, float zFar)
{
if(right == left || top == bottom || zFar == zNear)
return;
float tx = - (right + left)/(right - left);
float ty = - (top + bottom)/(top - bottom);
float tz = - (zFar + zNear)/(zFar - zNear);
matrix->entries[0] = 2.0f/(right-left);
matrix->entries[1] = 0.0f;
matrix->entries[2] = 0.0f;
matrix->entries[3] = 0.0f;
matrix->entries[4] = 0.0f;
matrix->entries[5] = 2.0f/(top-bottom);
matrix->entries[6] = 0.0f;
matrix->entries[7] = 0.0f;
matrix->entries[8] = 0.0f;
matrix->entries[9] = 0.0f;
matrix->entries[10] = -2.0f/(zFar-zNear);
matrix->entries[11] = 0.0f;
matrix->entries[12] = tx;
matrix->entries[13] = ty;
matrix->entries[14] = tz;
matrix->entries[15] = 1.0f;
}
static void scaleTranslateToFit(MMatrix4x4 * output, const float * vMin, const float * vMax)
{
//createOrthoView(output, vMin[0], vMax[0], vMin[1], vMax[1], -vMax[2], -vMin[2]);
output->entries[ 0] = 2/(vMax[0]-vMin[0]);
output->entries[ 4] = 0;
output->entries[ 8] = 0;
output->entries[12] = -(vMax[0]+vMin[0])/(vMax[0]-vMin[0]);
output->entries[ 1] = 0;
output->entries[ 5] = 2/(vMax[1]-vMin[1]);
output->entries[ 9] = 0;
output->entries[13] = -(vMax[1]+vMin[1])/(vMax[1]-vMin[1]);
output->entries[ 2] = 0;
output->entries[ 6] = 0;
output->entries[10] = 2/(vMax[2]-vMin[2]);
output->entries[14] = -(vMax[2]+vMin[2])/(vMax[2]-vMin[2]);
output->entries[ 3] = 0;
output->entries[ 7] = 0;
output->entries[11] = 0;
output->entries[15] = 1;
}
static void linCombVector3(float * result, const float * pos, const float * dir, const double t)
{
for(int i=0; i<3; i++)
result[i] = pos[i]+t*dir[i];
}
static MVector3 calcNewDir(const MVector3 * B_points, const int B_size, const MVector3 & eyePos)
{
MVector3 dir;
for(int i=0; i<B_size; i++)
{
MVector3 p = B_points[i] - eyePos;
dir+=p;
}
return dir.getNormalized();
}
static MVector3 calcUpVec(const MVector3 & viewDir, const MVector3 & lightDir)
{
MVector3 left = lightDir.crossProduct(viewDir);
return (left.crossProduct(lightDir)).getNormalized();
}
static void appendToCubicHull(float * min, float * max, const float * v)
{
int j;
for(j=0; j<3; j++)
{
if(v[j] < min[j])
{
min[j] = v[j];
}
else if(v[j] > max[j])
{
max[j] = v[j];
}
}
}
static void calcCubicHull(MVector3 * min, MVector3 * max, const MVector3 * ps, const int size)
{
if(size > 0)
{
*min = *max = ps[0];
for(int i=1; i<size; i++)
appendToCubicHull(*min, *max, ps[i]);
}
}
void mulHomogenPoint(float * output, float * m, const float * v)
{
//if v == output -> overwriting problems -> so store in temp
double x = m[0]*v[0] + m[4]*v[1] + m[ 8]*v[2] + m[12];
double y = m[1]*v[0] + m[5]*v[1] + m[ 9]*v[2] + m[13];
double z = m[2]*v[0] + m[6]*v[1] + m[10]*v[2] + m[14];
double w = m[3]*v[0] + m[7]*v[1] + m[11]*v[2] + m[15];
output[0] = x/w;
output[1] = y/w;
output[2] = z/w;
}
static void calcLispSMMtx(const MVector3 * B_points, const int B_size,
const MVector3 & eyePos, const MVector3 & viewDir, const MVector3 & lightDir, float nearDist,
MMatrix4x4 * lightView, MMatrix4x4 * lightProj)
{
MVector3 * B_copy = new MVector3[B_size];
double dotProd = viewDir.dotProduct(lightDir);
double sinGamma = sqrt(1.0 - dotProd*dotProd);
// uniform test
/*if(dotProd > 0.75f)
{
lightView->lookAt(eyePos, lightDir, viewDir);
for(int i=0; i<B_size; i++)
B_copy[i] = (*lightView) * B_points[i];
MVector3 min, max;
calcCubicHull(&min, &max, B_copy, B_size);
scaleTranslateToFit(lightProj, min, max);
//transform from right handed into left handed coordinate system
MMatrix4x4 rh2lf(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1);
(*lightProj) = rh2lf * (*lightProj);
delete[] B_copy;
return;
}*/
//MVector3 up = calcUpVec(viewDir, lightDir);
MVector3 up = calcUpVec(calcNewDir(B_points, B_size, eyePos), lightDir);
//temporal light View
//look from position(eyePos)
//into direction(lightDir)
//with up vector(up)
lightView->lookAt(eyePos, lightDir, up);
//transform the light volume points from world into light space
for(int i=0; i<B_size; i++)
mulHomogenPoint(B_copy[i], lightView->entries, B_points[i]);
//calculate the cubic hull (an AABB)
//of the light space extents of the intersection body B
//and save the two extreme points min and max
MVector3 min, max;
calcCubicHull(&min, &max, B_copy, B_size);
MMatrix4x4 lispMtx;
{
//use the formulas of the paper to get n (and f)
double factor = 1.0/sinGamma;
double z_n = factor*nearDist; //often 1
double d = ABS(max.y-min.y); //perspective transform depth //light space y extents
double z_f = z_n + d*sinGamma;
double n = (z_n+sqrt(z_f*z_n))/sinGamma;
double f = n+d;
MVector3 pos;
//new observer point n-1 behind eye position
//pos = eyePos-up*(n-nearDist)
linCombVector3(pos,eyePos,up*(1+MAX(0, dotProd)*nearDist*4),-(n-nearDist)); // hack
lightView->lookAt(pos,lightDir,up);
//one possibility for a simple perspective transformation matrix
//with the two parameters n(near) and f(far) in y direction
// a = (f+n)/(f-n); b = -2*f*n/(f-n);
lispMtx.entries[ 5] = (f+n)/(f-n); // [ 1 0 0 0]
lispMtx.entries[13] = -2*f*n/(f-n); // [ 0 a 0 b]
lispMtx.entries[ 7] = 1; // [ 0 0 1 0]
lispMtx.entries[15] = 0; // [ 0 1 0 0]
//temporal arrangement for the transformation of the points to post-perspective space
(*lightProj) = lispMtx * (*lightView);
//transform the light volume points from world into the distorted light space
for(int i=0; i<B_size; i++)
mulHomogenPoint(B_copy[i], lightProj->entries, B_points[i]);
//calculate the cubic hull (an AABB)
//of the light space extents of the intersection body B
//and save the two extreme points min and max
calcCubicHull(&min, &max, B_copy, B_size);
}
//refit to unit cube
//this operation calculates a scale translate matrix that
//maps the two extreme points min and max into (-1,-1,-1) and (1,1,1)
scaleTranslateToFit(lightProj, min, max);
//together
(*lightProj) = (*lightProj) * lispMtx; // ligthProjection = scaleTranslate*lispMtx
//transform from right handed into left handed coordinate system
MMatrix4x4 rh2lf(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1);
(*lightProj) = rh2lf * (*lightProj);
delete[] B_copy;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Init
/////////////////////////////////////////////////////////////////////////////////////////////////////////
MStandardRenderer::MStandardRenderer(void):
m_forceNoFX(false),
m_fboId(0),
m_currentCamera(NULL),
m_FXsNumber(0)
{
MRenderingContext * render = MEngine::getInstance()->getRenderingContext();
// default FXs
addFX(vertShader0.c_str(), fragShader0.c_str());
addFX(vertShader1.c_str(), fragShader1.c_str());
addFX(vertShader2.c_str(), fragShader2.c_str());
addFX(vertShader3.c_str(), fragShader3.c_str());
addFX(vertShader4.c_str(), fragShader4.c_str());
addFX(vertShader5.c_str(), fragShader5.c_str());
addFX(vertShader6.c_str(), fragShader6.c_str());
addFX(vertShader7.c_str(), fragShader7.c_str());
addFX(vertShader8.c_str(), fragShader8.c_str());
// rand texture
MImage image;
image.create(M_UBYTE, 64, 64, 4);
unsigned char * pixel = (unsigned char *)image.getData();
for(unsigned int i=0; i<image.getSize(); i++)
{
(*pixel) = (unsigned char)(rand()%256);
pixel++;
}
render->createTexture(&m_randTexture);
render->bindTexture(m_randTexture);
render->setTextureFilterMode(M_TEX_FILTER_LINEAR_MIPMAP_LINEAR, M_TEX_FILTER_LINEAR);
render->setTextureUWrapMode(M_WRAP_REPEAT);
render->setTextureVWrapMode(M_WRAP_REPEAT);
render->sendTextureImage(&image, 1, 1, 0);
}
MStandardRenderer::~MStandardRenderer(void)
{
unsigned int i;
MRenderingContext * render = MEngine::getInstance()->getRenderingContext();
// delete default FXs
for(i=0; i<m_FXsNumber; i++)
{
render->deleteFX(&m_FXs[i]);
render->deleteShader(&m_fragShaders[i]);
render->deleteShader(&m_vertShaders[i]);
}
// delete shadowLights
map<unsigned long, MShadowLight>::iterator
mit (m_shadowLights.begin()),
mend(m_shadowLights.end());
for(;mit!=mend;++mit)
{
render->deleteTexture(&mit->second.shadowTexture);
}
// delete occlusion queries
/*for(i=0; i<MAX_TRANSP; i++)
render->deleteQuery(&m_transpList[i].occlusionQuery);
for(i=0; i<MAX_OPAQUE; i++)
render->deleteQuery(&m_opaqueList[i].occlusionQuery);
*/
// delete rand texture
render->deleteTexture(&m_randTexture);
// delete FBO
render->deleteFrameBuffer(&m_fboId);
}
void MStandardRenderer::destroy(void)
{
delete this;
}
MRenderer * MStandardRenderer::getNew(void)
{
return new MStandardRenderer();
}
void MStandardRenderer::addFX(const char * vert, const char * frag)
{
if(m_FXsNumber < MAX_DEFAULT_FXS)
{
MRenderingContext * render = MEngine::getInstance()->getRenderingContext();
render->createVertexShader(&m_vertShaders[m_FXsNumber]);
render->sendShaderSource(m_vertShaders[m_FXsNumber], vert);
render->createPixelShader(&m_fragShaders[m_FXsNumber]);
render->sendShaderSource(m_fragShaders[m_FXsNumber], frag);
render->createFX(&m_FXs[m_FXsNumber], m_vertShaders[m_FXsNumber], m_fragShaders[m_FXsNumber]);
m_FXsNumber++;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Drawing
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static bool hasSubMeshTransparency(MOEntity * entity, MSubMesh * subMesh)
{
unsigned int i, displayNumber = subMesh->getDisplaysNumber();
for(i=0; i<displayNumber; i++)
{
MDisplay * display = subMesh->getDisplay(i);
MMaterial * material = entity->getMaterial(display->getMaterialId());
if((! display->isVisible()) || (! material))
continue;
if(material->getBlendMode() != M_BLENDING_NONE)
return true;
}
return false;
}
void MStandardRenderer::initVBO(MSubMesh * subMesh, MSubMeshCache * subMeshCahe, MVector3 * vertices, MVector3 * normals, MVector3 * tangents)
{
MEngine * engine = MEngine::getInstance();
MRenderingContext * render = engine->getRenderingContext();
unsigned int * vboId1 = subMesh->getVBOid1();
unsigned int * vboId2 = subMesh->getVBOid2();
// indices
M_TYPES indicesType = subMesh->getIndicesType();
void * indices = subMesh->getIndices();
M_VBO_MODES mode = M_VBO_STATIC;
if(subMeshCahe)
{
mode = M_VBO_DYNAMIC;
vboId1 = subMeshCahe->getVBOid();
}
if(*vboId1 == 0 || mode != M_VBO_STATIC)
{
// data
MColor * colors = subMesh->getColors();
MVector2 * texCoords = subMesh->getTexCoords();
unsigned int totalSize = sizeof(MVector3)*subMesh->getVerticesSize();
if(normals)
totalSize += sizeof(MVector3)*subMesh->getNormalsSize();
if(tangents)
totalSize += sizeof(MVector3)*subMesh->getTangentsSize();
if(texCoords)
totalSize += sizeof(MVector2)*subMesh->getTexCoordsSize();
if(colors)
totalSize += sizeof(MColor)*subMesh->getColorsSize();
// data VBO
bool init = (*vboId1 == 0);
if(init)
render->createVBO(vboId1);
render->bindVBO(M_VBO_ARRAY, *vboId1);
if(init)
render->setVBO(M_VBO_ARRAY, 0, totalSize, mode);
unsigned int offset = 0;
render->setVBOSubData(M_VBO_ARRAY, offset, vertices, sizeof(MVector3)*subMesh->getVerticesSize());
offset += sizeof(MVector3)*subMesh->getVerticesSize();
if(normals)
{
render->setVBOSubData(M_VBO_ARRAY, offset, normals, sizeof(MVector3)*subMesh->getNormalsSize());
offset += sizeof(MVector3)*subMesh->getNormalsSize();
}
if(tangents)
{
render->setVBOSubData(M_VBO_ARRAY, offset, tangents, sizeof(MVector3)*subMesh->getTangentsSize());
offset += sizeof(MVector3)*subMesh->getTangentsSize();
}
if(*vboId1 == 0)
{
if(texCoords)
{
render->setVBOSubData(M_VBO_ARRAY, offset, texCoords, sizeof(MVector2)*subMesh->getTexCoordsSize());
offset += sizeof(MVector2)*subMesh->getTexCoordsSize();
}
if(colors)
{
render->setVBOSubData(M_VBO_ARRAY, offset, colors, sizeof(MColor)*subMesh->getColorsSize());
offset += sizeof(MColor)*subMesh->getColorsSize();
}
}
render->bindVBO(M_VBO_ARRAY, 0);
}
// indices VBO
if(*vboId2 == 0 && indices)
{
unsigned int typeSize = indicesType == M_USHORT ? sizeof(short) : sizeof(int);
render->createVBO(vboId2);
render->bindVBO(M_VBO_ELEMENT_ARRAY, *vboId2);
render->setVBO(M_VBO_ELEMENT_ARRAY, indices, subMesh->getIndicesSize()*typeSize, mode);
render->bindVBO(M_VBO_ELEMENT_ARRAY, 0);
}
}
void MStandardRenderer::drawDisplay(MOEntity * entity, MSubMeshCache * subMeshCahe, MSubMesh * subMesh, MDisplay * display, MVector3 * vertices, MVector3 * normals, MVector3 * tangents, MColor * colors)
{
MEngine * engine = MEngine::getInstance();
MRenderingContext * render = engine->getRenderingContext();
// VBO
unsigned int * vboId1 = subMesh->getVBOid1();
if(subMeshCahe)
vboId1 = subMeshCahe->getVBOid();
unsigned int * vboId2 = subMesh->getVBOid2();
// get material
MMaterial * material = entity->getMaterial(display->getMaterialId());
{
float opacity = material->getOpacity();
if(opacity <= 0.0f)
return;
// data
M_TYPES indicesType = subMesh->getIndicesType();
void * indices = subMesh->getIndices();
MVector2 * texCoords = subMesh->getTexCoords();
// begin / size
unsigned int begin = display->getBegin();
unsigned int size = display->getSize();
// get properties
M_PRIMITIVE_TYPES primitiveType = display->getPrimitiveType();
M_BLENDING_MODES blendMode = material->getBlendMode();
M_CULL_MODES cullMode = display->getCullMode();
MVector3 diffuse = material->getDiffuse();
MVector3 specular = material->getSpecular();
MVector3 emit = material->getEmit();
float shininess = material->getShininess();
// get current fog color
MVector3 currentFogColor;
render->getFogColor(¤tFogColor);
// set cull mode
if(cullMode == M_CULL_NONE)
{
render->disableCullFace();
}
else{
render->enableCullFace();
render->setCullMode(cullMode);
}
// set blending mode
render->setBlendingMode(blendMode);
// set fog color depending on blending
switch(blendMode)
{
case M_BLENDING_ADD:
case M_BLENDING_LIGHT:
render->setFogColor(MVector3(0, 0, 0));
break;
case M_BLENDING_PRODUCT:
render->setFogColor(MVector3(1, 1, 1));
break;
default:
break;
}
// texture passes
unsigned int texturesPassNumber = MIN(8, material->getTexturesPassNumber());
// FX
unsigned int fxId = 0;
MFXRef * FXRef = material->getFXRef();
MFXRef * ZFXRef = material->getZFXRef();
if(FXRef)
fxId = FXRef->getFXId();
bool basicFX = false;
// force NoFX
if(m_forceNoFX)
{
// optimize only for standard shader (for custom shader we don't know how geometry and alpha test is done)
if(fxId == 0)
{
fxId = m_FXs[0]; // basic FX
texturesPassNumber = 0;
// alpha test
if(material->getTexturesPassNumber() > 0)
{
MTexture * texture = material->getTexturePass(0)->getTexture();
if(texture)
{
if(texture->getTextureRef())
{
if(texture->getTextureRef()->getComponents() > 3)
{
fxId = m_FXs[7]; // basic FX with texture
texturesPassNumber = 1;
}
}
}
}
basicFX = true;
}
else if(ZFXRef) // if custom shader, use the Z FX is any
{
fxId = ZFXRef->getFXId();
}
}
// standard shader
else if(fxId == 0)
{
if(material->getTexturesPassNumber() == 0) // simple
fxId = m_FXs[1];
else if(material->getTexturesPassNumber() == 1) // diffuse
fxId = m_FXs[2];
else if(material->getTexturesPassNumber() == 2) // diffuse+specular
fxId = m_FXs[3];
else if(material->getTexturesPassNumber() == 3) // diffuse+specular+normal
fxId = m_FXs[4];
else{
if(material->getTexturePass(2)->getTexture()) // diffuse+specular+emit+normal
fxId = m_FXs[6];
else
fxId = m_FXs[5]; // diffuse+specular+emit
}
}
// FX pipeline
{
unsigned int attribList[64];
unsigned int attribListNb = 0;
int attribIndex;
MMatrix4x4 * cameraViewMatrix = m_currentCamera->getCurrentViewMatrix();
MMatrix4x4 * cameraProjMatrix = m_currentCamera->getCurrentProjMatrix();
// properties
int AlphaTest;
MVector3 FogColor;
float FogEnd;
float FogScale;
MVector4 LightPosition[4];
MVector3 LightDiffuseProduct[4];
MVector3 LightSpecularProduct[4];
MVector3 LightSpotDirection[4];
float LightConstantAttenuation[4];
float LightQuadraticAttenuation[4];
float LightSpotCosCutoff[4];
float LightSpotExponent[4];
int LightActive[4];
int ShadowMaps[4];
int Texture[8] = {0, 1, 2, 3, 4, 5, 6, 7};
MMatrix4x4 TextureMatrix[8];
MMatrix4x4 ProjModelViewMatrix;
MMatrix4x4 NormalMatrix;
// Alpha test
AlphaTest = (blendMode != M_BLENDING_ALPHA);
// Matrix
ProjModelViewMatrix = (*cameraProjMatrix) * m_currModelViewMatrix;
if(! basicFX)
{
// Fog
float min, max;
render->getFogColor(&FogColor);
render->getFogDistance(&min, &max);
FogEnd = max;
FogScale = 1.0f / (max - min);
// Lights
for(int i=0; i<4; i++)
{
float spotAngle;
float linearAttenuation;
MVector4 lightDiffuse;
render->getLightDiffuse(i, &lightDiffuse);
render->getLightPosition(i, &LightPosition[i]);
render->getLightSpotDirection(i, &LightSpotDirection[i]);
render->getLightAttenuation(i, &LightConstantAttenuation[i], &linearAttenuation, &LightQuadraticAttenuation[i]);
render->getLightSpotAngle(i, &spotAngle);
render->getLightSpotExponent(i, &LightSpotExponent[i]);
LightActive[i] = (lightDiffuse.w > 0.0f);
LightSpotCosCutoff[i] = cosf(spotAngle*DEG_TO_RAD);
LightDiffuseProduct[i] = (diffuse) * MVector3(lightDiffuse);
LightSpecularProduct[i] = (specular) * MVector3(lightDiffuse);
LightPosition[i] = (*cameraViewMatrix) * MVector4(LightPosition[i]);
LightSpotDirection[i] = (cameraViewMatrix->getRotatedVector3(LightSpotDirection[i])).getNormalized();
}
// Normal Matrix
NormalMatrix = m_currModelViewMatrix.getInversetranspose();
}
// bind FX
render->bindFX(fxId);
// bind VBO is any
if(*vboId1 > 0)
render->bindVBO(M_VBO_ARRAY, *vboId1);
// Vertex
render->getAttribLocation(fxId, "Vertex", &attribIndex);
if(attribIndex != -1)
{
if(*vboId1 > 0) render->setAttribPointer(attribIndex, M_FLOAT, 3, 0);
else render->setAttribPointer(attribIndex, M_FLOAT, 3, vertices);
render->enableAttribArray(attribIndex);
attribList[attribListNb] = attribIndex; attribListNb++;
}
if(! basicFX)
{
unsigned long offset = sizeof(MVector3)*subMesh->getVerticesSize();
// Normal
if(normals)
{
render->getAttribLocation(fxId, "Normal", &attribIndex);
if(attribIndex != -1)
{
if(*vboId1 > 0) render->setAttribPointer(attribIndex, M_FLOAT, 3, (void*)offset);
else render->setAttribPointer(attribIndex, M_FLOAT, 3, normals);
render->enableAttribArray(attribIndex);
attribList[attribListNb] = attribIndex; attribListNb++;
}
offset += sizeof(MVector3)*subMesh->getNormalsSize();
}
// Tangent
if(tangents)
{
render->getAttribLocation(fxId, "Tangent", &attribIndex);
if(attribIndex != -1)
{
if(*vboId1 > 0) render->setAttribPointer(attribIndex, M_FLOAT, 3, (void*)offset);
else render->setAttribPointer(attribIndex, M_FLOAT, 3, tangents);
render->enableAttribArray(attribIndex);
attribList[attribListNb] = attribIndex; attribListNb++;
}
offset += sizeof(MVector3)*subMesh->getTangentsSize();
}
// Texcoords
if(texCoords)
{
offset += sizeof(MVector2)*subMesh->getTexCoordsSize();
}
// Color
if(colors)
{
render->getAttribLocation(fxId, "Color", &attribIndex);
if(attribIndex != -1)
{
if(*vboId1 > 0) render->setAttribPointer(attribIndex, M_UBYTE, 3, (void*)offset, true);
else render->setAttribPointer(attribIndex, M_UBYTE, 3, colors, true);
render->enableAttribArray(attribIndex);
attribList[attribListNb] = attribIndex; attribListNb++;
}
}
}
// Textures
unsigned int textureArrayOffset = sizeof(MVector3)*subMesh->getVerticesSize();
{
if(normals) textureArrayOffset += sizeof(MVector3)*subMesh->getNormalsSize();
if(tangents) textureArrayOffset += sizeof(MVector3)*subMesh->getTangentsSize();
}
int id = texturesPassNumber;
for(unsigned int t=0; t<texturesPassNumber; t++)
{
MTexturePass * texturePass = material->getTexturePass(t);
MTexture * texture = texturePass->getTexture();
if((! texture) || (! texCoords))
{
render->bindTexture(0, t);
continue;
}
// texCoords
unsigned int offset = 0;
if(subMesh->isMapChannelExist(texturePass->getMapChannel()))
offset = subMesh->getMapChannelOffset(texturePass->getMapChannel());
// texture id
unsigned int textureId = 0;
MTextureRef * texRef = texture->getTextureRef();
if(texRef)
textureId = texRef->getTextureId();
// bind texture
render->bindTexture(textureId, t);
render->setTextureUWrapMode(texture->getUWrapMode());
render->setTextureVWrapMode(texture->getVWrapMode());
// texture matrix
MMatrix4x4 * texMatrix = &TextureMatrix[t];
texMatrix->loadIdentity();
texMatrix->translate(MVector2(0.5f, 0.5f));
texMatrix->scale(texture->getTexScale());
texMatrix->rotate(MVector3(0, 0, -1), texture->getTexRotate());
texMatrix->translate(MVector2(-0.5f, -0.5f));
texMatrix->translate(texture->getTexTranslate());
// texture coords
char name[16];
sprintf(name, "TexCoord%d", t);
render->getAttribLocation(fxId, name, &attribIndex);
if(attribIndex != -1)
{
if(*vboId1 > 0) render->setAttribPointer(attribIndex, M_FLOAT, 2, (void*)(textureArrayOffset + sizeof(MVector2)*offset));
else render->setAttribPointer(attribIndex, M_FLOAT, 2, texCoords + offset);
render->enableAttribArray(attribIndex);
attribList[attribListNb] = attribIndex; attribListNb++;
}
}
if(! basicFX)
{
// Shadows
for(int i=0; i<4; i++)
{
if(m_lightShadow[i] == 1)
{
render->bindTexture(m_lightShadowTexture[i], id);
ShadowMaps[i] = id;
id++;
}
else {
render->bindTexture(0, id);
ShadowMaps[i] = id;
id++;
}
}
// rand texture
int randTextureId = id;
{
render->bindTexture(m_randTexture, id);
id++;
}
// uniforms
render->sendUniformVec4(fxId, "FogColor", MVector4(FogColor));
render->sendUniformFloat(fxId, "FogEnd", &FogEnd);
render->sendUniformFloat(fxId, "FogScale", &FogScale);
render->sendUniformVec3(fxId, "MaterialEmit", emit);
render->sendUniformFloat(fxId, "MaterialShininess", &shininess);
render->sendUniformVec4(fxId, "LightPosition", LightPosition[0], 4);
render->sendUniformVec3(fxId, "LightDiffuseProduct", LightDiffuseProduct[0], 4);
render->sendUniformVec3(fxId, "LightSpecularProduct", LightSpecularProduct[0], 4);
render->sendUniformVec3(fxId, "LightSpotDirection", LightSpotDirection[0], 4);
render->sendUniformFloat(fxId, "LightConstantAttenuation", LightConstantAttenuation, 4);
render->sendUniformFloat(fxId, "LightQuadraticAttenuation", LightQuadraticAttenuation, 4);
render->sendUniformFloat(fxId, "LightSpotCosCutoff", LightSpotCosCutoff, 4);
render->sendUniformFloat(fxId, "LightSpotExponent", LightSpotExponent, 4);
render->sendUniformInt(fxId, "LightActive", LightActive, 4);
render->sendUniformInt(fxId, "LightShadowMap", ShadowMaps, 4);
render->sendUniformInt(fxId, "LightShadow", m_lightShadow, 4);
render->sendUniformFloat(fxId, "LightShadowBias", m_lightShadowBias, 4);
render->sendUniformFloat(fxId, "LightShadowBlur", m_lightShadowBlur, 4);
render->sendUniformMatrix(fxId, "LightShadowMatrix", m_lightShadowMatrix, 4);
render->sendUniformInt(fxId, "RandTexture", &randTextureId);
render->sendUniformMatrix(fxId, "ModelViewMatrix", &m_currModelViewMatrix);
render->sendUniformMatrix(fxId, "NormalMatrix", &NormalMatrix);
render->sendUniformMatrix(fxId, "ProjectionMatrix", cameraProjMatrix);
}
if(texturesPassNumber > 0)
{
render->sendUniformInt(fxId, "AlphaTest", &AlphaTest);
render->sendUniformInt(fxId, "Texture", Texture, texturesPassNumber);
render->sendUniformMatrix(fxId, "TextureMatrix", TextureMatrix, texturesPassNumber);
}
render->sendUniformFloat(fxId, "MaterialOpacity", &opacity);
render->sendUniformMatrix(fxId, "ProjModelViewMatrix", &ProjModelViewMatrix);
// draw
if(indices)
{
if(*vboId2 > 0)
{
render->bindVBO(M_VBO_ELEMENT_ARRAY, *vboId2);
switch(indicesType)
{
case M_USHORT:
render->drawElement(primitiveType, size, indicesType, (void*)(begin*sizeof(short)));
break;
case M_UINT:
render->drawElement(primitiveType, size, indicesType, (void*)(begin*sizeof(int)));
break;
default:
break;
}
}
else
{
switch(indicesType)
{
case M_USHORT:
render->drawElement(primitiveType, size, indicesType, (unsigned short*)indices + begin);
break;
case M_UINT:
render->drawElement(primitiveType, size, indicesType, (unsigned int*)indices + begin);
break;
default:
break;
}
}
}
else{
render->drawArray(primitiveType, begin, size);
}
// disable attribs
for(int i=0; i<attribListNb; i++)
render->disableAttribArray(attribList[i]);
// restore textures
for(int t=(int)(id-1); t>=0; t--)
{
render->bindTexture(0, t);
render->disableTexture();
}
// restore FX
render->bindFX(0);
// restore VBO
render->bindVBO(M_VBO_ARRAY, 0);
render->bindVBO(M_VBO_ELEMENT_ARRAY, 0);
}
// restore fog and alpha test
render->setFogColor(currentFogColor);
}
}
void MStandardRenderer::drawOpaques(MOEntity * entity, MSubMeshCache * subMeshCahe, MSubMesh * subMesh, MArmature * armature)
{
// data
MVector3 * vertices = subMesh->getVertices();
MVector3 * normals = subMesh->getNormals();
MVector3 * tangents = subMesh->getTangents();
MColor * colors = subMesh->getColors();
if(! vertices)
return;
if(subMeshCahe)
{
vertices = subMeshCahe->getVertices();