Exemple #1
0
        protected override void OnLoad(EventArgs e)
        {
            VSync = VSyncMode.On;

            GL.ClearColor(0.1f, 0.2f, 0.5f, 0.0f);
            GL.Enable(EnableCap.DepthTest);

            float aspectRatio = ClientSize.Width / (float)(ClientSize.Height);

            Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, aspectRatio, 1, 100, out projectionMatrix);
            modelviewMatrix = Matrix4.LookAt(new Vector3(0, 3, 5), new Vector3(0, 0, 0), new Vector3(0, 1, 0));

            shader = new Program(vertexShaderSource, fragmentShaderSource);
            shader.Bind();
            shader.UniformMatrix4("camera", false, ref projectionMatrix);
            shader.UniformMatrix4("model", false, ref modelviewMatrix);
            shader.Unbind();

            positionVbo = VertexBuffer.Create(VertexFormat.PositionTexture, positionVboData);
            //normalVbo = VertexBuffer.Create(new VertexFormat(new List<VertexAttribute>() { new VertexAttribute(VertexUsage.TextureCoordinate, VertexAttribPointerType.Float, 0, 2) }), textureVboData);

            indexVbo = IndexBuffer.Create(indicesVboData);
            vao      = new VertexArray(positionVbo, indexVbo);
            Utilities.CheckGLError();
            vao.AddBinding(shader.VertexAttribute("vert").Slot, VertexUsage.Position);
            vao.AddBinding(shader.VertexAttribute("vertTexCoord").Slot, VertexUsage.TextureCoordinate);
            Utilities.CheckGLError();
            Bitmap bmp = new Bitmap(@"..\..\..\Resources\wooden-crate.jpg");

            texture = Texture2D.Create(bmp, TextureMinFilter.Linear, TextureMagFilter.Linear);
        }
Exemple #2
0
        protected override void OnLoad(EventArgs e)
        {
            VSync = VSyncMode.On;

            GL.ClearColor(0.1f, 0.2f, 0.5f, 0.0f);
            GL.Enable(EnableCap.DepthTest);

            float aspectRatio = ClientSize.Width / (float)(ClientSize.Height);

            Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, aspectRatio, 1, 100, out projectionMatrix);
            modelviewMatrix = Matrix4.LookAt(new Vector3(0, 3, 5), new Vector3(0, 0, 0), new Vector3(0, 1, 0));

            shader = new Program(vertexShaderSource, fragmentShaderSource);
            shader.Bind();
            shader.UniformMatrix4("projection_matrix", false, ref projectionMatrix);
            shader.UniformMatrix4("modelview_matrix", false, ref modelviewMatrix);
            shader.Unbind();

            positionVbo = VertexBuffer.Create(new VertexFormat(new List <VertexAttribute>()
            {
                new VertexAttribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3)
            }), positionVboData);
            normalVbo = VertexBuffer.Create(new VertexFormat(new List <VertexAttribute>()
            {
                new VertexAttribute(VertexUsage.Normal, VertexAttribPointerType.Float, 0, 3)
            }), positionVboData);
            indexVbo = IndexBuffer.Create(indicesVboData);
            vao      = new VertexArray(positionVbo, indexVbo);
            Utilities.CheckGLError();
            vao.AddBinding(shader.VertexAttribute("in_position").Slot, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0);
            vao.AddBinding(shader.VertexAttribute("in_normal").Slot, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0);
            Utilities.CheckGLError();
        }
Exemple #3
0
        protected override void OnLoad(EventArgs e)
        {
            VSync = VSyncMode.On;

            GL.ClearColor(0.1f, 0.2f, 0.5f, 0.0f);
            GL.Enable(EnableCap.DepthTest);

            float aspectRatio = ClientSize.Width / (float)(ClientSize.Height);

            Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, aspectRatio, 1, 100, out projectionMatrix);
            modelviewMatrix = Matrix4.LookAt(new Vector3(0, 3, 5), new Vector3(0, 0, 0), new Vector3(0, 1, 0));

            //shader = new Program(vertexShaderSource, fragmentShaderSource);
            //shader.Bind();
            //shader.UniformMatrix4("camera", false, ref projectionMatrix);
            //shader.UniformMatrix4("model", false, ref modelviewMatrix);
            //shader.Unbind();


            ContentManager content = new ContentManager(new ServiceProvider(), "content");

            content.RegisterTypeReader <Texture2D>(new Texture2DReader());
            content.RegisterTypeReader <Program>(new ShaderSourceReader());
            texture = content.Get <Texture2D>("\\wooden-crate.meb").Content as Texture2D;
            shader  = content.Get <Program>("\\simpleTextureShader.meb").Content as Program;
            shader.Bind();
            shader.UniformMatrix4("camera", false, ref projectionMatrix);
            shader.UniformMatrix4("model", false, ref modelviewMatrix);
            shader.Unbind();

            positionVbo = VertexBuffer.Create(VertexFormat.PositionTexture, positionVboData);
            //normalVbo = VertexBuffer.Create(new VertexFormat(new List<VertexAttribute>() { new VertexAttribute(VertexUsage.TextureCoordinate, VertexAttribPointerType.Float, 0, 2) }), textureVboData);

            indexVbo = IndexBuffer.Create(indicesVboData);
            vao      = new VertexArray(positionVbo, indexVbo);
            Utilities.CheckGLError();
            vao.AddBinding(shader.VertexAttribute("vert").Slot, VertexUsage.Position);
            vao.AddBinding(shader.VertexAttribute("vertTexCoord").Slot, VertexUsage.TextureCoordinate);
            Utilities.CheckGLError();
        }
