def create_dodecahedron(color=None):
    """
    Creates a dodecahedron.

    :param color: Dodecahedron color
    :type color: list
    :return: OpenGL list
    """
    obj = _gl.glGenLists(1)
    _gl.glNewList(obj, _gl.GL_COMPILE)
    _gl.glPushMatrix()
    if color is not None:
        _gl.glColor4fv(color)
    # noinspection PyBroadException
    try:
        _glut.glutSolidDodecahedron()
    except:
        if not _FIGURES_ERRS[6]:
            _print_gl_error(
                'OpenGL actual version dost not support glutSolidDodecahedron function'
            )
        _FIGURES_ERRS[6] = True
    _gl.glPopMatrix()
    _gl.glEndList()
    return obj
def create_teapot(color=None):
    """
    Create a OpenGL teapot.

    :param color: Object color
    :type color: list
    :return: OpenGL list
    """
    obj = _gl.glGenLists(1)
    _gl.glNewList(obj, _gl.GL_COMPILE)
    _gl.glPushMatrix()
    if color is not None:
        _gl.glColor4fv(color)
    _gl.glRotate(90, 1, 0, 0)
    # noinspection PyBroadException
    try:
        _glut.glutSolidTeapot(1.0)
    except:
        if not _FIGURES_ERRS[4]:
            _print_gl_error(
                'OpenGL actual version doest not support glutSolidTeapot function'
            )
        _FIGURES_ERRS[4] = True
    _gl.glPopMatrix()
    _gl.glEndList()
    return obj
def create_teapot_textured(texture_list):
    """
    Creates a teapot textured.

    :param texture_list: Texture OpenGL list
    :return: Object list
    """
    obj = _gl.glGenLists(1)
    _gl.glNewList(obj, _gl.GL_COMPILE)
    _gl.glPushMatrix()
    for _i in range(len(texture_list)):
        _gl.glActiveTexture(_gl.GL_TEXTURE0 + _i)
        _gl.glEnable(_gl.GL_TEXTURE_2D)
        _gl.glBindTexture(_gl.GL_TEXTURE_2D, texture_list[_i])
    _gl.glRotate(90, 1, 0, 0)
    # noinspection PyBroadException
    try:
        _glut.glutSolidTeapot(1.0)
    except:
        if not _FIGURES_ERRS[4]:
            _print_gl_error(
                'OpenGL actual version does not support glutSolidTeapot function'
            )
        _FIGURES_ERRS[4] = True
    for _i in range(len(texture_list)):
        _gl.glActiveTexture(_gl.GL_TEXTURE0 + _i)
        _gl.glDisable(_gl.GL_TEXTURE_2D)
    _gl.glPopMatrix()
    _gl.glEndList()
    return obj
def create_cube_solid(color=None):
    """
    Create a solid cube.

    :param color: Cube color
    :type color: list
    :return: OpenGL list
    """
    obj = _gl.glGenLists(1)
    _gl.glNewList(obj, _gl.GL_COMPILE)
    _gl.glPushMatrix()
    if color is not None:
        _gl.glColor4fv(color)
    # noinspection PyBroadException
    try:
        _glut.glutSolidCube(1.0)
    except:
        if not _FIGURES_ERRS[3]:
            _print_gl_error(
                'OpenGL actual version does not support glutSolidCube function'
            )
        _FIGURES_ERRS[3] = True
    _gl.glPopMatrix()
    _gl.glEndList()
    return obj
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')
def create_cone(base=1.0, height=1.0, lat=20, lng=20, color=None):
    """
    Creates an cone with base and height, radius 1.

    :param base: Cone base
    :param height: Cone height
    :param lat: Cone latitude
    :param lng: Cone longitude
    :param color: Cone color
    :type base: float, int
    :type height: float, int
    :type lat: int
    :type lng: int
    :type color: list
    :return: OpenGL list
    """
    if lat >= 3 and lng >= 10:
        # noinspection PyArgumentEqualDefault
        circlebase = create_circle(base - 0.05, 0.1, [0.0, 0.0, -1.0], color)
        obj = _gl.glGenLists(1)
        _gl.glNewList(obj, _gl.GL_COMPILE)
        _gl.glPushMatrix()
        if color is not None:
            _gl.glColor4fv(color)
        # noinspection PyBroadException
        try:
            _glut.glutSolidCone(base, height, lat, lng)
        except:
            if not _FIGURES_ERRS[3]:
                _print_gl_error(
                    'OpenGL actual version does not support glutSolidCone function'
                )
            _FIGURES_ERRS[3] = True
        _gl.glCallList(circlebase)
        _gl.glPopMatrix()
        _gl.glEndList()
        return obj
    else:
        raise Exception(
            'Latitude and longitude of the figure must be greater than 3')
