Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for (int Y = minY; Y <= maxY; Y++) {
- for (int X = minX; X <= maxX; X++) {
- float lambda1, lambda2, lambda3;
- getBarycentricCoordinate(v1, v2, v3, X, Y, &lambda1, &lambda2, &lambda3);
- if ((0.0f <= lambda1 && 0.0f <= lambda2 && 0.0f <= lambda3)) {
- float zValue = lambda1 * zA + lambda2 * zB + lambda3 * zC;
- 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
- float lamb1, lamb2, lamb3;
- getBarycentricCoordinate(eyeCoorV1, eyeCoorV2, eyeCoorV3, X, Y, &lamb1, &lamb2, &lamb3);
- Vector4f incidentPoint = lamb1 * eyeCoorV1 + lamb2 * eyeCoorV2 + lamb3 * eyeCoorV3;
- Vector4f l = lightInEyeCoor - incidentPoint;
- //transforming normals to eye coordinates
- Vector4f normal1 = modelViewMatrix * objects[k].vertexNormal[vn1Index];
- Vector4f normal2 = modelViewMatrix * objects[k].vertexNormal[vn2Index];
- Vector4f normal3 = modelViewMatrix * objects[k].vertexNormal[vn3Index];
- Vector4f normal = lamb1 * normal1 + lamb2 * normal2 + lamb3 * normal3; // lamb1,lamb2 lamb3 are getting weird results
- l.normalize();
- normal.normalize();
- Colorf color = pointLight.LdLs * objects[k].KaKd * fmaxf(l*normal, 0.0f); // add diffuse color
- color = color + ambientLight * objects[k].KaKd; // add ambient color
- Vector4f r = 2 * (normal * l) * normal - l;
- Vector4f v = -incidentPoint; // because eye is at (0, 0, 0) in eye coordinate, view vector is just negative incidentPoint
- r.normalize();
- v.normalize();
- //specular lighting
- color = color + pointLight.LdLs * objects[k].Ks * powf(fmax(r*v, 0.0f), objects[k].Ks.A);
- raster.pixels[Y * rasterWidth + X] = floatColorToIntColor(color);
- raster.zBuffer[Y * rasterWidth + X] = zValue;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment