旋转OpenGL纹理python
Posted
技术标签:
【中文标题】旋转OpenGL纹理python【英文标题】:Rotating OpenGL texture python 【发布时间】:2019-06-30 10:16:14 【问题描述】:我在代码中添加了纹理。但纹理出现在 (x,y,0) 轴上 当我希望它成为 (x,0,z) [像地面纹理] 时,我尝试了很多方法来将其旋转为平坦,但我无法在任何地方更改它。
这就是我添加纹理的方式,当我希望它成为旗地时,它似乎只在 x、y 轴上。
def MTL(filename):
contents =
mtl = None
for line in open(filename, "r"):
if line.startswith('#'): continue
values = line.split()
if not values: continue
if values[0] == 'newmtl':
mtl = contents[values[1]] =
elif mtl is None:
raise ValueError, "mtl file doesn't start with newmtl stmt"
elif values[0] == 'map_Kd':
# load the texture referred to by this declaration
mtl[values[0]] = values[1]
surf = pygame.image.load(mtl['map_Kd'])
image = pygame.image.tostring(surf, 'RGBA', 1)
ix, iy = surf.get_rect().size
texid = mtl['texture_Kd'] = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texid)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA,
GL_UNSIGNED_BYTE, image)
else:
mtl[values[0]] = map(float, values[1:])
return contents
class OBJ:
def __init__(self, filename, swapyz=False, stop_loop = False):
"""Loads a Wavefront OBJ file. """
self.vertices = []
self.normals = []
self.texcoords = []
self.faces = []
self.stop = stop_loop
material = None
for line in open(filename, "r"):
if line.startswith('#'): continue
values = line.split()
if not values: continue
if values[0] == 'v':
v = map(float, values[1:4])
if swapyz:
v = v[0], v[2], v[1]
self.vertices.append(v)
elif values[0] == 'vn':
v = map(float, values[1:4])
if swapyz:
v = v[0], v[2], v[1]
self.normals.append(v)
elif values[0] == 'vt':
self.texcoords.append(map(float, values[1:3]))
elif values[0] in ('usemtl', 'usemat'):
material = values[1]
elif values[0] == 'mtllib':
self.mtl = MTL(values[1])
elif values[0] == 'f':
face = []
texcoords = []
norms = []
for v in values[1:]:
w = v.split('/')
face.append(int(w[0]))
if len(w) >= 2 and len(w[1]) > 0:
texcoords.append(int(w[1]))
else:
texcoords.append(0)
if len(w) >= 3 and len(w[2]) > 0:
norms.append(int(w[2]))
else:
norms.append(0)
self.faces.append((face, norms, texcoords, material))
self.gl_list = glGenLists(1)
glNewList(self.gl_list, GL_COMPILE)
glEnable(GL_TEXTURE_2D)
glFrontFace(GL_CCW)
for face in self.faces:
vertices, normals, texture_coords, material = face
mtl = self.mtl[material]
if 'texture_Kd' in mtl:
# use diffuse texmap
glBindTexture(GL_TEXTURE_2D, mtl['texture_Kd'])
else:
# just use diffuse colour
glColor(*mtl['Kd'])
glBegin(GL_POLYGON)
for i in range(len(vertices)):
if normals[i] > 0:
glNormal3fv(self.normals[normals[i] - 1])
if texture_coords[i] > 0:
glTexCoord2fv(self.texcoords[texture_coords[i] - 1])
glVertex3fv(self.vertices[vertices[i] - 1])
glEnd()
glDisable(GL_TEXTURE_2D)
glEndList()
main -> while True:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glCallList(obj.gl_list)
DrawBuffer(bufferObj, noPoints, noCirclePoints)
pygame.display.flip()
pygame.time.wait(10)
我尝试添加
glMatrixMode(GL_TEXTURE)
glLoadIdentity()
glTranslatef(0.5, 0.5, 0)
glRotatef(90, 0, 0, 1)
但它不会将其更改为地面。 任何想法都会很棒
【问题讨论】:
【参考方案1】:已修复! 我所要做的就是在
之间切换 if values[0] == 'v':
v = map(float, values[1:4])
if swapyz:
v = v[0], v[1], v[2] # <------------- HERE (WAS v[0], v[2], v[1])
self.vertices.append(v)
elif values[0] == 'vn':
v = map(float, values[1:4])
if swapyz:
v = v[0], v[1], v[2] # <------------- HERE (WAS v[0], v[2], v[1])
【讨论】:
以上是关于旋转OpenGL纹理python的主要内容,如果未能解决你的问题,请参考以下文章
openGL三维网格坐标,旋转,缩放,灯光设置,纹理读取,模型读取(MFC单文档)