def initialize(self): """ Set up the viewport. """ x, y = self.viewSize # Hide things that are behind other things glEnable(GL_DEPTH_TEST) # Create the OpenGL viewport, setting the size of the window (in pixels) # and defining how the scene is projected onto it. glViewport(0, 0, x, y) glMatrixMode(GL_PROJECTION) glLoadIdentity() # Field of view, aspect ratio, near clipping, far clipping gluPerspective(45.0, x / y, 0.5, 1000.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() # This makes material color properties be defined by glColor calls, # necessary to get the right colors when lighting is enabled. glEnable(GL_COLOR_MATERIAL) # This might be desirable at some point, who knows. # glColorMaterial(GL_FRONT_AND_BACK, GL_EMISSION) # Make lighting work, because we like lights. glEnable(GL_LIGHTING) # Make textures work too. glEnable(GL_TEXTURE_2D)
def _init_GL(self): ''' ''' glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glEnable(GL_BLEND) glEnable(GL_LINE_SMOOTH) glLineWidth(2.0) glEnable(GL_DEPTH_TEST) # glEnable(GL_TEXTURE_2D) # set the lighting self._set_lighting() # set the background color # glClearColor(0.15, 0.15, 0.15, 1) glClearDepth(1.0) # set the camera glMatrixMode(GL_PROJECTION) # glPushMatrix() glLoadIdentity() self._set_view_volume() glMatrixMode(GL_MODELVIEW) return
def _setup_lighting(self): """ [private method] Set up lighting in the model. [Called from both initializeGL and paintGL.] """ # note: there is some duplicated code in this method # in GLPane_lighting_methods (has more comments) and ThumbView, # but also significant differences. Should refactor sometime. # [bruce 060415/080912 comment] glEnable(GL_NORMALIZE) glMatrixMode(GL_PROJECTION) glLoadIdentity() glMatrixMode(GL_MODELVIEW) glLoadIdentity() #bruce 060415 moved following from ThumbView.initializeGL to this split-out method... #bruce 051212 revised lighting code to share prefs and common code with GLPane # (to fix bug 1200 and mitigate bugs 475 and 1158; # fully fixing those would require updating lighting in all ThumbView widgets # whenever lighting prefs change, including making .update calls on them, # and is not planned for near future since it's easy enough to close & reopen them) try: lights = self.shareWidget._lights #bruce 060415 shareWidget --> self.shareWidget; presumably always failed before that ###@@@ will this fix some bugs about common lighting prefs?? except: lights = _default_lights setup_standard_lights(lights, self.glprefs) return
def main(): moons = [Moon(pos) for pos in d12_input] glutInit(sys.argv) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) glutInitWindowSize(800, 800) glutInitWindowPosition(350, 200) glutCreateWindow('name') glClearColor(0., 0., 0., 1.) glShadeModel(GL_SMOOTH) glEnable(GL_CULL_FACE) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) lightZeroPosition = [10., 4., 10., 1.] lightZeroColor = [0.8, 1.0, 0.8, 1.0] glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition) glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor) glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1) glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05) glEnable(GL_LIGHT0) glutDisplayFunc(lambda: display_scene(moons)) glutTimerFunc(0, timer, 0) glMatrixMode(GL_PROJECTION) gluPerspective(40., 1., 1., 40.) glMatrixMode(GL_MODELVIEW) gluLookAt(0, -10, 10, 0, 0, 0, 0, 1, 0) glPushMatrix() glutMainLoop()
def get_ray(self, x, y): """ Generate a ray beginning at the near plane, in the direction that the x, y coordinates are facing Consumes: x, y coordinates of mouse on screen Return: start, direction of the ray """ self.init_view() glMatrixMode(GL_MODELVIEW) glLoadIdentity() # get two points on the line. start = numpy.array(gluUnProject(x, y, 0.001)) end = numpy.array(gluUnProject(x, y, 0.999)) # convert those points into a ray direction = end - start direction = direction / norm(direction) return (start, direction)
def InitGL(self, Width, Height): self.view_port_xr = 1 self.view_port_yr = 1 self.original_x = Width self.original_y = Height glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() glOrtho(0, Width, 0, Height, -1, 1) glScalef(1, -1, 1) glTranslatef(0, -Height, 0) glMatrixMode(GL_MODELVIEW) glDisable(GL_DEPTH_TEST) # Disables Depth Testing glShadeModel(GL_SMOOTH) # Enables Smooth Color Shading # Anti-aliasing/prettyness stuff glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glEnable(GL_BLEND) glHint(GL_LINE_SMOOTH_HINT, GL_NICEST) glHint(GL_POINT_SMOOTH_HINT, GL_NICEST) glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST) glEnable(GL_LINE_SMOOTH) glEnable(GL_POINT_SMOOTH) glEnable(GL_POLYGON_SMOOTH) glClearColor(background_color()[0], background_color()[1], background_color()[2], 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
def _setup_lighting(self): """ [private method] Set up lighting in the model (according to self._lights). [Called from both initializeGL and paintGL.] """ # note: there is some duplicated code in this method # in GLPane_lighting_methods (has more comments) and ThumbView, # but also significant differences. Should refactor sometime. # [bruce 060415/080912 comment] glEnable(GL_NORMALIZE) # bruce comment 050311: I don't know if this relates to lighting or not # grantham 20051121: Yes, if NORMALIZE is not enabled (and normals # aren't unit length or the modelview matrix isn't just rotation) # then the lighting equation can produce unexpected results. #bruce 050413 try to fix bug 507 in direction of lighting: ##k might be partly redundant now; not sure whether projection matrix needs to be modified here [bruce 051212] glMatrixMode(GL_PROJECTION) glLoadIdentity() glMatrixMode(GL_MODELVIEW) glLoadIdentity() #bruce 051212 moved most code from this method into new function, setup_standard_lights setup_standard_lights(self._lights, self.glprefs) # record what glprefs data was used by that, for comparison to see when we need to call it again # (not needed for _lights since another system tells us when that changes) self._last_glprefs_data_used_by_lights = glprefs_data_used_by_setup_standard_lights( self.glprefs) return
def render(self): """ The render pass for the scene """ self.init_view() # Enable lighting and color glEnable(GL_LIGHTING) glClearColor(0.4, 0.4, 0.4, 0.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Load the modelview matrix from the current state of the trackball glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() loc = self.interaction.translation glTranslated(-loc[0], -loc[1], -loc[2]) glMultMatrixf(self.interaction.trackball.matrix) # store the inverse of the current modelview. currentModelView = numpy.array(glGetFloatv(GL_MODELVIEW_MATRIX)) self.modelView = numpy.transpose(currentModelView) self.inverseModelView = inv(numpy.transpose(currentModelView)) # render the scene. This will call the render function for each object in the scene self.scene.render() # draw the grid glDisable(GL_LIGHTING) glCallList(G_OBJ_PLANE) glPopMatrix() # flush the buffers so that the scene can be drawn glFlush()
def draw_circle( self, circle_center, circle_normal, circle_radius, color=RGBA(1.1, 0.2, 0.8), num_segments=20, ): vertices = [] vertices.append((0, 0, 0)) # circle center # create circle vertices in the xy plane for i in np.linspace(0.0, 2.0 * math.pi, num_segments): x = math.sin(i) y = math.cos(i) z = 0 vertices.append((x, y, z)) glPushMatrix() glMatrixMode(GL_MODELVIEW) glLoadMatrixf( self.get_pupil_transformation_matrix(circle_normal, circle_center, circle_radius)) glutils.draw_polyline((vertices), color=color, line_type=GL_TRIANGLE_FAN) # circle glutils.draw_polyline([(0, 0, 0), (0, 0, 4)], color=RGBA(0, 0, 0), line_type=GL_LINES) # normal glPopMatrix()
def reshape_function(self, w, h): # prevent dividing by zero if window height is 0 if h == 0: h = 1 self.window_width = w self.window_height = h proportional_rate = float(w) / h glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60.0, proportional_rate, 0.1, 100) camera_settings_matrix = [0 for i in range(9)] # calculate camera's point of view camera_settings_matrix = self.recalculate_camera_settings( camera_settings_matrix) # set camera's point of view gluLookAt(camera_settings_matrix[0], camera_settings_matrix[1], camera_settings_matrix[2], camera_settings_matrix[3], camera_settings_matrix[4], camera_settings_matrix[5], camera_settings_matrix[6], camera_settings_matrix[7], camera_settings_matrix[8]) glScalef(self.scale_factor, self.scale_factor, self.scale_factor) glMatrixMode(GL_MODELVIEW)
def compile_total_list(self): if self.total_list is not None: glNewList(self.total_list, GL_COMPILE) # reset a few things glDisable(GL_DEPTH_TEST) glDisable(GL_LIGHTING) glMatrixMode(GL_PROJECTION) glLoadIdentity() glMatrixMode(GL_MODELVIEW) glLoadIdentity() # change the modelview to camera coordinates viewport = glGetIntegerv(GL_VIEWPORT) width = viewport[2] - viewport[0] height = viewport[3] - viewport[1] if width > height: w = float(width) / float(height) h = 1.0 else: w = 1.0 h = float(height) / float(width) glScalef(2 / w, 2 / h, 1.0) glCallList(self.draw_list) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) glEndList()
def resizeGL(self, width, height): glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() aspect = width / height gluPerspective(60.0, aspect, 1, 400) glMatrixMode(GL_MODELVIEW)
def drawWheels(): global seconds wheelXOff = 2 wheelZOff = 2 wheelSpreed = 10 glMatrixMode(GL_MODELVIEW) glPushMatrix() glTranslated(-wheelXOff, 0, wheelZOff + seconds - 10) glRotated(seconds * wheelSpreed, 1, 0, 0) glRotated(90, 0, 1, 0) drawTire() glPopMatrix() glPushMatrix() glTranslated(wheelXOff, 0, wheelZOff + seconds - 10) glRotated(seconds * wheelSpreed, 1, 0, 0) glRotated(90, 0, 1, 0) drawTire() glPopMatrix() glPushMatrix() glTranslated(-wheelXOff, 0, -wheelZOff + seconds - 10) glRotated(seconds * wheelSpreed, 1, 0, 0) glRotated(90, 0, 1, 0) drawTire() glPopMatrix() glPushMatrix() glTranslated(wheelXOff, 0, -wheelZOff + seconds - 10) glRotated(seconds * wheelSpreed, 1, 0, 0) glRotated(90, 0, 1, 0) drawTire() glPopMatrix()
def compile_total_list(self): if self.total_list is not None: glNewList(self.total_list, GL_COMPILE) # reset a few things glDisable(GL_DEPTH_TEST) glDisable(GL_LIGHTING) glMatrixMode(GL_PROJECTION) glLoadIdentity() glMatrixMode(GL_MODELVIEW) glLoadIdentity() # change the modelview to camera coordinates viewport = glGetIntegerv(GL_VIEWPORT) width = viewport[2] - viewport[0] height = viewport[3] - viewport[1] if width > height: w = float(width) / float(height) h = 1.0 else: w = 1.0 h = float(height) / float(width) glScalef(2/w, 2/h, 1.0) glCallList(self.draw_list) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) glEndList()
def render(self): #init shadow matrix self.init_view() #open light glEnable(GL_LIGHTING) #clear color and depth caches glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) #set model view matrix(danwei matrix is ok) #set trackball's rotate matrix as Modelview glMatrixMode(GL_MODELVIEW) glPushMatrix() #replace now-matrix with hengdeng matrix glLoadIdentity() glMultMatrixf(self.interaction.trackball.matrix) #save Modelview matrix, later change system with anti-matrix currentModelView = numpy.array(glGetFloatv(GL_MODELVIEW_MATRIX)) self.modelView = numpy.transpose(currentModelView) self.inverseModelView = inv(numpy.transpose(currentModelView)) #rend the scene self.scene.render() #after each shadow , huifu light state glDisable(GL_LIGHTING) glPopMatrix() glFlush()
def reshape(self, width, height): glClearColor(0.0, 0.0, 0.0, 1.0) glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60, width / height, 1.0, 10000.0) glMatrixMode(GL_MODELVIEW)
def _setup_lighting(self): """ [private method] Set up lighting in the model. [Called from both initializeGL and paintGL.] """ # note: there is some duplicated code in this method # in GLPane_lighting_methods (has more comments) and ThumbView, # but also significant differences. Should refactor sometime. # [bruce 060415/080912 comment] glEnable(GL_NORMALIZE) glMatrixMode(GL_PROJECTION) glLoadIdentity() glMatrixMode(GL_MODELVIEW) glLoadIdentity() #bruce 060415 moved following from ThumbView.initializeGL to this split-out method... #bruce 051212 revised lighting code to share prefs and common code with GLPane # (to fix bug 1200 and mitigate bugs 475 and 1158; # fully fixing those would require updating lighting in all ThumbView widgets # whenever lighting prefs change, including making .update calls on them, # and is not planned for near future since it's easy enough to close & reopen them) try: lights = self.shareWidget._lights #bruce 060415 shareWidget --> self.shareWidget; presumably always failed before that ###@@@ will this fix some bugs about common lighting prefs?? except: lights = _default_lights setup_standard_lights( lights, self.glprefs) return
def do_configure_event(self, event): ClientWindow.do_configure_event(self, event) drawable = self.glarea.get_gl_drawable() context = self.glarea.get_gl_context() self.yuv420_shader = None # Re-create textures self.current_mode = GLClientWindow.MODE_UNINITIALIZED if not drawable.gl_begin(context): raise Exception("** Cannot create OpenGL rendering context!") w, h = self.get_size() log("Configure widget size: %d x %d" % (w, h)) glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0.0, w, h, 0.0, -1.0, 1.0) glMatrixMode(GL_MODELVIEW) glEnableClientState(GL_VERTEX_ARRAY) glEnableClientState(GL_TEXTURE_COORD_ARRAY) glDisable(GL_FRAGMENT_PROGRAM_ARB) if self.textures is None: self.textures = glGenTextures(3) drawable.gl_end()
def render(self): """ The render pass for the scene """ self.init_view() # Enable lighting and color glEnable(GL_LIGHTING) glClearColor(0.4, 0.4, 0.4, 0.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Load the modelview matrix from the current state of the trackball glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() loc = self.interaction.translation glTranslated(-loc[0], -loc[1], -loc[2]) glMultMatrixf(self.interaction.trackball.matrix) # store the inverse of the current modelview. currentModelView = numpy.array(glGetFloatv(GL_MODELVIEW_MATRIX)) self.modelView = numpy.transpose(currentModelView) self.inverseModelView = inv(numpy.transpose(currentModelView)) # render the scene. This will call the render function for each object in the scene self.scene.render() # draw the grid glDisable(GL_LIGHTING) glCallList(G_OBJ_PLANE) glPopMatrix() # flush the buffers so that the scene can be drawn glFlush()
def do_configure_event(self, event): ClientWindow.do_configure_event(self, event) drawable = self.glarea.get_gl_drawable() context = self.glarea.get_gl_context() self.yuv420_shader = None # Re-create textures self.current_mode = GLClientWindow.MODE_UNINITIALIZED if not drawable.gl_begin(context): raise Exception("** Cannot create OpenGL rendering context!") w, h = self.get_size() log("Configure widget size: %d x %d" % (w, h)) glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0.0, w, h, 0.0, -1.0, 1.0) glMatrixMode(GL_MODELVIEW) glEnableClientState(GL_VERTEX_ARRAY) glEnableClientState(GL_TEXTURE_COORD_ARRAY) glDisable(GL_FRAGMENT_PROGRAM_ARB) if self.textures is None: self.textures = glGenTextures(3) drawable.gl_end()
def _setup_lighting(self): """ [private method] Set up lighting in the model (according to self._lights). [Called from both initializeGL and paintGL.] """ # note: there is some duplicated code in this method # in GLPane_lighting_methods (has more comments) and ThumbView, # but also significant differences. Should refactor sometime. # [bruce 060415/080912 comment] glEnable(GL_NORMALIZE) # bruce comment 050311: I don't know if this relates to lighting or not # grantham 20051121: Yes, if NORMALIZE is not enabled (and normals # aren't unit length or the modelview matrix isn't just rotation) # then the lighting equation can produce unexpected results. #bruce 050413 try to fix bug 507 in direction of lighting: ##k might be partly redundant now; not sure whether projection matrix needs to be modified here [bruce 051212] glMatrixMode(GL_PROJECTION) glLoadIdentity() glMatrixMode(GL_MODELVIEW) glLoadIdentity() #bruce 051212 moved most code from this method into new function, setup_standard_lights setup_standard_lights( self._lights, self.glprefs) # record what glprefs data was used by that, for comparison to see when we need to call it again # (not needed for _lights since another system tells us when that changes) self._last_glprefs_data_used_by_lights = glprefs_data_used_by_setup_standard_lights( self.glprefs) return
def draw_colour_legend(self): menubar_height = self.logo.size[1] + 4 width = glutGet(GLUT_WINDOW_WIDTH) height = glutGet(GLUT_WINDOW_HEIGHT) # Colour legend left_x = width - 20 right_x = width - 10 min_y = menubar_height + 5 max_y = height - 20 time_step_size = math.ceil(self.max_hit_time / 20 / 50) * 50 hit_times = list(range(int(self.min_hit_time), int(self.max_hit_time), int(time_step_size))) if len(hit_times) > 1: segment_height = int((max_y - min_y) / len(hit_times)) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glDisable(GL_LIGHTING) glBegin(GL_QUADS) for hit_time in hit_times: segment_nr = hit_times.index(hit_time) glColor3f(*self.spectrum(hit_time)) glVertex2f(left_x, max_y - segment_height * segment_nr) glVertex2f(right_x, max_y - segment_height * segment_nr) glColor3f(*self.spectrum(hit_time + time_step_size)) glVertex2f(left_x, max_y - segment_height * (segment_nr + 1)) glVertex2f(right_x, max_y - segment_height * (segment_nr + 1)) glEnd() # Colour legend labels self.colourist.now_text() for hit_time in hit_times: segment_nr = hit_times.index(hit_time) draw_text_2d("{0:>5}ns".format(hit_time), width - 80, (height - max_y) + segment_height * segment_nr)
def SetupProjection(self, width = 640, height = 480, zNear = 5., zFar = 300.): """ Sets up the projection matrix for opengl. """ # set the projection transformation glMatrixMode(GL_PROJECTION) glLoadIdentity() #TODO: replace the glu call with something else. #gluPerspective(45.0, float(width) / height, scale * 50.0, scale * 1000.0) #gluPerspective(45.0, float(self.width) / float(self.height), 5.0, 1000.0) self.zNear = zNear self.zFar = zFar self.buffer_calc_a = self.zFar / ( self.zFar - self.zNear ) self.buffer_calc_b = self.zFar * self.zNear / ( self.zNear - self.zFar ) gluPerspective(45.0, float(width) / float(height), self.zNear, self.zFar) self.gl_projection_matrix = glGetFloatv(GL_PROJECTION_MATRIX) # set the model transformation glMatrixMode(GL_MODELVIEW)
def make_coord_system_eye_camera_based(window_size, focal_length): camera_fov = math.degrees(2.0 * math.atan(window_size[1] / (2.0 * focal_length))) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(camera_fov, float(window_size[0]) / window_size[1], 0.1, 2000.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def drawFilledCircle(color, center, radius, normal): """ Scale, rotate/translate the unit circle properly. Added a filled circle variant, piotr 080405 """ glMatrixMode(GL_MODELVIEW) glPushMatrix() glColor3fv(color) glDisable(GL_LIGHTING) glTranslatef(center[0], center[1], center[2]) rQ = Q(V(0, 0, 1), normal) rotAngle = rQ.angle*180.0/pi #This may cause problems as proved before in Linear motor display. #rotation around (0, 0, 0) #if vlen(V(rQ.x, rQ.y, rQ.z)) < 0.00005: # rQ.x = 1.0 glRotatef(rotAngle, rQ.x, rQ.y, rQ.z) glScalef(radius, radius, 1.0) glCallList(drawing_globals.filledCircleList) glEnable(GL_LIGHTING) glPopMatrix() return
def render(self): #初始化投影矩阵 self.init_view() #启动光照 glEnable(GL_LIGHTING) #清空颜色缓存与深度缓存 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) #设置模型视图矩阵,这节课先用单位矩阵就行了。 glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() glMultMatrixf(self.interaction.trackball.matrix) # 存储ModelView矩阵与其逆矩阵之后做坐标系转换用 currentModelView = numpy.array(glGetFloatv(GL_MODELVIEW_MATRIX)) self.modelView = numpy.transpose(currentModelView) self.inverseModelView = inv(numpy.transpose(currentModelView)) #渲染场景 self.scene.render() #每次渲染后复位光照状态 glDisable(GL_LIGHTING) glCallList(G_OBJ_PLANE) glPopMatrix() #把数据刷新到显存上 glFlush()
def drawFilledCircle(color, center, radius, normal): """ Scale, rotate/translate the unit circle properly. Added a filled circle variant, piotr 080405 """ glMatrixMode(GL_MODELVIEW) glPushMatrix() glColor3fv(color) glDisable(GL_LIGHTING) glTranslatef(center[0], center[1], center[2]) rQ = Q(V(0, 0, 1), normal) rotAngle = rQ.angle * 180.0 / pi #This may cause problems as proved before in Linear motor display. #rotation around (0, 0, 0) #if vlen(V(rQ.x, rQ.y, rQ.z)) < 0.00005: # rQ.x = 1.0 glRotatef(rotAngle, rQ.x, rQ.y, rQ.z) glScalef(radius, radius, 1.0) glCallList(drawing_globals.filledCircleList) glEnable(GL_LIGHTING) glPopMatrix() return
def keyboard(key, x, y): global mat, rot, animTrans, animRot if key == chr(27): import sys sys.exit(0) #Your Code Here if key == b'a': mat[2] = mat[2] - math.cos(math.radians(rot[0] + 90)) mat[0] = mat[0] + math.sin(math.radians(rot[0] + 90)) if key == b'd': mat[2] = mat[2] + math.cos(math.radians(rot[0] + 90)) mat[0] = mat[0] - math.sin(math.radians(rot[0] + 90)) if key == b'w': mat[2] = mat[2] + math.cos(math.radians(rot[0])) mat[0] = mat[0] - math.sin(math.radians(rot[0])) if key == b's': mat[2] = mat[2] - math.cos(math.radians(rot[0])) mat[0] = mat[0] + math.sin(math.radians(rot[0])) if key == b'q': rot[0] = rot[0] - 1 if key == b'e': rot[0] = rot[0] + 1 if key == b'r': mat[1] = mat[1] - 1 if key == b'f': mat[1] = mat[1] + 1 if key == b'h': mat = [0, -5, -35] rot = [0, 0, 0] animTrans = [0, 0, 0] animRot = [0, 0, 0] if key == b'o': glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(-50, 50, -50, 50, .1, 50) #glOrtho() if key == b'p': glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(50, DISPLAY_HEIGHT / DISPLAY_WIDTH, .1, 200) if key == b'z': import sys sys.exit(0) glutPostRedisplay()
class GLPixmapBacking(PixmapBacking): def __init__(self, wid, w, h, old_backing, mmap_enabled, mmap): Backing.__init__(self, wid, mmap_enabled, mmap) display_mode = (gtk.gdkgl.MODE_RGB | gtk.gdkgl.MODE_SINGLE) # We use single buffer because double doesn't work, figure out why try: self.glconfig = gtk.gdkgl.Config(mode=display_mode) except gtk.gdkgl.NoMatches, e: raise Exception("could not find matching mode for %s: %s" % (display_mode, e)) self._backing = gtk.gdkgl.ext(gdk.Pixmap(gdk.get_default_root_window(), w, h)) log.info("Creating GL pixmap size %d %d " % (w, h)) self.gldrawable = self._backing.set_gl_capability(self.glconfig) log.info("drawable ok") # Then create an indirect OpenGL rendering context. self.glcontext = gtk.gdkgl.Context(self.gldrawable, direct=True) log.info("context ok") if not self.glcontext: raise Exception("** Cannot create OpenGL rendering context!") log.info("OpenGL rendering context is created.") self.texture = None self.textures = [ 0 ] self.use_openGL_CSC = True self.yuv420_shader = None # OpenGL begin if not self.gldrawable.gl_begin(self.glcontext): return False glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0.0, w, h, 0.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW) glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glPixelStorei(GL_UNPACK_ALIGNMENT, 1) self.gldrawable.gl_end() cr = self._backing.cairo_create() if old_backing is not None and old_backing._backing is not None: # Really we should respect bit-gravity here but... meh. cr.set_operator(cairo.OPERATOR_SOURCE) cr.set_source_pixmap(old_backing._backing, 0, 0) cr.paint() old_w, old_h = old_backing._backing.get_size() cr.move_to(old_w, 0) cr.line_to(w, 0) cr.line_to(w, h) cr.line_to(0, h) cr.line_to(0, old_h) cr.line_to(old_w, old_h) cr.close_path() else: cr.rectangle(0, 0, w, h) cr.set_source_rgb(1, 1, 1) cr.fill()
def reshape(self, width, height): r = float(width) / float(height) glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60.0, r, 1., 50.) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def _set_view(self): log.info("_set_view()") glMatrixMode(GL_PROJECTION) glLoadIdentity() w, h = self.size glOrtho(0.0, w, h, 0.0, -1.0, 1.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def enable(self): # setup projection and model view matrix glMatrixMode (GL_PROJECTION) glLoadMatrixf (self.projectionCropMatrix) glMatrixMode (GL_MODELVIEW) glLoadMatrixf (self.modelViewMatrix) self._projectionUpdatedThisFrame = False
def initGL(self): """ initialize GL """ glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(self.clipping_area[0], self.clipping_area[1], self.clipping_area[2], self.clipping_area[3])
def _set_view(self): log.info("_set_view()") glMatrixMode(GL_PROJECTION) glLoadIdentity() w, h = self.size glOrtho(0.0, w, h, 0.0, -1.0, 1.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def setupViewport(self, width, height): side = min(width, height) glViewport((width - side) // 2, (height - side) // 2, side, side) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0) glMatrixMode(GL_MODELVIEW)
def setupViewport(self, width, height): side = min(width, height) glViewport((width - side) // 2, (height - side) // 2, side, side) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0) glMatrixMode(GL_MODELVIEW)
def render(self): glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glMatrixMode(GL_MODELVIEW) glPushMatrix() glTranslated(self.center[0], self.center[1], self.center[2]) glCallList(G_OBJ_CUBE) glPopMatrix() glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
def resizeGL(self, width, height): glViewport(0, 0, width, height if height else 1) glMatrixMode(GL_PROJECTION) glLoadIdentity() self.aspect = width / float(height) gluPerspective(45., self.aspect, 1., 100.) glMatrixMode(GL_MODELVIEW)
def viewport_changed(self): """This method is called when the view's size has changed, with the view's OpenGL context as the current context, and the OpenGL viewport set to (0, 0, width, height). The default implementation loads an identity projection matrix and calls init_projection().""" glMatrixMode(GL_PROJECTION) glLoadIdentity() self.init_projection()
def viewport_changed(self): """This method is called when the view's size has changed, with the view's OpenGL context as the current context, and the OpenGL viewport set to (0, 0, width, height). The default implementation loads an identity projection matrix and calls init_projection().""" glMatrixMode(GL_PROJECTION) glLoadIdentity() self.init_projection()
def draw(self): # this code is modified from GLPane.drawcompass glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() glMatrixMode( GL_PROJECTION ) # WARNING: we're now in nonstandard matrixmode (for sake of gluPickMatrix and glOrtho -- needed??##k) glPushMatrix() glLoadIdentity() try: glpane = self.env.glpane ## aspect = 1.0 ###WRONG -- should use glpane.aspect (which exists as of 070919) aspect = glpane.aspect # revised by bruce 070919, UNTESTED corner = self.corner delegate = self.delegate ###e should get glpane to do this for us (ie call a method in it to do this if necessary) # (this code is copied from it) glselect = glpane.current_glselect if glselect: # print "%r (ipath %r) setting up gluPickMatrix" % (self, self.ipath) x, y, w, h = glselect gluPickMatrix( x, y, w, h, glGetIntegerv(GL_VIEWPORT) # k is this arg needed? it might be the default... ) # the first three cases here are still ###WRONG if corner == UPPER_RIGHT: glOrtho(-50 * aspect, 5.5 * aspect, -50, 5.5, -5, 500) # Upper Right elif corner == UPPER_LEFT: glOrtho(-5 * aspect, 50.5 * aspect, -50, 5.5, -5, 500) # Upper Left elif corner == LOWER_LEFT: glOrtho(-5 * aspect, 50.5 * aspect, -5, 50.5, -5, 500) # Lower Left else: ## glOrtho(-50*aspect, 5.5*aspect, -5, 50.5, -5, 500) # Lower Right ## glOrtho(-50*aspect, 0, 0, 50, -5, 500) # Lower Right [used now] -- x from -50*aspect to 0, y (bot to top) from 0 to 50 glOrtho(-glpane.width * PIXELS, 0, 0, glpane.height * PIXELS, -5, 500) # approximately right for the checkbox, but I ought to count pixels to be sure (note, PIXELS is a pretty inexact number) glMatrixMode(GL_MODELVIEW) ###k guess 061210 at possible bugfix (and obviously needed in general) -- # do this last to leave the matrixmode standard # (status of bugs & fixes unclear -- hard to test since even Highlightable(projection=True) w/o any change to # projection matrix (test _9cx) doesn't work!) offset = (-delegate.bright, delegate.bbottom) # only correct for LOWER_RIGHT glTranslatef(offset[0], offset[1], 0) self.drawkid(delegate) ## delegate.draw() finally: glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) # be sure to do this last, to leave the matrixmode standard glPopMatrix() return
def gl_init(self): drawable = self.gl_begin() w, h = self.size debug("%s.gl_init() GL Pixmap backing size: %d x %d, drawable=%s", self, w, h, drawable) if not drawable: return None if not self.debug_setup: self.debug_setup = True self.gl_init_debug() if not self.gl_setup: self.gl_marker("Initializing GL context for window size %d x %d" % (w, h)) # Initialize viewport and matrices for 2D rendering glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0.0, w, h, 0.0, -1.0, 1.0) glMatrixMode(GL_MODELVIEW) # Could be more optimal to use vertex arrays: # glEnableClientState(GL_VERTEX_ARRAY) # glEnableClientState(GL_TEXTURE_COORD_ARRAY) # Clear background to transparent black glClearColor(0.0, 0.0, 0.0, 0.0) # Default state is good for YUV painting: # - fragment program enabled # - YUV fragment program bound # - render to offscreen FBO if self.textures is None: self.gl_init_textures() # Define empty FBO texture and set rendering to FBO glEnable(GL_FRAGMENT_PROGRAM_ARB) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO]) # nvidia needs this even though we don't use mipmaps (repeated through this file): glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0) glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, self.texture_pixel_format, w, h, 0, self.texture_pixel_format, GL_UNSIGNED_BYTE, None) glBindFramebuffer(GL_FRAMEBUFFER, self.offscreen_fbo) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO], 0) glClear(GL_COLOR_BUFFER_BIT) # Create and assign fragment programs if not self.shaders: self.gl_init_shaders() # Bind program 0 for YUV painting by default glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, self.shaders[YUV2RGB_SHADER]) self.gl_setup = True return drawable
def gl_init(self): drawable = self.gl_begin() w, h = self.size debug("GL Pixmap backing size: %d x %d, drawable=%s", w, h, drawable) if not drawable: return None if not self.gl_setup: # Ask GL to send us all debug messages if GL_DEBUG_OUTPUT and gl_debug_callback and glInitDebugKHR() == True: glEnable(GL_DEBUG_OUTPUT) glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS) glDebugMessageCallback(gl_debug_callback, None) glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, None, GL_TRUE) # Initialize string_marker GL debugging extension if available if glInitStringMarkerGREMEDY and glInitStringMarkerGREMEDY() == True: log.info("Extension GL_GREMEDY_string_marker available. Will output detailed information about each frame.") else: # General case - running without debugger, extension not available glStringMarkerGREMEDY = None # Initialize frame_terminator GL debugging extension if available if glInitFrameTerminatorGREMEDY and glInitFrameTerminatorGREMEDY() == True: glFrameTerminatorGREMEDY = None self.gl_marker("Initializing GL context for window size %d x %d" % (w, h)) # Initialize viewport and matrices for 2D rendering glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0.0, w, h, 0.0, -1.0, 1.0) glMatrixMode(GL_MODELVIEW) #TODO glEnableClientState(GL_VERTEX_ARRAY) #TODO glEnableClientState(GL_TEXTURE_COORD_ARRAY) # Clear to white glClearColor(1.0, 1.0, 1.0, 1.0) # Default state is good for YUV painting: # - fragment program enabled # - render to offscreen FBO glEnable(GL_FRAGMENT_PROGRAM_ARB) if self.textures is None: self.textures = glGenTextures(5) debug("textures for wid=%s of size %s : %s", self.wid, self.size, self.textures) if self.offscreen_fbo is None: self.offscreen_fbo = glGenFramebuffers(1) # Define empty FBO texture and set rendering to FBO glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO]) glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0) glBindFramebuffer(GL_FRAMEBUFFER, self.offscreen_fbo) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO], 0) glClear(GL_COLOR_BUFFER_BIT) self.gl_setup = True return drawable
def draw(self): # this code is modified from GLPane.drawcompass glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() glMatrixMode(GL_PROJECTION) # WARNING: we're now in nonstandard matrixmode (for sake of gluPickMatrix and glOrtho -- needed??##k) glPushMatrix() glLoadIdentity() try: glpane = self.env.glpane ## aspect = 1.0 ###WRONG -- should use glpane.aspect (which exists as of 070919) aspect = glpane.aspect # revised by bruce 070919, UNTESTED corner = self.corner delegate = self.delegate ###e should get glpane to do this for us (ie call a method in it to do this if necessary) # (this code is copied from it) glselect = glpane.current_glselect if glselect: # print "%r (ipath %r) setting up gluPickMatrix" % (self, self.ipath) x,y,w,h = glselect gluPickMatrix( x,y, w,h, glGetIntegerv( GL_VIEWPORT ) #k is this arg needed? it might be the default... ) # the first three cases here are still ###WRONG if corner == UPPER_RIGHT: glOrtho(-50*aspect, 5.5*aspect, -50, 5.5, -5, 500) # Upper Right elif corner == UPPER_LEFT: glOrtho(-5*aspect, 50.5*aspect, -50, 5.5, -5, 500) # Upper Left elif corner == LOWER_LEFT: glOrtho(-5*aspect, 50.5*aspect, -5, 50.5, -5, 500) # Lower Left else: ## glOrtho(-50*aspect, 5.5*aspect, -5, 50.5, -5, 500) # Lower Right ## glOrtho(-50*aspect, 0, 0, 50, -5, 500) # Lower Right [used now] -- x from -50*aspect to 0, y (bot to top) from 0 to 50 glOrtho(-glpane.width * PIXELS, 0, 0, glpane.height * PIXELS, -5, 500) # approximately right for the checkbox, but I ought to count pixels to be sure (note, PIXELS is a pretty inexact number) glMatrixMode(GL_MODELVIEW) ###k guess 061210 at possible bugfix (and obviously needed in general) -- # do this last to leave the matrixmode standard # (status of bugs & fixes unclear -- hard to test since even Highlightable(projection=True) w/o any change to # projection matrix (test _9cx) doesn't work!) offset = (-delegate.bright, delegate.bbottom) # only correct for LOWER_RIGHT glTranslatef(offset[0], offset[1], 0) self.drawkid( delegate) ## delegate.draw() finally: glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) # be sure to do this last, to leave the matrixmode standard glPopMatrix() return
def render(self): """ render the AABB. This can be useful for debugging purposes """ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glMatrixMode(GL_MODELVIEW) glPushMatrix() glTranslated(self.center[0], self.center[1], self.center[2]) glCallList(G_OBJ_CUBE) glPopMatrix() glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
def draw_line(x1, y1, x2, y2): glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0, 730, 0, 650, 0, 1) glMatrixMode(GL_MODELVIEW) glBegin(GL_LINES) glVertex2f(x1, y1) glVertex2f(x2, y2) glEnd()
def render(self): glMatrixMode(GL_MODELVIEW) glLoadIdentity() glRotate(self.pitch, 1, 0, 0) glRotate(self.yaw, 0, 1, 0) x, y, z = self.position glTranslate(-x, -y, -z)
def render(self): """ 渲染显示包围盒,可在调试的时候使用 """ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glMatrixMode(GL_MODELVIEW) glPushMatrix() glTranslated(self.center[0], self.center[1], self.center[2]) glCallList(G_OBJ_CUBE) glPopMatrix() glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
def resizeGL(self, width, height): if height == 0: height = 1 glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45.0, float(width) / float(height), 0.1, 100.0) glMatrixMode(GL_MODELVIEW)
def resizeGL(self, width, height): if height == 0: height = 1 glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45.0, float(width)/float(height), 0.1, 100.0) glMatrixMode(GL_MODELVIEW)
def translate(self, trans): # translate the object self.makeCurrent() glMatrixMode(GL_MODELVIEW) glLoadIdentity() glTranslated(trans[0], trans[1], trans[2]) glMultMatrixd(self._modelview_matrix) # update _modelview_matrix self._modelview_matrix = glGetDoublev(GL_MODELVIEW_MATRIX)
def resize(self): pygame.display.set_mode((self.W, self.H), pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0, self.W, self.H, 0) glViewport(0, 0, self.W, self.H) # gluOrtho2D(self.L * (self.W / self.H), self.R * (self.W / self.H), self.B, self.T) # gluOrtho2D(self.L, self.R, self.B, self.T) pygame.display.flip()
def gl_init(self): drawable = self.gl_begin() w, h = self.size log("%s.gl_init() GL Pixmap backing size: %d x %d, drawable=%s", self, w, h, drawable) if not drawable: return None if not self.debug_setup: self.debug_setup = True self.gl_init_debug() if not self.gl_setup: self.gl_marker("Initializing GL context for window size %d x %d" % (w, h)) # Initialize viewport and matrices for 2D rendering glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0.0, w, h, 0.0, -1.0, 1.0) glMatrixMode(GL_MODELVIEW) # Could be more optimal to use vertex arrays: # glEnableClientState(GL_VERTEX_ARRAY) # glEnableClientState(GL_TEXTURE_COORD_ARRAY) # Clear background to transparent black glClearColor(0.0, 0.0, 0.0, 0.0) # we don't use the depth (2D only): glDisable(GL_DEPTH_TEST) # only do alpha blending in present_fbo: glDisable(GL_BLEND) # Default state is good for YUV painting: # - fragment program enabled # - YUV fragment program bound # - render to offscreen FBO if self.textures is None: self.gl_init_textures() # Define empty FBO texture and set rendering to FBO glEnable(GL_FRAGMENT_PROGRAM_ARB) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO]) # nvidia needs this even though we don't use mipmaps (repeated through this file): glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0) glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, self.texture_pixel_format, w, h, 0, self.texture_pixel_format, GL_UNSIGNED_BYTE, None) glBindFramebuffer(GL_FRAMEBUFFER, self.offscreen_fbo) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO], 0) glClear(GL_COLOR_BUFFER_BIT) # Create and assign fragment programs if not self.shaders: self.gl_init_shaders() # Bind program 0 for YUV painting by default glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, self.shaders[YUV2RGB_SHADER]) self.gl_setup = True return drawable
def gl_init(self): #must be called within a context! #performs init if needed if not self.debug_setup: self.debug_setup = True self.gl_init_debug() if not self.gl_setup: w, h = self.size self.gl_marker("Initializing GL context for window size %d x %d", w, h) # Initialize viewport and matrices for 2D rendering glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0.0, w, h, 0.0, -1.0, 1.0) glMatrixMode(GL_MODELVIEW) # Mesa docs claim: this hint can improve the speed of texturing #when perspective-correct texture coordinate interpolation isn't needed, #such as when using a glOrtho() projection: glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST) # Could be more optimal to use vertex arrays: # glEnableClientState(GL_VERTEX_ARRAY) # glEnableClientState(GL_TEXTURE_COORD_ARRAY) # Clear background to transparent black glClearColor(0.0, 0.0, 0.0, 0.0) # we don't use the depth (2D only): glDisable(GL_DEPTH_TEST) # only do alpha blending in present_fbo: glDisable(GL_BLEND) # Default state is good for YUV painting: # - fragment program enabled # - YUV fragment program bound # - render to offscreen FBO if self.textures is None: self.gl_init_textures() # Define empty FBO texture and set rendering to FBO glEnable(GL_FRAGMENT_PROGRAM_ARB) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO]) # nvidia needs this even though we don't use mipmaps (repeated through this file): glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0) glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, self.texture_pixel_format, w, h, 0, self.texture_pixel_format, GL_UNSIGNED_BYTE, None) glBindFramebuffer(GL_FRAMEBUFFER, self.offscreen_fbo) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO], 0) glClear(GL_COLOR_BUFFER_BIT) # Create and assign fragment programs if not self.shaders: self.gl_init_shaders() # Bind program 0 for YUV painting by default glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, self.shaders[YUV2RGB_SHADER]) self.gl_setup = True
def set_projection(self, near, far, fovy): self._near = near self._far = far self._fovy = fovy self.makeCurrent() glMatrixMode(GL_PROJECTION) glLoadIdentity() height = max(self.height(), 1) gluPerspective(self._fovy, float(self.width()) / float(height), self._near, self._far) self.updateGL()
def initializeGL(self): glClearColor(*self.color_background) glClearDepth(1.0) glDepthFunc(GL_LESS) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) glMatrixMode(GL_PROJECTION) glLoadIdentity() glMatrixMode(GL_MODELVIEW)
def reshape(w, h): glViewport(0, 0, w, h) ratio = float(w) / h glMatrixMode(GL_PROJECTION) glLoadIdentity() viewport_center = HEIGHT * ratio / 2 photo_center = HEIGHT * mosaic_factory.ratio / 2 left = photo_center - viewport_center right = photo_center + viewport_center glOrtho(left, right, 0.0, HEIGHT, -1.0, 1.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def draw_rect(x, y, width, height): glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0, 730, 0, 650, 0, 1) glMatrixMode(GL_MODELVIEW) glColor4f(0.71, 0.83, 1, 0.3) glBegin(GL_QUADS) glVertex2f(x, y) glVertex2f(x + width, y) glVertex2f(x + width, y + height) glVertex2f(x, y + height) glEnd()
def rotate(self, axis, angle): # rotate the object self.makeCurrent() glMatrixMode(GL_MODELVIEW) glLoadIdentity() t = [self._modelview_matrix[3][0], self._modelview_matrix[3][1], self._modelview_matrix[3][2]] glTranslatef(t[0], t[1], t[2]) glRotated(angle, axis[0], axis[1], axis[2]) glTranslatef(-t[0], -t[1], -t[2]) glMultMatrixd(self._modelview_matrix) # update _modelview_matrix self._modelview_matrix = glGetDoublev(GL_MODELVIEW_MATRIX)
def init_view(self): """ initialize the projection matrix """ xSize, ySize = glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) aspect_ratio = float(xSize) / float(ySize) # load the projection matrix. Always the same glMatrixMode(GL_PROJECTION) glLoadIdentity() glViewport(0, 0, xSize, ySize) gluPerspective(70, aspect_ratio, 0.1, 1000.0) glTranslated(0, 0, -15)