예제 #1
0
        private void ScaleHeights(float L)
        {
            label label = new label(SetToolStripLabel);
            progres progres = new progres(SetProgressBar);
            form.statusStrip1.Invoke(label, "Prebieha škálovanie výšok...");
            LinearFunction map_z;
            LinearFunction percenta = new LinearFunction(50.0f, 1.0f, 0.5f, 0.01f);
            if((maxX-minX)>(maxY-minY))
                map_z = new LinearFunction(min, max, 0, (maxX - minX) * percenta.Value(L));
            else
                map_z = new LinearFunction(min, max, 0, (maxY - minY) * percenta.Value(L));

            //System.Diagnostics.Debug.WriteLine("[{0},{1}]",map_z.Value(min),map_z.Value(max));
            //System.Windows.Forms.MessageBox.Show("["+map_z.Value(min)+","+map_z.Value(max)+"]");

            int d = coords.Count / 100;
            float dx = minX + (maxX - minX) / 2.0f;
            float dy = minY + (maxY - minY) / 2.0f;

            for (int i = 0; i < coords.Count; i++)
            {
                //vertices[i] = new Vector3( -coords[i].X + dx, coords[i].Y - dy, coords[i].Z / (L * dl) - dz / (L * dl));
                vertices[i] = new Vector3(-coords[i].X + dx, coords[i].Y - dy, map_z.Value(coords[i].Z));
                if (i % d == 0)
                    form.statusStrip1.Invoke(progres, i * 100 / coords.Count);
            }
        }
예제 #2
0
        private void LoadData(string pathToFile)
        {
            label label = new label(SetToolStripLabel);
            progres progres = new progres(SetProgressBar);
            form.statusStrip1.Invoke(label, "Prebieha načítavanie dát...");
            int count = File.ReadLines(pathToFile).Count();
            int d = count / 100;
            StreamReader sr;
            Vector3 tmp;
            char[] separator = { ' ', '\t' };
            string[] line;
            try
            {
                sr = new StreamReader(pathToFile);
                while (!sr.EndOfStream)
                {
                    line = sr.ReadLine().Split(separator);
                    tmp = new Vector3(float.Parse(line[0]), float.Parse(line[1]), float.Parse(line[2]));
                    coords.Add(tmp);
                    if (coords.Last().X < minX)
                        minX = coords.Last().X;
                    if (coords.Last().X > maxX)
                        maxX = coords.Last().X;
                    if (coords.Last().Y < minY)
                        minY = coords.Last().Y;
                    if (coords.Last().Y > maxY)
                        maxY = coords.Last().Y;
                    if (coords.Last().Z < min)
                        min = coords.Last().Z;
                    if (coords.Last().Z > max)
                        max = coords.Last().Z;

                    if (coords.Count % d == 0)
                        form.statusStrip1.Invoke(progres, 100 * coords.Count / count);
                }
                sr.Close();
            }
            catch (FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("Subor sa nenasiel!");
            }
        }
예제 #3
0
        public Surface(int w, int h, string pathToFile, System.Windows.Forms.ToolStripProgressBar bar = null, System.Windows.Forms.ToolStripLabel label = null, Form1 form = null, bool shaderOption = false)
        {
            this.shaderOption = shaderOption;
            this.form = form;
            toolStripBar = bar;
            toolStripLabel = label;
            WhatToDraw = 1;
            Status = colrscl = rotate = false;
            minX = minY = min = float.MaxValue;
            maxX = maxY = max = float.MinValue;
            scale = 1.0f;
            angleX = angleY = 0.0f;
            width = w; height = h;
            NumOfIndexes = NumOfVertices = Xwidth = Ywidth = 0;
            //svetlo - smer,ambient,specular,diffuse
            light = new DirectionalLight(new Vector3(0.0f, 0.0f, -1.0f), new Vector3(1.0f, 1.0f, 1.0f), new Vector3(1.0f, 1.0f, 1.0f), new Vector3(1.0f, 1.0f, 1.0f));
            //material - ambient,specular,diffuse,koeficienty - ambient, specular, diffuse, shininess
            material = new Material(0.29f, 0.86f, 0.57f, 128);
            //BigBrother = new Kamera(new Vector3(0.0f, 0.0f, 3.5f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));

            coords = new List<Vector3>();
            VBO = new int[4];
            VAO = new int[1];
            VertexShader = new Shaders.Shader();
            FragmentShader = new Shaders.Shader();
            spMain = new Shaders.ShaderProgram();

            var bw = new System.ComponentModel.BackgroundWorker();

            bw.DoWork += (sender, args) =>
            {
                // do your lengthy stuff here -- this will happen in a separate thread
                LoadData(pathToFile);
                if (Status = SetWidth())
                {
                    vertices = new Vector3[NumOfVertices];
                    color = new Vector3[NumOfVertices];
                    normals = new Vector3[NumOfVertices];
                    Indices = new List<int>();
                    Indexes = new int[NumOfIndexes];
                    //SetIndices();
                    SetIndexes();
                    if (Ywidth > Xwidth)
                    {
                        rotate = true;
                        MatrixStore_Rotations = Matrix4.CreateFromAxisAngle(new Vector3(0.0f, 0.0f, 1.0f), -90.0f * (float)Math.PI / 180.0f);
                    }
                }
                else
                {
                    //dorobit sem este resetovanie progressBaru/labelu
                    System.Windows.Forms.MessageBox.Show("Súbor " + pathToFile + " nemá podporu!", "Vnimanie!", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                    label lab = new label(SetToolStripLabel);
                    progres progres = new progres(SetProgressBar);
                    form.statusStrip1.Invoke(lab, "Súbor nemá podporu...");
                    form.statusStrip1.Invoke(progres, 0);
                }
            };

            bw.RunWorkerCompleted += (sender, args) =>
            {
                if (args.Error != null)  // if an exception occurred during DoWork,
                    System.Windows.Forms.MessageBox.Show(args.Error.ToString());  // do your error handling here

                if (Status)
                {
                    colorScale = new ColorScale(min, max, width, height);
                    CalculateColor();
                    ScaleHeights(10.0f);
                    CalculateNormals();
                    InitScene(false);
                    FirstDraw();
                    form.SetBoolean_surface(true);
                }
                else
                    form.SetMenuStrip_Enabled(true);
            };

            bw.RunWorkerAsync(); // starts the background worker
        }
예제 #4
0
        private void InitScene(bool b)
        {
            label label = new label(SetToolStripLabel);
            progres progres = new progres(SetProgressBar);
            form.statusStrip1.Invoke(label, "Prebieha nastavovanie scény...");
            form.statusStrip1.Invoke(progres, 0);

            SetMatrices(b);

            GL.GenBuffers(4, VBO);
            GL.GenVertexArrays(1, VAO);

            GL.BindVertexArray(VAO[0]);

            //vrcholy
            GL.BindBuffer(BufferTarget.ArrayBuffer, VBO[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(NumOfVertices * Vector3.SizeInBytes), vertices, BufferUsageHint.StaticDraw);
            GL.EnableVertexAttribArray(0);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);

            //farby
            GL.BindBuffer(BufferTarget.ArrayBuffer, VBO[1]);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(NumOfVertices * Vector3.SizeInBytes), color, BufferUsageHint.StaticDraw);
            GL.EnableVertexAttribArray(1);
            GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 0, 0);

            //normaly
            GL.BindBuffer(BufferTarget.ArrayBuffer, VBO[2]);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(NumOfVertices * Vector3.SizeInBytes), normals, BufferUsageHint.StaticDraw);
            GL.EnableVertexAttribArray(2);
            GL.VertexAttribPointer(2, 3, VertexAttribPointerType.Float, false, 0, 0);

            //indices
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBO[3]);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(sizeof(int) * Indices.Count), Indices.ToArray(), BufferUsageHint.StaticDraw);
            GL.Enable(EnableCap.PrimitiveRestart);
            GL.PrimitiveRestartIndex(coords.Count);
            //GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(sizeof(int) * Indexes.Length), Indexes, BufferUsageHint.StaticDraw);

            if (shaderOption)
            {
                //per pixel
                if (!VertexShader.LoadShaderS(Kocka.Properties.Resources.perPixelShaderVert, ShaderType.VertexShader))
                    System.Windows.Forms.MessageBox.Show("Nepodarilo sa nacitat vertex sahder!");
                if (!FragmentShader.LoadShaderS(Kocka.Properties.Resources.perPixelShaderFrag, ShaderType.FragmentShader))
                    System.Windows.Forms.MessageBox.Show("Nepodarilo sa nacitat fragment sahder!");
            }
            else
            {
                //per fragment
                if (!VertexShader.LoadShaderS(Kocka.Properties.Resources.perFragmentShaderVert, ShaderType.VertexShader))
                    System.Windows.Forms.MessageBox.Show("Nepodarilo sa nacitat vertex sahder!");
                if (!FragmentShader.LoadShaderS(Kocka.Properties.Resources.perFragmentShaderFrag, ShaderType.FragmentShader))
                    System.Windows.Forms.MessageBox.Show("Nepodarilo sa nacitat fragment sahder!");
            }

            spMain.CreateProgram();
            spMain.AddShaderToProgram(VertexShader);
            spMain.AddShaderToProgram(FragmentShader);
            spMain.LinkProgram();
            spMain.UseProgram();

            spMain.SetUniform("projectionMatrix", projectionMatrix);
            spMain.SetUniform("eye", new Vector3(0.0f, 0.0f, 30.0f));
            material.SetMaterialUniforms(spMain);
            light.SetDirectionalLightUniforms(spMain);
        }
예제 #5
0
        private void CalculateNormals()
        {
            label label = new label(SetToolStripLabel);
            progres progres = new progres(SetProgressBar);
            form.statusStrip1.Invoke(label, "Prebieha výpočet normál...");
            int d = NumOfIndexes / 300;

            for (int i = 0; i < NumOfIndexes / 3; i++)
            {
                int index1 = Indexes[i * 3];
                int index2 = Indexes[i * 3 + 1];
                int index3 = Indexes[i * 3 + 2];

                Vector3 side1 = vertices[index1] - vertices[index3];
                Vector3 side2 = vertices[index2] - vertices[index1];
                Vector3 normal = Vector3.Cross(side1, side2);

                normals[index1] += normal;
                normals[index2] += normal;
                normals[index3] += normal;
                if (i % d == 0)
                {
                    float tmp = (3 * i / (float)NumOfIndexes);
                    form.statusStrip1.Invoke(progres, (int)(100 * tmp));
                }
            }
        }
예제 #6
0
 private void CalculateColor()
 {
     label label = new label(SetToolStripLabel);
     progres progres = new progres(SetProgressBar);
     form.statusStrip1.Invoke(label, "Prebieha nastavovanie farieb...");
     color = colorScale.SetColorList(coords).ToArray();
 }