def predraw(w,h): gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION,vec(1,1,10, 3)) gl.glLightModelfv( gl.GL_LIGHT_MODEL_AMBIENT|gl.GL_LIGHT_MODEL_TWO_SIDE, vec(1,1,1, 1.0) ) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() #glOrtho(-1, 1, -1, 1, -1, 1) #(w,h) = self.get_size() gl.glScalef( float(min(w,h))/w, -float(min(w,h))/h, 1 ) gl.gluPerspective(45.0, 1, 0.1, 1000.0) gl.gluLookAt( camera.x, camera.y, camera.z, 0,0,0, camera.up[0], camera.up[1], camera.up[2] )
def on_draw(self): self.clear() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() (w, h) = self.get_size() gl.glScalef( float(min(w, h))/w, -float(min(w, h))/h, 1 ) gl.gluPerspective(45.0, 1, 0.1, 1000.0) gl.gluLookAt(0, 0, 2.4, 0, 0, 0, 0, 1, 0) global render_texture render_texture = self.texture for v in self.visions.values(): gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() v.iteration() buf = pyglet.image.get_buffer_manager().get_color_buffer() rawimage = buf.get_image_data() self.texture = rawimage.get_texture() clock.tick()
def world_projection(self, aspect): """ Sets OpenGL projection and modelview matrices such that the window is centered on self.(x,y), shows at least scale world units in every direction, and is oriented by angle. """ glMatrixMode(GL_PROJECTION) glLoadIdentity() if aspect < 1: gluOrtho2D( -self.scale, +self.scale, -self.scale / aspect, +self.scale / aspect) else: gluOrtho2D( -self.scale * aspect, +self.scale * aspect, -self.scale, +self.scale) glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt( self.x, self.y, +1.0, self.x, self.y, -1.0, sin(self.angle), cos(self.angle), 0.0)
def on_draw(): pyglet.clock.tick() window.clear() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() (w, h) = window.get_size() gl.glScalef( float(min(w, h))/w, -float(min(w, h))/h, 1 ) gl.gluPerspective(45.0, 1, 0.1, 1000.0) gl.gluLookAt(0, 0, 2.4, 0, 0, 0, 0, 1, 0) for vision in window.visions.values(): gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() vision() buf = pyglet.image.get_buffer_manager().get_color_buffer() rawimage = buf.get_image_data() window.texture = rawimage.get_texture()
def set_projection3D(self): """Sets a 3D projection mantaining the aspect ratio of the original window size""" # virtual (desired) view size vw, vh = self.get_window_size() gl.glViewport(self._offset_x, self._offset_y, self._usable_width, self._usable_height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60, self._usable_width / float(self._usable_height), 0.1, 3000.0) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.gluLookAt( vw / 2.0, vh / 2.0, vh / 1.1566, # eye vw / 2.0, vh / 2.0, 0, # center 0.0, 1.0, 0.0 # up vector )
def on_resize(self, width, height): ''' calculate perspective matrix ''' v_ar = width/float(height) usableWidth = int(min(width, height*v_ar)) usableHeight = int(min(height, width/v_ar)) ox = (width - usableWidth) // 2 oy = (height - usableHeight) // 2 glViewport(ox, oy, usableWidth, usableHeight) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60, usableWidth/float(usableHeight), 0.1, 3000.0) ''' set camera position on modelview matrix ''' glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(width/2.0, height/2.0, height/1.1566, width/2.0, height/2.0, 0, 0.0, 1.0, 0.0) ''' update scene controller with size ''' self.controller.resize(width, height) #clears to a grey. glClearColor(0.4,0.4,0.4,0.) return pyglet.event.EVENT_HANDLED
def update(self): self.x += (self.target_x - self.x) * 0.1 self.y += (self.target_y - self.y) * 0.1 self.scale += (self.target_scale - self.scale) * 0.1 self.angle += (self.target_angle - self.angle) * 0.1 "Set projection and modelview matrices ready for rendering" glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D( -self.scale * self.aspect, +self.scale * self.aspect, -self.scale, +self.scale) # Set modelview matrix to move, scale & rotate to camera position" glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt( self.x, self.y, +1.0, self.x, self.y, -1.0, sin(self.angle), cos(self.angle), 0.0) print 'gluLookAt:', self.x,self.y, self.angle
def world_projection(self): """ Sets OpenGL projection and modelview matrices such that the window is centered on self.(x,y), shows at least 'scale' world units in every direction, and is oriented by rot. """ left = bottom = -self.scale right = top = self.scale aspect = self.width / self.height if aspect >= 1: # landscape left *= aspect right *= aspect else: # portrait bottom /= aspect top /= aspect glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(left, right, bottom, top) glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt( self.x, self.y, +1.0, self.x, self.y, -1.0, sin(self.rot), cos(self.rot), 0.0)
def on_draw(): window.clear() gl.glLoadIdentity() gl.gluLookAt(0, 8, 8, 0, 0, 0, 0, 1, 0) gl.glRotatef(rot, 1, 0, 0) gl.glRotatef(rot/2, 0, 1, 0) batch.draw() gl.glFinish()
def position(self, target=None): gl.glLoadIdentity() if target: gl.gluLookAt(self.x, self.y, self.z, target[0], target[1], target[2], 0, 1, 0) else: gl.glTranslatef(-self.x, -self.y, -self.z) gl.glRotatef(self.rx, 1, 0, 0) gl.glRotatef(self.ry, 0, 1, 0) gl.glRotatef(self.rz, 0, 0, 1)
def orientation(self, vec): mat = (gl.GLfloat * 16)() gl.glPushMatrix() gl.glLoadIdentity() gl.gluLookAt(0., 0., 0., vec[0], vec[1], vec[2], 0, 1, 0) gl.glGetFloatv(gl.GL_MODELVIEW_MATRIX, mat) gl.glPopMatrix() mat = np.array(mat).reshape(4, 4) # rot_mat = rotutils.rotation_matrix_between_vectors(self.orientation0, vec) self.rotation = self.rotation.from_matrix(mat[:3, :3])
def on_draw_impl(): window.clear() gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_LINE_SMOOTH) width, height = window.get_size() # print(width,height) # width = 2560 # height = 1600 gl.glViewport(0, 0, width, height) # 射影行列の設定 gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() aspect = width / float(height * 2) bottom = 0 top = state.zNear * np.tan(np.radians(PARAMS.FOVY)) left = - top * aspect right = top * aspect gl.glFrustum(left, right, bottom, top, state.zNear, PARAMS.Z_FAR) # 視野錐台の大事なやつ # 視点の設定 gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -1.0, 0.0) gl.glTranslatef(0, 0, state.distance) gl.glRotated(state.pitch, 1, 0, 0) gl.glRotated(state.yaw, 0, 1, 0) gl.glTranslatef(0, 0, -state.distance) gl.glTranslatef(*state.translation) # * は分解して渡すことを意味している # gl.glTranslatef(*[a,b,c]) は gl.glTranslatef(a,b,c) と同じ if state.lighting: ldir = [0.5, 0.5, 0.5] # world-space lighting ldir = np.dot(state.rotation, (0, 0, 1)) # MeshLab style lighting ldir = list(ldir) + [0] # w=0, directional light gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, (gl.GLfloat * 4)(*ldir)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, (gl.GLfloat * 3)(1.0, 1.0, 1.0)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, (gl.GLfloat * 3)(0.75, 0.75, 0.75)) gl.glEnable(gl.GL_LIGHT0) gl.glEnable(gl.GL_NORMALIZE) gl.glEnable(gl.GL_LIGHTING) # comment this to get round points with MSAA on gl.glEnable(gl.GL_POINT_SPRITE) board()
def focus(self, width, height): "Set projection and model view matrices ready for rendering" # Set projection matrix suitable for 2D rendering" glMatrixMode(GL_PROJECTION) glLoadIdentity() aspect = width / height gluOrtho2D(-self.scale * aspect, +self.scale * aspect, -self.scale, +self.scale) # Set model view matrix to move, scale & rotate to camera position" glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(self.x, self.y, 1.0, self.x, self.y, -1.0, 0.0, 1.0, 0.0)
def focus(self, width, height): "Set projection and modelview matrices ready for rendering" # Set projection matrix suitable for 2D rendering" glMatrixMode(GL_PROJECTION) glLoadIdentity() aspect = width / height gluOrtho2D(-self.scale * aspect, +self.scale * aspect, -self.scale, +self.scale) # Set modelview matrix to move, scale & rotate to camera position" glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(self.x, self.y, +1.0, self.x, self.y, -1.0, sin(self.angle), cos(self.angle), 0.0)
def focus(self, width, height): aspect = width / height glMatrixMode(GL_PROJECTION) glLoadIdentity() glViewport(0, 0, width, height) gluPerspective(self._fov * self._zoom, aspect, self._near, self._far) glScalef(self._scalex, self._scaley, self._scalez) glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(self._x, self._y, self._z, self._target.x, self._target.y, self._target.z, 0.0, 1.0, 0.0)
def __init__(self): self.window = Window(visible=False, fullscreen=False) self.window.on_resize = self.on_resize self.window.on_draw = self.on_draw self.window.on_key_press = self.on_key_press self.files = SvgFiles() glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt( 0.0, -0.0, 1.0, # eye 0.0, -0.0, -1.0, # lookAt 0.0, 1.0, 0.0) # up
def locate(self, force=False): """Sets the camera using gluLookAt using its eye, center and up_vector :Parameters: `force` : bool whether or not the camera will be located even if it is not dirty """ if force or self.dirty or self.once: gl.glLoadIdentity() gl.gluLookAt(self._eye.x, self._eye.y, self._eye.z, # camera eye self._center.x, self._center.y, self._center.z, # camera center self._up_vector.x, self._up_vector.y, self._up_vector.z # camera up vector ) self.once = False
def set_3d(self): """ Configure OpenGL to draw in 3d. """ width, height = self.get_size() glEnable(GL_DEPTH_TEST) glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(65.0, width / float(height), 0.1, 20060.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() # Rotation is a unit vector on 2d ground plane, position in a 2d vector # of the position in the world. position, (ex, ey, ez) = self._camera.get_viewport() gluLookAt(ex, ey, ez, position.x, position.y, 5, 0, 0, 1)
def focus(self, width, height): aspect = width/height glMatrixMode(GL_PROJECTION) glLoadIdentity() glViewport(0, 0, width, height) gluPerspective(self._fov*self._zoom, aspect, self._near, self._far) glScalef(self._scalex, self._scaley, self._scalez) glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt( self._x, self._y, self._z, self._target.x, self._target.y, self._target.z, 0.0, 1.0, 0.0)
def set_projection3D(self): """Sets a 3D projection mantaining the aspect ratio of the original window size""" # virtual (desired) view size vw, vh = self.get_window_size() gl.glViewport(self._offset_x, self._offset_y, self._usable_width, self._usable_height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60, self._usable_width / float(self._usable_height), 0.1, 3000.0) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.gluLookAt(vw / 2.0, vh / 2.0, vh / 1.1566, # eye vw / 2.0, vh / 2.0, 0, # center 0.0, 1.0, 0.0 # up vector )
def setup_3D(self): """ Setup the 3D matrix """ # ~ Modes and Flags ~ # Use 'GL_DEPTH_TEST' to ensure that OpenGL maintains a sensible drawing order for polygons no matter the viewing angle glEnable( GL_DEPTH_TEST ) # Do these setup functions really have to be run every single frame? # TODO: Try moving these to the '__init__' , see what happens # glEnable( GL_CULL_FACE ) # Uncomment to preform backface culling # This might erase arrowheads if they are away-facing! # ~ View Frustum Setup ~ glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(70, self.width / float(self.height), 0.1, 200) # Camera properties # ~ View Direction Setup ~ glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(*self.camera)
def on_resize(self, width, height): """Cette fonction permet de placer la camera a l'endroit ou le robot est dans la simulation. """ # Utiliser une projection pgl.glMatrixMode(ogl.GL_PROJECTION) pgl.glLoadIdentity() #self.x-=1 Ratio = width / height pgl.gluPerspective(35, Ratio, 1, 1000) x = self.robot.x + m.cos(self.robot.angle) * (self.robot.largeur // 2) y = self.robot.y - m.sin(self.robot.angle) * (self.robot.longueur // 2) a, b = self.obstacle(self.robot, self.robot.x, self.robot.y, self.arene, 1) #print(x,y) pgl.gluLookAt(x, 10, y, a, 0, b, 0, 1, 0) pgl.glMatrixMode(ogl.GL_MODELVIEW)
def apply(self): gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(self.yFov_rad * 180 / pi, self.xFov_rad / self.yFov_rad, self.zNear, self.zFar) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.gluLookAt( self.eye[0], self.eye[1], self.eye[2], self.tgt[0], self.tgt[1], self.tgt[2], self.up[0], self.up[1], self.up[2], )
def focus(self, window_width, window_height): gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() aspect = window_width / window_height gl.gluOrtho2D(-self.scale * aspect, +self.scale * aspect, -self.scale, +self.scale) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.gluLookAt( self.x, self.y, 1.0, # camera position self.x, self.y, -1.0, # thing we're looking at math.sin(self.angle), math.cos(self.angle), 0.0, )
def on_draw(): # Reset # ===== gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() # Camera view # =========== gl.gluPerspective(70, window.width / window.height, 1, 1000) gl.gluLookAt(2, 1, 1, 0, 0, 0, 0, 0, 1) # Draw the cube # ============= gl.glBegin(gl.GL_QUADS) # Left face gl.glColor3f(1.0, 0, 0) gl.glVertex3f(1, -1, -1) gl.glVertex3f(1, -1, 1) gl.glVertex3f(-1, -1, 1) gl.glVertex3f(-1, -1, -1) # Right face gl.glColor3f(0, 1.0, 0) gl.glVertex3f(-1, -1, 1) gl.glVertex3f(-1, 1, 1) gl.glVertex3f(-1, 1, -1) gl.glVertex3f(-1, -1, -1) #Bottom face gl.glColor3f(0, 0, 1.0) gl.glVertex3f(1, -1, -1) gl.glVertex3f(-1, -1, -1) gl.glVertex3f(-1, 1, -1) gl.glVertex3f(1, 1, -1) gl.glEnd() gl.glColor3f(0, 0, 0) pointer = gl.gluNewQuadric() gl.gluSphere(pointer, 0.1, 20, 20)
def __init__(self,image_fname=None,pmat=None,window_coords=None,**kwargs): if window_coords is None: # set default value window_coords = 'y down' super(MyAppWindow, self).__init__(**kwargs) self.calib = decompose(pmat) self.window_coords = window_coords self.img = pyglet.image.load(image_fname).get_texture(rectangle=True) if self.window_coords=='y up': self.img = self.img.get_transform(flip_y=True) self.img.anchor_x = self.img.anchor_y = 0 self.width = self.img.width self.height = self.img.height checks = pyglet.image.create(32, 32, pyglet.image.CheckerImagePattern()) self.background = pyglet.image.TileableTexture.create_for_image(checks) # Enable alpha blending, required for image.blit. gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) self.cyl = PointCylinder() # set modelview matrix to camera extrinsic parameters if 0: # do it directly e = np.vstack((self.calib['extrinsic'],[[0,0,0,1]])) # These HZ eye coords have +Z in front of camera. coord_xform = np.eye(4) coord_xform[1,1]=-1 # flip Y coordinate in eye space (OpenGL has +Y as up, HZ has -Y) coord_xform[2,2]=-1 # flip Z coordinate in eye space (OpenGL has -Z in front of camera, HZ has +Z) e2 = np.dot( coord_xform, e) extrinsic = map(float,e2.T.flat) extrinsic = (gl.GLfloat * 16)(*extrinsic) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadMatrixf(extrinsic) else: # compose view matrix r = get_gluLookAt(pmat) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.gluLookAt( *r['all_args'] ) gl.glDisable(gl.GL_DEPTH_TEST)
def on_resize(width, height): glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45, width / float(height), .1, 1000) gluLookAt( 0, 0, 4, # eye 0, 0, 0, # target 0, 1, 0 # up ) glMatrixMode(GL_MODELVIEW) return EVENT_HANDLED
def update(self): self.x += (self.target_x - self.x) * 0.1 self.y += (self.target_y - self.y) * 0.1 self.scale += (self.target_scale - self.scale) * 0.1 self.angle += (self.target_angle - self.angle) * 0.1 "Set projection and modelview matrices ready for rendering" glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(-self.scale * self.aspect, +self.scale * self.aspect, -self.scale, +self.scale) # Set modelview matrix to move, scale & rotate to camera position" glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(self.x, self.y, +1.0, self.x, self.y, -1.0, sin(self.angle), cos(self.angle), 0.0) print 'gluLookAt:', self.x, self.y, self.angle
def focus(self, width=1, height=1): """Set projection and modelview matrices ready for rendering""" if height <= 0: height = 1 if width <= 0: width = 1 # Set projection matrix suitable for 2D rendering" glMatrixMode(GL_PROJECTION) glLoadIdentity() aspect = width / float(height) # gluOrtho2D(-self.scale * aspect, +self.scale * aspect, -self.scale, +self.scale) glOrtho(-self.scale * aspect, +self.scale * aspect, -self.scale, +self.scale, self.near_plane, self.far_plane) # Set modelview matrix to move, scale & rotate to camera position" glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(int(self.x), int(self.y), +1.0, int(self.x), int(self.y), -1.0, sin(self.angle), cos(self.angle), 0.0)
def focus(self, width=1, height=1): """Set projection and modelview matrices ready for rendering""" if height <= 0: height = 1 if width <= 0: width = 1 # Set projection matrix suitable for 2D rendering" glMatrixMode(GL_PROJECTION) glLoadIdentity() aspect = width / float(height) # gluOrtho2D(-self.scale * aspect, +self.scale * aspect, -self.scale, +self.scale) glOrtho(-self.scale * aspect, +self.scale * aspect, -self.scale, +self.scale, self.near_plane, self.far_plane) # Set modelview matrix to move, scale & rotate to camera position" glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt( int(self.x), int(self.y), +1.0, int(self.x), int(self.y), -1.0, sin(self.angle), cos(self.angle), 0.0)
def on_draw(self): gl.glDepthMask(gl.GL_TRUE) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT | gl.GL_STENCIL_BUFFER_BIT) gl.glDepthMask(gl.GL_FALSE) with push_matrix(gl.GL_PROJECTION): self.set_camera_projection() with push_matrix(gl.GL_MODELVIEW): gl.glLoadIdentity() gl.gluLookAt(self.camera_x, self.camera_y, self.camera_z, self.camera_x, self.camera_y, 0.0, 0.0, 1.0, 0.0) with push_matrix(gl.GL_MODELVIEW): gl.gluLookAt(0, 0, 0, 0, -1, 0, 0, 0, -1) self.draw_ground_plane() self.target.draw() # self.shadow.draw() self.draw_shadow_lem() self.draw_lem() with push_attrib(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT): gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE) gl.glEnable(gl.GL_DEPTH_TEST) with bound_texture(self.puff_texture.target, self.puff_texture.id): Thruster.draw(self.particles) if self.simulator.crashed: self.crashed.draw() if self.simulator.landed: self.landed.draw() self.fps_display.draw()
def on_draw (self): gl.glDepthMask(gl.GL_TRUE) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT | gl.GL_STENCIL_BUFFER_BIT) gl.glDepthMask(gl.GL_FALSE) with push_matrix(gl.GL_PROJECTION): self.set_camera_projection() with push_matrix(gl.GL_MODELVIEW): gl.glLoadIdentity() gl.gluLookAt (self.camera_x, self.camera_y, self.camera_z, self.camera_x, self.camera_y, 0.0, 0.0, 1.0, 0.0) with push_matrix(gl.GL_MODELVIEW): gl.gluLookAt(0, 0, 0, 0, -1, 0, 0, 0, -1) self.draw_ground_plane() self.target.draw() # self.shadow.draw() self.draw_shadow_lem() self.draw_lem() with push_attrib(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT): gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE) gl.glEnable(gl.GL_DEPTH_TEST) with bound_texture (self.puff_texture.target, self.puff_texture.id): Thruster.draw(self.particles) if self.simulator.crashed: self.crashed.draw() if self.simulator.landed: self.landed.draw() self.fps_display.draw()
def draw(self): eyex,eyey,eyez,centx,centy,centz,upx,upy,upz=self.lookat gluLookAt(eyex,eyey,eyez,centx,centy,centz,upx,upy,upz) glMultMatrixf(self.cam_trans.matrix) glMultMatrixf(self.cam_rot.matrix)
def _render_img(self, width, height, multi_fbo, final_fbo, img_array, top_down=True): """ Render an image of the environment into a frame buffer Produce a numpy RGB array image as output """ if not self.graphics: return # Switch to the default context # This is necessary on Linux nvidia drivers # pyglet.gl._shadow_window.switch_to() self.shadow_window.switch_to() from pyglet import gl # Bind the multisampled frame buffer gl.glEnable(gl.GL_MULTISAMPLE) gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, multi_fbo) gl.glViewport(0, 0, width, height) # Clear the color and depth buffers c0, c1, c2 = self.horizon_color gl.glClearColor(c0, c1, c2, 1.0) gl.glClearDepth(1.0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) # Set the projection matrix gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective( self.cam_fov_y, width / float(height), 0.04, 100.0 ) # Set modelview matrix # Note: we add a bit of noise to the camera position for data augmentation pos = self.cur_pos angle = self.cur_angle if self.domain_rand: pos = pos + self.randomization_settings['camera_noise'] x, y, z = pos + self.cam_offset dx, dy, dz = self.get_dir_vec(angle) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() if self.draw_bbox: y += 0.8 gl.glRotatef(90, 1, 0, 0) elif not top_down: y += self.cam_height gl.glRotatef(self.cam_angle[0], 1, 0, 0) gl.glRotatef(self.cam_angle[1], 0, 1, 0) gl.glRotatef(self.cam_angle[2], 0, 0, 1) gl.glTranslatef(0, 0, self._perturb(gym_duckietown.simulator.CAMERA_FORWARD_DIST)) if top_down: gl.gluLookAt( # Eye position (self.grid_width * self.road_tile_size) / 2, self.top_cam_height, (self.grid_height * self.road_tile_size) / 2, # Target (self.grid_width * self.road_tile_size) / 2, 0, (self.grid_height * self.road_tile_size) / 2, # Up vector 0, 0, -1.0 ) else: gl.gluLookAt( # Eye position x, y, z, # Target x + dx, y + dy, z + dz, # Up vector 0, 1.0, 0.0 ) # Draw the ground quad gl.glDisable(gl.GL_TEXTURE_2D) gl.glColor3f(*self.ground_color) gl.glPushMatrix() gl.glScalef(50, 1, 50) self.ground_vlist.draw(gl.GL_QUADS) gl.glPopMatrix() # Draw the ground/noise triangles self.tri_vlist.draw(gl.GL_TRIANGLES) # Draw the road quads gl.glEnable(gl.GL_TEXTURE_2D) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) # For each grid tile for j in range(self.grid_height): for i in range(self.grid_width): # Get the tile type and angle tile = self._get_tile(i, j) if tile is None: continue # kind = tile['kind'] angle = tile['angle'] color = tile['color'] texture = tile['texture'] gl.glColor3f(*color) gl.glPushMatrix() gl.glTranslatef((i + 0.5) * self.road_tile_size, 0, (j + 0.5) * self.road_tile_size) gl.glRotatef(angle * 90, 0, 1, 0) # Bind the appropriate texture texture.bind() self.road_vlist.draw(gl.GL_QUADS) gl.glPopMatrix() if self.draw_curve and tile['drivable']: # Find curve with largest dotproduct with heading curves = self._get_tile(i, j)['curves'] curve_headings = curves[:, -1, :] - curves[:, 0, :] curve_headings = curve_headings / np.linalg.norm(curve_headings).reshape(1, -1) dirVec = get_dir_vec(angle) dot_prods = np.dot(curve_headings, dirVec) # Current ("closest") curve drawn in Red pts = curves[np.argmax(dot_prods)] bezier_draw(pts, n=20, red=True) pts = self._get_curve(i, j) for idx, pt in enumerate(pts): # Don't draw current curve in blue if idx == np.argmax(dot_prods): continue bezier_draw(pt, n=20) # For each object for idx, obj in enumerate(self.objects): obj.render(self.draw_bbox) # Draw the agent's own bounding box if self.draw_bbox: corners = get_agent_corners(pos, angle) gl.glColor3f(1, 0, 0) gl.glBegin(gl.GL_LINE_LOOP) gl.glVertex3f(corners[0, 0], 0.01, corners[0, 1]) gl.glVertex3f(corners[1, 0], 0.01, corners[1, 1]) gl.glVertex3f(corners[2, 0], 0.01, corners[2, 1]) gl.glVertex3f(corners[3, 0], 0.01, corners[3, 1]) gl.glEnd() if top_down: gl.glPushMatrix() gl.glTranslatef(*self.cur_pos) gl.glScalef(1, 1, 1) gl.glRotatef(self.cur_angle * 180 / np.pi, 0, 1, 0) # glColor3f(*self.color) self.mesh.render() gl.glPopMatrix() # Resolve the multisampled frame buffer into the final frame buffer gl.glBindFramebuffer(gl.GL_READ_FRAMEBUFFER, multi_fbo) gl.glBindFramebuffer(gl.GL_DRAW_FRAMEBUFFER, final_fbo) gl.glBlitFramebuffer( 0, 0, width, height, 0, 0, width, height, gl.GL_COLOR_BUFFER_BIT, gl.GL_LINEAR ) # Copy the frame buffer contents into a numpy array # Note: glReadPixels reads starting from the lower left corner gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, final_fbo) gl.glReadPixels( 0, 0, width, height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, img_array.ctypes.data_as(POINTER(gl.GLubyte)) ) # Unbind the frame buffer gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0) # Flip the image because OpenGL maps (0,0) to the lower-left corner # Note: this is necessary for gym.wrappers.Monitor to record videos # properly, otherwise they are vertically inverted. img_array = np.ascontiguousarray(np.flip(img_array, axis=0)) return img_array def render_obs(self): """ Render an observation from the point of view of the agent """ observation = self._render_img( self.camera_width, self.camera_height, self.multi_fbo, self.final_fbo, self.img_array, top_down=True ) # self.undistort - for UndistortWrapper if self.distortion and not self.undistort: observation = self.camera_model.distort(observation) return observation
def render(self, select_pass=0, visible=None, lights=None, effect_pass=0, before_render=None): if isinstance(self.projection, Viewport) and not select_pass: self.projection.enable() if not effect_pass or select_pass: for effect in self.effects: effect.enable() angle = self.angle if not select_pass: gl.glLoadIdentity() p = self.pos p = (-p.x, -p.y, -p.z) if self.track_target: t = self.track_target.pos up = (t - self.pos).normalized() eux = up.cross(Vector3(0,1,0)) up = eux.cross(up) gl.gluLookAt(self.pos.x, self.pos.y, self.pos.z, t.x, t.y, t.z, up.x, up.y, up.z) elif self.rotation_mode == Camera.ORBIT_MODE: if not select_pass: gl.glTranslatef(*p) gl.glRotatef(angle.z, 0, 0, 1) gl.glRotatef(angle.y, 0, 1, 0) gl.glRotatef(angle.x, 1, 0, 0) else: if not (select_pass or effect_pass): gl.glRotatef(angle.z, 0, 0, 1) gl.glRotatef(angle.y, 0, 1, 0) gl.glRotatef(angle.x, 1, 0, 0) gl.glTranslatef(*p) lights = (lights, self.lights)[lights is None] if not select_pass: if self.lights: lights.on() visible = visible or self.objects if not select_pass: if effect_pass: beforeRender = before_render or (lambda : None) beforeRender() for (idx,v) in enumerate(visible): self._render_idx = idx v.draw() else: for (idx,v) in enumerate([ v for v in visible if _translateable(v) ]): gl.glLoadName(idx) v.draw() if not select_pass and self.lights: lights.off() if not effect_pass or select_pass: for effect in self.effects: effect.disable() self._render_idx = None
def on_draw(): window.clear() gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_LINE_SMOOTH) width, height = window.get_size() gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60, width / float(height), 0.01, 20) gl.glMatrixMode(gl.GL_TEXTURE) gl.glLoadIdentity() # texcoords are [0..1] and relative to top-left pixel corner, add 0.5 to center gl.glTranslatef(0.5 / image_data.width, 0.5 / image_data.height, 0) # texture size may be increased by pyglet to a power of 2 tw, th = image_data.texture.owner.width, image_data.texture.owner.height gl.glScalef(image_data.width / float(tw), image_data.height / float(th), 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.gluLookAt(0, 0, 0, 0, 0, 1, 0, -1, 0) gl.glTranslatef(0, 0, state.distance) gl.glRotated(state.pitch, 1, 0, 0) gl.glRotated(state.yaw, 0, 1, 0) if any(state.mouse_btns): axes(0.1, 4) gl.glTranslatef(0, 0, -state.distance) gl.glTranslatef(*state.translation) gl.glColor3f(0.5, 0.5, 0.5) gl.glPushMatrix() gl.glTranslatef(0, 0.5, 0.5) grid() gl.glPopMatrix() psz = max(window.get_size()) / float(max(w, h)) if state.scale else 1 gl.glPointSize(psz) distance = (0, 0, 1) if state.attenuation else (1, 0, 0) gl.glPointParameterfv(gl.GL_POINT_DISTANCE_ATTENUATION, (gl.GLfloat * 3)(*distance)) if state.lighting: ldir = [0.5, 0.5, 0.5] # world-space lighting ldir = np.dot(state.rotation, (0, 0, 1)) # MeshLab style lighting ldir = list(ldir) + [0] # w=0, directional light gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, (gl.GLfloat * 4)(*ldir)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, (gl.GLfloat * 3)(1.0, 1.0, 1.0)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, (gl.GLfloat * 3)(0.75, 0.75, 0.75)) gl.glEnable(gl.GL_LIGHT0) gl.glEnable(gl.GL_NORMALIZE) gl.glEnable(gl.GL_LIGHTING) gl.glColor3f(1, 1, 1) texture = image_data.get_texture() gl.glEnable(texture.target) gl.glBindTexture(texture.target, texture.id) gl.glTexParameteri( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST) # comment this to get round points with MSAA on gl.glEnable(gl.GL_POINT_SPRITE) if not state.scale and not state.attenuation: gl.glDisable(gl.GL_MULTISAMPLE) # for true 1px points with MSAA on vertex_list.draw(gl.GL_POINTS) gl.glDisable(texture.target) if not state.scale and not state.attenuation: gl.glEnable(gl.GL_MULTISAMPLE) gl.glDisable(gl.GL_LIGHTING) gl.glColor3f(0.25, 0.25, 0.25) frustum(depth_intrinsics) axes() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, width, 0, height, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.glMatrixMode(gl.GL_TEXTURE) gl.glLoadIdentity() gl.glDisable(gl.GL_DEPTH_TEST) fps_display.draw()
def on_draw(): window.clear() gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_LINE_SMOOTH) width, height = window.get_size() gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60, width / float(height), 0.01, 20) gl.glMatrixMode(gl.GL_TEXTURE) gl.glLoadIdentity() # texcoords are [0..1] and relative to top-left pixel corner, add 0.5 to center gl.glTranslatef(0.5 / image_data.width, 0.5 / image_data.height, 0) # texture size may be increased by pyglet to a power of 2 tw, th = image_data.texture.owner.width, image_data.texture.owner.height gl.glScalef(image_data.width / float(tw), image_data.height / float(th), 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.gluLookAt(0, 0, 0, 0, 0, 1, 0, -1, 0) gl.glTranslatef(0, 0, state.distance) gl.glRotated(state.pitch, 1, 0, 0) gl.glRotated(state.yaw, 0, 1, 0) """ if any(state.mouse_btns): axes(0.1, 4) """ gl.glTranslatef(0, 0, -state.distance) gl.glTranslatef(*state.translation) gl.glColor3f(0.5, 0.5, 0.5) gl.glPushMatrix() gl.glTranslatef(0, 0.5, 0.5) #grid() gl.glPopMatrix() psz = 1 gl.glPointSize(psz) distance = (1, 0, 0) gl.glPointParameterfv(gl.GL_POINT_DISTANCE_ATTENUATION, (gl.GLfloat * 3)(*distance)) gl.glColor3f(1, 1, 1) texture = image_data.get_texture() gl.glEnable(texture.target) gl.glBindTexture(texture.target, texture.id) gl.glTexParameteri( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST) # comment this to get round points with MSAA on gl.glEnable(gl.GL_POINT_SPRITE) gl.glDisable(gl.GL_MULTISAMPLE) # for true 1px points with MSAA on vertex_list.draw(gl.GL_POINTS) gl.glDisable(texture.target) gl.glEnable(gl.GL_MULTISAMPLE) gl.glDisable(gl.GL_LIGHTING) gl.glColor3f(0.25, 0.25, 0.25) #frustum(depth_intrinsics) #axes() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, width, 0, height, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.glMatrixMode(gl.GL_TEXTURE) gl.glLoadIdentity() gl.glDisable(gl.GL_DEPTH_TEST)
def draw(self): gluLookAt( self.eyex, self.eyey, self.eyez, self.centerx, self.centery, self.centerz, self.upx, self.upy, self.upz )
def generate_map(self): scene_radius = 95.0 light_to_scene_distance = math.sqrt( light_pos[0]**2 + light_pos[1]**2 + light_pos[2]**2) near_plane = light_to_scene_distance - scene_radius field_of_view = math3d.radians_to_degrees(2.0 * math.atan(scene_radius / float(light_to_scene_distance))) tmp = (gl.GLfloat * 16)() gl.glMatrixMode( gl.GL_PROJECTION ) gl.glLoadIdentity() gl.gluPerspective( field_of_view, 1.0, near_plane, near_plane + (2.0 * scene_radius) ) gl.glGetFloatv( gl.GL_PROJECTION_MATRIX, tmp ) gl.light_projection = euclid.Matrix4() gl.light_projection[0:16] = tmp[:] # Switch to light's point of view gl.glMatrixMode( gl.GL_MODELVIEW ) gl.glLoadIdentity() gl.gluLookAt( self.light.pos.x, self.light.pos.y, self.light.pos.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ) gl.glGetFloatv( gl.GL_MODELVIEW_MATRIX, tmp ) light_mview = euclid.Matrix4() light_mview[0:16] = tmp[:] #glViewport(0, 0, shadow_width, shadow_height) # Clear the depth buffer only gl.glClear( gl.GL_DEPTH_BUFFER_BIT ) # Remember the current shade model gl.glPushAttrib( gl.GL_ENABLE_BIT ) prev_shade_model = cypes.GLint() gl.glGetIntegerv( gl.GL_SHADE_MODEL, prev_shade_model ) # All we care about here is resulting depth values gl.glShadeModel( gl.GL_FLAT ) gl.glDisable( gl.GL_LIGHTING ) gl.glDisable( gl.GL_COLOR_MATERIAL ) gl.glDisable( gl.GL_NORMALIZE ) gl.glColorMask( 0, 0, 0, 0 ) # Overcome imprecision gl.glEnable( gl.GL_POLYGON_OFFSET_FILL ) #draw_models(False) self.context.render( exclude=self.exclude_shadows ) # Copy depth values into depth texture gl.glCopyTexImage2D( gl.GL_TEXTURE_2D, 0, gl.GL_DEPTH_COMPONENT, 0, 0, shadow_width, shadow_height, 0 ) # Restore normal drawing state gl.glShadeModel( gl.GL_SMOOTH ) gl.glEnable( gl.GL_LIGHTING ) gl.glEnable( gl.GL_COLOR_MATERIAL ) gl.glEnable( gl.GL_NORMALIZE ) gl.glColorMask( 1, 1, 1, 1 ) gl.glDisable( gl.GL_POLYGON_OFFSET_FILL ) # Setup up the texture matrix which will be use in eye-linear # texture mapping self.tex_matrix = euclid.Matrix4() self.tex_matrix.translate(0.5, 0.5, 0.5).scale(0.5, 0.5, 0.5) tmp_matrix.scale(0.5, 0.5, 0.5) tex_matrix = (tmp_matrix * light_projection) * light_mview self.tex_matrix.transpose() # Give us immediate access to ctypes arrays self.tex_matrix = ( (gl.GLfloat * 4)(*self.tex_matrix[0:4]), (gl.GLfloat * 4)(*self.tex_matrix[4:8]), (gl.GLfloat * 4)(*self.tex_matrix[8:12]), (gl.GLfloat * 4)(*self.tex_matrix[12:16]) )
def look(self): gl.glLoadIdentity() data = list(self.position) + list(self.looking_at) + list(self.up) gl.gluLookAt(*data)
def on_draw(): window.clear() gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_LINE_SMOOTH) width, height = window.get_size() gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60, width / float(height), 0.01, 20) gl.glMatrixMode(gl.GL_TEXTURE) gl.glLoadIdentity() # texcoords are [0..1] and relative to top-left pixel corner, add 0.5 to center gl.glTranslatef(0.5 / image_data.width, 0.5 / image_data.height, 0) image_texture = image_data.get_texture() # texture size may be increased by pyglet to a power of 2 tw, th = image_texture.owner.width, image_texture.owner.height gl.glScalef(image_data.width / float(tw), image_data.height / float(th), 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.gluLookAt(0, 0, 0, 0, 0, 1, 0, -1, 0) gl.glTranslatef(0, 0, state.distance) gl.glRotated(state.pitch, 1, 0, 0) gl.glRotated(state.yaw, 0, 1, 0) if any(state.mouse_btns): axes(0.1, 4) gl.glTranslatef(0, 0, -state.distance) gl.glTranslatef(*state.translation) gl.glColor3f(0.5, 0.5, 0.5) gl.glPushMatrix() gl.glTranslatef(0, 0.5, 0.5) grid() gl.glPopMatrix() psz = max(window.get_size()) / float(max(w, h)) if state.scale else 1 gl.glPointSize(psz) distance = (0, 0, 1) if state.attenuation else (1, 0, 0) gl.glPointParameterfv(gl.GL_POINT_DISTANCE_ATTENUATION, (gl.GLfloat * 3)(*distance)) if state.lighting: ldir = [0.5, 0.5, 0.5] # world-space lighting ldir = np.dot(state.rotation, (0, 0, 1)) # MeshLab style lighting ldir = list(ldir) + [0] # w=0, directional light gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, (gl.GLfloat * 4)(*ldir)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, (gl.GLfloat * 3)(1.0, 1.0, 1.0)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, (gl.GLfloat * 3)(0.75, 0.75, 0.75)) gl.glEnable(gl.GL_LIGHT0) gl.glEnable(gl.GL_NORMALIZE) gl.glEnable(gl.GL_LIGHTING) gl.glColor3f(1, 1, 1) texture = image_data.get_texture() gl.glEnable(texture.target) gl.glBindTexture(texture.target, texture.id) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST) # comment this to get round points with MSAA on gl.glEnable(gl.GL_POINT_SPRITE) if not state.scale and not state.attenuation: gl.glDisable(gl.GL_MULTISAMPLE) # for true 1px points with MSAA on vertex_list.draw(gl.GL_POINTS) gl.glDisable(texture.target) if not state.scale and not state.attenuation: gl.glEnable(gl.GL_MULTISAMPLE) gl.glDisable(gl.GL_LIGHTING) gl.glColor3f(0.25, 0.25, 0.25) frustum(depth_intrinsics) axes() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, width, 0, height, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.glMatrixMode(gl.GL_TEXTURE) gl.glLoadIdentity() gl.glDisable(gl.GL_DEPTH_TEST) fps_display.draw()
def draw(self): eyex, eyey, eyez, centx, centy, centz, upx, upy, upz = self.lookat gluLookAt(eyex, eyey, eyez, centx, centy, centz, upx, upy, upz) glMultMatrixf(self.cam_trans.matrix) glMultMatrixf(self.cam_rot.matrix)