Unable to calculate tex size

BlitzMax Forums/OpenGL Module/Unable to calculate tex size

deps(Posted 2006) [#1]
Got a bit of a problem using OpenGL on XP...

The game works just fine on Mac OSX 10.3.9 but crashes with the following error message on Win XP sp2:

Unhandled Exception: Unable to calculate tex size


at the line with GLTexFromPixmap here:

	Local pix:tpixmap
	
	pix = LoadPixmap( "data/misc/flare.png" )
	flare_tex = GLTexFromPixmap( pix )


Not sure if it's relevant but here is how I setup the grahics:

SetGraphicsDriver GLGraphicsDriver()
Graphics screen_w,screen_h,0

MoveMouse screen_w_half, screen_h_half
HideMouse()

glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT

glenable GL_TEXTURE_2D
glenable GL_DEPTH_TEST								'Enables Depth Testing
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
glBlendFunc(GL_SRC_ALPHA, GL_ONE)
glEnable(GL_BLEND)


Anyone got any solution?

Edit:
I am setting the graphics before trying to load the image, so this was not a solution for me: http://www.blitzmax.com/Community/posts.php?topic=43320
This didn't work either: http://www.blitzmax.com/Community/posts.php?topic=53842


xlsior(Posted 2006) [#2]
It could be that you don't have an openGL capable videodriver on the PC, or that the resolution you are trying to enter isn't supported by the system.

(do you define screen_w and screen_h anywhere?)


deps(Posted 2006) [#3]
screen_w and _h is defined to 640 and 480. I knew that wasn't the problem since the unmodified code works just great on my mac. :)

The resolution is supported and I do think the driver can handle OpenGL since the GLBlurr demo that comes with BlitzMax uses the same resolution and works just fine.

All textures are loaded after the call to graphics. Switching to GlGraphics does not solve the problem. :/


Chris C(Posted 2006) [#4]
are you using framework?
have you imported the apropriate jpg/png loader module?

have you tried anothe png ?

can you post a minimum example that shows the problem


deps(Posted 2006) [#5]
I do not use framework. Never used it before. Not sure how to use it, but I guess I have to add a lot of import stuff afterwards to make it compile at all?
If this is just to make the exe smaller then it can wait. It doesn't bother me if it's a Mb or two too big while I'm still coding on it.

I haven't tried another png, but it's already at a power-of-two size (128*128). And why is it working on Mac but not on XP? I could try to use another image later when I have access to the XP computer again, and creating an example. (With my luck I guess the example will work just fine. :P)

Thanks for the replies. If anyone have other tips then feel free to post. :)


Chris C(Posted 2006) [#6]
any luck deps?


deps(Posted 2006) [#7]
Yes, I think I have found the problem. Tried to post about it yesterday but this site was down. :/

The problem seemes to be GLAdjustTexSize(), but I'm not skilled enough in OpenGL to see what it's doing wrong and how I could fix it.
GLTexFromPixmap calls it before it converts the pixmap into a GL texture.

The XP computer got the latest drivers for the integrated graphics card and it supports OpenGL.


deps(Posted 2006) [#8]
Problem is still there and I don't have a solution. Anyone know of one? Other games using OpenGL seemes to run fine.


Dreamora(Posted 2006) [#9]
If this error appears then the pixmap has not been loaded correctly for some reason as it only appears when the pixmap width & height is recognized as 1 or 0 (as it starts with a width / height in the Powof2 of 1)


deps(Posted 2006) [#10]
Here is some code that reproduces the error:
SuperStrict

GLGraphics( 800, 600, 0 )

Local pix:tpixmap = LoadPixmapPNG("bmax120.png")

If Not pix Then
	RuntimeError("Unable to load texture.")
EndIf

Local glimg:Int = GLTexFromPixmap( pix ) ' BOOM!

WaitKey ' never reached...

End

The image is loaded, since I don't get a runtimeerror saying that it was unable to load the texture.
I guess the integrated intel graphics chipset thingy is the problem here, but I already have the latest drivers for it so there isn't much else I can do. :/


Dreamora(Posted 2006) [#11]
No problem here. After I changed it to a texture I actually had in the same folder as the source, it worked without any problems.

I'm on an ATI Radeon 9700 mobile with modded Catalyst 6.2 drivers. WinXP Pro SP2


Chris C(Posted 2006) [#12]
works fine here too... :|

the only thing I can think is its the implementation
of opengl for the intel driver.

Out of interest have you tried a jpg instead, tho that
png has no alpha, which might have been a suspect

Nasty one, hope you suss it.


boomboommax(Posted 2006) [#13]
use a lower driver version


Defoc8(Posted 2006) [#14]
This might be dumb - but what are the image dimensions?
are they power of 2?..256x256, 512x512....if not, try it again
with a square texture @ 256x256..


BlackSp1der(Posted 2006) [#15]
use it

_Max2DDriver.CreateFrameFromPixmap(pix,flags)

instead

GLTexFromPixmap( pix )


Vertex(Posted 2006) [#16]
Hi!
Yes, GLAdjustTexSize doesn't work here too. The problem lies by the Proxytextures. BMax use this Proxytextures to check, if is a textureformat in this resolution and colordepth supported by the graphicscard.

So I used in my own 3D engine:
Function AdjustTexSize(Width:Int Var, Height:Int Var)
	Function Pow2Size:Int(N:Int)
		Local Size:Int

		Size = 1
		While Size < N
			Size = Size Shl 1
		Wend

		Return Size
	End Function

	Width  = Pow2Size(Width)
	Height = Pow2Size(Height)
End Function


You can use you own GLTexFromPixmap too:
Function TexFromPixmap( pixmap:TPixmap,mipmap=True )
	If pixmap.format<>PF_RGBA8888 pixmap=pixmap.Convert( PF_RGBA8888 )
	Local width=pixmap.width,height=pixmap.height
	AdjustTexSize width,height
	If width<>pixmap.width Or height<>pixmap.height pixmap=ResizePixmap( pixmap,width,height )
	
	Local old_name,old_row_len
	glGetIntegerv GL_TEXTURE_BINDING_2D,Varptr old_name
	glGetIntegerv GL_UNPACK_ROW_LENGTH,Varptr old_row_len

	Local name
	glGenTextures 1,Varptr name
	glBindtexture GL_TEXTURE_2D,name
	
	Local mip_level
	Repeat
		glPixelStorei GL_UNPACK_ROW_LENGTH,pixmap.pitch/BytesPerPixel[pixmap.format]
		glTexImage2D GL_TEXTURE_2D,mip_level,GL_RGBA8,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,pixmap.pixels
		If Not mipmap Exit
		If width=1 And height=1 Exit
		If width>1 width:/2
		If height>1 height:/2
		pixmap=ResizePixmap( pixmap,width,height )
		mip_level:+1
	Forever
	
	glBindTexture GL_TEXTURE_2D,old_name
	glPixelStorei GL_UNPACK_ROW_LENGTH,old_row_len

	Return name
End Function


In MiniB3D for example, I must replace explicit GLAdjustTexSize with AdjustTexSize to make it work. But on compiled games there is no way. So this is very importent to use.

cu olli


Kistjes(Posted 2007) [#17]
Hello,

I need some help here. It's a rather complex problem but I'll try to explain. On some computers I get the error "Unable to calculate tex size" too. Unfortunatly not no my PC, so it's hard to debug.

It's in the AdjustPixmap code in TTexture.bmx
The code I show you below is from Klepto and it's posted in a post I did for another problem (see: http://www.blitzbasic.com/Community/posts.php?topic=72501#811490)
	Function AdjustPixmap:TPixmap(pixmap:TPixmap)
	
		' adjust width and height size to next biggest power of 2 size
		Local width=Pow2Size(pixmap.width)
		Local height=Pow2Size(pixmap.height)

		' check that width and height size are valid (not too big)
		Repeat
			Local t
			glTexImage2D GL_PROXY_TEXTURE_2D,0,4,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,Null
			glGetTexLevelParameteriv GL_PROXY_TEXTURE_2D,0,GL_TEXTURE_WIDTH,Varptr t
			If t Exit
			If width=1 And height=1 RuntimeError "Unable to calculate tex size"
			
			If width>1 width:/2
			If height>1 height:/2
		Forever

		' if width or height have changed then resize pixmap
		If width<>pixmap.width Or height<>pixmap.height
			pixmap=ResizePixmap(pixmap,width,height)
		EndIf
		
		' return pixmap
		Return pixmap
		
	End Function

The problem should lie in the two lines:
			glTexImage2D GL_PROXY_TEXTURE_2D,0,4,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,Null
			glGetTexLevelParameteriv GL_PROXY_TEXTURE_2D,0,GL_TEXTURE_WIDTH,Varptr t


It seems there is a possibility the pixmap is interpreted wrong on some systems. I checked if the (intergrated) graphics cards have OpenGL support.
Are there more specific OpenGL-compatibilities I have to check for, or is this a software problem that can be solved?

PS: I tried different colour depths:
Graphics3D 1024, 768, 32, 1
Graphics3D 1024, 768, 16, 1
Graphics3D 1024, 768, 0, 1
with no results.

<edit>
Replaced the AdjustPixmap() function with the remarked version in TTexture:
	Function AdjustPixmap:TPixmap(pixmap:TPixmap)
	
		Local width=Pow2Size(pixmap.width)
		Local height=Pow2Size(pixmap.height)

		If width<>pixmap.width Or height<>pixmap.height
			pixmap=ResizePixmap(pixmap,width,height)
		EndIf
		
		Return pixmap
		
	End Function
and that solved the problem. I'm happy with that for now (there's a user test comming up next week so I was in desperate need of a working version) but I'd like to now what is going on. Klepto2... any ideas?


JoshK(Posted 2007) [#18]
BRL's GLAdjustTexSize() function is written wrong. Here is a working modification:
http://blitzmax.com/Community/posts.php?topic=73299

I don't know why they used a proxy texture, when you can easily fetch the max texture size from OpenGL.