Textured Poly > 4 sides.

BlitzMax Forums/OpenGL Module/Textured Poly > 4 sides.

tonyg(Posted 2005) [#1]
I have the following code...
SetGraphicsDriver GLMax2DDriver()
Graphics 800,600,0

Global w,h
Global tex=LoadTexture("max.png")

glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR
glLoadIdentity
glEnable GL_TEXTURE_2D

While Not KeyHit(KEY_ESCAPE)
	mx=MouseX() ; my=MouseY()
	Cls
	glLoadIdentity
    glBindTexture GL_TEXTURE_2D,tex
	' quad

	glBegin GL_POLYGON
	 glTexCoord2f 0.0, 0.0 ; glVertex2f mx , my
	 glTexCoord2f 1.0, 0.0 ; glVertex2f mx+w , my
	 glTexCoord2f 1.0, 1.0 ; glVertex2f mx+w , my+h
	 glTexCoord2f 0.0, 1.0 ; glVertex2f mx , my+h
	glEnd

	FlushMem
	Flip
	Delay 10
Wend

End

Function LoadTexture(f$)
	Local pm:TPixmap=LoadPixmap(f$)
	w=pm.width ; h=pm.height
	Local texname=bglTexFromPixmap(pm)
	Return texname
End Function

cobbled together from various examples in these forums (so thanks to the people who posted them)
How would I draw the Polygon with more than 4 sides as I don't understand what I would o with the gltexcoord2f statements.
Thanks in advance
<edit> It's OK, I've got it. You can define as many glvertex2f commands but the gltexcoord2f will be associated with the next one... seemingly.


ImaginaryHuman(Posted 2005) [#2]
Yes, the texture coordinates that are defined with the most recent call to glTexCoord will `tie` that pixel of the texture to the corner point that you define with the next glVertex. If that causes the texture to stretch to fit the shape, so be it. Presumably you want it unstretched though. You just have to work out the coordinate in the texture image - which should be the same as the coordinate of the vertex more or less.


tonyg(Posted 2005) [#3]
Yep, that seems to be going well.
Just happy to have a texture poly using graphics, cls and flip.
How would I unbind the texture? The OGL manual suggests glDeleteTextures but I'll want to use it each loop but with a drawimage in between.


REDi(Posted 2005) [#4]
I think you have to disable texturing to unbind


ImaginaryHuman(Posted 2005) [#5]
DrawImage will need to have texturing enabled and I'm not sure if it enabled it when it created the context or with each call to that function. Just let DrawImage() bind the texture by itself, if it does that. Then bind yours again later.


tonyg(Posted 2005) [#6]
Unfortunately, drawimage doesn't bind the texture so both the drawimage and polygon end up with the texture intended just for the polygon.
glDisable GL_TEXTURE_2D results in white (no textures) images and, if you glEnable it will bind the poly texture again (in this case #1).
Everything I have found suggests you either...
a) glDeleteTextures : But this does exactly that resulting in a white drawimage as it is never rebound.
b) glbind GL_TEXTURE_2D,0 to rebind to the default texture... white again.
I think the glbind can only REALLY be overridden with a glbind to another existing texture. To do this you need to know your texture number.... which I can't find a way to get from Bmax.


tonyg(Posted 2005) [#7]
I read this yesterday but didn't know enough to understand it. Basically, without knowing the drawimage texture number/name, I can't rebind that texture... poo!!
<Edit> Mark, if you see this how about a command to texture a poly,oval,rect etc or allow us to get the texture number for images?


REDi(Posted 2005) [#8]
Not sure but glPushAttrib and glPopAttrib might be able to help, I havnt got time to check atm.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glfunc03_68iq.asp


REDi(Posted 2005) [#9]
Wait a minute, (not tested)if you draw another different image before you draw the polygon it should work fine, because blitz will be switching between the two textures.


tonyg(Posted 2005) [#10]
...afraid not...
SetGraphicsDriver GLMax2DDriver()
Graphics 800,600,0

Global w,h
Global tex=LoadTexture("my_shadow.png")
Global my_image = LoadImage("max.png")
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR
glLoadIdentity
glEnable GL_TEXTURE_2D

While Not KeyHit(KEY_ESCAPE)
	Cls
	mx=MouseX() ; my=MouseY()
	DrawImage my_image,600,400

	glLoadIdentity
	
    glBindTexture GL_TEXTURE_2D,tex
	glBegin GL_POLYGON
	 glTexCoord2f 0.0, 0.0 ; glVertex2f mx , my
	 glTexCoord2f 1.0, 0.0 ; glVertex2f mx+w , my
	 glTexCoord2f 1.0, 1.0 ; glVertex2f mx+w , my+h
	 glTexCoord2f 0.0, 1.0 ; glVertex2f mx , my+h
	glEnd
 '   glDeleteTextures(1,Varptr tex)
'    glbindtexture GL_TEXTURE_2D,0
	FlushMem
	Flip
Wend

End

Function LoadTexture(f$)
	Local pm:TPixmap=LoadPixmap(f$)
	w=pm.width ; h=pm.height
	Local texname=bglTexFromPixmap(pm)
	Return texname
End Function


The bind is in effect for the next draw operation unless I either unbind by binding to another texture (haven't got the bind number for the drawimage) or deletetextures which delete all textures or my texture '1' resulting in texture '0' being used again.


REDi(Posted 2005) [#11]
Sorry I didn't explain very well, try one DrawImage before you bind your texture and then a different DrawImage afterwards (different texture).

Because I think this will only be a problem if blitz is trying to draw a texture that it thinks is already bound, so if your using at least two different textures either side of your bind it should be okay. (or even two different DrawImages after will also work).


tonyg(Posted 2005) [#12]
Thanks Papa Lazarou.
That's good thinking and works perfectly.


REDi(Posted 2005) [#13]
np :)

I will say that hacking between Max2D to OpenGL is a bad idea, and you will come across many more problems like this one.

It would be much better to use bglCreateContext instead of Graphics, and write your own functions for drawimage ect, after all its basically what you've done above just put in a function.

Just my 2 cents.


tonyg(Posted 2005) [#14]
I read that in another thread as well.
I don't really want to spend too much time 'programming'. Texturing a poly is all I really need at the moment.
Thanks for your (and everybody elses) help.


SoggyP(Posted 2005) [#15]
Hello.

@tonyg: I'm thinking that this is what I was after previously, yes? Texturing the drawpoly command (or at least similar)

Goodbye.


tonyg(Posted 2005) [#16]
Yep, I think it is.
The texture is scaled to fit the poly.
I still need to map the texture to the poly using the
gltextcoord2f to glvertex2f but that should be OK.
I don't need my poly rotated or scaled but that should be doable.
Another thing is to save the poly away somewhere so it can be access without recreating each time.
<edit> or Mark could add a TextureShape command ;)


SoggyP(Posted 2005) [#17]
Hello.

Excellent, I'll give that a try right away.

D'ya know what - I think perhaps Mark should add a TextureShape command. What do you think?

:0)

Goodbye.