Search code examples
pythonopengltexturestexture2dpyopengl

Terrain Texture in PyOpenGL not Rendered


I need help for this code. I want to render a grass texture using file "grass.bmp" which I already have. This is the code for load image.

texsurfGrass = pygame.image.load('grass.bmp')
imageGrass = pygame.image.tostring(texsurfGrass, "RGB", 1)
texID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D,texID)

This is the code in drawing mode (Draw texture and grid for the floor)

# set drawing mode
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # POINT, LINE, FILL
    glPushMatrix()
    glTranslate(-ground_gridsize/2,ground_gridsize/2,0)
    glTexImage2D(GL_TEXTURE_2D, 0, 3, texsurfGrass.get_width(), texsurfGrass.get_height(), 0, GL_RGB, GL_UNSIGNED_BYTE, imageGrass)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # GL_LINEAR
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glColor3f(0.0, 1.0, 0.0)
    for i in range(-ground_size/2, (ground_size/2)+ground_gridsize, ground_gridsize):
        for j in range (-ground_size/2, (ground_size/2)+ground_gridsize, ground_gridsize):
            glPushMatrix()
            glTranslate(i,j,0)
            glEnable(GL_TEXTURE_2D)
            glBindTexture(GL_TEXTURE_2D,texID)
            glBegin(GL_QUADS)
            glColor3f(0.0, 0.5, 0.0)
            glVertex3f(0, 0, 0)
            glVertex3f(ground_gridsize, 0, 0)
            glVertex3f(ground_gridsize, -ground_gridsize, 0)
            glVertex3f(0, -ground_gridsize, 0)
            glEnd()
            glDisable(GL_TEXTURE_2D)
            glPopMatrix()
    glPopMatrix()

Those codes are still produce grid floor instead of texture-rendered floor. Please help me to show the rendered floor. Thanks in advance.


Solution

  • I see a couple of problems. First the glPolygonMode() call is asking for wireframe (you're telling it you want wireframes, you probably want fill, which is the default).

    Second, you'll eventually have to put in some texture coordinates somehow. It looks like you are programming OpenGL 1.0 or 2.0, so you can look at glTexCoord2f or the related TexCoord functions.