예제 #1
0
        /// <summary>
        /// Draw buffers
        /// </summary>
        /// <param name="mode">Element type to draw</param>
        /// <param name="update_values">Specifies if Glorg should update uniforms</param>
        /// <remarks>This function uses the currently set vertex and index buffer (if any) If no index buffer is set, the function will assume that elements follows each other in the vertex buffer.</remarks>
        public void Draw(DrawMode mode, bool update_values)
        {
            bool        found_err = false;
            OpenGLError err;

            GL.glGetError();
            if (update_values)
            {
                modelview_matrix.Update();
                projection_matrix.Update();
                texture_matrix.Update();
                normal_matrix.Update();

                err = (OpenGLError)GL.glGetError();
                if (err != OpenGLError.NoError && !encountered_draw_error)
                {
                    Debugging.Debug.WriteLine("Setting standard uniforms: " + err.ToString());
                    found_err = true;
                }
                if (active_shader != null)
                {
                    foreach (var u in active_shader.uniforms)
                    {
                        u.SetValue();
                    }
                    err = (OpenGLError)GL.glGetError();
                    if (err != OpenGLError.NoError && !encountered_draw_error)
                    {
                        Debugging.Debug.WriteLine("Setting uniforms: " + err.ToString());
                        found_err = true;
                    }
                }
            }
            if (vertex_buffer == null)
            {
                throw new InvalidOperationException("No vertex buffer has been set.");
            }
            if (index_buffer != null)
            {
                GL.glDrawElements((uint)mode, index_buffer.Count, index_buffer.Type, IntPtr.Zero);
            }
            else
            {
                GL.glDrawArrays((uint)mode, 0, vertex_buffer.Count);
            }
            err = (OpenGLError)GL.glGetError();
            if (err != OpenGLError.NoError && !encountered_draw_error)
            {
                Debugging.Debug.WriteLine("Drawing: " + err.ToString());
                found_err = true;
            }
            if (found_err)
            {
                encountered_draw_error = true;
            }
        }
예제 #2
0
        public GraphicsDevice(IntPtr target)
        {
            context = OpenGLContext.GetContext();

            context.Samples = 4;
            // Create context using platform specific methods
            context.CreateContext(target, IntPtr.Zero, null);
            //GL.InitGeneral(context);
            //foreach (var str in GL.GetSupportedExtensions())
            //Console.WriteLine(str);

            modelview_matrix  = new MatrixState();
            projection_matrix = new MatrixState();
            texture_matrix    = new MatrixState();
            normal_matrix     = new MatrixState();

            var err = GL.glGetError();

            GL.InitGL_1_2(context);
            GL.InitGL_1_3(context);
            GL.InitGL_1_4(context);
            GL.InitGL_1_5(context);
            GL.InitGL_2_0(context);
            GL.InitGL_2_1(context);
            GL.InitGL_3_0(context);
            GL.InitGL_3_1(context);
            GL.InitGL_3_2(context);
            GL.InitFramebuffers(context);
            GL.InitVertexArrayObjects(context);
            var sp = GL.GetSupportedExtensions();

            if (sp.Contains("GL_AMD_debug_output"))
            {
                GL.InitDebugOutputAMD(context);
                GL.glDebugMessageCallbackAMD(new OpenGL.OpenGL.DebugProc(DebugProcecure), IntPtr.Zero);
                GL.glDebugMessageEnableAMD(0, 0, 0, new int[] { }, GL.boolean.TRUE);
            }

            err = GL.glGetError();

            GL.glEnable(GL.Const.GL_TEXTURE_2D);

            state       = new OpenGLState(this);
            state.Blend = true;
            state.BlendFunction(Blend.SrcAlpha, Blend.OneMinusSrcAlpha);
            state.Culling   = true;
            state.DepthTest = true;
        }
