public static void ApplyMVPNParameter(OpenGL gl, ExtShaderProgram esp, Projection pr, ModelView mv, Normal nrml)
        {
            var prms = esp.Parameters as IMVPNParameters;
            var p = esp.Program;

            // Set the matrices.
            p.SetUniformMatrix4(gl, prms.ProjectionMatrixId, pr.ToArray());
            p.SetUniformMatrix4(gl, prms.ModelviewMatrixId, mv.ToArray());
            p.SetUniformMatrix3(gl, prms.NormalMatrixId, nrml.ToArray());
        }
Пример #2
0
        /// <summary>
        /// This method let's the Draw(...) know to retrieve the object at the requested location. When an object is found, the ModelSelectedEvent will be triggered.
        /// </summary>
        /// <param name="p">The 2D point relative to the OpenGL viewport.</param>
        public void GetModelAtPoint(Point p, IEnumerable <ElementAndTransformation> models, HitTestMethod method = HitTestMethod.OpenGLHack)
        {
            return; //TODO!!

            if (p != null && models != null && models.Count() > 0)
            {
                Point correctedPoint = new Point((int)(p.X * _performanceScaleValue), (int)(p.Y * _performanceScaleValue));


                // Create an array that will be the viewport.
                int[] viewport = new int[4];
                // Get the viewport, then convert the mouse point to an opengl point.
                GL.GetInteger(OpenGL.GL_VIEWPORT, viewport);


                // Take deep copy of everything we need.
                var mvCopy = new ModelView()
                {
                    ResultMatrix = ModelView.ModelviewMatrix
                };
                var prCopy = new Projection()
                {
                    ProjectionMatrix = Projection.ProjectionMatrix
                };
                var nrmlCopy = new Normal()
                {
                    NormalMatrix = Normal.NormalMatrix
                };

                Task t = new Task(() =>
                {
                    int selectedIndex = -1;

                    if (method == HitTestMethod.OpenGLHack)
                    {
                        selectedIndex = GetModelAtPointHack(correctedPoint, models, viewport, mvCopy, prCopy, nrmlCopy);
                    }

                    OnModelSelected(correctedPoint, models, selectedIndex);
                });

                t.Start();
            }
        }
 public void ApplyAll(SharpGL.OpenGL gl, ExtShaderProgram esp, Material mat = null, Projection pr = null, ModelView mv = null, Normal nrml = null)
 {
 }
 /// <summary>
 /// Simplify the creation of the normal matrix by using a modelView.
 /// </summary>
 /// <param name="modelView"></param>
 public void CreateFromModelView(ModelView modelView)
 {
     _normalMatrix = modelView.ModelviewMatrix.to_mat3();
 }
        /// <summary>
        /// Applies the Modelview-, projection- and normal matrix to the shaders if the "Parameters" are an implementation of "IMVPNParameters".
        /// </summary>
        /// <param name="gl">The GL.</param>
        /// <param name="modelview">The modelview class that contains the matrix.</param>
        /// <param name="projection">The projection class that contains the matrix.</param>
        /// <param name="normal">The normal class that contains the matrix.</param>
        /// <param name="throwException">Property that sets whether it should throw an exception or just return when this shaderprogram doesn't implement the required interface.</param>
        public void ApplyMVPNMatrices(OpenGL gl, ModelView modelview, Projection projection, Normal normal, bool throwException = true)
        {
            // Test if the parameters implementing the required interface.
            if (!TypeTest(throwException, typeof(IMVPNParameters)))
                return;

            var prms = Parameters as IMVPNParameters;

            prms.ApplyMVPNParameters(gl, this, projection, modelview, normal);
        }
        /// <summary>
        /// This method let's the Draw(...) know to retrieve the object at the requested location. When an object is found, the ModelSelectedEvent will be triggered.
        /// </summary>
        /// <param name="p">The 2D point relative to the OpenGL viewport.</param>
        public void GetModelAtPoint(Point p, IEnumerable<ElementAndTransformation> models, HitTestMethod method = HitTestMethod.OpenGLHack)
        {
            return; //TODO!!

            if (p != null && models != null && models.Count() > 0)
            {
                Point correctedPoint = new Point((int)(p.X * _performanceScaleValue), (int)(p.Y * _performanceScaleValue));

                // Create an array that will be the viewport.
                int[] viewport = new int[4];
                // Get the viewport, then convert the mouse point to an opengl point.
                GL.GetInteger(OpenGL.GL_VIEWPORT, viewport);

                // Take deep copy of everything we need.
                var mvCopy = new ModelView()
                {
                    ResultMatrix = ModelView.ModelviewMatrix
                };
                var prCopy = new Projection()
                {
                    ProjectionMatrix = Projection.ProjectionMatrix
                };
                var nrmlCopy = new Normal()
                {
                    NormalMatrix = Normal.NormalMatrix
                };

                Task t = new Task(() =>
                {
                    int selectedIndex = -1;

                    if (method == HitTestMethod.OpenGLHack)
                    {
                        selectedIndex = GetModelAtPointHack(correctedPoint, models, viewport, mvCopy, prCopy, nrmlCopy);
                    }

                    OnModelSelected(correctedPoint, models, selectedIndex);
                });

                t.Start();
            }
        }
        /// <summary>
        /// Source: http://www.opengl-tutorial.org/miscellaneous/clicking-on-objects/picking-with-an-opengl-hack/
        /// It's recommended to read the source before using this algorithm.
        /// </summary>
        /// <param name="scene">The OpenGLScene.</param>
        /// <param name="point">The 2D point.</param>
        /// <param name="models">The drawn models.</param>
        /// <param name="performanceScaleValue">A factor that affects performance by scaling the size of the temperory viewport.</param>
        /// <returns>The model on this location or null.</returns>
        public static int GetModelAtPointHack(Point point, IEnumerable<ElementAndTransformation> models, 
            int[] viewport, ModelView modelview, Projection projection, Normal normal, float performanceScaleValue = 1)
        {
            return -1; // TODO

            int id = -1;

            int width = (int)(viewport[2] * performanceScaleValue);
            int height = (int)(viewport[3] * performanceScaleValue);
            int x = (int)(point.X * performanceScaleValue);
            int y = height - (int)(point.Y * performanceScaleValue);

            #region create a temperory gl to prevent flickering
            OpenGL gl = new OpenGL();

            // Create OpenGL.
            var openGLVersion = OpenGLVersion.OpenGL2_1;
            var renderContextType = RenderContextType.FBO;
            gl.Create(openGLVersion, renderContextType, 1, 1, 32, null);
            // Set the dimensions and viewport.
            gl.SetDimensions(width, height);
            gl.Viewport(0, 0, width, height);

            // Make GL current.
            gl.MakeCurrent();

            gl.Enable(OpenGL.GL_DEPTH_TEST);
            //gl.Clear(OpenGL.GL_DEPTH_CLEAR_VALUE);

            #endregion create a temperory gl to prevent flickering

            // Initialize the shader for our new GL.
            //var esp = Shaders.LoadSimpleShader(gl);
            //var idxModelTransformation = new List<Tuple<int, int>>(); // Item1 = idx in models; Item2 = idx of model.Transformations

            //var buffersToBeRemoved = new List<uint>();

            //esp.UseProgram(gl, () =>
            //{
            //    // Set the matrices.
            //    esp.ApplyMVPNMatrices(gl, modelview, projection, normal);

            //    var curModelId = 0;

            //    // render models, using a temperory color
            //    for (int i = 0; i < models.Count(); )
            //    {
            //        var model = models.ElementAt(i);

            //        // Extract the color. Since we don't need the i in this loop anymore, we can use it to set the color.
            //        i++; // We don't want to use 0 for the color, so we increment it already. ( = black)
            //        var col = new ColorF(Convert.ToUInt32(i*20));
            //        esp.ApplyMaterial(gl, new Material() { Ambient = col });

            //        // Use the transformation in the model, else use the identity matrix (I do this to override previous transformations)
            //        if (model.Transformation != null)
            //            esp.ApplyTransformationMatrix(gl, model.Transformation.ResultMatrix);
            //        else
            //            esp.ApplyTransformationMatrix(gl, mat4.identity());

            //        var createdBuffers = OGLVisualSceneElementBase.GenerateAndDrawOnce(gl, (OGLVisualSceneElementBase)model.SceneElement); // model.Render(gl, RenderMode.HitTest);
            //        buffersToBeRemoved.AddRange(createdBuffers);
            //    }
            //});
            //esp.Dispose();

            //// Wait for GPU to finish.
            //gl.Flush();
            //gl.Finish();

            //gl.PixelStore(OpenGL.GL_UNPACK_ALIGNMENT, 1);

            //uint format = OpenGL.GL_RGBA;
            //uint type = OpenGL.GL_UNSIGNED_BYTE;

            //byte[] data = new byte[40];
            //gl.ReadPixels(x, y, 1, 1, format, type, data);

            //// Delete the created buffers.
            //gl.DeleteBuffers(buffersToBeRemoved.Count, buffersToBeRemoved.ToArray());

            //// Remove the temperory gl from memory.
            //gl.RenderContextProvider.Dispose();

            //// Get color id from pixel data.
            //id = data[0] + data[1] * 255 + data[2] * 65025; // id = r + g * 255 + b * 255².

            //// if the pixel is black, then there was nothing selected.
            //if (id == 0)
            //{
            //    return -1;
            //}

            //// Return the index of the model and the used transformation.
            //return id - 1;
        }
