def InitGL(self): glClearColor(0.0, 0.0, 0.0, 1) glColor3f(1, 0, 0) glEnable(GL_DEPTH_TEST) glEnable(GL_CULL_FACE) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glEnable(GL_LIGHT1) glLightfv(GL_LIGHT0, GL_POSITION, vec(.5, .5, 1, 0)) glLightfv(GL_LIGHT0, GL_SPECULAR, vec(.5, .5, 0.5, 1)) glLightfv(GL_LIGHT0, GL_DIFFUSE, vec(1, 1, 1, 1)) glLightfv(GL_LIGHT1, GL_POSITION, vec(1, 0, .5, 0)) glLightfv(GL_LIGHT1, GL_DIFFUSE, vec(.5, .5, .5, 1)) glLightfv(GL_LIGHT1, GL_SPECULAR, vec(1, 1, 1, 1)) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0.5, 0, 0.3, 1)) glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, vec(1, 1, 1, 1)) glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80) glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, vec(0.1, 0.1, 0.1, 0.9)) glLightfv(GL_LIGHT0, GL_POSITION, vec(0.0, 200.0, 100.0, 1)) glLightfv(GL_LIGHT1, GL_POSITION, vec(0.0, -200.0, 100.0, 1))
def apply_material(color, glprefs = None): # todo: move into glprefs.py (see comment above for why) """ In the current GL context, set material parameters based on the given color (length 3 or 4) and the material-related prefs values in glprefs (which defaults to drawing_globals.glprefs). """ if glprefs is None: glprefs = drawing_globals.glprefs pass #bruce 051215: make sure color is a tuple, and has length exactly 4, for all # uses inside this function, assuming callers pass sequences of length 3 or # 4. Needed because glMaterial requires four-component vector and PyOpenGL # doesn't check. [If this is useful elsewhere, we can split it into a # separate function.] color = tuple(color) if len(color) == 3: color = color + (1.0,) # usual case elif len(color) != 4: # should never happen; if it does, this assert will always fail assert len(color) in [3,4], \ "color tuples must have length 3 or 4, unlike %r" % (color,) glColor4fv(color) # For drawing lines with lighting disabled. if not glprefs.enable_specular_highlights: glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color) # glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (0,0,0,1)) return glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color) whiteness = glprefs.specular_whiteness brightness = glprefs.specular_brightness if whiteness == 1.0: specular = (1.0, 1.0, 1.0, 1.0) # optimization else: if whiteness == 0.0: specular = color # optimization else: # assume color[3] (alpha) is not passed or is always 1.0 c1 = 1.0 - whiteness specular = ( c1 * color[0] + whiteness, c1 * color[1] + whiteness, c1 * color[2] + whiteness, 1.0 ) if brightness != 1.0: specular = ( specular[0] * brightness, specular[1] * brightness, specular[2] * brightness, 1.0 ) #e Could optimize by merging this with above 3 cases (or, of course, # by doing it in C, which we'll do eventually.) glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular) glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, glprefs.specular_shininess) return
def apply_material(color, glprefs=None): # todo: move into glprefs.py (see comment above for why) """ In the current GL context, set material parameters based on the given color (length 3 or 4) and the material-related prefs values in glprefs (which defaults to drawing_globals.glprefs). """ if glprefs is None: glprefs = drawing_globals.glprefs pass #bruce 051215: make sure color is a tuple, and has length exactly 4, for all # uses inside this function, assuming callers pass sequences of length 3 or # 4. Needed because glMaterial requires four-component vector and PyOpenGL # doesn't check. [If this is useful elsewhere, we can split it into a # separate function.] color = tuple(color) if len(color) == 3: color = color + (1.0, ) # usual case elif len(color) != 4: # should never happen; if it does, this assert will always fail assert len(color) in [3,4], \ "color tuples must have length 3 or 4, unlike %r" % (color,) glColor4fv(color) # For drawing lines with lighting disabled. if not glprefs.enable_specular_highlights: glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color) # glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (0,0,0,1)) return glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color) whiteness = glprefs.specular_whiteness brightness = glprefs.specular_brightness if whiteness == 1.0: specular = (1.0, 1.0, 1.0, 1.0) # optimization else: if whiteness == 0.0: specular = color # optimization else: # assume color[3] (alpha) is not passed or is always 1.0 c1 = 1.0 - whiteness specular = (c1 * color[0] + whiteness, c1 * color[1] + whiteness, c1 * color[2] + whiteness, 1.0) if brightness != 1.0: specular = (specular[0] * brightness, specular[1] * brightness, specular[2] * brightness, 1.0) #e Could optimize by merging this with above 3 cases (or, of course, # by doing it in C, which we'll do eventually.) glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular) glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, glprefs.specular_shininess) return
def initializeGL(self): self.resizeGL(self.width(), self.height()) glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LEQUAL) # lighting light_position = [1.0, 1.0, 2.0, 0.0] glLight(GL_LIGHT0, GL_POSITION, light_position) glMaterialfv(GL_FRONT, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0]) glMaterialf(GL_FRONT, GL_SHININESS, 100.0)
def apply(self, face=GL_FRONT_AND_BACK): '''Apply the material on current context''' if self.texture: self.texture.enable() self.texture.bind() glEnable(GL_COLOR_MATERIAL) glMaterialfv(face, GL_DIFFUSE, self.diffuse + [self.opacity]) glMaterialfv(face, GL_AMBIENT, self.ambient + [self.opacity]) glMaterialfv(face, GL_SPECULAR, self.specular + [self.opacity]) glMaterialfv(face, GL_EMISSION, self.emission + [self.opacity]) glMaterialf(face, GL_SHININESS, self.shininess) glColorMaterial(face, GL_AMBIENT_AND_DIFFUSE)
def render(self): if self.list < 0: self.list = glGenLists(1) glNewList(self.list, GL_COMPILE) for n, f in enumerate(self.faces): glMaterialfv(GL_FRONT, GL_DIFFUSE, self.colors[n]) glMaterialfv(GL_FRONT, GL_SPECULAR, self.colors[n]) glMaterialf(GL_FRONT, GL_SHININESS, 50.0) glColor4fv(self.colors[n]) glBegin(GL_POLYGON) if self.colors[n][0] > 0: glNormal3fv(self.normals[n]) for i in f: if self.colors[n][1] > 0: glNormal3fv(self.vnormals[i]) glVertex3fv(self.vertices[i]) glEnd() glEndList() glCallList(self.list)
def __draw_visualisations(self, triangles, frame, leds_vertices, intensities): # dict_properties = getPropertiesFile("../properties/default.properties") # _frame_objfilename = dict_properties['FrameModel']['frame.objfilename'] # _frame_scale = float(dict_properties['FrameModel']['frame.scale']) _frame_objfilename = property_to_string(section="FrameModel", key="frame.objfilename") _frame_scale = property_to_number(section="FrameModel", key="frame.scale", vmin=0, vmax=None, vtype=float) glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, (0.0, 0.0, 0.0, 1)) glMaterialfv(GL_FRONT, GL_SPECULAR, (0, 0, 0, .2)) glMaterialf(GL_FRONT, GL_SHININESS, 20) draw_wire_frame_of_obj_from_filename(_frame_objfilename, scale=float(_frame_scale) * 1.04) # if not EvaluatorGeneric.warned: # print("Frame has n mounting points available:"+str(len(frame))) # EvaluatorGeneric.warned=True glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, (.2, 0.2, 0.2, 1)) glMaterialfv(GL_FRONT, GL_SPECULAR, (1, 0, 0, .2)) glMaterialf(GL_FRONT, GL_SHININESS, 20) for j in range(len(frame)): draw_point(frame[j], size=8) if frame[j] is not None else None glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, (1.0, 0.0, 0.0, 1)) glMaterialfv(GL_FRONT, GL_SPECULAR, (1, 0, 0, .2)) glMaterialf(GL_FRONT, GL_SHININESS, 20) for i in range(len(leds_vertices)): led = leds_vertices[i] draw_wire_sphere(vertex=led, size=0.35, scale=1) if led is not None else None glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, (0.2, 0.2, 0.6, 1)) glMaterialfv(GL_FRONT, GL_SPECULAR, (1, 1, 1, .2)) glMaterialf(GL_FRONT, GL_SHININESS, 99) for tri in triangles: make_triangle_face(tri)
def apply_material(self): # Set material glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, self.ambient_color) glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, self.diffuse_color) glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, [1, 1, 1, 0.0]) glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 20)
def material(cls, face: MaterialFace, material_parameter: MaterialParameter, value): if isinstance(value, Iterable): glMaterialfv(face.value, material_parameter.value, value) else: glMaterialf(face.value, material_parameter.value, value)