예제 #1
0
 public void getPhoton(double randX1, double randY1, double randX2, double randY2, Point3 p, Vector3 dir, Color power)
 {
     float phi = (float)(2 * Math.PI * randX1);
     float s = (float)Math.Sqrt(1.0f - randY1);
     dir.x = r * (float)Math.Cos(phi) * s;
     dir.y = r * (float)Math.Sin(phi) * s;
     dir.z = 0;
     basis.transform(dir);
     Point3.add(src, dir, p);
     dir.set(this.dir);
     power.set(radiance).mul((float)Math.PI * r2);
 }
예제 #2
0
 private TriangleMesh generate(int[] tris, float[] verts, bool smoothNormals)
 {
     ParameterList pl = new ParameterList();
     pl.addIntegerArray("triangles", tris);
     pl.addPoints("points", ParameterList.InterpolationType.VERTEX, verts);
     if (smoothNormals)
     {
         float[] normals = new float[verts.Length]; // filled with 0's
         Point3 p0 = new Point3();
         Point3 p1 = new Point3();
         Point3 p2 = new Point3();
         Vector3 n = new Vector3();
         for (int i3 = 0; i3 < tris.Length; i3 += 3)
         {
             int v0 = tris[i3 + 0];
             int v1 = tris[i3 + 1];
             int v2 = tris[i3 + 2];
             p0.set(verts[3 * v0 + 0], verts[3 * v0 + 1], verts[3 * v0 + 2]);
             p1.set(verts[3 * v1 + 0], verts[3 * v1 + 1], verts[3 * v1 + 2]);
             p2.set(verts[3 * v2 + 0], verts[3 * v2 + 1], verts[3 * v2 + 2]);
             Point3.normal(p0, p1, p2, n); // compute normal
             // add face normal to each vertex
             // note that these are not normalized so this in fact weights
             // each normal by the area of the triangle
             normals[3 * v0 + 0] += n.x;
             normals[3 * v0 + 1] += n.y;
             normals[3 * v0 + 2] += n.z;
             normals[3 * v1 + 0] += n.x;
             normals[3 * v1 + 1] += n.y;
             normals[3 * v1 + 2] += n.z;
             normals[3 * v2 + 0] += n.x;
             normals[3 * v2 + 1] += n.y;
             normals[3 * v2 + 2] += n.z;
         }
         // normalize all the vectors
         for (int i3 = 0; i3 < normals.Length; i3 += 3)
         {
             n.set(normals[i3 + 0], normals[i3 + 1], normals[i3 + 2]);
             n.normalize();
             normals[i3 + 0] = n.x;
             normals[i3 + 1] = n.y;
             normals[i3 + 2] = n.z;
         }
         pl.addVectors("normals", ParameterList.InterpolationType.VERTEX, normals);
     }
     TriangleMesh m = new TriangleMesh();
     if (m.update(pl, null))
         return m;
     // something failed in creating the mesh, the error message will be
     // printed by the mesh itself - no need to repeat it here
     return null;
 }
예제 #3
0
 public Vector3 untransform(Vector3 a)
 {
     float x = Vector3.dot(a, u);
     float y = Vector3.dot(a, v);
     float z = Vector3.dot(a, w);
     return a.set(x, y, z);
 }
예제 #4
0
 public Vector3 transform(Vector3 a)
 {
     float x = (a.x * u.x) + (a.y * v.x) + (a.z * w.x);
     float y = (a.x * u.y) + (a.y * v.y) + (a.z * w.y);
     float z = (a.x * u.z) + (a.y * v.z) + (a.z * w.z);
     return a.set(x, y, z);
 }
