如何解决框图不正确
我在绘制盒子形状时有问题(很长)
创建矩阵的代码(其中宽度和高度是窗口的大小):
GL.Enable(EnableCap.DepthTest);
GL.DepthMask(true);
GL.DepthFunc(DepthFunction.Lequal);
float aspect = (float)width / (float)height;
matrix = Matrix4.CreateTranslation(0,-100) * // 100 > 51
Matrix4.CreatePerspectiveOffCenter(-aspect,aspect,1,-1,1000);
绘制框的代码:
public void Box(double w,double h,double d)
{
float x1 = (float)-w / 2f; float x2 = (float)w / 2f;
float y1 = (float)-h / 2f; float y2 = (float)h / 2f;
float z1 = (float)-d / 2f; float z2 = (float)d / 2f;
var vertbuffer = new Vector3[]
{
// front
new Vector3(x1,y1,z1),new Vector3(x2,y2,new Vector3(x1,// right
new Vector3(x2,z2),// back
new Vector3(x2,// left
new Vector3(x1,// top
new Vector3(x1,// bottom
new Vector3(x1,};
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.normalArray);
GL.VertexPointer(3,VertexPointerType.Float,Vector3.SizeInBytes,vertbuffer);
GL.normalPointer(normalPointerType.Float,normalBuffer);
GL.Color4(fillColor);
GL.DrawArrays(PrimitiveType.Quads,vertbuffer.Length);
GL.Color4(strokeColor);
GL.linewidth((float)strokeWeight);
for (int i = 0; i < vertbuffer.Length / 4; i++)
{
GL.DrawArrays(PrimitiveType.LineLoop,i * 4,4);
}
}
它看起来确实像是前四边形的平局:
我尝试减小Matrix4.CreatePerspectiveOffCenter
的depthFar参数,但这不起作用。
编辑:我已经更新了代码。
解决方法
所有不在近平面和远平面之间的几何都将被裁剪。
立方体的大小是100、100、100。到近平面的距离是1,到远平面的距离是1000。
因此,相机与立方体中心之间的距离必须至少为51(d / 2 +近= 100/2 +1),否则立方体的前部将被{{ 3}}。
OpenGL坐标系是Viewing frustum系统。 x轴指向右侧,y轴指向上方。 z轴由x轴和y轴的right handed计算,并指向视图之外。因此,几何形状必须沿负z方向移动。
对于大小为100x100x100的立方体,我建议使用以下投影和转换:
float aspect = (float)width/(float)height;
matrix = Matrix4.CreateTranslation(0,-100) * // 100 > 51
Matrix4.CreatePerspectiveOffCenter(-aspect,aspect,1,-1,1000);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。