Textured Polygon

BlitzMax Forums/BlitzMax Programming/Textured Polygon

The TexturedPoly Func works perfectly now that the Ints have been changed to Floats, thanks warpy. Just one thing though, I noticed that the line,

DrawImage tmpImage, -100, - 100 ' Chtob zbit' flag texturi


at the end of the 'DrawTexturedPolyOGL' function is there to correct a colour problem? Without this call to DrawImage the colours go screwy and are to dark. I managed to hide the image behind my poly tiles, which is fine except this extra call to DrawImage is an overhead my code could do without.

Even tried checking the 'glmax2D' mod to try found out how drawimage is correcting the colour,without any luck! Does anyone have any idea how to correct the colour without having to use DrawImage?

I thought of drawing all the tiles first them calling DrawImage to correct the colour, but Im making an Iso map, so need to layer my tiles with Bmps,so am forced to make the extra DrawImage function call each tile draw to corret the colour first.

Function DrawTexturedPoly( image:TImage,xyuv#[],frame=0, vertex = -1)
	Local handle_x#, handle_y#
	GetHandle handle_x#, handle_y#
	Local origin_x#, origin_y#
	GetOrigin origin_x#, origin_y#

	Local D3DDriver:TD3D7Max2DDriver = TD3D7Max2DDriver(_max2dDriver)

	Assert Image, "Image not found"

	If D3DDriver Then
		DrawTexturedPolyD3D ..
		D3DDriver,..
		TD3D7ImageFrame(image.Frame(frame)), ..
		xyuv, handle_x, handle_y, origin_x,origin_y, vertex*4
		Return
	End If

	Local OGLDriver:TGLMax2DDriver = TGLMax2DDriver(_max2dDriver)
	If OGLDriver Then
		DrawTexturedPolyOGL ..
		OGLDriver,..
		TGLImageFrame(image.Frame(frame)), ..
		xyuv, handle_x, handle_y, origin_x,origin_y, vertex*4
		Return
	End If
End Function


Function DrawTexturedPolyD3D( Driver:TD3D7Max2DDriver, Frame:TD3D7ImageFrame,xyuv#[],handlex#,handley#,tx#,ty# , vertex)
	If Driver.islost Return
	If xyuv.length<6 Return
	Local segs=xyuv.length/4
	Local len_ = Len(xyuv)

	If vertex > - 1 Then
		segs = vertex / 4
		len_ = vertex
	End If
	Local uv#[] = New Float[segs*6] ' 6


	Local c:Int Ptr=Int Ptr(Float Ptr(uv))

	Local ii:Int = 0
	For Local i=0 Until len_ Step 4
		Local x# = xyuv[i+0]+handlex
		Local y# = xyuv[i+1]+handley
		uv[ii+0] = x*Driver.ix+y*Driver.iy+tx
		uv[ii+1] = x*Driver.jx+y*Driver.jy+ty
		uv[ii+2] = 0 ' *********** THIS IS THE Z-COORDINATE
		c[ii+3] = Driver.DrawColor
		uv[ii+4] = xyuv[i+2]
		uv[ii+5] = xyuv[i+3]
		ii:+6
	Next

	Driver.SetActiveFrame Frame
	Driver.device.DrawPrimitive(D3DPT_TRIANGLEFAN,D3DFVF_XYZ| D3DFVF_DIFFUSE | D3DFVF_TEX1,uv,segs,0)
End Function

Function DrawTexturedPolyOGL (Driver:TGLMax2DDriver, Frame:TGLImageFrame, xy#[],handle_x#,handle_y#,origin_x#,origin_y#, vertex) 
	Private
	Global TmpImage:TImage
	Public
	
	If xy.length<6 Return
	
	Local rot#  = GetRotation()
	Local tform_scale_x#, tform_scale_y#
	GetScale tform_scale_x, tform_scale_y
	
	Local s#=Sin(rot)
	Local c#=Cos(rot)
	Const scale#=1.28
	Local ix#= c*tform_scale_x*scale
	Local iy#= -s*tform_scale_y*scale
	Local jx#= s*tform_scale_x*scale
	Local jy#= c*tform_scale_y*scale
	
	glBindTexture GL_TEXTURE_2D, Frame.name
	glEnable GL_TEXTURE_2D
	
	glBegin GL_POLYGON
	For Local i=0 Until Len xy Step 4
		If vertex > -1 And i >= vertex Then Exit
		Local x#=xy[i+0]+handle_x
		Local y#=xy[i+1]+handle_y
		Local u#=xy[i+2]
		Local v#=xy[i+3]
		glTexCoord2f u,v
		glVertex2f x*ix+y*iy+origin_x,x*jx+y*jy+origin_y
	Next
	glEnd
	If Not tmpImage Then tmpImage = CreateImage(1,1)
	DrawImage tmpImage, -100, - 100 ' Chtob zbit' flag texturi
End Function


Interesting piece of code !
Don't know well at this point if all these specific drivers things will work on different hardware and if it's fast though.

I'm wondering if adding repeated UV (tiled texture) is possible ?

There is a bindTexture function for opengl that do the trick.
Don't know about DX though.

I get this here :
http://blitzmax.com/Community/posts.php?topic=79462#894458

Function SetTextureRepeat(image:TImage)
	Local n:Int = image.seqs.Length
	Repeat
		n:-1
		glBindTexture GL_TEXTURE_2D, TGLImageFrame(image.Frame(n)).name
		glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT
		glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT
	Until n < 1
End Function


Then it's just a matter of setting higher than 1.0 the UV of the DrawTexturedPoly function :)

EDIT: improved SetTextureRepeat to handle sequences frame TImage

In fact there is a DX version, a complete SetTextureRepeat and DrawTexturedPoly here :

http://blitzmax.com/Community/posts.php?topic=79234#889717

I don't know why but the second one corrupt my viewport with white Lightblend stuff.
So I choose your version of DrawTexturedPoly with the second SetTextureRepeat

I don't know why but the second one corrupt my viewport with white Lightblend stuff.
It does?

I can't seem to recreate the the problem here. Got any example code?

Hard to reproduce since it's inside the main code of my game but I will try. I probably miss a graphic specific setting somewhere.

I didn't see it at first glance but as my game use pixel art assets the Const scale#=1.28 obviously scale every pixels.
Just set it up to 1.0 and things are greats again :)