Jump to content
  • Advertisement
Sign in to follow this  
Paragon123

OpenGL Need help with OpenTK

This topic is 840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

 So, I am trying to use OpenTK for a tile based game in C#... I have a bit of code now, that loads a texture... then I try to render it onto two triangles to make a quad... 

 

There are two things that I can't explain happening.

1) When I proved texture vertexes where the bottom left corner is (0,0) and the top right is (1,1) I end up with the sprite being tiled 2x2... as it is in the code now using (0,0) as the bottom left and (0.5, 0.5) as the top right shows the entire sprite once (not the bottom left quarter of the sprite as I would have expected.)

 

2) I set the orthoginal screen space to be between 0 and 1 for the x and y axis... yet when I render two triangles to generate a quad with bottom left corner (0,0) and top right corner (1,1) the quad only covers the top right corner of the screen rather than the entire screen as I would have expected.

 

 

Can some please help me figure out what is going on?

Here is the texture load code:

        static int LoadTexture(string filename)
        {
            if (String.IsNullOrEmpty(filename))
                throw new ArgumentException(filename);

            int id = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, id);

            GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);

            Bitmap bmp = new Bitmap(filename);
            BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp.Width, bmp.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0);

            bmp.UnlockBits(bmp_data);

            return id;
        }

And the render function

RenderFrame(sender, e)
                {
        // render graphics
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

        GL.MatrixMode(MatrixMode.Texture);
        GL.LoadIdentity();
        GL.Ortho(0.0, 1.0, 0.0, 1.0, 0.0, 4.0);
        GL.Enable(EnableCap.Texture2D);
        GL.BindTexture(TextureTarget.Texture2D, blueShipTexture);

        //BlueShip.png is 150pxX120px
        GL.Begin(PrimitiveType.Triangles);

        GL.TexCoord2(0, 0.5f); GL.Vertex2(0, 1);
        GL.TexCoord2(0.5f, 0.5f); GL.Vertex2(1, 1);
        GL.TexCoord2(0.5f, 0); GL.Vertex2(1, 0);

        GL.TexCoord2(0, 0); GL.Vertex2(0, 0);
        GL.TexCoord2(0, 0.5f); GL.Vertex2(0, 1);
        GL.TexCoord2(0.5, 0);GL.Vertex2(1, 0);

        GL.End();


        game.SwapBuffers();
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!