Esempio n. 1
0
        public void AddTarget(PixelInternalFormat format, PixelType type)
        {
            Texture target = new Texture();

            // Bind the texture
            target.Bind();

            // Give an empty image to OpenGL
            GL.TexImage2D(TextureTarget.Texture2D, 0, format, gameWindow.Width, gameWindow.Height, 0, PixelFormat.Rgba, type, IntPtr.Zero);

            // Poor filtering. Needed !
            GL.TexParameter(TextureTarget.Texture2D,
                            TextureParameterName.TextureMinFilter,
                            (int)TextureMagFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D,
                            TextureParameterName.TextureMagFilter,
                            (int)TextureMagFilter.Nearest);

            targets.Add(target);
        }
Esempio n. 2
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            PackageManager.BasePath = "../../Assets/";

            // Load shaders
            ShaderManager.LoadCollection("Shaders/collection.xml");
            shader = ShaderManager.Get("texturedquad");

            // Create screen-filling quad
            batch = new VertexBatch();
            batch.StartBatch(PrimitiveType.TriangleStrip);
            batch.Add(new Vector3(-1, -1, 0)); // Bottom left
            batch.Add(new Vector3(1, -1, 0));  // Bottom right
            batch.Add(new Vector3(-1, 1, 0));  // Top left
            batch.Add(new Vector3(1, 1, 0));   // Top Right
            batch.EndBatch();

            // Load texture
            textures = new TextureManager();
            texture = textures.Get("Textures/concreteslabs.tex", true);
        }
Esempio n. 3
0
        void Initialize()
        {
            var textureQuality = new TextureQuality()
            {
                TextureWrapMode = TextureWrapMode.ClampToEdge,
                Filtering = TextureFiltering.Nearest,
            };

            TextureTarget target;

            int texID = GL.GenTexture();
            var msaa = GL.GetInteger(GetPName.MaxSamples);

            if (MSAA < 0)
                MSAA = 0;

            if (MSAA > msaa)
                MSAA = msaa;

            switch(MSAA)
            {
                case 1:
                case 2:
                case 4:
                case 8:
                case 16:
                case 32:
                case 64:
                case 128:
                case 256:
                    target = TextureTarget.Texture2DMultisample;

                    GL.BindTexture(target, texID);
                    GL.TexImage2DMultisample(TextureTargetMultisample.Texture2DMultisample, MSAA, PixelInternalFormat.Rgba, Width, Height, true);

                    break;
                default:
                    target = TextureTarget.Texture2D;

                    GL.BindTexture(target, texID);
                    GL.TexImage2D(target, 0, PixelInternalFormat.Rgba, Width, Height, 0, PixelFormat.Rgba, PixelType.Byte, IntPtr.Zero);

                    break;
            }

            GL.BindTexture(target, 0);

            Texture = new Texture(texID, Width, Height, target, PixelInternalFormat.Rgba);
            Texture.Quality = textureQuality;

            Texture.Bind();
            Texture.UpdateQuality();
            Texture.UnBind();

            /*texture.Bind();
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1); // automatic mipmap
            texture.UnBind();*/

            // create a renderbuffer object to store depth info
            GL.GenRenderbuffers(1, out rboId);
            GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, rboId);

            if(MSAA > 0)
            {
                GL.RenderbufferStorageMultisample(RenderbufferTarget.Renderbuffer, MSAA, RenderbufferStorage.DepthComponent32, Width, Height);
            }
            else
            {
                GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent32, Width, Height);
            }

            GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0);

            // create a framebuffer object
            GL.GenFramebuffers(1, out fbo);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, fbo);

            // attach the texture to FBO color attachment point
            GL.FramebufferTexture2D(FramebufferTarget.Framebuffer,        // 1. fbo target: GL_FRAMEBUFFER
                                   FramebufferAttachment.ColorAttachment0,  // 2. attachment point
                                   Texture.TextureTarget,         // 3. tex target: GL_TEXTURE_2D
                                   Texture.ID,             // 4. tex ID
                                   0);                    // 5. mipmap level: 0(base)

            // attach the renderbuffer to depth attachment point
            GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer,      // 1. fbo target: GL_FRAMEBUFFER
                                      FramebufferAttachment.DepthAttachment, // 2. attachment point
                                      RenderbufferTarget.Renderbuffer,     // 3. rbo target: GL_RENDERBUFFER
                                      rboId);              // 4. rbo ID

            // check FBO status
            var status = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
            if (status != FramebufferErrorCode.FramebufferComplete)
            {
                Complete = false;
                Console.WriteLine("ERROR: Can not create framebuffer because " + status.ToString());
            }
            else
            {
                Complete = true;
            }

            // switch back to window-system-provided framebuffer
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);

            if (!Complete)
                return;

            var error = GL.GetError();

            while(error != ErrorCode.NoError)
            {
                Debug.WriteLine("Error creating FBO: " + error.ToString());
                error = GL.GetError();
            }

            batch = new VertexBatch();
            batch.StartBatch(PrimitiveType.Triangles);
            batch.Add(new Vector3(-1, -1, 0)); // Bottom left
            batch.Add(new Vector3(1, -1, 0));  // Bottom right
            batch.Add(new Vector3(-1, 1, 0));  // Top left
            batch.Add(new Vector3(1, -1, 0));  // Bottom right
            batch.Add(new Vector3(1, 1, 0)); // Top right
            batch.Add(new Vector3(-1, 1, 0));  // Top left
            batch.EndBatch();
        }
