示例#1
0
 public warp_Scene(int w, int h)
 {
     width          = w;
     height         = h;
     renderPipeline = new warp_RenderPipeline(this, w, h);
     defaultCamera.setScreensize(w, h);
 }
        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();
        }