Basic3D Tutorials 3D
  .

 

1 3

 

, , (). : , , , . . , , . , ?

 

- , . , . , , . 1, - 2 - 3. . :

 

xslope = (max_x-min_x)/(max_y-min_y)

x = min_x

 

max_y y

{

x = x + xslope

y = y + 1

putpixel(x y color)

}

 

C:

 

void drawline(int x1, int y1, int x2, int y2, char color)

{

int side = 0;

float temp = x1;

float xslope = (x2-x1)/(y2-y1);

 

if (y1 >= y2)

{

side = 1;

x1 = x2;

x2 = temp;

temp = y1;

y1 = y2;

y2 = temp;

}

for (y=y1;y<=y2;y++)

{

putpixel(x,y,color);

x1 += xslope;

}

}

 

, , y . , (0,0) (0,10), , , (0,0) (10,1). , . . , , , .

 

, ? -, . , , , , . , , . , . 1 , 2 , 3 - . . , , , . , , , , , . ( ), . , , min_y max_y. , , , . , , - . , xpos. , [200] ( 320x200). X Y. :

 

 

 

4

 

, , y. , , x , y ( [200] , x y ). , . y , .. y xpos[y]. , . ? , , ( ) ( ). xpos, xpos[200][2]. , , , . , drawline ( ), drawedge ( ):

 

void drawedge(int x1, int y1, int x2, int y2, char color)

{

int side = 0;

float temp = x1;

float xslope = (x2-x1)/(y2-y1);

if (y1 >= y2)

{

side = 1;

x1 = x2;

x2 = temp;

temp = y1;

y1 = y2;

y2 = temp;

}

for (y=y1;y<=y2;y++)

{

xpos[y][side]=x;

x1 += xslope;

}

}

 

xpos. xpos :

 

drawedge(x1,y1,x2,y2);

drawedge(x2,y2,x3,y3);

drawedge(x3,y3,x1,y1);

 

, xpos. , , xpos [y] [0] xpos [y] [1]. 320x200x256, ( : , , ):

 

void BufPixel( int x, int y, int color )

{

*(char*)(0xa0000+(x)+((y)*320))=color;

}

 

void hline(int x1,int x2,int y,char color)

{

int i;

int wherey;

if (x1>=x2)

{

i=x1;

x1=x2;

x2=i;

}

wherey=y*320;

for (i=x1;i<=x2;i++)

BufPixel(i, wherey, color );

}

 

, . min_y max_y. :

 

void drawedge(int x1, int y1, int x2, int y2)

{

int side = 0;

float temp = x1;

float xslope = (x2-x1)/(y2-y1);

if (y1 >= y2)

{

side = 1;

x1 = x2;

x2 = temp;

temp = y1;

y1 = y2;

y2 = temp;

}

for (y=y1;y<=y2;y++)

{

xedge[y][side] = x1;

x1 += xslope;

}

}

 

void hline(int x1,int x2,int y,char color)

{

int i;

int wherey;

if (x1>=x2)

{

i=x1;

x1=x2;

x2=i;

}

wherey=y*320;

for (i=x1;i<=x2;i++)

BufPixel(i, wherey, color);

}

 

void polygon(int x1,int y1,int x2,int y2,int x3,int y3,char c)

{

int minx,maxx;

 

drawedge(x1,y1,x2,y2);

drawedge(x2,y2,x3,y3);

drawedge(x3,y3,x1,y1);

miny=y1;

if (miny > y2)

miny=y2;

if (miny > y3)

miny=y3;

maxy=y1;

if (maxy < y2)

maxy=y2;

if (maxy < y3)

maxy=y3;

minx=x1;

if (minx > x2)

minx=x2;

if (minx > x3)

minx=x3;

maxx=x1;

if (maxx < x2)

maxx=x2;

if (maxx < x3)

maxx=x3;

if (maxy==miny)

hline(minx,maxx,miny,c);

else

for (y=miny;y<=maxy;y++)

hline(xedge[y][0],xedge[y][1],y,c);

};

 

, !

 

 

 

5

 

. z , , . z- .

 

. , hline . , , . , , , , , , z-. ?

 

, zpos. Zpos , xpos, z x, xpos . . , xpos z-. ? , , hline, xpos [y] [0] xpos [y] [1] . , , , .

 

, . , zedge, , , z , . :

 

void drawedge(int x1, int y1, int z1, int x2, int y2, int z2)

 

z, - zslope. zslope , z y. , zslope (z2-z1) / (y2-y1). , zpos. :

 

for (y=y1;y<y2;y++)

{

x += xslope;

z += zslope;

xpos[y][side] = x;

zpos[y][side] = z;

}

 

:

 

void drawedge(int x1, int y1, int z1, int x2, int y2, int z2, char color)

