OpenGL bindings for .NET, implementing the full OpenGL API, including extensions.

This class contains all OpenGL enums and functions defined in the latest OpenGL specification. The official .spec files can be found at: http://opengl.org/registry/.

A valid OpenGL context must be created before calling any OpenGL function.

Use the GL.Load and GL.LoadAll methods to prepare function entry points prior to use. To maintain cross-platform compatibility, this must be done for both core and extension functions. The GameWindow and the GLControl class will take care of this automatically.

You can use the GL.SupportsExtension method to check whether any given category of extension functions exists in the current OpenGL context. Keep in mind that different OpenGL contexts may support different extensions, and under different entry points. Always check if all required extensions are still supported when changing visuals or pixel formats.

You may retrieve the entry point for an OpenGL function using the GL.GetDelegate method.

상속: GraphicsBindingsBase
예제 #1
0
        public void render(float partialTicks)
        {
            if (camera == null)
            {
                return;
            }

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            prepare();

            var viewMatrix = MatrixHelper.createViewMatrix(camera);

            if (Game.INSTANCE.world != null)
            {
                worldRenderer.render(viewMatrix);
                entityRenderer.render(partialTicks);

                skyboxRenderer.render(viewMatrix);
            }

            //render other gui
            if (Game.INSTANCE.player != null)
            {
                guiRenderer.renderCrosshair();
                guiRenderer.renderHUD();
            }

            //render gui screen
            if (Game.INSTANCE.guiScreen != null)
            {
                Game.INSTANCE.CursorVisible = true;
                guiRenderer.render(Game.INSTANCE.guiScreen);
            }
        }
예제 #2
0
        private void DrawBegin(RectangleF dstRect)
        {
            GL.Enable(EnableCap.AlphaTest);
            GL.Disable(EnableCap.DepthTest);
            GL.Color4(Color);
            GL.Viewport(0, 0, Width, Height);

            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            var w = MMW.Width;
            var h = MMW.Height;

            GL.Ortho(0, w, h, 0, -1, 1);

            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            var trans = new Vector3(dstRect.X + dstRect.Width * CenterPivot.X, dstRect.Y + dstRect.Height * CenterPivot.Y, 0.0f);

            GL.Translate(trans);
            GL.Scale(Scale.X, Scale.Y, 1.0f);
            GL.Rotate(Rotate.Z, 0.0f, 0.0f, 1.0f);
            GL.Rotate(Rotate.X, 1.0f, 0.0f, 0.0f);
            GL.Rotate(Rotate.Y, 0.0f, 1.0f, 0.0f);
            GL.Translate(-trans);
            GL.Translate(dstRect.X - 0.5f, dstRect.Y - 0.5f, 0.0f);
        }
예제 #3
0
        static int LoadTexture(string filename)
        {
            if (String.IsNullOrEmpty(filename))
            {
                throw new ArgumentException(filename);
            }

            int id = GL.GenTexture();

            GL.BindTexture(TextureTarget.Texture2D, id);

            // We will not upload mipmaps, so disable mipmapping (otherwise the texture will not appear).
            // We can use GL.GenerateMipmaps() or GL.Ext.GenerateMipmaps() to create
            // mipmaps automatically. In that case, use TextureMinFilter.LinearMipmapLinear to enable them.
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

            System.Drawing.Bitmap bmp      = new System.Drawing.Bitmap(filename);
            BitmapData            bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp_data.Width, bmp_data.Height, 0,
                          OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0);

            bmp.UnlockBits(bmp_data);

            return(id);
        }
예제 #4
0
        public GLShaderHandle CreateShader(GLShaderType type)
        {
            int shaderName = GL.CreateShader((ShaderType)type);

            AddContextObject(new ShaderDisposable(shaderName));
            return((GLShaderHandle)shaderName);
        }
예제 #5
0
        public int GetProgramParameter(GLProgramHandle program, GLProgramParameter pname)
        {
            int result;

            GL.GetProgram((int)program, (GetProgramParameterName)pname, out result);
            return(result);
        }
예제 #6
0
 public void TexSubImage2D(GLTextureTarget target, int level, int xoffset, int yoffset, int width, int height, GLPixelFormat format, GLPixelType type, IntPtr data)
 {
     GL.TexSubImage2D((TextureTarget)target, level,
                      xoffset, yoffset, width, height,
                      (PixelFormat)format, (PixelType)type,
                      data);
 }