Exemple #4
0
        protected void DrawString(int x, int y, string s, BitmapFont font, int size)
        {
            float curX = x;
            float curY = y;

            List <VertexPositionTexture> vertexData = new List <VertexPositionTexture>();
            List <int> indexData = new List <int>();

            float scale = size / (float)font.CharacterSet.RenderedSize;

            int  i = 0;
            bool firstCharacter = true;

            foreach (char c in s)
            {
                BitmapCharacter bc = font.CharacterSet.GetCharacterByID((int)c);

                float xOffset  = bc.XOffset * scale;
                float yOffset  = bc.YOffset * scale;
                float xAdvance = bc.XAdvance * scale;
                float width    = bc.Width * scale;
                float height   = bc.Height * scale;

                // create the quads triangles for this character
                // uper left
                //vertexData.Add(new VertexPositionTexture(curX + (bc.XOffset + font.CharacterSet.PaddingLeft )* scale, curY + (font.Ascent - bc.YOffset - font.CharacterSet.PaddingUp) * scale, 0, (float)bc.X / (float)font.CharacterSet.Width, (float)bc.Y / (float)font.CharacterSet.Height));
                vertexData.Add(new VertexPositionTexture(curX + xOffset, curY + yOffset, 0, (float)bc.X / (float)font.CharacterSet.Width, (float)bc.Y / (float)font.CharacterSet.Height));
                // upper right
                //vertexData.Add(new VertexPositionTexture(curX + width, curY + (font.Ascent - bc.YOffset - font.CharacterSet.PaddingUp) * scale, 0, (float)(bc.X + bc.Width) / (float)font.CharacterSet.Width, (float)bc.Y / (float)font.CharacterSet.Height));
                vertexData.Add(new VertexPositionTexture(curX + xOffset + width, curY + yOffset, 0, (float)(bc.X + bc.Width) / (float)font.CharacterSet.Width, (float)bc.Y / (float)font.CharacterSet.Height));
                // lower right
                //vertexData.Add(new VertexPositionTexture(curX + bc.Width * scale + bc.XOffset * scale, curY + (font.Ascent - bc.YOffset + bc.Height) * scale, 0, (float)(bc.X + bc.Width) / (float)font.CharacterSet.Width, (float)(bc.Y + bc.Height) / (float)font.CharacterSet.Height));
                vertexData.Add(new VertexPositionTexture(curX + xOffset + width, curY + yOffset + height, 0, (float)(bc.X + bc.Width) / (float)font.CharacterSet.Width, (float)(bc.Y + bc.Height) / (float)font.CharacterSet.Height));
                // lower left
                //vertexData.Add(new VertexPositionTexture(curX + bc.XOffset * scale, curY + (font.Ascent - bc.YOffset + bc.Height) * scale, 0, (float)bc.X / (float)font.CharacterSet.Width, (float)(bc.Y + bc.Height) / (float)font.CharacterSet.Height));
                vertexData.Add(new VertexPositionTexture(curX + xOffset, curY + yOffset + height, 0, (float)bc.X / (float)font.CharacterSet.Width, (float)(bc.Y + bc.Height) / (float)font.CharacterSet.Height));
                indexData.Add(i);
                indexData.Add(i + 1);
                indexData.Add(i + 2);
                indexData.Add(i + 2);
                indexData.Add(i + 3);
                indexData.Add(i);
                i += 4;



                curX += bc.XAdvance * scale;
            }

            VertexBuffer positionVbo = VertexBuffer.Create(VertexFormat.PositionTexture, vertexData.ToArray());
            IndexBuffer  indexVbo    = IndexBuffer.Create(indexData.ToArray());
            VertexArray  vao         = new VertexArray(positionVbo, indexVbo);

            vao.AddBinding(shader.VertexAttribute("vert").Slot, VertexUsage.Position);
            vao.AddBinding(shader.VertexAttribute("vertTexCoord").Slot, VertexUsage.TextureCoordinate);
            vao.Bind();

            font.Shader.Bind();
            font.Shader.Uniform1("tex", 0);
            GL.ActiveTexture(TextureUnit.Texture0);
            font.Texture.Bind();
            indexVbo.Bind();
            GL.DrawElements(BeginMode.Triangles, indexVbo.Length,
                            DrawElementsType.UnsignedInt, IntPtr.Zero);
        }