Exemplo n.º 1
0
    public void CreateBuffer_Creates_Buffer()
    {
        var bufferSize = 50;

        var buffer = factory.CreateBuffer(bufferSize);

        Assert.NotNull(buffer);
    }
Exemplo n.º 2
0
        protected override void OnLoadContent()
        {
            try
            {
                var currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

                // load shaders
                var vertexShaderFile   = Path.Combine(currentDirectory, "VertexShader.txt");
                var fragmentShaderFile = Path.Combine(currentDirectory, "FragmentShader.txt");

                this.ShaderProgram = new ShaderProgramFactory <SampleShader>()
                                     .Shader(() => new ShaderFactory(ShaderType.GL_VERTEX_SHADER).Source(vertexShaderFile).Compile())
                                     .Shader(() => new ShaderFactory(ShaderType.GL_FRAGMENT_SHADER).Source(fragmentShaderFile).Compile()
                                             ).Link();

                // create buffers to contain our quad
                var bufferFactory = new BufferFactory();
                this.VertexBuffer = bufferFactory.CreateBuffer(BufferTarget.GL_ARRAY_BUFFER);
                this.IndiceBuffer = bufferFactory.CreateBuffer(BufferTarget.GL_ELEMENT_ARRAY_BUFFER);

                Vertex[] box = new[]
                {
                    new Vertex(-0.5f, -0.5f, 0.5f, 0, 0), new Vertex(0.5f, -0.5f, 0.5f, 1f, 0),
                    new Vertex(-0.5f, 0.5f, 0.5f, 0, 1f), new Vertex(0.5f, 0.5f, 0.5f, 1f, 1f),
                    // BACK
                    new Vertex(-0.5f, -0.5f, -0.5f, 1, 0), new Vertex(-0.5f, 0.5f, -0.5f, 1, 1),
                    new Vertex(0.5f, -0.5f, -0.5f, 0, 0), new Vertex(0.5f, 0.5f, -0.5f, 0, 1),
                    // LEFT
                    new Vertex(-0.5f, -0.5f, 0.5f, 1, 0), new Vertex(-0.5f, 0.5f, 0.5f, 1, 1),
                    new Vertex(-0.5f, -0.5f, -0.5f, 0, 0), new Vertex(-0.5f, 0.5f, -0.5f, 0, 1),
                    // RIGHT
                    new Vertex(0.5f, -0.5f, -0.5f, 1, 0), new Vertex(0.5f, 0.5f, -0.5f, 1, 1),
                    new Vertex(0.5f, -0.5f, 0.5f, 0, 0), new Vertex(0.5f, 0.5f, 0.5f, 0, 1),
                    // TOP
                    new Vertex(-0.5f, 0.5f, 0.5f, 0, 0), new Vertex(0.5f, 0.5f, 0.5f, 1, 0),
                    new Vertex(-0.5f, 0.5f, -0.5f, 0, 1), new Vertex(0.5f, 0.5f, -0.5f, 1, 1),
                    // BOTTOM
                    new Vertex(-0.5f, -0.5f, 0.5f, 1, 0), new Vertex(-0.5f, -0.5f, -0.5f, 1, 1),
                    new Vertex(0.5f, -0.5f, 0.5f, 0, 0), new Vertex(0.5f, -0.5f, -0.5f, 0, 1)
                };

                // upload data to the buffers
                this.GraphicsDevice.BindBuffer(this.VertexBuffer);
                this.VertexBuffer.BufferData(box.Length * Marshal.SizeOf(typeof(Vertex)), box, BufferUsage.GL_STATIC_DRAW);

                //uint[] indices = new uint[]
                //                     {
                //                             0, 1, 2, 3
                //                     };

                //this.GraphicsDevice.BindBuffer(this.IndiceBuffer);
                //this.IndiceBuffer.BufferData(Marshal.SizeOf(typeof(uint)) * 4, indices, NativeGl.GL_STATIC_DRAW);

                // enable vertex attrib pointers
                this.GraphicsDevice.EnableVertexAttribArray(this.ShaderProgram.Vertex.Location);
                this.GraphicsDevice.EnableVertexAttribArray(this.ShaderProgram.TexCoord.Location);

                this.GraphicsDevice.VertexAttribPointer(
                    this.ShaderProgram.Vertex.Location,
                    3,
                    GlType.GL_FLOAT,
                    GlBoolean.GL_FALSE,
                    Marshal.SizeOf(typeof(Vertex)));

                this.GraphicsDevice.VertexAttribPointer(
                    this.ShaderProgram.TexCoord.Location,
                    2,
                    GlType.GL_FLOAT,
                    GlBoolean.GL_FALSE,
                    Marshal.SizeOf(typeof(Vertex)),
                    new IntPtr(Marshal.SizeOf(typeof(float)) * 3));

                // use the shader);));
                this.GraphicsDevice.UseProgram(this.ShaderProgram);

                // our camera matrix);)
                // this.View = Matrix4.LookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
                this.Camera = new Camera(new Vector3(-4, 1.5f, -4), new Vector3(0, 0, 0), new Vector3(0, 1, 0));

                this.Camera.Rotate(0, 0, 0);

                // projection matrix
                var fovy = this.RenderingWindow.Width / (float)this.RenderingWindow.Height;
                this.Projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45), fovy, 1, 100);

                // world matrix
                this.World = Matrix4.CreateTranslation(0, -1, 0);

                // load texture
                this.TextureFactory = new TextureFactory(this.GraphicsDevice);
                this.texture        = this.TextureFactory.CreateFromFile(Path.Combine(currentDirectory, @"Content\Textures\texture01.jpg"));

                this.GraphicsDevice.ActivateTextureUnit(TextureUnit.GL_TEXTURE1);
                this.GraphicsDevice.BindTexture(TextureTarget.GL_TEXTURE_2D, this.texture.TextureName);

                // set initial shader values
                this.ShaderProgram.BaseTexture.SetValue(this.texture, 1); // TextureUnit.GL_TEXTURE1
                this.ShaderProgram.World.SetValue(this.World);
                this.ShaderProgram.View.SetValue(this.Camera.View);
                this.ShaderProgram.Projection.SetValue(this.Projection);

                this.GraphicsDevice.Enable(Cap.GL_CULL_FACE);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
                this.ExitGame = true;
            }
        }