Advertisement
Guest User

Backface culling example npg

a guest
Apr 23rd, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. //Create a vector representing the vector from the iDraw to the camera
  2. Vector toCam;
  3.  
  4. toCam = _position - _camera->GetPosition();
  5. //Normalize toCam so it represents only the direction
  6. toCam.Normalize();
  7.  
  8.  
  9. //Create a vector representing the heading of a directional light
  10. Vector toLight(4, NULL);
  11. toLight(0) = 1;
  12. toLight(1) = 1.8;
  13. toLight(2) = 1;
  14.  
  15. //Normalize toLight
  16. toLight.Normalize();
  17.  
  18. //Create a vector representing the rotated face normal
  19. Vector faceNormal;
  20.  
  21. //For each face
  22. for (int i = 0; i < _numFaces; i++)
  23. {
  24. //Rotate _faceNormal at i and store
  25. faceNormal = _faceNormals[i] * _coordinateSystem;
  26.  
  27. //Determine color
  28. //The dot product of two vectors is the magnitude of the vectors multiplied by the cosine of the angle between them
  29. //Find the dot product of the face normal with the vector representing the direction of the light
  30. float dotProd = toLight.DotProduct(faceNormal);
  31.  
  32. //The dot product between two normalized vectors is the cosine of the angle between them
  33. //Cosine is bound between -1 and 1. When the angle is 0, PI or any equivilent value the surface must be directly lit and cosine will equal +-1
  34. //When the angle is PI/2 or any other multiple of the surface is recieving no direct light and should be dark and cosine will equal 0
  35.  
  36. //Consider the angle between the faceNormal and the toCam vector
  37. //When cosine of the angle is positive, the face shouldn't be drawn as it does not face you- it is facing the same direction as you
  38. //The dot product of the face normal with the normalized toCam vector will return the cosine of the angle between them
  39. //This is called Backface Culling
  40. //IF the face is facing us
  41. if (toCam.DotProduct(faceNormal) <= 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement