Esempio n. 1
0
		public void project (warp_Matrix vertexProjection, warp_Matrix normalProjection, warp_Camera camera)
			// Projects this vertex into camera space
		{
			pos2 = pos.transform (vertexProjection);
			n2 = n.transform (normalProjection);

			float fact;
			if (camera.isOrthographic)
			{
				x = (int)(pos2.x * (camera.screenscale / camera.orthoViewWidth) + (camera.screenwidth >> 1));
				y = (int)(-pos2.y * (camera.screenscale / camera.orthoViewHeight) + (camera.screenheight >> 1));
			} else
			{
				fact = camera.screenscale / camera.fovfact / ((pos2.z > 0.1) ? pos2.z : 0.1f);
				x = (int)(pos2.x * fact + (camera.screenwidth >> 1));
				y = (int)(-pos2.y * fact + (camera.screenheight >> 1));
			}

			z = (int)(65536f * pos2.z);
			sw = -(pos2.z);
			nx = (int)(n2.x * 127 + 127);
			ny = (int)(n2.y * 127 + 127);
			if (parent.material == null)
				return;
			if (parent.material.texture == null)
				return;
			tx = (int)((float)parent.material.texture.width * u);
			ty = (int)((float)parent.material.texture.height * v);
		}
Esempio n. 2
0
        void rebuildMatrices()
        {
            if (!needsRebuild)
            {
                return;
            }
            needsRebuild = false;

            warp_Vector forward, up, right;

            forward = warp_Vector.sub(lookat, pos);
            up      = new warp_Vector(0f, 1f, 0f);
            right   = warp_Vector.getNormal(up, forward);
            up      = warp_Vector.getNormal(forward, right);

            forward.normalize();
            up.normalize();
            right.normalize();

            normalmatrix = new warp_Matrix(right, up, forward);
            normalmatrix.rotate(0, 0, rollfactor);
            matrix = normalmatrix.getClone();
            matrix.shift(pos.x, pos.y, pos.z);
            normalmatrix = normalmatrix.inverse();
            matrix       = matrix.inverse();
        }
Esempio n. 3
0
        public void project(warp_Matrix vertexProjection, warp_Matrix normalProjection, warp_Camera camera)
        // Projects this vertex into camera space
        {
            pos2 = pos.transform(vertexProjection);
            n2   = n.transform(normalProjection);

            float fact;

            if (camera.isOrthographic)
            {
                x = (int)(pos2.x * (camera.screenscale / camera.orthoViewWidth) + (camera.screenwidth >> 1));
                y = (int)(-pos2.y * (camera.screenscale / camera.orthoViewHeight) + (camera.screenheight >> 1));
            }
            else
            {
                fact = camera.screenscale / camera.fovfact / ((pos2.z > 0.1) ? pos2.z : 0.1f);
                x    = (int)(pos2.x * fact + (camera.screenwidth >> 1));
                y    = (int)(-pos2.y * fact + (camera.screenheight >> 1));
            }

            z  = (int)(65536f * pos2.z);
            sw = -(pos2.z);
            nx = (int)(n2.x * 127 + 127);
            ny = (int)(n2.y * 127 + 127);
            if (parent.material == null)
            {
                return;
            }
            if (parent.material.texture == null)
            {
                return;
            }
            tx = (int)((float)parent.material.texture.width * u);
            ty = (int)((float)parent.material.texture.height * v);
        }
        public warp_Vector transform(warp_Matrix m)
        // Modifies the vector by matrix m
        {
            float newx = x * m.m00 + y * m.m01 + z * m.m02 + m.m03;
            float newy = x * m.m10 + y * m.m11 + z * m.m12 + m.m13;
            float newz = x * m.m20 + y * m.m21 + z * m.m22 + m.m23;

            return(new warp_Vector(newx, newy, newz));
        }
Esempio n. 5
0
        public bool RotateScene(warp_Matrix m)
        {
            if (_scene == null)
            {
                return(false);
            }

            _scene.rotate(m);

            return(true);
        }
        static public warp_Quaternion matrix(warp_Matrix xfrm)
        {
            warp_Quaternion quat = new warp_Quaternion();
            // Check the sum of the diagonal
            float tr = xfrm [0, 0] + xfrm [1, 1] + xfrm [2, 2];

            if (tr > 0.0f)
            {
                // The sum is positive
                // 4 muls, 1 div, 6 adds, 1 trig function call
                float s = (float)Math.Sqrt(tr + 1.0f);
                quat.W = s * 0.5f;
                s      = 0.5f / s;
                quat.X = (xfrm [1, 2] - xfrm [2, 1]) * s;
                quat.Y = (xfrm [2, 0] - xfrm [0, 2]) * s;
                quat.Z = (xfrm [0, 1] - xfrm [1, 0]) * s;
            }
            else
            {
                // The sum is negative
                // 4 muls, 1 div, 8 adds, 1 trig function call
                int[] nIndex = { 1, 2, 0 };
                int   i, j, k;
                i = 0;
                if (xfrm [1, 1] > xfrm [i, i])
                {
                    i = 1;
                }
                if (xfrm [2, 2] > xfrm [i, i])
                {
                    i = 2;
                }
                j = nIndex [i];
                k = nIndex [j];

                float s = (float)Math.Sqrt((xfrm [i, i] - (xfrm [j, j] + xfrm [k, k])) + 1.0f);
                quat [i] = s * 0.5f;
                if (!warp_Math.FloatApproxEqual(s, 0.0f))
                {
                    s = 0.5f / s;
                }
                quat [j] = (xfrm [i, j] + xfrm [j, i]) * s;
                quat [k] = (xfrm [i, k] + xfrm [k, i]) * s;
                quat [3] = (xfrm [j, k] - xfrm [k, j]) * s;
            }

            return(quat);
        }
Esempio n. 7
0
        public bool RotateSelf(string name, warp_Matrix m)
        {
            if (_scene == null)
            {
                return(false);
            }

            warp_Object o = _scene.sceneobject(name);

            if (o == null)
            {
                return(false);
            }

            o.rotateSelf(m);

            return(true);
        }
Esempio n. 8
0
		static public warp_Quaternion matrix (warp_Matrix xfrm)
		{
			warp_Quaternion quat = new warp_Quaternion ();
			// Check the sum of the diagonal
			float tr = xfrm [0, 0] + xfrm [1, 1] + xfrm [2, 2];
			if (tr > 0.0f)
			{
				// The sum is positive
				// 4 muls, 1 div, 6 adds, 1 trig function call
				float s = (float)Math.Sqrt (tr + 1.0f);
				quat.W = s * 0.5f;
				s = 0.5f / s;
				quat.X = (xfrm [1, 2] - xfrm [2, 1]) * s;
				quat.Y = (xfrm [2, 0] - xfrm [0, 2]) * s;
				quat.Z = (xfrm [0, 1] - xfrm [1, 0]) * s;
			} else
			{
				// The sum is negative
				// 4 muls, 1 div, 8 adds, 1 trig function call
				int[] nIndex = { 1, 2, 0 };
				int i, j, k;
				i = 0;
				if (xfrm [1, 1] > xfrm [i, i])
					i = 1;
				if (xfrm [2, 2] > xfrm [i, i])
					i = 2;
				j = nIndex [i];
				k = nIndex [j];

				float s = (float)Math.Sqrt ((xfrm [i, i] - (xfrm [j, j] + xfrm [k, k])) + 1.0f);
				quat [i] = s * 0.5f;
				if (s != 0.0)
				{
					s = 0.5f / s;
				}
				quat [j] = (xfrm [i, j] + xfrm [j, i]) * s;
				quat [k] = (xfrm [i, k] + xfrm [k, i]) * s;
				quat [3] = (xfrm [j, k] - xfrm [k, j]) * s;
			}

			return quat;
		}
Esempio n. 9
0
		void rebuildMatrices ()
		{
			if (!needsRebuild)
				return;
			needsRebuild = false;

			warp_Vector forward, up, right;

			forward = warp_Vector.sub (lookat, pos);
			up = new warp_Vector (0f, 1f, 0f);
			right = warp_Vector.getNormal (up, forward);
			up = warp_Vector.getNormal (forward, right);

			forward.normalize ();
			up.normalize ();
			right.normalize ();

			normalmatrix = new warp_Matrix (right, up, forward);
			normalmatrix.rotate (0, 0, rollfactor);
			matrix = normalmatrix.getClone ();
			matrix.shift (pos.x, pos.y, pos.z);
			normalmatrix = normalmatrix.inverse ();
			matrix = matrix.inverse ();
		}
        public static warp_Object TUBE(warp_Vector[] path, float r, int steps, bool closed)
        {
            warp_Vector[] circle = new warp_Vector[steps];
            float         angle;

            for (int i = 0; i < steps; i++)
            {
                angle      = 2 * 3.14159265f * (float)i / (float)steps;
                circle [i] = new warp_Vector(r * warp_Math.cos(angle),
                                             r * warp_Math.sin(angle), 0f);
            }

            warp_Object newObject = new warp_Object();
            int         segments = path.GetLength(0);
            warp_Vector forward, up, right;
            warp_Matrix frenetmatrix;
            warp_Vertex tempvertex;
            float       relx, rely;
            int         a, b, c, d;

            for (int i = 0; i < segments; i++)
            {
                // Calculate frenet frame matrix

                if (i != segments - 1)
                {
                    forward = warp_Vector.sub(path [i + 1], path [i]);
                }
                else
                {
                    if (!closed)
                    {
                        forward = warp_Vector.sub(path [i], path [i - 1]);
                    }
                    else
                    {
                        forward = warp_Vector.sub(path [1], path [0]);
                    }
                }

                forward.normalize();
                up           = new warp_Vector(0f, 0f, 1f);
                right        = warp_Vector.getNormal(forward, up);
                up           = warp_Vector.getNormal(forward, right);
                frenetmatrix = new warp_Matrix(right, up, forward);
                frenetmatrix.shift(path [i].x, path [i].y, path [i].z);

                // Add nodes

                relx = (float)i / (float)(segments - 1);
                for (int k = 0; k < steps; k++)
                {
                    rely         = (float)k / (float)steps;
                    tempvertex   = new warp_Vertex(circle [k].transform(frenetmatrix));
                    tempvertex.u = relx;
                    tempvertex.v = rely;
                    newObject.addVertex(tempvertex);
                }
            }

            for (int i = 0; i < segments - 1; i++)
            {
                for (int k = 0; k < steps - 1; k++)
                {
                    a = i * steps + k;
                    b = a + 1;
                    c = a + steps;
                    d = b + steps;
                    newObject.addTriangle(a, c, b);
                    newObject.addTriangle(b, c, d);
                }
                a = (i + 1) * steps - 1;
                b = a + 1 - steps;
                c = a + steps;
                d = b + steps;
                newObject.addTriangle(a, c, b);
                newObject.addTriangle(b, c, d);
            }

            return(newObject);
        }