예제 #7
0
 public void UniformMatrix4(int location, bool transpose, Float4x4[] value)
 {
     if (value.Length > 0)
     {
         GL.UniformMatrix4(location, value.Length, transpose, ref value[0].M11);
     }
 }
예제 #8
0
        public GLTextureHandle CreateTexture()
        {
            var texture = MacGL.GenTexture();

            AddContextObject(new TextureDisposable(texture));
            return(new GLTextureHandle(texture));
        }
예제 #9
0
 public void UniformMatrix3(int location, bool transpose, Float3x3[] value)
 {
     if (value.Length > 0)
     {
         MacGL.UniformMatrix3(location, value.Length, transpose, ref value[0].M11);
     }
 }
예제 #10
0
        public int GetInteger(GLIntegerName pname)
        {
            int result;

            MacGL.GetInteger((GetPName)pname, out result);
            return(result);
        }
예제 #11
0
        public Int4 GetInteger(GLInteger4Name pname)
        {
            var p = new int[4];

            MacGL.GetInteger((GetPName)pname, p);
            return(new Int4(p[0], p[1], p[2], p[3]));
        }
예제 #12
0
 public void FramebufferRenderbuffer(GLFramebufferTarget target, GLFramebufferAttachment attachment, GLRenderbufferTarget renderbuffertarget, GLRenderbufferHandle renderbuffer)
 {
     GL.FramebufferRenderbuffer(
         (FramebufferTarget)target,
         (FramebufferAttachment)attachment,
         (RenderbufferTarget)renderbuffertarget, (int)renderbuffer);
 }
예제 #13
0
 public void FramebufferTexture2D(GLFramebufferTarget target, GLFramebufferAttachment attachment, GLTextureTarget textarget, GLTextureHandle texture, int level)
 {
     GL.FramebufferTexture2D(
         (FramebufferTarget)target,
         (FramebufferAttachment)attachment,
         (TextureTarget)textarget, (int)texture, level);
 }
예제 #14
0
 public void RenderbufferStorage(GLRenderbufferTarget target, GLRenderbufferStorage internalFormat, int width, int height)
 {
     GL.RenderbufferStorage(
         (RenderbufferTarget)target,
         (RenderbufferStorage)internalFormat,
         width, height);
 }
예제 #15
0
        protected ShaderProgram(string shaderName, PrimitiveType renderType)
        {
            this.renderType = renderType;
            var file = "SharpCraft_Data/assets/shaders/" + shaderName;

            VertexShaderID   = loadShader(ShaderType.VertexShader, file);
            FragmentShaderID = loadShader(ShaderType.FragmentShader, file);

            Console.WriteLine($"DEBUG: '{shaderName}' vertex shader: {(VertexShaderID != -1 ? "OK" : "ERROR")}");
            Console.WriteLine($"DEBUG: '{shaderName}' fragment shader: {(FragmentShaderID != -1 ? "OK" : "ERROR")}");

            ProgramID = GL.CreateProgram();

            GL.AttachShader(ProgramID, VertexShaderID);
            GL.AttachShader(ProgramID, FragmentShaderID);

            bindAttributes();

            GL.LinkProgram(ProgramID);
            GL.ValidateProgram(ProgramID);

            getAllUniformLocations();

            ShaderManager.registerShader(this);
        }
예제 #16
0
        public GLShaderHandle CreateShader(GLShaderType type)
        {
            var shader = MacGL.CreateShader((ShaderType)type);

            AddContextObject(new ShaderDisposable(shader));
            return(new GLShaderHandle(shader));
        }
예제 #17
0
 public void TexImage2D(GLTextureTarget target, int level, GLPixelFormat internalFormat, int width, int height, int border, GLPixelFormat format, GLPixelType type, IntPtr data)
 {
     GL.TexImage2D((TextureTarget)target, level,
                   (PixelInternalFormat)internalFormat, width, height, border,
                   (PixelFormat)format, (PixelType)type,
                   data);
 }
예제 #18
0
        public GLProgramHandle CreateProgram()
        {
            var program = MacGL.CreateProgram();

            AddContextObject(new ProgramDisposable(program));
            return(new GLProgramHandle(program));
        }
예제 #19
0
 public void BindFramebuffer(GLFramebufferTarget target, GLFramebufferHandle fb)
 {
     //if (!AppDomain.CurrentDomain.IsDefaultAppDomain())
     //	System.Diagnostics.Debug.WriteLine(AppDomain.CurrentDomain.FriendlyName + " bound " + fb);
     _currentFramebufferBinding = fb;
     GL.BindFramebuffer((FramebufferTarget)target, (int)fb);
 }
