Use backtick code blocks for syntax highlighting
This commit is contained in:
parent
8ed9dea394
commit
f658bfbb40
203 changed files with 26546 additions and 25801 deletions
186
50-04.md
186
50-04.md
|
|
@ -35,103 +35,107 @@ but it will certainly reduce confusion!
|
|||
|
||||
**LISTING 50.2 L50-2.C**
|
||||
|
||||
/* Matrix arithmetic functions.
|
||||
Tested with Borland C++ in the small model. */
|
||||
```c
|
||||
/* Matrix arithmetic functions.
|
||||
Tested with Borland C++ in the small model. */
|
||||
|
||||
/* Matrix multiplies Xform by SourceVec, and stores the result in
|
||||
DestVec. Multiplies a 4x4 matrix times a 4x1 matrix; the result
|
||||
is a 4x1 matrix, as follows:
|
||||
-- -- -- -- -- --
|
||||
| | | 4 | | 4 |
|
||||
| 4x4 | X | x | = | x |
|
||||
| | | 1 | | 1 |
|
||||
-- -- -- -- -- -- */
|
||||
void XformVec(double Xform[4][4], double * SourceVec,
|
||||
double * DestVec)
|
||||
{
|
||||
int i,j;
|
||||
/* Matrix multiplies Xform by SourceVec, and stores the result in
|
||||
DestVec. Multiplies a 4x4 matrix times a 4x1 matrix; the result
|
||||
is a 4x1 matrix, as follows:
|
||||
-- -- -- -- -- --
|
||||
| | | 4 | | 4 |
|
||||
| 4x4 | X | x | = | x |
|
||||
| | | 1 | | 1 |
|
||||
-- -- -- -- -- -- */
|
||||
void XformVec(double Xform[4][4], double * SourceVec,
|
||||
double * DestVec)
|
||||
{
|
||||
int i,j;
|
||||
|
||||
for (i=0; i<4; i++) {
|
||||
DestVec[i] = 0;
|
||||
for (j=0; j<4; j++)
|
||||
DestVec[i] += Xform[i][j] * SourceVec[j];
|
||||
}
|
||||
}
|
||||
for (i=0; i<4; i++) {
|
||||
DestVec[i] = 0;
|
||||
for (j=0; j<4; j++)
|
||||
DestVec[i] += Xform[i][j] * SourceVec[j];
|
||||
}
|
||||
}
|
||||
|
||||
/* Matrix multiplies SourceXform1 by SourceXform2 and stores the
|
||||
result in DestXform. Multiplies a 4x4 matrix times a 4x4 matrix;
|
||||
the result is a 4x4 matrix, as follows:
|
||||
-- -- -- -- -- --
|
||||
| | | | | |
|
||||
| 4x4 | X | 4x4 | = | 4x4 |
|
||||
| | | | | |
|
||||
-- -- -- -- -- -- */
|
||||
void ConcatXforms(double SourceXform1[4][4], double SourceXform2[4][4],
|
||||
double DestXform[4][4])
|
||||
{
|
||||
int i,j,k;
|
||||
/* Matrix multiplies SourceXform1 by SourceXform2 and stores the
|
||||
result in DestXform. Multiplies a 4x4 matrix times a 4x4 matrix;
|
||||
the result is a 4x4 matrix, as follows:
|
||||
-- -- -- -- -- --
|
||||
| | | | | |
|
||||
| 4x4 | X | 4x4 | = | 4x4 |
|
||||
| | | | | |
|
||||
-- -- -- -- -- -- */
|
||||
void ConcatXforms(double SourceXform1[4][4], double SourceXform2[4][4],
|
||||
double DestXform[4][4])
|
||||
{
|
||||
int i,j,k;
|
||||
|
||||
for (i=0; i<4; i++) {
|
||||
for (j=0; j<4; j++) {
|
||||
DestXform[i][j] = 0;
|
||||
for (k=0; k<4; k++)
|
||||
DestXform[i][j] += SourceXform1[i][k] * SourceXform2[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (i=0; i<4; i++) {
|
||||
for (j=0; j<4; j++) {
|
||||
DestXform[i][j] = 0;
|
||||
for (k=0; k<4; k++)
|
||||
DestXform[i][j] += SourceXform1[i][k] * SourceXform2[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**LISTING 50.3 L50-3.C**
|
||||
|
||||
/* Transforms convex polygon Poly (which has PolyLength vertices),
|
||||
performing the transformation according to Xform (which generally
|
||||
represents a transformation from object space through world space
|
||||
to view space), then projects the transformed polygon onto the
|
||||
screen and draws it in color ???Color. Also updates the extent of the
|
||||
rectangle (EraseRect) that's used to erase the screen later.
|
||||
Tested with Borland C++ in the small model. */
|
||||
#include "polygon.h"
|
||||
```c
|
||||
/* Transforms convex polygon Poly (which has PolyLength vertices),
|
||||
performing the transformation according to Xform (which generally
|
||||
represents a transformation from object space through world space
|
||||
to view space), then projects the transformed polygon onto the
|
||||
screen and draws it in color ???Color. Also updates the extent of the
|
||||
rectangle (EraseRect) that's used to erase the screen later.
|
||||
Tested with Borland C++ in the small model. */
|
||||
#include "polygon.h"
|
||||
|
||||
void XformAndProjectPoly(double Xform[4][4], struct Point3 * Poly,
|
||||
int PolyLength, int Color)
|
||||
{
|
||||
int i;
|
||||
struct Point3 XformedPoly[MAX_POLY_LENGTH];
|
||||
struct Point ProjectedPoly[MAX_POLY_LENGTH];
|
||||
struct PointListHeader Polygon;
|
||||
void XformAndProjectPoly(double Xform[4][4], struct Point3 * Poly,
|
||||
int PolyLength, int Color)
|
||||
{
|
||||
int i;
|
||||
struct Point3 XformedPoly[MAX_POLY_LENGTH];
|
||||
struct Point ProjectedPoly[MAX_POLY_LENGTH];
|
||||
struct PointListHeader Polygon;
|
||||
|
||||
/* Transform to view space, then project to the screen */
|
||||
for (i=0; i<PolyLength; i++) {
|
||||
/* Transform to view space */
|
||||
XformVec(Xform, (double *)&Poly[i], (double *)&XformedPoly[i]);
|
||||
/* Project the X & Y coordinates to the screen, rounding to the
|
||||
nearest integral coordinates. The Y coordinate is negated to
|
||||
flip from view space, where increasing Y is up, to screen
|
||||
space, where increasing Y is down. Add in half the screen
|
||||
width and height to center on the screen */
|
||||
ProjectedPoly[i].X = ((int) (XformedPoly[i].X/XformedPoly[i].Z *
|
||||
PROJECTION_RATIO*(SCREEN_WIDTH/2.0)+0.5))+SCREEN_WIDTH/2;
|
||||
ProjectedPoly[i].Y = ((int) (XformedPoly[i].Y/XformedPoly[i].Z *
|
||||
-1.0 * PROJECTION_RATIO * (SCREEN_WIDTH / 2.0) + 0.5)) +
|
||||
SCREEN_HEIGHT/2;
|
||||
/* Appropriately adjust the extent of the rectangle used to
|
||||
erase this page later */
|
||||
if (ProjectedPoly[i].X > EraseRect[NonDisplayedPage].Right)
|
||||
if (ProjectedPoly[i].X < SCREEN_WIDTH)
|
||||
EraseRect[NonDisplayedPage].Right = ProjectedPoly[i].X;
|
||||
else EraseRect[NonDisplayedPage].Right = SCREEN_WIDTH;
|
||||
if (ProjectedPoly[i].Y > EraseRect[NonDisplayedPage].Bottom)
|
||||
if (ProjectedPoly[i].Y < SCREEN_HEIGHT)
|
||||
EraseRect[NonDisplayedPage].Bottom = ProjectedPoly[i].Y;
|
||||
else EraseRect[NonDisplayedPage].Bottom = SCREEN_HEIGHT;
|
||||
if (ProjectedPoly[i].X < EraseRect[NonDisplayedPage].Left)
|
||||
if (ProjectedPoly[i].X > 0)
|
||||
EraseRect[NonDisplayedPage].Left = ProjectedPoly[i].X;
|
||||
else EraseRect[NonDisplayedPage].Left = 0;
|
||||
if (ProjectedPoly[i].Y < EraseRect[NonDisplayedPage].Top)
|
||||
if (ProjectedPoly[i].Y > 0)
|
||||
EraseRect[NonDisplayedPage].Top = ProjectedPoly[i].Y;
|
||||
else EraseRect[NonDisplayedPage].Top = 0;
|
||||
}
|
||||
/* Draw the polygon */
|
||||
DRAW_POLYGON(ProjectedPoly, PolyLength, Color, 0, 0);
|
||||
}
|
||||
/* Transform to view space, then project to the screen */
|
||||
for (i=0; i<PolyLength; i++) {
|
||||
/* Transform to view space */
|
||||
XformVec(Xform, (double *)&Poly[i], (double *)&XformedPoly[i]);
|
||||
/* Project the X & Y coordinates to the screen, rounding to the
|
||||
nearest integral coordinates. The Y coordinate is negated to
|
||||
flip from view space, where increasing Y is up, to screen
|
||||
space, where increasing Y is down. Add in half the screen
|
||||
width and height to center on the screen */
|
||||
ProjectedPoly[i].X = ((int) (XformedPoly[i].X/XformedPoly[i].Z *
|
||||
PROJECTION_RATIO*(SCREEN_WIDTH/2.0)+0.5))+SCREEN_WIDTH/2;
|
||||
ProjectedPoly[i].Y = ((int) (XformedPoly[i].Y/XformedPoly[i].Z *
|
||||
-1.0 * PROJECTION_RATIO * (SCREEN_WIDTH / 2.0) + 0.5)) +
|
||||
SCREEN_HEIGHT/2;
|
||||
/* Appropriately adjust the extent of the rectangle used to
|
||||
erase this page later */
|
||||
if (ProjectedPoly[i].X > EraseRect[NonDisplayedPage].Right)
|
||||
if (ProjectedPoly[i].X < SCREEN_WIDTH)
|
||||
EraseRect[NonDisplayedPage].Right = ProjectedPoly[i].X;
|
||||
else EraseRect[NonDisplayedPage].Right = SCREEN_WIDTH;
|
||||
if (ProjectedPoly[i].Y > EraseRect[NonDisplayedPage].Bottom)
|
||||
if (ProjectedPoly[i].Y < SCREEN_HEIGHT)
|
||||
EraseRect[NonDisplayedPage].Bottom = ProjectedPoly[i].Y;
|
||||
else EraseRect[NonDisplayedPage].Bottom = SCREEN_HEIGHT;
|
||||
if (ProjectedPoly[i].X < EraseRect[NonDisplayedPage].Left)
|
||||
if (ProjectedPoly[i].X > 0)
|
||||
EraseRect[NonDisplayedPage].Left = ProjectedPoly[i].X;
|
||||
else EraseRect[NonDisplayedPage].Left = 0;
|
||||
if (ProjectedPoly[i].Y < EraseRect[NonDisplayedPage].Top)
|
||||
if (ProjectedPoly[i].Y > 0)
|
||||
EraseRect[NonDisplayedPage].Top = ProjectedPoly[i].Y;
|
||||
else EraseRect[NonDisplayedPage].Top = 0;
|
||||
}
|
||||
/* Draw the polygon */
|
||||
DRAW_POLYGON(ProjectedPoly, PolyLength, Color, 0, 0);
|
||||
}
|
||||
```
|
||||
|
|
|
|||
Loading…
Reference in a new issue