コード例 #1
0
ファイル: OpenGLTools.py プロジェクト: zancas/pycam
def draw_direction_cone(p1, p2, position=0.5, precision=12, size=0.1):
    distance = psub(p2, p1)
    length = pnorm(distance)
    direction = pnormalized(distance)
    if direction is None:
        # zero-length line
        return
    cone_length = length * size
    cone_radius = cone_length / 3.0
    # move the cone to the middle of the line
    GL.glTranslatef((p1[0] + p2[0]) * position, (p1[1] + p2[1]) * position,
                    (p1[2] + p2[2]) * position)
    # rotate the cone according to the line direction
    # The cross product is a good rotation axis.
    cross = pcross(direction, (0, 0, -1))
    if pnorm(cross) != 0:
        # The line direction is not in line with the z axis.
        try:
            angle = math.asin(sqrt(direction[0]**2 + direction[1]**2))
        except ValueError:
            # invalid angle - just ignore this cone
            return
        # convert from radians to degree
        angle = angle / math.pi * 180
        if direction[2] < 0:
            angle = 180 - angle
        GL.glRotatef(angle, cross[0], cross[1], cross[2])
    elif direction[2] == -1:
        # The line goes down the z axis - turn it around.
        GL.glRotatef(180, 1, 0, 0)
    else:
        # The line goes up the z axis - nothing to be done.
        pass
    # center the cone
    GL.glTranslatef(0, 0, -cone_length * position)
    # draw the cone
    GLUT.glutWireCone(cone_radius, cone_length, precision, 1)
コード例 #2
0
ファイル: Display.py プロジェクト: chaserhkj/solar-system
    def paintGL(self):
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)

        _array = galaxy.\
                celaArray_frompointer(self._galaxy.\
                                      output())
        if self._trace >= 0:
            self._dx = _array[self._trace].p.x * self._scale_factor
            self._dy = _array[self._trace].p.y * self._scale_factor
            self._dz = _array[self._trace].p.z * self._scale_factor
            if self._trace_v:
                self._phi, self._theta = self._get_angle_by_v(_array[self._trace].v)
            self.setCamera()
            
        gl.glColor(*self._planec)
        step = self._planes / float(self._celld)
        gl.glBegin(gl.GL_LINES)
        for i in xrange(2 * self._celld):
            gl.glVertex(- self._planes + i*step,
                        - self._planes)
            gl.glVertex(- self._planes + i*step,
                        self._planes - step)
        for i in xrange(2 * self._celld):
            gl.glVertex(- self._planes,
                        - self._planes + i*step)
            gl.glVertex(self._planes - step,
                        - self._planes + i*step)

        gl.glEnd()
        if self._axisl > 0:
            gl.glColor(*self._axisc)
            gl.glBegin(gl.GL_LINES)
            gl.glVertex(0,0,0)
            gl.glVertex((self._axisl+1) * step, 0 ,0)
            gl.glEnd()
            gl.glPushMatrix()
            gl.glTranslate(self._axisl * step, 0, 0)
            gl.glRotate(90, 0, 1, 0)
            glut.glutWireCone(step / 4, step , 8, 8)
            gl.glPopMatrix()

            gl.glBegin(gl.GL_LINES)
            gl.glVertex(0,0,0)
            gl.glVertex(0,(self._axisl+1) * step, 0)
            gl.glEnd()
            gl.glPushMatrix()
            gl.glTranslate(0, self._axisl * step, 0)
            gl.glRotate(-90, 1, 0, 0)
            glut.glutWireCone(step / 4, step , 8, 8)
            gl.glPopMatrix()

            gl.glBegin(gl.GL_LINES)
            gl.glVertex(0,0,0)
            gl.glVertex(0,0,(self._axisl+1) * step)
            gl.glEnd()
            gl.glPushMatrix()
            gl.glTranslate(0,0,self._axisl * step)
            glut.glutWireCone(step / 4, step , 8, 8)
            gl.glPopMatrix()

        for i in xrange(self._n):
            graphic = self._graphic[i]

            if "color" in graphic:
                gl.glColor(*graphic["color"])
            else:
                gl.glColor(*self._planec)

            if "style" in graphic:
                style = graphic["style"]
            else:
                style = self._dstyle
                
            if style == "solid":
                drawfunc = glut.glutSolidSphere
            else:
                drawfunc = glut.glutWireSphere
                
            gl.glPushMatrix()
            gl.glTranslate(_array[i].p.x,
                           _array[i].p.y,
                           _array[i].p.z)
            drawfunc(graphic["radius"], 16, 16)
            if i == self._trace:
                gl.glColor(0.0,1.0,0.0)
                
                gl.glScale(graphic["radius"],graphic["radius"],graphic["radius"])
                gl.glTranslate(0 ,0, 3)
                glut.glutSolidOctahedron()
                gl.glColor(1.0,1.0,1.0) 
                glut.glutWireOctahedron()
            gl.glPopMatrix()
            if self._trace_line and (self._trace == i or self._trace == - 1 ) :
                if self._timer.isActive():
                    self._trace_buffer[i].append(_array[i].p.x)
                    self._trace_buffer[i].append(_array[i].p.y)
                    self._trace_buffer[i].append(_array[i].p.z)
                    if len(self._trace_buffer[i]) > self._trace_buffer_size:
                        self._trace_buffer[i].pop(0)
                        self._trace_buffer[i].pop(0)
                        self._trace_buffer[i].pop(0)
                gl.glBegin(gl.GL_LINE_STRIP)
                line_step = 3 * self._line_int
                num = len(self._trace_buffer[i]) / line_step
                for j in xrange(num):
                    if self._shadow:
                        color = [float(k) / num * j for k in self._axisc]
                    else:
                        color = self._axisc
                    gl.glColor(*color)
                    gl.glVertex(self._trace_buffer[i][j * line_step],
                                self._trace_buffer[i][j * line_step+ 1],
                                self._trace_buffer[i][j * line_step+ 2])
                gl.glEnd()
コード例 #3
0
ファイル: Display.py プロジェクト: chaserhkj/solar-system
    def paintGL(self):
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)

        _array = galaxy.\
                celaArray_frompointer(self._galaxy.\
                                      output())
        if self._trace >= 0:
            self._dx = _array[self._trace].p.x * self._scale_factor
            self._dy = _array[self._trace].p.y * self._scale_factor
            self._dz = _array[self._trace].p.z * self._scale_factor
            if self._trace_v:
                self._phi, self._theta = self._get_angle_by_v(
                    _array[self._trace].v)
            self.setCamera()

        gl.glColor(*self._planec)
        step = self._planes / float(self._celld)
        gl.glBegin(gl.GL_LINES)
        for i in xrange(2 * self._celld):
            gl.glVertex(-self._planes + i * step, -self._planes)
            gl.glVertex(-self._planes + i * step, self._planes - step)
        for i in xrange(2 * self._celld):
            gl.glVertex(-self._planes, -self._planes + i * step)
            gl.glVertex(self._planes - step, -self._planes + i * step)

        gl.glEnd()
        if self._axisl > 0:
            gl.glColor(*self._axisc)
            gl.glBegin(gl.GL_LINES)
            gl.glVertex(0, 0, 0)
            gl.glVertex((self._axisl + 1) * step, 0, 0)
            gl.glEnd()
            gl.glPushMatrix()
            gl.glTranslate(self._axisl * step, 0, 0)
            gl.glRotate(90, 0, 1, 0)
            glut.glutWireCone(step / 4, step, 8, 8)
            gl.glPopMatrix()

            gl.glBegin(gl.GL_LINES)
            gl.glVertex(0, 0, 0)
            gl.glVertex(0, (self._axisl + 1) * step, 0)
            gl.glEnd()
            gl.glPushMatrix()
            gl.glTranslate(0, self._axisl * step, 0)
            gl.glRotate(-90, 1, 0, 0)
            glut.glutWireCone(step / 4, step, 8, 8)
            gl.glPopMatrix()

            gl.glBegin(gl.GL_LINES)
            gl.glVertex(0, 0, 0)
            gl.glVertex(0, 0, (self._axisl + 1) * step)
            gl.glEnd()
            gl.glPushMatrix()
            gl.glTranslate(0, 0, self._axisl * step)
            glut.glutWireCone(step / 4, step, 8, 8)
            gl.glPopMatrix()

        for i in xrange(self._n):
            graphic = self._graphic[i]

            if "color" in graphic:
                gl.glColor(*graphic["color"])
            else:
                gl.glColor(*self._planec)

            if "style" in graphic:
                style = graphic["style"]
            else:
                style = self._dstyle

            if style == "solid":
                drawfunc = glut.glutSolidSphere
            else:
                drawfunc = glut.glutWireSphere

            gl.glPushMatrix()
            gl.glTranslate(_array[i].p.x, _array[i].p.y, _array[i].p.z)
            drawfunc(graphic["radius"], 16, 16)
            if i == self._trace:
                gl.glColor(0.0, 1.0, 0.0)

                gl.glScale(graphic["radius"], graphic["radius"],
                           graphic["radius"])
                gl.glTranslate(0, 0, 3)
                glut.glutSolidOctahedron()
                gl.glColor(1.0, 1.0, 1.0)
                glut.glutWireOctahedron()
            gl.glPopMatrix()
            if self._trace_line and (self._trace == i or self._trace == -1):
                if self._timer.isActive():
                    self._trace_buffer[i].append(_array[i].p.x)
                    self._trace_buffer[i].append(_array[i].p.y)
                    self._trace_buffer[i].append(_array[i].p.z)
                    if len(self._trace_buffer[i]) > self._trace_buffer_size:
                        self._trace_buffer[i].pop(0)
                        self._trace_buffer[i].pop(0)
                        self._trace_buffer[i].pop(0)
                gl.glBegin(gl.GL_LINE_STRIP)
                line_step = 3 * self._line_int
                num = len(self._trace_buffer[i]) / line_step
                for j in xrange(num):
                    if self._shadow:
                        color = [float(k) / num * j for k in self._axisc]
                    else:
                        color = self._axisc
                    gl.glColor(*color)
                    gl.glVertex(self._trace_buffer[i][j * line_step],
                                self._trace_buffer[i][j * line_step + 1],
                                self._trace_buffer[i][j * line_step + 2])
                gl.glEnd()