Пример #8
0
        public enum HitTestMethod { OpenGLHack }//, RayOBB}


        /// <summary>
        /// Source: http://www.opengl-tutorial.org/miscellaneous/clicking-on-objects/picking-with-an-opengl-hack/
        /// It's recommended to read the source before using this algorithm.
        /// </summary>
        /// <param name="scene">The OpenGLScene.</param>
        /// <param name="point">The 2D point.</param>
        /// <param name="models">The drawn models.</param>
        /// <param name="performanceScaleValue">A factor that affects performance by scaling the size of the temperory viewport.</param>
        /// <returns>The model on this location or null.</returns>
        public static int GetModelAtPointHack(Point point, IEnumerable <ElementAndTransformation> models,
                                              int[] viewport, ModelView modelview, Projection projection, Normal normal, float performanceScaleValue = 1)
        {
            return(-1); // TODO

            int id = -1;

            int width  = (int)(viewport[2] * performanceScaleValue);
            int height = (int)(viewport[3] * performanceScaleValue);
            int x      = (int)(point.X * performanceScaleValue);
            int y      = height - (int)(point.Y * performanceScaleValue);


            #region create a temperory gl to prevent flickering
            OpenGL gl = new OpenGL();

            // Create OpenGL.
            var openGLVersion     = OpenGLVersion.OpenGL2_1;
            var renderContextType = RenderContextType.FBO;
            gl.Create(openGLVersion, renderContextType, 1, 1, 32, null);
            // Set the dimensions and viewport.
            gl.SetDimensions(width, height);
            gl.Viewport(0, 0, width, height);


            // Make GL current.
            gl.MakeCurrent();

            gl.Enable(OpenGL.GL_DEPTH_TEST);
            //gl.Clear(OpenGL.GL_DEPTH_CLEAR_VALUE);

            #endregion create a temperory gl to prevent flickering

            // Initialize the shader for our new GL.
            //var esp = Shaders.LoadSimpleShader(gl);
            //var idxModelTransformation = new List<Tuple<int, int>>(); // Item1 = idx in models; Item2 = idx of model.Transformations

            //var buffersToBeRemoved = new List<uint>();

            //esp.UseProgram(gl, () =>
            //{
            //    // Set the matrices.
            //    esp.ApplyMVPNMatrices(gl, modelview, projection, normal);

            //    var curModelId = 0;

            //    // render models, using a temperory color
            //    for (int i = 0; i < models.Count(); )
            //    {
            //        var model = models.ElementAt(i);

            //        // Extract the color. Since we don't need the i in this loop anymore, we can use it to set the color.
            //        i++; // We don't want to use 0 for the color, so we increment it already. ( = black)
            //        var col = new ColorF(Convert.ToUInt32(i*20));
            //        esp.ApplyMaterial(gl, new Material() { Ambient = col });

            //        // Use the transformation in the model, else use the identity matrix (I do this to override previous transformations)
            //        if (model.Transformation != null)
            //            esp.ApplyTransformationMatrix(gl, model.Transformation.ResultMatrix);
            //        else
            //            esp.ApplyTransformationMatrix(gl, mat4.identity());

            //        var createdBuffers = OGLVisualSceneElementBase.GenerateAndDrawOnce(gl, (OGLVisualSceneElementBase)model.SceneElement); // model.Render(gl, RenderMode.HitTest);
            //        buffersToBeRemoved.AddRange(createdBuffers);
            //    }
            //});
            //esp.Dispose();

            //// Wait for GPU to finish.
            //gl.Flush();
            //gl.Finish();

            //gl.PixelStore(OpenGL.GL_UNPACK_ALIGNMENT, 1);

            //uint format = OpenGL.GL_RGBA;
            //uint type = OpenGL.GL_UNSIGNED_BYTE;

            //byte[] data = new byte[40];
            //gl.ReadPixels(x, y, 1, 1, format, type, data);

            //// Delete the created buffers.
            //gl.DeleteBuffers(buffersToBeRemoved.Count, buffersToBeRemoved.ToArray());

            //// Remove the temperory gl from memory.
            //gl.RenderContextProvider.Dispose();

            //// Get color id from pixel data.
            //id = data[0] + data[1] * 255 + data[2] * 65025; // id = r + g * 255 + b * 255².

            //// if the pixel is black, then there was nothing selected.
            //if (id == 0)
            //{
            //    return -1;
            //}

            //// Return the index of the model and the used transformation.
            //return id - 1;
        }
 /// <summary>
 /// Simplify the creation of the normal matrix by using a modelView.
 /// </summary>
 /// <param name="modelView"></param>
 public void CreateFromModelView(ModelView modelView)
 {
     _normalMatrix = modelView.ModelviewMatrix.to_mat3();
 }