Ejemplo n.º 1
0
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);

            //	Resize the DIB Surface.
            OpenGL.Create(Width, Height);

            //	Set the viewport.
            gl.Viewport(0, 0, Width, Height);

            //  If we have a project handler, call it...
            if (Width != -1 && Height != -1)
            {
                if (Resized != null)
                {
                    Resized(this, e);
                }
                else
                {
                    //  Otherwise we do our own projection.
                    gl.MatrixMode(OpenGL.PROJECTION);
                    gl.LoadIdentity();

                    // Calculate The Aspect Ratio Of The Window
                    gl.Perspective(45.0f, (float)Width / (float)Height, 0.1f, 100.0f);

                    gl.MatrixMode(OpenGL.MODELVIEW);
                    gl.LoadIdentity();
                }
            }

            Invalidate();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ApplicationState"/> class.
        /// </summary>
        private ApplicationState()
        {
            //  Create the OpenGL instance.
            OpenGL = new OpenGL();

            //  Initialise the OpenGL instance.
            OpenGL.Create(RenderContextType.FBO,
                800, 600, 24, null);
        }
Ejemplo n.º 3
0
        protected override void OnSizeChanged(EventArgs e)
        {
            //  Don't call the base- we handle sizing ourselves.

            //	Resize the DIB Surface.
            OpenGL.Create(Width, Height);

            //	OpenGL needs to resize the viewport.
            scene.Resize(Width, Height);

            Invalidate();
        }
Ejemplo n.º 4
0
        public void Initialize(IntPtr handle, Viewport viewport)
        {
            this.viewport = viewport;

            gl = new OpenGL();
            gl.Create(RenderContextType.NativeWindow, viewport.Width, viewport.Height, 32, handle);
            gl.MakeCurrent();

            ShaderManager.Initialize(gl);
            MaterialManager.Initialize(gl);

            renderer = new Renderer();
            renderer.Initialize(gl);

            camera = new PerspectiveCamera();
            camera.LocalTranslate(new Vector(0, 0, 3));

            var mesh = new Mesh(gl, MeshType.Points);
            mesh.AddBuffer(
                new Buffer<Point>(gl,
                    new Point(-2, -2, 0),
                    new Point(-1, -1, 0),
                    new Point( 0,  0, 0),
                    new Point( 1, -1, 0),
                    new Point( 2, -2, 0)));

            scene = new Scene();
            scene.Add(camera);

            for (int j = -10; j < 10; j++)
            for (int i = -10; i < 10; i++)
            {
                var e = new PhysicalEntity(mesh, MaterialManager.GetMaterial("Voxels"));
                e.GlobalTranslate(Vector.UnitZ * -5 * i);
                e.GlobalTranslate(Vector.UnitX * -5 * j);
                scene.Add(e);
            }

            gl.MakeNothingCurrent();
        }
Ejemplo n.º 5
0
 private bool createRenderContext(OpenGL gl, int width, int height)
 {
     return gl.Create(
     OpenGLVersion.OpenGL3_3,
     RenderContextType.FBO,
     width,
     height,
     32,
     null);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Call this function to iniatilise a scene loaded from a file.
        /// </summary>
        /// <param name="ctrl">The OpenGLCtrl.</param>
        /// <param name="sceneType">The scene type.</param>
        public virtual void IntialiseFromSerial(int width, int height, SceneType sceneType)
        {
            //	Create OpenGL.
            gl = new OpenGL();
            gl.Create(width, height);

            //	Find out how many lights OpenGL can support.
            int [] umaxlights = new int[1];
            gl.GetInteger(OpenGL.MAX_LIGHTS, umaxlights);

            //	Re-create as many openGL lights as we can.
            for(uint u = 0; u < umaxlights[0]; u++)
            {
                if(u < Lights.Count)
                {
                    Light light = lights[(int)u];
                    light.GLCode = OpenGL.LIGHT0 + u;
                }
            }

            //	Re-create all the quadrics
            foreach(Quadric quadric in quadrics)
                quadric.Create(gl);

            //	Set the scene type.
            SetSceneType(sceneType);

            //	Initialise stock drawing.
            gl.InitialiseStockDrawing();

            //	Set the Scene OpenGL for the quadrics.
            Quadric.SceneOpenGL = OpenGL;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Create device and renderbuffer, initialize NV_DX_interop and start rendering.
        /// </summary>
        private void StartRendering()
        {
            _gl = new OpenGL();

            _hwnd = new HwndSource(0, 0, 0, 0, 0, "test", IntPtr.Zero).Handle;

            _gl.Create(SharpGL.Version.OpenGLVersion.OpenGL2_1, RenderContextType.HiddenWindow, 1, 1, 32, _hwnd);

            //  Set the most basic OpenGL styles.
            _gl.ShadeModel(OpenGL.GL_SMOOTH);
            _gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            _gl.ClearDepth(1.0f);

            _gl.Enable(OpenGL.GL_BLEND);
            _gl.Disable(OpenGL.GL_DEPTH_TEST);

            _gl.Enable(OpenGL.GL_TEXTURE_2D);
            _gl.TexEnv(OpenGL.GL_TEXTURE_ENV, OpenGL.GL_TEXTURE_ENV_MODE, OpenGL.GL_REPLACE);

            _gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);

            //_gl.Enable(OpenGL.GL_MULTISAMPLE);
            //_gl.Enable(OpenGL.GL_MULTISAMPLE_ARB);

            _gl.Hint(0x8534, OpenGL.GL_FASTEST);

            ResizeRendering();

            // leverage the Rendering event of WPF's composition target to
            // update the custom D3D scene
            CompositionTarget.Rendering += OnRenderOpenGL;

            if (GLInitialize != null)
                GLInitialize(this, new EventArgs());
        }
        /// <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;
        }
Ejemplo n.º 9
0
 //private ShaderProgram _program;
 // private ShaderProgram _flatprogram;
 public OpenGLRenderer()
 {
     Name = "OpenGL";
     GL = new OpenGL();
     GL.Create(RenderContextType.FBO, 1, 1, 32, null);
 }