def draw_textured_rect_subtriangle( origin, dx, dy, tex_origin, tex_dx, tex_dy, points ): # 070404 modified from draw_textured_rect """ Like draw_textured_rect, but draw only the sub-triangle of the same rect (textured in the same way), where the subtriangle has relative 2d vertices as specified inside that rect (treating its own coords as each in [0.0, 1.0]). WARNING: depending on the glEnables set up by the caller, the sub-triangle coords might need to be in CCW winding order for the triangle to be visible from the front. """ # e could easily generalize API to polygon, and this implem to convex polygon, if desired ##e WARNING: this function's name and api are likely to be revised; # or we might just replace the whole scheme, using things like Textured(Triangle(...),...) instead, # perhaps implemented by telling OpenGL how to compute the texture coords in a wrapper, then just drawing the triangle. assert len(points) == 3 # and each point should be a V of length 2, or a 2-tuple, with elements convertible to floats -- this is assumed below glEnable(GL_TEXTURE_2D) glBegin(GL_TRIANGLES) for px, py in points: px = float(px) py = float(py) glTexCoord2fv((tex_origin + px * tex_dx + py * tex_dy).tolist()) # glVertex3fv(origin + px * dx + py * dy) glEnd() glDisable(GL_TEXTURE_2D)
def __gen_gl_code(faces, mtls, normals, vertices, texcoords): gl_list = glGenLists(1) glNewList(gl_list, GL_COMPILE) glEnable(GL_TEXTURE_2D) glFrontFace(GL_CCW) for face in faces: face_vertices, face_normals, texture_coords, material = face try: mtl = mtls[material] if 'texture_Kd' in mtl: # use diffuse texmap glBindTexture(GL_TEXTURE_2D, mtl['texture_Kd']) else: # just use diffuse colour glColor(mtl['Kd']) except KeyError: print("Key error" + str(material)) glColor((1, 1, 1)) glBegin(GL_POLYGON) for i in range(len(face_vertices)): if face_normals[i] > 0: glNormal3fv(normals[face_normals[i] - 1]) if texture_coords[i] > 0: glTexCoord2fv(texcoords[texture_coords[i] - 1]) glVertex3fv(vertices[face_vertices[i] - 1]) glEnd() glDisable(GL_TEXTURE_2D) glEndList() return gl_list
def draw_textured_rect_subtriangle( origin, dx, dy, tex_origin, tex_dx, tex_dy, points): #070404 modified from draw_textured_rect """ Like draw_textured_rect, but draw only the sub-triangle of the same rect (textured in the same way), where the subtriangle has relative 2d vertices as specified inside that rect (treating its own coords as each in [0.0, 1.0]). WARNING: depending on the glEnables set up by the caller, the sub-triangle coords might need to be in CCW winding order for the triangle to be visible from the front. """ #e could easily generalize API to polygon, and this implem to convex polygon, if desired ##e WARNING: this function's name and api are likely to be revised; # or we might just replace the whole scheme, using things like Textured(Triangle(...),...) instead, # perhaps implemented by telling OpenGL how to compute the texture coords in a wrapper, then just drawing the triangle. assert len(points) == 3 # and each point should be a V of length 2, or a 2-tuple, with elements convertible to floats -- this is assumed below glEnable(GL_TEXTURE_2D) glBegin(GL_TRIANGLES) for px, py in points: px = float(px) py = float(py) glTexCoord2fv(tex_origin + px * tex_dx + py * tex_dy) glVertex3fv(origin + px * dx + py * dy) glEnd() glDisable(GL_TEXTURE_2D)
def build_from_mesh_data(self, mesh_data: MeshData): """Uses a loaded mesh to create 3d geometry to store in the view. All groups in the mesh will pre-computed and stored by name. :param mesh_data: the source data that 3d geometry will be pre-computed from. """ material_library = mesh_data.material_library for key in mesh_data.groups: new_gl_list = glGenLists(1) # pylint: disable=assignment-from-no-return glNewList(new_gl_list, GL_COMPILE) group = mesh_data.groups[key] glEnable(GL_TEXTURE_2D) glFrontFace(GL_CCW) for face in group.faces: self._apply_material( material_library.get_material_by_name(face.material)) # Polygon (N verts) with optional normals and tex coords glBegin(GL_POLYGON) for i in range(face.vertex_count): normal_index = face.normal_ids[i] if normal_index > 0: glNormal3fv(mesh_data.normals[normal_index - 1]) tex_coord_index = face.tex_ids[i] if tex_coord_index > 0: glTexCoord2fv(mesh_data.tex_coords[tex_coord_index - 1]) glVertex3fv(mesh_data.vertices[face.position_ids[i] - 1]) glEnd() glDisable(GL_TEXTURE_2D) glEndList() self._display_lists[key] = new_gl_list
def draw_textured_rect(origin, dx, dy, tex_origin, tex_dx, tex_dy): """ Fill a spatial rect defined by the 3d points (origin, dx, dy) with the 2d-texture subrect defined by the 2d points (tex_origin, tex_dx, tex_dy) in the currently bound texture object. """ glEnable(GL_TEXTURE_2D) glBegin(GL_QUADS) glTexCoord2fv(tex_origin) # tex coords have to come before vertices, I think! ###k glVertex3fv(origin) glTexCoord2fv(tex_origin + tex_dx) glVertex3fv(origin + dx) glTexCoord2fv(tex_origin + tex_dx + tex_dy) glVertex3fv(origin + dx + dy) glTexCoord2fv(tex_origin + tex_dy) glVertex3fv(origin + dy) glEnd() glDisable(GL_TEXTURE_2D)
def drawPlane(color, w, h, textureReady, opacity, SOLID=False, pickCheckOnly=False, tex_coords=None): """ Draw polygon with size of <w>*<h> and with color <color>. Optionally, it could be texuture mapped, translucent. @pickCheckOnly This is used to draw the geometry only, used for OpenGL pick selection purpose. """ vs = [[-0.5, 0.5, 0.0], [-0.5, -0.5, 0.0], [0.5, -0.5, 0.0], [0.5, 0.5, 0.0]] # piotr 080529: use external texture coordinates if provided if tex_coords is None: vt = [[0.0, 1.0], [0.0, 0.0], [1.0, 0.0], [1.0, 1.0]] else: vt = tex_coords if textureReady: opacity = 1.0 glDisable(GL_LIGHTING) glColor4fv(list(color) + [opacity]) glPushMatrix() glScalef(w, h, 1.0) if SOLID: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) else: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glDisable(GL_CULL_FACE) if not pickCheckOnly: # This makes sure a translucent object will not occlude another # translucent object. glDepthMask(GL_FALSE) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) if textureReady: glEnable(GL_TEXTURE_2D) glBegin(GL_QUADS) for ii in range(len(vs)): t = vt[ii]; v = vs[ii] if textureReady: glTexCoord2fv(t) glVertex3fv(v) glEnd() if not pickCheckOnly: if textureReady: glDisable(GL_TEXTURE_2D) glDisable(GL_BLEND) glDepthMask(GL_TRUE) glEnable(GL_CULL_FACE) if not SOLID: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) glPopMatrix() glEnable(GL_LIGHTING) return
def drawPlane(color, w, h, textureReady, opacity, SOLID=False, pickCheckOnly=False, tex_coords=None): """ Draw polygon with size of <w>*<h> and with color <color>. Optionally, it could be texuture mapped, translucent. @pickCheckOnly This is used to draw the geometry only, used for OpenGL pick selection purpose. @param tex_coords: texture coordinates to be explicitly provided (for simple image transformation purposes) """ vs = [[-0.5, 0.5, 0.0], [-0.5, -0.5, 0.0], [0.5, -0.5, 0.0], [0.5, 0.5, 0.0]] # piotr 080529: use external texture coordinates if provided if tex_coords is None: vt = [[0.0, 1.0], [0.0, 0.0], [1.0, 0.0], [1.0, 1.0]] else: vt = tex_coords if textureReady: opacity = 1.0 glDisable(GL_LIGHTING) glColor4fv(list(color) + [opacity]) glPushMatrix() glScalef(w, h, 1.0) if SOLID: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) else: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glDisable(GL_CULL_FACE) if not pickCheckOnly: # This makes sure a translucent object will not occlude another # translucent object. glDepthMask(GL_FALSE) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) if textureReady: glEnable(GL_TEXTURE_2D) glBegin(GL_QUADS) for ii in range(len(vs)): t = vt[ii] v = vs[ii] if textureReady: glTexCoord2fv(t) glVertex3fv(v) glEnd() if not pickCheckOnly: if textureReady: glDisable(GL_TEXTURE_2D) glDisable(GL_BLEND) glDepthMask(GL_TRUE) glEnable(GL_CULL_FACE) if not SOLID: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) glPopMatrix() glEnable(GL_LIGHTING) return