def draw_vr_anaglyph(cube_fbo, vr_scene, active_scene, eye_poses=(.035, -.035)): """ Experimental anaglyph drawing function for VR system with red/blue glasses, used in Sirota lab. Draws a virtual scene in red and blue, from subject's (heda trackers) perspective in active scene. Note: assumes shader uses playerPos like ratcave's default shader Args: cube_fbo: texture frameBuffer object. vr_scene: virtual scene object active_scene: active scene object eye_poses: the eye positions Returns: """ color_masks = [(True, False, False, True), (False, True, True, True)] cam = vr_scene.camera orig_cam_position = cam.position.xyz for color_mask, eye_pos in zip(color_masks, eye_poses): gl.glColorMask(*color_mask) cam.position.xyz = cam.model_matrix.dot( [eye_pos, 0., 0., 1.])[:3] # inter_eye_distance / 2. cam.uniforms['playerPos'] = cam.position.xyz with cube_fbo as fbo: vr_scene.draw360_to_texture(fbo.texture) cam.position.xyz = orig_cam_position active_scene.draw()
def draw(self, uniforms, entity, models, textures=(), color=(1.0, 1.0, 1.0), *args, **kwargs): glUseProgram(self) glDisable(GL_DEPTH_TEST) # Draw objects once as invisible to set stencil values. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE) # Don't write any color values to color buffer. glStencilMask(0xFF) # Enable writing. model = models[entity.model] model.positions.enable() model.indices.enable() self.uniforms[b'perspective'].load( uniforms.get(b'perspective').ctypes.data_as(POINTER(GLfloat))) self.uniforms[b'view'].load( uniforms.get(b'view').ctypes.data_as(POINTER(GLfloat))) self.uniforms[b'color'].load(*color) entity.get_transformation_matrix( location=self.uniforms[b'transformation']) model.draw() # Draw again with larger model. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) glStencilFunc(GL_NOTEQUAL, 1, 0xFF) # Update with 1's where the objects are rendered. glStencilMask( 0x00 ) # Value that AND's the value written to buffer. 0x00 basically disables writing to stencil. glUniformMatrix4fv( self.uniforms[b'transformation'], 1, GL_TRUE, create_transformation_matrix( *entity._location, *entity.rotation, *(entity.scale + 0.05)).ctypes.data_as(POINTER(GLfloat))) model.draw() glBindBuffer(GL_ARRAY_BUFFER, 0) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) glDisableVertexAttribArray(0) glEnable(GL_DEPTH_TEST) glStencilMask(0xFF)
def _startOfFlip(self): """Custom :py:class:`~Rift._startOfFlip` for HMD rendering. This finalizes the HMD texture before diverting drawing operations back to the on-screen window. This allows :py:class:`~Rift.flip` to swap the on-screen and HMD buffers when called. This function always returns `True`. Returns ------- True """ # nop if we are still setting up the window if not hasattr(self, '_eyeBuffers'): return True # direct draw being used, don't do FBO blit if self._directDraw: return True # Switch off multi-sampling # GL.glDisable(GL.GL_MULTISAMPLE) oldColor = self.color self.setColor((-1, -1, -1)) self.setBuffer('back', clear=True) self._prepareFBOrender() # need blit the framebuffer object to the actual back buffer # unbind the framebuffer as the render target GL.glDisable(GL.GL_BLEND) stencilOn = self.stencilTest self.stencilTest = False # before flipping need to copy the renderBuffer to the # frameBuffer GL.glActiveTexture(GL.GL_TEXTURE0) GL.glEnable(GL.GL_TEXTURE_2D) GL.glColor3f(1.0, 1.0, 1.0) # glColor multiplies with texture GL.glColorMask(True, True, True, True) # blit the textures to the back buffer for eye in ('left', 'right'): self._blitEyeBuffer(eye) GL.glEnable(GL.GL_BLEND) self._finishFBOrender() self.stencilTest = stencilOn self.setColor(oldColor) # This always returns True return True
def draw_rectangle(self, ellipse=False): loader = self.loader if loader.fillType == MOTIF_FILL: if ellipse: self.motif_batch.draw() else: glEnable(GL_STENCIL_TEST) glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE) glStencilFunc(GL_ALWAYS, 1, 1) glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE) self.motif_stencil.draw(GL_QUADS) glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) glStencilFunc(GL_EQUAL, 1, 1) self.motif_batch.draw() else: self.vertex.draw(GL_QUADS)
def on_draw(): window.clear() with shader: for x, mask in zip((-eye_dist / 2., eye_dist / 2.), [(True, False, False, True), (False, True, True, True)]): cam.position.x = x # cam.look_at(0, 0, np.mean([fg.position.z, -edge_dist])) # cam.look_at(0, 0, focus_dist) cam.look_at(0, 0, -0.35) # cam.look_at(0, 0, fg.position.z) # cam.look_at(0, 0, -edge_dist) with cam: gl.glColorMask(*mask) bg.draw() fg.draw() gl.glColorMask(True, True, True, True) fps_display.draw()
def on_draw(): ## Render virtual scene onto cube texture with framebuffer: with cube_shader: for mask, camside in zip([(True, False, False, True), (False, True, True, True)], [vr_camgroup.left, vr_camgroup.right]): gl.glColorMask(*mask) virtual_scene.camera.position.xyz = camside.position_global virtual_scene.draw360_to_texture(cube_texture) ## Render real scene onto screen gl.glColorMask(True, True, True, True) window.clear() with cube_shader: # usign cube shader to create the actuall 6 sided virtual cube which gets upated with position and angle of the camera/viewer rc.clear_color(255, 0, 0) # why is it here 39? e with projector, light: arena.draw()
def draw(self): glPushMatrix() glPushAttrib(GL_STENCIL_BUFFER_BIT) glClear(GL_STENCIL_BUFFER_BIT) glTranslatef(self.parent.x, self.parent.y, 0) glDisable(GL_TEXTURE_2D) loader = self.loader if loader.shape == LINE_SHAPE: glEnable(GL_LINE_SMOOTH) glEnable(GL_POINT_SMOOTH) glLineWidth(loader.borderSize) glPointSize(loader.borderSize) self.vertex.draw(GL_LINES) self.vertex.draw(GL_POINTS) elif loader.shape == RECTANGLE_SHAPE: width = self.parent.width height = self.parent.height if loader.fillType != NONE_FILL: # for counters if self.counter_stencil: glEnable(GL_STENCIL_TEST) glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE) glStencilFunc(GL_ALWAYS, 1, 1) glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE) self.counter_stencil.draw(GL_QUADS) glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) glStencilFunc(GL_EQUAL, 1, 1) self.draw_rectangle() glDisable(GL_STENCIL_TEST) else: self.draw_rectangle() if self.border is not None: self.border.draw(GL_QUADS) elif loader.shape == ELLIPSE_SHAPE: if loader.fillType != NONE_FILL: glEnable(GL_STENCIL_TEST) glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE) glStencilFunc(GL_ALWAYS, 1, 1) glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE) self.stencil.draw(GL_TRIANGLES) glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) glStencilFunc(GL_EQUAL, 1, 1) self.draw_rectangle(True) glDisable(GL_STENCIL_TEST) if self.border: self.border.draw(GL_QUADS) glPopAttrib() glPopMatrix()
def enable(self): gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT | gl.GL_STENCIL_BUFFER_BIT) gl.glDisable(gl.GL_DEPTH_TEST) gl.glColorMask(gl.GL_FALSE, gl.GL_FALSE, gl.GL_FALSE, gl.GL_FALSE) gl.glEnable(gl.GL_STENCIL_TEST) gl.glStencilOp(gl.GL_REPLACE, gl.GL_REPLACE, gl.GL_REPLACE) gl.glStencilFunc(gl.GL_ALWAYS, 1, 0xffffffff) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(Blend.SRC_ALPHA, Blend.ONE_MINUS_SRC_ALPHA) self.ground.draw() gl.glColorMask(gl.GL_TRUE, gl.GL_TRUE, gl.GL_TRUE, gl.GL_TRUE) gl.glEnable(gl.GL_DEPTH_TEST) gl.glStencilFunc(gl.GL_EQUAL, 1, 0xffffffff) gl.glStencilOp(gl.GL_KEEP, gl.GL_KEEP, gl.GL_KEEP) gl.glPushMatrix() gl.glCullFace(gl.GL_FRONT) gl.glEnable(gl.GL_NORMALIZE) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(Blend.SRC_ALPHA, Blend.ONE_MINUS_SRC_ALPHA) self.camera.render(lights=self.lights, effect_pass=1, before_render=self._before_visible) gl.glDisable(gl.GL_NORMALIZE) gl.glCullFace(gl.GL_BACK) gl.glPopMatrix() gl.glDisable(gl.GL_STENCIL_TEST) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(Blend.SRC_ALPHA, Blend.ONE_MINUS_SRC_ALPHA) self.ground.draw()
def draw_anaglyph(self, clear=True, inter_eye_distance=.08): cam = self.camera orig_cam_position = cam.uniforms['view_matrix'][0, 3] gl.glColorMask(True, False, False, True) cam.uniforms['view_matrix'][0, 3] += inter_eye_distance / 2. self.draw(clear=clear) gl.glClear(gl.GL_DEPTH_BUFFER_BIT) gl.glColorMask(False, True, True, True) cam.uniforms['view_matrix'][0, 3] -= inter_eye_distance self.draw(clear=clear) gl.glColorMask(True, True, True, True) cam.uniforms['view_matrix'][0, 3] = orig_cam_position
def on_draw(): window.clear() with rc.default_shader: cam.position.x = .02 with cam: gl.glColorMask(True, False, False, True) fg_mesh.draw() blocker1.draw() blocker2.draw() bg_mesh.draw() cam.position.x = -.02 with cam: gl.glColorMask(False, False, True, True) fg_mesh.draw() blocker1.draw() blocker2.draw() bg_mesh.draw() gl.glColorMask(True, True, True, True) fps_display.draw()
def unset_state(self): glTranslatef(-self.focusX, -self.focusY, 0) if self.visible == False: glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE)
def set_state(self): glTranslatef(self.focusX, self.focusY, 0) if self.visible == False: glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE)
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]) )