Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimizing a pinhole camera rendering system
    primarykey
    data
    text
    <p>I'm making a software rasterizer for school, and I'm using an unusual rendering method instead of traditional matrix calculations. It's based on a <a href="http://en.wikipedia.org/wiki/Pinhole_camera_model" rel="nofollow noreferrer">pinhole camera</a>. I have a few points in 3D space, and I convert them to 2D screen coordinates by taking the distance between it and the camera and normalizing it</p> <pre><code>Vec3 ray_to_camera = (a_Point - plane_pos).Normalize(); </code></pre> <p>This gives me a directional vector towards the camera. I then turn that direction into a ray by placing the ray's origin on the camera and performing a ray-plane intersection with a plane slightly behind the camera.</p> <pre><code>Vec3 plane_pos = m_Position + (m_Direction * m_ScreenDistance); float dot = ray_to_camera.GetDotProduct(m_Direction); if (dot &lt; 0) { float time = (-m_ScreenDistance - plane_pos.GetDotProduct(m_Direction)) / dot; // if time is smaller than 0 the ray is either parallel to the plane or misses it if (time &gt;= 0) { // retrieving the actual intersection point a_Point -= (m_Direction * ((a_Point - plane_pos).GetDotProduct(m_Direction))); // subtracting the plane origin from the intersection point // puts the point at world origin (0, 0, 0) Vec3 sub = a_Point - plane_pos; // the axes are calculated by saying the directional vector of the camera // is the new z axis projected.x = sub.GetDotProduct(m_Axis[0]); projected.y = sub.GetDotProduct(m_Axis[1]); } } </code></pre> <p>This works wonderful, but I'm wondering: can the algorithm be made any faster? Right now, for every triangle in the scene, I have to calculate three normals. </p> <pre><code>float length = 1 / sqrtf(GetSquaredLength()); x *= length; y *= length; z *= length; </code></pre> <p>Even with a fast reciprocal square root approximation (<code>1 / sqrt(x)</code>) that's going to be very demanding.</p> <p>My questions are thus:<br> Is there a good way to approximate the three normals?<br> What is this rendering technique called?<br> Can the three vertex points be approximated using the normal of the centroid? ((v0 + v1 + v2) / 3)</p> <p>Thanks in advance.</p> <p>P.S. "You will build a fully functional software rasterizer in the next seven weeks with the help of an expert in this field. Begin." I ADORE my education. :)</p> <p><strong>EDIT:</strong></p> <pre><code>Vec2 projected; // the plane is behind the camera Vec3 plane_pos = m_Position + (m_Direction * m_ScreenDistance); float scale = m_ScreenDistance / (m_Position - plane_pos).GetSquaredLength(); // times -100 because of the squared length instead of the length // (which would involve a squared root) projected.x = a_Point.GetDotProduct(m_Axis[0]).x * scale * -100; projected.y = a_Point.GetDotProduct(m_Axis[1]).y * scale * -100; return projected; </code></pre> <p>This returns the correct results, however the model is now independent of the camera position. :(</p> <p>It's a lot shorter and faster though!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload