示例#1
0
 /// <summary>
 /// Draws this instance.
 /// </summary>
 public void Draw()
 {
     ShaderProgram.Activate();
     foreach (var uniform in uniforms)
     {
         uniform.Update(ShaderProgram);
     }
     if (TextureBindings is null)
     {
         Drawable.Draw();
     }
     else
     {
         DrawTools.BindTextures(ShaderProgram, TextureBindings);
         Drawable.Draw();
         DrawTools.UnbindTextures(TextureBindings);
     }
     ShaderProgram.Deactivate();
 }
示例#2
0
        private void glControl3_Paint(object sender, PaintEventArgs e)
        {
            Matrix4 modelMatrix = Matrix4.Identity; //Por ahora usamos la identidad.
            Matrix4 viewMatrix  = myCamera.getViewMatrix();
            Matrix4 projMatrix  = myCamera.getProjectionMatrix();
            Matrix4 mvMatrix    = Matrix4.Mult(viewMatrix, modelMatrix);
            Vector4 figColor    = new Vector4(1.0f, 1.0f, 0.0f, 1.0f); // amarillo
            Matrix4 rotacion;
            Matrix4 traslacion;
            Matrix4 mmat = Matrix4.Identity;

            gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); //Borramos el contenido del glControl.
            int i = 0;

            gl.Viewport(viewport); //Especificamos en que parte del glControl queremos dibujar..
            //gl.Enable(EnableCap.DepthTest);
            sProgram.Activate();   //Activamos el programa de shaders
            //seteamos los valores uniformes.
            sProgram.SetUniformValue("figureColor", figColor);
            sProgram.SetUniformValue("projMat", projMatrix);
            sProgram.SetUniformValue("mvMat", mvMatrix);

            //Dibujamos los ejes de referencia.
            ejes_globales.Dibujar(sProgram);

            // Transformaciones que vamos a utilizar
            modelMatrix = Matrix4.Identity;                            // Guardamos la matriz identidad en la variable modelMatrix.
            rotacion    = Matrix4.CreateRotationY(45.0f);              // Rotacion de 45 grados respecto al eje-y.
            traslacion  = Matrix4.CreateTranslation(0.5f, 0.0f, 0.0f); // Traslacion de 0.5 unidades sobre el eje-x.

            // Esto es solo un ejemplo de como podemos crear la trasalcion de manera manual.
            // CUIDAD QUE PARA UTILIZARLA HAY QUE TRASPONERLA! (Recordar lo que se explica en la clase).
            Matrix4 traslacion_a_mano = new Matrix4
                                            (1.0f, 0.0f, 0.0f, 0.5f,
                                            0.0f, 1.0f, 0.0f, 0.0f,
                                            0.0f, 0.0f, 1.0f, 0.0f,
                                            0.0f, 0.0f, 0.0f, 1.0f);

            // Interacciones para aplicar las transformaciones.
            int cantMatrices = matrices.Count;

            switch (transformaciones)
            {
            case 0:
                //No hago nada, se dibujo el cubo en su posición inicial.
                break;

            //Probamos que pasa cuando acumulamos transformaciones de IZQUIERDA a DERECHA
            case 1:

                modelMatrix = Matrix4.Mult(rotacion, modelMatrix);         //( rotacion * model )
                for (int j = 0; j < matrices.Count; j++)
                {
                    matrices[j] = Matrix4.Mult(rotacion, matrices[j]);
                }
                i          = 0;
                primeraVez = 0;
                break;

            case 2:
                modelMatrix = Matrix4.Mult(rotacion, modelMatrix);
                modelMatrix = Matrix4.Mult(traslacion, modelMatrix);     //( traslacion * ( rotacion * model ) )
                for (int j = 0; j < matrices.Count; j++)
                {
                    matrices[j] = Matrix4.Mult(rotacion, matrices[j]);
                    matrices[j] = Matrix4.Mult(traslacion, matrices[j]);
                }
                i          = 0;
                primeraVez = 0;
                break;

            case 3:
                modelMatrix = Matrix4.Mult(traslacion, modelMatrix);     //( traslacion * model )
                for (int j = 0; j < matrices.Count; j++)
                {
                    matrices[j] = Matrix4.Mult(traslacion, matrices[j]);
                }
                i          = 0;
                primeraVez = 0;
                break;

            case 4:
                modelMatrix = Matrix4.Mult(traslacion, modelMatrix);
                modelMatrix = Matrix4.Mult(rotacion, modelMatrix);     //( rotacion * ( traslacion * model ) )
                for (int j = 0; j < matrices.Count; j++)
                {
                    matrices[j] = Matrix4.Mult(traslacion, matrices[j]);
                    matrices[j] = Matrix4.Mult(rotacion, matrices[j]);
                }
                i          = 0;
                primeraVez = 0;
                break;

            //Probamos que pasa cuando acumulamos transformaciones de DERECHA a IZQUIERDA
            case 5:
                modelMatrix = Matrix4.Mult(modelMatrix, rotacion);     //( model * rotacion )
                break;

            case 6:
                modelMatrix = Matrix4.Mult(modelMatrix, rotacion);
                modelMatrix = Matrix4.Mult(modelMatrix, traslacion);     //( model * ( rotacion * traslacion ) )
                break;

            case 7:
                modelMatrix = Matrix4.Mult(modelMatrix, traslacion);     //( model * traslacion )
                break;

            case 8:
                modelMatrix = Matrix4.Mult(modelMatrix, traslacion);
                modelMatrix = Matrix4.Mult(modelMatrix, rotacion);     //( model * ( traslacion * rotacion ) )
                break;
            }
            List <ObjetoGrafico> lista = mundo.getObjetos();

            DoublyConectedEdgeList l;

            //i = 0;

            if (matrices.Count == 0)
            {
                for (int j = 0; j < lista.Count; j++)
                {
                    matrices.Add(new Matrix4());
                    matrices[j] = Matrix4.Identity;
                }
                primeraVez = 1;
            }
            else
            {
                primeraVez = 0;
            }

            foreach (ObjetoGrafico o in lista)
            {
                l = o.getEstructura();
                Vector3 v = l.getTraslacion();
                Vector3 r = l.getRotacion();
                traslacion = Matrix4.CreateTranslation(v.X, v.Y, v.Z);

                //  modelMatrix = Matrix4.Identity;
                Matrix4 rotacionx = Matrix4.Identity;
                Matrix4 rotaciony = Matrix4.Identity;
                Matrix4 rotacionz = Matrix4.Identity;

                if (r.X != 0f)
                {
                    Console.WriteLine(" x" + r.X);

                    //  rotacionx = Matrix4.CreateRotationX((float)((r[0] * Math.PI) / (180)));
                    rotacionx = Matrix4.CreateRotationX(((float)r[0] * (float)Math.PI) / (180));
                    Console.WriteLine("larotacion es: en x");
                    Console.WriteLine(rotacionx.ToString());
                }
                if (r.Y != 0f)
                {
                    Console.WriteLine(" y" + r.Y);
                    rotaciony = Matrix4.CreateRotationY((float)((float)(r[1] * Math.PI) / (180)));
                    Console.WriteLine("la rotacion es: en y");
                    Console.WriteLine(rotaciony.ToString());
                }
                if (r.Z != 0f)
                {
                    Console.WriteLine(" z " + r.Z);
                    rotacionz = Matrix4.CreateRotationZ(((float)((float)(r[2] * Math.PI) / (180))));
                    Console.WriteLine("la rotacion es: en z");
                    Console.WriteLine(rotacionz.ToString());
                }
                //multiplicacion de matrices:prinmero rotacion y luego traslacion.. se lee de izquierda a a derecha en opentk
                //matematicamente se lee de derecha a izquierda


                //   modelMatrix = Matrix4.Mult(modelMatrix, rotacion);
                //  modelMatrix = Matrix4.Mult(modelMatrix, traslacion); //( model * ( rotacion * traslacion ) )

                if (primeraVez == 1)
                {
                    matrices[i] = Matrix4.Mult(matrices[i], rotacionx);
                    matrices[i] = Matrix4.Mult(matrices[i], rotaciony);
                    matrices[i] = Matrix4.Mult(matrices[i], rotacionz);
                    matrices[i] = Matrix4.Mult(matrices[i], Matrix4.CreateTranslation(v.X, v.Y, v.Z));
                }
                mvMatrix = Matrix4.Mult(matrices[i], viewMatrix);


                figColor = new Vector4(l.getColor(), 1.0f);
                sProgram.SetUniformValue("figureColor", figColor);
                sProgram.SetUniformValue("mvMat", mvMatrix);
                o.Dibujar(sProgram);

                i++;
            }
            //}//fin de bloque por primera vez

            //Dibujamos el cubo.
            //    modelMatrix = Matrix4.Identity;
            sProgram.SetUniformValue("figureColor", figColor); //Seteamos color amarillo al shader de fragmentos.
            mvMatrix = Matrix4.Mult(modelMatrix, viewMatrix);
            sProgram.SetUniformValue("mvMat", mvMatrix);
            myCube.Dibujar(sProgram);
            ejes_locales.Dibujar(sProgram);

            sProgram.Deactivate();    //Desactivamos el programa de shader.

            glControl3.SwapBuffers(); //Intercambiamos buffers frontal y trasero, para evitar flickering.
        }