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
|
#include "common.h"
#ifdef RW_OPENGL
#include "main.h"
#include "RwHelper.h"
#include "Lights.h"
#include "Timecycle.h"
#include "FileMgr.h"
#include "Clock.h"
#include "Weather.h"
#include "TxdStore.h"
#include "Renderer.h"
#include "World.h"
#include "custompipes.h"
#ifdef EXTENDED_PIPELINES
#ifndef LIBRW
#error "Need librw for EXTENDED_PIPELINES"
#endif
namespace CustomPipes {
static int32 u_viewVec;
static int32 u_rampStart;
static int32 u_rampEnd;
static int32 u_rimData;
static int32 u_lightMap;
static int32 u_eye;
static int32 u_reflProps;
static int32 u_specDir;
static int32 u_specColor;
#define U(i) currentShader->uniformLocations[i]
/*
* Neo Vehicle pipe
*/
rw::gl3::Shader *neoVehicleShader;
static void
uploadSpecLights(void)
{
using namespace rw::gl3;
rw::RGBAf colors[1 + NUMEXTRADIRECTIONALS];
struct {
rw::V3d dir;
float power;
} dirs[1 + NUMEXTRADIRECTIONALS];
memset(colors, 0, sizeof(colors));
memset(dirs, 0, sizeof(dirs));
for(int i = 0; i < 1+NUMEXTRADIRECTIONALS; i++)
dirs[i].power = 1.0f;
float power = Power.Get();
Color speccol = SpecColor.Get();
colors[0].red = speccol.r;
colors[0].green = speccol.g;
colors[0].blue = speccol.b;
dirs[0].dir = pDirect->getFrame()->getLTM()->at;
dirs[0].power = power;
for(int i = 0; i < NUMEXTRADIRECTIONALS; i++){
if(pExtraDirectionals[i]->getFlags() & rw::Light::LIGHTATOMICS){
colors[1+i] = pExtraDirectionals[i]->color;
dirs[1+i].dir = pExtraDirectionals[i]->getFrame()->getLTM()->at;
dirs[1+i].power = power*2.0f;
}
}
glUniform4fv(U(u_specDir), 1 + NUMEXTRADIRECTIONALS, (float*)&dirs);
glUniform4fv(U(u_specColor), 1 + NUMEXTRADIRECTIONALS, (float*)&colors);
}
static void
vehicleRenderCB(rw::Atomic *atomic, rw::gl3::InstanceDataHeader *header)
{
using namespace rw;
using namespace rw::gl3;
// TODO: make this less of a kludge
if(VehiclePipeSwitch == VEHICLEPIPE_MATFX){
matFXGlobals.pipelines[rw::platform]->render(atomic);
return;
}
Material *m;
rw::uint32 flags = atomic->geometry->flags;
setWorldMatrix(atomic->getFrame()->getLTM());
lightingCB(atomic);
setupVertexInput(header);
InstanceData *inst = header->inst;
rw::int32 n = header->numMeshes;
neoVehicleShader->use();
V3d eyePos = rw::engine->currentCamera->getFrame()->getLTM()->pos;
glUniform3fv(U(u_eye), 1, (float*)&eyePos);
uploadSpecLights();
float reflProps[4];
reflProps[0] = Fresnel.Get();
reflProps[1] = SpecColor.Get().a;
setTexture(1, EnvMapTex);
SetRenderState(SRCBLEND, BLENDONE);
while(n--){
m = inst->material;
setMaterial(flags, m->color, m->surfaceProps);
setTexture(0, m->texture);
rw::SetRenderState(VERTEXALPHA, inst->vertexAlpha || m->color.alpha != 0xFF);
reflProps[2] = m->surfaceProps.specular * VehicleShininess;
reflProps[3] = m->surfaceProps.specular == 0.0f ? 0.0f : VehicleSpecularity;
glUniform4fv(U(u_reflProps), 1, reflProps);
drawInst(header, inst);
inst++;
}
SetRenderState(SRCBLEND, BLENDSRCALPHA);
setTexture(1, nil);
teardownVertexInput(header);
}
void
CreateVehiclePipe(void)
{
using namespace rw;
using namespace rw::gl3;
if(CFileMgr::LoadFile("neo/carTweakingTable.dat", work_buff, sizeof(work_buff), "r") <= 0)
printf("Error: couldn't open 'neo/carTweakingTable.dat'\n");
else{
char *fp = (char*)work_buff;
fp = ReadTweakValueTable(fp, Fresnel);
fp = ReadTweakValueTable(fp, Power);
fp = ReadTweakValueTable(fp, DiffColor);
fp = ReadTweakValueTable(fp, SpecColor);
}
{
#include "shaders/obj/neoVehicle_frag.inc"
#include "shaders/obj/neoVehicle_vert.inc"
const char *vs[] = { shaderDecl, header_vert_src, neoVehicle_vert_src, nil };
const char *fs[] = { shaderDecl, header_frag_src, neoVehicle_frag_src, nil };
neoVehicleShader = Shader::create(vs, fs);
assert(neoVehicleShader);
}
rw::gl3::ObjPipeline *pipe = rw::gl3::ObjPipeline::create();
pipe->instanceCB = rw::gl3::defaultInstanceCB;
pipe->uninstanceCB = nil;
pipe->renderCB = vehicleRenderCB;
vehiclePipe = pipe;
}
void
DestroyVehiclePipe(void)
{
neoVehicleShader->destroy();
neoVehicleShader = nil;
((rw::gl3::ObjPipeline*)vehiclePipe)->destroy();
vehiclePipe = nil;
}
/*
* Neo World pipe
*/
rw::gl3::Shader *neoWorldShader;
static void
worldRenderCB(rw::Atomic *atomic, rw::gl3::InstanceDataHeader *header)
{
using namespace rw;
using namespace rw::gl3;
if(!LightmapEnable){
gl3::defaultRenderCB(atomic, header);
return;
}
Material *m;
setWorldMatrix(atomic->getFrame()->getLTM());
lightingCB(atomic);
setupVertexInput(header);
InstanceData *inst = header->inst;
rw::int32 n = header->numMeshes;
neoWorldShader->use();
float lightfactor[4];
while(n--){
m = inst->material;
if(MatFX::getEffects(m) == MatFX::DUAL){
MatFX *matfx = MatFX::get(m);
Texture *dualtex = matfx->getDualTexture();
if(dualtex == nil)
goto notex;
setTexture(1, dualtex);
lightfactor[0] = lightfactor[1] = lightfactor[2] = WorldLightmapBlend.Get()*LightmapMult;
}else{
notex:
setTexture(1, nil);
lightfactor[0] = lightfactor[1] = lightfactor[2] = 0.0f;
}
lightfactor[3] = m->color.alpha/255.0f;
glUniform4fv(U(u_lightMap), 1, lightfactor);
RGBA color = { 255, 255, 255, m->color.alpha };
setMaterial(color, m->surfaceProps);
setTexture(0, m->texture);
rw::SetRenderState(VERTEXALPHA, inst->vertexAlpha || m->color.alpha != 0xFF);
drawInst(header, inst);
inst++;
}
setTexture(1, nil);
teardownVertexInput(header);
}
void
CreateWorldPipe(void)
{
using namespace rw;
using namespace rw::gl3;
if(CFileMgr::LoadFile("neo/worldTweakingTable.dat", work_buff, sizeof(work_buff), "r") <= 0)
printf("Error: couldn't open 'neo/worldTweakingTable.dat'\n");
else
ReadTweakValueTable((char*)work_buff, WorldLightmapBlend);
{
#include "shaders/obj/neoWorldIII_frag.inc"
#include "shaders/obj/default_UV2_vert.inc"
const char *vs[] = { shaderDecl, header_vert_src, default_UV2_vert_src, nil };
const char *fs[] = { shaderDecl, header_frag_src, neoWorldIII_frag_src, nil };
neoWorldShader = Shader::create(vs, fs);
assert(neoWorldShader);
}
rw::gl3::ObjPipeline *pipe = rw::gl3::ObjPipeline::create();
pipe->instanceCB = rw::gl3::defaultInstanceCB;
pipe->uninstanceCB = nil;
pipe->renderCB = worldRenderCB;
worldPipe = pipe;
}
void
DestroyWorldPipe(void)
{
neoWorldShader->destroy();
neoWorldShader = nil;
((rw::gl3::ObjPipeline*)worldPipe)->destroy();
worldPipe = nil;
}
/*
* Neo Gloss pipe
*/
rw::gl3::Shader *neoGlossShader;
static void
glossRenderCB(rw::Atomic *atomic, rw::gl3::InstanceDataHeader *header)
{
using namespace rw;
using namespace rw::gl3;
worldRenderCB(atomic, header);
if(!GlossEnable)
return;
Material *m;
setupVertexInput(header);
InstanceData *inst = header->inst;
rw::int32 n = header->numMeshes;
neoGlossShader->use();
V3d eyePos = rw::engine->currentCamera->getFrame()->getLTM()->pos;
glUniform3fv(U(u_eye), 1, (float*)&eyePos);
float reflProps[4];
reflProps[0] = GlossMult;
reflProps[1] = 0.0f;
reflProps[2] = 0.0f;
reflProps[3] = 0.0f;
glUniform4fv(U(u_reflProps), 1, reflProps);
SetRenderState(VERTEXALPHA, TRUE);
SetRenderState(SRCBLEND, BLENDONE);
SetRenderState(DESTBLEND, BLENDONE);
SetRenderState(ZWRITEENABLE, FALSE);
SetRenderState(ALPHATESTFUNC, ALPHAALWAYS);
while(n--){
m = inst->material;
RGBA color = { 255, 255, 255, m->color.alpha };
setMaterial(color, m->surfaceProps);
if(m->texture){
Texture *tex = GetGlossTex(m);
if(tex){
setTexture(0, tex);
drawInst(header, inst);
}
}
inst++;
}
SetRenderState(ZWRITEENABLE, TRUE);
SetRenderState(ALPHATESTFUNC, ALPHAGREATEREQUAL);
SetRenderState(SRCBLEND, BLENDSRCALPHA);
SetRenderState(DESTBLEND, BLENDINVSRCALPHA);
teardownVertexInput(header);
}
void
CreateGlossPipe(void)
{
using namespace rw;
using namespace rw::gl3;
{
#include "shaders/obj/neoGloss_frag.inc"
#include "shaders/obj/neoGloss_vert.inc"
const char *vs[] = { shaderDecl, header_vert_src, neoGloss_vert_src, nil };
const char *fs[] = { shaderDecl, header_frag_src, neoGloss_frag_src, nil };
neoGlossShader = Shader::create(vs, fs);
assert(neoGlossShader);
}
rw::gl3::ObjPipeline *pipe = rw::gl3::ObjPipeline::create();
pipe->instanceCB = rw::gl3::defaultInstanceCB;
pipe->uninstanceCB = nil;
pipe->renderCB = glossRenderCB;
glossPipe = pipe;
}
void
DestroyGlossPipe(void)
{
neoGlossShader->destroy();
neoGlossShader = nil;
((rw::gl3::ObjPipeline*)glossPipe)->destroy();
glossPipe = nil;
}
/*
* Neo Rim pipes
*/
rw::gl3::Shader *neoRimShader;
rw::gl3::Shader *neoRimSkinShader;
static void
uploadRimData(bool enable)
{
using namespace rw;
using namespace rw::gl3;
V3d viewVec = rw::engine->currentCamera->getFrame()->getLTM()->at;
glUniform3fv(U(u_viewVec), 1, (float*)&viewVec);
float rimData[4];
rimData[0] = Offset.Get();
rimData[1] = Scale.Get();
if(enable)
rimData[2] = Scaling.Get()*RimlightMult;
else
rimData[2] = 0.0f;
rimData[3] = 0.0f;
glUniform3fv(U(u_rimData), 1, rimData);
Color col = RampStart.Get();
glUniform4fv(U(u_rampStart), 1, (float*)&col);
col = RampEnd.Get();
glUniform4fv(U(u_rampEnd), 1, (float*)&col);
}
static void
rimSkinRenderCB(rw::Atomic *atomic, rw::gl3::InstanceDataHeader *header)
{
using namespace rw;
using namespace rw::gl3;
if(!RimlightEnable){
gl3::skinRenderCB(atomic, header);
return;
}
Material *m;
rw::uint32 flags = atomic->geometry->flags;
setWorldMatrix(atomic->getFrame()->getLTM());
lightingCB(atomic);
setupVertexInput(header);
InstanceData *inst = header->inst;
rw::int32 n = header->numMeshes;
neoRimSkinShader->use();
uploadRimData(atomic->geometry->flags & Geometry::LIGHT);
uploadSkinMatrices(atomic);
while(n--){
m = inst->material;
setMaterial(flags, m->color, m->surfaceProps);
setTexture(0, m->texture);
rw::SetRenderState(VERTEXALPHA, inst->vertexAlpha || m->color.alpha != 0xFF);
drawInst(header, inst);
inst++;
}
teardownVertexInput(header);
}
static void
rimRenderCB(rw::Atomic *atomic, rw::gl3::InstanceDataHeader *header)
{
using namespace rw;
using namespace rw::gl3;
if(!RimlightEnable){
gl3::defaultRenderCB(atomic, header);
return;
}
Material *m;
rw::uint32 flags = atomic->geometry->flags;
setWorldMatrix(atomic->getFrame()->getLTM());
lightingCB(atomic);
setupVertexInput(header);
InstanceData *inst = header->inst;
rw::int32 n = header->numMeshes;
neoRimShader->use();
uploadRimData(atomic->geometry->flags & Geometry::LIGHT);
while(n--){
m = inst->material;
setMaterial(flags, m->color, m->surfaceProps);
setTexture(0, m->texture);
rw::SetRenderState(VERTEXALPHA, inst->vertexAlpha || m->color.alpha != 0xFF);
drawInst(header, inst);
inst++;
}
teardownVertexInput(header);
}
void
CreateRimLightPipes(void)
{
using namespace rw::gl3;
if(CFileMgr::LoadFile("neo/rimTweakingTable.dat", work_buff, sizeof(work_buff), "r") <= 0)
printf("Error: couldn't open 'neo/rimTweakingTable.dat'\n");
else{
char *fp = (char*)work_buff;
fp = ReadTweakValueTable(fp, RampStart);
fp = ReadTweakValueTable(fp, RampEnd);
fp = ReadTweakValueTable(fp, Offset);
fp = ReadTweakValueTable(fp, Scale);
fp = ReadTweakValueTable(fp, Scaling);
}
{
#include "shaders/obj/simple_frag.inc"
#include "shaders/obj/neoRimSkin_vert.inc"
const char *vs[] = { shaderDecl, header_vert_src, neoRimSkin_vert_src, nil };
const char *fs[] = { shaderDecl, header_frag_src, simple_frag_src, nil };
neoRimSkinShader = Shader::create(vs, fs);
assert(neoRimSkinShader);
}
{
#include "shaders/obj/simple_frag.inc"
#include "shaders/obj/neoRim_vert.inc"
const char *vs[] = { shaderDecl, header_vert_src, neoRim_vert_src, nil };
const char *fs[] = { shaderDecl, header_frag_src, simple_frag_src, nil };
neoRimShader = Shader::create(vs, fs);
assert(neoRimShader);
}
rw::gl3::ObjPipeline *pipe = rw::gl3::ObjPipeline::create();
pipe->instanceCB = rw::gl3::defaultInstanceCB;
pipe->uninstanceCB = nil;
pipe->renderCB = rimRenderCB;
rimPipe = pipe;
pipe = rw::gl3::ObjPipeline::create();
pipe->instanceCB = rw::gl3::skinInstanceCB;
pipe->uninstanceCB = nil;
pipe->renderCB = rimSkinRenderCB;
rimSkinPipe = pipe;
}
void
DestroyRimLightPipes(void)
{
neoRimShader->destroy();
neoRimShader = nil;
neoRimSkinShader->destroy();
neoRimSkinShader = nil;
((rw::gl3::ObjPipeline*)rimPipe)->destroy();
rimPipe = nil;
((rw::gl3::ObjPipeline*)rimSkinPipe)->destroy();
rimSkinPipe = nil;
}
void
CustomPipeRegisterGL(void)
{
u_viewVec = rw::gl3::registerUniform("u_viewVec");
u_rampStart = rw::gl3::registerUniform("u_rampStart");
u_rampEnd = rw::gl3::registerUniform("u_rampEnd");
u_rimData = rw::gl3::registerUniform("u_rimData");
u_lightMap = rw::gl3::registerUniform("u_lightMap");
u_eye = rw::gl3::registerUniform("u_eye");
u_reflProps = rw::gl3::registerUniform("u_reflProps");
u_specDir = rw::gl3::registerUniform("u_specDir");
u_specColor = rw::gl3::registerUniform("u_specColor");
}
}
#endif
#ifdef NEW_RENDERER
#ifndef LIBRW
#error "Need librw for NEW_PIPELINES"
#endif
namespace WorldRender
{
struct BuildingInst
{
rw::Matrix matrix;
rw::gl3::InstanceDataHeader *instHeader;
uint32 cullMode;
uint8 fadeAlpha;
bool lighting;
};
BuildingInst blendInsts[3][2000];
int numBlendInsts[3];
static RwRGBAReal black;
static bool
IsTextureTransparent(RwTexture *tex)
{
if(tex == nil || tex->raster == nil)
return false;
return PLUGINOFFSET(rw::gl3::Gl3Raster, tex->raster, rw::gl3::nativeRasterOffset)->hasAlpha;
}
// Render all opaque meshes and put atomics that needs blending
// into the deferred list.
void
AtomicFirstPass(RpAtomic *atomic, int pass)
{
using namespace rw;
using namespace rw::gl3;
BuildingInst *building = &blendInsts[pass][numBlendInsts[pass]];
atomic->getPipeline()->instance(atomic);
building->instHeader = (gl3::InstanceDataHeader*)atomic->geometry->instData;
assert(building->instHeader != nil);
assert(building->instHeader->platform == PLATFORM_GL3);
building->fadeAlpha = 255;
building->lighting = !!(atomic->geometry->flags & rw::Geometry::LIGHT);
building->cullMode = rw::GetRenderState(rw::CULLMODE);
rw::uint32 flags = atomic->geometry->flags;
WorldLights lights;
lights.numAmbients = 1;
lights.numDirectionals = 0;
lights.numLocals = 0;
if(building->lighting)
lights.ambient = pAmbient->color;
else
lights.ambient = black;
bool setupDone = false;
bool defer = false;
building->matrix = *atomic->getFrame()->getLTM();
InstanceData *inst = building->instHeader->inst;
for(rw::uint32 i = 0; i < building->instHeader->numMeshes; i++, inst++){
Material *m = inst->material;
if(inst->vertexAlpha || m->color.alpha != 255 ||
IsTextureTransparent(m->texture)){
defer = true;
continue;
}
// alright we're rendering this atomic
if(!setupDone){
rw::SetRenderState(rw::CULLMODE, building->cullMode);
defaultShader->use();
setWorldMatrix(&building->matrix);
setupVertexInput(building->instHeader);
setLights(&lights);
setupDone = true;
}
setMaterial(flags, m->color, m->surfaceProps);
setTexture(0, m->texture);
drawInst(building->instHeader, inst);
}
teardownVertexInput(building->instHeader);
if(defer)
numBlendInsts[pass]++;
}
void
AtomicFullyTransparent(RpAtomic *atomic, int pass, int fadeAlpha)
{
using namespace rw;
using namespace rw::gl3;
BuildingInst *building = &blendInsts[pass][numBlendInsts[pass]];
atomic->getPipeline()->instance(atomic);
building->instHeader = (gl3::InstanceDataHeader*)atomic->geometry->instData;
assert(building->instHeader != nil);
assert(building->instHeader->platform == PLATFORM_GL3);
building->fadeAlpha = fadeAlpha;
building->lighting = !!(atomic->geometry->flags & rw::Geometry::LIGHT);
building->cullMode = rw::GetRenderState(rw::CULLMODE);
building->matrix = *atomic->getFrame()->getLTM();
numBlendInsts[pass]++;
}
void
RenderBlendPass(int pass)
{
using namespace rw;
using namespace rw::gl3;
defaultShader->use();
WorldLights lights;
lights.numAmbients = 1;
lights.numDirectionals = 0;
lights.numLocals = 0;
int i;
for(i = 0; i < numBlendInsts[pass]; i++){
BuildingInst *building = &blendInsts[pass][i];
rw::SetRenderState(rw::CULLMODE, building->cullMode);
setupVertexInput(building->instHeader);
setWorldMatrix(&building->matrix);
if(building->lighting)
lights.ambient = pAmbient->color;
else
lights.ambient = black;
setLights(&lights);
InstanceData *inst = building->instHeader->inst;
for(rw::uint32 j = 0; j < building->instHeader->numMeshes; j++, inst++){
Material *m = inst->material;
if(!inst->vertexAlpha && m->color.alpha == 255 && !IsTextureTransparent(m->texture) && building->fadeAlpha == 255)
continue; // already done this one
rw::RGBA color = m->color;
color.alpha = (color.alpha * building->fadeAlpha)/255;
setMaterial(color, m->surfaceProps); // always modulate here
setTexture(0, m->texture);
drawInst(building->instHeader, inst);
}
teardownVertexInput(building->instHeader);
}
}
}
#endif
#endif
|