0
votes

Im programming with opengl (lwjgl) and building my own mini-library. My Camera, which takes the projection type, builds its projection matrix like this:

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);

}

So far so good. Now, the VBO input, so the raw meshes of objects - for example a quad - i keep in the normalized dimension, so values in the range of [ -1 | 1 ]. If i want to scale it, i scale the model matrix to a value, and to move it i translate the model matrix. My Problem is: That are all relative values. If i say "matrix.scale(0.5f, 0.5f, 0.5f)" the object will take the half of its previous size. But what if for example i want to have an object with 500 pixel width? How can i calculate this? Or if i want the object to be Screen.width / heiht, and x = -Screen.width * 0.5 and y = -Screen.height * 0.5 - so an object wich fills out the screen and has his position in the upper left corner of the screen? I have to calculate something with help of the projection matrix - right? But how?

1

1 Answers

1
votes

Not exactly what you are asking, but maybe it helps. With this code the camera is set so that screen coordinates match world coordinates and the lower left corner of the viewport is zero for X and Y. Orthogonal projection.

        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;
        }

As for your question: You would have to put your desired screen coordinates trough the inverse of the view-projection matrix. And you would have to add the depth information on the way as you are going from 2D to 3D. I am sorry, but I cant help you with the math for that.