Advertisement Jump to content
Sign in to follow this  

OpenGL Need help with OpenTK

This topic is 939 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);


            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);


            return id;

And the render function

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

        GL.Ortho(0.0, 1.0, 0.0, 1.0, 0.0, 4.0);
        GL.BindTexture(TextureTarget.Texture2D, blueShipTexture);

        //BlueShip.png is 150pxX120px

        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);



Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!