예제 #5
0
        private void GenerateMesh()
        {
            List<Vector3> curvePoints = new List<Vector3>();
            List<Vector3> attractorPoints = new List<Vector3>();

            pipeFunction.InitParameters();

            attractorPoints.Capacity = pipeSegments + 2;
            curvePoints.Capacity = (pipeSegments + 2) * 5;

            int circleSegments = outlinePoints.Count;
            float tDelta = 1.0f/(knotsPerPipeSegment-1);

            attractorPoints.Add(startPosition);
            Vector3 temp = new Vector3();
            temp.set(startPosition);

            //    		Console.WriteLine(temp);

            for (int i =0; i<pipeSegments; i++) {

                pipeFunction.GetNextPosition(temp);
                Vector3 nextPoint = new Vector3();
                nextPoint.set(temp);
                attractorPoints.Add(nextPoint);

            }

            // special case the first point
            int lastItem = attractorPoints.Count-1;
            if (attractorPoints[0] == attractorPoints[lastItem]) {
                // it loops
                attractorPoints.RemoveAt(lastItem);
                Vector3 tmp = attractorPoints[ attractorPoints.Count-1];
                attractorPoints.Add(attractorPoints[0]);
                attractorPoints.Insert(0, tmp);
            } else {
                // it does not loop

                Vector3 diff=  new Vector3();
                diff = Vector3.sub(attractorPoints[0], attractorPoints[1], diff);
                diff =  Vector3.add(attractorPoints[0], diff, diff);

                Vector3 diff2 =  new Vector3();
                diff2 = Vector3.sub(attractorPoints[lastItem], attractorPoints[lastItem-1], diff2);
                diff2 =  Vector3.add(attractorPoints[lastItem], diff2, diff2);

                attractorPoints.Add(diff2);
                attractorPoints.Insert(0,diff );

            }

            //  	    	Console.WriteLine("attractorPoints: {0}" , attractorPoints.Count);

            int knotIndex = 0;
            Vector3 splinePoint = new Vector3();
            Vector3 tangent = new Vector3();
            Vector3 normal = new Vector3();
            Vector3 up = new Vector3(0f,1f,0f);
            Point3 zero = new Point3(0f,0f,0f);
            Point3 tangentAsPoint = new Point3();
            Point3 rotatedPoint = new Point3();
            Vector3 oldSplinePoint = new  Vector3();
            Matrix4 rotateToTangent ;

            int pipeIndex = 0;
            int quadIndex = 0;
            int circleIndex = 0;
            int normalIndex = 0;

            float t=0f;

            foreach ( Vector3 tempv in attractorPoints) {
                tempv.mul(4.0f);
                // 	    		Console.WriteLine("attractorPoint: {0}" , tempv);
            }

            oldSplinePoint.set(attractorPoints[0]);

            for (int i=0; i<=pipeSegments-1; i++) {

                while (t <= 1.0f) {
                    //				Console.WriteLine("t : {0}", t);
                    //				Console.WriteLine("knotIndex : {0} - {1}", knotIndex, knotIndex+3);
                    //				Console.WriteLine(attractorPoints[knotIndex+1]);
                    //				Console.WriteLine(attractorPoints[knotIndex+2]);

                    splinePoint.x = CatmullRomSpline(t,
                                                     attractorPoints[knotIndex].x,
                                                     attractorPoints[knotIndex+1].x,
                                                     attractorPoints[knotIndex+2].x,
                                                     attractorPoints[knotIndex+3].x);
                    splinePoint.y = CatmullRomSpline(t,
                                                     attractorPoints[knotIndex].y,
                                                     attractorPoints[knotIndex+1].y,
                                                     attractorPoints[knotIndex+2].y,
                                                     attractorPoints[knotIndex+3].y);
                    splinePoint.z = CatmullRomSpline(t,
                                                     attractorPoints[knotIndex].z,
                                                     attractorPoints[knotIndex+1].z,
                                                     attractorPoints[knotIndex+2].z,
                                                     attractorPoints[knotIndex+3].z);

                    t += tDelta;

                    tangent = Vector3.sub(splinePoint, oldSplinePoint, tangent).normalize();
                    tangentAsPoint.set(tangent.x, tangent.y, tangent.z);

            //					normal =  Vector3.cross(tangent,up,normal).normalize();

                    oldSplinePoint.set(splinePoint);

            //					Matrix4 rotateAlongTangent = Matrix4.rotate(tangent.x, tangent.y, tangent.z, (float)thetaDelta);

                    rotateToTangent = Matrix4.lookAt(zero, tangentAsPoint, up);//.inverse();

                    if(circleIndex == 0)
                    {

                        for (int circleSegement	= 0; circleSegement < circleSegments ; circleSegement++) {

            //							pointOnOutline.set (pipeRadius * (float)Math.Cos(theta) ,pipeRadius * (float)Math.Sin(theta), 0f);

                            rotatedPoint =  rotateToTangent.transformP(outlinePoints[circleSegement]);

                            bb.include(rotatedPoint.x + splinePoint.x, rotatedPoint.y + splinePoint.y,  rotatedPoint.z + splinePoint.z);

                            points[pipeIndex++] = rotatedPoint.x + splinePoint.x;
                            points[pipeIndex++] = rotatedPoint.y + splinePoint.y;
                            points[pipeIndex++] = rotatedPoint.z + splinePoint.z;

                            if(smooth) {
                                normal.x = rotatedPoint.x;
                                normal.y = rotatedPoint.y;
                                normal.z = rotatedPoint.z;

                                normal.normalize();

                                normals.data[normalIndex++] = normal.x;
                                normals.data[normalIndex++] = normal.y;
                                normals.data[normalIndex++] = normal.z;
                            }

                        }
                    }
                    else
                    {
                        // go round it a circle.
                        int circleSegement;
                        for (circleSegement = 0; circleSegement < circleSegments; circleSegement++) {

                            rotatedPoint =  rotateToTangent.transformP(outlinePoints[circleSegement]);
                            bb.include(rotatedPoint.x + splinePoint.x, rotatedPoint.y + splinePoint.y,  rotatedPoint.z + splinePoint.z);

                            points[pipeIndex++] = rotatedPoint.x + splinePoint.x;
                            points[pipeIndex++] = rotatedPoint.y + splinePoint.y;
                            points[pipeIndex++] = rotatedPoint.z + splinePoint.z;

                            if(smooth) {
                                normal.x = rotatedPoint.x;
                                normal.y = rotatedPoint.y;
                                normal.z = rotatedPoint.z;

                                normal.normalize();

                                normals.data[normalIndex++] = normal.x;
                                normals.data[normalIndex++] = normal.y;
                                normals.data[normalIndex++] = normal.z;
                            }

                            if (circleSegement + 1 < circleSegments)
                            {
                                //  	    						Console.WriteLine("quadIndex : {0}", quadIndex);
                                quads[quadIndex++] = circleSegement + ((circleIndex - 1) * circleSegments)  ;
                                quads[quadIndex++] = circleSegement + ((circleIndex - 1) * circleSegments) + 1;
                                quads[quadIndex++] = circleSegement + (circleIndex * circleSegments) + 1;
                                quads[quadIndex++] = circleSegement + (circleIndex * circleSegments);

                            }

                        }

                        // joint it back to first points
                        quads[quadIndex++] = (circleSegement - 1)  + ((circleIndex - 1) * circleSegments)  ;
                        quads[quadIndex++] = ((circleIndex - 1) * circleSegments);
                        quads[quadIndex++] = (circleIndex * circleSegments) ;
                        quads[quadIndex++] = (circleSegement - 1) + (circleIndex * circleSegments);

                    }
                    circleIndex++;

                }

                t = tDelta;
                knotIndex++;

            }
        }
예제 #6
0
        public void getPhoton(double randX1, double randY1, double randX2, double randY2, Point3 p, Vector3 dir, Color power)
        {
            p.x = (float)(lxmin * (1 - randX2) + lxmax * randX2);
            p.y = (float)(lymin * (1 - randY2) + lymax * randY2);
            p.z = maxZ - 0.001f;

            double u = 2 * Math.PI * randX1;
            double s = Math.Sqrt(randY1);
            dir.set((float)(Math.Cos(u) * s), (float)(Math.Sin(u) * s), (float)-Math.Sqrt(1.0f - randY1));
            Color.mul((float)Math.PI * area, radiance, power);
        }