Exemple #1
0
        private void SceneRender(OpenGL gl)
        {
            ShaderVariables shaderVars = GetShaderVars();

            //  Get a reference to the raytracer shader.
            var shader = shaderRayMarch;

            shader.Bind(gl);

            shaderVars.Set(gl, shader);

            int rt1 = shader.GetUniformLocation(gl, "renderedTexture");
            int rn1 = shader.GetUniformLocation(gl, "randomNumbers");

            gl.Uniform1(rt1, 0);
            gl.Uniform1(rn1, 1);
            shader.SetUniform1(gl, "depth", _Depth ? 1 : 0);
            shader.SetUniform1(gl, "screenWidth", _TargetWidth);
            shader.SetUniform1(gl, "screenHeight", _TargetHeight);

            gl.ActiveTexture(OpenGL.GL_TEXTURE0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, _RaytracerBuffer[_PingPong ? 0 : 1]);
            gl.ActiveTexture(OpenGL.GL_TEXTURE1);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, _RandomNumbers[0]);
            gl.ActiveTexture(OpenGL.GL_TEXTURE0);
            if (_Rendering)
            {
                gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, (256 / _ProgressiveSteps) * 6);
                //            CheckForError(gl);
            }
            shader.Unbind(gl);
        }
        public override void Render(NkHandle Userdata, GL_Texture Texture, NkRect ClipRect, uint Offset, uint Count)
        {
            float[] vert   = new float[Count * 2];
            float[] uvs    = new float[Count * 2];
            float[] colors = new float[Count * 4];

            for (int i = 0; i < Count; i++)
            {
                NkVertex V = Verts[Inds[Offset + i]];
                vert[i * 2]     = V.Position.X;
                vert[i * 2 + 1] = V.Position.Y;

                uvs[i * 2]     = V.UV.X;
                uvs[i * 2 + 1] = V.UV.Y;

                NkColor color = V.Color;
                colors[i * 4]     = color.R;
                colors[i * 4 + 1] = color.G;
                colors[i * 4 + 2] = color.B;
                colors[i * 4 + 3] = color.A;
            }



            var vertexDataBuffer = new VertexBuffer();

            vertexDataBuffer.Create(gl);
            vertexDataBuffer.Bind(gl);


            vertexDataBuffer.SetDataV2F(gl, 0, rawData, NkVertex.SIZE * (int)Count, false, 3);

            //    vertexDataBuffer.SetData(gl, 0, vert, false, 2);

            var colourDataBuffer = new VertexBuffer();

            colourDataBuffer.Create(gl);
            colourDataBuffer.Bind(gl);
            colourDataBuffer.SetData(gl, 1, colors, false, 4);

            var uvDataBuffer = new VertexBuffer();

            uvDataBuffer.Create(gl);
            uvDataBuffer.Bind(gl);
            uvDataBuffer.SetData(gl, 2, uvs, false, 2);

            gl.ActiveTexture(OpenGL.GL_TEXTURE0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, Texture.TextureName);
            glScissor2(window.DisplayHeight, (int)ClipRect.X, (int)ClipRect.Y, (int)ClipRect.W, (int)ClipRect.H);
            gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, (int)Count);

            vertexDataBuffer.Unbind(gl);
            //  colourDataBuffer.Unbind(gl);
            //  uvDataBuffer.Unbind(gl);
        }
