
Texture problems
Posted Sunday, 21 February, 2010 - 01:45 by Micah inHello everyone,
I am having troubles getting my texture to display on my quad. i have look at a bunch of topics in on these forums and also general opengl forums and have not found anything that has helped me fix my problems.
i made a ball class that contains all my object code including loading textures and drawing the object.
am i able to call GL functions in another class? or should i keep all my drawing code inside the main class?
here is my code that im using to load the texture
public void loadTexture(string File) { textureId = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D,textureId); Bitmap bmp = new Bitmap(File); System.Drawing.Imaging.BitmapData bmp_data = bmp.LockBits(new Rectangle(0,0,bmp.Width,bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly,System.Drawing.Imaging.PixelFormat.Format24bppRgb); 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); GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureMinFilter,(int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureMagFilter,(int)TextureMagFilter.Linear); }
and here is the draw code
public void Draw() { GL.BindTexture(TextureTarget.Texture2D,textureId); GL.Enable(EnableCap.Texture2D); GL.Begin(BeginMode.Quads); GL.TexCoord2(0,0); GL.Vertex2(-(radius) + position.X,(radius * 2) + position.Y); GL.TexCoord2(0,1f); GL.Vertex2((radius) + position.X,(radius * 2) + position.Y); GL.TexCoord2(1f,1f); GL.Vertex2((radius) + position.X,position.Y); GL.TexCoord2(1f,0); GL.Vertex2(-(radius) + position.X, position.Y); GL.End(); }
i can get the square to draw but it just draws solid red...but i never told it to draw red. i changed the ball image to a purple to see if somehow the texture was loading but just not mapping right but the square remained red.
also im using ubuntu linux 9.10 and monodevelop.
any help would be great!
thank you ,
Micah.


Comments
Re: Texture problems
You can call GL commands wherever you want, as long as they are in a thread which contains a "current" GraphicsContext.
Try changing
System.Drawing.Imaging.PixelFormat.Format24bppRgbtoPixelFormat.Format32bppRgba(in the following line you are callingGL.TexImage2D(..., OpenTK.Graphics.OpenGL.PixelFormat.Bgra, ...), which matches 32bppRgba, rather than 24bppRgb.) I can't see anything else wrong in your code, but you can try the debug version of OpenTK.dll (included in the zip) which will notify you of OpenGL errors immediately. Makes debugging quite a bit easier!Re: Texture problems
And for the texture loading :
Re: Texture problems
Edit... just messed around with stuff and somehow it just started working! i have no idea what made it work but it's working now. that for all the help :)
Thanks for the replys.
now i have a white square :) texture is still not showing.
here is my full code for both the main class and the GameObject... if i really need to i can keep all the drawing code inside the main class but the reason behind this little project is to make some general game objects for a game i am working on. so if i can keep the drawing code for each object seperate from all the rest that would be best. but if thats just not possible does anyone have any ideas on what i could do?
Game.cs..
GameObject2d.cs...
note that i have been building my code from modifying the quickstart code.
also i may have some GL calls that may not need to be called... i have been trying to get textures working for a couple days and so have been throwing anything i find into there hoping it will work...
also little side question.
my coord system right looks like this
-----------------------------------
|0,1................ -1.333,1 |
| .................................|
| .................................|
|0,0_________ -1.333,0|
what should i do to make it like this?...
-----------------------------------
|0,1................. 1.333,1 |
|................................. |
|................................. |
|0,0___________1.333,0|
i have been playing around with the orthoprojection but all i have managed to do is flip the coord system on the x axis(in other words my 0,0 is on the right and my -1.333,0 is on the left)
sorry for the noobness i have been programming for years but am new to openGL and am having a hard time finding any documentation that is current or that is relative to opentk.
thanks for the help,
Micah.
Re: Texture problems
i figured out my coord error. needed to set the right coord to -1.3333 instead of 1.3333.
kinda feel dumb for that one lol :)