PyOpenGL中的地形纹理未渲染

时间:2015-11-26 18:43:02

标签: python opengl textures texture2d pyopengl

我需要这段代码的帮助。 我想使用我已经拥有的文件“grass.bmp”渲染草纹理。 这是加载图像的代码。

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

这是绘图模式中的代码(绘制地板的纹理和网格)

# 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()

这些代码仍然是生成网格地板而不是纹理渲染地板。 请帮我展示渲染的地板。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我看到了几个问题。首先glPolygonMode()调用要求线框(你告诉它你想要线框,你可能想要填充,这是默认值)。

其次,你最终必须以某种方式放入一些纹理坐标。看起来您正在编写OpenGL 1.0或2.0,因此您可以查看glTexCoord2f或相关的TexCoord函数。

相关问题