سؤال

I've got an issue where I'm one of the lucky people who need a Power Of Two (POT) texture and cannot seem to use NPOTs.

Normally, this doesn't bug me too much because I'll just make sure any textures i need to use are POTs. BUT, I've run into a problem trying to render the cursor X and Y positions to the screen.

        public int LoadHudTextures(int xPos, int yPos, Color color, GLControl context)
    {
        context.MakeCurrent();

        int Text_Texture;
        text_bmp1 = new Bitmap(50, 24);

        Text_Texture = GL.GenTexture();
        GL.BindTexture(TextureTarget.Texture2D, Text_Texture);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
        //GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, text_bmp1.Width, text_bmp1.Height, 0,
            //OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);
        GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, 1024, 1024, 0, 
            OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);

        Graphics gfx = Graphics.FromImage(text_bmp1);
        gfx.Clear(Color.Transparent);
        FontFamily fontfamily = new FontFamily("Arial");
        Font font = new Font(fontfamily, 16, FontStyle.Regular,GraphicsUnit.Pixel);
        gfx.DrawString("X:" + xPos.ToString() + "\n"
            + "Y:" + yPos.ToString(), font, new SolidBrush(color), new PointF(1.5f, 1.5f));

        // Upload the Bitmap to OpenGL.
        // Do this only when text changes.
        System.Drawing.Imaging.BitmapData data = text_bmp1.LockBits(new Rectangle(0, 0, text_bmp1.Width, text_bmp1.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, 50, 24, 0,
            OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
        text_bmp1.UnlockBits(data);
        return Text_Texture;

    }

Here's the problem. If I use the "text_bmp1.Width" and "text_bmp1.Height" all I get is a white box. (the object to be textured). I assume this is because of my POT problem. If I use the width and height of a POT (512,1024, etc..) a texture renders, but A) It's not the string I want to display, B) It gets corrupted after a few renders and then blanks out eventually, C) The texture that renders is not cropped and is stretched very oddly.

C) can be fixed if I just change the TexCoord2(,) X and Y values. I am wondering if there is a way to Dynamically pad the image of the text up to the next POT and crop off the excess?

Just to be clear, I am programming an environment that will display the items from a HP/GL2 PCL source code file to the screen before sending it to the printer for Dynamic pharmacy labels.

If anyone needs more clarification please let me know. THANK YOU

هل كانت مفيدة؟

المحلول

GL.TexImage2D destroys the currently bound texture and recreates it with the size you specify. What you wish to do is use GL.TexImage2D once, when you create the POT texture, and use GL.TexSubImage2D to update specific parts of it.

When you render this texture on screen, specify texcoords that correspond to the specific part you updated, i.e. do not render from (0,0) to (1,1) but from (0,0) to (text_width / pot_width, text_height / pot_height). This way, you ignore the padding and avoid stretching the texture.

In any case, do check out QuickFont ( http://www.opentk.com/project/QuickFont ), which allows you to render text directly without going into the dirty details.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top