def gl_enable_depth(z_near, z_far): gl.glDepthRange(z_near, z_far) gl.glClearDepth(1.0) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LEQUAL) gl.glEnable(gl.GL_CULL_FACE)
def set_depth_test(self, on=True): """Enables z test. On by default""" if on: gl.glClearDepth(1.0) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LEQUAL) gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST) else: gl.glDisable(gl.GL_DEPTH_TEST)
def _gl_enable_depth(): gl.glDepthRange(0.0, 1000.0) gl.glClearDepth(1.0) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LEQUAL) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_CULL_FACE)
def on_draw(self): gl.glClearColor(0, 0, 0, 0) gl.glClearDepth(1) self.clear() gl.glEnable(gl.GL_LINE_SMOOTH) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glViewport(0, 0, self.width, self.height) transformation = GlTransformation() transformation.reset() self.layer.do_draw(window=self, transformation=transformation)
def init_gl(self): """ Perform the magic incantations to create an OpenGL scene. """ # the background color gl.glClearColor(.97, .97, .97, 1.0) max_depth = (np.abs(self.scene.bounds).max(axis=1)**2).sum()**.5 max_depth = np.clip(max_depth, 500.00, np.inf) gl.glDepthRange(0.0, max_depth) gl.glClearDepth(1.0) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LEQUAL) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_CULL_FACE) gl.glEnable(gl.GL_LIGHTING) gl.glEnable(gl.GL_LIGHT0) gl.glEnable(gl.GL_LIGHT1) # put the light at one corner of the scenes AABB gl.glLightfv( gl.GL_LIGHT0, gl.GL_POSITION, rendering.vector_to_gl(np.append(self.scene.bounds[1], 0))) gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPECULAR, rendering.vector_to_gl(.5, .5, 1, 1)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, rendering.vector_to_gl(1, 1, 1, .75)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, rendering.vector_to_gl(.1, .1, .1, .2)) gl.glColorMaterial(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE) gl.glEnable(gl.GL_COLOR_MATERIAL) gl.glShadeModel(gl.GL_SMOOTH) gl.glMaterialfv(gl.GL_FRONT, gl.GL_AMBIENT, rendering.vector_to_gl(0.192250, 0.192250, 0.192250)) gl.glMaterialfv(gl.GL_FRONT, gl.GL_DIFFUSE, rendering.vector_to_gl(0.507540, 0.507540, 0.507540)) gl.glMaterialfv(gl.GL_FRONT, gl.GL_SPECULAR, rendering.vector_to_gl(.5082730, .5082730, .5082730)) gl.glMaterialf(gl.GL_FRONT, gl.GL_SHININESS, .4 * 128.0) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST) gl.glLineWidth(1.5) gl.glPointSize(4)
def _gl_enable_depth(camera): """ Enable depth test in OpenGL using distances from `scene.camera`. """ # set the culling depth from our camera object gl.glDepthRange(camera.z_near, camera.z_far) gl.glClearDepth(1.0) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LEQUAL) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_CULL_FACE)
def init_gl(): gl.glClearDepth(1.0) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glDepthFunc(gl.GL_LEQUAL) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LEQUAL) gl.glShadeModel(gl.GL_SMOOTH) gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_REPEAT) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_REPEAT)
def OnInitGL(self, call_reshape=True): '''Initialize OpenGL for use in the window.''' if self.GLinitialized: return self.GLinitialized = True # create a pyglet context for this panel self.pygletcontext = gl.Context(gl.current_context) self.pygletcontext.canvas = self self.pygletcontext.set_current() # normal gl init glClearColor(0, 0, 0, 1) glColor3f(1, 0, 0) glEnable(GL_DEPTH_TEST) glClearDepth(1.0) glDepthFunc(GL_LEQUAL) glEnable(GL_CULL_FACE) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) # Uncomment this line for a wireframe view # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # Simple light setup. On Windows GL_LIGHT0 is enabled by default, # but this is not the case on Linux or Mac, so remember to always # include it. glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glEnable(GL_LIGHT1) glLightfv(GL_LIGHT0, GL_POSITION, vec(.5, .5, 1, 0)) glLightfv(GL_LIGHT0, GL_SPECULAR, vec(.5, .5, 1, 1)) glLightfv(GL_LIGHT0, GL_DIFFUSE, vec(1, 1, 1, 1)) glLightfv(GL_LIGHT1, GL_POSITION, vec(1, 0, .5, 0)) glLightfv(GL_LIGHT1, GL_DIFFUSE, vec(.5, .5, .5, 1)) glLightfv(GL_LIGHT1, GL_SPECULAR, vec(1, 1, 1, 1)) glShadeModel(GL_SMOOTH) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0.5, 0, 0.3, 1)) glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, vec(1, 1, 1, 1)) glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50) glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, vec(0, 0.1, 0, 0.9)) if call_reshape: self.OnReshape() if hasattr(self.parent, "filenames") and self.parent.filenames: for filename in self.parent.filenames: self.parent.load_file(filename) self.parent.autoplate() if hasattr(self.parent, "loadcb"): self.parent.loadcb() self.parent.filenames = None
def OnInitGL(self, call_reshape = True): '''Initialize OpenGL for use in the window.''' if self.GLinitialized: return self.GLinitialized = True # create a pyglet context for this panel self.pygletcontext = gl.Context(gl.current_context) self.pygletcontext.canvas = self self.pygletcontext.set_current() # normal gl init glClearColor(0, 0, 0, 1) glColor3f(1, 0, 0) glEnable(GL_DEPTH_TEST) glClearDepth(1.0) glDepthFunc(GL_LEQUAL) glEnable(GL_CULL_FACE) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) # Uncomment this line for a wireframe view # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # Simple light setup. On Windows GL_LIGHT0 is enabled by default, # but this is not the case on Linux or Mac, so remember to always # include it. glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glEnable(GL_LIGHT1) glLightfv(GL_LIGHT0, GL_POSITION, vec(.5, .5, 1, 0)) glLightfv(GL_LIGHT0, GL_SPECULAR, vec(.5, .5, 1, 1)) glLightfv(GL_LIGHT0, GL_DIFFUSE, vec(1, 1, 1, 1)) glLightfv(GL_LIGHT1, GL_POSITION, vec(1, 0, .5, 0)) glLightfv(GL_LIGHT1, GL_DIFFUSE, vec(.5, .5, .5, 1)) glLightfv(GL_LIGHT1, GL_SPECULAR, vec(1, 1, 1, 1)) glShadeModel(GL_SMOOTH) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0.5, 0, 0.3, 1)) glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, vec(1, 1, 1, 1)) glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50) glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, vec(0, 0.1, 0, 0.9)) if call_reshape: self.OnReshape() if hasattr(self.parent, "filenames") and self.parent.filenames: for filename in self.parent.filenames: self.parent.load_file(filename) self.parent.autoplate() if hasattr(self.parent, "loadcb"): self.parent.loadcb() self.parent.filenames = None
def setBuffer(self, buffer, clear=True): """Set the eye buffer to draw to. Subsequent draw calls will be diverted to the specified eye. Parameters ---------- buffer : str Eye buffer to draw to. Values can either be 'left' or 'right'. clear : bool Clear the buffer prior to drawing. """ # check if the buffer name is valid if buffer not in ('left', 'right', 'back'): raise RuntimeError("Invalid buffer name specified.") # don't change the buffer if same, but allow clearing if buffer != self.buffer: if not self._directDraw: # handle when the back buffer is selected if buffer == 'back': if self.useFBO: GL.glBindFramebuffer( GL.GL_FRAMEBUFFER, self._eyeBuffers[buffer]['frameBuffer']) else: GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0) else: GL.glBindFramebuffer( GL.GL_FRAMEBUFFER, self._eyeBuffers[buffer]['frameBuffer']) self.viewport = self.scissor = self._bufferViewports[buffer] GL.glEnable(GL.GL_SCISSOR_TEST) self.buffer = buffer # set buffer string if clear: self.setColor(self.color) # clear the buffer to the window color GL.glClearDepth(1.0) GL.glDepthMask(GL.GL_TRUE) GL.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT) GL.glDisable(GL.GL_TEXTURE_2D) GL.glEnable(GL.GL_BLEND)
def init_gl(self, on_init=None, **kwargs): gl.glClearColor(.97, .97, .97, 1.0) max_depth = (np.abs(self.scene.bounds).max(axis=1)**2).sum()**.5 max_depth = np.clip(max_depth, 500.00, np.inf) gl.glDepthRange(0.0, max_depth) gl.glClearDepth(1.0) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LEQUAL) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_CULL_FACE) gl.glEnable(gl.GL_LIGHTING) gl.glEnable(gl.GL_LIGHT0) gl.glEnable(gl.GL_LIGHT1) # put the light at one corner of the scenes AABB gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, _gl_vector(np.append(self.scene.bounds[1], 0))) gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPECULAR, _gl_vector(.5, .5, 1, 1)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, _gl_vector(1, 1, 1, .75)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, _gl_vector(.1, .1, .1, .2)) gl.glColorMaterial(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE) gl.glEnable(gl.GL_COLOR_MATERIAL) gl.glShadeModel(gl.GL_SMOOTH) gl.glMaterialfv(gl.GL_FRONT, gl.GL_AMBIENT, _gl_vector(0.192250, 0.192250, 0.192250)) gl.glMaterialfv(gl.GL_FRONT, gl.GL_DIFFUSE, _gl_vector(0.507540, 0.507540, 0.507540)) gl.glMaterialfv(gl.GL_FRONT, gl.GL_SPECULAR, _gl_vector(.5082730, .5082730, .5082730)) gl.glMaterialf(gl.GL_FRONT, gl.GL_SHININESS, .4 * 128.0) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST) gl.glLineWidth(1.5) gl.glPointSize(4) if on_init: on_init(gl, **kwargs)
def OnInitGL(self, call_reshape=True): '''Initialize OpenGL for use in the window.''' if self.GLinitialized: return self.GLinitialized = True # create a pyglet context for this panel self.pygletcontext = gl.Context(gl.current_context) self.pygletcontext.canvas = self self.pygletcontext.set_current() # normal gl init glClearColor(*self.color_background) glClearDepth(1.0) # set depth value to 1 glDepthFunc(GL_LEQUAL) glEnable(GL_COLOR_MATERIAL) glEnable(GL_DEPTH_TEST) glEnable(GL_CULL_FACE) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) if call_reshape: self.OnReshape()
def OnInitGL(self, call_reshape = True): '''Initialize OpenGL for use in the window.''' if self.GLinitialized: return self.GLinitialized = True # create a pyglet context for this panel self.pygletcontext = gl.Context(gl.current_context) self.pygletcontext.canvas = self self.pygletcontext.set_current() # normal gl init glClearColor(*self.color_background) glClearDepth(1.0) # set depth value to 1 glDepthFunc(GL_LEQUAL) glEnable(GL_COLOR_MATERIAL) glEnable(GL_DEPTH_TEST) glEnable(GL_CULL_FACE) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) if call_reshape: self.OnReshape()
def setup(self): self.camera = PlotCamera(self, ortho=self.ortho) self.controller = PlotController(self, invert_mouse_zoom=self.invert_mouse_zoom) self.push_handlers(self.controller) pgl.glClearColor(1.0, 1.0, 1.0, 0.0) pgl.glClearDepth(1.0) pgl.glDepthFunc(pgl.GL_LESS) pgl.glEnable(pgl.GL_DEPTH_TEST) pgl.glEnable(pgl.GL_LINE_SMOOTH) pgl.glShadeModel(pgl.GL_SMOOTH) pgl.glLineWidth(self.linewidth) pgl.glEnable(pgl.GL_BLEND) pgl.glBlendFunc(pgl.GL_SRC_ALPHA, pgl.GL_ONE_MINUS_SRC_ALPHA) if self.antialiasing: pgl.glHint(pgl.GL_LINE_SMOOTH_HINT, pgl.GL_NICEST) pgl.glHint(pgl.GL_POLYGON_SMOOTH_HINT, pgl.GL_NICEST) self.camera.setup_projection()
def init_gl(self): """ Perform the magic incantations to create an OpenGL scene using pyglet. """ # default background color is white-ish background = [.99, .99, .99, 1.0] # if user passed a background color use it if 'background' in self.kwargs: try: # convert to (4,) uint8 RGBA background = to_rgba(self.kwargs['background']) # convert to 0.0 - 1.0 float background = background.astype(np.float64) / 255.0 except BaseException: log.error('background color set but wrong!', exc_info=True) # apply the background color gl.glClearColor(*background) # find the maximum depth based on # maximum length of scene AABB max_depth = (np.abs(self.scene.bounds).max(axis=1) ** 2).sum() ** .5 max_depth = np.clip(max_depth, 500.00, np.inf) gl.glDepthRange(0.0, max_depth) gl.glClearDepth(1.0) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LEQUAL) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_CULL_FACE) # do some openGL things gl.glColorMaterial(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE) gl.glEnable(gl.GL_COLOR_MATERIAL) gl.glShadeModel(gl.GL_SMOOTH) gl.glMaterialfv(gl.GL_FRONT, gl.GL_AMBIENT, rendering.vector_to_gl( 0.192250, 0.192250, 0.192250)) gl.glMaterialfv(gl.GL_FRONT, gl.GL_DIFFUSE, rendering.vector_to_gl( 0.507540, 0.507540, 0.507540)) gl.glMaterialfv(gl.GL_FRONT, gl.GL_SPECULAR, rendering.vector_to_gl( .5082730, .5082730, .5082730)) gl.glMaterialf(gl.GL_FRONT, gl.GL_SHININESS, .4 * 128.0) # enable blending for transparency gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) # make the lines from Path3D objects less ugly gl.glEnable(gl.GL_LINE_SMOOTH) gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST) # set the width of lines to 1.5 pixels gl.glLineWidth(1.5) # set PointCloud markers to 4 pixels in size gl.glPointSize(4) # set up the viewer lights using self.scene self.update_lighting()
def init_gl(self): """ Perform the magic incantations to create an OpenGL scene. """ # default background color is white-ish background = [.99, .99, .99, 1.0] # if user passed a background color use it if 'background' in self.kwargs: try: # convert to (4,) uint8 RGBA background = to_rgba(self.kwargs['background']) # convert to 0.0 - 1.0 float background = background.astype(np.float64) / 255.0 except BaseException: log.error('background color wrong!', exc_info=True) # apply the background color gl.glClearColor(*background) max_depth = (np.abs(self.scene.bounds).max(axis=1)**2).sum()**.5 max_depth = np.clip(max_depth, 500.00, np.inf) gl.glDepthRange(0.0, max_depth) gl.glClearDepth(1.0) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LEQUAL) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_CULL_FACE) gl.glEnable(gl.GL_LIGHTING) gl.glEnable(gl.GL_LIGHT0) gl.glEnable(gl.GL_LIGHT1) # put the light at one corner of the scenes AABB gl.glLightfv( gl.GL_LIGHT0, gl.GL_POSITION, rendering.vector_to_gl(np.append(self.scene.bounds[1], 0))) gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPECULAR, rendering.vector_to_gl(.5, .5, 1, 1)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, rendering.vector_to_gl(1, 1, 1, .75)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, rendering.vector_to_gl(.1, .1, .1, .2)) gl.glColorMaterial(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE) gl.glEnable(gl.GL_COLOR_MATERIAL) gl.glShadeModel(gl.GL_SMOOTH) gl.glMaterialfv(gl.GL_FRONT, gl.GL_AMBIENT, rendering.vector_to_gl(0.192250, 0.192250, 0.192250)) gl.glMaterialfv(gl.GL_FRONT, gl.GL_DIFFUSE, rendering.vector_to_gl(0.507540, 0.507540, 0.507540)) gl.glMaterialfv(gl.GL_FRONT, gl.GL_SPECULAR, rendering.vector_to_gl(.5082730, .5082730, .5082730)) gl.glMaterialf(gl.GL_FRONT, gl.GL_SHININESS, .4 * 128.0) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST) gl.glLineWidth(1.5) gl.glPointSize(4)
def _initGL(self): gl.glClearColor(0.8, 0.8, 0.9, 0) gl.glClearDepth(1.0) # Enables Clearing Of The Depth Buffer gl.glDepthFunc(gl.GL_LESS) # The Type Of Depth Test To Do gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST) # make stuff look nice gl.glEnable(gl.GL_LINE_SMOOTH) gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST) if self.anti_alias: gl.glLineWidth(0.1) else: gl.glLineWidth(1.0) #gl.glEnable(gl.GL_BLEND) #gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_DEPTH_TEST) # Enables Depth Testing gl.glEnable(gl.GL_LIGHTING) #gl.glEnable(gl.GL_NORMALIZE) #gl.glLightModeli(gl.GL_LIGHT_MODEL_TWO_SIDE, gl.GL_FALSE) if not gl.glUseProgram: print("Can't run shaders!") sys.exit(1) self.default_shader = compileProgram( compileShader( ''' varying vec3 vN; varying vec3 v; void main(void) { v = vec3(gl_ModelViewMatrix * gl_Vertex); vN = normalize(gl_NormalMatrix * gl_Normal); gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; } ''', gl.GL_VERTEX_SHADER), compileShader( ''' varying vec3 vN; varying vec3 v; #define MAX_LIGHTS 1 void main (void) { vec3 N = normalize(vN); vec4 finalColor = vec4(0.0, 0.0, 0.0, 0.0); for (int i=0;i<MAX_LIGHTS;i++) { vec3 L = normalize(gl_LightSource[i].position.xyz - v); vec3 E = normalize(-v); // we are in Eye Coordinates, so EyePos is (0,0,0) vec3 R = normalize(-reflect(L,N)); //calculate Ambient Term: vec4 Iamb = gl_FrontLightProduct[i].ambient; //calculate Diffuse Term: vec4 Idiff = gl_FrontLightProduct[i].diffuse * max(dot(N,L), 0.0); Idiff = clamp(Idiff, 0.0, 1.0); // calculate Specular Term: vec4 Ispec = gl_FrontLightProduct[i].specular * pow(max(dot(R,E),0.0),0.3*gl_FrontMaterial.shininess); Ispec = clamp(Ispec, 0.0, 1.0); finalColor += Iamb + Idiff + Ispec; } // write Total Color: gl_FragColor = gl_FrontLightModelProduct.sceneColor + finalColor; } ''', gl.GL_FRAGMENT_SHADER), ) self.cube_list = Cube().getVerticeList() self.coord_list = Coord().getVerticeList() self.grid_list = Grid().getVerticeList() # fill later self.mesh_lists = {} # type: Dict[str, Any]
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