Exemple #3
0
        /// <summary>
        /// Draws the scene.
        /// </summary>
        /// <param name="gl">The OpenGL instance.</param>
        public void Draw(OpenGL gl, float width, float height)
        {
            if (needsRefresh)
            {
                Initialise(gl);
            }

            resolutionX = width;
            resolutionY = height;

            //  Clear the scene.
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_STENCIL_BUFFER_BIT);

            //  Bind the shader, set the matrices.
            shaderProgram.Bind(gl);

            shaderProgram.SetUniform3(gl, "iResolution", resolutionX, resolutionY, 0.0f);
            shaderProgram.SetUniform1(gl, "iGlobalTime", time);

            time += 0.1f;

            //  Bind the out vertex array.
            vertexBufferArray.Bind(gl);
            texCoordsBufferArray.Bind(gl);

            //Bind Textures
            var ch0loc = shaderProgram.GetUniformLocation(gl, "iChannel0");

            gl.ActiveTexture(OpenGL.GL_TEXTURE0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, glTextureArray[0]);
            gl.Uniform1(ch0loc, 0);

            var ch1loc = shaderProgram.GetUniformLocation(gl, "iChannel1");

            gl.ActiveTexture(OpenGL.GL_TEXTURE1);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, glTextureArray[1]);
            gl.Uniform1(ch1loc, 1);

            //  Draw the square.
            gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 6);

            //  Unbind our vertex array and shader.
            vertexBufferArray.Unbind(gl);
            texCoordsBufferArray.Unbind(gl);


            shaderProgram.Unbind(gl);
        }
        //public TextureManager()
        //{
        //    _textureCollection = new Dictionary<string, TexContainer>();
        //    _idCount = 0;
        //}

        // http://www.mastropaolo.com/devildotnet/
        // Path is to the image (only png/jpg for now)
        // This creates a texture and assigns it to a texture unit on the GPU.
        // What happens if all the texture units are taken ??
        public void CreateTexture(string path, OpenGL gl, bool UseNewTU = false)
        {
            if (!_textureCollection.ContainsKey(path))
            {
                Debug.WriteLine("Found New Texture! " + "ID: " + _idCount.ToString() + " Path: " + path);

                if (UseNewTU)
                {
                    _idCount++;
                }

                int[] test = new int[3];
                gl.GetInteger(OpenGL.GL_MAX_TEXTURE_IMAGE_UNITS, test);
                if (test[0] <= _idCount)
                {
                    throw new Exception("Out of Texture Units");
                }

                Bitmap       img  = new Bitmap(path);
                TexContainer texC = new TexContainer(new Texture(), _idCount);

                gl.Enable(OpenGL.GL_TEXTURE_2D);
                gl.ActiveTexture(OpenGL.GL_TEXTURE0 + (uint)_idCount);
                texC.Tex.Create(gl, img);
                texC.Tex.Bind(gl);
                gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
                _textureCollection.Add(path, texC);
            }
        }
Exemple #5
0
        /// <summary>
        /// Initialises the scene.
        /// </summary>
        /// <param name="gl">The OpenGL instance.</param>
        public void Initialise(OpenGL gl)
        {
            time = DateTime.Now.Millisecond / 1000;
            //  Set a blue clear colour.
            //gl.ClearColor(0.4f, 0.6f, 0.9f, 0.0f);

            //  Create the shader program.
            var vertexShaderSource   = ResourceHelper.LoadTextFromRecource("ShaderToy.NET.Shaders.main.vert");
            var fragmentShaderSource = ResourceHelper.LoadTextFromRecource(
                $"ShaderToy.NET.Shaders.{ActiveShader.ResourceName}.frag");

            shaderProgram = new DynamicShaderProgram();
            shaderProgram.Create(gl, vertexShaderSource, fragmentShaderSource, null);

            shaderProgram.BindAttributeLocation(gl, attributeIndexPosition, "position");
            shaderProgram.AssertValid(gl);

            //Generate Textures
            gl.GenTextures(2, glTextureArray);
            //shaderProgram.BindAttributeLocation(gl, glTextureArray[0], "iChannel0");
            //shaderProgram.BindAttributeLocation(gl, glTextureArray[1], "iChannel1");
            var ch0loc = shaderProgram.GetUniformLocation(gl, "iChannel0");

            gl.ActiveTexture(OpenGL.GL_TEXTURE0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, glTextureArray[0]);
            gl.Uniform1(ch0loc, 0);

            var ch1loc = shaderProgram.GetUniformLocation(gl, "iChannel1");

            gl.ActiveTexture(OpenGL.GL_TEXTURE1);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, glTextureArray[1]);
            gl.Uniform1(ch1loc, 1);

            /*gl.ActiveTexture(OpenGL.GL_TEXTURE0);
             * gl.BindTexture(OpenGL.GL_TEXTURE_2D, glTextureArray[0]);
             * gl.ActiveTexture(OpenGL.GL_TEXTURE1);
             * gl.BindTexture(OpenGL.GL_TEXTURE_2D, glTextureArray[1]);*/

            //  Now create the geometry for the square.
            CreateVerticesForSquare(gl);

            needsRefresh = false;
        }
