How can i draw a textured triangle in max2d?

BlitzMax Forums/BlitzMax Programming/How can i draw a textured triangle in max2d?

Max2D cannot draws textured triangles, so i think i need mix max2d with ogl. How can i do it without problems?

Well. You can't draw a textured triangle in Max2D. You can draw a textured Quad which is two triangles, using DrawImage.

This page has an introduction to using textures in OpenGL:

http://www.gamedev.net/reference/articles/article947.asp

actually you can if you don't mind going slightly low level.

glbegin(GL_TRIANGLES)
'el code'
glmultitexcoord3f unit,u,v,0
glvertex2f screenpixelx,screenpixely
gl'etc
glEnd()
-

i did this to create custom styles in a game i never finished(another one..) mark's 2d stuff sets up gl so the above is all you have to do gl wise.

you should then just have to look through max2d to find the bit of code mark uses to bind max2d images to the texture units and pre-cded the above with it.

Going to a lower level is not Max2D, it's direct OpenGL, which is different.

I do agree you can easily draw your own triangles with textures using direct OpenGL commands.

Thanks, I used direct OpenGL and I made this:
' Prueba para pintar triangulos con textura.
' <a href="http://www.blitzbasic.com/Community/posts.php?topic=48462" target="_blank">http://www.blitzbasic.com/Community/posts.php?topic=48462</a>

Strict


Local texturaGL	: Int
Local imagen	: TImage
Local trisList	: TList


SetGraphicsDriver(GLMax2DDriver())
Graphics(800,600,16,NOSYNC)

texturaGL% = bglTexFromPixmap%(LoadPixmap:TPixmap("max.png"))
imagen = LoadImage("max.png")
trisList = New TList
For Local cont1=1 To 50	'Adding triangles to triangle list
	Local tris	: TTriangle
	tris = New TTriangle

	tris.SetXY([RndFloat()*600.0,RndFloat()*600.0,RndFloat()*600.0,RndFloat()*600.0,RndFloat()*600.0,RndFloat()*600.0])
	tris.SetUV([RndFloat(),RndFloat(),RndFloat(),RndFloat(),RndFloat(),RndFloat()])
	tris.SetRGBA([RndFloat(),RndFloat(),RndFloat(),RndFloat(), RndFloat(),RndFloat(),RndFloat(),RndFloat(), RndFloat(),RndFloat(),RndFloat(),RndFloat()])
	tris.SetGLTexture(Rand(0,1)*texturaGL%)
	tris.SetBlendMode(ALPHABLEND)

	trisList.AddLast(tris:TTriangle)
Next


While ( Not KeyDown(KEY_ESCAPE) )	'Main
	Local time	: Int

	time% = MilliSecs()
	TTriangle.RenderTriangleList(trisList:TList)
	time% = MilliSecs()-time%
	
	DrawText("DrawText function test. RenderTriangleList time: " + String(time%), 10,10)
	DrawImage(imagen,MouseX(),MouseY())
	
	Flip()
	Cls()
	FlushMem()
Wend


