Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Augmented textures with PyOpenGL

  1. #1
    Newbie Newbie
    Join Date
    May 2013
    Posts
    2

    Augmented textures with PyOpenGL

    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.

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    754
    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 for additional ideas how to ask questions that are more likely to get a useful answer.
    Last edited by carsten neumann; 05-29-2013 at 04:30 PM. Reason: add link to forum guidelines

  3. #3
    Newbie Newbie
    Join Date
    May 2013
    Posts
    2
    Sorry I'm new to the forum but here is a simplified version:

    For opening the image:
    Code :
    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:
    Code :
        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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •