Ejemplo n.º 1
0
        public void DrawChildMeshes(int res, float minx, float miny, float width, gl.Transform transform, Vector3 Camera, TriangleDrawer graphics, Shader shader, bool onlyborders, float zoom, float precision, int maxprecision)
        {
            const int Spread = 4, sq = Spread * Spread;

            res /= Spread;
            float step = width / Spread;

            MeshHolder[] childs = Childs;
            if (childs == null)
            {
                childs = new MeshHolder[sq];
                int k = 0;
                for (int j = 0; j < Spread; j++)
                {
                    float mny = miny + step * j;
                    for (int i = 0; i < Spread; i++)
                    {
                        float mnx = minx + step * i;
                        childs[k] = new MeshHolder(Parent = this, shapeGenerator, LocalUp, AxisA, AxisB, mnx, mny, step);
                        k++;
                    }
                }
                Childs = childs;
            }
            for (int i = 0; i < sq; i++)
            {
                childs[i].DrawMesh(transform, Camera, graphics, shader, onlyborders, zoom, precision, maxprecision);
            }
        }
Ejemplo n.º 2
0
        public void Draw(TriangleDrawer g, gl.Transform info, Vector3 Camera, bool onlyborders, Size screensize, float pixelprecision, int MaxResolution, Shader shader, float zoom)
        {
            int   maxsize   = screensize.Width > screensize.Height ? screensize.Width : screensize.Height;
            float precision = maxsize / pixelprecision * shapeSettings.PlanetRadius;

            for (int i = 0; i < 6; i++)
            {
                TerrainFaces[i].DrawMesh(info, Camera, g, shader, onlyborders, zoom, precision, 10);
            }
        }
Ejemplo n.º 3
0
        void Draw()
        {
            Shader shader = Shade ?
                            new Shader {
                ShadeB = 0.05f, ShadeG = 0.03f, ShadeR = 0.03f, LightB = 1f, LightG = 1f, LigtR = 1f, Enabled = true
            }
            : new Shader();
            int w = (int)SystemParameters.PrimaryScreenWidth;
            int h = (int)SystemParameters.PrimaryScreenHeight;

            using (TriangleDrawer g = new TriangleDrawer(w, h))
            {
                openview.Size size = new openview.Size(w, h);
                g.Lock();
                gl.Matrix matrix = transform.Matrix;
                for (int i = 0; i < planets.Count; i++)
                {
                    ShapeSettings shapeSettings = planets[i].shapeSettings;
                    Vector3       v = matrix.ApplyTo(shapeSettings.position - transform.position);
                    float         x = v.x, y = v.y, z = v.z;
                    float         r = shapeSettings.PlanetRadius + shapeSettings.MaxDifference;
                    y += r;
                    bool         xCorrect = x > 0 ? x - r <y : x + r> -y;
                    bool         zCorrect = z > 0 ? z - r <y : z + r> -y;
                    gl.Transform Applied  = transform;
                    Applied.position = Applied.position - shapeSettings.position;
                    //Applied.position = shapeSettings.position- Applied.position;
                    if (y > 0 && xCorrect && zCorrect)
                    {
                        planets[i].Draw(g, Applied, transform.position, DrawOnlyBorders, size, precision, 1000 / precision, shader, zoom);
                    }
                }
                if (DrawCockpit)
                {
                    g.DrawImage(cockpit, 255);
                }
                if (MotionBlur)
                {
                    if (LastDraw != null)
                    {
                        g.DrawImage(LastDraw, 128);
                    }
                    LastDraw   = g.Unlock();
                    Background = new ImageBrush(LastDraw);
                }
                else
                {
                    Background = new ImageBrush(g.Unlock());
                }
                Title = g.elapsed.ToString();
            }
        }
Ejemplo n.º 4
0
        public void DrawMesh(gl.Transform transform, Vector3 Camera, TriangleDrawer graphics, Shader shader, bool onlyborders, float zoom, float precision, int maxprecision)
        {
            float minx = this.minx, miny = this.miny, width = this.width;

            maxprecision--;
            ShapeGenerator    shapeGenerator = this.shapeGenerator;
            ShapeSettings     shapeSettings  = shapeGenerator.settings;
            NoiseSettings     noiseSettings  = shapeSettings.NoiseSettings;
            ElevationSettings settings       = noiseSettings.settings;
            Vector3           PlanetCentre = shapeSettings.position;
            //Vector3 pos = transform.position - PlanetCentre;
            float   radius                   = shapeSettings.PlanetRadius;
            float   r           = settings.WaterHeight * shapeSettings.MaxDifference + radius;
            float   Middleelev  = shapeGenerator.noiseFilter.Evaluate(MiddlePointOnUnitSphere);
            Vector3 MiddlePoint = MiddlePoint = MiddlePointOnUnitSphere * (radius + Middleelev * shapeSettings.MaxDifference) + PlanetCentre;
            float   distance    = Vector3.Distance(Camera, MiddlePoint);

            if (distance == 0)
            {
                return;
            }
            if (minx > 0)
            {
            }
            int resolution = Mathf.FloorToPower((int)(precision / distance * width), 2);

            //resolution will be >0
            //#if false
            if (maxprecision > 0 && resolution > 16)//max 16x16=256
            {
                DrawChildMeshes(resolution, minx, miny, width, transform, Camera, graphics, shader, onlyborders, zoom, precision, maxprecision);
                return;
            }
            //#endif
            else if (maxprecision == 0)
            {
            }
            Vector3 Max00   = (LocalUp * .5f + (minx - .5f) * AxisA + (miny - .5f) * AxisB).normalized * r + PlanetCentre;
            Vector3 Max01   = (LocalUp * .5f + (minx - .5f) * AxisA + ((miny + width) - .5f) * AxisB).normalized * r + PlanetCentre;
            Vector3 Max10   = (LocalUp * .5f + ((minx + width) - .5f) * AxisA + (miny - .5f) * AxisB).normalized * r + PlanetCentre;
            Vector3 Max11   = (LocalUp * .5f + ((minx + width) - .5f) * AxisA + ((miny + width) - .5f) * AxisB).normalized * r + PlanetCentre;
            Vector3 nearest = Vector3.Nearest(Camera, Max00, Max01, Max10, Max11);

            //int intersections = Vector3.FindIntersections(PlanetCentre, r - Mathf.FloatEpsilon, nearest, Camera - nearest, out Vector3 nearestpoint, out Vector3 farthestpoint);
            //if (intersections > 0) return;//intersects with sphere
            if (LastResolution == resolution)
            {
                OpenGl.Draw(graphics, LastMesh, transform, onlyborders, shader, zoom);
                return;
            }
            #region initialisation
            int         resolution1 = resolution + 1;
            Vector3[]   vertices    = new Vector3[resolution1 * resolution1];
            int         cnt         = resolution * resolution * 6;
            int[]       triangles   = new int[cnt];
            byte[]      colors      = new byte[cnt];
            int         triIndex    = 0;
            int         i           = 0;
            float       wh          = settings.WaterHeight;
            Color       watercolor  = settings.WaterColor;
            Elevation[] Elevations  = settings.Elevations.ToArray();
            int         elevcount   = Elevations.Length;
            float       Scale       = width / resolution;
            #endregion
            for (int y = 0; y < resolution1; y++)
            {
                for (int x = 0; x < resolution1; x++)
                {
                    #region Calculate Vertices
                    Vector2 percent           = new Vector2(x * Scale + minx, y * Scale + miny);
                    Vector3 pointOnUnitCube   = LocalUp * .5f + (percent.x - .5f) * AxisA + (percent.y - .5f) * AxisB;
                    Vector3 PointOnUnitSphere = pointOnUnitCube.normalized;
                    float   elev = shapeGenerator.noiseFilter.Evaluate(PointOnUnitSphere);
                    vertices[i] = PointOnUnitSphere * (radius + elev * shapeSettings.MaxDifference);// + PlanetCentre;
                    #endregion
                    if (x != resolution && y != resolution)
                    {
                        Color color = watercolor;
                        if (elev > wh + Mathf.FloatEpsilon)
                        {
                            for (int j = 0; j < elevcount; j++)
                            {
                                if (elev < Elevations[j].height + Mathf.FloatEpsilon)
                                {
                                    color = Elevations[j].color;
                                    break;
                                }
                            }
                        }
                        #region Calculate Colors
                        colors[triIndex]     = color.R;
                        colors[triIndex + 1] = color.G;
                        colors[triIndex + 2] = color.B;
                        colors[triIndex + 3] = color.R;
                        colors[triIndex + 4] = color.G;
                        colors[triIndex + 5] = color.B;
                        #endregion
                        #region Calculate Triangles
                        triangles[triIndex]     = i;
                        triangles[triIndex + 1] = i + resolution1 + 1;
                        triangles[triIndex + 2] = i + resolution1;
                        triangles[triIndex + 3] = i;
                        triangles[triIndex + 4] = i + 1;
                        triangles[triIndex + 5] = i + resolution1 + 1;
                        #endregion
                        triIndex += 6;
                    }
                    i++;
                }
            }
            //mesh.Clear();
            LastMesh = new Mesh(vertices, triangles, colors);
            OpenGl.Draw(graphics, LastMesh, transform, onlyborders, shader, zoom);
            LastResolution = resolution;
            //mesh.shader = shader;
            //mesh.RecalculateNormals();
        }