Guest User

Untitled

a guest
Feb 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. for (int Y = minY; Y <= maxY; Y++) {
  2. for (int X = minX; X <= maxX; X++) {
  3. float lambda1, lambda2, lambda3;
  4. getBarycentricCoordinate(v1, v2, v3, X, Y, &lambda1, &lambda2, &lambda3);
  5. if ((0.0f <= lambda1 && 0.0f <= lambda2 && 0.0f <= lambda3)) {
  6. float zValue = lambda1 * zA + lambda2 * zB + lambda3 * zC;
  7. if (abs(zValue) < abs(raster.zBuffer[Y * rasterWidth + X])) {// If a point is closer to the eye, which is at (0, 0, 0) then draw. zBuffer check
  8. float lamb1, lamb2, lamb3;
  9. getBarycentricCoordinate(eyeCoorV1, eyeCoorV2, eyeCoorV3, X, Y, &lamb1, &lamb2, &lamb3);
  10.  
  11. Vector4f incidentPoint = lamb1 * eyeCoorV1 + lamb2 * eyeCoorV2 + lamb3 * eyeCoorV3;
  12. Vector4f l = lightInEyeCoor - incidentPoint;
  13.  
  14. //transforming normals to eye coordinates
  15. Vector4f normal1 = modelViewMatrix * objects[k].vertexNormal[vn1Index];
  16. Vector4f normal2 = modelViewMatrix * objects[k].vertexNormal[vn2Index];
  17. Vector4f normal3 = modelViewMatrix * objects[k].vertexNormal[vn3Index];
  18. Vector4f normal = lamb1 * normal1 + lamb2 * normal2 + lamb3 * normal3; // lamb1,lamb2 lamb3 are getting weird results
  19.  
  20. l.normalize();
  21. normal.normalize();
  22.  
  23. Colorf color = pointLight.LdLs * objects[k].KaKd * fmaxf(l*normal, 0.0f); // add diffuse color
  24. color = color + ambientLight * objects[k].KaKd; // add ambient color
  25. Vector4f r = 2 * (normal * l) * normal - l;
  26. Vector4f v = -incidentPoint; // because eye is at (0, 0, 0) in eye coordinate, view vector is just negative incidentPoint
  27. r.normalize();
  28. v.normalize();
  29. //specular lighting
  30. color = color + pointLight.LdLs * objects[k].Ks * powf(fmax(r*v, 0.0f), objects[k].Ks.A);
  31. raster.pixels[Y * rasterWidth + X] = floatColorToIntColor(color);
  32. raster.zBuffer[Y * rasterWidth + X] = zValue;
  33. }
  34.  
  35. }
  36. }
  37. }
Add Comment
Please, Sign In to add comment