将图像绘制为平行四边形
大家好,
我目前处于个人挑战项目上.仅使用GDI在C#中构建3D引擎(以学习c#和3d数学).我目前已经构建了一个引擎,该引擎可以添加3D对象,并带有基本的人脸绘制(也具有使用平方反比的基本照明:)).我一直坚持在给定的面/三角形上绘制纹理.
如何将图像绘制为平行四边形/三角形,以使原始图像的边角与平行四边形相匹配? :confused:
(请记住,这些是2D坐标,而不是根据它们计算得出的3D坐标)
谢谢所有帮助我解决此问题的人:) :)
示例代码情况
--------------------------------------
Hi all,
I am currently stuck on a personal challenge project. To build a 3D engine in C# using only the GDI (to learn c# & 3d math to go with). I have currently built an engine that can have 3D objects added to it, with basic face drawing (Also has basic lighting using inverse square law :) ). I am stuck at drawing a texture to a given face/triangle.
How could I draw an Image to a parallelogram/triangle such that the corners of the original image match that of a parallelogram? :confused:
(Bare in mind that these are the 2D co-ordinates not the 3D ones they were calculated from)
Thank you all who help me solve this :) :)
example code situation
--------------------------------------
Image Texture; //From engine source
Point[] DrawPoints; //4 points of parallelogram // 3 points of triangle (either)
Graphics Drawer;
//TODO - Draw Texture to map to the 3/4 points of draw points
这将是一个很长的答案.我发布了代码,我认为这很简单,但是如果您觉得需要进一步的说明,我会发布评论.
我想您可以通过高级数学更轻松(更快速)地完成此操作,但是我敢打赌,您将从下面的简单代码中获得主要思想.我警告您,这段代码非常慢;尝试使用小图像.由于缺乏时间,因此根本没有对代码进行优化,您可以通过更改几行来轻松提高速度,但是我留给您:)
This is going to be a long answer. I post the code, which I think is rather straightforward, but if you feel you need further explanations, I will post comments.
I guess you can accomplish this more easily (and faster) with advanced math, but I bet you''ll get the main idea from the simple code below. I warn you that this code is very slow; try it with small images. Due to lack of time this code is not optimized at all, you can easily improve speed by changing a few lines, but I leave it to you :)
protected override void OnPaint(PaintEventArgs e)
{
if (Texture == null)
{
return;
}
Bitmap bmp = (Bitmap)Texture;
int width = Texture.Width;
int height = Texture.Height;
using (Graphics g = this.CreateGraphics())
{
g.Clear(Color.Black);
Point[] Dwu = this.DivideEdge(DrawPoints[0], DrawPoints[1], width, false);
Point[] Dwd = this.DivideEdge(DrawPoints[2], DrawPoints[3], width, true);
Point[] Dhl = this.DivideEdge(DrawPoints[3], DrawPoints[0], height, false);
Point[] Dhr = this.DivideEdge(DrawPoints[1], DrawPoints[2], height, true);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Point a = GetIntersection(Dwu[x], Dwd[x], Dhl[y], Dhr[y]);
g.DrawRectangle(new Pen(bmp.GetPixel(x, y)), a.X, a.Y, 1, 1);
}
}
}
}
Point[] DivideEdge(Point a, Point b, int points, bool reverse)
{
Point[] d = new Point[points + 1];
for (int i = 0; i <= points; i++)
{
d[i] = new Point(a.X + (b.X - a.X) * i / points, a.Y + (b.Y - a.Y) * i / points);
}
if (reverse)
{
Point[] d2 = new Point[points + 1];
for (int j = 0; j <= points; j++)
{
d2[points - j] = d[j];
}
return d2;
}
else
{
return d;
}
}
public Point GetIntersection(Point p1, Point p2, Point p3, Point p4)
{
int x1 = p1.X;int x2 = p2.X;int x3 = p3.X;int x4 = p4.X;
int y1 = p1.Y;int y2 = p2.Y;int y3 = p3.Y;int y4 = p4.Y;
float d = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
if (d == 0f)
{
return new Point(0, 0);
}
float na = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
float nb = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3);
float ua = na / d;
float ub = nb / d;
return new Point((int)(x1 + ua * (x2 - x1)), (int)(y1 + ua * (y2 - y1)));
}
希望对您有帮助,祝您好运!
Hope this helps, good luck!
我找到了解决问题的方法.
我在网络上的某个地方偶然发现了此应用程序
http://www.vcskicks.com/image-distortion.php [
I have found a solution to the problem.
I stumbled on this application somewhere on the web
http://www.vcskicks.com/image-distortion.php[^]
The source code in the example does exactly what I asked. I am now implementing a solution :) :) thank you to all those who have helped.