예제 #20
0
        public int GetProgramParameter(GLProgramHandle program, GLProgramParameter pname)
        {
            int result;

            MacGL.GetProgram((uint)(int)program, (ProgramParameter)pname, out result);
            return(result);
        }
예제 #21
0
        public Int4 GetInteger(GLInteger4Name name)
        {
            var v = new int[4];

            GL.GetInteger((GetPName)name, v);
            return(new Int4(v[0], v[1], v[2], v[3]));
        }
예제 #22
0
 private void GLLoad(object sender, EventArgs e)
 {
     _glLoaded = true;
     GL.ClearColor(Color.White);
     SetupViewport();
     _currentWaveFormList = new List <double>();
 }
예제 #23
0
        public int GetShaderParameter(GLShaderHandle shader, GLShaderParameter pname)
        {
            int result;

            GL.GetShader((int)shader, (ShaderParameter)pname, out result);
            return(result);
        }
예제 #24
0
        private void MusicCirsle(double x, double y, double begin, double end)
        {
            for (int j = 0; j < 5; j++)
            {
                GL.Begin(PrimitiveType.TriangleFan);
                GL.Color4(firstColorSpectr.R, firstColorSpectr.G, firstColorSpectr.B, 0.8);

                if (j == 0)
                {
                    spectrRadius[j] += _FFTSpectr[j] * glControl1.Width / 3;
                }
                else
                {
                    spectrRadius[j] += _FFTSpectr[j] * glControl1.Width / Math.Sqrt(j + 10) + spectrRadius[j - 1];
                }
                spectrRadius[j] /= 2;

                GL.Vertex3(x, y, (double)(1 - 0.1));
                for (double i = begin; i < end; i += 0.05)
                {
                    GL.Color4(secondColorSpectr[j].R, secondColorSpectr[j].G, secondColorSpectr[j].B, 0.8);
                    GL.Vertex3(spectrRadius[j] * Math.Cos(i) + x,
                               spectrRadius[j] * Math.Sin(i) + y, (double)(1 - 0.1));
                    GL.Vertex3(spectrRadius[j] * Math.Cos(i + 0.01) + x,
                               spectrRadius[j] * Math.Sin(i + 0.01) + y, (double)(1 - 0.1));
                }
                GL.End();
            }
        }
예제 #25
0
        public void Render2D()
        {
            if (_rect.IsEmpty)
            {
                var te = _printer.Measure(_text, SystemFonts.MessageBoxFont, new RectangleF(0, 0, Viewport.Width, Viewport.Height));
                _rect         = te.BoundingBox;
                _rect.X      += 5;
                _rect.Y      += 2;
                _rect.Width  += 5;
                _rect.Height += 2;
            }

            GL.Disable(EnableCap.CullFace);

            _printer.Begin();
            if (_showing)
            {
                GL.Begin(PrimitiveType.Quads);
                GL.Color3(Viewport is Viewport3D ? View.ViewportBackground : Grid.Background);
                GL.Vertex2(0, 0);
                GL.Vertex2(_rect.Right, 0);
                GL.Vertex2(_rect.Right, _rect.Bottom);
                GL.Vertex2(0, _rect.Bottom);
                GL.End();
            }
            _printer.Print(_text, SystemFonts.MessageBoxFont, _showing ? Color.White : Grid.GridLines, _rect);
            _printer.End();

            GL.Enable(EnableCap.CullFace);
        }