Exemple #6
0
        public void Draw(OpenGL gl, float originX, float originY, float originZ, vec3 leftColor, vec3 rightColor, float audioModifier)
        {
            float scaleModifier = audioModifier * 1.6f;

            if (scaleModifier < MinSize)
            {
                scaleModifier = MinSize;
            }

            for (int index = 0; index < _quad.VertexData.Length / _quad.DataStride; index++)
            {
                _quad.VertexData[index * _quad.DataStride + 3] = Constants.Colors[6, 0]; // r
                _quad.VertexData[index * _quad.DataStride + 4] = Constants.Colors[6, 1]; // g
                _quad.VertexData[index * _quad.DataStride + 5] = Constants.Colors[6, 2]; // b
                _quad.VertexData[index * _quad.DataStride + 6] = 1.0f;                   // a
            }

            // Begin Draw
            GlState.Instance.ModelMatrix = glm.translate(GlState.Instance.ModelMatrix, new vec3(originX, originY, originZ));
            GlState.Instance.ModelMatrix = glm.scale(GlState.Instance.ModelMatrix, new vec3(scaleModifier, scaleModifier, scaleModifier));

            // Update buffers
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _vertexBufferObject[0]);
            GlBuffer.SetArrayData(gl, _quad.VertexData, _quad.SizeOfVertexDataBytes, OpenGL.GL_STATIC_DRAW);

            // Make model matrix available for drawing
            gl.UniformMatrix4(GlState.Instance.DefaultTexturedModelMatrixLocation, 1, false, GlState.Instance.ModelMatrix.to_array());

            // Set blending for particle system
            gl.BlendFunc(OpenGL.GL_ONE, OpenGL.GL_ONE);
            gl.DepthFunc(OpenGL.GL_ALWAYS);

            // Draw
            gl.ActiveTexture(OpenGL.GL_TEXTURE0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, _texture);
            gl.BindVertexArray(_vertexArrayObject[0]);
            gl.DrawElements(OpenGL.GL_TRIANGLES, _quad.IndexData.Length, _quad.IndexData);
            gl.BindVertexArray(0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);

            // Reset depth and blend func
            gl.DepthFunc(OpenGL.GL_LESS);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);

            GlState.Instance.ModelMatrix = mat4.identity();
            // End Draw
        }
        public void Render(OpenGL GL, GShaderProgram shader)
        {
            compassVAO.Bind(GL);
            TexContainer tc;

            tc = TextureManager.Instance.GetElement(_textureName);
            tc.Tex.Bind(GL); // Bind to the current texture on texture unit 0
            GL.ActiveTexture(OpenGL.GL_TEXTURE0 + (uint)tc.ID);
            shader.SetUniform(GL, "uSampler", tc.ID);
            // Turn ON aplha blending for the arrow so the empty background is ignored
            GL.Enable(OpenGL.GL_BLEND);
            GL.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            GL.DrawArrays(OpenGL.GL_TRIANGLE_FAN, 0, _numSegments);
            GL.Disable(OpenGL.GL_BLEND);

            compassVAO.Unbind(GL);
        }
Exemple #8
0
        private void LoadRandomNumbers(OpenGL gl)
        {
            gl.GenTextures(1, _RandomNumbers);

            gl.ActiveTexture(OpenGL.GL_TEXTURE0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, _RandomNumbers[0]);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_CLAMP_TO_EDGE);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_GENERATE_MIPMAP_SGIS, OpenGL.GL_FALSE); // automatic mipmap

            byte[] pixels = new byte[4 * 4 * 1024 * 1024];                                          // sizeof(int)

            var fs   = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\WooFractal\\" + "randomSequences.vec2", FileMode.Open, FileAccess.Read);
            var len  = (int)fs.Length;
            var bits = new byte[len];

            fs.Read(bits, 0, len);
            fs.Close();

            var floats  = new float[len / 2];
            int byteidx = 0;
            int idx     = 0;

            for (int y = 0; y < 1024; y++)
            {
                for (int x = 0; x < 1024; x++)
                {
                    floats[idx++] = BitConverter.ToSingle(bits, byteidx);
                    byteidx      += 4;
                    floats[idx++] = BitConverter.ToSingle(bits, byteidx);
                    byteidx      += 4;
                    floats[idx++] = 0;
                    floats[idx++] = 1;
                }
            }

            Buffer.BlockCopy(floats, 0, pixels, 0, pixels.Length);

            gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA32F, 1024, 1024, 0,
                          OpenGL.GL_RGBA, OpenGL.GL_FLOAT, pixels);

            gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);
        }
Exemple #9
0
        /// <summary>
        /// updates pixel data of the desired texture.
        /// </summary>
        public void UpdateTextureBitmap(OpenGL gl, int texIndex, Bitmap image)
        {
            int[] textureMaxSize = { 0 };
            gl.GetInteger(OpenGL.GL_MAX_TEXTURE_SIZE, textureMaxSize);

            if (image == null)
            {
                return;
            }

            //	Find the target width and height sizes, which is just the highest
            //	posible power of two that'll fit into the image.
            int targetWidth  = textureMaxSize[0];
            int targetHeight = textureMaxSize[0];

            //Console.WriteLine("Updating Tex " + texIndex + "Tex Max Size : " + targetWidth + "x" + targetHeight);

            for (int size = 1; size <= textureMaxSize[0]; size *= 2)
            {
                if (image.Width < size)
                {
                    targetWidth = size / 2;
                    break;
                }
                if (image.Width == size)
                {
                    targetWidth = size;
                }
            }

            for (int size = 1; size <= textureMaxSize[0]; size *= 2)
            {
                if (image.Height < size)
                {
                    targetHeight = size / 2;
                    break;
                }
                if (image.Height == size)
                {
                    targetHeight = size;
                }
            }

            //  If need to scale, do so now.
            if (image.Width != targetWidth || image.Height != targetHeight)
            {
                //  Resize the image.
                Image newImage = image.GetThumbnailImage(targetWidth, targetHeight, null, IntPtr.Zero);

                //  Destory the old image, and reset.
                image.Dispose();
                image = (Bitmap)newImage;
            }

            //  Lock the image bits (so that we can pass them to OGL).
            BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
                                                   ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            //	Set the width and height.
            width[texIndex]  = image.Width;
            height[texIndex] = image.Height;

            if (texIndex == 0)
            {
                gl.ActiveTexture(OpenGL.GL_TEXTURE0);
            }
            else
            {
                gl.ActiveTexture(OpenGL.GL_TEXTURE1);
            }

            //	Bind our texture object (make it the current texture).
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, glTextureArray[texIndex]);

            //  Set the image data.
            gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGBA,
                          width[texIndex], height[texIndex], 0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE,
                          bitmapData.Scan0);

            //  Unlock the image.
            image.UnlockBits(bitmapData);

            //  Dispose of the image file.
            image.Dispose();

            //  Set linear filtering mode.
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
        }
Exemple #10
0
        /// <summary>
        /// Renders the scene in retained mode.
        /// </summary>
        /// <param name="gl">The OpenGL instance.</param>
        /// <param name="useToonShader">if set to <c>true</c> use the toon shader, otherwise use a per-pixel shader.</param>
        public void Render(OpenGL gl)
        {
            if (_SaveNextRender)
            {
                SaveInternal(gl);
            }

            if (_MaxIterations > 0 && _FramesRendered > _MaxIterations - 1 && _Rendering == false)
            {
                return;
            }

            //  Clear the color and depth buffer.
            gl.ClearColor(0f, 0f, 0f, 0f);
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_STENCIL_BUFFER_BIT);

            int renderBuffer = 0;

            if (_PingPong)
            {
                renderBuffer = 1;
            }

            float[] viewport = new float[4];
            gl.GetFloat(OpenGL.GL_VIEWPORT, viewport);

            Debug.WriteLine("Rendering renderbuffer : " + renderBuffer);

            if (_Depth)
            {
                gl.Viewport(0, 0, 1, 1);
                gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _DepthFrameBuffer[0]);

                SceneRender(gl);

                gl.Viewport(0, 0, (int)viewport[2], (int)viewport[3]);
                gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, 0);
                gl.BindTexture(OpenGL.GL_TEXTURE_2D, _DepthCalcBuffer[0]);
                int[] pixels = new int[4];
                gl.GetTexImage(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, OpenGL.GL_FLOAT, pixels);
                float valr = BitConverter.ToSingle(BitConverter.GetBytes(pixels[0]), 0);
//                if (valr != valr)
//                  throw new Exception("Depth test failed");
                //                float valg = BitConverter.ToSingle(BitConverter.GetBytes(pixels[1]), 0);
                //                float valb = BitConverter.ToSingle(BitConverter.GetBytes(pixels[2]), 0);
                //                float vala = BitConverter.ToSingle(BitConverter.GetBytes(pixels[3]), 0);
                _ImageDepth    = valr;
                _ImageDepthSet = true;
                _Depth         = false;
            }

            gl.Viewport(0, 0, _TargetWidth, _TargetHeight);
            gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _FrameBuffer[renderBuffer]);

            SceneRender(gl);

            int target = 0;

            _PostProcess.Render(gl, _TargetWidth, _TargetHeight, ref target, _EffectFrameBuffer, _RaytracerBuffer[renderBuffer], _EffectRaytracerBuffer);

            // !!!!!!!!!!!!!!!! Tonemapping
            gl.Viewport(0, 0, _TargetWidth, _TargetHeight);
            gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _IntFrameBuffer[0]);

            var shader = shaderTransfer;

            gl.ActiveTexture(OpenGL.GL_TEXTURE0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, _EffectRaytracerBuffer[target]);

            shader.Bind(gl);
            shader.SetUniform1(gl, "renderedTexture", 0);
            shader.SetUniform1(gl, "gammaFactor", (float)_PostProcess._GammaFactor);
            shader.SetUniform1(gl, "gammaContrast", (float)_PostProcess._GammaContrast);
            shader.SetUniform1(gl, "mode", _PostProcess._ToneMappingMode);
            shader.SetUniform1(gl, "toneFactor", (float)_PostProcess._ToneFactor);

            gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3);
