示例#1
0
def create_sphere(lats=10, longs=10, color=None):
    """
    Creates an sphere.

    :param lats: Latitude
    :param longs: Longitude
    :param color: Color
    :type lats: int
    :type longs: int
    :type color: list
    :return: OpenGL list
    """
    if lats >= 3 and longs >= 10:
        obj = _gl.glGenLists(1)
        _gl.glNewList(obj, _gl.GL_COMPILE)
        _gl.glPushMatrix()
        if color is not None:
            _gl.glColor4fv(color)
        # noinspection PyBroadException
        try:
            _glut.glutSolidSphere(1.0, lats, longs)
        except:
            if not _FIGURES_ERRS[0]:
                _print_gl_error(
                    'OpenGL actual version does not support glutSolidSphere function'
                )
            _FIGURES_ERRS[0] = True

            for _i in range(0, lats + 1):
                lat0 = _pi * (-0.5 + float(float(_i - 1) / float(lats)))
                z0 = _sin(lat0)
                zr0 = _cos(lat0)

                lat1 = _pi * (-0.5 + float(float(_i) / float(lats)))
                z1 = _sin(lat1)
                zr1 = _cos(lat1)

                # Use Quad strips to draw the sphere
                _gl.glBegin(_gl.GL_QUAD_STRIP)

                for _j in range(0, longs + 1):
                    _long = 2 * _pi * float(float(_j - 1) / float(longs))
                    x = _cos(_long)
                    y = _sin(_long)
                    _gl.glNormal3f(x * zr0, y * zr0, z0)
                    _gl.glVertex3f(x * zr0, y * zr0, z0)
                    _gl.glNormal3f(x * zr1, y * zr1, z1)
                    _gl.glVertex3f(x * zr1, y * zr1, z1)

                _gl.glEnd()

        _gl.glPopMatrix()
        _gl.glEndList()
        return obj
    else:
        raise Exception('Latitude and logitude must be greater than 3')
示例#2
0
 def place(self):
     """
     Place camera in world.
     """
     _glLoadIdentity()
     _gluLookAt(self._r * _sin(self._theta) * _cos(self._phi),
                self._r * _sin(self._theta) * _sin(self._phi),
                self._r * _cos(self._theta), self._center.get_x(),
                self._center.get_y(), self._center.get_z(),
                self._up.get_x(), self._up.get_y(), self._up.get_z())
示例#3
0
    def rotate_z(self, angle):
        """
        Rotate eye position in z-axis.

        :param angle: Rotation angle
        :type angle: float, int
        """
        x = self._pos.get_x() * _cos(angle) - self._pos.get_y() * _sin(angle)
        y = self._pos.get_x() * _sin(angle) + self._pos.get_y() * _cos(angle)
        z = self._pos.get_z()
        self._pos.set_x(x)
        self._pos.set_y(y)
        self._pos.set_z(z)
示例#4
0
    def rotate_z(self, ang):
        """
        Rotates the particle by <ang> grades in z-axis.

        :param ang: Rotation angle
        :type ang: float, int
        """
        if ang != 0.0:
            x = self.get_x() * _cos(ang) - self.get_y() * _sin(ang)
            y = self.get_x() * _sin(ang) + self.get_y() * _cos(ang)
            z = self.get_z()
            self.set_x(x)
            self.set_y(y)
            self.set_z(z)
示例#5
0
    def get_pos_z(self):
        """
        Return z position.

        :return:
        :rtype: float, int
        """
        return self._r * _cos(self._theta)
示例#6
0
    def get_pos_x(self):
        """
        Return x position.

        :return:
        :rtype: float, int
        """
        return self._r * _sin(self._theta) * _cos(self._phi)
示例#7
0
    def rotate_center_z(self, angle):
        """
        Rotate center around z.

        :param angle: Rotation angle
        :type angle: float, int
        """
        rad = _math.sqrt(self._pos.get_x()**2 + self._pos.get_y()**2)
        self._pos.set_x(rad * _cos(self._angle))
        self._pos.set_y(rad * _sin(self._angle))
示例#8
0
def create_circle(rad=1.0, diff=0.1, normal=None, color=None):
    """
    Creates a circle.

    :param rad: Radius
    :param diff: Difference
    :param normal: Normal
    :param color: Color
    :type rad: float, int
    :type diff: float, int
    :type normal: list
    :type color: list
    :return: OpenGL list
    """
    if normal is None:
        normal = [0.0, 0.0, 1.0]
    if diff > 0:
        obj = _gl.glGenLists(1)
        _gl.glNewList(obj, _gl.GL_COMPILE)
        _gl.glPushMatrix()
        if color is not None:
            _gl.glColor4fv(color)
        ang = 0.0
        _gl.glBegin(_gl.GL_POLYGON)
        while ang <= 360.0:
            _gl.glNormal3fv(normal)
            _gl.glVertex2f(_sin(ang) * rad, _cos(ang) * rad)
            ang += diff
        _gl.glEnd()
        _gl.glBegin(_gl.GL_LINE_LOOP)
        while ang <= 360.0:
            _gl.glVertex2f(_sin(ang) * rad, _cos(ang) * rad)
            ang += diff
        _gl.glEnd()
        _gl.glPopMatrix()
        _gl.glEndList()
        return obj
    else:
        raise Exception('Difference must be greater than zero')