[英]Terrain Texture in PyOpenGL not Rendered
我需要此代碼的幫助。 我想使用已有的文件“ 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()
這些代碼仍然產生網格地板,而不是紋理渲染地板。 請幫助我展示渲染的地板。 提前致謝。
我看到幾個問題。 首先,glPolygonMode()調用要求線框(您告訴它要線框,您可能想要填充,這是默認設置)。
其次,您最終將不得不以某種方式放入一些紋理坐標。 看起來您正在編程OpenGL 1.0或2.0,因此可以查看glTexCoord2f或相關的TexCoord函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.