Esempio n. 11
0
		public static warp_Object TUBE (warp_Vector[] path, float r, int steps, bool closed)
		{
			warp_Vector[] circle = new warp_Vector[steps];
			float angle;
			for (int i = 0; i < steps; i++)
			{
				angle = 2 * 3.14159265f * (float)i / (float)steps;
				circle [i] = new warp_Vector (r * warp_Math.cos (angle),
					r * warp_Math.sin (angle), 0f);
			}

			warp_Object newObject = new warp_Object ();
			int segments = path.GetLength (0);
			warp_Vector forward, up, right;
			warp_Matrix frenetmatrix;
			warp_Vertex tempvertex;
			float relx, rely;
			int a, b, c, d;

			for (int i = 0; i < segments; i++)
			{
				// Calculate frenet frame matrix

				if (i != segments - 1)
				{
					forward = warp_Vector.sub (path [i + 1], path [i]);
				} else
				{
					if (!closed)
					{
						forward = warp_Vector.sub (path [i], path [i - 1]);
					} else
					{
						forward = warp_Vector.sub (path [1], path [0]);
					}
				}

				forward.normalize ();
				up = new warp_Vector (0f, 0f, 1f);
				right = warp_Vector.getNormal (forward, up);
				up = warp_Vector.getNormal (forward, right);
				frenetmatrix = new warp_Matrix (right, up, forward);
				frenetmatrix.shift (path [i].x, path [i].y, path [i].z);

				// Add nodes

				relx = (float)i / (float)(segments - 1);
				for (int k = 0; k < steps; k++)
				{
					rely = (float)k / (float)steps;
					tempvertex = new warp_Vertex (circle [k].transform (frenetmatrix));
					tempvertex.u = relx;
					tempvertex.v = rely;
					newObject.addVertex (tempvertex);
				}
			}

			for (int i = 0; i < segments - 1; i++)
			{
				for (int k = 0; k < steps - 1; k++)
				{
					a = i * steps + k;
					b = a + 1;
					c = a + steps;
					d = b + steps;
					newObject.addTriangle (a, c, b);
					newObject.addTriangle (b, c, d);
				}
				a = (i + 1) * steps - 1;
				b = a + 1 - steps;
				c = a + steps;
				d = b + steps;
				newObject.addTriangle (a, c, b);
				newObject.addTriangle (b, c, d);
			}

			return newObject;
		}
        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 (useId)
            {
                warp_Math.clearBuffer(idBuffer, -1);
            }
            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;
            int           w = screen.width;
            int           h = screen.height;

            for (int id = scene.objects - 1; id >= 0; id--)
            {
                obj = scene.wobject [id];
                if (obj.visible)
                {
                    vertexProjection = obj.matrix.getClone();
                    normalProjection = obj.normalmatrix.getClone();
                    vertexProjection.transform(m);
                    normalProjection.transform(nm);

                    for (int i = obj.vertices - 1; i >= 0; i--)
                    {
                        v = obj.fastvertex [i];
                        v.project(vertexProjection, normalProjection, cam);
                        v.clipFrustrum(w, h);
                    }
                    for (int i = obj.triangles - 1; i >= 0; i--)
                    {
                        t = obj.fasttriangle [i];
                        t.project(normalProjection);
                        t.clipFrustrum(w, h);
                        enqueueTriangle(t);
                    }
                }
            }

            //screen.lockImage();

            warp_Triangle[] tri;
            tri = getOpaqueQueue();
            if (tri != null)
            {
                for (int i = tri.GetLength(0) - 1; i >= 0; i--)
                {
                    rasterizer.loadMaterial(tri [i].parent.material);
                    rasterizer.render(tri [i]);
                }
            }

            tri = getTransparentQueue();
            if (tri != null)
            {
                for (int i = 0; i < tri.GetLength(0); i++)
                {
                    rasterizer.loadMaterial(tri [i].parent.material);
                    rasterizer.render(tri [i]);
                }
            }

            //screen.unlockImage();
        }
Esempio n. 13
0
		public bool RotateSelf (string name, warp_Matrix m)
		{
			if (_scene == null)
			{
				return false;
			}

			warp_Object o = _scene.sceneobject (name);
			if (o == null)
			{
				return false;
			}

			o.rotateSelf (m);

			return true;
		}
Esempio n. 14
0
		public bool RotateScene (warp_Matrix m)
		{
			if (_scene == null)
			{
				return false;
			}

			_scene.rotate (m);

			return true;
		}