Esempio n. 4
0
        public RenderableTexture(Texture texture)
        {
            Vector2 halfSize = new Vector2(texture.Width / 2.0f, texture.Height / 2.0f);

            model = new Model();
            model.Material = new Material() {
                Transparent = false,
                UniformData = new List<IUniformData>
                {
                    new UniformDataVector4()
                    {
                        Name = "Color",
                        Data = Vector4.One
                    },
                    new UniformDataInt()
                    {
                        Name = "textureSampler",
                        Data = 0
                    }
                },
                ShaderProgram = ShaderManager.Get("TexturedQuad")
            };
            model.Material.Textures.Add("default", texture);
            var mesh = new Mesh();
            mesh.Type = PrimitiveType.TriangleStrip;
            /*mesh.Vertices = new Vector3[]{
                new Vector3(-halfSize.X, -halfSize.Y, 0) * 100.0f,
                new Vector3(halfSize.X, -halfSize.Y, 0) * 100.0f,
                new Vector3(-halfSize.X, halfSize.Y, 0) * 100.0f,
                new Vector3(halfSize.X, halfSize.Y, 0) * 100.0f,
            };*/
            mesh.Vertices = new Vector3[]{
                new Vector3(-1, -1, 0),
                new Vector3(1, -1, 0),
                new Vector3(-1, 1, 0),
                new Vector3(1, 1, 0),
            };
            mesh.UV = new Vector3[][] {
                new Vector3[] {
                    new Vector3(0,1,0),
                    new Vector3(1,1,0),
                    new Vector3(0,0,0),
                    new Vector3(1,0,0),
                }
            };
            mesh.Indices = new uint[]{
                0,1,2,3
            };
            model.Mesh = mesh;
            model.Transform.Position = new Vector3(0, 0, 0);
            model.Transform.Scale = new Vector3(1, 1, 1);
            model.Transform.Rotation = Quaternion.Identity;

            Camera camera = new Camera(null);
            camera.SetAsCurrent();

            float w = EditorRenderer.Width / 2.0f;
            float h = EditorRenderer.Height / 2.0f;

            camera.Projection = Matrix4.CreateOrthographicOffCenter(-w, w, -h, h, -1, 1);
            camera.View = Matrix4.Identity;
        }