예제 #26
0
        public BasicGameWindow(int width, int height)
            : base(width, height, GraphicsMode.Default,
                   "window", GameWindowFlags.Default, DisplayDevice.Default,
                   3, 2, ContextFlags)
        {
            Closing += (sender, e) => e.Cancel = ExitRequested?.Invoke() ?? false;
            Closed  += (sender, e) => Exited?.Invoke();
            Cursor   = MouseCursor.Empty;

            MakeCurrent();

            string version = GL.GetString(StringName.Version);
            string versionNumberSubstring = GetVersionNumberSubstring(version);

            GLVersion = new Version(versionNumberSubstring);
            version   = GL.GetString(StringName.ShadingLanguageVersion);
            if (!string.IsNullOrEmpty(version))
            {
                try
                {
                    GLSLVersion = new Version(versionNumberSubstring);
                }
                catch (Exception e)
                {
                    Logger.Error(e, $@"couldn't set GLSL version using string '{version}'");
                }
            }

            if (GLSLVersion == null)
            {
                GLSLVersion = new Version();
            }

            //Set up OpenGL related characteristics
            GL.Disable(EnableCap.DepthTest);
            GL.Disable(EnableCap.StencilTest);
            GL.Enable(EnableCap.Blend);
            GL.Enable(EnableCap.ScissorTest);

            string extensions    = "";
            int    numExtensions = GL.GetInteger(GetPName.NumExtensions);

            for (int i = 0; i < numExtensions;)
            {
                extensions += GL.GetString(StringNameIndexed.Extensions, i);
                if (++i < numExtensions)
                {
                    extensions += " ";
                }
            }

            Logger.Log($@"GL Initialized
                        GL Version:                 {GL.GetString(StringName.Version)}
                        GL Renderer:                {GL.GetString(StringName.Renderer)}
                        GL Shader Language version: {GL.GetString(StringName.ShadingLanguageVersion)}
                        GL Vendor:                  {GL.GetString(StringName.Vendor)}
                        GL Extensions:              {extensions}", LoggingTarget.Runtime, LogLevel.Important);

            Context.MakeCurrent(null);
        }
예제 #27
0
        // Penguin
        private void Render()
        {
            if (this.image.ActualWidth <= 0 || this.image.ActualHeight <= 0)
            {
                return;
            }

            this.framebufferHandler.Prepare(new Size((int)this.ActualWidth, (int)this.ActualHeight));

            GL.MatrixMode(OpenTK.Graphics.OpenGL.MatrixMode.Projection);
            GL.LoadIdentity();
            float halfWidth  = (float)(this.ActualWidth / 2);
            float halfHeight = (float)(this.ActualHeight / 2);

            GL.Ortho(-halfWidth, halfWidth, halfHeight, -halfHeight, 1000, -1000);
            GL.Viewport(0, 0, (int)this.ActualWidth, (int)this.ActualHeight);

            string filename2   = "C:\\Users\\hp\\image.bmp";
            int    loadTexture = LoadTexture(filename2);

            //DrawImage(loadTexture);

            this.renderer.Render();

            GL.Finish();

            this.framebufferHandler.Cleanup(ref this.backbuffer);

            if (this.backbuffer != null)
            {
                this.image.Source = this.backbuffer;
            }

            this.frames++;
        }
예제 #28
0
        public void RenderTransparent(IGraphicsContext context, Action <TextureReference> textureCallback, Coordinate cameraLocation, Coordinate lookAt)
        {
            var sorted =
                from subset in GetSubsets <Face>(Transparent)
                let face = subset.Instance as Face
                           where face != null
                           orderby LookAtOrder(face, cameraLocation, lookAt) ascending
                           select subset;

            foreach (var subset in sorted)
            {
                var tex = ((Face)subset.Instance).Texture;
                if (tex.Texture != null)
                {
                    tex.Texture.Bind();
                }
                else
                {
                    TextureHelper.Unbind();
                }
                textureCallback(tex);
                GL.DepthMask(false);
                if (!tex.IsToolTexture)
                {
                    GL.Disable(OpenTK.Graphics.OpenGL.EnableCap.CullFace);
                }
                //program.Set("isTextured", tex.Texture != null);
                Render(context, PrimitiveType.Triangles, subset);
                GL.DepthMask(true);
                GL.Enable(OpenTK.Graphics.OpenGL.EnableCap.CullFace);
            }
        }
예제 #29
0
        protected override void OnLoad()
        {
            GL.ClearColor(1f, 1f, 1f, 1.0f);

            VertexBufferObject = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject);
            GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw);

            VertexArrayObject = GL.GenVertexArray();
            GL.BindVertexArray(VertexArrayObject);
            GL.BindBuffer(BufferTarget.ArrayBuffer, VertexArrayObject);
            GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw);

            ElementBufferObject = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, ElementBufferObject);
            GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length * sizeof(uint), indices, BufferUsageHint.StaticDraw);

            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0);
            GL.EnableVertexAttribArray(0);

            Shader = new Shader(
                Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"shaders\shader.vert.glsl")),
                Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"shaders\shader.frag.glsl")));

            Shader.Use();

            _timer = new Stopwatch();
            _timer.Start();

            base.OnLoad();
        }
예제 #30
0
 public void prepare()
 {
     GL.Enable(EnableCap.DepthTest);
     GL.Enable(EnableCap.DepthClamp);
     GL.Enable(EnableCap.CullFace);
     GL.CullFace(CullFaceMode.Back);
 }