"Texture" for DrawPoly/DrawRect, etc

BlitzMax Forums/BlitzMax Beginners Area/"Texture" for DrawPoly/DrawRect, etc

SoggyP(Posted 2005) [#1]
Hello.

Is there any way of using a background texture instead of a solid colour when using DrawPoly, or should I be going the OpenGL route?

Ta.

Goodbye.


tonyg(Posted 2005) [#2]
Hi SoggyP,
I'm trying to do something very similar.
The GL_TRIANGLE_STRIP and GL_TRIANGLE_FAN OGL commands look useful (thanks Dreamora)
I've butchered the Nehe tutorial 6 to texture a 4 sided poly using GL_TRIANGLE_STRIP.
I'm having trouble converting OGL type co-ords to 2D co-ords.
This is what I have done so far...
Replace the glBegin GL_QUADS section in the Nehe6() function with...
glBegin GL_TRIANGLE_STRIP
	glTexCoord2d(1,1); glVertex3f(x+0.5,y+0.5,z)					' Top Right
	glTexCoord2d(0,1); glVertex3f(x-0.5,y+0.5,z)					' Top Left
	glTexCoord2d(1,0); glVertex3f(x+0.5,y-0.5,z)					' Bottom Right
	glTexCoord2d(0,0);  glVertex3f(x-0.5,y-0.5,z)					' Bottom Left
    glEnd

Not really sure what I'm doing yet but it's a start.
<edit> If there's a nice simple way to texture a DrawPoly created shape it would be ideal.


ImaginaryHuman(Posted 2005) [#3]
You can just use a GL_QUAD instead of a GL_TRIANGLE_STRIP to draw a rectangle.

You are right in that you need to define the texture coordinates

You also need to select which texture is being used with glBindTexture()

You may also need to switch texturing on.


tonyg(Posted 2005) [#4]
Hi AngelDaniel,
That was a simple example. In fact, I'll try with GL_POLYGON but the manuals I have don't say what the difference is (unless GL_POLYGON can only do convex shapes).


ImaginaryHuman(Posted 2005) [#5]
I'm not too sure what polygon is without looking back at the docs. Triangles have 3 points, quads have 4. Triangle strips are strips of joined triangles with shared points, quad strips are strips of joined quads with shared points (which might be converted to triangles internally). There is also the triangle fan. Not sure on the polygon.


SoggyP(Posted 2005) [#6]
Hello.

Have you got a basic example of how to do this?

Goodbye.


tonyg(Posted 2005) [#7]
Global ScreenWidth:Int=800
Global ScreenHeight:Int=600
Global ScreenDepth:Int=32

Global xrot:Float=0
Global yrot:Float=0
Global zrot:Float=0

Global Checkimage:Byte[256,256,4]
Global Texname:Int

bglCreateContext(ScreenWidth,ScreenHeight,ScreenDepth,0,BGL_BACKBUFFER | BGL_DEPTHBUFFER) '| BGL_FULLSCREEN
InitGl()
While Not KeyHit( KEY_ESCAPE )
	nehe6()
	glColor3f(1.0,1.0,1.0)
	'Framecounter--------------------------------------------
	Framecounter_counter=Framecounter_counter+1
	If Framecounter_time=0 Then Framecounter_time=MilliSecs()
	If Framecounter_time+1001 <MilliSecs() Then
		Framecounter_framerate=Framecounter_counter
		Framecounter_counter=0
		Framecounter_time=MilliSecs()
	EndIf
	bglDrawText("FPS : "+Framecounter_framerate,ScreenWidth-(8*12),ScreenHeight-16-8)
	'--------------------------------------------------------
	bglDrawText("Nehe lesson 6",10,24)
	FlushMem
	bglSwapBuffers
Wend
End

Function InitGl()
	LoadGlTextures()
	glClearColor(0.0, 0.0, 0.0, 0.0)
	glClearDepth 1.0
	glDepthFunc(GL_LESS)
	glEnable(GL_DEPTH_TEST)
	glShadeModel(GL_SMOOTH)
	glViewport(0,0,ScreenWidth,ScreenHeight)
	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()
	gluPerspective(45.0,Float(ScreenWidth)/Float(ScreenHeight),1.0,100.0)
	glMatrixMode(GL_MODELVIEW)
End Function

Function LoadGlTextures()
	Local PointeurImg:Byte Ptr
	Local TexWidth
	Local TexHeight
	tex01:TPixmap=LoadPixmap("data/logo.bmp")
	TexWidth=tex01.Width
	TexHeight=tex01.Height
	PointeurImg=PixmapPixelPtr(tex01,0,0)
	pp=0
	For y=TexHeight-1 To 0 Step -1
		For x=0 To TexWidth-1
			Checkimage[y,x,0]=PointeurImg[pp]
			Checkimage[y,x,1]=PointeurImg[pp+1]
			Checkimage[y,x,2]=PointeurImg[pp+2]
			Checkimage[y,x,3]=100
			pp=pp+3
		Next
	Next
	tex01=Null
	glPixelStorei(GL_UNPACK_ALIGNMENT,1)
	glGenTextures(1, Varptr Texname)
	glBindTexture(GL_TEXTURE_2D, Texname)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TexWidth, TexHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, Checkimage)
End Function

Function nehe6()
	glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
   	glEnable(GL_TEXTURE_2D)
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
	glBindTexture GL_TEXTURE_2D,Texname
	
	glLoadIdentity
	glTranslatef 0.0,0.0,-5.0
	glRotatef xrot,1.0,0.0,0.0                         ' Rotate On The X Axis
	glRotatef yrot,0.0,1.0,0.0                         ' Rotate On The Y Axis
	glRotatef zrot,0.0,0.0,1.0                         ' Rotate On The Z Axis

	glBegin(GL_TRIANGLE_STRIP)										' Build Quad From A Triangle Strip
		glTexCoord2d(1,1); glVertex3f(x+0.5,y+0.5,z)					' Top Right
		glTexCoord2d(0,1); glVertex3f(x-0.5,y+0.5,z)					' Top Left
		glTexCoord2d(1,0); glVertex3f(x+0.5,y-0.5,z)					' Bottom Right
		glTexCoord2d(0,0); glVertex3f(x-0.5,y-0.5,z)					' Bottom Left
	glEnd()														' Done Building Triangle Strip

	xrot = xrot + 0.003                     ' X Axis Rotation
	yrot = yrot + 0.002                     ' Y Axis Rotation
	zrot = zrot + 0.004                     ' Z Axis Rotation
	glDisable(GL_TEXTURE_2D)
End Function

This is the butchered test I did from Nehe tutorials.
The .bmp has to be 256*256.


SoggyP(Posted 2005) [#8]
Hello.

@Tonyg: Thanks very much for that. Quick question (before I start asking long-winded, complex ones, should the glXXX commands become highlighted in the Standard IDE?

Goodbye.


tonyg(Posted 2005) [#9]
Nope, only the bgl ones.
My next step is to check gluOrtho2d to use 2D co-ordinates and then create a real 'poly' to use with either GL_POLYGON in that example or a better GL_TRIANGLE_STRIP. Once (if) it's working then check whether it works with bmx graphics mode rather than GL context.
Seems a shame we can't just say my+poly=drawpoly(array)and then 'texture mypoly,myimage' but that's the result I'm going for.


tonyg(Posted 2005) [#10]
I've got a horrible feeling I'm going about this the wrong way.
The brl.glmax2d module shows that Drawpoly uses GL_POLYGON.
I'm assuming that OGL can texture poly created using GL_POLYGON so, in theory, shouldn't it be able to texture a poly created using Drawpoly?
BMAX has a bglCreateFromPixmap command and, within glmax2d, there are functions such as bindtex/enabletex
and createtex function.
Now, the bit where I'm banging my head against the wall.
I've read THIS
which suggests I can't use BindTex. However, I can assign a texture taken from a pixmap to an image.
How would I do the same assign but to a Drawpoly?


Perturbatio(Posted 2005) [#11]
I managed it in very few lines :) (but it's no good for realtime unless you simply want the texture to remain the same)




tonyg(Posted 2005) [#12]
Hi Perturbatio,
Thanks for the response.
This is what I'm after (soggyp, sorry if it answers your query... I'll open another post if needed).
SetGraphicsDriver GLMax2DDriver()
Graphics 640,480,0

img1:TImage = LoadImage("beach1.png")   '64*64
img2:TPixmap = LoadPixmap("bmax160.png") '160*160
texture_name:Int = bglTexFromPixmap(img2)

Cls

DrawImage img1,100,100
DrawPixmap img2,100,300
glBindTexture GL_TEXTURE_2D, texture_name
DrawImage img1,300,100

Flip
WaitKey

The texture is resized to fit the new image.


SoggyP(Posted 2005) [#13]
Hello.

At the moment I'm not sure, I'll have to check, though I think I was initially after what the Pert one has posted though if there's a way of stretching to fit, etc, that might be better.

I'll try when I get home later.

Ta.

Goodbye.