{

int side = 0;

float temp = x1;

float xslope = (x2-x1)/(y2-y1);

float zslope = (z2-z1)/(y2-y1);

if (y1 >= y2)

{

side = 1;

x1 = x2;

x2 = temp;

temp = y1;

y1 = y2;

y2 = temp;

temp = z1;

z1 = z2;

z2 = temp;

}

for (y=y1;y<=y2;y++)

{

xpos[y][side]=x;

x1 += xslope;

zpos[y][side]=z;

z1 += zslope;

}

}

 

hline. , drawedge, z , zslope, , z . , zslope (z2-z1) / (x2-x1). , x. :

 

void drawline(int x1,int x2,int y,char c)

{

int i;

float zinc;

float z1,z2;

char ch;

 

z1 = zpos[y][0];

z2 = zpos[y][1];

if (x1>x2)

{

i=x1;

x1=x2;

x2=i;

i=z1;

z1=z2;

z2=i;

}

zinc = (z2-z1)/(x2-x1);

for (i=x1;i<=x2;i++)

{

BufPixel(i,y,z1+c);

z1 += zinc;

}

}

 

BufPixel , 0 255, 0 , .. , 255 , .. . , .

 

. . , :

 

void drawedge(int x1, int y1, int z1, int x2, int y2, int z2, char color)

{

int side = 0;

float temp = x1;

float xslope = (x2-x1)/(y2-y1);

float zslope = (z2-z1)/(y2-y1);

if (y1 >= y2)

{

side = 1;

x1 = x2;

x2 = temp;

temp = y1;

y1 = y2;

y2 = temp;

temp = z1;

z1 = z2;

z2 = temp;

}

for (y=y1;y<=y2;y++)

{

xpos[y][side]=x;

x1 += xslope;

zpos[y][side]=z;

z1 += zslope;

}

}

 

void hline(int x1,int x2,int y,char c)

{

int i;

float zinc;

float z1,z2;

char ch;

 

z1 = zpos[y][0];

z2 = zpos[y][1];

if (x1>x2)

{

i=x1;

x1=x2;

x2=i;

i=z1;

z1=z2;

z2=i;

}

zinc = (z2-z1)/(x2-x1);

for (i=x1;i<=x2;i++)

{

BufPixel(i,y,z1+c);

z1 += zinc;

}

}

 

void gpoly(int x1,int y1,int z1,int x2,int y2,int z2,int x3,int y3,int z3,char c)

 

{

if (y1<=0)

y1=0;

if (y2<=0)

y2=0;

if (y3<=0)

y3=0;

 

if (y1>=200)

y1=199;

if (y2>=200)

y2=199;

if (y3>=200)

y3=199;

 

if (x1<=0)

x1=0;

if (x2<=0)

x2=0;

if (x3<=0)

x3=0;

 

if (x1>=320)

x1=319;

if (x2>=320)

x2=319;

if (x3>=320)

x3=319;

 

drawedge(x1,y1,z1,x2,y2,z2);

drawedge(x2,y2,z2,x3,y3,z3);

drawedge(x3,y3,z3,x1,y1,z1);

 

miny=y1;

if (miny > y2)

miny=y2;

if (miny > y3)

miny=y3;

maxy=y1;

if (maxy < y2)

maxy=y2;

if (maxy < y3)

maxy=y3;

 

for (y=miny;y<=maxy;y++)

hline(xedge[y][0],xedge[y][1],y,c);

};

 

Z- ( )

 

 

Z- , . z ( ). z-. , , z- z-, z- , z-, . , , .

 

, - zbuffer. , . , Z-. z , , . hline: z- z- , , , Z-. :

 

for (y=y1;y<y2;y++)

{

x += xslope;

z += zslope;

xpos[y][side] = x;

if (z < zbuffer[x+y*320])

{

zpos[y][side] = z;

zbuffer[x+y*320] = z;

}

}

 

Z-, . z, Z- .

 

for (y=y1;y<y2;y++)

{

x += xslope;

z += zslope;

z1 = 1/z;

xpos[y][side] = x;

if (z1 > zbuffer[x+y*320])

{

zpos[y][side] = z;

zbuffer[x+y*320] = z1;

}

}

 

 

. , , . , . U V , , . , U V . , UV . UV ?

 

U V X Y, XY , . , UV XY , ? , . - , , 256x256. , , UV . , UV , , . Uslope Vslope. , : uslope = (u2-u1) / (x2-x1) vslope = (v2-v1) / (x2-x1). , :

void drawedge

(int x1,int y1,float u1,float v1,int x2,int y2,float u2,float v2)

 

. uedge [200] [2] vedge [200] [2]. , u v , . , x, , , u v , , . . , ?

void drawedge

(int x1,int y1,float u1,float v1,int x2,int y2,float u2,float v2)

{

int side = 0;

float temp = x1;

float xslope;

float uslope;

float vslope;

if (y1 >= y2)

{

side = 1;

x1 = x2;

x2 = temp;

temp = y1;

y1 = y2;

y2 = temp;

temp = u1;

u1 = u2;

u2 = temp;

temp = v1;

v1 = v2;

v2 = temp;

}

if (y2-y1==0)

{

xslope = (x2-x1);

uslope = (u2-u1);

vslope = (v2-v1);

}

else

{

xslope = (x2-x1)/(y2-y1);

uslope = (u2-u1)/(y2-y1);

vslope = (v2-v1)/(y2-y1);

}

for (y=y1;y<=y2;y++)

{

xedge[y][side] = x1;

x1 += xslope;

uedge[y][side] = u1;

u1 += uslope;

vedge[y][side] = v1;

v1 += vslope;

}

}

 

, U V , . , xedge [200] [0] xedge [200] [1] texture [mapx + mapy*256]. Mapx mapy u vedge [200] [0], , u v . , hline , , hline . , . , , :

 

void drawedge

(int x1,int y1,float u1,float v1,int x2,int y2,float u2,float v2)

{

int side = 0;

float temp = x1;

float xslope;

float uslope;

float vslope;

if (y1 >= y2)

{

side = 1;

x1 = x2;

x2 = temp;

temp = y1;

y1 = y2;

y2 = temp;

temp = u1;

u1 = u2;

u2 = temp;

temp = v1;

v1 = v2;

v2 = temp;

}

if (y2-y1==0)

{

xslope = (x2-x1);

uslope = (u2-u1);

vslope = (v2-v1);

}

else

{

xslope = (x2-x1)/(y2-y1);

uslope = (u2-u1)/(y2-y1);

vslope = (v2-v1)/(y2-y1);

}

for (y=y1;y<=y2;y++)

{

xedge[y][side] = x1;

x1 += xslope;

uedge[y][side] = u1;

u1 += uslope;

vedge[y][side] = v1;

v1 += vslope;

}

}

 

void texturepoly(int x1,int y1,float u1,float v1,

int x2,int y2,float u2,float v2,

int x3,int y3,float u3,float v3)

{

 

int minx,maxx;

int i;

float mapx, mapy;

float uslope,vslope;

float temp;

int x;

int xpos1,xpos2;

 

texturedrawedge(x1,y1,u1,v1,x2,y2,u2,v2);

texturedrawedge(x2,y2,u2,v2,x3,y3,u3,v3);

texturedrawedge(x3,y3,u3,v3,x1,y1,u1,v1);

 

miny=y1;

if (miny > y2)

miny=y2;

if (miny > y3)

miny=y3;

maxy=y1;

if (maxy < y2)

maxy=y2;

if (maxy < y3)

maxy=y3;

minx=x1;

if (minx > x2)

minx=x2;

if (minx > x3)

minx=x3;

maxx=x1;

if (maxx < x2)

maxx=x2;

if (maxx < x3)

maxx=x3;

 

for (y=miny;y<=maxy;y++)

{

if (xedge[y][1]-xedge[y][0] != 0)

{

uslope = (uedge[y][1]-uedge[y][0])/(xedge[y][1]-xedge[y][0]);

vslope = (vedge[y][1]-vedge[y][0])/(xedge[y][1]-xedge[y][0]);

}

else

{

uslope = (uedge[y][1]-uedge[y][0]);

vslope = (vedge[y][1]-vedge[y][0]);

}

mapxf=uedge[y][0];

mapyf=vedge[y][0];

for (x=xedge[y][0];x<=xedge[y][1];x++)

{

BufPixel(x,y,texture[(mapx)+(mapy<<8)]);

mapx += uslope;

mapy += vslope;

}

}

}

 

 

, . , , . , , (lightmap). , , . , , ( 3D).

 

, , . , , . , U V . , . , , .

 

, , - , . , , . , . , . , , . , , , . , , Y, , . ? , . . , . , Y , . , , . , . . ( : , , , , , ).

 

? . . , , , , , . , , . , , , , , , . , uv, , , . , , ( uv).

 

, . , , 1 -1. ? x y , z .

 

 

 

, , , . , , c . , -. .

 

, , . 1 2, 2 3, , . , , , 1 2, 1 3. , , , , 2 3. , , . , .

 

xslope1 = (x2-x1)/(y2-y1);

xslope2 = (x3-x1)/(y3-y1);

leftx=x1;

rightx=x1;

for (y=y1;y< y2;y++)

{

leftx+=xslope1;

rightx+=xslope2;

for (x=leftx;x<=rightx;x++)

BufPixel(x,y,color);

}

 

- . , , x1 x2 . , :

 

if (leftx>rightx)

{

temp = leftx;

leftx = rightx;

rightx = leftx;

}

 

, y1 , y2 y2 y3. , x1 x y3. , :

 

if (y1>y2)

{

temp = y1;

y1 = y2;

y2 = temp;

temp = x1;

x1 = x2;

x2 = temp;

}

if (y2>y3)

{

temp = y2;

y2 = y3;

y3 = tem3;

temp = x2;

x2 = x3;

x3 = temp;

}

if (y1>y3)

{

temp = y1;

y1 = y3;

y3 = temp;

temp = x1;

x1 = x3;

x3 = temp;

}

 

, , . , , slopex2x3.

 

The coding place

 

PMG  18 2004 (c)