public void calculateRaster(float width, float height, bool isNDC) { polygons_raster = new List <RPolygon>(); foreach (RPolygon poly in polygons_proj) { RVertex vertex1 = new RVertex(poly.vertex0); vertex1.position.x = isNDC ? (poly.v0.x + 1.0f) / 2.0f * width : poly.v0.x * width; vertex1.position.y = isNDC ? (poly.v0.y + 1.0f) / 2.0f * height : poly.v0.y * height; vertex1.textureCoordinates.x *= vertex1.position.z; vertex1.textureCoordinates.y *= vertex1.position.z; RVertex vertex2 = new RVertex(poly.vertex1); vertex2.position.x = isNDC ? (poly.v1.x + 1.0f) / 2.0f * width : poly.v1.x * width; vertex2.position.y = isNDC ? (poly.v1.y + 1.0f) / 2.0f * height : poly.v1.y * height; vertex2.textureCoordinates.x *= vertex2.position.z; vertex2.textureCoordinates.y *= vertex2.position.z; RVertex vertex3 = new RVertex(poly.vertex2); vertex3.position.x = isNDC ? (poly.v2.x + 1.0f) / 2.0f * width : poly.v2.x * width; vertex3.position.y = isNDC ? (poly.v2.y + 1.0f) / 2.0f * height : poly.v2.y * height; vertex3.textureCoordinates.x *= vertex3.position.z; vertex3.textureCoordinates.y *= vertex3.position.z; polygons_raster.Add(new RPolygon(vertex1, vertex2, vertex3, poly.rasterType, poly.texture)); } }
public void calculateWorld() { polygons_world = new List <RPolygon>(); foreach (RPolygon poly in polygons_model) { RVertex vertex1 = new RVertex(poly.vertex[0]); vertex1.position = new Vec3f(world * poly.v0); RVertex vertex2 = new RVertex(poly.vertex[1]); vertex2.position = new Vec3f(world * poly.v1); RVertex vertex3 = new RVertex(poly.vertex[2]); vertex3.position = new Vec3f(world * poly.v2); polygons_world.Add(new RPolygon(vertex1, vertex2, vertex3, poly.rasterType, poly.texture)); } }
public void calculateModel() { polygons_model = new List <RPolygon>(); foreach (RPolygon poly in polygons) { RVertex vertex1 = new RVertex(poly.vertex0); vertex1.position = new Vec3f(model * poly.v0); RVertex vertex2 = new RVertex(poly.vertex1); vertex2.position = new Vec3f(model * poly.v1); RVertex vertex3 = new RVertex(poly.vertex2); vertex3.position = new Vec3f(model * poly.v2); polygons_model.Add(new RPolygon(vertex1, vertex2, vertex3, poly.rasterType, poly.texture)); } }
public void calculateProj(float nearPlane = 0.1f, float farPlane = 1000.0f) { polygons_proj = new List <RPolygon>(); foreach (RPolygon poly in polygons_view) { Vec3f nearPlaneNormal = new Vec3f(0.0f, 0.0f, 1.0f); Vec3f nearPlaneP0 = new Vec3f(0.0f, 0.0f, nearPlane); float sideOfPoint1 = nearPlaneNormal.dot(poly.vertex0.position - nearPlaneP0); float sideOfPoint2 = nearPlaneNormal.dot(poly.vertex1.position - nearPlaneP0); float sideOfPoint3 = nearPlaneNormal.dot(poly.vertex2.position - nearPlaneP0); if (sideOfPoint1 <= 0 || sideOfPoint2 <= 0 || sideOfPoint3 <= 0) { poly.visible = false; } if (poly.visible) { RVertex vertex1 = new RVertex(poly.vertex0); vertex1.position = new Vec3f(proj * poly.v0); RVertex vertex2 = new RVertex(poly.vertex1); vertex2.position = new Vec3f(proj * poly.v1); RVertex vertex3 = new RVertex(poly.vertex2); vertex3.position = new Vec3f(proj * poly.v2); RPolygon newPolygon = new RPolygon(vertex1, vertex2, vertex3, poly.rasterType, poly.texture); float cosTheta = newPolygon.normalCalc().dot(GlobalDirection.forward3f); newPolygon.visible = cosTheta >= 0.0f ? false : true; if (newPolygon.visible) { polygons_proj.Add(newPolygon); } } } }
public void calculateView(bool needToSort) { polygons_view = new List <RPolygon>(); foreach (RPolygon poly in polygons_world) { RVertex vertex1 = new RVertex(poly.vertex[0]); vertex1.position = new Vec3f(view * poly.v0); RVertex vertex2 = new RVertex(poly.vertex[1]); vertex2.position = new Vec3f(view * poly.v1); RVertex vertex3 = new RVertex(poly.vertex[2]); vertex3.position = new Vec3f(view * poly.v2); RPolygon newPolygon = new RPolygon(vertex1, vertex2, vertex3, poly.rasterType, poly.texture); polygons_view.Add(newPolygon); } if (needToSort) { polygons_view = polygons_view.OrderBy(p => p.center).ToList(); } }
public void calculateProj() { polygons_proj = new List <RPolygon>(); foreach (RPolygon poly in polygons_view) { RVertex vertex1 = new RVertex(poly.vertex[0]); vertex1.position = new Vec3f(proj * poly.v0); RVertex vertex2 = new RVertex(poly.vertex[1]); vertex2.position = new Vec3f(proj * poly.v1); RVertex vertex3 = new RVertex(poly.vertex[2]); vertex3.position = new Vec3f(proj * poly.v2); RPolygon newPolygon = new RPolygon(vertex1, vertex2, vertex3, poly.rasterType, poly.texture); float cosTheta = newPolygon.normalCalc().dot(GlobalDirection.forward3f); newPolygon.visible = cosTheta >= 0.0f ? false : true; if (newPolygon.visible) { polygons_proj.Add(newPolygon); } } }
public void calculateAll(float nearPlane, float farPlane, float width, float height, bool isNDC, bool backFaceCull = true) { polygons_raster = new List <RPolygon>(); object sync = new object(); Parallel.ForEach <RPolygon>( polygons, poly => { Mat4f mwv = model; mwv = mwv * world; mwv = mwv * view; RVertex vertex1 = new RVertex(poly.vertex0); vertex1.position = new Vec3f(mwv * poly.v0); RVertex vertex2 = new RVertex(poly.vertex1); vertex2.position = new Vec3f(mwv * poly.v1); RVertex vertex3 = new RVertex(poly.vertex2); vertex3.position = new Vec3f(mwv * poly.v2); Vec3f nearPlaneNormal = new Vec3f(0.0f, 0.0f, 1.0f); Vec3f nearPlaneP0 = new Vec3f(0.0f, 0.0f, nearPlane); Vec3f farPlaneNormal = new Vec3f(0.0f, 0.0f, -1.0f); Vec3f farPlaneP0 = new Vec3f(0.0f, 0.0f, farPlane); float sideOfPoint1near = nearPlaneNormal.dot(vertex1.position - nearPlaneP0); float sideOfPoint2near = nearPlaneNormal.dot(vertex2.position - nearPlaneP0); float sideOfPoint3near = nearPlaneNormal.dot(vertex3.position - nearPlaneP0); float sideOfPoint1far = farPlaneNormal.dot(vertex1.position - farPlaneP0); float sideOfPoint2far = farPlaneNormal.dot(vertex2.position - farPlaneP0); float sideOfPoint3far = farPlaneNormal.dot(vertex3.position - farPlaneP0); if ((sideOfPoint1near >= 0 && sideOfPoint2near >= 0 && sideOfPoint3near >= 0) && (sideOfPoint1far >= 0 && sideOfPoint2far >= 0 && sideOfPoint3far >= 0)) { vertex1.position = new Vec3f(proj * vertex1.position); vertex2.position = new Vec3f(proj * vertex2.position); vertex3.position = new Vec3f(proj * vertex3.position); vertex1.position.x = isNDC ? (vertex1.position.x + 1.0f) * 0.5f * width : vertex1.position.x * width; vertex1.position.y = isNDC ? (vertex1.position.y + 1.0f) * 0.5f * height : vertex1.position.y * height; vertex2.position.x = isNDC ? (vertex2.position.x + 1.0f) * 0.5f * width : vertex2.position.x * width; vertex2.position.y = isNDC ? (vertex2.position.y + 1.0f) * 0.5f * height : vertex2.position.y * height; vertex3.position.x = isNDC ? (vertex3.position.x + 1.0f) * 0.5f * width : vertex3.position.x * width; vertex3.position.y = isNDC ? (vertex3.position.y + 1.0f) * 0.5f * height : vertex3.position.y * height; RPolygon newPolygon = new RPolygon(vertex1, vertex2, vertex3, poly.rasterType, poly.texture); float cosTheta = newPolygon.normalCalc().dot(GlobalDirection.forward3f); newPolygon.visible = cosTheta >= 0.0f ? false : true; if (!backFaceCull || newPolygon.visible) { newPolygon.sort(); lock (sync) { polygons_raster.Add(newPolygon); } } } } ); }