//            CheckForError(gl);
            shader.Unbind(gl);

            // !!!!!!!!!!!!!!!!! Int to final framebuffer
            gl.Viewport(0, 0, (int)viewport[2], (int)viewport[3]);
            gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, 0);

            // get a reference to the transfer shader
            shader = shaderIntTransfer;

            gl.ActiveTexture(OpenGL.GL_TEXTURE0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, _PostprocessBuffer[0]);

            shader.Bind(gl);
            shader.SetUniform1(gl, "renderedTexture", 0);

            gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3);


            gl.Viewport(0, 0, (int)viewport[2], (int)viewport[3]);
            //       CheckForError(gl);
            shader.Unbind(gl);

            // TIDY UP
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);

            if (_Rendering)
            {
                _FramesRendered += 1.0 / (double)_ProgressiveSteps;

                if (_MaxIterations > 0 && _FramesRendered > _MaxIterations - 1)
                {
                    _Rendering = false;
                }
                else
                {
                    _ProgressiveIndex += 256 / _ProgressiveSteps;
                    _Rays              = _TargetHeight * _TargetHeight * _FramesRendered;
                    if (_ProgressiveIndex >= 256)
                    {
                        _ProgressiveIndex = 0;
                        _PingPong         = !_PingPong;
                    }
                }
            }
        }
        public GL_GUI_Context(Window window, float width, float height)
        {
            this.window = window;
            this.Width  = width;
            this.Height = height;

            context = SDL.SDL_GL_CreateContext(window.Handle);
            SDL.SDL_GL_MakeCurrent(window.Handle, context);

            gl = new OpenGL();

            gl.Enable(OpenGL.GL_TEXTURE_2D);

            shaderProgram = new ShaderProgram();
            shaderProgram.Create(gl, VERTEX_SHADER, FRAGMENT_SHADER, null);
            shaderProgram.BindAttributeLocation(gl, 0, "in_Position");
            shaderProgram.BindAttributeLocation(gl, 1, "in_Color");
            shaderProgram.AssertValid(gl);

            gl.GenFramebuffersEXT(1, framebuffers);
            gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, framebuffers[0]);

            gl.GenTextures(1, framebufferTargets);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, framebufferTargets[0]);
            gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGB, window.DisplayWidth, window.DisplayHeight, 0, OpenGL.GL_RGB, OpenGL.GL_UNSIGNED_BYTE, IntPtr.Zero);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_NEAREST);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_NEAREST);
            gl.FramebufferTexture(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, framebufferTargets[0], 0);
            gl.DrawBuffers(1, new uint[] { OpenGL.GL_COLOR_ATTACHMENT0_EXT });

            if (gl.CheckFramebufferStatusEXT(OpenGL.GL_FRAMEBUFFER_EXT) != OpenGL.GL_FRAMEBUFFER_COMPLETE_EXT)
            {
                throw new Exception("Frame buffer setup not complete");
            }

            gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, 0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);


            ortho = new float[] {
                2.0f / Width, 0.0f, 0.0f, 0.0f,
                0.0f, -2.0f / Height, 0.0f, 0.0f,
                0.0f, 0.0f, -1.0f, 0.0f,
                -1.0f, 1.0f, 0.0f, 1.0f,
            };

            gl.Viewport(0, 0, window.DisplayWidth, window.DisplayHeight);
            gl.Enable(OpenGL.GL_BLEND);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            gl.Disable(OpenGL.GL_CULL_FACE);
            gl.Disable(OpenGL.GL_DEPTH_TEST);
            gl.ActiveTexture(OpenGL.GL_TEXTURE0);
            gl.Enable(OpenGL.GL_SCISSOR_TEST);

            shaderProgram.Bind(gl);
            shaderProgram.SetUniformMatrix4(gl, "projectionMatrix", ortho);

            vertexBufferArray = new VertexBufferArray();
            vertexBufferArray.Create(gl);
            vertexBufferArray.Bind(gl);
        }
        public void Draw(OpenGL gl, float originX, float originY, float originZ, float audioModifier, bool doOriginTranslation)
        {
            OriginX = originX;
            OriginY = originY;
            OriginZ = OriginZ;

            // Rotate colors
            if (_colorRotateStopwatch.ElapsedMilliseconds >= ColorRotateIntervalMs)
            {
                pickColor();
                _colorRotateStopwatch.Restart();
            }

            for (int index = 0; index < _particles.Count; index++)
            {
                Particle particle = _particles[index];
                float    x        = particle.X;
                float    y        = particle.Y;
                float    z        = particle.Z;
                float    r        = particle.R;
                float    g        = particle.G;
                float    b        = particle.B;
                float    a        = particle.Life;
                float    size     = particle.Size;

                _pointData[_point.DataStride * index + 0] = x;
                _pointData[_point.DataStride * index + 1] = y;
                _pointData[_point.DataStride * index + 2] = z;
                _pointData[_point.DataStride * index + 3] = size;
                _pointData[_point.DataStride * index + 4] = r;
                _pointData[_point.DataStride * index + 5] = g;
                _pointData[_point.DataStride * index + 6] = b;
                _pointData[_point.DataStride * index + 7] = a;

                // Increment particle location and speed
                if (OverrideParticleUpdate != null)
                {
                    OverrideParticleUpdate(particle, audioModifier);
                }
                else
                {
                    particle.Update();
                }

                // Invoke after update delegate if present
                if (AfterParticleUpdate != null)
                {
                    AfterParticleUpdate(particle, audioModifier);
                }

                // Reset dead particles
                if (particle.Life <= 0.0f)
                {
                    if (_isContinuous)
                    {
                        if (OverrideParticleInit != null)
                        {
                            // Call custom init if specified
                            OverrideParticleInit(particle, audioModifier);
                        }
                        else
                        {
                            // Re-init happening, no longer initial
                            particle.IsInitialInit = false;

                            // Default init
                            particle.Init(_random, audioModifier);

                            // Custom init if specified
                            if (AfterParticleInit != null)
                            {
                                AfterParticleInit(particle, audioModifier);
                            }
                        }

                        if (_autoRotateColors)
                        {
                            particle.R = Constants.Colors[_colorIndex, 0];
                            particle.G = Constants.Colors[_colorIndex, 1];
                            particle.B = Constants.Colors[_colorIndex, 2];
                        }
                    }
                    else
                    {
                        if (particle.IsAlive)
                        {
                            _deadParticleCount++;
                            particle.IsAlive = false;

                            if (_deadParticleCount == _particles.Count)
                            {
                                // None are alive. This particle system is no longer active
                                // and the caller should let it get GC'd
                                _isActive = false;
                            }
                        }
                    }
                }
            }

            // Begin Draw
            if (doOriginTranslation)
            {
                GlState.Instance.ModelMatrix = glm.translate(GlState.Instance.ModelMatrix, new vec3(originX, originY, originZ));
            }

            // Update buffers
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _vertexBufferObject[0]);
            GlBuffer.SetArrayData(gl, null, _pointData.Length * PrimitiveSizes.FloatBytes, OpenGL.GL_STREAM_DRAW);
            GlBuffer.SetArraySubData(gl, _pointData, _pointData.Length * PrimitiveSizes.FloatBytes);

            // Make model matrix available for drawing
            gl.UniformMatrix4(GlState.Instance.ParticleModelMatrixLocation, 1, false, GlState.Instance.ModelMatrix.to_array());

            // Set blending for particle system
            gl.BlendFunc(_blendMode, OpenGL.GL_ONE);
            gl.DepthFunc(OpenGL.GL_ALWAYS);

            // Draw
            gl.ActiveTexture(OpenGL.GL_TEXTURE0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, _texture);
            gl.Uniform1(GlState.Instance.ParticleTextureLocation, 0);
            gl.BindVertexArray(_vertexArrayObject[0]);
            gl.DrawArrays(OpenGL.GL_POINTS, 0, _pointData.Length);
            gl.BindVertexArray(0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);

            // Reset depth and blend func
            gl.DepthFunc(OpenGL.GL_LESS);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);

            if (doOriginTranslation)
            {
                GlState.Instance.ModelMatrix = mat4.identity();
            }
            // End Draw
        }
