[OT] Texturing an indexed triangle mesh in OpenGL

Bengt Richter bokr at oz.net
Mon May 12 01:14:26 EDT 2003


On 11 May 2003 23:16:43 GMT, thedustbustr at aol.com (TheDustbustr) wrote:

>My renderer is written in python, and thats about all this has to do with this
>newsgroup, hence the [OT]
>
>This one has been bugging me forever, and nobody seems to know the answer (not
>even google!)  Once I have created a triangle mesh, what is the best way to
>texture it (with potentially different textures for each triangle)?  My current
>method, when used with a medium sized mesh (8000 triangles), yeilds me 40
>frames per second (I have a Geforce 4)- I should be getting about 200.
>
>        glVertexPointerf(VertexArray)
>        prevtexture=""
>        for ind_i in range(0, len(IndiceArray), 3):
>            if (VertexArray[int(IndiceArray[ind_i])][1] <= 0) and (prevtexture
>!= "data/water-0.bmp"):
>                glBindTexture(GL_TEXTURE_2D,
>ResourceMgr().get("data/water-0.bmp"))
>                prevtexture="data/water-0.bmp"
>            elif (VertexArray[int(IndiceArray[ind_i])][1] > 3 and
>VertexArray[int(IndiceArray[ind_i])][1] <= 10) and (prevtexture !=
>"data/grass-0.bmp"):
>                glBindTexture(GL_TEXTURE_2D,
>ResourceMgr().get("data/grass-0.bmp"))
>                prevtexture="data/grass-0.bmp"
>            elif (VertexArray[int(IndiceArray[ind_i])][1] > 0 and
>VertexArray[int(IndiceArray[ind_i])][1] <= 3) and (prevtexture !=
>"data/dirt-0.bmp"):
>                glBindTexture(GL_TEXTURE_2D,
>ResourceMgr().get("data/dirt-0.bmp"))
>                prevtexture="data/dirt-0.bmp"
>
>            glBegin(GL_TRIANGLES)
>            glArrayElement(IndiceArray[ind_i])
>            glArrayElement(IndiceArray[ind_i+1])
>            glArrayElement(IndiceArray[ind_i+2])
>            glEnd()
>
>IndiceArray is sorted by texture, so glBindTexture should only be called once
>per texture per frame (in this case, 3 times per frame).  Is there a better way
>to do this?
>
>If I render the terrain with just one tiled texture, I get roughly 120 frames
>per second.

It looks like you could evaluate the test term once as Andrew suggested,
and also hoist a few things clean out of your loop, e.g., (untested!)

        glVertexPointerf(VertexArray)
        prevtexture = None
        water = ResourceMgr().get("data/water-0.bmp")
        grass = ResourceMgr().get("data/grass-0.bmp") 
        dirt = ResourceMgr().get("data/dirt-0.bmp")
        for ind_i in range(0, len(IndiceArray), 3):
            term = VertexArray[int(IndiceArray[ind_i])][1] # ?? looks improvable ;-)
            if term <= 0 and prevtexture is not water:
                glBindTexture(GL_TEXTURE_2D, water)
                prevtexture = water
            elif term > 3 and term <= 10 and prevtexture is not grass:
                glBindTexture(GL_TEXTURE_2D, grass)
                prevtexture = grass
            elif term > 0 and term <= 3 and prevtexture is not dirt:
                glBindTexture(GL_TEXTURE_2D, dirt)
                prevtexture = dirt

            glBegin(GL_TRIANGLES)
            glArrayElement(IndiceArray[ind_i])
            glArrayElement(IndiceArray[ind_i+1])
            glArrayElement(IndiceArray[ind_i+2])
            glEnd()

If the value of term occurs in sorted order, you could probably rewrite the if/elif block
to be a few microseconds faster, but I'd look into how to improve on the
            term = VertexArray[int(IndiceArray[ind_i])][1]
calculation first. How often do VertexArray and/or IndiceArray change compared to how often you
execute the for loop? What is it actually doing? What can be done when the data is generated?

BTW, what happens when  term > 10? Does it stay grass?

Regards,
Bengt Richter




More information about the Python-list mailing list