Exemplo n.º 1
0
        protected void Initialize()
        {
            uint[] bufferIds = new uint[1];
            NativeGl.glGenBuffers(1, bufferIds);

            this.BufferId = bufferIds[0];
        }
Exemplo n.º 2
0
 public void UniformMatrix4Fv(uint location, int count, byte transpose, Matrix4 matrix)
 {
     unsafe
     {
         float *matrixPtr = &matrix.Row0.X;
         NativeGl.glUniformMatrix4fv(location, count, transpose, matrixPtr);
     }
 }
Exemplo n.º 3
0
        public string GetInfoLog()
        {
            int length;
            var error = new char[MaxInfologLength];

            NativeGl.glGetProgramInfoLog(this.ProgramId, MaxInfologLength, out length, error);

            return(StringExtensions.GetAnsiString(error, length));
        }
Exemplo n.º 4
0
        public uint GetUniformLocation(string name)
        {
            var pname  = MarshalExtensions.StringToPtrAnsi(name);
            var handle = NativeGl.glGetUniformLocation(this.ProgramId, pname);

            Marshal.FreeHGlobal(pname);

            return(handle);
        }
Exemplo n.º 5
0
        protected override void Dispose(bool disposing)
        {
            if (this.ShaderId != 0)
            {
                NativeGl.glDeleteShader(this.ShaderId);
            }

            base.Dispose(disposing);
        }
Exemplo n.º 6
0
        private void InitializeGraphics()
        {
            try
            {
                // platform graphics setup
                this.RenderingWindow = new RenderingWindow();
                this.RenderingWindow.Show();

                this.DisplayManager = new DisplayManager();

                this.PlatformManager = new PlatformGraphicsManager(this.DisplayManager.GetDisplay(this.RenderingWindow));

                this.PlatformManager.BindApi(Api.EGL_OPENGL_ES_API);

                var attribs = new Attribs <ConfigAttributes>();

                this.OnConfigureAttributes(attribs);

                var config = this.PlatformManager.ChooseConfigs(attribs, 1).FirstOrDefault();

                if (config == null)
                {
                    throw new InvalidOperationException("Could not find matching configuration");
                }

                this.RenderingContext = this.PlatformManager.CreateContext(config, ContextVersion.OPENGL_ES_2);

                this.RenderingSurface = this.PlatformManager.CreateWindowSurface(config, this.RenderingWindow);

                this.RenderingContext.MakeCurrent(this.RenderingSurface, this.RenderingSurface);

                // graphics device setup
                this.GraphicsDevice = new GraphicsDevice
                {
                    ClearColor = new Vector4(1, 1, 1, 1f),
                    Viewport   = new Rectangle(
                        0,
                        0,
                        this.RenderingWindow.Width,
                        this.RenderingWindow.Height)
                };

                var error = NativeGl.glGetError();

                if (error != NativeGl.GL_NO_ERROR)
                {
                    throw new InvalidOperationException("Error while initializing graphics.");
                }
            }
            catch (PlatformGraphicsException x)
            {
                MessageBox.Show(x.ToString());
                this.ExitGame = true;
            }
        }
Exemplo n.º 7
0
        protected override void Dispose(bool disposing)
        {
            if (this.TextureName != 0)
            {
                var textures = new uint[1];
                textures[0] = this.TextureName;

                NativeGl.glDeleteTextures(1, textures);
            }

            base.Dispose(disposing);
        }
Exemplo n.º 8
0
        public bool Compile()
        {
            NativeGl.glCompileShader(this.ShaderId);

            var success = new[]
            {
                -1
            };

            NativeGl.glGetShaderiv(this.ShaderId, NativeGl.GL_COMPILE_STATUS, success);

            return(success[0] == NativeGl.GL_TRUE);
        }
Exemplo n.º 9
0
        public void BufferData <T>(int size, [In] T[] data, BufferUsage usage) where T : struct
        {
            GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);

            try
            {
                NativeGl.glBufferData((uint)this.Target, (IntPtr)size, dataHandle.AddrOfPinnedObject(), (uint)usage);
            }
            finally
            {
                dataHandle.Free();
            }
        }
Exemplo n.º 10
0
        protected override void Dispose(bool disposing)
        {
            if (this.BufferId > 0)
            {
                NativeGl.glDeleteBuffers(
                    1,
                    new[]
                {
                    this.BufferId
                });
            }

            base.Dispose(disposing);
        }
Exemplo n.º 11
0
        public void ShaderSource(string source)
        {
            unsafe
            {
                int length = source.Length;

                IntPtr[] ptrArray = new IntPtr[1];
                IntPtr   strPtr   = MarshalExtensions.StringToPtrAnsi(source);
                ptrArray[0] = strPtr;

                NativeGl.glShaderSource(this.ShaderId, 1, ptrArray, &length);
                Marshal.FreeHGlobal(strPtr);
            }
        }