def create_torus(minr=0.5, maxr=1.0, lat=30, lng=30, color=None):
    """
    Creates a torus.

    :param minr: Minimum radius
    :param maxr: Maximum radius
    :param lat: Latitude
    :param lng: Longitude
    :param color: Color
    :type minr: float, int
    :type maxr: float, int
    :type lat: int
    :type lng: int
    :type color: list
    :return: OpenGl list
    """
    if lat >= 3 and lng >= 3:
        obj = _gl.glGenLists(1)
        _gl.glNewList(obj, _gl.GL_COMPILE)
        _gl.glPushMatrix()
        if color is not None:
            _gl.glColor4fv(color)
        # noinspection PyBroadException
        try:
            _glut.glutSolidTorus(minr, maxr, lat, lng)
        except:
            if not _FIGURES_ERRS[2]:
                _print_gl_error(
                    'OpenGL actual version does not support glutSolidTorus function'
                )
            _FIGURES_ERRS[2] = True
        _gl.glPopMatrix()
        _gl.glEndList()
        return obj
    else:
        raise Exception(
            'Latitude and longitude of the figure must be greater than 3')
Exemple #8
0
def init_light(light=None, ambient=None, constant_att=_OPENGL_DEFAULT_CONSTANT_ATTENUATION,
               diffuse=None, linear_att=_OPENGL_DEFAULT_LINEAR_ATTENUATION,
               quad_att=_OPENGL_DEFAULT_QUADRATIC_ATTENUATION, specular=None,
               spot_cutoff=_OPENGL_DEFAULT_SPOT_CUTOFF, spot_direction=None,
               spot_exponent=_OPENGL_DEFAULT_SPOT_EXPONENT):
    """
    Set light properties.

    :param light: Light OpenGL type GL_LIGHT{n}, n=0..8
    :param ambient: Ambient color
    :param constant_att: Constant attenuation
    :param diffuse: Diffuse color
    :param linear_att: Linear attenuation
    :param quad_att: Quadratic attenuation
    :param specular: Specular color
    :param spot_cutoff: Spot cutoff value
    :param spot_direction: Spot direction value
    :param spot_exponent: Spot exponent value
    :type light: int
    :type ambient: list
    :type constant_att: float, int
    :type diffuse: list
    :type linear_att: float, int
    :type quad_att: float, int
    :type specular: list
    :type spot_cutoff: float, int
    :type spot_direction: float, int
    :type spot_exponent: float, int
    """

    # Checks
    if spot_direction is None:
        spot_direction = _OPENGL_DEFAULT_SPOT_DIRECTION
    if specular is None:
        specular = _OPENGL_DEFAULT_SPECULAR_COLOR
    if diffuse is None:
        diffuse = _OPENGL_DEFAULT_DIFFUSE_COLOR
    if ambient is None:
        ambient = _OPENGL_DEFAULT_AMBIENT_COLOR
    if light is None:
        _print_gl_error('Light cannot be None')

    # Ambient color
    if ambient is not None:
        _gl.glLightfv(light, _gl.GL_AMBIENT, ambient)
    else:
        _gl.glLightfv(light, _gl.GL_AMBIENT, _OPENGL_DEFAULT_AMBIENT_COLOR)

    # Diffuse color
    if diffuse is not None:
        _gl.glLightfv(light, _gl.GL_DIFFUSE, diffuse)
    else:
        _gl.glLightfv(light, _gl.GL_DIFFUSE, _OPENGL_DEFAULT_DIFFUSE_COLOR)

    # Specular color
    if specular is not None:
        _gl.glLightfv(light, _gl.GL_SPECULAR, specular)
    else:
        _gl.glLightfv(light, _gl.GL_SPECULAR, _OPENGL_DEFAULT_SPECULAR_COLOR)

    # Cutoff
    if spot_cutoff is not None:
        _gl.glLightfv(light, _gl.GL_SPOT_CUTOFF, spot_cutoff)
    else:
        _gl.glLightfv(light, _gl.GL_SPOT_CUTOFF, _OPENGL_DEFAULT_SPOT_CUTOFF)

    # Exponent
    if spot_exponent is not None:
        _gl.glLightfv(light, _gl.GL_SPOT_EXPONENT, spot_exponent)
    else:
        _gl.glLightfv(light, _gl.GL_SPOT_EXPONENT, _OPENGL_DEFAULT_SPOT_EXPONENT)

    # Spot direction
    if spot_direction is not None:
        _gl.glLightfv(light, _gl.GL_SPOT_DIRECTION, spot_direction)
    else:
        _gl.glLightfv(light, _gl.GL_SPOT_DIRECTION, _OPENGL_DEFAULT_SPOT_DIRECTION)

    # Constant attenuation factor
    if constant_att is not None:
        _gl.glLightfv(light, _gl.GL_CONSTANT_ATTENUATION, constant_att)
    else:
        _gl.glLightfv(light, _gl.GL_CONSTANT_ATTENUATION, _OPENGL_DEFAULT_CONSTANT_ATTENUATION)

    # Lineal attenuation factor
    if linear_att is not None:
        _gl.glLightfv(light, _gl.GL_LINEAR_ATTENUATION, linear_att)
    else:
        _gl.glLightfv(light, _gl.GL_LINEAR_ATTENUATION, _OPENGL_DEFAULT_LINEAR_ATTENUATION)

    # Quadratic attenuation
    if quad_att is not None:
        _gl.glLightfv(light, _gl.GL_QUADRATIC_ATTENUATION, quad_att)
    else:
        _gl.glLightfv(light, _gl.GL_QUADRATIC_ATTENUATION, _OPENGL_DEFAULT_QUADRATIC_ATTENUATION)