Type TTriangle
	Field xy		: Float[6]
	Field uv		: Float[6]
	Field rgba		: Float[12]
	Field glTexture	: Int
	Field blendMode	: Int
	
	Function RenderTriangleList( triangleList:TList )
		Local tris	: TTriangle
		If (triangleList:TList)
			For tris:TTriangle = EachIn triangleList:TList
				If (tris.glTexture%)
					glBindTexture(GL_TEXTURE_2D,tris.glTexture%)
					glEnable(GL_TEXTURE_2D)
				EndIf
				SetBlend(tris.blendMode%)			
				glBegin(GL_TRIANGLES)
				 glTexCoord2f(tris.uv[0],tris.uv[1])	'coorner A
				 glcolor4f(tris.rgba[0],tris.rgba[1],tris.rgba[2],tris.rgba[3])
				 glVertex2f(tris.xy[0],tris.xy[1])
				 glTexCoord2f(tris.uv[2],tris.uv[3])	'coorner B
				 glcolor4f(tris.rgba[4],tris.rgba[5],tris.rgba[6],tris.rgba[7])
				 glVertex2f(tris.xy[2],tris.xy[3])
				 glTexCoord2f(tris.uv[4],tris.uv[5])	'coorner C
				 glcolor4f(tris.rgba[8],tris.rgba[9],tris.rgba[10],tris.rgba[11])
				 glVertex2f(tris.xy[4],tris.xy[5])
				glEnd()
			Next
			SetColor(255.0,255.0,255.0)
			SetAlpha(1.0)
		EndIf
	End Function
	
	Function RenderTriangle( tris:TTriangle )
		If (tris.glTexture%)
			glBindTexture(GL_TEXTURE_2D,tris.glTexture%)
			glEnable(GL_TEXTURE_2D)
		EndIf
		SetBlend(tris.blendMode%)			
		glBegin(GL_TRIANGLES)
		 glTexCoord2f(tris.uv[0],tris.uv[1])	'coorner A
		 glcolor4f(tris.rgba[0],tris.rgba[1],tris.rgba[2],tris.rgba[3])
		 glVertex2f(tris.xy[0],tris.xy[1])
		 glTexCoord2f(tris.uv[2],tris.uv[3])	'coorner B
		 glcolor4f(tris.rgba[4],tris.rgba[5],tris.rgba[6],tris.rgba[7])
		 glVertex2f(tris.xy[2],tris.xy[3])
		 glTexCoord2f(tris.uv[4],tris.uv[5])	'coorner C
		 glcolor4f(tris.rgba[8],tris.rgba[9],tris.rgba[10],tris.rgba[11])
		 glVertex2f(tris.xy[4],tris.xy[5])
		glEnd()
		SetColor(255.0,255.0,255.0)
		SetAlpha(1.0)
	End Function

	Method Render()
		If (glTexture%)
			glBindTexture(GL_TEXTURE_2D,glTexture%)
			glEnable(GL_TEXTURE_2D)
		EndIf
		SetBlend(blendMode%)			
		glBegin(GL_TRIANGLES)
		 glTexCoord2f(uv[0],uv[1])	'coorner A
		 glcolor4f(rgba[0],rgba[1],rgba[2],rgba[3])
		 glVertex2f(xy[0],xy[1])	
		 glTexCoord2f(uv[2],uv[3])	'coorner B
		 glcolor4f(rgba[4],rgba[5],rgba[6],rgba[7])
		 glVertex2f(xy[2],xy[3])
		 glTexCoord2f(uv[4],uv[5])	'coorner C
		 glcolor4f(rgba[8],rgba[9],rgba[10],rgba[11])
		 glVertex2f(xy[4],xy[5])
		glEnd()
		SetColor(255.0,255.0,255.0)
		SetAlpha(1.0)
	End Method

	Method SetXY( newXY:Float[] )
		If (newXY.length=6) MemCopy(xy,newXY,24)
	End Method

	Method SetUV( newUV:Float[] )
		If (newUV.length=6) MemCopy(uv,newUV,24)
	End Method

	Method SetRGBA( newRGBA:Float[] )
		If (newRGBA.length=12) MemCopy(rgba,newRGBA,48)
	End Method
	
	Method SetGLTexture( newGLTexture:Int )
		glTexture% = newGLTexture%
	End Method
	
	Method SetBlendMode( newBlendMode:Int )
		blendMode% = newBlendMode%
	End Method
End Type


Can someone say the essential changes for Blitz Max 1.6
EDIT: The bGLTexFromPixmap has changed to GLTexFromPixmap
And the FlushMem is dead (Delete the line).

Edit2: A simpler example
Edit3: The codeBox is gone.

You need my textured poly module :P

I thing that this is better.
I replaced the
glBindTexture(GL_TEXTURE_2D,tris.glTexture%)
and the glEnable(GL_TEXTURE_2D)
with the
EnableTex GLTexture
and I created a Function called DrawTexQuad
Edit1: I created the GLTexture from an TImage and now the MASKBLEND works for the textured QUAD
pls try this :
SuperStrict

SetGraphicsDriver GLMax2DDriver()

Graphics 640 , 480
Local filter$="Image Files:png,jpg,bmp;Text Files:txt;All Files:*"
Local filename$ = RequestFile( "Select graphic file to open" , filter$ )

If filename = Null Then End

Local image:TImage = LoadImage( filename)

Local GLTexture:Int = GLTexFromPixmap( Image.Pixmaps[0] )

Local x0:Float = 50.0 , y0:Float = 50.0
Local x1:Float =350.0 , y1:Float = 70.0
Local x2:Float = 640.0 , y2:Float = 480.0
Local x3:Float = 0.0 , y3:Float = 300.0

SetClsColor 150 , 150 , 150
While Not KeyDown(KEY_ESCAPE)
	Cls
	If KeyDown(KEY_1) Then x0 = MouseX() ; y0 = MouseY()
	If KeyDown(KEY_2) Then x1 = MouseX() ; y1 = MouseY()
	If KeyDown(KEY_3) Then x2 = MouseX() ; y2 = MouseY()
	If KeyDown(KEY_4) Then x3 = MouseX() ; y3 = MouseY()			
	
	DrawText " Something " , 10 , 10
	DrawImage image , 10 , 30
	DrawTexQuad( GLTexture , x0 , y0 , x1 , y1 , x2 , y2 , x3 , y3 )

	
	Flip
Wend
End

Function DrawTexQuad( GLTexture:Int, x0:Float , y0:Float , x1:Float , y1:Float , x2:Float , y2:Float , x3:Float , y3:Float)
	
	EnableTex GLTexture

	glBegin GL_QUADS
		glTexCoord2f 0.0 , 0.0
		glVertex2f x0 , y0
		glTexCoord2f 1.0 , 0.0
		glVertex2f x1 , y1		
		glTexCoord2f 1.0 , 1.0
		glVertex2f x2 , y2	
		glTexCoord2f 0.0 , 1.0
		glVertex2f x3 , y3
	glEnd
	
End Function

I think this is 100% stable. If anyone finds a problem pls post.