public void render(warp_Camera cam) { rasterizer.rebuildReferences(this); warp_Math.clearBuffer(zBuffer, zFar); //System.Array.Copy(screen.zBuffer,0,zBuffer,0,zBuffer.Length); if (scene.environment.background != null) { screen.drawBackground(scene.environment.background, 0, 0, screen.width, screen.height); } else { screen.clear(scene.environment.bgcolor); } cam.setScreensize(screen.width, screen.height); scene.prepareForRendering(); emptyQueues(); // Project warp_Matrix m = warp_Matrix.multiply(cam.getMatrix(), scene.matrix); warp_Matrix nm = warp_Matrix.multiply(cam.getNormalMatrix(), scene.normalmatrix); warp_Matrix vertexProjection, normalProjection; warp_Object obj; warp_Triangle t; warp_Vertex v; warp_Material objmaterial; const double log2inv = 1.4426950408889634073599246810019; int w = screen.width; int h = screen.height; int minx; int miny; int maxx; int maxy; for (int id = 0; id < scene.objects; ++id) { obj = scene.wobject[id]; objmaterial = obj.material; if (objmaterial == null) { continue; } if (!obj.visible) { continue; } if (objmaterial.opaque && objmaterial.reflectivity == 0) { continue; } vertexProjection = obj.matrix.getClone(); normalProjection = obj.normalmatrix.getClone(); vertexProjection.transform(m); normalProjection.transform(nm); minx = int.MaxValue; miny = int.MaxValue; maxx = int.MinValue; maxy = int.MinValue; for (int i = 0; i < obj.vertices; ++i) { v = obj.fastvertex[i]; v.project(vertexProjection, normalProjection, cam); v.clipFrustrum(w, h); if (minx > v.x) { minx = v.x; } if (maxx < v.x) { maxx = v.x; } if (miny > v.y) { miny = v.y; } if (maxy < v.y) { maxy = v.y; } } maxx -= minx; maxy -= miny; if (maxy > maxx) { maxx = maxy + 1; } else { maxx++; } obj.projectedmaxMips = (int)Math.Ceiling((Math.Log(maxx) * log2inv)); obj.cacheMaterialData(); if (objmaterial.opaque) { rasterizer.loadFastMaterial(obj); for (int i = 0; i < obj.triangles; ++i) { t = obj.fasttriangle[i]; t.project(normalProjection); if (t.clipFrustrum(w, h)) { rasterizer.render(t); } } } else { for (int i = 0; i < obj.triangles; ++i) { t = obj.fasttriangle[i]; t.project(normalProjection); if (t.clipFrustrum(w, h)) { transparentQueue.Add(t); } } } } //screen.lockImage(); warp_Triangle[] tri; obj = null; tri = getTransparentQueue(); if (tri != null) { transparentQueue.Clear(); for (int i = 0; i < tri.GetLength(0); i++) { if (obj != tri[i].parent) { obj = tri[i].parent; rasterizer.loadFastMaterial(obj); } rasterizer.render(tri[i]); } } //screen.unlockImage(); }
public float EstimateBoxProjectedArea(warp_Vector pos, warp_Vector size, warp_Matrix rotation) { warp_Matrix om = new warp_Matrix(); om.scale(size.x, size.y, size.z); om.transform(rotation); float xmax; float ymax; /* * if (defaultCamera.isOrthographic) * { * xmax = Math.Abs(om.m00); * ymax = Math.Abs(om.m22); * if (xmax < 1f || ymax < 1f) * return -1; * return xmax * ymax / (width * height); * } */ om.m03 = pos.x; om.m13 = pos.y; om.m23 = pos.z; warp_Vector side; warp_Vector v; float xmin; float ymin; warp_Matrix m = warp_Matrix.multiply(defaultCamera.getMatrix(), matrix); om.transform(m); float zmin; side = new warp_Vector(-1f, -1f, -1f); v = side.transform(om); xmin = v.x; xmax = xmin; ymin = v.y; ymax = ymin; zmin = v.z; side.x = 1f; v = side.transform(om); if (xmin > v.x) { xmin = v.x; } else if (xmax < v.x) { xmax = v.x; } if (ymin > v.y) { ymin = v.y; } else if (ymax < v.y) { ymax = v.y; } if (zmin > v.z) { zmin = v.z; } side.x = -1f; side.y = 1f; v = side.transform(om); if (xmin > v.x) { xmin = v.x; } else if (xmax < v.x) { xmax = v.x; } if (ymin > v.y) { ymin = v.y; } else if (ymax < v.y) { ymax = v.y; } if (zmin > v.z) { zmin = v.z; } side.x = 1f; v = side.transform(om); if (xmin > v.x) { xmin = v.x; } else if (xmax < v.x) { xmax = v.x; } if (ymin > v.y) { ymin = v.y; } else if (ymax < v.y) { ymax = v.y; } if (zmin > v.z) { zmin = v.z; } side.x = -1f; side.y = -1f; side.z = 1f; v = side.transform(om); if (xmin > v.x) { xmin = v.x; } else if (xmax < v.x) { xmax = v.x; } if (ymin > v.y) { ymin = v.y; } else if (ymax < v.y) { ymax = v.y; } if (zmin > v.z) { zmin = v.z; } side.x = 1f; v = side.transform(om); if (xmin > v.x) { xmin = v.x; } else if (xmax < v.x) { xmax = v.x; } if (ymin > v.y) { ymin = v.y; } else if (ymax < v.y) { ymax = v.y; } if (zmin > v.z) { zmin = v.z; } side.x = -1f; side.y = 1f; v = side.transform(om); if (xmin > v.x) { xmin = v.x; } else if (xmax < v.x) { xmax = v.x; } if (ymin > v.y) { ymin = v.y; } else if (ymax < v.y) { ymax = v.y; } if (zmin > v.z) { zmin = v.z; } side.x = 1f; v = side.transform(om); if (xmin > v.x) { xmin = v.x; } else if (xmax < v.x) { xmax = v.x; } if (ymin > v.y) { ymin = v.y; } else if (ymax < v.y) { ymax = v.y; } if (zmin > v.z) { zmin = v.z; } xmax -= xmin; ymax -= ymin; if (xmax < 1f || ymax < 1f) { return(-1); } return(xmax * ymax / (width * height)); }