opengl – Handbook calc of perspective projection and getting level on the display screen

opengl – Handbook calc of perspective projection and getting level on the display screen

[ad_1]

I am want to attract the crimson rect on the primary vertex of grey platform.

To start with, I draw a grey platform with a perspective projection and set it utilizing gluPerspective:

float fov = 69;
float aspectRatio = 960.0f / 480.0f;
float zNear = 0.1;
float zFar = 1000.0;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, aspectRatio, zNear, zFar);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
    0.0f, 0.0f, 5.0f,
    0.0f, 0.0f, 0.0f,
    0.0f, 1.0f, 0.0f
);

gray platform render

Then, I’ll set up an orthographic projection on the entire display screen. The rendering begins from the upper-left nook of the window. My activity is to attract a crimson sq. on the level the place the primary vertex of the grey platform begins to be drawn:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 960, 480, 0, 1, -1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

drawRedRect(Undertaking(glm::vec3(-1.0f, -1.0f, -1.0f), fov, aspectRatio, zNear, zFar));

Code for Undertaking perform:

float fov = tan(fovy / 2.0F);
glm::mat4x4 id = glm::mat4x4(
    1.0f, 0, 0, 0,
    0, 1, 0, 0,
    0, 0, 1, 0,
    0, 0, 0, 1
);
glm::mat4x4 projectionMatrix = glm::mat4x4(
    fov / aspectRatio, 0.0f, 0.0f, 0.0f,
    0.0f, fov, 0.0f, 0.0f,
    0.0f, 0.0f, (zFar + zNear) / (zNear - zFar), (2.0f * zFar * zNear) / (zNear - zFar),
    0.0f, 0.0f, -1.0f, 0.0f
);

glm::mat4x4 lookAt = glm::lookAt(
    glm::vec3(0.0f, 0.0f, 5.0f),
    glm::vec3(0.0f, 0.0f, 0.0f),
    glm::vec3(0.0f, 1.0f, 0.0f)
);

glm::vec4 platformVector4 = glm::vec4(platformVector.x, platformVector.y, platformVector.z, 1.0f);
platformVector4 = projectionMatrix * lookAt * platformVector4;
//platformVector4 /= platformVector4.w;

float halfWidth = 960.0f / 2.0f;
float halfHeight = 480.0f / 2.0f;

glm::vec2 screenProjectedVector = glm::vec2(
    halfWidth + (platformVector4.x * 960.0f) * 0.5f,
    halfHeight - (platformVector4.y * 480.0f) * 0.5f
);

return screenProjectedVector;

I additionally tried to make use of float fov = tan(glm::radians(fovy / 2.0F));, nevertheless it did not get profitable consequence.

In response to the system utilized by gluPerspective, I calculated the fov and the angle projection matrix:

projection matrix formula

Results of my code:

result 2

However I want the sq. to be on the primary vertex, and it is unclear the place.

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply