26 สิงหาคม 2553

การคำนวณพิกัด 2D หรือระนาบ เมื่อกำลังใช้งานพิกัด 3D ใน OpenGL แบบ Perspective Projection

การคำนวณพิกัด 2D หรือระนาบ เมื่อกำลังใช้งานพิกัด 3D ใน OpenGL แบบ Perspective

เมื่อเราตั้งค่า Projection Matrix เป็น 3D แบบ Perspective แล้ว

เรายังสามารถใช้การวาดรูปแบบ 2D ได้โดยสามารถคำนวณหา

ความกว้าง ความสูง และ พิกัดของ 2D ได้จาก Code การ Projection

glMatrixMode (GL_PROJECTION);                                    //set the matrix to projection
    glLoadIdentity ();
    gluPerspective (30.32, 4.0/3.0, 50.0, 300.0);               //set the perspective (angle of sight, width/height, near, far   )

และอีกค่าหนึ่งที่นำมาคิดคือ มุมมองของกล้อง

/*
* camera position, x,y,z, looking at x,y,z, Up Positions of the camera
*/
gluLookAt
(
    0.0, 0.0, 110.0,/* camera */
    0.0, 0.0, 0.0,    /* lens towards */
    0.0, 1.0, 0.0   /* up-vector */
);

จาก Code ข้างบนเราจะได้คำนวนหา

ขอบเขตความกว้าง [XWmin, XWmax]

ขอบเขตความยาว [YWmin, YWmax]

ได้ดังนี้

fovyH = 30.32;
aspect = 4/3;
Zvp = 110;
Zprp = 0;

height = 2 * Zvp * tan(deg2rad(fovyH/2));
YWmin = Zprp - height/2
YWmax = Zprp + height/2

width = aspect * height;
XWmin = Zprp - width/2
XWmax = Zprp + width/2

ไม่มีความคิดเห็น: