def _make_list( self ): """generate opengl display list to draw triangles in mesh """ # get available list name self.list_name = glGenLists( 1 ) # start new display list glNewList( self.list_name, GL_COMPILE ) # set material glMaterialfv( GL_FRONT, GL_SPECULAR, self.specular ) glMaterialfv( GL_FRONT, GL_SHININESS, self.shininess ) glMaterialfv( GL_FRONT, GL_DIFFUSE, self.diffuse ) # start list of triangles in mesh glBegin( GL_TRIANGLES ) # for each triangle give normal and 3 vertices for triangle in self.triangles: glNormal3f( *triangle[0] ) for i in range( 1, 4 ): glVertex3f( *triangle[i] ) glEnd() glEndList()
def show_geometry(): glNewList(1, GL_COMPILE) glBegin(GL_TRIANGLES) glColor3f(1, 1, 1) vnum = dm.np_get_vertices(np_verts) tnum = dm.np_get_triangles_vertices(np_tris) dm.np_get_triangles_intensity(np_int) move_scale(np_verts[:vnum, :]) coords = np_verts[np_tris[:tnum, :], :] mi = coords[:, :, 0].min(axis=0) ma = coords[:, :, 0].max(axis=0) for f, vv in enumerate(coords): v1 = vv[2, :] - vv[0, :] v2 = vv[1, :] - vv[0, :] n = cross(v2, v1).squeeze() n /= norm(n) glNormal3f(*n) c = np_int[f] glColor3f(c, c, c) glVertex3f(*vv[0, :].squeeze()) glVertex3f(*vv[1, :].squeeze()) glVertex3f(*vv[2, :].squeeze()) glEnd() glEndList() return concatenate((mi, ma))
def make_cube(): glNewList(G_OBJ_CUBE, GL_COMPILE) vertices = [((-0.5, -0.5, -0.5), (-0.5, -0.5, 0.5), (-0.5, 0.5, 0.5), (-0.5, 0.5, -0.5)), ((-0.5, -0.5, -0.5), (-0.5, 0.5, -0.5), (0.5, 0.5, -0.5), (0.5, -0.5, -0.5)), ((0.5, -0.5, -0.5), (0.5, 0.5, -0.5), (0.5, 0.5, 0.5), (0.5, -0.5, 0.5)), ((-0.5, -0.5, 0.5), (0.5, -0.5, 0.5), (0.5, 0.5, 0.5), (-0.5, 0.5, 0.5)), ((-0.5, -0.5, 0.5), (-0.5, -0.5, -0.5), (0.5, -0.5, -0.5), (0.5, -0.5, 0.5)), ((-0.5, 0.5, -0.5), (-0.5, 0.5, 0.5), (0.5, 0.5, 0.5), (0.5, 0.5, -0.5))] normals = [(-1.0, 0.0, 0.0), (0.0, 0.0, -1.0), (1.0, 0.0, 0.0), (0.0, 0.0, 1.0), (0.0, -1.0, 0.0), (0.0, 1.0, 0.0)] glBegin(GL_QUADS) for i in range(6): glNormal3f(normals[i][0], normals[i][1], normals[i][2]) for j in range(4): glVertex3f(vertices[i][j][0], vertices[i][j][1], vertices[i][j][2]) glEnd() glEndList()
def render_me(self): glColor3f(0.75, 0.3, 0.66) #glColor3f(1, 1, 1) # surface bounds x_lower_bound = -10 x_upper_bound = 10 y_lower_bound = -10 y_upper_bound = 10 step = 0.5 x_current = x_lower_bound + step y_current = y_lower_bound + step # draw part of surface glBegin(GL_LINES) while x_current <= x_upper_bound + 0.0001: while y_current < y_upper_bound + 0.0001: normal = self.calculate_vertex_normal([ x_current, self.surface_function(x_current, y_current), y_current ]) glNormal3f(normal[0], normal[1], normal[2]) glVertex3f(x_current, self.surface_function(x_current, y_current), y_current) y_current += step y_current = y_lower_bound + step x_current += step x_current = x_lower_bound + step y_current = y_lower_bound + step # draw part of surface while y_current <= y_upper_bound + 0.01: while x_current <= x_upper_bound + 0.01: normal = self.calculate_vertex_normal([ x_current, self.surface_function(x_current, y_current), y_current ]) glNormal3f(normal[0], normal[1], normal[2]) glVertex3f(x_current, self.surface_function(x_current, y_current), y_current) x_current += step x_current = x_lower_bound + step y_current += step glEnd()
def make_man(): glNewList(G_OBJ_MAN, GL_COMPILE) obj=OBJ('./OBJ/smpl_np.obj') vertices=obj.vertices face=obj.faces for i in range(len(face)): glColor3f(216 / 255, 186 / 255, 160 / 255) glBegin(GL_TRIANGLES) glNormal3f(face[i][1][0],face[i][1][1],face[i][1][2]) for j in range(3): glVertex3f(vertices[face[i][0][j]-1][0],vertices[face[i][0][j]-1][1],vertices[face[i][0][j]-1][2]) glEnd() glEndList()
def make_cube(): glNewList(G_OBJ_CUBE, GL_COMPILE) vertices = [((-0.5, -0.5, -0.5), (-0.5, -0.5, 0.5), (-0.5, 0.5, 0.5), (-0.5, 0.5, -0.5)), ((-0.5, -0.5, -0.5), (-0.5, 0.5, -0.5), (0.5, 0.5, -0.5), (0.5, -0.5, -0.5)), ((0.5, -0.5, -0.5), (0.5, 0.5, -0.5), (0.5, 0.5, 0.5), (0.5, -0.5, 0.5)), ((-0.5, -0.5, 0.5), (0.5, -0.5, 0.5), (0.5, 0.5, 0.5), (-0.5, 0.5, 0.5)), ((-0.5, -0.5, 0.5), (-0.5, -0.5, -0.5), (0.5, -0.5, -0.5), (0.5, -0.5, 0.5)), ((-0.5, 0.5, -0.5), (-0.5, 0.5, 0.5), (0.5, 0.5, 0.5), (0.5, 0.5, -0.5))] normals = [(-1.0, 0.0, 0.0), (0.0, 0.0, -1.0), (1.0, 0.0, 0.0), (0.0, 0.0, 1.0), (0.0, -1.0, 0.0), (0.0, 1.0, 0.0)] glBegin(GL_QUADS) for i in xrange(6): glNormal3f(normals[i][0], normals[i][1], normals[i][2]) for j in xrange(4): glVertex3f(vertices[i][j][0], vertices[i][j][1], vertices[i][j][2]) glEnd() glEndList()
def make_mesh(): glNewList(G_OBJ_MESH,GL_COMPILE) vertices=[[1.0,0.0,1.0],[0.0,0.0,-1.0],[-1.0,0.0,1.0],[0.0,1.0,0.0]] face=[[1,4,3],[1,2,4],[2,3,4],[1,3,2]] for i in range(len(face)): glColor3f(216/255,186/255,160/255) point0=np.array(vertices[face[i][0]-1]) point1 = np.array(vertices[face[i][1]-1]) point2 =np.array(vertices[face[i][2]-1]) A=point1-point0 B=point0-point2 C = np.cross(B, A) N = C/np.linalg.norm(C) glBegin(GL_TRIANGLES) glNormal3f(N[0],N[1],N[2]) for j in range(3): glVertex3f(vertices[face[i][j]-1][0],vertices[face[i][j]-1][1],vertices[face[i][j]-1][2]) glEnd() glEndList()
def _render_wall(cls, wall: Wall, height: float, color: List[float]): """Draws walls out of quads in the 3D Viewer. The walls are drawn a constant height above their ground-plane points, as a convention. This function must be invoked inside the OpenGL render loop. :param wall_list: the walls to draw :param radius: the size of the rendered circle :param sections: the number of vertices used in the dashed line circle :param color: the color to render the points """ cls._set_color(color) glBegin(GL_QUADS) for wall_segment in wall.segments: glNormal3f(wall_segment.normal.x, wall_segment.normal.y, wall_segment.normal.z) glVertex3f(wall_segment.a.x, wall_segment.a.y, wall_segment.a.z) glVertex3f(wall_segment.b.x, wall_segment.b.y, wall_segment.b.z) glVertex3f(wall_segment.b.x, wall_segment.b.y, wall_segment.b.z + height) glVertex3f(wall_segment.a.x, wall_segment.a.y, wall_segment.a.z + height) glEnd()
def render(self, random_colors=False): from OpenGL.GL import GL_TRIANGLES, glBegin, glEnd, glVertex3f, glNormal3f, glColor3f glBegin(GL_TRIANGLES) try: for triangle in self.yield_triangles(): plane = triangle.calc_plane() glNormal3f(plane.unit_normal.x, plane.unit_normal.y, plane.unit_normal.z) if random_colors: color = Vector().random() glColor3f(color.x, color.y, color.z) glVertex3f(triangle.point_a.x, triangle.point_a.y, triangle.point_a.z) glVertex3f(triangle.point_b.x, triangle.point_b.y, triangle.point_b.z) glVertex3f(triangle.point_c.x, triangle.point_c.y, triangle.point_c.z) except Exception as ex: error = str(ex) error = None finally: glEnd()
def draw(self, resolution_meter, position, orientation,yaw): try: self._lock.acquire() vehicle_position = ( position[0] * resolution_meter, position[1] * resolution_meter, position[2] * resolution_meter) glTranslatef(vehicle_position[0],vehicle_position[1],vehicle_position[2]) # Translate Box matrix = quaternion_matrix(orientation) # convert quaternion to translation matrix glMultMatrixf(matrix) # Rotate Box glBegin(GL_TRIANGLES) glColor3f(0.0078, 0.2588, 0.39607) for tri in self.get_triangles(): glNormal3f(tri.normal.x, tri.normal.z, tri.normal.y) glVertex3f((tri.points[0].x - self.vehicle_size_y) * resolution_meter, (tri.points[0].z - self.vehicle_size_x) * resolution_meter, (tri.points[0].y - self.vehicle_size_z) * resolution_meter) glVertex3f((tri.points[1].x - self.vehicle_size_y) * resolution_meter, (tri.points[1].z - self.vehicle_size_x) * resolution_meter, (tri.points[1].y - self.vehicle_size_z) * resolution_meter) glVertex3f((tri.points[2].x - self.vehicle_size_y) * resolution_meter, (tri.points[2].z - self.vehicle_size_x) * resolution_meter, (tri.points[2].y - self.vehicle_size_z) * resolution_meter) glEnd() if self._lock_on_sub: modelview_matrix = self.gl_view.get_view_matrix() modelview_matrix[3] = [vehicle_position[0] * -1 , vehicle_position[1] * -1, modelview_matrix[3][2], modelview_matrix[3][3]] self.gl_view.load_view_matrix(modelview_matrix) if self._lock_rotate: self.gl_view.rotate_absolute((0,0,1),yaw) finally: self._lock.release()
def initializeGL(self): glClearColor(0.85, 0.85, 0.85, 1.0) glClearDepth(1.0) glDepthFunc(GL_LESS) glEnable(GL_DEPTH_TEST) glEnable(GL_CULL_FACE) glShadeModel(GL_SMOOTH) glEnable(GL_NORMALIZE) glEnable(GL_COLOR_MATERIAL) glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) glLightfv(GL_LIGHT0, GL_POSITION, (0.0, 0.0, 1.0, 0.0)) glEnable(GL_LIGHT0) glMatrixMode(GL_PROJECTION) glLoadIdentity() glMatrixMode(GL_MODELVIEW) glLoadIdentity() self.display_list = glGenLists(1) glNewList(self.display_list, GL_COMPILE) glScalef(0.5, 0.5, 0.5) glEnable(GL_LIGHTING) # board glColor3f(0.0, 0.0, 0.0) self.draw_cuboid(4.0, 4.0, 0.16) # USB connector glPushMatrix() glColor3f(0.5, 0.51, 0.58) glTranslatef(0.0, -1.6, 0.28) self.draw_cuboid(0.75, 0.9, 0.4) glPopMatrix() # right button glPushMatrix() glColor3f(0.5, 0.51, 0.58) glTranslatef(1.15, -1.85, 0.16) self.draw_cuboid(0.4, 0.3, 0.16) glColor3f(0.0, 0.0, 0.0) glTranslatef(0.0, -0.155, 0.025) self.draw_cuboid(0.18, 0.1, 0.08) glPopMatrix() # left button glPushMatrix() glColor3f(0.5, 0.51, 0.58) glTranslatef(-1.15, -1.85, 0.16) self.draw_cuboid(0.4, 0.3, 0.16) glColor3f(0.0, 0.0, 0.0) glTranslatef(0.0, -0.155, 0.025) self.draw_cuboid(0.18, 0.1, 0.08) glPopMatrix() # left btb top glPushMatrix() glColor3f(1.0, 1.0, 1.0) glTranslatef(-1.65, 0.0, 0.38) self.draw_cuboid(0.5, 1.4, 0.9) glPopMatrix() # right btb top glPushMatrix() glColor3f(1.0, 1.0, 1.0) glTranslatef(1.65, 0.0, 0.38) self.draw_cuboid(0.5, 1.4, 0.9) glPopMatrix() # left btb bottom glPushMatrix() glColor3f(1.0, 1.0, 1.0) glTranslatef(-1.65, 0.0, -0.33) self.draw_cuboid(0.5, 1.4, 0.5) glPopMatrix() # right btb bottom glPushMatrix() glColor3f(1.0, 1.0, 1.0) glTranslatef(1.65, 0.0, -0.33) self.draw_cuboid(0.5, 1.4, 0.5) glPopMatrix() # left bricklet port glPushMatrix() glColor3f(1.0, 1.0, 1.0) glTranslatef(-0.85, 1.8, -0.23) self.draw_cuboid(1.2, 0.4, 0.3) glPopMatrix() # right bricklet port glPushMatrix() glColor3f(1.0, 1.0, 1.0) glTranslatef(0.85, 1.8, -0.23) self.draw_cuboid(1.2, 0.4, 0.3) glPopMatrix() # left direction LED glPushMatrix() glColor3f(0.0, 0.5, 0.0) glTranslatef(-1.05, 1.425, 0.115) self.draw_cuboid(0.1, 0.2, 0.07) glPopMatrix() # top direction LED glPushMatrix() glColor3f(0.0, 0.5, 0.0) glTranslatef(-0.675, 1.8, 0.115) self.draw_cuboid(0.2, 0.1, 0.07) glPopMatrix() # right direction LED glPushMatrix() glColor3f(0.0, 0.5, 0.0) glTranslatef(-0.3, 1.425, 0.115) self.draw_cuboid(0.1, 0.2, 0.07) glPopMatrix() # bottom direction LED glPushMatrix() glColor3f(0.0, 0.5, 0.0) glTranslatef(-0.675, 1.05, 0.115) self.draw_cuboid(0.2, 0.1, 0.07) glPopMatrix() # left y orientation LED glPushMatrix() glColor3f(0.0, 0.0, 1.0) glTranslatef(0.275, 1.7, 0.115) self.draw_cuboid(0.1, 0.2, 0.07) glPopMatrix() # right y orientation LED glPushMatrix() glColor3f(1.0, 0.0, 0.0) glTranslatef(0.425, 1.7, 0.115) self.draw_cuboid(0.1, 0.2, 0.07) glPopMatrix() # top z orientation LED glPushMatrix() glColor3f(1.0, 0.0, 0.0) glTranslatef(0.35, 1.15, 0.115) self.draw_cuboid(0.2, 0.1, 0.07) glPopMatrix() # bottom z orientation LED glPushMatrix() glColor3f(0.0, 0.0, 1.0) glTranslatef(0.35, 1.0, 0.115) self.draw_cuboid(0.2, 0.1, 0.07) glPopMatrix() # top x orientation LED glPushMatrix() glColor3f(1.0, 0.0, 0.0) glTranslatef(1.0, 1.15, 0.115) self.draw_cuboid(0.2, 0.1, 0.07) glPopMatrix() # bottom x orientation LED glPushMatrix() glColor3f(0.0, 0.0, 1.0) glTranslatef(1.0, 1.0, 0.115) self.draw_cuboid(0.2, 0.1, 0.07) glPopMatrix() # top alignment corner glPushMatrix() glColor3f(1.0, 1.0, 1.0) glBegin(GL_TRIANGLES) glNormal3f(0.0, 0.0, 1.0) glVertex3f(-2.0, -2.0, 0.09) glVertex3f(-1.1, -2.0, 0.09) glVertex3f(-2.0, -1.1, 0.09) glEnd() glPopMatrix() # bottom alignment corner glPushMatrix() glColor3f(1.0, 1.0, 1.0) glBegin(GL_TRIANGLES) glNormal3f(0.0, 0.0, -1.0) glVertex3f(-2.0, -2.0, -0.09) glVertex3f(-2.0, -1.1, -0.09) glVertex3f(-1.1, -2.0, -0.09) glEnd() glPopMatrix() glDisable(GL_LIGHTING) # axis glPushMatrix() glTranslatef(-2.3, -2.3, -0.38) glLineWidth(3.0) glBegin(GL_LINES) glColor3f(1,0,0) # x axis is red glVertex3f(0,0,0) glVertex3f(3,0,0) glColor3f(0,0.5,0) # y axis is green glVertex3f(0,0,0) glVertex3f(0,3,0) glColor3f(0,0,1) # z axis is blue glVertex3f(0,0,0) glVertex3f(0,0,3) glEnd() glLineWidth(1.0) glPopMatrix() glEndList()
def initializeGL(self): glClearColor(0.85, 0.85, 0.85, 1.0) glClearDepth(1.0) glDepthFunc(GL_LESS) glEnable(GL_DEPTH_TEST) glEnable(GL_CULL_FACE) glShadeModel(GL_SMOOTH) glEnable(GL_NORMALIZE) glEnable(GL_COLOR_MATERIAL) glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) glLightfv(GL_LIGHT0, GL_POSITION, (0.0, 0.0, 1.0, 0.0)) glEnable(GL_LIGHT0) glMatrixMode(GL_PROJECTION) glLoadIdentity() glMatrixMode(GL_MODELVIEW) glLoadIdentity() self.display_list = glGenLists(1) glNewList(self.display_list, GL_COMPILE) glScalef(0.5, 0.5, 0.5) glEnable(GL_LIGHTING) # board glColor3f(0.0, 0.0, 0.0) self.draw_cuboid(4.0, 4.0, 0.16) # USB connector glPushMatrix() glColor3f(0.5, 0.51, 0.58) glTranslatef(0.0, -1.6, 0.28) self.draw_cuboid(0.75, 0.9, 0.4) glPopMatrix() # right button glPushMatrix() glColor3f(0.5, 0.51, 0.58) glTranslatef(1.15, -1.85, 0.16) self.draw_cuboid(0.4, 0.3, 0.16) glColor3f(0.0, 0.0, 0.0) glTranslatef(0.0, -0.155, 0.025) self.draw_cuboid(0.18, 0.1, 0.08) glPopMatrix() # left button glPushMatrix() glColor3f(0.5, 0.51, 0.58) glTranslatef(-1.15, -1.85, 0.16) self.draw_cuboid(0.4, 0.3, 0.16) glColor3f(0.0, 0.0, 0.0) glTranslatef(0.0, -0.155, 0.025) self.draw_cuboid(0.18, 0.1, 0.08) glPopMatrix() # left btb top glPushMatrix() glColor3f(1.0, 1.0, 1.0) glTranslatef(-1.65, 0.0, 0.38) self.draw_cuboid(0.5, 1.4, 0.6) glPopMatrix() # right btb top glPushMatrix() glColor3f(1.0, 1.0, 1.0) glTranslatef(1.65, 0.0, 0.38) self.draw_cuboid(0.5, 1.4, 0.6) glPopMatrix() # left btb bottom glPushMatrix() glColor3f(1.0, 1.0, 1.0) glTranslatef(-1.65, 0.0, -0.33) self.draw_cuboid(0.5, 1.4, 0.5) glPopMatrix() # right btb bottom glPushMatrix() glColor3f(1.0, 1.0, 1.0) glTranslatef(1.65, 0.0, -0.33) self.draw_cuboid(0.5, 1.4, 0.5) glPopMatrix() # left bricklet port glPushMatrix() glColor3f(1.0, 1.0, 1.0) glTranslatef(-0.85, 1.8, -0.23) self.draw_cuboid(1.2, 0.4, 0.3) glPopMatrix() # right bricklet port glPushMatrix() glColor3f(1.0, 1.0, 1.0) glTranslatef(0.85, 1.8, -0.23) self.draw_cuboid(1.2, 0.4, 0.3) glPopMatrix() # left direction LED glPushMatrix() glColor3f(0.0, 0.5, 0.0) glTranslatef(-1.05, 1.425, 0.115) self.draw_cuboid(0.1, 0.2, 0.07) glPopMatrix() # top direction LED glPushMatrix() glColor3f(0.0, 0.5, 0.0) glTranslatef(-0.675, 1.8, 0.115) self.draw_cuboid(0.2, 0.1, 0.07) glPopMatrix() # right direction LED glPushMatrix() glColor3f(0.0, 0.5, 0.0) glTranslatef(-0.3, 1.425, 0.115) self.draw_cuboid(0.1, 0.2, 0.07) glPopMatrix() # bottom direction LED glPushMatrix() glColor3f(0.0, 0.5, 0.0) glTranslatef(-0.675, 1.05, 0.115) self.draw_cuboid(0.2, 0.1, 0.07) glPopMatrix() # left y orientation LED glPushMatrix() glColor3f(0.0, 0.0, 1.0) glTranslatef(0.275, 1.7, 0.115) self.draw_cuboid(0.1, 0.2, 0.07) glPopMatrix() # right y orientation LED glPushMatrix() glColor3f(1.0, 0.0, 0.0) glTranslatef(0.425, 1.7, 0.115) self.draw_cuboid(0.1, 0.2, 0.07) glPopMatrix() # top z orientation LED glPushMatrix() glColor3f(1.0, 0.0, 0.0) glTranslatef(0.35, 1.15, 0.115) self.draw_cuboid(0.2, 0.1, 0.07) glPopMatrix() # bottom z orientation LED glPushMatrix() glColor3f(0.0, 0.0, 1.0) glTranslatef(0.35, 1.0, 0.115) self.draw_cuboid(0.2, 0.1, 0.07) glPopMatrix() # top x orientation LED glPushMatrix() glColor3f(1.0, 0.0, 0.0) glTranslatef(1.0, 1.15, 0.115) self.draw_cuboid(0.2, 0.1, 0.07) glPopMatrix() # bottom x orientation LED glPushMatrix() glColor3f(0.0, 0.0, 1.0) glTranslatef(1.0, 1.0, 0.115) self.draw_cuboid(0.2, 0.1, 0.07) glPopMatrix() # top alignment corner glPushMatrix() glColor3f(1.0, 1.0, 1.0) glBegin(GL_TRIANGLES) glNormal3f(0.0, 0.0, 1.0) glVertex3f(-2.0, -2.0, 0.081) glVertex3f(-1.1, -2.0, 0.081) glVertex3f(-2.0, -1.1, 0.081) glEnd() glPopMatrix() # bottom alignment corner glPushMatrix() glColor3f(1.0, 1.0, 1.0) glBegin(GL_TRIANGLES) glNormal3f(0.0, 0.0, -1.0) glVertex3f(-2.0, -2.0, -0.081) glVertex3f(-2.0, -1.1, -0.081) glVertex3f(-1.1, -2.0, -0.081) glEnd() glPopMatrix() glDisable(GL_LIGHTING) # axis glPushMatrix() glTranslatef(-2.3, -2.3, -0.38) glLineWidth(3.0) glBegin(GL_LINES) glColor3f(1, 0, 0) # x axis is red glVertex3f(0, 0, 0) glVertex3f(3, 0, 0) glColor3f(0, 0.5, 0) # y axis is green glVertex3f(0, 0, 0) glVertex3f(0, 3, 0) glColor3f(0, 0, 1) # z axis is blue glVertex3f(0, 0, 0) glVertex3f(0, 0, 3) glEnd() glLineWidth(1.0) glPopMatrix() glEndList()