PDA

View Full Version : Augmented textures with PyOpenGL



JMGloyens
05-29-2013, 03:17 PM
I've just started using textures with OpenGL for Python and have a problem. When I render textures where the corners aren't perpendicular the texture seems to split in half and each half is augmented differently. Unfortunately I can't link a picture to help.

The code for loading a texture is below:
imagefilegl1 = Image.open("hlt1.png")
xsize, ysize, image = imagefilegl1.size[0], imagefilegl1.size[1], imagefilegl1.tostring("raw", "RGBX", 0, -1)
textureone = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, textureone)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexImage2D(
GL_TEXTURE_2D, 0, 3, xsize, ysize, 0,
GL_RGBA, GL_UNSIGNED_BYTE, image
)

And for rendering:
glClear(GL_COLOR_BUFFER_BIT)
if textureflag == 1:
count = 0
while count < quadnum:
if pz[texture[count][0]] < look and pz[texture[count][1]] < look and pz[texture[count][2]] < look and pz[texture[count][3]] < look:
coltemp = colour[count]
textemp = texture[count]
a = textemp[0]
b = textemp[1]
c = textemp[2]
d = textemp[3]
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, colour[count])
glBegin(GL_QUADS)
glTexCoord2f(0.0, 0.0); glVertex2f(px[a], py[a]);
glTexCoord2f(0.0, 1.0); glVertex2f(px[b], py[b]);
glTexCoord2f(1.0, 1.0); glVertex2f(px[c], py[c]);
glTexCoord2f(1.0, 0.0); glVertex2f(px[d], py[d]);
glEnd()
glDisable(GL_TEXTURE_2D)
count += 1
glFlush()

Any help is much appreciated.

carsten neumann
05-29-2013, 04:27 PM
Please use [ code]/[ /code] tags around source code (especially python code where the whitespace is critical ;) ). Also, can you perhaps simplify your code (or comment it). In particular the multi level array lookups with pz, px are a bit hard to digest without explanation. See the Forum Posting Guidelines (http://www.opengl.org/discussion_boards/showthread.php/176139-Forum-Posting-Guidelines) for additional ideas how to ask questions that are more likely to get a useful answer.

JMGloyens
05-29-2013, 04:41 PM
Sorry I'm new to the forum but here is a simplified version:

For opening the image:


imagefilegl1 = Image.open("hlt1.jpg")
xsize, ysize, image = imagefilegl1.size[0], imagefilegl1.size[1], imagefilegl1.tostring("raw", "RGBX", 0, -1)
textureone = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, textureone)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexImage2D(
GL_TEXTURE_2D, 0, 3, xsize, ysize, 0,
GL_RGBA, GL_UNSIGNED_BYTE, image
)


For rendering the texture:


glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, colour[count])
glBegin(GL_QUADS)
glTexCoord2f(0.0, 0.0); glVertex2f(px[a], py[a]);
glTexCoord2f(0.0, 1.0); glVertex2f(px[b], py[b]);
glTexCoord2f(1.0, 1.0); glVertex2f(px[c], py[c]);
glTexCoord2f(1.0, 0.0); glVertex2f(px[d], py[d]);
glEnd()
glDisable(GL_TEXTURE_2D)


The px and py arrays stand for "perspective x" and "perspective y" as there is a calculation before hand that works out where the point should be based on it's z coordinate. Hope this helps.