Example #1
0
        /// <summary>
        /// reverse transformation.
        /// </summary>
        public override Vec3 Transform(Vec3 input)
        {
            Vec3   p1   = input.Diff(_camera);
            double dp   = p1.Norm;
            double fac  = dp / d;
            Vec3   p1_p = p1.Mult(fac);

            return(p1_p.Sum(_camera));
        }
Example #2
0
        /// <summary>
        /// Reversed reverse transformation.
        /// </summary>
        public Vec3 ReverseTransform(Vec3 input)
        {
            Vec3   transformedCamera = Transform(_camera);
            Vec3   tempVec           = input.Diff(transformedCamera);
            double l  = tempVec.Norm;
            double dt = Math.Sqrt(l * d);

            tempVec.Normalize();
            tempVec = tempVec.Mult(dt);
            return(tempVec.Sum(transformedCamera));
        }
        /// <summary>
        /// Compute field of view.
        /// </summary>
        protected void SmoothPlane()
        {
            double fieldOfViewStart = minFieldOfView;
            ydGlobal = (areaDeph) / ((double)(Math.Max(pData.Width, pData.Height)));
            rgbSmoothPlane1 = new Vec3[pData.Width, pData.Height];
            rgbSmoothPlane2 = new Vec3[pData.Width, pData.Height];
            int intRange = 3;
            for (int i = 0; i < pData.Width; i++)
            {
                for (int j = 0; j < pData.Height; j++)
                {
                    rgbSmoothPlane2[i, j] = rgbPlane[i, j];
                }
            }
            if (ambientIntensity == 0)
            {
                //  No field of view defined:
                return;
            }

            // starts with rgbSmoothPlane2
            Vec3[,] currentPlane = rgbSmoothPlane2;
            Vec3[,] nextPlane = rgbSmoothPlane1;
            // contain the result colors
            Vec3[,] resultPlane = rgbSmoothPlane1;

            double mainDeph1 = areaDeph;
            for (int m = 0; m < ambientIntensity; m++)
            {
                if (_stopRequest)
                    return;
                for (int i = 0; i < pData.Width; i++)
                {
                    for (int j = 0; j < pData.Height; j++)
                    {
                        double neighborsFound = 0;
                        PixelInfo pInfo = pData.Points[i, j];
                        Vec3 nColor = new Vec3();
                        double ydNormalized = GetAmbientValue(smoothDeph2[i, j]);
                        ydNormalized = Math.Sqrt(ydNormalized);
                        intRange = 1;
                        if (intRange == 0)
                        {
                            nColor = currentPlane[i, j];
                            neighborsFound = 1;
                        }
                        double sumColor = 0;

                        //           if(pData.Points[i, j]!=null) {
                        if (true)
                        {
                            for (int k = -intRange; k <= intRange; k++)
                            {
                                for (int l = -intRange; l <= intRange; l++)
                                {
                                    // Center Pixel is also allowed
                                    // if (k != 0 || l != 0) {
                                    int posX = i + k;
                                    int posY = j + l;
                                    if (posX >= 0 && posX < pData.Width && posY >= 0 && posY < pData.Height)
                                    {
                                        Vec3 nColor1 = new Vec3();
                                        double ylocalDiff = smoothDeph1[i, j] - smoothDeph1[posX, posY];
                                        if (true)
                                        //   if ( (ylocalDiff > 0) ||(i==posX && j==posY))
                                        //   if ((ylocalDiff < 0) || (i == posX && j == posY))
                                        //   if(false)
                                        {
                                            //double range = (k * k + l * l) / (intRange * intRange);
                                            int range = (k * k + l * l);
                                            double mult = 1;

                                            if (range == 0)
                                            {
                                                // mult = 0.6;
                                                mult = ydNormalized * 0.3;
                                                //mult = 0.2;
                                            }
                                            if (range == 1)
                                            {
                                                //mult = 0.25;
                                                mult = (1.0 - ydNormalized) * 0.4;
                                                //mult = 0.45;
                                            }
                                            if (range == 2)
                                            {
                                                //mult=0.15;
                                                mult = (1.0 - ydNormalized) * 0.3;
                                                //mult = 0.35;

                                            }
                                            // mult += 0.00001;

                                            PixelInfo pInfo2 = pData.Points[posX, posY];

                                            double amount = 1;
                                            if (pInfo != null && pInfo2 != null)
                                            {
                                                double dist = pInfo.Coord.Dist(pInfo2.Coord);

                                                double dGlobal = maxPoint.Dist(minPoint);
                                                dGlobal /= 1500;
                                                if (dist < dGlobal)
                                                    amount = 1.0;
                                                else
                                                    //  else if (dist > dGlobal && dist < 10.0 * dGlobal)
                                                    amount = 1.0 - (dGlobal / dist) / 10.0;
                                                // else
                                                //     amount = 0.0;
                                            }

                                            mult *= amount;
                                            //  mult *= 1.0/ydNormalized;

                                            sumColor += mult;

                                            Vec3 currentColor = currentPlane[posX, posY];
                                            nColor1.X = currentColor.X;
                                            nColor1.Y = currentColor.Y;
                                            nColor1.Z = currentColor.Z;
                                            nColor1 = nColor1.Mult(mult); // Scaling;

                                            nColor.Add(nColor1);
                                            neighborsFound++;
                                        }
                                    }
                                }
                            }
                        }

                        if (neighborsFound > 1)
                        {
                            nColor = nColor.Mult(1 / sumColor);
                        }
                        else
                        {
                            nColor = currentPlane[i, j];
                        }
                        nextPlane[i, j] = nColor;
                    }
                }

                resultPlane = nextPlane;
                nextPlane = currentPlane;
                currentPlane = resultPlane;
            }

            for (int i = 0; i < pData.Width; i++)
            {
                for (int j = 0; j < pData.Height; j++)
                {
                    rgbPlane[i, j] = resultPlane[i, j];
                }
            }

            rgbSmoothPlane1 = null;
            rgbSmoothPlane2 = null;

            return;
        }
 /// <summary>
 /// Test, if the given point is inside the sharp shadow. 
 /// Returns the number of intersection with the ray and the fractal, but not more than maxIntersections.
 /// 
 /// </summary>
 /// <param name="point"></param>
 /// <param name="ray"></param>
 /// <param name="rayLenght"></param>
 /// <returns></returns>
 protected int IsInSharpShadow(Vec3 point, Vec3 ray, double rayLenght, bool inverse, int maxIntersections, int steps)
 {
     //int steps = 100;
     inverse = false;
     double dSteps = steps;
     double dist = 0;
     int shadowCount = 0;
     for (int gSteps = 0; gSteps < 6; gSteps++)
     {
         dist = rayLenght / dSteps;
         Vec3 currentPoint = new Vec3(point);
         currentPoint.Add(ray.Mult(dist));
         for (int i = 0; i < steps; i++)
         {
             currentPoint.Add(ray.Mult(dist));
             if (formula.TestPoint(currentPoint.X, currentPoint.Y, currentPoint.Z, inverse))
             {
                 shadowCount++;
                 if (shadowCount >= maxIntersections)
                     return maxIntersections;
             }
             else
             {
                 //  return false;
             }
         }
         rayLenght /= 1.4;
     }
     return shadowCount;
 }
        /// <summary>
        /// Get the color information of the bitmap at (x,y)
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        protected Vec3 GetRgb(int x, int y)
        {
            Vec3 retVal = new Vec3(0, 0, 1); // blau
            PixelInfo pInfo = pData.Points[x, y];
            if (pInfo == null)
            {
                return new Vec3(backColorRed, backColorGreen, backColorBlue);
            }

            Vec3 light = new Vec3(0, 0, 0);
            Vec3 normal = null;

            normal = normalesSmooth1[x, y];
            if (normal == null) { normal = pInfo.Normal; }
            // Testweise original Normale verwenden
            //  normal = pInfo.Normal;
            // TODO: Obiges auskommentieren
            if (normal == null)
                return new Vec3(0, 0, 0);

            // tempcoord2 enthält die umgerechnete Oberflächennormale.
            double tfac = 1000;

            Vec3 coord = formula.GetTransform(pInfo.Coord.X, pInfo.Coord.Y, pInfo.Coord.Z);
            normal.Normalize();
            Vec3 tempcoord2 = formula.GetTransform(pInfo.Coord.X + tfac * normal.X, pInfo.Coord.Y + tfac * normal.Y, pInfo.Coord.Z + tfac * normal.Z);

            tempcoord2.X -= coord.X;
            tempcoord2.Y -= coord.Y;
            tempcoord2.Z -= coord.Z;

            // Normalize:
            tempcoord2.Normalize();

            // debug only
            tempcoord2 = normal;

            if (pInfo.Normal != null)
            {
                light = GetLight(tempcoord2);
                if (sharpShadow != null) { light = light.Mult(1 - sharpShadow[x, y]); }
            }

            retVal.X = light.X;
            retVal.Y = light.Y;
            retVal.Z = light.Z;

            double d1 = maxY - minY;
            double d2 = pData.Width + pData.Height;
            double d3 = d1 / d2;

            retVal.X = (lightIntensity * retVal.X + (1 - lightIntensity) * (1 - shadowPlane[x, y]));
            retVal.Y = (lightIntensity * retVal.Y + (1 - lightIntensity) * (1 - shadowPlane[x, y]));
            retVal.Z = (lightIntensity * retVal.Z + (1 - lightIntensity) * (1 - shadowPlane[x, y]));

            if (retVal.X < 0)
                retVal.X = 0;
            if (retVal.Y < 0)
                retVal.Y = 0;
            if (retVal.Z < 0)
                retVal.Z = 0;

            if (retVal.X > 1)
                retVal.X = 1;
            if (retVal.Y > 1)
                retVal.Y = 1;
            if (retVal.Z > 1)
                retVal.Z = 1;

            double brightLightLevel = ParameterDict.Current.GetDouble("Renderer.BrightLightLevel");
            if (brightLightLevel > 0)
            {
                retVal.X = (1 - brightLightLevel) * retVal.X + brightLightLevel * light.X * (1 - shadowPlane[x, y]);
                retVal.Y = (1 - brightLightLevel) * retVal.Y + brightLightLevel * light.Y * (1 - shadowPlane[x, y]);
                retVal.Z = (1 - brightLightLevel) * retVal.Z + brightLightLevel * light.Z * (1 - shadowPlane[x, y]);
            }

            if (retVal.X < 0)
                retVal.X = 0;
            if (retVal.Y < 0)
                retVal.Y = 0;
            if (retVal.Z < 0)
                retVal.Z = 0;

            if (retVal.X > 1)
                retVal.X = 1;
            if (retVal.Y > 1)
                retVal.Y = 1;
            if (retVal.Z > 1)
                retVal.Z = 1;

            // Add surface color
            bool useAdditionalColorinfo = true;
            if (colorIntensity <= 0)
                useAdditionalColorinfo = false;
            if (useAdditionalColorinfo && ((pInfo.IsInside && _colorInside) || (!pInfo.IsInside && _colorOutside)))
            {
                if (pInfo != null && pInfo.AdditionalInfo != null)
                {
                    // Normalise;
                    double r1 = colorFactorRed * Math.Pow(pInfo.AdditionalInfo.red, colorIntensity);
                    double g1 = colorFactorGreen * Math.Pow(pInfo.AdditionalInfo.green, colorIntensity);
                    double b1 = colorFactorBlue * Math.Pow(pInfo.AdditionalInfo.blue, colorIntensity);
                    if (r1 < 0)
                        r1 = -r1;
                    if (g1 < 0)
                        g1 = -g1;
                    if (b1 < 0)
                        b1 = -b1;

                    // Normalize:
                    double norm = Math.Sqrt(r1 * r1 + g1 * g1 + b1 * b1)/Math.Sqrt(2.5);
                    r1 = r1 / norm;
                    g1 = g1 / norm;
                    b1 = b1 / norm;

                    for (int i = 0; i < 5; i++)
                    {
                        if (r1 > 1)
                        {
                            b1 += (r1 - 1) / 2.0;
                            g1 += (r1 - 1) / 2.0;
                            r1 = 1;
                        }
                        if (b1 > 1)
                        {

                            r1 += (b1 - 1) / 2.0;
                            g1 += (b1 - 1) / 2.0;
                            b1 = 1;

                        }
                        if (g1 > 1)
                        {

                            r1 += (g1 - 1) / 2.0;
                            b1 += (g1 - 1) / 2.0;
                            g1 = 1;
                        }
                    }

                    if (r1 > 1)
                        r1 = 1;
                    if (b1 > 1)
                        b1 = 1;
                    if (g1 > 1)
                        g1 = 1;

                    if (colorGreyness > 0)
                    {
                        r1 = colorGreyness + (1 - colorGreyness) * r1;
                        g1 = colorGreyness + (1 - colorGreyness) * g1;
                        b1 = colorGreyness + (1 - colorGreyness) * b1;
                    }

                    if (r1 > 1)
                        r1 = 1;
                    if (b1 > 1)
                        b1 = 1;
                    if (g1 > 1)
                        g1 = 1;

                    if (norm != 0)
                    {
                        switch (rgbType)
                        {
                            case 1:
                                retVal.X *= r1;
                                retVal.Y *= g1;
                                retVal.Z *= b1;
                                break;

                            case 2:
                                retVal.X *= r1;
                                retVal.Y *= b1;
                                retVal.Z *= g1;
                                break;

                            case 3:
                                retVal.X *= g1;
                                retVal.Y *= r1;
                                retVal.Z *= b1;
                                break;

                            case 4:
                                retVal.X *= g1;
                                retVal.Y *= b1;
                                retVal.Z *= r1;
                                break;

                            case 5:
                                retVal.X *= b1;
                                retVal.Y *= r1;
                                retVal.Z *= g1;
                                break;

                            case 6:
                                retVal.X *= b1;
                                retVal.Y *= g1;
                                retVal.Z *= r1;
                                break;

                            default:
                                retVal.X *= r1;
                                retVal.Y *= g1;
                                retVal.Z *= b1;
                                break;

                        }

                    }
                }
            }

            if (contrast != 1)
            {
                retVal.X = Math.Pow(retVal.X, contrast);
                retVal.Y = Math.Pow(retVal.Y, contrast);
                retVal.Z = Math.Pow(retVal.Z, contrast);

            }

            if (brightness > 1)
            {
                retVal.X *= brightness;
                retVal.Y *= brightness;
                retVal.Z *= brightness;
            }

            if (retVal.X < 0)
                retVal.X = 0;
            if (retVal.X > 1)
                retVal.X = 1;
            if (retVal.Y < 0)
                retVal.Y = 0;
            if (retVal.Z < 0)
                retVal.Z = 0;
            if (retVal.Y > 1)
                retVal.Y = 1;
            if (retVal.Z > 1)
                retVal.Z = 1;

            if (pInfo != null && pInfo.AdditionalInfo != null)
            {
                pInfo.AdditionalInfo.red2 = retVal.X;
                pInfo.AdditionalInfo.green2 = retVal.Y;
                pInfo.AdditionalInfo.blue2 = retVal.Z;
            }

            return retVal;
        }