Exemple #13
0
        public void Render(OpenGL gl, int targetWidth, int targetHeight, ref int target, uint[] effectFrameBuffer, uint raytracerBuffer, uint[] effectRaytracerBuffer)
        {
            if (_PostProcessFilter == 0)
            {
                // !!!!!!!!!!!!!!! rgb / a
                gl.Viewport(0, 0, targetWidth, targetHeight);
                gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, effectFrameBuffer[target]);
                var shader = shaderRGBA;
                gl.ActiveTexture(OpenGL.GL_TEXTURE0);
                gl.BindTexture(OpenGL.GL_TEXTURE_2D, raytracerBuffer);
                shader.Bind(gl);
                shader.SetUniform1(gl, "renderedTexture", 0);
                gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3);
                shader.Unbind(gl);
            }
            else if (_PostProcessFilter == 1)
            {
                // !!!!!!!!!!!!!!! rgb / a
                gl.Viewport(0, 0, targetWidth, targetHeight);
                gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, effectFrameBuffer[target]);
                var shader = shaderRGBA;
                gl.ActiveTexture(OpenGL.GL_TEXTURE0);
                gl.BindTexture(OpenGL.GL_TEXTURE_2D, raytracerBuffer);
                shader.Bind(gl);
                shader.SetUniform1(gl, "renderedTexture", 0);
                gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3);
                shader.Unbind(gl);

                // !!!!!!!!!!!!!!! exponent
                target = target > 0 ? 0 : 1;
                gl.Viewport(0, 0, targetWidth, targetHeight);
                gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, effectFrameBuffer[target]);
                shader = shaderHighlights;
                gl.ActiveTexture(OpenGL.GL_TEXTURE0);
                gl.BindTexture(OpenGL.GL_TEXTURE_2D, effectRaytracerBuffer[target > 0 ? 0 : 1]);
                shader.Bind(gl);
                shader.SetUniform1(gl, "exponent", (float)_GaussianExposure);
                shader.SetUniform1(gl, "renderedTexture", 0);
                gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3);
                shader.Unbind(gl);

                // !!!!!!!!!!!!!!! gaussianShaderX
                target = target > 0 ? 0 : 1;
                gl.Viewport(0, 0, targetWidth, targetHeight);
                gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, effectFrameBuffer[target]);
                shader = shaderGaussianX;
                gl.ActiveTexture(OpenGL.GL_TEXTURE0);
                gl.BindTexture(OpenGL.GL_TEXTURE_2D, effectRaytracerBuffer[target > 0 ? 0 : 1]);
                shader.Bind(gl);
                shader.SetUniform1(gl, "screenWidth", targetWidth);
                shader.SetUniform1(gl, "screenHeight", targetHeight);
                shader.SetUniform1(gl, "renderedTexture", 0);
                gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3);
                shader.Unbind(gl);

                // !!!!!!!!!!!!!!! gaussianShaderY
                target = target > 0 ? 0 : 1;
                gl.Viewport(0, 0, targetWidth, targetHeight);
                gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, effectFrameBuffer[target]);
                shader = shaderGaussianY;
                gl.ActiveTexture(OpenGL.GL_TEXTURE0);
                gl.BindTexture(OpenGL.GL_TEXTURE_2D, effectRaytracerBuffer[target > 0 ? 0 : 1]);
                shader.Bind(gl);
                shader.SetUniform1(gl, "screenWidth", targetWidth);
                shader.SetUniform1(gl, "screenHeight", targetHeight);
                shader.SetUniform1(gl, "renderedTexture", 0);
                gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3);
                shader.Unbind(gl);

                // !!!!!!!!!!!!!!! blend
                target = target > 0 ? 0 : 1;
                gl.Viewport(0, 0, targetWidth, targetHeight);
                gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, effectFrameBuffer[target]);
                shader = shaderBlend;
                gl.ActiveTexture(OpenGL.GL_TEXTURE0);
                gl.BindTexture(OpenGL.GL_TEXTURE_2D, raytracerBuffer);
                gl.ActiveTexture(OpenGL.GL_TEXTURE1);
                gl.BindTexture(OpenGL.GL_TEXTURE_2D, effectRaytracerBuffer[target > 0 ? 0 : 1]);
                shader.Bind(gl);
                shader.SetUniform1(gl, "factor1", 1);
                shader.SetUniform1(gl, "factor2", (float)_PostProcessAmount);
                int rte1 = shader.GetUniformLocation(gl, "renderedTexture1");
                int rne1 = shader.GetUniformLocation(gl, "renderedTexture2");
                gl.Uniform1(rte1, 0);
                gl.Uniform1(rne1, 1);
                gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3);
                shader.Unbind(gl);
            }
        }
Exemple #14
0
        public void Draw(OpenGL gl, float originX, float originY, float originZ, float[] audioData)
        {
            _ballOrigin.x = originX;
            _ballOrigin.y = originY;
            _ballOrigin.z = originZ;

            for (int index = 0; index < _pointSphere.VertexData.Length / _pointSphere.DataStride; index++)
            {
                float x    = _pointSphere.VertexData[_pointSphere.DataStride * index + 0];
                float y    = _pointSphere.VertexData[_pointSphere.DataStride * index + 1];
                float z    = _pointSphere.VertexData[_pointSphere.DataStride * index + 2];
                float r    = Constants.Colors[_colorIndex, 0];
                float g    = Constants.Colors[_colorIndex, 1];
                float b    = Constants.Colors[_colorIndex, 2];
                float a    = 0.0f;
                float size = audioData[0] / 8.0f;

                if (size < MinPointSize)
                {
                    size = MinPointSize;
                }
                if (size > MaxPointSize)
                {
                    size = MaxPointSize;
                }

                _pointData[_point.DataStride * index + 0] = x;
                _pointData[_point.DataStride * index + 1] = y;
                _pointData[_point.DataStride * index + 2] = z;
                _pointData[_point.DataStride * index + 3] = size;
                _pointData[_point.DataStride * index + 4] = r;
                _pointData[_point.DataStride * index + 5] = g;
                _pointData[_point.DataStride * index + 6] = b;
                _pointData[_point.DataStride * index + 7] = a;
            }

            // Rotation and scale factor
            _rotX += IdleRotationSpeed;
            _rotY += IdleRotationSpeed;
            _rotZ += IdleRotationSpeed;
            float scaleFactor = 1.0f + (audioData[0] * 0.04f);

            // Begin Draw
            GlState.Instance.ModelMatrix = mat4.identity();
            GlState.Instance.ModelMatrix = glm.translate(GlState.Instance.ModelMatrix, new vec3(originX, originY, originZ));
            GlState.Instance.ModelMatrix = glm.scale(GlState.Instance.ModelMatrix, new vec3(scaleFactor, scaleFactor, scaleFactor));
            GlState.Instance.ModelMatrix = glm.rotate(GlState.Instance.ModelMatrix, _rotX, new vec3(1, 0, 0));
            GlState.Instance.ModelMatrix = glm.rotate(GlState.Instance.ModelMatrix, _rotY, new vec3(0, 1, 0));
            GlState.Instance.ModelMatrix = glm.rotate(GlState.Instance.ModelMatrix, _rotZ, new vec3(0, 0, 1));

            // Update buffers
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _vertexBufferObject[0]);
            GlBuffer.SetArrayData(gl, null, _pointData.Length * PrimitiveSizes.FloatBytes, OpenGL.GL_STREAM_DRAW);
            GlBuffer.SetArraySubData(gl, _pointData, _pointData.Length * PrimitiveSizes.FloatBytes);

            // Make model matrix available for drawing
            gl.UniformMatrix4(GlState.Instance.ParticleModelMatrixLocation, 1, false, GlState.Instance.ModelMatrix.to_array());

            // Set blending for particle system
            gl.BlendFunc(OpenGL.GL_ONE, OpenGL.GL_ONE);
            gl.DepthFunc(OpenGL.GL_ALWAYS);

            // Draw
            gl.ActiveTexture(OpenGL.GL_TEXTURE0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, _texture);
            gl.Uniform1(GlState.Instance.ParticleTextureLocation, 0);
            gl.BindVertexArray(_vertexArrayObject[0]);
            gl.DrawArrays(OpenGL.GL_POINTS, 0, _pointData.Length);
            gl.BindVertexArray(0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);

            // Reset depth and blend func
            gl.DepthFunc(OpenGL.GL_LESS);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);

            GlState.Instance.ModelMatrix = mat4.identity();
            // End Draw
        }