예제 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="wnd"></param>
        /// <exception cref="AccessViolationException">OpenGL subsystem encountered an access violation, try updating graphics drivers</exception>
        /// <exception cref="InvalidOperationException">Unable to create OpenGL context</exception>
        /// <exception cref="NotSupportedException">OpenGL 3.2 is not supported by this system</exception>
        /// <example>
        /// public class DisplayForm : Form
        /// {
        ///		private WglContext ctx;
        ///		protected override virtual void OnHandleCreated(EventArgs e)
        ///		{
        ///			ctx = new WglContext();
        ///			ctx.CreateContext(this.Handle);
        ///		}
        ///		protected override virtual void OnClosing(ClosingEventArgs e)
        ///		{
        ///			ctx.Dispose();
        ///		}
        /// }
        /// </example>
        public override void CreateContext(IntPtr wnd, IntPtr draw, OpenGLContext share)
        {
            // Force OpenGL library to link
            // or we will not be able to create a context
            linker = GetLinker();

            hwnd    = wnd;
            display = hwnd;

            int  pixel_format          = 0;
            bool valid                 = false;
            PIXELFORMATDESCRIPTOR desc = new PIXELFORMATDESCRIPTOR();

            if (draw == IntPtr.Zero)
            {
                drawable = GetDC(wnd);
            }
            else
            {
                drawable = draw;
            }

            desc.nSize      = (short)Marshal.SizeOf(desc);
            desc.nVersion   = 1;
            desc.dwFlags    = PixelFormatFlags.PFD_DOUBLEBUFFER | PixelFormatFlags.PFD_DRAW_TO_WINDOW | PixelFormatFlags.PFD_SUPPORT_OPENGL;
            desc.cDepthBits = 32;
            desc.cColorBits = 32;
            pixel_format    = ChoosePixelFormat(drawable, ref desc);

            SetPixelFormat(drawable, pixel_format, ref desc);

            // Create legacy OpenGL context
            handle = wglCreateContext(drawable);

            wglMakeCurrent(drawable, handle);


            wglGetExtensionStringARB     = GetProc <GetExtensionsStringARB>("wglGetExtensionStringARB");
            wglChoosePixelFormatARB      = GetProc <ChoosePixelFormatARB>("wglChoosePixelFormatARB");
            wglGetPixelFormatAttribfvARB = GetProc <GetPixelFormatAttribfvARB>("wglGetPixelFormatAttribfvARB");
            wglGetPixelFormatAttribivARB = GetProc <GetPixelFormatAttribivARB>("wglGetPixelFormatAttribivARB");

            string ext = "";

            if (wglGetExtensionStringARB != null)
            {
                ext = Marshal.PtrToStringAnsi(wglGetExtensionStringARB(drawable));
            }

            var extensions = ext.Split(' ');

            if (drawable != IntPtr.Zero && wglChoosePixelFormatARB != null)
            {
                var fAttributes = new float[] { 0, 0 };
                var iAttributes = new int[]
                { WGL_DRAW_TO_WINDOW_ARB, 1,
                  WGL_SUPPORT_OPENGL_ARB, 1,
                  WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
                  WGL_COLOR_BITS_ARB, 24,
                  WGL_ALPHA_BITS_ARB, 8,
                  WGL_DEPTH_BITS_ARB, 24,
                  WGL_STENCIL_BITS_ARB, 8,
                  WGL_DOUBLE_BUFFER_ARB, 1,
                  WGL_SAMPLE_BUFFERS_ARB, 1,
                  WGL_SAMPLES_ARB, samples,                                                                     // Check For 4x Multisampling
                  0, 0 };
                uint num_formats = 0;
                valid = wglChoosePixelFormatARB(drawable, iAttributes, fAttributes, 1, ref pixel_format, ref num_formats) && num_formats > 0;
                if (valid)
                {
                    wglMakeCurrent(IntPtr.Zero, IntPtr.Zero);
                    wglDeleteContext(handle);
                    SetPixelFormat(drawable, pixel_format, ref desc);
                    handle = wglCreateContext(drawable);
                    wglMakeCurrent(drawable, handle);
                }
            }
            wglCreateContextAttribsARB = GetProc <CreateContextAttribsARB>("wglCreateContextAttribsARB");
            var s        = OpenGL.glGetString((uint)OpenGL.Const.GL_VERSION);
            var str      = Marshal.PtrToStringAnsi(s);
            var sub      = str.Split('.');
            int major    = 0;
            int minor    = 0;
            int revision = 0;

            int.TryParse(sub[0], out major);
            int.TryParse(sub[1], out minor);
            if (sub.Length >= 3)
            {
                int.TryParse(sub[0], out revision);
            }

            Glorg2.Debugging.Debug.WriteLine("OpenGL version " + major + "." + minor + " supported by your system.");

            if (major < 3)
            {
                throw new NotSupportedException("OpenGL 3 is not supported.");
            }
            if (minor < 2 && major == 3)
            {
                System.Diagnostics.Debug.WriteLine("OpenGL 3.2 is not supported. Consider upgrading display drivers.");
            }


            if (wglCreateContextAttribsARB != null)
            {
                int[] attribs = new int[]
                {
                    WGL_CONTEXT_MAJOR_VERSION_ARB, major,
                    WGL_CONTEXT_MINOR_VERSION_ARB, minor,
                    WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
                    WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
                    0
                };
                IntPtr share_ctx = IntPtr.Zero;
                if (share != null)
                {
                    share_ctx = share.Handle;
                }
                IntPtr newhandle = wglCreateContextAttribsARB(drawable, share_ctx, attribs);
                if (newhandle != IntPtr.Zero)
                {
                    wglMakeCurrent(IntPtr.Zero, IntPtr.Zero);
                    var err = OpenGL.glGetError();
                    wglDeleteContext(handle);
                    handle = IntPtr.Zero;
                    if (newhandle != IntPtr.Zero)                     // If OpenGL 3.0 context creation failed, fallback to legacy 2.x
                    {
                        handle = newhandle;
                    }
                    else
                    {
                        throw new NotSupportedException("Could not initialize OpenGL " + major + "." + minor + " : " + err.ToString());
                    }
                }
                else if (share_ctx != IntPtr.Zero)
                {
                    wglShareLists(handle, share_ctx);
                }
            }
            else
            {
                throw new NotSupportedException("OpenGL 3.2 is not supported by your system.");
            }

            if (handle != IntPtr.Zero)
            {
                wglMakeCurrent(drawable, handle);
            }
            else
            {
                throw new NotSupportedException("Could not create OpenGL context");
            }
        }