Java 正交投影-使对象适合屏幕?
我用opengl(lwjgl)编程并建立自己的迷你库。我的相机采用投影类型,其投影矩阵如下所示:Java 正交投影-使对象适合屏幕?,java,opengl,matrix,lwjgl,projection,Java,Opengl,Matrix,Lwjgl,Projection,我用opengl(lwjgl)编程并建立自己的迷你库。我的相机采用投影类型,其投影矩阵如下所示: this.aspect = (float) Display.getWidth() / (float) Display.getHeight(); this.top = (float) (Math.tan(Math.toRadians(fov) / 2)); this.bottom = -top; this.right = top * aspect; this.left = -right; if(typ
this.aspect = (float) Display.getWidth() / (float) Display.getHeight();
this.top = (float) (Math.tan(Math.toRadians(fov) / 2));
this.bottom = -top;
this.right = top * aspect;
this.left = -right;
if(type == AGLProjectionType.PERSPECTIVE){
float aspect = 800.0f / 600.0f;
final double f = (1.0 / Math.tan(Math.toRadians(fov / 2.0)));
projection = new Matrix4f();
projection.m00 = (float) (f / aspect);
projection.m11 = (float) f;
projection.m22 = (far + near) / (near - far);
projection.m23 = -1;
projection.m32 = (2 * far + near) / (near - far);
projection.m33 = 0;
}
else if(type == AGLProjectionType.ORTHOGONAL){
projection.m00 = 2 / (right - left);
projection.m03 = -(right + left) / (right - left);
projection.m11 = 2 / (top - bottom);
projection.m13 = -(top + bottom) / (top - bottom);
projection.m22 = -2 / (far - near);
}
到目前为止还不错。
现在,VBO输入,因此对象的原始网格-例如四边形-我保持在标准化维度中,因此值在[-1 | 1]范围内。
如果要缩放它,我将模型矩阵缩放为一个值,要移动它,我将转换模型矩阵。
我的问题是:这些都是相对值。如果我说“matrix.scale(0.5f,0.5f,0.5f)”,对象将取其先前大小的一半。但如果我想要一个500像素宽的对象呢?我怎么计算这个呢?或者如果我希望对象是Screen.width/height,x=-Screen.width*0.5和y=-Screen.height*0.5,那么一个对象填充屏幕并将其位置放在屏幕的左上角?我必须借助投影矩阵来计算一些东西-对吗?但是怎么做呢?这不完全是你所要求的,但可能会有所帮助。使用此代码设置相机,以便屏幕坐标与世界坐标匹配,并且对于X和Y正交投影,视口的左下角为零
case TwoD:
{
projectionMatrix.resetToZero();
projectionMatrix._11 = 2.0f/(float)this.viewPort.Width;
projectionMatrix._22 = 2.0f/(float)this.viewPort.Height;
projectionMatrix._33 = -2.0f/(this.farClip-this.nearClip);
projectionMatrix._43 = -1* this.nearClip;
projectionMatrix._44 = 1.0f;
float tx = -0.5f* (float)this.viewPort.Width;
float ty = -0.5f* (float)this.viewPort.Height;
float tz = this.nearClip +0.1f; //why +0.1f >> so that an object with Z = 0 is still displayed
viewMatrix.setIdetity();
viewMatrix._22 = 1.0f;
viewMatrix._41 = tx;
viewMatrix._42 = ty;
viewMatrix._43 = -tz;
break;
}
至于您的问题:您必须通过视图投影矩阵的倒数输入所需的屏幕坐标。在从2D到3D的过程中,必须添加深度信息。很抱歉,我帮不了你算数