Exemplo n.º 12
0
        public Texture CreateFromBytes(byte[] data, int width, int height, uint format, uint type)
        {
            uint textureId;

            uint[] tex = new uint[1];

            NativeGl.glGenTextures(1, tex);

            textureId = tex[0];

            NativeGl.glActiveTexture(NativeGl.GL_TEXTURE0);
            NativeGl.glBindTexture(NativeGl.GL_TEXTURE_2D, textureId);

            var handle = GCHandle.Alloc(data, GCHandleType.Pinned);

            try
            {
                NativeGl.glTexParameterf(NativeGl.GL_TEXTURE_2D, NativeGl.GL_TEXTURE_MIN_FILTER, NativeGl.GL_LINEAR);
                NativeGl.glTexParameterf(NativeGl.GL_TEXTURE_2D, NativeGl.GL_TEXTURE_MAG_FILTER, NativeGl.GL_LINEAR);
                NativeGl.glTexParameterf(NativeGl.GL_TEXTURE_2D, NativeGl.GL_TEXTURE_WRAP_S, NativeGl.GL_CLAMP_TO_EDGE);
                NativeGl.glTexParameterf(NativeGl.GL_TEXTURE_2D, NativeGl.GL_TEXTURE_WRAP_T, NativeGl.GL_CLAMP_TO_EDGE);

                NativeGl.glTexImage2D(
                    NativeGl.GL_TEXTURE_2D,
                    0,
                    (int)format,
                    width,
                    height,
                    0,
                    format,
                    type,
                    handle.AddrOfPinnedObject());
            }
            catch (Exception x)
            {
                NativeGl.glDeleteTextures(1, tex);
                throw;
            }
            finally
            {
                handle.Free();
            }

            NativeGl.glBindTexture(NativeGl.GL_TEXTURE_2D, 0);

            var texture = new Texture(NativeGl.GL_TEXTURE_2D, textureId);

            return(texture);
        }
Exemplo n.º 13
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                foreach (var shader in this.shaders)
                {
                    NativeGl.glDetachShader(this.ProgramId, shader.ShaderId);
                    shader.Dispose();
                }
            }

            if (this.ProgramId != 0)
            {
                NativeGl.glDeleteProgram(this.ProgramId);
            }

            base.Dispose(disposing);
        }
Exemplo n.º 14
0
        private void Render(double deltaTime)
        {
            this.OnRender(deltaTime);

            this.RenderingSurface.SwapBuffers();

            var errorCode = NativeGl.glGetError();

            if (errorCode == NativeGl.GL_NO_ERROR)
            {
                return;
            }

            var codeString = errorCode.ToString("X");

            Trace.WriteLine(codeString);
            this.ExitGame = true;
        }
Exemplo n.º 15
0
        public bool Link()
        {
            this.OnBeforeLinked();

            NativeGl.glLinkProgram(this.ProgramId);

            var success = new[]
            {
                -1
            };

            NativeGl.glGetProgramiv(this.ProgramId, NativeGl.GL_LINK_STATUS, success);

            var ok = success[0] == NativeGl.GL_TRUE;

            if (ok)
            {
                this.OnLinked();
            }

            return(ok);
        }
Exemplo n.º 16
0
 public void DisableBuffer(uint target)
 {
     NativeGl.glBindBuffer(target, 0);
 }
Exemplo n.º 17
0
 public void BindTexture(TextureTarget textureTarget, uint textureName)
 {
     NativeGl.glBindTexture((uint)textureTarget, textureName);
 }
Exemplo n.º 18
0
 public void Clear(ClearMask mask)
 {
     NativeGl.glClear((uint)mask);
 }
Exemplo n.º 19
0
 public void BindBuffer(DeviceBuffer buffer)
 {
     NativeGl.glBindBuffer((uint)buffer.Target, buffer.BufferId);
 }
Exemplo n.º 20
0
 public void ActivateTextureUnit(TextureUnit textureUnit)
 {
     NativeGl.glActiveTexture((uint)textureUnit);
 }
Exemplo n.º 21
0
 private void UpdateViewport(Rectangle vp)
 {
     NativeGl.glViewport(vp.Left, vp.Top, vp.Width, vp.Height);
 }
Exemplo n.º 22
0
 public void DisableVertexAttribArray(uint indx)
 {
     NativeGl.glDisableVertexAttribArray(indx);
 }
Exemplo n.º 23
0
 public void VertexAttribPointer(uint indx, int size, GlType type, GlBoolean normalized, int stride, IntPtr ptr)
 {
     NativeGl.glVertexAttribPointer(indx, size, (uint)type, (byte)normalized, stride, ptr);
 }
Exemplo n.º 24
0
 public void UseProgram(ShaderProgram program)
 {
     NativeGl.glUseProgram(program.ProgramId);
 }
Exemplo n.º 25
0
 public void EnableVertexAttribArray(uint index)
 {
     NativeGl.glEnableVertexAttribArray(index);
 }
Exemplo n.º 26
0
 public void Disable(Cap cap)
 {
     NativeGl.glDisable((uint)cap);
 }
Exemplo n.º 27
0
 public void Enable(Cap cap)
 {
     NativeGl.glEnable((uint)cap);
 }
Exemplo n.º 28
0
 public void DrawArrays(PrimitiveType mode, int first, int count)
 {
     NativeGl.glDrawArrays((uint)mode, first, count);
 }
Exemplo n.º 29
0
 public void DrawElements(PrimitiveType mode, int count, uint type)
 {
     NativeGl.glDrawElements((uint)mode, count, type, IntPtr.Zero);
 }
Exemplo n.º 30
0
 private void UpdateClearColor(Vector4 color)
 {
     NativeGl.glClearColor(color.X, color.Y, color.Z, color.W);
 }