Exemplo n.º 1
0
        /**
         * glControl 방식의 x,y 좌표를 윈도우 방식의 x,y 좌표로 변환 및 Z-buffer 값을 찾아냄. (winX/Y/Z)
         * winZ의 경우 GL.ReadPixels를 통해서 현재 커서가 가르키는 물체의 공간상에서의 z좌표를 읽어와 사용함.
         * 커서가 물체에서 벗어날 경우 다른 좌표를 찍게 되는데 면이 아닌 점인 경우,
         * 마우스가 점 위에 위치하지 않으면 잘못 된 좌표를 찾게 될 수 있으므로 주의해야함.
         *
         * OpenTK.Graphics.Glu에서 지원하는 UnProject 함수를 통해서 mouseCoordOn3d에 3차원 위치 좌표를 저장
         * */
        private bool Get3dVertexPosition(int mouseX, int mouseY)
        {
            int[]    viewport    = new int[4];
            double[] modelMatrix = new double[16];
            double[] projMatrix  = new double[16];

            float winX = 0, winY = 0, winZ = 0;

            GL.GetInteger(GetPName.Viewport, viewport);
            GL.GetDouble(GetPName.ModelviewMatrix, modelMatrix);
            GL.GetDouble(GetPName.ProjectionMatrix, projMatrix);

            winX = mouseX;
            winY = viewport[3] - mouseY;
            GL.ReadPixels((int)winX, (int)winY, 1, 1, PixelFormat.DepthComponent, PixelType.Float, ref winZ);

            Glu.UnProject(new Vector3(winX, winY, winZ), modelMatrix, projMatrix, viewport, out mouseCoordOn3d);

            if ((mouseCoordOn3d.X < 500 && mouseCoordOn3d.X > -500))
            {
                if ((mouseCoordOn3d.Z < 500 && mouseCoordOn3d.Z > -500))
                {
                    return(true); // 원점으로부터 전후좌우 500 이내의 영역은 mesh 위의 점이라고 판단한다.
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 2
0
        private void gl_Paint(object sender, PaintEventArgs e)
        {
            // Preconfiguracion
            if (!loaded)
            {
                return;
            }
            if (GList == null)
            {
                return;
            }
            loaded = false;
            //--


            // Camara
            GL.LoadIdentity();
            Glu.LookAt(camare.position[0], camare.position[1], camare.position[2], camare.view[0], camare.view[1], camare.view[2], 0, 1, 0);
            //--


            // Luz
            ligth.positionLigth[0]  = (float)camare.position[0];
            ligth.positionLigth[1]  = (float)camare.position[1];
            ligth.positionLigth[2]  = (float)camare.position[2];
            ligth.directionLigth[0] = (float)(camare.view[0] - camare.position[0]);
            ligth.directionLigth[1] = (float)(camare.view[1] - camare.position[1]);
            ligth.directionLigth[2] = (float)(camare.view[2] - camare.position[2]);
            ligth.directionLigth    = Engine.OperationMatrixVector.normalize(ligth.directionLigth);

            GL.Light(LightName.Light0, LightParameter.Position, ligth.positionLigth);
            GL.Light(LightName.Light0, LightParameter.SpotDirection, ligth.directionLigth);

            if (ligth.turnOn)
            {
                GL.Enable(EnableCap.Lighting);
            }
            else
            {
                GL.Disable(EnableCap.Lighting);
            }
            //--


            // Profundidad
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            //--


            // Truco de transparencia
            if (isTransparency)
            {
                GL.Enable(EnableCap.Blend);
            }
            else
            {
                GL.Disable(EnableCap.Blend);
            }
            //--


            // Objetos
            GL.PushMatrix();
            for (int i = 0; i < GList.Count; i++)
            {
                GL.CallList(GList[i]);
            }
            GL.PopMatrix();
            GL.Finish();
            //--


            // Posconfiguracion
            gl.SwapBuffers();
            loaded = true;
            //--
        }
Exemplo n.º 3
0
        public void drawMap()
        {
            GList = new List <Int32>();
            GList.Add(0);

            foreach (Objects.Plane plane in map.planes)
            {
                GL.NewList(GList.Count, ListMode.Compile);
                GL.BindTexture(TextureTarget.Texture2D, Engine.Texture.LoadTexture(plane.textureName, plane.repeat, plane.rotate));
                GL.Color3(plane.color);
                foreach (Objects.Rectangle rectangle in plane.rectangles)
                {
                    GL.Begin(BeginMode.Polygon);
                    for (int i = 0; i < rectangle.points.Count; i++)
                    {
                        if (i == 0)
                        {
                            GL.TexCoord2(0, 0);
                        }
                        if (i == 1)
                        {
                            GL.TexCoord2(1, 0);
                        }
                        if (i == 2)
                        {
                            GL.TexCoord2(1, 1);
                        }
                        if (i == 3)
                        {
                            GL.TexCoord2(0, 1);
                        }

                        GL.Vertex3(rectangle.points[i][0], rectangle.points[i][1], rectangle.points[i][2]);
                    }
                    GL.End();
                }
                GL.EndList();
                GList.Add(GList.Count);
            }

            foreach (Objects.Sphere sphere in map.spheres)
            {
                GL.NewList(GList.Count, ListMode.Compile);
                GL.PushMatrix();
                GL.Color3(sphere.color);
                IntPtr q = Glu.NewQuadric();

                GL.Translate(sphere.position[0], sphere.position[1], sphere.position[2]);
                Glu.Sphere(q, sphere.radio, 100, 100);
                GL.Translate(-sphere.position[0], -sphere.position[1], -sphere.position[2]);

                GL.PopMatrix();
                GL.EndList();
                GList.Add(GList.Count);
            }

            foreach (Objects.Cone cone in map.cones)
            {
                GL.NewList(GList.Count, ListMode.Compile);
                GL.PushMatrix();
                GL.Color3(cone.color);
                IntPtr q = Glu.NewQuadric();

                GL.Translate(cone.position[0], cone.position[1], cone.position[2] - (cone.height / 2));
                Glu.Cylinder(q, 0, cone.radio, cone.height, Engine.Constant.QUALITY, Engine.Constant.QUALITY);
                GL.Translate(-cone.position[0], -cone.position[1], -cone.position[2] + (cone.height / 2));

                GL.PopMatrix();
                GL.EndList();
                GList.Add(GList.Count);
            }

            gl_Paint(gl, null);
            loaded = true;
        }
Exemplo n.º 4
0
        private void gl_Load(object sender, EventArgs e)
        {
            // Perspectivas
            GL.Viewport(0, 0, gl.Size.Width - 1, gl.Size.Height - 1);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            Glu.Perspective(90, (gl.Size.Width / gl.Size.Height), 0.1f, 10000f);
            GL.MatrixMode(MatrixMode.Modelview);
            //--


            // Profundidad
            GL.ClearColor(Color.Black);
            GL.ClearDepth(1.0);
            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Lequal);
            //--


            // Tipo de Material
            float[] mat_especular = { 1.0f, 1.0f, 1.0f, 1.0f };
            float[] mat_brillo    = { 50.0f };

            GL.ShadeModel(ShadingModel.Smooth);
            GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Specular, mat_especular);
            GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Shininess, mat_brillo);
            GL.Enable(EnableCap.ColorMaterial);
            //--


            // Textura
            GL.Enable(EnableCap.Texture2D);
            //--


            // Camara
            camare = new Engine.Camera(0, 0, -10, 0, 0, -1, 4, 2.0, 0.2, 0.1, 0.5);
            //--


            // Luz
            float[] white = { 1.0f, 1.0f, 1.0f, 1.0f };
            ligth = new Engine.Light();

            GL.Light(LightName.Light0, LightParameter.Ambient, white);
            GL.Light(LightName.Light0, LightParameter.Diffuse, white);
            GL.Light(LightName.Light0, LightParameter.Specular, white);
            GL.Light(LightName.Light0, LightParameter.SpotCutoff, 10);
            GL.Light(LightName.Light0, LightParameter.ConstantAttenuation, 2);
            GL.Enable(EnableCap.Light0);
            //--


            // Trucos
            GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.One);
            //--


            // Preconfiguracion de presentacion y nivel
            level        = -1;
            template     = Engine.Template.levelCompleted(-2);
            ligth.turnOn = false;
            drawTemplate();
            camare.levelCompleted = true;
            //--


            // Iniciar sonido
            Engine.Sound.play("init.wav");
            //--
        }
Exemplo n.º 5
0
        public void narisi()
        {
            #region simple model
            if (!modelOK)  //če so bile napake pri nalaganju modela, potem nariši osnovno obliko robota
            {
                GL.Begin(BeginMode.Quads);

                GL.PushMatrix();
                GL.LoadIdentity();


                //spordaj osnova
                GL.Color3(Color.Blue);
                GL.Translate(0.0f, 0.0f, -6.0f);
                IntPtr quadric = GLU.NewQuadric();
                GL.Rotate(-90, 1.0f, 0.0f, 0.0f);
                GLU.Cylinder(quadric, 2.0, 2.0, 1.0, 32, 32);

                GL.Translate(0.0f, 0.0f, 1.0f);
                quadric = GLU.NewQuadric();
                GLU.PartialDisk(quadric, 0.1, 2.0, 32, 32, 0, 360);

                // 1 del zgoraj
                GL.Rotate(rotacija1 + 90, 0.0f, 0.0f, 1.0f);
                quadric = GLU.NewQuadric();
                GLU.Cylinder(quadric, 0.5, 0.5, 2.0, 32, 32);
                GL.Translate(0.0f, 0.0f, 2.0f);

                GL.Color3(Color.SkyBlue);
                quadric = GLU.NewQuadric();
                GLU.Sphere(quadric, 0.5, 32, 32);

                GL.Color3(Color.Blue);
                GL.Rotate(-90, 1.0f, 0.0f, 0.0f);
                GL.Translate(0.0f, 0.0f, 0.0f);
                quadric = GLU.NewQuadric();
                GLU.Cylinder(quadric, 0.5f, 0.5f, 1.0f, 32, 32);

                GL.Color3(Color.SkyBlue);
                GL.Translate(0.0f, 0.0f, 1.0f);
                quadric = GLU.NewQuadric();
                GLU.Sphere(quadric, 0.5, 32, 32);

                //2 del
                GL.Color3(Color.Blue);
                quadric = GLU.NewQuadric();
                GL.Rotate(-90, 0.0f, 1.0f, 0.0f);
                GL.Rotate(rotacija2, 1.0f, 0.0f, 0.0f);
                GLU.Cylinder(quadric, 0.5f, 0.5f, 7.0f, 32, 32);

                GL.Color3(Color.SkyBlue);
                GL.Translate(0.0f, 0.0f, 7.0f);
                quadric = GLU.NewQuadric();
                GLU.Sphere(quadric, 0.5, 32, 32);

                GL.Color3(Color.Blue);
                GL.Rotate(-90, 0.0f, 1.0f, 0.0f);
                GL.Translate(0.0f, 0.0f, 0.0f);
                quadric = GLU.NewQuadric();
                GLU.Cylinder(quadric, 0.5f, 0.5f, 1.0f, 32, 32);

                //3del
                GL.Color3(Color.SkyBlue);
                GL.Translate(0.0f, 0.0f, 1.0f);
                quadric = GLU.NewQuadric();
                GLU.Sphere(quadric, 0.5, 32, 32);

                GL.Color3(Color.Blue);
                quadric = GLU.NewQuadric();
                GL.Rotate(-90, 0.0f, 1.0f, 0.0f);
                GL.Rotate((rotacija3 * -1), 1.0f, 0.0f, 0.0f);
                GLU.Cylinder(quadric, 0.5f, 0.5f, 2.3f, 32, 32);

                GL.Color3(Color.SkyBlue);
                quadric = GLU.NewQuadric();
                GL.Translate(0.0f, 0.0f, 2.3f);
                GLU.Cylinder(quadric, 0.7f, 0.7f, 1.0f, 32, 32);


                quadric = GLU.NewQuadric();
                GLU.PartialDisk(quadric, 0.1, 0.7f, 32, 32, 0, 360);

                GL.Translate(0.0f, 0.0f, 1.0f);
                quadric = GLU.NewQuadric();
                GLU.PartialDisk(quadric, 0.1, 0.7f, 32, 32, 0, 360);

                //4 del

                GL.Color3(0.0f, 0.0f, 1.0f);
                quadric = GLU.NewQuadric();
                GL.Rotate(rotacija4, 0.0f, 0.0f, 1.0f);
                GLU.Cylinder(quadric, 0.4f, 0.4f, 2.5f, 32, 32);


                GL.Translate(0.0f, 0.3f, 2.7f);
                GL.Color3(1.0f, 0.0f, 0.0f);
                quadric = GLU.NewQuadric();
                GL.Rotate(90, 1.0f, 0.0f, 0.0f);
                GLU.Cylinder(quadric, 0.1f, 0.1f, 0.6f, 32, 32);



                GL.Translate(0.0f, 0.0f, 0.3f);
                GL.Color3(1.0f, 0.0f, 0.0f);
                quadric = GLU.NewQuadric();
                GL.Rotate(-90, 1.0f, 0.0f, 0.0f);
                GL.Rotate(rotacija5, 0.0f, 1.0f, 0.0f);
                GLU.Cylinder(quadric, 0.01f, 0.01f, 1.0f, 32, 32);

                GL.End();
            }
            #endregion
            else  //če pa ni bilo napak pa nariši naložen LWO objekt
            {
                GL.Rotate(90, 1.0f, 0.0f, 0.0f);

                /* okolica robota  */
                GL.PushMatrix();
                GL.Translate(0.0f, -3.2f, 0.0f);
                risi_model(okolica[2]);
                GL.Translate(-5.0f, 0.0f, -50.0f);
                risi_model(okolica[0]);
                GL.Translate(7.0f, 0.0f, 0.0f);
                risi_model(okolica[0]);
                GL.Translate(-3.5f, 10.0f, 0.0f);
                risi_model(okolica[0]);
                GL.PopMatrix();
                GL.PushMatrix();
                GL.Translate(0.0f, -3.0f, 0.0f);
                risi_model(okolica[1]);
                GL.PopMatrix();

                // robot
                GL.PushMatrix();

                //dolžine posameznih objektov
                //podstavek: 0, -3.2, 0;
                //prvi zglob: 3.75, 8.3, -1.6; rot: z : rot1+90
                //prva roka: 0, 15.35, -0.4; rot: z
                //drugi zglob: 5.2, 3.9, 2.0; rot: x, pa -90 po x
                //druga roka: 10.8, 0, 0; rot: z
                //zadnji zglob: 2.2, 0, 0; rot: x
                //oni krogec za orodja montirat: 0.4, 0, 0


                risi_model(robot_model[0]);  //podstavek

                GL.Rotate(rotacija1 + 90, 0.0f, 1.0f, 0.0f);
                risi_model(robot_model[1]); // prvi motor


                GL.Translate(3.75f, 8.30f, -1.60f); // prva roka
                GL.Rotate(rotacija2, 0.0f, 0.0f, 1.0f);
                risi_model(robot_model[2]);

                GL.Translate(0.0f, 15.35f, -0.4f);
                GL.Rotate(rotacija3, 0.0f, 0.0f, 1.0f);
                risi_model(robot_model[3]);

                GL.Translate(5.2f, 3.9f, 2.0f);
                GL.Rotate(rotacija4, 1.0f, 0.0f, 0.0f);
                GL.Rotate(-90.0f, 1.0f, 0.0f, 0.0f);
                risi_model(robot_model[4]);

                GL.Translate(10.8f, 0.0f, 0.0f);
                GL.Rotate(rotacija5, 0.0f, 0.0f, 1.0f);
                risi_model(robot_model[5]);

                GL.Translate(2.2f, 0.0f, 0.0f);
                GL.Rotate(rotacija6, 1.0f, 0.0f, 0.0f);
                risi_model(robot_model[6]);

                if (set_tool)
                {
                    GL.Translate(0.4f, 0.0f, 0.0f);
                    risi_model(robot_model[7]);
                }

                GL.PopMatrix();
            }
        }