Surface texture seams

Blitz3D Forums/Blitz3D Programming/Surface texture seams

Okey dokey.

It's taken a lot of research, and trawling through forum posts looking for clues.
People like Stevie G have posted a lot of useful information over the years - thanks everyone!

The first thing to note is that in my project I am using a texture size of 256 by 256 (texSize=256).
I have tried other sizes just for testing, and my technique doesn't seem to work for other sizes.
I guess the offset needs to be adjusted or something...

I'll use the x direction of an image to describe what I'm doing.

I'm actually grabbing 257 x 257 pixels at a time, and grab (N+1) will start with the 2nd last pixel from grab N.

ie.
- 1st grab will be 0 to 256
- 2nd grab will be 255 to 511
- 3rd grab will be 510 to 766
etc.

The I have to use an offset when setting the uv coordinates in the AddVertex commands. I sort of understand why this is required (directx filtering etc etc), but not well enough to explain it here without sounding silly.

Note that I am using the FreeImage library in my project, but this technique works with Blitz's normal image handling as well.

I hope someone out there finds this helpful.
If anyone can think of any improvements, or thinks I've done something wrong (or indeed if what I've done is nonsense), I'd love to hear any suggestions.

If I come up with any improvements, I'll stick them here as well.

Function getImage()
	Local xCount,yCount
	Local imgHandle,imgWidth,imgHeight
	Local texLeft,texTop,texRight,texBottom
	Local tmpBitmap,tmpImage,tmpTexture,tmpBrush
	Local uvOffset#=0.5/texSize
	Local xDone,yDone
	
	imgHandle=FreeImage_LoadFunction("test image.png")
	imgWidth=FreeImage_GetWidth(imgHandle)
	imgHeight=FreeImage_GetHeight(imgHandle)

	If imgWidth<>0 Then
		If imgHeight<>0 Then
			mesh=CreateMesh()

			xDone=False
			xCount=0
		
			While xDone=False
				yDone=False
				yCount=0
				
				While yDone=False
					qd.quad=New quad
					qd\surface=CreateSurface(mesh)
			
					qd\vertex[0]=AddVertex(qd\surface,0+xCount,1+yCount,0,0+uvOffset#,0+uvOffset#)
					qd\vertex[1]=AddVertex(qd\surface,1+xCount,1+yCount,0,1-uvOffset#,0+uvOffset#)
					qd\vertex[2]=AddVertex(qd\surface,1+xCount,0+yCount,0,1-uvOffset#,1-uvOffset#)
					qd\vertex[3]=AddVertex(qd\surface,0+xCount,0+yCount,0,0+uvOffset#,1-uvOffset#)

					AddTriangle(qd\surface,qd\vertex[0],qd\vertex[1],qd\vertex[3])
					AddTriangle(qd\surface,qd\vertex[2],qd\vertex[3],qd\vertex[1])
	
					texLeft=(xCount*texSize)-xCount
					texRight=texLeft+texSize
	
					If texRight>=imgWidth Then
						texRight=imgWidth
						xDone=True
					EndIf
	
					texBottom=imgHeight-((yCount*texSize)-yCount)
					texTop=texBottom-texSize

					If texTop<=0 Then
						texTop=0
						yDone=True
					EndIf
	
					tmpBitmap=FreeImage_Copy(imgHandle,texLeft,texTop,texRight,texBottom)
					tmpImage=FreeImage_ReadFunction(tmpBitmap)
			
					tmpTexture=CreateTexture(texSize,texSize,1+16+32+256)
					SetBuffer TextureBuffer(tmpTexture)

					If texTop=0 Then
						DrawImage tmpImage,0,texSize-texBottom
					Else
						DrawImage tmpImage,0,0
					EndIf
				
					tmpBrush=CreateBrush()
					BrushTexture tmpBrush,tmpTexture
					BrushFX tmpBrush,1
				
					PaintSurface qd\surface,tmpBrush
			
					If tmpBitmap<>0 Then
						FreeImage_Unload(tmpBitmap)
					EndIf
			
					If tmpImage<>0 Then
						FreeImage tmpImage
					EndIf
						
					If tmpTexture<>0 Then
						FreeTexture tmpTexture
					EndIf
						
					If tmpBrush<>0 Then
						FreeBrush tmpBrush
					EndIf
			
					yCount=yCount+1
				Wend

				xCount=xCount+1
			Wend
		EndIf
	EndIf
		
	If imgHandle<>0 Then
		FreeImage_Unload(imgHandle)
	EndIf
End Function