Пример #1
0
        public unsafe void LoadTexture(string texturePath)
        {
            if (fileName == null)
            {
                return;
            }

            var imageStream = SourceLoader.GetStream(texturePath);

            if (imageStream == null)
            {
                throw new ApplicationException(@"Texture resource '{texturePath}.{fileName}' not found!");
            }

            // Read bitmap header
            var header = new byte[BITMAP_HEADER_LENGTH];

            imageStream.Read(header, 0, BITMAP_HEADER_LENGTH);
            var start = parseHeader(header);

            // Read bitmap data
            var buffer = new byte[textureWidth * textureHeight * 3];

            imageStream.Seek(start, SeekOrigin.Begin);
            imageStream.Read(buffer, 0, textureWidth * textureHeight * 3);

            fixed(byte *p = buffer)
            {
                var ptr = (IntPtr)p;

                textureId = GL.GenTexture();
                GL.BindTexture(TextureTarget.Texture2D, textureId);
                GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);

                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, textureWidth, textureHeight, 0, PixelFormat.Bgr, PixelType.UnsignedByte, ptr);

                GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, new [] { GL_LINEAR });
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, new [] { GL_LINEAR_MIPMAP_LINEAR });

                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, GL_CLAMP_TO_EDGE);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, GL_CLAMP_TO_EDGE);

                GL.BindTexture(TextureTarget.Texture2D, 0);
            }

            haveTexture = true;
        }
Пример #2
0
        public BaseShader(string name)
        {
            var vertexShader   = GL.CreateShader(ShaderType.VertexShader);
            var fragmentShader = GL.CreateShader(ShaderType.FragmentShader);

            GL.ShaderSource(vertexShader, SourceLoader.Read(@"shaders.{name}.vs.glsl"));
            GL.CompileShader(vertexShader);
            checkCompileStatus(@"Vertex Shader: {name}", vertexShader);

            GL.ShaderSource(fragmentShader, SourceLoader.Read(@"shaders.{name}.fs.glsl"));
            GL.CompileShader(fragmentShader);
            checkCompileStatus(@"Fragment Shader: {name}", fragmentShader);

            ShaderProgram = GL.CreateProgram();
            GL.AttachShader(ShaderProgram, fragmentShader);
            GL.AttachShader(ShaderProgram, vertexShader);
            GL.LinkProgram(ShaderProgram);
            GL.UseProgram(ShaderProgram);

            GL.ValidateProgram(ShaderProgram);
            Console.WriteLine(GL.GetProgramInfoLog(ShaderProgram));

            SetUniforms();
        }