Simple texture class

BlitzMax Forums/OpenGL Module/Simple texture class

ozak(Posted 2005) [#1]
Hi. I just threw together a quick texture class as my first BMX project. Use as you please :)



Just throw it in a file named Texture.bmx and Import it at the top of your source file.

Now you can create and load a texture like so.

MyTex:Texture
MyTex=new Texture
MyTex.Load("MyTex.tga")


And then you simply bind it before drawing your 3D model.

MyTex.Bind()


Oh and I'm a new user BTW but have a long record as a professional game programmer on both PC, Playstation and Mac using all from assembler, over C/C++ to Java.

I'm hoping to contribute some nice OOP based classes for my own prototyping amusement in BlitzMax and possibly do a few simple games for fun :)

Oh yeah. Just ask me any question about game programming, OpenGL etc. If I have the time, I'm always up for a chat :)


StuC(Posted 2005) [#2]
Welcome.


ozak(Posted 2005) [#3]
Thanks :)


Gabriel(Posted 2005) [#4]
I'm not a new user but I don't have a long record as a professional programmer, particularly in relation to OOP based classes, so you'll have to forgive me what may be a stupid question.

Is there a particular reason that Load is a method and not a function? It seems like you could then create the new instance within the function to save one more step every time you load a new texture.


N(Posted 2005) [#5]
I have to ask what's up with that as well...

That really doesn't make any sense to me, as Load would prove far more useful as a static function. And making it like so would work better because then it doesn't always have to be an unloaded file, it could be a existing TStream, string, or any other type.

Method Load(url:Object)
	
	' Attempt To load image
	TextureImage:TPixmap=LoadPixmap(url)
	
	' Save dimensions for later
	Width=TextureImage.Width
	Height=TextureImage.Height
	
	' Create GL texture
	glGenTextures 1, Varptr TexID
	
	' Bind it first
	Bind()

	' Enable bilinear filtering
	glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR
	glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR
	
	' Create actual texture
	glTexImage2D GL_TEXTURE_2D, 0, 3, Width, Height, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage.pixels
	
	' Flag texture as unused so the garbage collector can do it's thing
	TextureImage = Null
	
	' Save filename for later
	Self.FileName = FileName

EndMethod



ozak(Posted 2005) [#6]
Uh yeah. I had been programming in Blitzbasic for about 15 minutes when I wrote it.
I do however think it should be a method since it accesses internal class members. In other OOP languages you rarely use static functions, since it can seriously complicate reading the code later. But yeah. You could do that :)