Exemple #15
0
        public void Render()
        {
            if (postprocessingEnabled && postShader != null && postShader.Compiled)
            {
                gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, fbo);
            }

            float[] clear = Convert.ColorToGLColor(ClearColor);
            gl.ClearColor(clear[0], clear[1], clear[2], clear[3]);

            camera.Project(gl);

            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT |
                OpenGL.GL_STENCIL_BUFFER_BIT);

            attrs.Push(gl, null);

            if (RenderGrid)
                grid.Render(gl);
            if (RenderAxies)
                axies.Render(gl);

            gl.Enable(OpenGL.GL_BLEND);
            gl.BlendFunc(OpenGL.GL_ONE, OpenGL.GL_ONE);

            foreach (Node node in children)
            {
                if (node is Light)
                    node.Render(gl);
            }

            if (Model != null)
                Model.Render(gl);

            foreach (Node node in children)
            {
                if (node is Light)
                    ((Light)node).Pop(gl);
            }

            gl.Disable(OpenGL.GL_BLEND);

            attrs.Pop(gl, null);

            gl.Flush();

            if (postprocessingEnabled && postShader != null && postShader.Compiled)
            {
                gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, 0);

                gl.ClearColor(0, 0, 0, 1);
                gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

                gl.MatrixMode(OpenGL.GL_PROJECTION);
                gl.LoadIdentity();
                gl.Ortho(0, viewportSize.Width, viewportSize.Height, 0, -1, 1);
                gl.MatrixMode(OpenGL.GL_MODELVIEW);
                gl.LoadIdentity();

                postShader.Bind();
                postShader.SetUniform1("resolution_x", viewportSize.Width);
                postShader.SetUniform1("resolution_y", viewportSize.Height);
                //gl.Uniform2(postShader.GetUniformLocation("resolution"),
                //    viewportSize.Width, viewportSize.Height);

                gl.ActiveTexture(OpenGL.GL_TEXTURE0);
                gl.BindTexture(OpenGL.GL_TEXTURE_2D, fboTexture);
                postShader.SetUniform1("texFramebuffer", 0);

                gl.Begin(OpenGL.GL_QUADS);
                gl.TexCoord(0, 1); gl.Vertex(0, 0);
                gl.TexCoord(0, 0); gl.Vertex(0, (float)viewportSize.Height);
                gl.TexCoord(1, 0); gl.Vertex((float)viewportSize.Width, (float)viewportSize.Height);
                gl.TexCoord(1, 1); gl.Vertex((float)viewportSize.Width, 0);
                gl.End();

                postShader.Unbind();
            }
        }
 public void UseTexure(OpenGL gl)
 {
     _texture.Bind(gl);
     gl.ActiveTexture(_texture.TextureName);
 }