-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3D.c
871 lines (666 loc) · 21.3 KB
/
3D.c
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
#ifdef __cplusplus
extern "C" {
#endif
/*-- NOTE: You will have to manipulate this code to correspond with the
structures and stack implementation you chose to use.
Don't forget to put declarations in your .h files.
See the supplemental handout for information on integrating C and
C++ code.
--*/
/* The following (not including LookAt and Vertex3f) could either be
implemented into your file or they can stand as a separate file called
lines.c It will be used for drawing pixels to the screen and
for clipping. */
#include <stdio.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include "3D.h"
/* the clipping window */
static float xmin = 0.0;
static float xmax = 1.0;
static float ymin = 0.0;
static float ymax = 1.0;
static void set_clip_window(float, float, float, float);
static int clip_line(float *, float *, float *, float *);
/* These go in your .h file or in lines.h */
/*
Line drawing header.
*/
void draw_line(float, float, float, float);
int near_far_clip(float, float, float *, float *, float *, float *,
float *, float *);
/******************************************************************************
Draw a white line.
Entry:
x0,y0 - first endpoint of line
x1,y1 - second line endpoint
******************************************************************************/
void draw_line(float x0, float y0, float x1, float y1)
{
GLint viewport[4];
int i;
float x,y;
float dx,dy;
float xinc,yinc;
float length=0.;
int result;
int width,height;
//printf("71-into draw_line routine");
/* set the clipping window */
glGetIntegerv(GL_VIEWPORT, viewport);
width = abs(viewport[2]-viewport[0]);
height = abs(viewport[3]-viewport[1]);
//printf("78-width:%d, height:%d\n", width, height);
set_clip_window (0.0, 0.0, width - 0.51, height - 0.51);
/* clip the line in 2D */
result = clip_line (&x0, &y0, &x1, &y1);
/* return if line is entirely outside the clip window */
if (result == 0)
return;
/* incremental line drawing */
dx = x1 - x0;
dy = y1 - y0;
/*
printf("3D:94-x0:%f,y0:%f\n", x0, y0);
printf("3D:95-x1:%f,y1:%f\n", x1, y1);
printf("3D:96-dx:%f,dy:%f\n", dx, dy);
*/
/* determine whether horizontal or vertical change is larger */
if (fabs(dx) > fabs(dy))
length = fabs(dx);
else
length = fabs(dy);
/* special case to avoid dividing by zero */
if (length == 0) {
//printf("3D:109-length==0\n");
glColor3f( 1.0, 1.0, 1.0 );
glBegin(GL_POINTS);
//printf("3D:111-length==0 -x0:%f, y0:%f\n", x0, y0);
glVertex2i((int) floor(x0+0.5), (int) floor(y0+0.5));
glEnd();
return;
}
xinc = dx / length;
yinc = dy / length;
x = x0;
y = y0;
/* write "length" number of pixels along the line */
for (i = 0; i <= length; i++) {
glColor3f( 1.0, 1.0, 1.0 );
glBegin(GL_POINTS);
//printf("3D:130-(int) floor(x+0.5):%d, (int) floor(y+0.5):%d\n", (int) floor(x+0.5), (int) floor(y+0.5));
glVertex2i((int) floor(x+0.5), (int) floor(y+0.5));
glEnd();
x += xinc;
y += yinc;
}
glFlush();
}
/******************************************************************************
Specify a clipping window.
Entry:
x0,y0 - lower left boundaries of clipping window
x1,y1 - upper right boundaries
******************************************************************************/
static void set_clip_window(float x0, float y0, float x1, float y1)
{
xmin = x0;
ymin = y0;
xmax = x1;
ymax = y1;
}
/******************************************************************************
Given a point P outside the window and the rise and run of a line, return
the intersection of line with window that is nearest P.
Entry:
dx,dy - run and rise of line
x,y - the given point P
Exit:
ix,iy - intersection point
return 1 if there was a valid intersection, 0 if not
******************************************************************************/
static int clip_helper(
float dx,
float dy,
float x,
float y,
float *ix,
float *iy
)
{
/* if line not vertical, check against left and right edges of window */
if (dx != 0) {
/* check against left edge */
*iy = dy / dx * (xmin - x) + y;
if (xmin >= x && *iy >= ymin && *iy <= ymax) {
*ix = xmin;
return (1);
}
/* check against right edge */
*iy = dy / dx * (xmax - x) + y;
if (xmax <= x && *iy >= ymin && *iy <= ymax) {
*ix = xmax;
return (1);
}
}
/* if line not horizontal, check against top and bottom edges of window */
if (dy != 0) {
/* check against bottom edge */
*ix = dx / dy * (ymin - y) + x;
if (ymin >= y && *ix >= xmin && *ix <= xmax) {
*iy = ymin;
return (1);
}
/* check against top edge */
*ix = dx / dy * (ymax - y) + x;
if (ymax <= y && *ix >= xmin && *ix <= xmax) {
*iy = ymax;
return (1);
}
}
/* if we get here, we found no intersection */
return (0);
}
/******************************************************************************
Clip a line segment to a pre-specified window.
Entry:
x0,y0 - first line segment endpoint
x1,y1 - second endpoint
Exit:
x0,y0,x1,y1 - clipped endpoint positions
returns 1 if segment is at least partially in window,
returns 0 if segment is entirely outside window
******************************************************************************/
static int clip_line(float *x0, float *y0, float *x1, float *y1)
{
int count;
float dx,dy;
float xx0 = *x0;
float yy0 = *y0;
float xx1 = *x1;
float yy1 = *y1;
int code04 = (xx0 < xmin) ? 1 : 0;
int code03 = (xx0 > xmax) ? 1 : 0;
int code02 = (yy0 < ymin) ? 1 : 0;
int code01 = (yy0 > ymax) ? 1 : 0;
int code14 = (xx1 < xmin) ? 1 : 0;
int code13 = (xx1 > xmax) ? 1 : 0;
int code12 = (yy1 < ymin) ? 1 : 0;
int code11 = (yy1 > ymax) ? 1 : 0;
int sum0 = code01 + code02 + code03 + code04;
int sum1 = code11 + code12 + code13 + code14;
/* completely inside window? */
if (sum0 == 0 && sum1 == 0)
return (1);
/* check for trivial invisibility (both endpoints on wrong side of */
/* a single side of the window) */
if ((code01 && code11) || (code02 && code12) || (code03 && code13) ||
(code04 && code14)) {
return (0);
}
/* compute run and rise */
dx = xx1 - xx0;
dy = yy1 - yy0;
/* case: only x0,y0 is inside window */
if (sum0 == 0) {
//int dummy =
clip_helper (dx, dy, xx1, yy1, &xx1, &yy1);
*x0 = xx0;
*y0 = yy0;
*x1 = xx1;
*y1 = yy1;
return (1);
}
/* case: only x1,y1 is inside window */
if (sum1 == 0) {
// int dummy =
clip_helper (dx, dy, xx0, yy0, &xx0, &yy0);
*x0 = xx0;
*y0 = yy0;
*x1 = xx1;
*y1 = yy1;
return (1);
}
/* neither endpoint is inside the window */
count = 0;
count += clip_helper (dx, dy, xx0, yy0, &xx0, &yy0);
count += clip_helper (dx, dy, xx1, yy1, &xx1, &yy1);
*x0 = xx0;
*y0 = yy0;
*x1 = xx1;
*y1 = yy1;
if (count)
return (1);
else
return (0);
}
/******************************************************************************
Clip a line segment to front and back clipping planes. These clip planes
are along the z-axis. If your objects are on the negative z portion of
the axis, be sure to specify negative values for "near" and "far".
Entry:
near,far - clip planes along z-axis
x0,y0,z0 - first line segment endpoint
x1,y1,z1 - second endpoint
Exit:
x0,y0,z0,x1,y1,z1 - clipped endpoint positions
returns 1 if segment is at least partially in window,
returns 0 if segment is entirely outside window
******************************************************************************/
int near_far_clip(
float near,
float far,
float *x0,
float *y0,
float *z0,
float *x1,
float *y1,
float *z1
)
{
float temp;
float fract;
float xx0 = *x0;
float yy0 = *y0;
float zz0 = *z0;
float xx1 = *x1;
float yy1 = *y1;
float zz1 = *z1;
int code00,code01,code10,code11;
/* make sure near < far */
if (near > far) {
//printf("358-swap near and far\n");
temp = far;
far = near;
near = temp;
}
/* figure out which endpoints are outside the clipping volume */
//printf("365-near:%f, far:%f\n", near, far);
code00 = (zz0 < near) ? 1 : 0;
code01 = (zz0 > far) ? 1 : 0;
code10 = (zz1 < near) ? 1 : 0;
code11 = (zz1 > far) ? 1 : 0;
//printf("370-code00:%d, code01:%d, code10:%d, code11:%d\n", code00, code01, code10, code11);
/* return without clipping if all endpoints are inside clip volume */
if (code00 + code01 + code10 + code11 == 0)
return (1); /* signals inside volume */
/* if both endpoints are entirely out of clip volume, exit and signal this */
if ((code00 && code10) || (code01 && code11))
return (0); /* signals outside volume */
/* clip to near plane if necessary */
if (code00) {
fract = (near - zz0) / (zz1 - zz0);
xx0 = xx0 + fract * (xx1 - xx0);
yy0 = yy0 + fract * (yy1 - yy0);
zz0 = near;
}
else if (code10) {
fract = (near - zz1) / (zz0 - zz1);
xx1 = xx1 + fract * (xx0 - xx1);
yy1 = yy1 + fract * (yy0 - yy1);
zz1 = near;
}
/* clip to far plane if necessary */
if (code01) {
fract = (far - zz0) / (zz1 - zz0);
xx0 = xx0 + fract * (xx1 - xx0);
yy0 = yy0 + fract * (yy1 - yy0);
zz0 = far;
}
else if (code11) {
fract = (far - zz1) / (zz0 - zz1);
xx1 = xx1 + fract * (xx0 - xx1);
yy1 = yy1 + fract * (yy0 - yy1);
zz1 = far;
}
/* copy the clipped endpoints */
*x0 = xx0;
*y0 = yy0;
*z0 = zz0;
*x1 = xx1;
*y1 = yy1;
*z1 = zz1;
printf("420-x0:%f, y0:%f, z0:%f, x1:%f, y1:%f, z1:%f\n", *x0, *y0, *z0, *x1, *y1, *z1);
/* signal that we're inside the clip volume */
return (1);
}
/*=================== End of lines.c =====================================*/
/* Implement the following routines into your file */
#define ROW 4
#define COL 4
/* Definitions of the types of structures and variables used in the following
code. Use these or implement your own. */
float Near=0.1, Far=10;//Far actually means the near clipping plane
float w;
int perspflag=0;
const matrix_unit I = {
{ {1., 0., 0., 0.},
{0., 1., 0., 0.},
{0., 0., 1., 0.},
{0., 0., 0., 1.} },
};
matrix_unit ini = {
{ {1., 0., 0., 0.},
{0., 1., 0., 0.},
{0., 0., 1., 0.},
{0., 0., 0., 1.} },
};
//matrix_unit current = I;
matrix_unit *stack[50]={&ini}; /* array of pointers to act as a stack */
int top = 0; /* points to top of the stack */
int width, height; /* height and width of frame buffer */
matrix_unit orth = {
{ {1.25, 0. , 0. , 124.5},
{0. , 1.25, 0. , 124.5},
{0. , 0. , -0.04, -0.6},
{0. , 0. , 0. , 1.} },
};
/* global ortho and perspective matrices */
/* to be used in Vertex3f */
matrix_unit perspect= {
{ {216.506, 0. , 124.5, 0. },
{0. , 216.506 , 124.5, 0. },
{0. , 0. , -1. , 0.002},
{0. , 0. , 1. , 0.} },
};
/* These go in your .h file */
void gtLookAt( float fx, float fy, float fz, float atx, float aty,
float atz, float upx, float upy, float upz);
void gtVertex3f(float x, float y, float z);
/*========================================================================*/
/* Finds a cross product of two vectors */
/*========================================================================*/
void Cross(Vector *x, Vector *y, Vector *z) {
z->i = x->j * y->k - x->k * y->j;
z->j = x->k * y->i - x->i * y->k;
z->k = x->i * y->j - x->j * y->i;
}
/*========================================================================*/
/* Finds a unit vector in the direction of a given vector */
/*========================================================================*/
void Unitvec(float x, float y, float z, Vector *vec){
float vec_length;
vec_length = sqrt(x*x + y*y + z*z);
vec->i = x/vec_length;
vec->j = y/vec_length;
vec->k = z/vec_length;
}
/*========================================================================*/
/* Multiplies a 4x4 matrix by a 4x1 vector */
/* Returns 0 upon success and 1 if pointers are not pointing to matrices */
/*========================================================================*/
int Mult_end(matrix_unit *M, matrix41 *V, matrix41 *result) {
int i, j;
if (M == NULL || V == NULL || result == NULL) {
printf("error in matrix multiplication \n");
return 1;
}
//printf("518-input:\n");
//for(i=0;i<4;i++)
//printf("520-intput[%d]:%f\n", i, (V->mat41)[i]);
//printf("521-end\n");
for (i=0; i<4; i++) {
(result->mat41)[i] = 0;
for (j=0; j<4; j++) {
(result->mat41)[i] += (M->mat)[i][j]*(V->mat41)[j];
//printf("522-result[%d]:%f\n", i, (result->mat41)[i]);
}
}
//printf("525-result:\n");
//for(i=0;i<4;i++)
//printf("527-result[%d]:%f\n", i, (result->mat41)[i]);
//printf("528-end\n");
return 0;
}
/*========================================================================*/
/* Multiplies two 4x4 matrices */
/* Returns 0 upon success and 1 if pointers are not pointing to matrices */
/*========================================================================*/
int Mult_mat(matrix_unit *left, matrix_unit *right, matrix_unit *result) {
int i, j, k;
if (left == NULL || right == NULL || result == NULL) {
printf("error in matrix multiplication -- matrix not allocated\n");
return 1;
}
for (i=0; i<4; i++) {
for (j=0; j<4; j++) {
(result->mat)[i][j] = 0;
for (k=0; k<4; k++)
(result->mat)[i][j] += (left->mat)[i][k]*(right->mat)[k][j];
}
}
return 0;
}
int Copy_mat(const matrix_unit *from, matrix_unit *to) {
int i, j;
if (from != NULL && to != NULL) {
for (i=0; i<ROW; i++)
for (j=0; j<ROW; j++)
(to->mat)[i][j] = (from->mat)[i][j];
return 0;
}
else {
printf(" Cannot copy matrix\n");
return 1;
}
}
/*========================================================================*/
/*gtLookAt allows a change of the point of view from which the scene is
is looked at. (like a virtual camera)*/
/*========================================================================*/
void gtLookAt( float fx, float fy, float fz, float atx, float aty,
float atz, float upx, float upy, float upz)
{
//printf("571-gtLookAt\n");
float dx, dy, dz ;
Vector slnv, rx, ry, rz, up;
matrix_unit ltrans, tmpsln, *t, rfin, lookat;
//Vertex_unit v, sv;
//int i, j;
/* translation */
ltrans = I;
t = <rans;
t->mat[0][3] = -fx;
t->mat[1][3] = -fy;
t->mat[2][3] = -fz;
up.i = upx; up.j = upy; up.k = upz;
Unitvec(up.i, up.j, up.k, &slnv);
up = slnv;
/* make P1P2 (rz) vector */
dx = atx - fx; dy = aty - fy; dz = atz - fz;
Unitvec( dx, dy, dz, &rz);
rfin = I;
/* make rx */
Cross(&rz, &up, &rx);
Unitvec( rx.i, rx.j, rx.k, &slnv);
rx = slnv;
/* make ry */
Cross(&rx, &rz, &ry);
rfin.mat[0][0] = rx.i; rfin.mat[0][1] = rx.j; rfin.mat[0][2] = rx.k;
rfin.mat[1][0] = ry.i; rfin.mat[1][1] = ry.j; rfin.mat[1][2] = ry.k;
rfin.mat[2][0] = -rz.i; rfin.mat[2][1] = -rz.j; rfin.mat[2][2] = -rz.k;
Mult_mat(&rfin, <rans, &lookat);
/* Multiply the lookat matrix by the matrix currently on the top
of the stack. This then becomes the new top of the stack. */
/* This will depend on how you implemented your stack */
Mult_mat(stack[top], &lookat, &tmpsln);
Copy_mat(&tmpsln, stack[top]);
/*
printf("633lookat-stack[%d]:\n", top);
for(int i = 0; i <4;i++){
for(int j = 0; j<4;j++){
printf("%f ", stack[top]->mat[i][j]);
}
printf("\n");
}
printf("end of debug\n");
printf("642lookat- I:\n");
for(int i = 0; i <4;i++){
for(int j = 0; j<4;j++){
printf("%f ", I.mat[i][j]);
}
printf("\n");
}
printf("end of debug\n");
*/
}
/*------------------------------End of gtLookAt -------------------------*/
/*=====================================================================*/
/*gtVertex3f specifies a 3D vertex that is a line endpoint.
White lines are drawn between successive odd/even pairs of
these vertices.*/
/*=====================================================================*/
void gtVertex3f(float x, float y, float z)
{
matrix41 printvec, tmp, vertex1, vertex2, pvert1, pvert2, permat1, permat2;
float x0, y0, z0, x1, y1, z1;
static int printflag=0;
static matrix41 savemat;
//float xpixel1, ypixel1, xpixel2, ypixel2;
printvec.mat41[0]=x; /* set up 4x1 matrix of the vertex */
printvec.mat41[1]=y;
printvec.mat41[2]=z;
printvec.mat41[3]=1;
/*Mult_end is a function to multiply matrix a * matrix b and put the
solution in sln. This function multiplies [4][4]*[4][1]. This will
vary depending on your implementation. */
Mult_end(stack[top],&printvec, &tmp);
/*
printf("679:stack[top]:\n");
for(int i = 0; i<4; i++){
for(int j = 0; j<4; j++){
printf(" %f", stack[top]->mat[i][j]);
}
printf("\n");
}
printf("\n");
printf("end of debug\n");
*/
printflag++; /* increase counter */
//printf("3D.c:640-printflag:%d\n", printflag);
if(printflag==1) { /* if the first vertex, save the points */
savemat.mat41[0]=tmp.mat41[0];
savemat.mat41[1]=tmp.mat41[1];
savemat.mat41[2]=tmp.mat41[2];
savemat.mat41[3]=1;
//printf("687-savemat.mat41:%f %f %f\n", savemat.mat41[0], savemat.mat41[1], savemat.mat41[2]);
}
if(printflag==2) { /* if the second vertex, test clipping */
x1=tmp.mat41[0];
y1=tmp.mat41[1];
z1=tmp.mat41[2];
x0=savemat.mat41[0];
y0=savemat.mat41[1];
z0=savemat.mat41[2];
//printf("699- x0 vector %f %f %f\n", x0, y0, z0);
//printf("700- x1 vector %f %f %f\n", x1, y1, z1);
/* if clipping occurs and points are within view volume, draw line */
/* from v1 to v2 */
if(near_far_clip(Near, Far, &x0, &y0, &z0, &x1, &y1, &z1)==1) {
//printf("706-within clipping\n");
pvert1.mat41[0]=x0;
pvert1.mat41[1]=y0;
pvert1.mat41[2]=z0;
pvert1.mat41[3]=1;
pvert2.mat41[0]=x1;
pvert2.mat41[1]=y1;
pvert2.mat41[2]=z1;
pvert2.mat41[3]=1;
//printf("716-x0:%f, y0:%f, z0:%f, x1:%f, y1:%f, z1:%f\n", x0, y0, z0, x1, y1, z1);
if(perspflag==0) { /* if not a perpective projection, use ortho */
//printf("717-ortho\n");
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT,viewport);
width = abs(viewport[2]-viewport[0]);
height = abs(viewport[3]-viewport[1]);
Mult_end(&orth, &pvert1, &vertex1); /* calculate 2d coordinates */
Mult_end(&orth, &pvert2, &vertex2);
draw_line(vertex1.mat41[0], vertex1.mat41[1],
vertex2.mat41[0], vertex2.mat41[1]);
printflag=0;
}
else { /* if a perspective projection, use persp */
//printf("765-persp\n");
permat1.mat41[0]=x0/abs(z0); /* divide by abs(z) to account for */
permat1.mat41[1]=y0/abs(z0); /* z=1 assumption in gtPerspective */
permat1.mat41[2]=1.0;
permat1.mat41[3]=(float)(1/abs(z0));
permat2.mat41[0]=x1/abs(z1);
permat2.mat41[1]=y1/abs(z1);
permat2.mat41[2]=1.0;
permat2.mat41[3]=(float)(1/abs(z1));
Mult_end(&perspect, &permat1, &vertex1);
Mult_end(&perspect, &permat2, &vertex2);
//BOBBY
//printf(" Vertex 1: %f %f\n Vertex 2: %f %f\n",vertex1.mat41[0],vertex1.mat41[1],vertex2.mat41[0],vertex2.mat41[1]);
draw_line(vertex1.mat41[0],
vertex1.mat41[1],
vertex2.mat41[0],
vertex2.mat41[1]);
printflag=0; /* set counter=0 to look for another set of vertices */
}
}
else{
printf("3D.c:717-outside clipping\n");
printflag=0; /* if vertices beyond clipping plane, reset counter */
}
}
}
float determinant(mat3 matrix);
float determinant(mat3 matrix){
float result = matrix.mat[0][0]*matrix.mat[1][1]*matrix.mat[2][2]+
matrix.mat[0][1]*matrix.mat[1][2]*matrix.mat[2][0]+
matrix.mat[0][2]*matrix.mat[1][0]*matrix.mat[2][1]-
matrix.mat[0][2]*matrix.mat[1][1]*matrix.mat[2][0]-
matrix.mat[0][1]*matrix.mat[1][0]*matrix.mat[2][2]-
matrix.mat[0][0]*matrix.mat[1][2]*matrix.mat[2][1]
;
return result;
}
float vec3Mul(vec3 a, vec3 b){
float result;
result= a.mat[0] *b.mat[0]+a.mat[1] *b.mat[1]+a.mat[2] *b.mat[2];
return result;
}
vec3 vec3Cross(vec3 a, vec3 b){
vec3 result;
result.mat[0] = a.mat[1]*b.mat[2] - a.mat[2]*b.mat[1];
result.mat[1] = a.mat[2]*b.mat[0] - a.mat[0]*b.mat[2];
result.mat[2] = a.mat[0]*b.mat[1] - a.mat[1]*b.mat[0];
return result;
}
vec3 vec3NumMul(float num, vec3 a){
vec3 result;
result.mat[0] = a.mat[0]*num;
result.mat[1] = a.mat[1]*num;
result.mat[2] = a.mat[2]*num;
return result;
}
vec3 vec3Add(vec3 a, vec3 b){
vec3 result;
result.mat[0] = a.mat[0] +b.mat[0];
result.mat[1] = a.mat[1] +b.mat[1];
result.mat[2] = a.mat[2] +b.mat[2];
return result;
}
vec3 vec3Minus(vec3 a, vec3 b){
vec3 result;
result.mat[0] = a.mat[0] -b.mat[0];
result.mat[1] = a.mat[1] -b.mat[1];
result.mat[2] = a.mat[2] -b.mat[2];
return result;
}
vec3 normal(vec3 a){
float norm = sqrt(a.mat[0]*a.mat[0]+a.mat[1]*a.mat[1]+a.mat[2]*a.mat[2]);
vec3 result = {{0,0,0}};
if(norm!=0){
result = {{a.mat[0]/norm, a.mat[1]/norm, a.mat[2]/norm}};
}
return result;
}
/*--------------------End of gtVertex3f----------------------------------*/
#ifdef __cplusplus
}
#endif