-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrendcopy.cpp
1096 lines (905 loc) · 28 KB
/
rendcopy.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
/* CS580 Homework 3 */
#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "Gz.h"
#include "rend.h"
#define _USE_MATH_DEFINES
#include <cmath>
// --------- quaternion representation -----------------------------------------
//
// Each quaternion can be specified by four scalars q = A + Bi + Cj + Dk, so are
// stored as a float4. I’ve tried a struct containing a separate scalar and
// 3-vector to avoid a lot of swizzling, but the float4 representation ends up
// using fewer instructions. A matrix representation is also possible.
//
//---------- QuatMult()----------
//Returns the product of two quaternion number q1 and q2
//Need to change this function to create different Mandelbrot 3D fractals
float4 quatMult(float4 q1, float4 q2){
float4 r;
r.x = q1.x*q2.x - q1.y*q2.y - q1.z*q2.z - q1.w*q2.w;
r.y = q1.x*q2.y + q1.y*q2.x + q1.z*q2.w - q1.w*q2.z;
r.z = q1.x*q2.z - q1.y*q2.w + q1.z*q2.x + q1.w*q2.y;
r.w = q1.x*q2.w + q1.y*q2.z - q1.z*q2.y + q1.w*q2.x;
return r;
}
//maybe define a QuatSq if it makes sense...
//again this might need to change for different fractals
float4 quatSq(float4 q1){
float4 r;
r.x = q1.x*q1.x - q1.y*q1.y - q1.z*q1.z - q1.w*q1.w;
r.y = 2.0*q1.x*q1.y;
r.z = 2.0*q1.x*q1.z;
r.w = 2.0*q1.x*q1.w;
return r;
}
// dot product of two float3 vectors
float dot(float3 a, float3 b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
// dot product of two float4 vectors
float dot(float4 a, float4 b) {
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}
void iteratePoint(float4 q, float4 qp, float4 c, int maxIterations){
for (int i = 0; i < maxIterations; i++){
qp = quatMult(q, qp);
qp = qp * 2.0;
q = quatSq(q) + c;
if (dot(q, q) > ESCAPE_THRESHOLD){
break;
}
}
}
float length(float4 q){
float l = q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w;
return l;
}
float3 normalizef3(float3 f){
float norm = f.x*f.x + f.y*f.y + f.z*f.z;
float3 normalizedf;
normalizedf.x /= norm;
normalizedf.y /= norm;
normalizedf.z /= norm;
return normalizedf;
}
float3 estimateNorm(float3 p, float4 c, int maxIterations){
float3 N;
float4 qP = float4(p, 0);
float gradX, gradY, gradZ;
float4 gx1 = qP - float4(DEL, 0, 0, 0);
float4 gx2 = qP + float4(DEL, 0, 0, 0);
float4 gy1 = qP - float4(0, DEL, 0, 0);
float4 gy2 = qP + float4(0, DEL, 0, 0);
float4 gz1 = qP - float4(0, 0, DEL, 0);
float4 gz2 = qP + float4(0, 0, DEL, 0);
for (int i = 0; i < maxIterations; i++){
gx1 = quatSq(gx1) + c;
gx2 = quatSq(gx2) + c;
gy1 = quatSq(gy1) + c;
gy2 = quatSq(gy2) + c;
gz1 = quatSq(gz1) + c;
gz2 = quatSq(gz2) + c;
}
gradX = length(gx2) - length(gx1);
gradY = length(gy2) - length(gy1);
gradZ = length(gz2) - length(gz1);
N = normalizef3(float3(gradX, gradY, gradZ));
return N;
}
float intersectQJulia(float3 r0, float3 rD, float4 c, int maxIterations, float epsilon){
float dist;
while (1){
float4 z = float4(r0, 0);
float4 zp = float4(1, 0, 0, 0);
iteratePoint(z, zp, c, maxIterations);
float normZ = length(z);
dist = 0.5*normZ*log(normZ) / length(zp);
r0 = r0 + rD*dist;
//ending condition
//We need to break if the ray distance is smaller than epsilon or length is past bound
if (dist <epsilon || dot(r0, r0) > BOUNDING_RADIUS_2)
break;
}
return dist;
}
float3 absf3(float3 n){
if (n.x < 0)
n.x*-1;
if (n.y < 0)
n.y*-1;
if (n.z < 0)
n.z*-1;
return n;
}
float3 Phong(float3 light, float3 eye, float3 pt, float3 N)
{
float3 diffuse = float3(1.00, 0.45, 0.25); // base color of shading
const int specularExponent = 10; // shininess of shading
const float specularity = 0.45; // amplitude of specular highlight
float3 L = normalizef3(light - pt); // find the vector to the light
float3 E = normalizef3(eye - pt); // find the vector to the eye
float NdotL = dot(N, L); // find the cosine of the angle between light and normal
float3 R = L - N *2.0 * NdotL; // find the reflected vector
diffuse = diffuse + absf3(N)*0.3; // add some of the normal to the
// color to make it more interesting
// compute the illumination using the Phong equation
return diffuse * max(NdotL, 0) + specularity*pow(max(dot(E, R), 0), specularExponent);
}
float3 intersectSphere(float3 rO, float3 rD)
{
float B, C, d, t0, t1, t;
B = 2 * dot(rO, rD);
C = dot(rO, rO) - BOUNDING_RADIUS_2;
d = sqrt(B*B - 4.0 * C);
t0 = (-B + d) * 0.5;
t1 = (-B - d) * 0.5;
t = min(t0, t1);
rO = rO + rD * t;
return rO;
}
float4 pixelColor(float3 rO, // ray origin
float3 rD, // ray direction (unit length)
float4 mu, // quaternion constant specifying the particular set
float epsilon, // specifies precision of intersection
float3 eye, // location of the viewer
float3 light, // location of a single point light
bool renderShadows, // flag for turning self-shadowing on/off
int maxIterations){
const float4 backgroundColor = float4(0.3, 0.3, 0.3, 0); //define the background color of the image
float4 color; // This color is the final output of our program.
color = backgroundColor;
rD = normalizef3(rD); //the ray direction is interpolated and may need to be normalized
rO = intersectSphere(rO, rD);
// Next, try to find a point along the ray which intersects the Julia set.
float dist = intersectQJulia(rO, rD, mu, maxIterations, epsilon);
if (dist < epsilon)
{
// Determine a "surface normal" which we’ll use for lighting calculations.
float3 N = estimateNorm(rO, mu, maxIterations);
// Compute the Phong illumination at the point of intersection.
float3 phong = Phong(light, rD, rO, N);
color.x = phong.x; color.y = phong.y; color.z = phong.z;
color.w = 1; // (make this fragment opaque)
// If the shadow flag is on, determine if this point is in shadow
if (renderShadows == true)
{
float3 L = normalizef3(light - rO);
rO = rO + N*epsilon*2.0;
dist = intersectQJulia(rO, L, mu, maxIterations, epsilon);
/*if (dist < epsilon){
color.x = color.x * 0.4; // (darkening the shaded value is not really correct, but looks good)
color.y = color.y * 0.4;
color.z = color.z * 0.4;
}*/
}
}
// Return the final color which is still the background color if we didn’t hit anything.
return color;
}
int GzRotXMat(float degree, GzMatrix mat)
{
// Create rotate matrix : rotate along x axis
// Pass back the matrix using mat value
if (mat == NULL){
return GZ_FAILURE;
}
//GzMatrix rotx;
float radian = degree*M_PI / 180;
//set matrix rows
mat[0][0] = 1;
mat[0][1] = 0;
mat[0][2] = 0;
mat[0][3] = 0;
mat[1][0] = 0;
mat[1][1] = cos(radian);
mat[1][2] = -sin(radian);
mat[1][3] = 0;
mat[2][0] = 0;
mat[2][1] = sin(radian);
mat[2][2] = cos(radian);
mat[2][3] = 0;
mat[3][0] = 0;
mat[3][1] = 0;
mat[3][2] = 0;
mat[3][3] = 1;
return GZ_SUCCESS;
}
int GzRotYMat(float degree, GzMatrix mat)
{
// Create rotate matrix : rotate along y axis
// Pass back the matrix using mat value
if (mat == NULL){
return GZ_FAILURE;
}
float radian = degree*M_PI / 180;
//set matrix rows
mat[0][0] = cos(radian);
mat[0][1] = 0;
mat[0][2] = sin(radian);
mat[0][3] = 0;
mat[1][0] = 0;
mat[1][1] = 1;
mat[1][2] = 0;
mat[1][3] = 0;
mat[2][0] = -sin(radian);
mat[2][1] = 0;
mat[2][2] = cos(radian);
mat[2][3] = 0;
mat[3][0] = 0;
mat[3][1] = 0;
mat[3][2] = 0;
mat[3][3] = 1;
return GZ_SUCCESS;
}
int GzRotZMat(float degree, GzMatrix mat)
{
// Create rotate matrix : rotate along z axis
// Pass back the matrix using mat value
if (mat == NULL){
return GZ_FAILURE;
}
float radian = degree*M_PI / 180;
//set matrix rows
mat[0][0] = cos(radian);
mat[0][1] = -sin(radian);
mat[0][2] = 0;
mat[0][3] = 0;
mat[1][0] = sin(radian);
mat[1][1] = cos(radian);
mat[1][2] = 0;
mat[1][3] = 0;
mat[2][0] = 0;
mat[2][1] = 0;
mat[2][2] = 1;
mat[2][3] = 0;
mat[3][0] = 0;
mat[3][1] = 0;
mat[3][2] = 0;
mat[3][3] = 1;
return GZ_SUCCESS;
}
int GzTrxMat(GzCoord translate, GzMatrix mat)
{
// Create translation matrix
// Pass back the matrix using mat value
if (mat == NULL){
return GZ_FAILURE;
}
//set matrix rows
mat[0][0] = 1;
mat[0][1] = 0;
mat[0][2] = 0;
mat[0][3] = translate[X];
mat[1][0] = 0;
mat[1][1] = 1;
mat[1][2] = 0;
mat[1][3] = translate[Y];
mat[2][0] = 0;
mat[2][1] = 0;
mat[2][2] = 1;
mat[2][3] = translate[Z];
mat[3][0] = 0;
mat[3][1] = 0;
mat[3][2] = 0;
mat[3][3] = 1;
return GZ_SUCCESS;
}
int GzScaleMat(GzCoord scale, GzMatrix mat)
{
// Create scaling matrix
// Pass back the matrix using mat value
if (mat == NULL){
return GZ_FAILURE;
}
//set matrix rows
mat[0][0] = scale[X];
mat[0][1] = 0;
mat[0][2] = 0;
mat[0][3] = 0;
mat[1][0] = 0;
mat[1][1] = scale[Y];
mat[1][2] = 0;
mat[1][3] = 0;
mat[2][0] = 0;
mat[2][1] = 0;
mat[2][2] = scale[Z];
mat[2][3] = 0;
mat[3][0] = 0;
mat[3][1] = 0;
mat[3][2] = 0;
mat[3][3] = 1;
return GZ_SUCCESS;
}
/* Given vector (GzCoord) as input we find the normalization factor of it*/
int normalizeVector(GzCoord g){
if (g == NULL)
return GZ_FAILURE;
float x2 = g[X] * g[X];
float y2 = g[Y] * g[Y];
float z2 = g[Z] * g[Z];
float normal = sqrt(x2 + y2 + z2);
GzCoord n;
g[X] = g[X] / normal;
g[Y] = g[Y] / normal;
g[Z] = g[Z] / normal;
return GZ_SUCCESS;
}
/* Return dot product of two vectors*/
float dotProduct(GzCoord g, GzCoord g1){
float dotprod = g[X] * g1[X] + g[Y] * g1[Y] + g[Z] * g1[Z];
return dotprod;
}
int normalizeMatrix(GzMatrix g){
float K = 1 / (sqrt(g[0][0] * g[0][0] + g[0][1] * g[0][1] + g[0][2] * g[0][2] + g[0][3] * g[0][3]));
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
g[i][j] *= K;
}
}
return GZ_SUCCESS;
}
/* Given normal verts for shading we need to calculate color */
int GzShader(GzRender* render, GzCoord normal, GzColor color){
if (render == NULL)
return GZ_FAILURE;
//normal should be normalized but just in case
normalizeVector(normal);
//set eye vector
GzCoord eye;
eye[X] = 0.0;
eye[Y] = 0.0;
eye[Z] = -1.0;
normalizeVector(eye);
//compute reflections for each light
GzCoord* reflection = new GzCoord[render->numlights];
int* normDirection = new int[render->numlights];
float NL;
float NE;
NE = dotProduct(normal, eye);
for (int i = 0; i < render->numlights; i++){
NL = dotProduct(normal, render->lights[i].direction);
//regular case
if (NE > 0 && NL > 0){
reflection[i][X] = 2 * NL*normal[X] - render->lights[i].direction[X];
reflection[i][Y] = 2 * NL*normal[Y] - render->lights[i].direction[Y];
reflection[i][Z] = 2 * NL*normal[Z] - render->lights[i].direction[Z];
normalizeVector(reflection[i]);
normDirection[i] = 1;
}
//flip normal
else if (NE < 0 && NL < 0){
reflection[i][X] = 2 * NL*(-normal[X]) - render->lights[i].direction[X];
reflection[i][Y] = 2 * NL*(-normal[Y]) - render->lights[i].direction[Y];
reflection[i][Z] = 2 * NL*(-normal[Z]) - render->lights[i].direction[Z];
normalizeVector(reflection[i]);
normDirection[i] = -1;
}
//do nothing
else{
normDirection[i] = 0;
continue;
}
}
//specular coefficient
//Ks * sum[le (R dot E)^s]
GzColor specularSum;
GzColor specular;
specularSum[0] = 0; specularSum[1] = 0; specularSum[2] = 0;
for (int i = 0; i < render->numlights; i++){
if (normDirection[i] == 0)
continue;
float RE = dotProduct(reflection[i], eye);
//clip R dot E to keep in range [0,1]
if (RE < 0){
RE = 0;
}
else if (RE > 1){
RE = 1;
}
specularSum[0] += render->lights[i].color[0] * pow(RE, render->spec);
specularSum[1] += render->lights[i].color[1] * pow(RE, render->spec);
specularSum[2] += render->lights[i].color[2] * pow(RE, render->spec);
}
specular[0] = render->Ks[0] * specularSum[0];
specular[1] = render->Ks[1] * specularSum[1];
specular[2] = render->Ks[2] * specularSum[2];
//diffusion coefficient
//Kd sum[le N dot L]
GzColor diffusionSum = { 0, 0, 0 };
GzColor diffusion = { 0, 0, 0 };
for (int i = 0; i < render->numlights; i++){
if (normDirection[i] == 0){
continue;
}
//negate normal
if (normDirection[i] == -1){
GzCoord negativeNorm;
negativeNorm[X] = -normal[X];
negativeNorm[Y] = -normal[Y];
negativeNorm[Z] = -normal[Z];
float negNL = dotProduct(negativeNorm, render->lights[i].direction);
diffusionSum[0] += render->lights[i].color[0] * negNL;
diffusionSum[1] += render->lights[i].color[1] * negNL;
diffusionSum[2] += render->lights[i].color[2] * negNL;
}
//regular normal
else if (normDirection[i] == 1){
float NL1 = dotProduct(normal, render->lights[i].direction);
diffusionSum[0] += render->lights[i].color[0] * NL1;
diffusionSum[1] += render->lights[i].color[1] * NL1;
diffusionSum[2] += render->lights[i].color[2] * NL1;
}
if (true)
int y = 10;
}
diffusion[0] = render->Kd[0] * diffusionSum[0];
diffusion[1] = render->Kd[1] * diffusionSum[1];
diffusion[2] = render->Kd[2] * diffusionSum[2];
//compute ambient
//Ka * la
GzColor ambient = { 0, 0, 0 };
ambient[0] = render->Ka[0] * render->ambientlight.color[0];
ambient[1] = render->Ka[1] * render->ambientlight.color[1];
ambient[2] = render->Ka[2] * render->ambientlight.color[2];
//compute color
color[0] = specular[0] + diffusion[0] + ambient[0];
color[1] = specular[1] + diffusion[1] + ambient[1];
color[2] = specular[2] + diffusion[2] + ambient[2];
if (true)
int k = 10;
return GZ_SUCCESS;
}
//----------------------------------------------------------
// Begin main functions
int GzNewRender(GzRender **render, GzDisplay *display)
{
/*
- malloc a renderer struct
- setup Xsp and anything only done once
- save the pointer to display
- init default camera
*/
*render = new GzRender[1];
render[0]->display = display;
//intialize Xsp array
render[0]->Xsp[0][0] = display->xres / 2;
render[0]->Xsp[0][1] = 0; render[0]->Xsp[0][2] = 0;
render[0]->Xsp[0][3] = display->xres / 2;
render[0]->Xsp[1][1] = -(display->yres / 2);
render[0]->Xsp[1][0] = 0; render[0]->Xsp[1][2] = 0;
render[0]->Xsp[1][3] = display->yres / 2;
render[0]->Xsp[2][2] = MAXINT;
render[0]->Xsp[2][0] = 0; render[0]->Xsp[2][1] = 0; render[0]->Xsp[2][3] = 0;
render[0]->Xsp[3][3] = 1;
render[0]->Xsp[3][0] = 0; render[0]->Xsp[3][1] = 0; render[0]->Xsp[3][2] = 0;
//set my camera stuff yo
render[0]->camera.position[X] = DEFAULT_IM_X;
render[0]->camera.position[Y] = DEFAULT_IM_Y;
render[0]->camera.position[Z] = DEFAULT_IM_Z;
render[0]->camera.lookat[X] = 0.0;
render[0]->camera.lookat[Y] = 0.0;
render[0]->camera.lookat[Z] = 0.0;
render[0]->camera.worldup[X] = 0.0;
render[0]->camera.worldup[Y] = 1.0;
render[0]->camera.worldup[Z] = 0.0;
render[0]->camera.FOV = DEFAULT_FOV;
//this render is so empty man
render[0]->matlevel = -1;
render[0]->numlights = 0;
return GZ_SUCCESS;
}
int GzFreeRender(GzRender *render)
{
/*
-free all renderer resources
*/
//GzFreeDisplay(render->display);
//delete render->camera.lookat;
//delete render->camera.position;
//delete render->camera.worldup;
//delete render->camera.Xiw;
//delete render->camera.Xpi;
//delete render->Xsp;
//delete render->Ximage;
delete render;
return GZ_SUCCESS;
}
int GzBeginRender(GzRender *render)
{
/*
- setup for start of each frame - init frame buffer color,alpha,z
- compute Xiw and projection xform Xpi from camera definition
- init Ximage - put Xsp at base of stack, push on Xpi and Xiw
- now stack contains Xsw and app can push model Xforms when needed
*/
GzInitDisplay(render->display);
//set Xpi
render->camera.Xpi[0][0] = 1;
render->camera.Xpi[0][1] = 0; render->camera.Xpi[0][2] = 0; render->camera.Xpi[0][3] = 0;
render->camera.Xpi[1][1] = 1;
render->camera.Xpi[1][0] = 0; render->camera.Xpi[1][2] = 0; render->camera.Xpi[1][3] = 0;
render->camera.Xpi[2][2] = tan((render->camera.FOV / 2.0)*M_PI / 180.0);
render->camera.Xpi[2][0] = 0; render->camera.Xpi[2][1] = 0; render->camera.Xpi[2][3] = 0;
render->camera.Xpi[3][0] = 0; render->camera.Xpi[3][1] = 0;
render->camera.Xpi[3][2] = tan((render->camera.FOV / 2.0)*M_PI / 180.0);
render->camera.Xpi[3][3] = 1;
//set Xiw
//need to calculate Z
GzCoord cz;
cz[X] = render->camera.lookat[X] - render->camera.position[X];
cz[Y] = render->camera.lookat[Y] - render->camera.position[Y];
cz[Z] = render->camera.lookat[Z] - render->camera.position[Z];
normalizeVector(cz);
//need to calculate Y
GzCoord cy, upprime;
float dot = dotProduct(render->camera.worldup, cz);
upprime[X] = render->camera.worldup[X] - dot*cz[X];
upprime[Y] = render->camera.worldup[Y] - dot*cz[Y];
upprime[Z] = render->camera.worldup[Z] - dot*cz[Z];
cy[X] = upprime[X];
cy[Y] = upprime[Y];
cy[Z] = upprime[Z];
normalizeVector(cy);
//need to calculate X bro
//A = a2*b3 - a3*b2
//B = a3*b1 - a1*b3
//C = a1*b2 - a2*b1
GzCoord cx;
cx[X] = cy[Y] * cz[Z] - cy[Z] * cz[Y];
cx[Y] = cy[Z] * cz[X] - cy[X] * cz[Z];
cx[Z] = cy[X] * cz[Y] - cy[Y] * cz[X];
normalizeVector(cx);
render->camera.Xiw[0][0] = cx[X];
render->camera.Xiw[0][1] = cx[Y];
render->camera.Xiw[0][2] = cx[Z];
render->camera.Xiw[0][3] = -dotProduct(cx, render->camera.position);
render->camera.Xiw[1][0] = cy[X];
render->camera.Xiw[1][1] = cy[Y];
render->camera.Xiw[1][2] = cy[Z];
render->camera.Xiw[1][3] = -dotProduct(cy, render->camera.position);
render->camera.Xiw[2][0] = cz[X];
render->camera.Xiw[2][1] = cz[Y];
render->camera.Xiw[2][2] = cz[Z];
render->camera.Xiw[2][3] = -dotProduct(cz, render->camera.position);
render->camera.Xiw[3][0] = 0; render->camera.Xiw[3][1] = 0; render->camera.Xiw[3][2] = 0;
render->camera.Xiw[3][3] = 1;
GzMatrix I = { 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 };
//Need to push Xsp => Xpi => Xiw
GzPushMatrix(render, render->Xsp);
GzPushNormalMatrix(render, I);
GzPushMatrix(render, render->camera.Xpi);
GzPushNormalMatrix(render, I);
GzPushMatrix(render, render->camera.Xiw);
//GzPushNormalMatrix(render, render->camera.Xiw);
return GZ_SUCCESS;
}
int GzPutCamera(GzRender *render, GzCamera *camera)
{
/*
- overwrite renderer camera structure with new camera definition
*/
render->camera.FOV = camera->FOV;
render->camera.lookat[X] = camera->lookat[X];
render->camera.lookat[Y] = camera->lookat[Y];
render->camera.lookat[Z] = camera->lookat[Z];
render->camera.position[X] = camera->position[X];
render->camera.position[Y] = camera->position[Y];
render->camera.position[Z] = camera->position[Z];
render->camera.worldup[X] = camera->worldup[X];
render->camera.worldup[Y] = camera->worldup[Y];
render->camera.worldup[Z] = camera->worldup[Z];
return GZ_SUCCESS;
}
int GzPushNormalMatrix(GzRender *render, GzMatrix matrix){
/*
- push a matrix onto the Xnorm stack
- check for stack overflow
*/
if (render == NULL)
return GZ_FAILURE;
if (matrix == NULL)
return GZ_FAILURE;
//we have overflow
//render->matlevel = render->matlevel + 1;
if (render->matlevel >= MATLEVELS)
return GZ_FAILURE;
//GzMatrix g;
//get Rid of T, but should we save it? No. Cause...yeah
//matrix[0][3] = 0;
//matrix[1][3] = 0;
//matrix[2][3] = 0;
normalizeMatrix(matrix);
matrix[3][0] = 0;
//push that Matrix yo
if (render->matlevel == 0){
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
render->Xnorm[render->matlevel][i][j] = matrix[i][j];
}
}
}
else{
//multiply the matrix and then we need to put it at the top
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
render->Xnorm[render->matlevel][i][j] = render->Xnorm[render->matlevel - 1][i][0] * matrix[0][j] + render->Xnorm[render->matlevel - 1][i][1] * matrix[1][j] +
render->Xnorm[render->matlevel - 1][i][2] * matrix[2][j] + render->Xnorm[render->matlevel - 1][i][3] * matrix[3][j];
}
}
}
if (true)
int k = 10;
return GZ_SUCCESS;
}
int GzPushMatrix(GzRender *render, GzMatrix matrix)
{
/*
- push a matrix onto the Ximage stack
- check for stack overflow
*/
if (render == NULL)
return GZ_FAILURE;
if (matrix == NULL)
return GZ_FAILURE;
//we have overflow
render->matlevel = render->matlevel + 1;
if (render->matlevel >= MATLEVELS)
return GZ_FAILURE;
GzMatrix g;
float multvalue = 0;
if (render->matlevel == 0){
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
render->Ximage[render->matlevel][i][j] = matrix[i][j];
}
}
}
else{
//multiply the matrix and then we need to put it at the top
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
render->Ximage[render->matlevel][i][j] = render->Ximage[render->matlevel - 1][i][0] * matrix[0][j] + render->Ximage[render->matlevel - 1][i][1] * matrix[1][j] +
render->Ximage[render->matlevel - 1][i][2] * matrix[2][j] + render->Ximage[render->matlevel - 1][i][3] * matrix[3][j];
}
}
}
GzPushNormalMatrix(render, matrix);
return GZ_SUCCESS;
}
int GzPopMatrix(GzRender *render)
{
/*
- pop a matrix off the Ximage stack
- check for stack underflow
*/
if (render == NULL)
return GZ_FAILURE;
//underflow
if (render->Ximage[0] == NULL)
return GZ_FAILURE;
if (render->matlevel < 0)
return GZ_FAILURE;
//change top of pointer to one level lower
render->matlevel--;
return GZ_SUCCESS;
}
int GzPutAttribute(GzRender *render, int numAttributes, GzToken *nameList, GzPointer *valueList) /* void** valuelist */
{
/*
- set renderer attribute states (e.g.: GZ_RGB_COLOR default color)
- later set shaders, interpolaters, texture maps, and lights
*/
//error checking
if (render == NULL)
return GZ_FAILURE;
if (nameList == NULL)
return GZ_FAILURE;
if (valueList == NULL)
return GZ_FAILURE;
for (int i = 0; i < numAttributes; i++){
if (nameList[i] == GZ_RGB_COLOR){
GzColor* a = (GzColor *)(valueList[i]);
float r = a[0][0];
float g = a[0][1];
float b = a[0][2];
render->flatcolor[0] = r;
render->flatcolor[1] = g;
render->flatcolor[2] = b;
}
else if (nameList[i] == GZ_AMBIENT_COEFFICIENT){
GzColor* a = (GzColor *)valueList[i];
render->Ka[0] = a[0][0];
render->Ka[1] = a[0][1];
render->Ka[2] = a[0][2];
}
else if (nameList[i] == GZ_AMBIENT_LIGHT){
GzLight* l = (GzLight*)(valueList[i]);
render->ambientlight = *l;
}
else if (nameList[i] == GZ_DIFFUSE_COEFFICIENT){
GzColor* d = (GzColor *)valueList[i];
render->Kd[0] = d[0][0];
render->Kd[1] = d[0][1];
render->Kd[2] = d[0][2];
}
else if (nameList[i] == GZ_DIRECTIONAL_LIGHT){
GzLight* l = (GzLight*)(valueList[i]);
render->lights[render->numlights] = *l;
render->numlights += 1;
if (true)
int k = 10;
}
else if (nameList[i] == GZ_DISTRIBUTION_COEFFICIENT){
float* n = (float*)valueList[i];
render->spec = *n;
}
else if (nameList[i] == GZ_SPECULAR_COEFFICIENT){
GzColor* s = (GzColor *)valueList[i];
render->Ks[0] = s[0][0];
render->Ks[1] = s[0][1];
render->Ks[2] = s[0][2];
}
else if (nameList[i] == GZ_INTERPOLATE){
int* n = (int *)valueList[i];
render->interp_mode = *n;
}
}
return GZ_SUCCESS;
}
int GzPutRays(GzRender *render)
{
/*
- pass in a triangle description with tokens and values corresponding to
GZ_POSITION:3 vert positions in model space
- Xform positions of verts using matrix on top of stack
- Clip - just discard any triangle with any vert(s) behind view plane
- optional: test for triangles with all three verts off-screen (trivial frustum cull)
- invoke triangle rasterizer
*/
//declare variables to store vertex information
float xgap = (4.0) / render->display->xres;//(secondxdim - firstxdim) / render->display->xres;
float ygap = (6.0) / render->display->yres;//(secondydim - firstydim) / render->display->yres;
float4* colorValue = new float4[render->display->xres*render->display->yres];
float* normRayOriginX = new float[render->display->xres*render->display->yres];
float* normRayOriginY = new float[render->display->xres*render->display->yres];
float* normRayOriginZ = new float[render->display->xres*render->display->yres];
GzMatrix topXform;
bool isBehindPlane = false;
float4 rorigin;
float4 modelrorigin;
float W;
//variables needed for coloring algorithm
float4 mu = (0.20, 0.20, 0.0, 0.0);
float3 normrayorigin;
float3 raydir;
float epsilon = 0.003;
float3 light = ( 0.3, 0.3, 0.3 );
float3 eye = { render->camera.lookat[X], render->camera.lookat[Y], render->camera.lookat[Z] };
int xstep = 0;
int ystep = 0;
float firstxdim = -1.5;
float secondxdim = 1.5;
float firstydim = -2.5;
float secondydim = 2.5;
//float firstzdim = -;
float secondzdim = 3.0;
for (int m = 0; m < 4; m++){
for (int n = 0; n < 4; n++){
topXform[m][n] = render->Ximage[render->matlevel][m][n];
}
}
//need to convert points in interval
for (float i = firstxdim; i < secondxdim; i += xgap){
xstep++;
for (float j = firstydim; j < secondydim; j += ygap){
ystep++;
rorigin = float4(i, j, 2.0, 0.0);
modelrorigin.x = rorigin.x * topXform[0][0] + rorigin.y * topXform[0][1] + rorigin.z * topXform[0][2] + topXform[0][3];
modelrorigin.y = rorigin.x * topXform[1][0] + rorigin.y * topXform[1][1] + rorigin.z * topXform[1][2] + topXform[1][3];
modelrorigin.z = rorigin.x * topXform[2][0] + rorigin.y * topXform[2][1] + rorigin.z * topXform[2][2] + topXform[2][3];
W = rorigin.x * topXform[3][0] + rorigin.y * topXform[3][1] + rorigin.z * topXform[3][2] + topXform[3][3];
//check to see if we are behind of the viewing plane
if (modelrorigin.z < render->camera.position[Z]){
isBehindPlane = true;
break;
}
//normalize to 3d
modelrorigin.x /= W;
modelrorigin.y /= W;
modelrorigin.z /= W;
//normalized ray origin
normrayorigin = (modelrorigin.x, modelrorigin.y, modelrorigin.z);
//how do we calculate ray direction?
//Should it be to the camera look-at point translated to our current respective position from the camera's origin?
raydir = (0.9, 0.5, 0.0);
//We need to save where these rays are emanating from and their respective colors
//Then we can use these for something but I'm not sure what
float4 colorrayval = pixelColor(normrayorigin, raydir, mu, epsilon, eye, light, false, 100);
colorValue[ystep + xstep*render->display->yres] = colorrayval;
normRayOriginX[ystep + xstep*render->display->yres] = normrayorigin.x;
normRayOriginY[ystep + xstep*render->display->yres] = normrayorigin.y;
normRayOriginZ[ystep + xstep*render->display->yres] = normrayorigin.z;
}
ystep = 0;
}
if (true){
int x = 10;
}