def to_opengl(self, color=None, show_directions=False): if not GL_enabled: return if color is not None: GL.glColor4f(*color) GL.glBegin(GL.GL_TRIANGLES) # use normals to improve lighting (contributed by imyrek) normal_t = self.normal GL.glNormal3f(normal_t[0], normal_t[1], normal_t[2]) # The triangle's points are in clockwise order, but GL expects # counter-clockwise sorting. GL.glVertex3f(self.p1[0], self.p1[1], self.p1[2]) GL.glVertex3f(self.p3[0], self.p3[1], self.p3[2]) GL.glVertex3f(self.p2[0], self.p2[1], self.p2[2]) GL.glEnd() if show_directions: # display surface normals n = self.normal c = self.center d = 0.5 GL.glBegin(GL.GL_LINES) GL.glVertex3f(c[0], c[1], c[2]) GL.glVertex3f(c[0] + n[0] * d, c[1] + n[1] * d, c[2] + n[2] * d) GL.glEnd() if False: # display bounding sphere GL.glPushMatrix() middle = self.middle GL.glTranslate(middle[0], middle[1], middle[2]) if not hasattr(self, "_sphere"): self._sphere = GLU.gluNewQuadric() GLU.gluSphere(self._sphere, self.radius, 10, 10) GL.glPopMatrix() if pycam.Utils.log.is_debug(): # draw triangle id on triangle face GL.glPushMatrix() c = self.center GL.glTranslate(c[0], c[1], c[2]) p12 = pmul(padd(self.p1, self.p2), 0.5) p3_12 = pnormalized(psub(self.p3, p12)) p2_1 = pnormalized(psub(self.p1, self.p2)) pn = pcross(p2_1, p3_12) GL.glMultMatrixf((p2_1[0], p2_1[1], p2_1[2], 0, p3_12[0], p3_12[1], p3_12[2], 0, pn[0], pn[1], pn[2], 0, 0, 0, 0, 1)) n = pmul(self.normal, 0.01) GL.glTranslatef(n[0], n[1], n[2]) maxdim = max((self.maxx - self.minx), (self.maxy - self.miny), (self.maxz - self.minz)) factor = 0.001 GL.glScalef(factor * maxdim, factor * maxdim, factor * maxdim) w = 0 id_string = "%s." % str(self.id) for ch in id_string: w += GLUT.glutStrokeWidth(GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w / 2, 0, 0) for ch in id_string: GLUT.glutStrokeCharacter(GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix() if False: # draw point id on triangle face c = self.center p12 = pmul(padd(self.p1, self.p2), 0.5) p3_12 = pnormalized(psub(self.p3, p12)) p2_1 = pnormalized(psub(self.p1, self.p2)) pn = pcross(p2_1, p3_12) n = pmul(self.normal, 0.01) for p in (self.p1, self.p2, self.p3): GL.glPushMatrix() pp = psub(p, pmul(psub(p, c), 0.3)) GL.glTranslate(pp[0], pp[1], pp[2]) GL.glMultMatrixf( (p2_1[0], p2_1[1], p2_1[2], 0, p3_12[0], p3_12[1], p3_12[2], 0, pn[0], pn[1], pn[2], 0, 0, 0, 0, 1)) GL.glTranslatef(n[0], n[1], n[2]) GL.glScalef(0.001, 0.001, 0.001) w = 0 for ch in str(p.id): w += GLUT.glutStrokeWidth(GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w / 2, 0, 0) for ch in str(p.id): GLUT.glutStrokeCharacter(GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix()
def text2D(text, pos, fontSize, displaySize, angle=None, fixedWidth=False, calcSize=False): """Renders a 2D string using ``glutStrokeCharacter``. :arg text: The text to render. Only ASCII characters 32-127 (and newlines) are supported. :arg pos: 2D text position in pixels. :arg fontSize: Font size in pixels :arg displaySize: ``(width, height)`` of the canvas in pixels. :arg angle: Angle (in degrees) by which to rotate the text. :arg fixedWidth: If ``True``, a fixed-width font is used. Otherwise a variable-width font is used. :arg calcSize: If ``True``, the text is not rendered. Instead, the size of the text, in pixels, is calculated and returned (before any rotation by the ``angle``). """ if fixedWidth: font = glut.GLUT_STROKE_MONO_ROMAN else: font = glut.GLUT_STROKE_ROMAN pos = list(pos) width, height = displaySize # The glut characters have a default # height (in display coordinates) of # 152.38. Scale this to the requested # pixel font size. scale = fontSize / 152.38 # Get the current matrix mode, # and restore it when we're done mm = gl.glGetInteger(gl.GL_MATRIX_MODE) # Set up an ortho view where the # display coordinates correspond # to the canvas pixel coordinates. gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() gl.glOrtho(0, width, 0, height, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glPushMatrix() gl.glLoadIdentity() gl.glEnable(gl.GL_LINE_SMOOTH) # Draw each line one at a time width = 0 height = 0 lines = text.split('\n') for i, line in enumerate(lines): height += scale * 152.38 pos[1] -= scale * 152.38 * i gl.glPushMatrix() gl.glTranslatef(pos[0], pos[1], 0) gl.glScalef(scale, scale, scale) lineWidth = 0 for char in line: # We either calculate the # character size, or draw # the character, but not # both if calcSize: charWidth = glut.glutStrokeWidth(font, ord(char)) lineWidth += charWidth * (fontSize / 152.38) else: glut.glutStrokeCharacter(font, ord(char)) if lineWidth > width: width = lineWidth gl.glPopMatrix() gl.glMatrixMode(gl.GL_PROJECTION) gl.glPopMatrix() gl.glMatrixMode(gl.GL_MODELVIEW) gl.glPopMatrix() gl.glMatrixMode(mm) if calcSize: return width, height else: return 0, 0
def to_OpenGL(self, color=None, show_directions=False): if not GL_enabled: return if not color is None: GL.glColor4f(*color) GL.glBegin(GL.GL_TRIANGLES) # use normals to improve lighting (contributed by imyrek) normal_t = self.normal GL.glNormal3f(normal_t.x, normal_t.y, normal_t.z) # The triangle's points are in clockwise order, but GL expects # counter-clockwise sorting. GL.glVertex3f(self.p1.x, self.p1.y, self.p1.z) GL.glVertex3f(self.p3.x, self.p3.y, self.p3.z) GL.glVertex3f(self.p2.x, self.p2.y, self.p2.z) GL.glEnd() if show_directions: # display surface normals n = self.normal c = self.center d = 0.5 GL.glBegin(GL.GL_LINES) GL.glVertex3f(c.x, c.y, c.z) GL.glVertex3f(c.x+n.x*d, c.y+n.y*d, c.z+n.z*d) GL.glEnd() if False: # display bounding sphere GL.glPushMatrix() middle = self.middle GL.glTranslate(middle.x, middle.y, middle.z) if not hasattr(self, "_sphere"): self._sphere = GLU.gluNewQuadric() GLU.gluSphere(self._sphere, self.radius, 10, 10) GL.glPopMatrix() if pycam.Utils.log.is_debug(): # draw triangle id on triangle face GL.glPushMatrix() c = self.center GL.glTranslate(c.x, c.y, c.z) p12 = self.p1.add(self.p2).mul(0.5) p3_12 = self.p3.sub(p12).normalized() p2_1 = self.p1.sub(self.p2).normalized() pn = p2_1.cross(p3_12) GL.glMultMatrixf((p2_1.x, p2_1.y, p2_1.z, 0, p3_12.x, p3_12.y, p3_12.z, 0, pn.x, pn.y, pn.z, 0, 0, 0, 0, 1)) n = self.normal.mul(0.01) GL.glTranslatef(n.x, n.y, n.z) maxdim = max((self.maxx - self.minx), (self.maxy - self.miny), (self.maxz - self.minz)) factor = 0.001 GL.glScalef(factor * maxdim, factor * maxdim, factor * maxdim) w = 0 id_string = "%s." % str(self.id) for ch in id_string: w += GLUT.glutStrokeWidth(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w/2, 0, 0) for ch in id_string: GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix() if False: # draw point id on triangle face c = self.center p12 = self.p1.add(self.p2).mul(0.5) p3_12 = self.p3.sub(p12).normalized() p2_1 = self.p1.sub(self.p2).normalized() pn = p2_1.cross(p3_12) n = self.normal.mul(0.01) for p in (self.p1, self.p2, self.p3): GL.glPushMatrix() pp = p.sub(p.sub(c).mul(0.3)) GL.glTranslate(pp.x, pp.y, pp.z) GL.glMultMatrixf((p2_1.x, p2_1.y, p2_1.z, 0, p3_12.x, p3_12.y, p3_12.z, 0, pn.x, pn.y, pn.z, 0, 0, 0, 0, 1)) GL.glTranslatef(n.x, n.y, n.z) GL.glScalef(0.001, 0.001, 0.001) w = 0 for ch in str(p.id): w += GLUT.glutStrokeWidth(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w/2, 0, 0) for ch in str(p.id): GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix()
def to_OpenGL(self, color=None, show_directions=False): if not GL_enabled: return if not color is None: GL.glColor4f(*color) GL.glBegin(GL.GL_TRIANGLES) # use normals to improve lighting (contributed by imyrek) normal_t = self.normal GL.glNormal3f(normal_t.x, normal_t.y, normal_t.z) # The triangle's points are in clockwise order, but GL expects # counter-clockwise sorting. GL.glVertex3f(self.p1.x, self.p1.y, self.p1.z) GL.glVertex3f(self.p3.x, self.p3.y, self.p3.z) GL.glVertex3f(self.p2.x, self.p2.y, self.p2.z) GL.glEnd() if show_directions: # display surface normals n = self.normal c = self.center d = 0.5 GL.glBegin(GL.GL_LINES) GL.glVertex3f(c.x, c.y, c.z) GL.glVertex3f(c.x + n.x * d, c.y + n.y * d, c.z + n.z * d) GL.glEnd() if False: # display bounding sphere GL.glPushMatrix() middle = self.middle GL.glTranslate(middle.x, middle.y, middle.z) if not hasattr(self, "_sphere"): self._sphere = GLU.gluNewQuadric() GLU.gluSphere(self._sphere, self.radius, 10, 10) GL.glPopMatrix() if pycam.Utils.log.is_debug(): # draw triangle id on triangle face GL.glPushMatrix() c = self.center GL.glTranslate(c.x, c.y, c.z) p12 = self.p1.add(self.p2).mul(0.5) p3_12 = self.p3.sub(p12).normalized() p2_1 = self.p1.sub(self.p2).normalized() pn = p2_1.cross(p3_12) GL.glMultMatrixf((p2_1.x, p2_1.y, p2_1.z, 0, p3_12.x, p3_12.y, p3_12.z, 0, pn.x, pn.y, pn.z, 0, 0, 0, 0, 1)) n = self.normal.mul(0.01) GL.glTranslatef(n.x, n.y, n.z) maxdim = max((self.maxx - self.minx), (self.maxy - self.miny), (self.maxz - self.minz)) factor = 0.001 GL.glScalef(factor * maxdim, factor * maxdim, factor * maxdim) w = 0 id_string = "%s." % str(self.id) for ch in id_string: w += GLUT.glutStrokeWidth(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w / 2, 0, 0) for ch in id_string: GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix() if False: # draw point id on triangle face c = self.center p12 = self.p1.add(self.p2).mul(0.5) p3_12 = self.p3.sub(p12).normalized() p2_1 = self.p1.sub(self.p2).normalized() pn = p2_1.cross(p3_12) n = self.normal.mul(0.01) for p in (self.p1, self.p2, self.p3): GL.glPushMatrix() pp = p.sub(p.sub(c).mul(0.3)) GL.glTranslate(pp.x, pp.y, pp.z) GL.glMultMatrixf((p2_1.x, p2_1.y, p2_1.z, 0, p3_12.x, p3_12.y, p3_12.z, 0, pn.x, pn.y, pn.z, 0, 0, 0, 0, 1)) GL.glTranslatef(n.x, n.y, n.z) GL.glScalef(0.001, 0.001, 0.001) w = 0 for ch in str(p.id): w += GLUT.glutStrokeWidth(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w / 2, 0, 0) for ch in str(p.id): GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix()
def to_OpenGL(self, color=None, show_directions=False): if not GL_enabled: return if not color is None: GL.glColor4f(*color) GL.glBegin(GL.GL_TRIANGLES) # use normals to improve lighting (contributed by imyrek) normal_t = self.normal GL.glNormal3f(normal_t[0], normal_t[1], normal_t[2]) # The triangle's points are in clockwise order, but GL expects # counter-clockwise sorting. GL.glVertex3f(self.p1[0], self.p1[1], self.p1[2]) GL.glVertex3f(self.p3[0], self.p3[1], self.p3[2]) GL.glVertex3f(self.p2[0], self.p2[1], self.p2[2]) GL.glEnd() if show_directions: # display surface normals n = self.normal c = self.center d = 0.5 GL.glBegin(GL.GL_LINES) GL.glVertex3f(c[0], c[1], c[2]) GL.glVertex3f(c[0]+n[0]*d, c[1]+n[1]*d, c[2]+n[2]*d) GL.glEnd() if False: # display bounding sphere GL.glPushMatrix() middle = self.middle GL.glTranslate(middle[0], middle[1], middle[2]) if not hasattr(self, "_sphere"): self._sphere = GLU.gluNewQuadric() GLU.gluSphere(self._sphere, self.radius, 10, 10) GL.glPopMatrix() if pycam.Utils.log.is_debug(): # draw triangle id on triangle face GL.glPushMatrix() c = self.center GL.glTranslate(c[0], c[1], c[2]) p12 = pmul(padd(self.p1, self.p2), 0.5) p3_12 = pnormalized(psub(self.p3, p12)) p2_1 = pnormalized(psub(self.p1, self.p2)) pn = pcross(p2_1, p3_12) GL.glMultMatrixf((p2_1[0], p2_1[1], p2_1[2], 0, p3_12[0], p3_12[1], p3_12[2], 0, pn[0], pn[1], pn[2], 0, 0, 0, 0, 1)) n = pmul(self.normal, 0.01) GL.glTranslatef(n[0], n[1], n[2]) maxdim = max((self.maxx - self.minx), (self.maxy - self.miny), (self.maxz - self.minz)) factor = 0.001 GL.glScalef(factor * maxdim, factor * maxdim, factor * maxdim) w = 0 id_string = "%s." % str(self.id) for ch in id_string: w += GLUT.glutStrokeWidth(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w/2, 0, 0) for ch in id_string: GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix() if False: # draw point id on triangle face c = self.center p12 = pmul(padd(self.p1, self.p2), 0.5) p3_12 = pnormalized(psub(self.p3, p12)) p2_1 = pnormalized(psub(self.p1, self.p2)) pn = pcross(p2_1, p3_12) n = pmul(self.normal, 0.01) for p in (self.p1, self.p2, self.p3): GL.glPushMatrix() pp = psub(p, pmul(psub(p, c), 0.3)) GL.glTranslate(pp[0], pp[1], pp[2]) GL.glMultMatrixf((p2_1[0], p2_1[1], p2_1[2], 0, p3_12[0], p3_12[1], p3_12[2], 0, pn[0], pn[1], pn[2], 0, 0, 0, 0, 1)) GL.glTranslatef(n[0], n[1], n[2]) GL.glScalef(0.001, 0.001, 0.001) w = 0 for ch in str(p.id): w += GLUT.glutStrokeWidth(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w/2, 0, 0) for ch in str(p.id): GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix()