简体   繁体   中英

How can I define vertices of a plane that is always parallel to the camera?

I am trying to draw a rectangle (basically a plane) that is always parallel to the camera. I want to restrict plane to a certain size (lets say height = 2 and width = 2 units). However, I do not understand how to set position to the vertices such that rectangle will always be parallel to the camera.

First I am calculating camera normal (direction) using:

glm::normalize(mPosition - mTargetPos); // normal

and then I am using point-normal equation to define the plane:

normal = (A, B, C)
point = (a, b, c)  // this point will serve as a center to the plane
A(x−a)+B(y−b)+C(z−c) = 0

Question: How can I define vertices of the plane?

  1. Take some normilized vector UpDir for up direction (it can be UpDir=(0,1,0) or UpDir=(0,0,1) depending on your coordinate system, or it can be computed somehow)
  2. Compute cross product SideDir of the normal and the UpDir .
  3. Now you can use the SideDir and UpDir as basis for your plane's coordinate system, and compute four vertices of the rectangle as point+width*SideDir+height*UpDir , point+width*SideDir-height*UpDir , point-width*SideDir-height*UpDir , point-width*SideDir+height*UpDir

I recommend to define the points in view space. Finally transform the points by the inverse view matrix.
In view space the points are parallel to the view if they have the same z coordinate. The z-coordinate has to be negative and its amount has to greater than the distance to the near plane and less than the distance to the far plane:

near < -z < far

Compute the view matrix ( view_mat ) and define the points in view sapce:

glm::mat4 view_mat = glm::lookAt(mPosition, mTargetPos, mUp);
float z = 

glm::vec3 pt1View(x1, y1, z);
glm::vec3 pt2View(x2, y2, z);
// [...]

Transform the points from view space to world space:

glm::mat4 inverse_view_mat = glm::inverse(view_mat);

glm::vec3 pt1World = glm::vec3(inverse_view_mat * glm::vec4(pt1View, 1.0f));
glm::vec3 pt2World = glm::vec3(inverse_view_mat * glm::vec4(pt2View, 1.0f));
// [...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM