我为正在玩的3D图形制作了这个三角形填充物,但它只能在平面上画一条线吗? 用于sdl2。
void ftri(SDL_Renderer* r,int x1,int y1,int x2,int y2,int x3,int y3)
{
int yt,xt;
if(y1 > y2){yt = y1;y1 = y2;y2 = yt;xt = x1;x1 = x2;x2 = xt;}
if(y2 > y3){yt = y2;y2 = y3;y3 = yt;xt = x2;x2 = x3;x3 = xt;}
if(y3 > y1){yt = y3;y3 = y1;y1 = yt;xt = x3;x3 = x1;x1 = xt;}
float s1,s2;
s1 = (float(x3)-float(x1))/(float(y3)-float(y1));
if(y1==y2)
SDL_RenderDrawLine(r,x1,y1,x2,y1);
else
{
s2 = (float(x2)-float(x1))/(float(y2)-float(y1));
for (int y = y1;y < y2;y++)
SDL_RenderDrawLine(r,s1*float(y),y,s2*float(y),y);
}
if(y2==y3)
SDL_RenderDrawLine(r,x2,y2,x3,y2);
else
{
s2 = (float(x3)-float(x2))/(float(y3)-float(y2));
for (int y = y2;y < y3;y++)
SDL_RenderDrawLine(r,s1*float(y),y,s2*float(y),y);
}
}
答案 0 :(得分:0)
这更好吗?
void ftri(SDL_Renderer* r,int x1,int y1,int x2,int y2,int x3,int y3)
{
int yt,xt;
if(y1 < y2){yt = y1;y1 = y2;y2 = yt;xt = x1;x1 = x2;x2 = xt;}
if(y2 < y3){yt = y2;y2 = y3;y3 = yt;xt = x2;x2 = x3;x3 = xt;}
if(y3 < y1){yt = y3;y3 = y1;y1 = yt;xt = x3;x3 = x1;x1 = xt;}
float s1,s2;
s1 = (float(y3)-float(y1))/(float(x3)-float(x1));
if(y1==y2)
SDL_RenderDrawLine(r,x1,y1,x2,y1);
else
{
s2 = (float(y2)-float(y1))/(float(x2)-float(x1));
for (int y = y1;y < y2;y++)
SDL_RenderDrawLine(r,float(y-y1)/s1+x1,y,float(y-y1)/s2+x1,y);
}
if(y2==y3)
SDL_RenderDrawLine(r,x2,y2,x3,y2);
else
{
s2 = (float(y3)-float(y2))/(float(x3)-float(x2));
for (int y = y2;y < y3;y++)
SDL_RenderDrawLine(r,float(y-y1)/s1+x1,y,float(y-y2)/s2+x2,y);
}
}