def create_pyramid(color=None): """ Creates a pyramid. :param color: Pyramid color :type color: list :return: OpenGL list """ arista = 2.0 a = Point3(-0.5, -0.5, -0.333) * arista b = Point3(0.5, -0.5, -0.333) * arista c = Point3(0.5, 0.5, -0.333) * arista d = Point3(-0.5, 0.5, -0.333) * arista # noinspection PyArgumentEqualDefault e = Point3(0.0, 0.0, 0.666) * arista obj = _gl.glGenLists(1) _gl.glNewList(obj, _gl.GL_COMPILE) _gl.glPushMatrix() if color is not None: _gl.glColor4fv(color) _gl.glBegin(_gl.GL_QUADS) draw_vertex_list_create_normal([d, c, b, a]) _gl.glEnd() _gl.glBegin(_gl.GL_TRIANGLES) draw_vertex_list_create_normal([a, b, e]) draw_vertex_list_create_normal([b, c, e]) draw_vertex_list_create_normal([c, d, e]) draw_vertex_list_create_normal([d, a, e]) _gl.glEnd() _gl.glPopMatrix() _gl.glEndList() return obj
def create_tetrahedron_vbo(edge=1.0): """ Creates a VBO tetrahedron for shaders. :param edge: Edge length :type edge: float, int :return: VBO object :rtype: VBObject """ def ex(element): """ Export element to list. :param element: Element :return: List :rtype: list """ return element.export_to_list() # Create points a = Point3(-0.5, -0.288675, -0.288675) * edge b = Point3(0.5, -0.288675, -0.288675) * edge # noinspection PyArgumentEqualDefault c = Point3(0.0, 0.577350, -0.288675) * edge # noinspection PyArgumentEqualDefault d = Point3(0.0, 0.0, 0.57735) * edge # Create normals n1 = ex(_normal_3_points(a, b, d)) n2 = ex(_normal_3_points(b, c, d)) n3 = ex(_normal_3_points(c, a, d)) n4 = ex(_normal_3_points(c, b, a)) # Create triangles vertex_array = [ ex(a), ex(b), ex(d), ex(b), ex(c), ex(d), ex(c), ex(a), ex(d), ex(a), ex(b), ex(c) ] normal_array = [n1, n1, n1, n2, n2, n2, n3, n3, n3, n4, n4, n4] # Return VBO return VBObject(_vbo.VBO(_array(vertex_array, 'f')), _vbo.VBO(_array(normal_array, 'f')), len(vertex_array))
def create_cube(color=None): """ Cretes a cube. :param color: Cube color :type color: list :return: OpenGL list """ a = Point3(-1.0, -1.0, -1.0) b = Point3(1.0, -1.0, -1.0) c = Point3(1.0, -1.0, 1.0) d = Point3(-1.0, -1.0, 1.0) e = Point3(-1.0, 1.0, -1.0) f = Point3(1.0, 1.0, -1.0) g = Point3(1.0, 1.0, 1.0) h = Point3(-1.0, 1.0, 1.0) obj = _gl.glGenLists(1) _gl.glNewList(obj, _gl.GL_COMPILE) _gl.glPushMatrix() _gl.glBegin(_gl.GL_QUADS) if color is not None: _gl.glColor4fv(color) draw_vertex_list_create_normal([a, b, c, d]) draw_vertex_list_create_normal([b, f, g, c]) draw_vertex_list_create_normal([f, e, h, g]) draw_vertex_list_create_normal([e, a, d, h]) draw_vertex_list_create_normal([d, c, g, h]) draw_vertex_list_create_normal([a, e, f, b]) _gl.glEnd() _gl.glPopMatrix() _gl.glEndList() return obj
def create_pyramid_textured(texture_list): """ Create a textured pyramid. :param texture_list: Texture OpenGL list :return: OpenGL list """ edge = 2.0 a = Point3(-0.5, -0.5, -0.333) * edge b = Point3(0.5, -0.5, -0.333) * edge c = Point3(0.5, 0.5, -0.333) * edge d = Point3(-0.5, 0.5, -0.333) * edge # noinspection PyArgumentEqualDefault e = Point3(0.0, 0.0, 0.666) * edge t_list = [Point2(0, 0), Point2(1, 0), Point2(1, 1), Point2(0, 1)] t_list_face = [Point2(0, 0), Point2(0.5, 1.0), Point2(1, 0)] 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.glBegin(_gl.GL_QUADS) draw_vertex_list_create_normal_textured([d, c, b, a], t_list) _gl.glEnd() _gl.glBegin(_gl.GL_TRIANGLES) draw_vertex_list_create_normal_textured([a, b, e], t_list_face) draw_vertex_list_create_normal_textured([b, c, e], t_list_face) draw_vertex_list_create_normal_textured([c, d, e], t_list_face) draw_vertex_list_create_normal_textured([d, a, e], t_list_face) _gl.glEnd() 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_textured(texture_list): """ Create a textured cube. :param texture_list: Texture OpenGL list :return: OpenGL list """ a = Point3(-1.0, -1.0, -1.0) b = Point3(1.0, -1.0, -1.0) c = Point3(1.0, -1.0, 1.0) d = Point3(-1.0, -1.0, 1.0) e = Point3(-1.0, 1.0, -1.0) f = Point3(1.0, 1.0, -1.0) g = Point3(1.0, 1.0, 1.0) h = Point3(-1.0, 1.0, 1.0) t_list = [Point2(0, 0), Point2(1, 0), Point2(1, 1), Point2(0, 1)] 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.glBegin(_gl.GL_QUADS) draw_vertex_list_create_normal_textured([a, b, c, d], t_list) draw_vertex_list_create_normal_textured([b, f, g, c], t_list) draw_vertex_list_create_normal_textured([f, e, h, g], t_list) draw_vertex_list_create_normal_textured([e, a, d, h], t_list) draw_vertex_list_create_normal_textured([d, c, g, h], t_list) draw_vertex_list_create_normal_textured([a, e, f, b], t_list) _gl.glEnd() 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 __init__(self, posx=0.0, posy=0.0, posz=0.0): """ Constructor. :param posx: X-position :param posy: Y-position :param posz: Z-position :type posx: float, int :type posy: float, int :type posz: float, int """ self._name = 'unnamed' self._angvel = Vector3() # Angular velocity self._boolrot = [False, False, False] self._boolvel = [False, False, False] self._functionArguments = [] self._functions = [] self._functionUpdate = [] self._position = Point3(posx, posy, posz) self._posVel = Vector3() # Velocity self._properties = {}
def create_diamond(color=None): """ Creates a diamond. :param color: Diamond color :type color: list :return: OpenGL list """ # noinspection PyArgumentEqualDefault a = Point3(-1.0, -1.0, 0.0) # noinspection PyArgumentEqualDefault b = Point3(1.0, -1.0, 0.0) # noinspection PyArgumentEqualDefault c = Point3(1.0, 1.0, 0.0) # noinspection PyArgumentEqualDefault d = Point3(-1.0, 1.0, 0.0) # noinspection PyArgumentEqualDefault e = Point3(0.0, 0.0, 1.0) # noinspection PyArgumentEqualDefault f = Point3(0.0, 0.0, -1.0) obj = _gl.glGenLists(1) _gl.glNewList(obj, _gl.GL_COMPILE) _gl.glPushMatrix() if color is not None: _gl.glColor4fv(color) _gl.glBegin(_gl.GL_TRIANGLES) draw_vertex_list_create_normal([a, b, e]) draw_vertex_list_create_normal([b, c, e]) draw_vertex_list_create_normal([c, d, e]) draw_vertex_list_create_normal([d, a, e]) draw_vertex_list_create_normal([b, a, f]) draw_vertex_list_create_normal([c, b, f]) draw_vertex_list_create_normal([d, c, f]) draw_vertex_list_create_normal([a, d, f]) _gl.glEnd() _gl.glPopMatrix() _gl.glEndList() return obj
def create_pyramid_vbo(edge=1.0): """ Creates a VBO pyramid for shaders. :param edge: Edge length :type edge: float, int :return: VBO Object :rtype: VBObject """ def ex(element): """ Export element to list. :param element: Element :return: List :rtype: list """ return element.export_to_list() # Create points a = Point3(-0.5, -0.5, -0.333) * edge b = Point3(0.5, -0.5, -0.333) * edge c = Point3(0.5, 0.5, -0.333) * edge d = Point3(-0.5, 0.5, -0.333) * edge # noinspection PyArgumentEqualDefault e = Point3(0.0, 0.0, 0.666) * edge # Create normals n1 = ex(_normal_3_points(a, b, e)) n2 = ex(_normal_3_points(b, c, e)) n3 = ex(_normal_3_points(c, d, e)) n4 = ex(_normal_3_points(d, a, e)) n5 = ex(_normal_3_points(c, b, a)) # Create point list vertex_array = [ ex(b), ex(e), ex(a), ex(b), ex(c), ex(e), ex(c), ex(d), ex(e), ex(d), ex(a), ex(e), ex(a), ex(b), ex(c), ex(c), ex(d), ex(a) ] normal_array = [ n1, n1, n1, n2, n2, n2, n3, n3, n3, n4, n4, n4, n5, n5, n5, n5, n5, n5 ] # Return VBO Object return VBObject(_vbo.VBO(_array(vertex_array, 'f')), _vbo.VBO(_array(normal_array, 'f')), len(vertex_array))
def create_axes(length, both=False, text=False, font=_glut.GLUT_BITMAP_HELVETICA_18): """ Create axes system. :param length: Axes length :param both: Both axes :param text: Show axes names (x,y,z) :param font: Font :type length: float, int :type both: bool :type text: bool :type font: int :return: OpenGL list """ if length > 0: # Valid length # Crate points x = Point3(length, 0, 0) y = Point3(0, length, 0) z = Point3(0, 0, length) o = Point3() # Create list lista = _gl.glGenLists(1) _gl.glNewList(lista, _gl.GL_COMPILE) # Init primitve _gl.glBegin(_gl.GL_LINES) _gl.glColor4fv([1, 0, 0, 1]) draw_vertex_list([o, x]) _gl.glColor4fv([0, 1, 0, 1]) draw_vertex_list([o, y]) _gl.glColor4fv([0, 0, 1, 1]) draw_vertex_list([o, z]) if both: # Draw axes in both directions x = Point3(-length, 0, 0) y = Point3(0, -length, 0) z = Point3(0, 0, -length) _gl.glColor4fv([1, 0, 0, 1]) draw_vertex_list([o, x]) _gl.glColor4fv([0, 1, 0, 1]) draw_vertex_list([o, y]) _gl.glColor4fv([0, 0, 1, 1]) draw_vertex_list([o, z]) # End primitive _gl.glEnd() if text: # Draw axes names draw_text('x', Point3(length + 60, 0, -15), [1, 0, 0], font) draw_text('y', Point3(0, length + 50, -15), [0, 1, 0], font) draw_text('z', Point3(+0, +0, length + 50), [0, 0, 1], font) if both: draw_text('-x', Point3(-length - 60, 0, -15), [1, 0, 0], font) draw_text('-y', Point3(0, -length - 70, -15), [0, 1, 0], font) draw_text('-z', Point3(+0, +0, -length - 80), [0, 0, 1], font) # Returns list _gl.glEndList() return lista else: raise Exception('Axes length must be positive, greater than zero')