0 Replies - 2472 Views - Last Post: 01 November 2010 - 12:11 PM Rate Topic: -----

#1 matio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 01-September 09

OpenTK texture coordinate problem

Posted 01 November 2010 - 12:11 PM

Hi everyone. I am trying to load a texture with OpenTK, a C# library for OpenGL. The texture is not a power of 2 (eg 64,128,256 etc.), so I am finding the next power of two, creating an image that size and drawing the original image on to the new one and then finding the ratio:
using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using System.Drawing;
using System.Drawing.Imaging;

namespace StarterKit {
	public class Tex {
		public int tex;
		public int width,height;
		public float width_ratio, height_ratio;
		
		public Tex(string filename) {
			LoadTexture(filename);
		}
		
		private double PowerOf2(int num) {
			return Math.Pow(2, Math.Ceiling(Math.Log(num,2)));
		}
		
		public void 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);
			width = bmp.Width;
			height = bmp.Height;
			int w2 = (int)PowerOf2(width);
			int h2 = (int)PowerOf2(height);
			Bitmap bmp2 = new Bitmap(w2, h2);
			Graphics gfx = Graphics.FromImage(bmp2);
			gfx.DrawImage(bmp, new Point(0,0));		
			width_ratio = width/(float)w2;
			height_ratio = 1 - height/(float)h2;
			bmp2.Save("save.bmp");
		    BitmapData bmp_data = bmp2.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
		    GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp_data.Width, bmp_data.Height, 0,
		        OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0);
		 
		    bmp2.UnlockBits(bmp_data);
		 
		    // We haven't uploaded mipmaps, so disable mipmapping (otherwise the texture will not appear).
		    // On newer video cards, we can use GL.GenerateMipmaps() or GL.Ext.GenerateMipmaps() to create
		    // mipmaps automatically. In that case, use TextureMinFilter.LinearMipmapLinear to enable them.
		    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
		    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
		 
		    tex = id;
		}
	}



Here is the drawing code:
	class Square {
		public int x, y, z;
		private Tex tex;
		private double? x_rot, y_rot, z_rot;
		
		public Square (int _x, int _y, int _z=10) {
			x = _x;
			y = _y;
			z = _z;
			tex = new Tex("rocket.bmp");
			x_rot = y_rot = z_rot = null;			
		}
		
		public void Rotate(double? x_angle=null, double? y_angle=null, double? z_angle=null) {
			x_rot = x_angle;
			y_rot = y_angle;
			z_rot = z_angle;
		}
		
		public void Render () {
			if (x_rot != null) {
				GL.Rotate((double)x_rot, 1,0,0);
			}
			if (y_rot != null) {
				GL.Rotate((double)y_rot, 0,1,0);
			}
			if (z_rot != null) {
				GL.Rotate((double)z_rot, 0,0,1);
			}
			GL.BindTexture(TextureTarget.Texture2D, tex.tex);
				//GL.Rotate(1,0,0,1);			
				GL.Begin (BeginMode.Quads);
					GL.TexCoord2(0,1); GL.Vertex2 (x, y+tex.height);		
					GL.TexCoord2(tex.width_ratio,1); GL.Vertex2 (x+tex.width, y+tex.height);
		            GL.TexCoord2(tex.width_ratio,tex.height_ratio);  GL.Vertex2 (x+tex.width, y);
					GL.TexCoord2(0,tex.height_ratio); GL.Vertex2 (x, y);			
				GL.End ();
		}
	}
}



Unfortunately I seem to have got the texture coordinates wrong, though I am not sure how. Instead if the whole texture been shown there is only 2/3 of it shown.

Game.cs:
using System; 

using OpenTK;

using OpenTK.Graphics;

using OpenTK.Graphics.OpenGL;

using OpenTK.Audio;

using OpenTK.Audio.OpenAL;

using OpenTK.Input;



namespace StarterKit 

{ 

	

    class Game : GameWindow 

    { 

		private Square s; 

		float rot = 5f;

        public Game()

            : base(800, 600, GraphicsMode.Default, "OpenTK Quick Start Sample")

 

        {

            VSync = VSyncMode.On; 

			s = new Square (200,200); 

        }



        protected override void onload(EventArgs e) 

        { 

            base.onload(e); 

//            GL.Enable (EnableCap.DepthTest); 

			GL.Enable (EnableCap.Texture2D);			

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

			GL.MatrixMode (MatrixMode.Projection);

			GL.LoadIdentity ();

			GL.Ortho (0,800,600,0,-100,100);

			GL.MatrixMode(MatrixMode.Modelview);

			GL.LoadIdentity ();

        }

 

         protected override void onresize(EventArgs e)

        {

            base.onresize(e);

//            GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);

//            Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 64.0f); 

//            GL.MatrixMode(MatrixMode.Projection); 

//            GL.LoadMatrix(ref projection); 

        }

 

        protected override void OnUpdateFrame(FrameEventArgs e) 

        { 

            base.OnUpdateFrame(e);

            if (Keyboard[Key.Escape]) 

                Exit();

        }

		

        protected override void OnRenderFrame(FrameEventArgs e) 

        { 

            base.OnRenderFrame(e);   

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); 

//            Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY); 

//            GL.MatrixMode(MatrixMode.Modelview); 

//            GL.LoadMatrix(ref modelview);

			//rot += 0.01f;

			//s.Rotate(null,null,rot);

			s.Render ();

            SwapBuffers(); 

        }



        [STAThread]

        static void Main() 

        {

            using (Game game = new Game())

            {

                 game.Run(30.0);

            }

        }

    }

}



Have I got the coordinates wrong?

EDIT: Sorry about the formatting in the last code block. I pasted it in but when I pressed submit it added the line breaks

This post has been edited by matio: 01 November 2010 - 12:36 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1