def main(): GLUT.glutInit(argv) GLUT.glutInitDisplayMode(GLUT.GLUT_DOUBLE | GLUT.GLUT_RGBA | GLUT.GLUT_DEPTH | GLUT.GLUT_MULTISAMPLE) larguraTela = GLUT.glutGet(GLUT.GLUT_SCREEN_WIDTH) alturaTela = GLUT.glutGet(GLUT.GLUT_SCREEN_HEIGHT) larguraJanela = round(2 * larguraTela / 3) alturaJanela = round(2 * alturaTela / 3) GLUT.glutInitWindowSize(larguraJanela, alturaJanela) GLUT.glutInitWindowPosition(round((larguraTela - larguraJanela) / 2), round((alturaTela - alturaJanela) / 2)) GLUT.glutCreateWindow(janela) GLUT.glutDisplayFunc(draw) load() GL.glEnable(GL.GL_MULTISAMPLE) GL.glEnable(GL.GL_DEPTH_TEST) GL.glEnable(GL.GL_TEXTURE_2D) GL.glClearColor(*corFundo) GL.glClearDepth(1.0) GL.glDepthFunc(GL.GL_LESS) GL.glShadeModel(GL.GL_SMOOTH) GL.glMatrixMode(GL.GL_PROJECTION) GLU.gluPerspective(-45, larguraJanela / alturaJanela, 0.1, 100.0) GL.glTranslatef(0.0, 0.0, -10) GL.glMatrixMode(GL.GL_MODELVIEW) GLUT.glutTimerFunc(10, timer, 1) GLUT.glutMainLoop()
def __init__(self): """ Initialize and create GLUT window """ glut.glutInit(sys.argv) glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH) glut.glutCreateWindow("OpenGL") glut.glutFullScreen() gl.glEnable(gl.GL_TEXTURE_2D) gl.glDisable(gl.GL_LIGHTING) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) # Modify matrices for screen size width = glut.glutGet(glut.GLUT_SCREEN_WIDTH) height = glut.glutGet(glut.GLUT_SCREEN_HEIGHT) gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) aspect = width / height gl.glOrtho(-aspect, aspect, 1, -1, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity()
def setFullscreen(self, state): ''' If **state** is True, the set_fullscreen() method requests the window manager to place the window in the fullscreen state. If **state** is False the set_fullscreen() method requests the window manager to toggle off the fullscreen state for the window. Note that in any case, you shouldn't not assume the window state is definitely set afterward, because other entities (e.g. the user or window manager) could fullscreen/unfullscreen it again, and not all window managers honor requests to fullscreen windows. :param bool state: Fullscreen state to be set. ''' if self._fullscreen == state: return if state == True: glut.glutSetWindow( self._id ) self._saved_width = glut.glutGet(glut.GLUT_WINDOW_WIDTH) self._saved_height = glut.glutGet(glut.GLUT_WINDOW_HEIGHT) self._saved_x = glut.glutGet(glut.GLUT_WINDOW_X) self._saved_y = glut.glutGet(glut.GLUT_WINDOW_Y) self._fullscreen = True glut.glutFullScreen() else: self._fullscreen = False glut.glutSetWindow( self._id ) glut.glutReshapeWindow(self._saved_width, self._saved_height) glut.glutPositionWindow( self._saved_x, self._saved_y ) glut.glutSetWindowTitle( self._title )
def render(self): """ Render the scene using the sphere display list """ if self.do_exit: print('renderer exiting ...') # glut event loop needs hard exit ... sys.exit(0) if self.bodies is None: time.sleep(1 / self.fps) return GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) GL.glMatrixMode(GL.GL_PROJECTION) GL.glLoadIdentity() x_size = GLUT.glutGet(GLUT.GLUT_WINDOW_WIDTH) y_size = GLUT.glutGet(GLUT.GLUT_WINDOW_HEIGHT) GLU.gluPerspective(60, float(x_size) / float(y_size), 0.05, 10) GL.glMatrixMode(GL.GL_MODELVIEW) GL.glLoadIdentity() GL.glTranslatef(-_CAMERA_POSITION[0], -_CAMERA_POSITION[1], -_CAMERA_POSITION[2]) self.mouse_interactor.apply_transformation() for body_index in range(self.bodies.shape[0]): body = self.bodies[body_index, :] GL.glPushMatrix() GL.glTranslatef(body[0], body[1], body[2]) GL.glScalef(body[3], body[3], body[3]) GL.glCallList(self.sphere) GL.glPopMatrix() GLUT.glutSwapBuffers()
def main(): light_position = (7, 2, 1) GLUT.glutInit(argv) GLUT.glutInitDisplayMode(GLUT.GLUT_DOUBLE | GLUT.GLUT_RGBA | GLUT.GLUT_DEPTH | GLUT.GLUT_MULTISAMPLE) larguraTela = GLUT.glutGet(GLUT.GLUT_SCREEN_WIDTH) alturaTela = GLUT.glutGet(GLUT.GLUT_SCREEN_HEIGHT) larguraJanela = round(2 * larguraTela / 3) alturaJanela = round(2 * alturaTela / 3) GLUT.glutInitWindowSize(larguraJanela, alturaJanela) GLUT.glutInitWindowPosition(round((larguraTela - larguraJanela) / 2), round((alturaTela - alturaJanela) / 2)) GLUT.glutCreateWindow(janela) GLUT.glutReshapeFunc(reshape) GLUT.glutDisplayFunc(draw) GLUT.glutMouseFunc(clique) GL.glShadeModel(GL.GL_SMOOTH) GL.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, dados[0][0]) GL.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, dados[0][1]) GL.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, dados[0][2]) GL.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, dados[0][3]) GL.glEnable(GL.GL_LIGHTING) GL.glEnable(GL.GL_LIGHT0) GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position) GL.glEnable(GL.GL_MULTISAMPLE) GL.glEnable(GL.GL_DEPTH_TEST) GL.glClearColor(*corFundo) GLU.gluPerspective(45, larguraJanela / alturaJanela, 0.1, 50.0) GLUT.glutTimerFunc(50, timer, 1) GLUT.glutMainLoop()
def __glDraw(self): gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glClear(gl.GL_COLOR_BUFFER_BIT) width = glut.glutGet(glut.GLUT_WINDOW_WIDTH) height = glut.glutGet(glut.GLUT_WINDOW_HEIGHT) if width == 0: return wh = width * 1.0 / height ctime = float(time.time()) - float(self.initTime) gl.glUniformMatrix2fv(self.timeloc, 1, False, [ctime, 0.0, 0.0, ctime]) for varIndex, varValue in self.fractalSettings.vars.items(): cValue = [varValue, 0.0, 0.0, varValue] gl.glUniformMatrix2fv(self.varloc[varIndex], 1, False, cValue) st = self.fractalSettings vertexAttrib = [st.center[0], st.center[1], st.scale, wh] gl.glVertexAttrib4f(1, *vertexAttrib) gl.glBegin(gl.GL_TRIANGLE_FAN) gl.glVertex2f(-1, -1) gl.glVertex2f(-1, 1) gl.glVertex2f(1, 1) gl.glVertex2f(1, -1) gl.glEnd() gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDisableClientState(gl.GL_COLOR_ARRAY) glut.glutSwapBuffers()
def _vispy_get_geometry(self): # Should return widget (x, y, w, h) glut.glutSetWindow(self._id) x = glut.glutGet(glut.GLUT_WINDOW_X) y = glut.glutGet(glut.GLUT_WINDOW_Y) w = glut.glutGet(glut.GLUT_WINDOW_WIDTH) h = glut.glutGet(glut.GLUT_WINDOW_HEIGHT) return x, y, w, h
def func(index): handler, fps = self._timer_stack[index] t = glut.glutGet(glut.GLUT_ELAPSED_TIME) dt = (t - self._timer_date[index])/1000.0 self._timer_date[index] = t handler(dt) glut.glutTimerFunc(int(1000./fps), func, index) self._timer_date[index] = glut.glutGet(glut.GLUT_ELAPSED_TIME)
def func(index): handler, fps = self._timer_stack[index] t = glut.glutGet(glut.GLUT_ELAPSED_TIME) dt = (t - self._timer_date[index])/1000.0 self._timer_date[index] = t handler(dt) glut.glutTimerFunc(int(1000./fps), func, index) self._timer_date[index] = glut.glutGet(glut.GLUT_ELAPSED_TIME)
def _vispy_get_geometry(self): # Should return widget (x, y, w, h) glut.glutSetWindow(self._id) x = glut.glutGet(glut.GLUT_WINDOW_X) y = glut.glutGet(glut.GLUT_WINDOW_Y) w = glut.glutGet(glut.GLUT_WINDOW_WIDTH) h = glut.glutGet(glut.GLUT_WINDOW_HEIGHT) return x, y, w, h
def display(self, camera_tr_vec, camera_rot_mat, camera_fov_y, tracked_cam_track_pos_float): """ Draw everything with specified render camera position, projection parameters and tracked camera position :param camera_tr_vec: vec3 position of render camera in global space :param camera_rot_mat: mat3 rotation matrix of render camera in global space :param camera_fov_y: render camera field of view. To be used for building a projection matrix. Use glutGet to calculate current aspect ratio :param tracked_cam_track_pos_float: a frame in which tracked camera model and frustrum should be drawn (see tracked_cam_track_pos for basic task) :return: returns nothing """ GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) aspect_ratio = GLUT.glutGet(GLUT.GLUT_WINDOW_WIDTH) / GLUT.glutGet( GLUT.GLUT_WINDOW_HEIGHT) proj_mat = _build_projection_matrix(camera_fov_y, aspect_ratio, 0.1, 100) unrotate_mat = _build_rotation_matrix(np.linalg.inv(camera_rot_mat)) untranslate_mat = _build_translation_matrix(-camera_tr_vec) mvp = proj_mat.dot( unrotate_mat.dot(untranslate_mat.dot(_opencvgl_matrix))) self._render_in_mode(mvp, self._points_vbo, self._colors_vbo, self._count, GL.GL_POINTS) self._render_in_mode(mvp, self._cam_points_vbo, self._cam_colors_vbo, self._cam_count, GL.GL_LINE_STRIP) # a frame in which a tracked camera model and frustrum should be drawn # without interpolation tracked_cam_track_pos = int(tracked_cam_track_pos_float) cam_track_pos = self._tracked_cam_track[tracked_cam_track_pos] frustrum_points = self._get_frustrum_points( cam_track_pos, self._tracked_cam_parameters.fov_y, self._tracked_cam_parameters.aspect_ratio, 20.0) frustrum_border_points = np.array([ frustrum_points[0], frustrum_points[1], frustrum_points[1], frustrum_points[2], frustrum_points[2], frustrum_points[3], frustrum_points[3], frustrum_points[0], cam_track_pos.t_vec, frustrum_points[0], cam_track_pos.t_vec, frustrum_points[1], cam_track_pos.t_vec, frustrum_points[2], cam_track_pos.t_vec, frustrum_points[3] ], dtype=np.float32) points_vbo = vbo.VBO(frustrum_border_points.reshape(-1)) yellow_color = [1, 1, 0] colors_vbo = vbo.VBO( np.array(yellow_color * len(frustrum_border_points), dtype=np.float32)) self._render_in_mode(mvp, points_vbo, colors_vbo, len(frustrum_border_points), GL.GL_LINES) GLUT.glutSwapBuffers()
def get_dimensions(self): d = {} d['window_width'] = glut.glutGet(glut.GLUT_WINDOW_WIDTH) d['window_height'] = glut.glutGet(glut.GLUT_WINDOW_HEIGHT) d['subwindow_width'] = self.width_pct * d['window_width'] d['subwindow_height'] = self.height_pct * d['window_height'] d['subwindow_origin_x'] = self.x1_pct * d['window_width'] d['subwindow_origin_y'] = self.y1_pct * d['window_height'] return d
def _render_screen_texture(self, inv_view_mat): GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, self._camera_screen_frame_buffer) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) GL.glViewport(0, 0, _SCREEN_TEXTURE_W, _SCREEN_TEXTURE_H) mvp = self._tracked_cam_projection.dot(np.linalg.inv(inv_view_mat)) self._render_point_cloud(mvp) GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0) GL.glViewport(0, 0, GLUT.glutGet(GLUT.GLUT_WINDOW_WIDTH), GLUT.glutGet(GLUT.GLUT_WINDOW_HEIGHT))
def set_fullscreen(self, state): ''' Exit fullscreen mode ''' self._fullscreen = state if state: self._saved_width = glut.glutGet(glut.GLUT_WINDOW_WIDTH) self._saved_height = glut.glutGet(glut.GLUT_WINDOW_HEIGHT) glut.glutFullScreen() else: glut.glutReshapeWindow(self._saved_width, self._saved_height)
def get_size(self): '''Return the current size of the window. The window size does not include the border or title bar. :rtype: (int, int) :return: The width and height of the window, in pixels. ''' width = glut.glutGet(glut.GLUT_WINDOW_WIDTH) height = glut.glutGet(glut.GLUT_WINDOW_HEIGHT) return width,height
def snapshot(self, path): import cv2 self.on_draw() x = 0 y = 0 width = glut.glutGet(glut.GLUT_WINDOW_WIDTH) height = glut.glutGet(glut.GLUT_WINDOW_HEIGHT) data = gl.glReadPixels(x, y, width, height, gl.GL_BGR, gl.GL_UNSIGNED_BYTE) data = np.fromstring(data, dtype=np.uint8) cv2.imwrite( path, np.flipud(data.reshape((height, width, 3))) )
def getPosition(self): ''' The get_position() method returns the current posiiton of the window. :rtype: (int, int) :return: The current window coordinates, in pixels. ''' glut.glutSetWindow( self._id ) self._x = glut.glutGet( glut.GLUT_WINDOW_X ) self._y = glut.glutGet( glut.GLUT_WINDOW_Y ) return self._x, self._y
def main(): GLUT.glutInit(argv) GLUT.glutInitDisplayMode(GLUT.GLUT_DOUBLE | GLUT.GLUT_RGBA | GLUT.GLUT_DEPTH | GLUT.GLUT_MULTISAMPLE) # Creating a screen with good resolution proportions screen_width = GLUT.glutGet(GLUT.GLUT_SCREEN_WIDTH) screen_height = GLUT.glutGet(GLUT.GLUT_SCREEN_HEIGHT) window_width = round(2 * screen_width / 3) window_height = round(2 * screen_height / 3) GLUT.glutInitWindowSize(window_width, window_height) GLUT.glutInitWindowPosition(round((screen_width - window_width) / 2), round((screen_height - window_height) / 2)) GLUT.glutCreateWindow(window_name) # Reshape Function GLUT.glutReshapeFunc(reshape) # Drawing Function GLUT.glutDisplayFunc(draw) # Input Functions GLUT.glutSpecialFunc(special_key_pressed) GLUT.glutKeyboardFunc(key_pressed) GLUT.glutMouseFunc(mouse_click) GLUT.glutMotionFunc(mouse_move) #GL.glShadeModel(GL.GL_FLAT) GL.glShadeModel(GL.GL_SMOOTH) #First Material GL.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, materials[0][0]) GL.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, materials[0][1]) GL.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, materials[0][2]) GL.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, materials[0][3]) light_position = (10, 0, 0) GL.glEnable(GL.GL_LIGHTING) GL.glEnable(GL.GL_LIGHT0) GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position) GL.glEnable(GL.GL_MULTISAMPLE) GL.glEnable(GL.GL_DEPTH_TEST) GL.glClearColor(*background_color) # Pre-render camera positioning GLU.gluPerspective(45, window_width / window_height, 0.1, 50.0) GLUT.glutTimerFunc(50, timer, 1) GLUT.glutMainLoop()
def display(self, camera_tr_vec, camera_rot_mat, camera_fov_y, tracked_cam_track_pos_float): """ Draw everything with specified render camera position, projection parameters and tracked camera position :param camera_tr_vec: vec3 position of render camera in global space :param camera_rot_mat: mat3 rotation matrix of render camera in global space :param camera_fov_y: render camera field of view. To be used for building a projection matrix. Use glutGet to calculate current aspect ratio :param tracked_cam_track_pos_float: a frame in which tracked camera model and frustrum should be drawn (see tracked_cam_track_pos for basic task) :return: returns nothing """ tracked_cam_track_pos = int(tracked_cam_track_pos_float) tracked_cam_t = tracked_cam_track_pos_float - tracked_cam_track_pos self._camera_track.append(self._camera_track[-1]) tracked_cam_pose = _interpolate_cam_pose( tracked_cam_t, *self._camera_track[tracked_cam_track_pos:tracked_cam_track_pos + 2]) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) aspect_ratio = GLUT.glutGet(GLUT.GLUT_WINDOW_WIDTH) / GLUT.glutGet( GLUT.GLUT_WINDOW_HEIGHT) mvp = _setup_projection(camera_fov_y, aspect_ratio, 0.1, 100) @ \ np.linalg.inv(_setup_view(camera_tr_vec, camera_rot_mat)) # camera in OpenGL coordinates tracked_cam_p = _setup_projection(self._camera_params.fov_y, self._camera_params.aspect_ratio, 0.1, 50) tracked_cam_mv = _opencv_opengl_transform @ \ _setup_view(tracked_cam_pose.t_vec, tracked_cam_pose.r_mat) @ \ _opencv_opengl_transform # camera in OpenCV coordinates -> conversion needed tracked_cam_mvp = tracked_cam_p @ np.linalg.inv(tracked_cam_mv) self._render_cam_frustum( mvp @ tracked_cam_mv @ np.linalg.inv(tracked_cam_p)) self._render_points( mvp @ _opencv_opengl_transform) # point cloud in OpenCV coordinates self._render_cam_track( mvp @ _opencv_opengl_transform) # track in OpenCV coordinates self._render_cam( mvp @ tracked_cam_mv, # camera model in OpenGL coordinates tracked_cam_mvp @ _opencv_opengl_transform # point cloud in OpenCV coordinates ) GLUT.glutSwapBuffers()
def __init__(self, width=None, height=None, caption=None, visible=True, fullscreen=False): event.EventDispatcher.__init__(self) self._event_queue = [] if width and width > 0: self._width = width else: self._width = Window._default_width if height and height > 0: self._height = height else: self._height = Window._default_height if caption is None: caption = sys.argv[0] self._caption = caption self._saved_width = self._width self._saved_height = self._height if _window is None: glut.glutInit(sys.argv) glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH) self._window_id = glut.glutCreateWindow(self._caption) glut.glutDisplayFunc(self._display) glut.glutReshapeFunc(self._reshape) glut.glutKeyboardFunc(self._keyboard) glut.glutKeyboardUpFunc(self._keyboard_up) glut.glutMouseFunc(self._mouse) glut.glutMotionFunc(self._motion) glut.glutPassiveMotionFunc(self._passive_motion) glut.glutVisibilityFunc(self._visibility) glut.glutEntryFunc(self._entry) glut.glutSpecialFunc(self._special) glut.glutSpecialUpFunc(self._special_up) gl.glClearColor(0,0,0,0) self._visible = visible self._time = glut.glutGet(glut.GLUT_ELAPSED_TIME) if not visible: glut.glutHideWindow() else: glut.glutShowWindow() self.set_size(self._width, self._height) screen_width = glut.glutGet(glut.GLUT_SCREEN_WIDTH) screen_height= glut.glutGet(glut.GLUT_SCREEN_HEIGHT) glut.glutPositionWindow((screen_width-self._width)//2, (screen_height-self._height)//2) self.fullscreen = fullscreen
def timer(self, value): """The timer callback function.""" if value != 0: caption = '{}: {} frames per secound @ {} x {}'.format( self.window_title.decode(), self.frame_count * 4, GLUT.glutGet(GLUT.GLUT_WINDOW_WIDTH), GLUT.glutGet(GLUT.GLUT_WINDOW_HEIGHT)) GLUT.glutSetWindowTitle(caption) self.frame_count = 0 GLUT.glutTimerFunc(250, self.timer, 1) # a quarter of a second
def getSize(self): ''' The get_size() methods returns the current size of the window and does not include the size of the window manager decorations (aka the window frame or border). :rtype: (int, int) :return: The width and height of the window, in pixels. ''' glut.glutSetWindow( self._id ) self._width = glut.glutGet( glut.GLUT_WINDOW_WIDTH ) self._height = glut.glutGet( glut.GLUT_WINDOW_HEIGHT ) return self._width, self._height
def _matrix_project(self, fovy, znear=0.5, zfar=100.): aspect_ratio = GLUT.glutGet(GLUT.GLUT_WINDOW_WIDTH) / GLUT.glutGet( GLUT.GLUT_WINDOW_HEIGHT) ymax = znear * np.tan(fovy / 2.) xmax = ymax * aspect_ratio delta = znear - zfar m = np.zeros((4, 4), dtype=np.float32) m[0, 0] = znear / xmax m[1, 1] = znear / ymax m[2, 2] = (zfar + znear) / delta m[2, 3] = 2. * znear * zfar / delta m[3, 2] = -1. return m
def display(self): self.fps_frame += 1 self.fps_time = glut.glutGet(glut.GLUT_ELAPSED_TIME) if self.fps_time - self.fps_timebase > 1000: print('FPS:%4.2f' % (self.fps_frame * 1000.0 / (self.fps_time - self.fps_timebase))) self.fps_timebase = self.fps_time self.fps_frame = 0 gl.glClear(self.clear_bit) x, y, w, h = self.viewport gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() if self.isperspect: fovy, aspect, zNear, zFar = self.glu_perspect #print fovy glu.gluPerspective(fovy, w / float(h), zNear, zFar) else: left, right, bottom, top, near, far = self.gl_orthog gl.glOrtho(left, right, bottom, top, near, far) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() eyex, eyey, eyez, centx, centy, centz, upx, upy, upz = self.glu_lookat glu.gluLookAt(eyex, eyey, eyez, centx, centy, centz, upx, upy, upz) self.mouse.applyTransformation() #Add objects self.plot.display() gl.glFlush() glut.glutSwapBuffers()
def init_window(self): glut.glutInit(sys.argv) glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_ALPHA | glut.GLUT_DEPTH) glut.glutInitWindowPosition( (glut.glutGet(glut.GLUT_SCREEN_WIDTH) - self.window_width) // 2, (glut.glutGet(glut.GLUT_SCREEN_HEIGHT) - self.window_height) // 2, ) glut.glutSetOption(glut.GLUT_ACTION_ON_WINDOW_CLOSE, glut.GLUT_ACTION_CONTINUE_EXECUTION) glut.glutInitWindowSize(self.window_width, self.window_height) self.window_handle = glut.glutCreateWindow(WINDOW_TITLE_PREFIX) glut.glutDisplayFunc(self.render_func) glut.glutReshapeFunc(self.resize_func) glut.glutMouseFunc(self.mouse) glut.glutMotionFunc(self.mouse_motion) glut.glutKeyboardFunc(self.keyboard) glut.glutSpecialFunc(self.keyboard_s)
def __init__(self, world, title='', height=500, width=500): self.world = world self.time_interval = 10 GLUT.glutInit(sys.argv) self.lastTime = GLUT.glutGet(GLUT.GLUT_ELAPSED_TIME) GLUT.glutInitDisplayMode(GLUT.GLUT_DOUBLE | GLUT.GLUT_RGB | GLUT.GLUT_DEPTH) GLUT.glutInitWindowSize(height, width) GLUT.glutCreateWindow(title) GLUT.glutDisplayFunc(self.render) GLUT.glutReshapeFunc(self.onSize) self.init = False #initialized to actual values in setupView self.viewport_left = 0 self.viewport_bottom = 0 self.viewport_height = 0 self.viewport_width = 0 self.timeStep = 1 self.timeElapsed = self.time_interval / 1000.0
def on_display( ): gl.glClearColor(1,1,1,1) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) global t, t0, frames t = glut.glutGet( glut.GLUT_ELAPSED_TIME ) frames = frames + 1 if t-t0 > 2500: print "FPS : %.2f (%d frames in %.2f second)" % (frames*1000.0/(t-t0), frames, (t-t0)/1000.0) t0, frames = t,0 shader.bind() shader.uniformi( 'texture', 0) shader.uniformf( 'size', w,h) shader.uniformf( 'major_grid_width', major_grid_width ) shader.uniformf( 'minor_grid_width', minor_grid_width ) shader.uniformf( 'major_grid_color', *major_grid_color ) shader.uniformf( 'minor_grid_color', *minor_grid_color ) shader.uniformf( 'major_tick_size', *major_tick_size ) shader.uniformf( 'minor_tick_size', *minor_tick_size ) shader.uniformf( 'major_tick_width', major_tick_width ) shader.uniformf( 'minor_tick_width', minor_tick_width ) shader.uniformf( 'major_tick_color', *major_tick_color ) shader.uniformf( 'minor_tick_color', *minor_tick_color ) axis.draw( gl.GL_TRIANGLES ) shader.unbind() glut.glutSwapBuffers()
def on_display(): global cube, theta, phi, frame, time, timebase frame += 1 time = glut.glutGet( glut.GLUT_ELAPSED_TIME ) if (time - timebase > 1000): print frame*1000.0/(time-timebase) timebase = time; frame = 0; gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glPushMatrix() gl.glRotatef(theta, 0,0,1) gl.glRotatef(phi, 0,1,0) gl.glDisable( gl.GL_BLEND ) gl.glEnable( gl.GL_LIGHTING ) gl.glEnable( gl.GL_DEPTH_TEST ) gl.glEnable( gl.GL_POLYGON_OFFSET_FILL ) gl.glPolygonMode( gl.GL_FRONT_AND_BACK, gl.GL_FILL ) cube.draw( gl.GL_QUADS, 'pnc' ) gl.glDisable( gl.GL_POLYGON_OFFSET_FILL ) gl.glEnable( gl.GL_BLEND ) gl.glDisable( gl.GL_LIGHTING ) gl.glPolygonMode( gl.GL_FRONT_AND_BACK, gl.GL_LINE ) gl.glDepthMask( gl.GL_FALSE ) gl.glColor( 0.0, 0.0, 0.0, 0.5 ) cube.draw( gl.GL_QUADS, 'p' ) gl.glDepthMask( gl.GL_TRUE ) gl.glPopMatrix() glut.glutSwapBuffers()
def display(self): global last_time current_time = glut.glutGet(glut.GLUT_ELAPSED_TIME) Dt = current_time - last_time #print Dt now = self.time #if Dt < 40: for s in self.slots: if now >= self.slots[s]['slot'][0] and now <=self.slots[s]['slot'][1]: self.slots[s]['actor'].near_pick = self.near_pick self.slots[s]['actor'].far_pick = self.far_pick self.slots[s]['actor'].display() last_time = current_time
def animate(self): d_time_millis = GLUT.glutGet( GLUT.GLUT_ELAPSED_TIME) - self._data.prev_time self._data.prev_time += d_time_millis d_time = d_time_millis / 1000.0 if self._data.key_states[ord(b'q')] or self._data.key_states[ord( b'e')]: if self._data.key_states[ord(b'q')]: self._tracked_cam_track_pos_float -= \ d_time * self._change_rates.track_pos if self._data.key_states[ord(b'e')]: self._tracked_cam_track_pos_float += \ d_time * self._change_rates.track_pos self._tracked_cam_track_pos_float = np.clip( self._tracked_cam_track_pos_float, 0, self._tracked_cam_track_len - 1) self._tracked_cam_track_pos_float = np.clip( self._tracked_cam_track_pos_float, 0, self._tracked_cam_track_len - 1) if self._data.key_states[ord(b'a')]: self.move_camera(d_time * np.array([-self._change_rates.tr_xz, 0, 0])) if self._data.key_states[ord(b'd')]: self.move_camera(d_time * np.array([+self._change_rates.tr_xz, 0, 0])) if self._data.key_states[ord(b's')]: self.move_camera(d_time * np.array([0, 0, +self._change_rates.tr_xz])) if self._data.key_states[ord(b'w')]: self.move_camera(d_time * np.array([0, 0, -self._change_rates.tr_xz])) GLUT.glutPostRedisplay()
def main(): GLUT.glutInit(argv) GLUT.glutInitDisplayMode(GLUT.GLUT_DOUBLE | GLUT.GLUT_RGBA | GLUT.GLUT_DEPTH | GLUT.GLUT_MULTISAMPLE) # Creating a screen with good resolution proportions screen_width = GLUT.glutGet(GLUT.GLUT_SCREEN_WIDTH) screen_height = GLUT.glutGet(GLUT.GLUT_SCREEN_HEIGHT) window_width = round(2 * screen_width / 3) window_height = round(2 * screen_height / 3) GLUT.glutInitWindowSize(window_width, window_height) GLUT.glutInitWindowPosition(round((screen_width - window_width) / 2), round((screen_height - window_height) / 2)) GLUT.glutCreateWindow(window_name) # Drawing Function GLUT.glutDisplayFunc(draw) # Input Functions GLUT.glutSpecialFunc(special_key_pressed) GLUT.glutKeyboardFunc(key_pressed) GLUT.glutMouseFunc(mouse_click) GLUT.glutMotionFunc(mouse_move) load_textures() GL.glEnable(GL.GL_MULTISAMPLE) GL.glEnable(GL.GL_DEPTH_TEST) GL.glEnable(GL.GL_TEXTURE_2D) GL.glClearColor(*background_color) GL.glClearDepth(1.0) GL.glDepthFunc(GL.GL_LESS) GL.glShadeModel(GL.GL_SMOOTH) GL.glMatrixMode(GL.GL_PROJECTION) # Pre-render camera positioning GLU.gluPerspective(-45, window_width / window_height, 0.1, 100.0) GL.glTranslatef(0.0, 0.0, -10) GL.glMatrixMode(GL.GL_MODELVIEW) GLUT.glutTimerFunc(10, timer, 1) GLUT.glutMainLoop()
def on_idle(): global t, t0, frames t = glut.glutGet( glut.GLUT_ELAPSED_TIME ) frames = frames + 1 if t-t0 > 2500: print "FPS : %.2f (%d frames in %.2f second)" % (frames*1000.0/(t-t0), frames, (t-t0)/1000.0) t0, frames = t,0 glut.glutPostRedisplay()
def on_idle(): #Advances frame global frame t = glut.glutGet(glut.GLUT_ELAPSED_TIME) frame = frame + 30 if (frame > 1439): frame = frame - 1439
def Calculations(): global objects global test global frames global start w = GLUT.glutGet(GLUT.GLUT_WINDOW_WIDTH) h = GLUT.glutGet(GLUT.GLUT_WINDOW_HEIGHT) cam.facing(fx, fy, w, h) GLUT.glutPostRedisplay() frames = frames + 1 if frames > 100: elapsed = t.time() - start sys.stdout.write('\r') sys.stdout.write(str(frames / elapsed)) sys.stdout.flush() frames = 0 start = t.time()
def snapshot(self, path): import cv self.on_draw() if False: # pylint: disable=using-constant-test x, y, width, height = gl.glGetIntegerv(gl.GL_VIEWPORT) else: x = 0 y = 0 width = glut.glutGet(glut.GLUT_WINDOW_WIDTH) height = glut.glutGet(glut.GLUT_WINDOW_HEIGHT) data = gl.glReadPixels(x, y, width, height, gl.GL_BGR, gl.GL_UNSIGNED_BYTE) image = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 3) flipped_image = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 3) cv.SetData(image, data) cv.Flip(image, flipped_image, 0) cv.SaveImage(path, flipped_image)
def init_window(self): glut.glutInit(sys.argv) glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_ALPHA | glut.GLUT_DEPTH) glut.glutInitWindowPosition( (glut.glutGet(glut.GLUT_SCREEN_WIDTH) - self.window_width) // 2, (glut.glutGet(glut.GLUT_SCREEN_HEIGHT) - self.window_height) // 2) glut.glutSetOption(glut.GLUT_ACTION_ON_WINDOW_CLOSE, glut.GLUT_ACTION_CONTINUE_EXECUTION) glut.glutInitWindowSize(self.window_width, self.window_height) self.window_handle = glut.glutCreateWindow(WINDOW_TITLE_PREFIX) glut.glutDisplayFunc(self.render_func) glut.glutReshapeFunc(self.resize_func) glut.glutMouseFunc(self.mouse) glut.glutMotionFunc(self.mouse_motion) glut.glutKeyboardFunc(self.keyboard) glut.glutSpecialFunc(self.keyboard_s)
def on_idle(): #Advances frame global frame t = glut.glutGet(glut.GLUT_ELAPSED_TIME) frame = frame + 30 if (frame > 1439): frame = frame - 1439
def on_idle(): #Advances frame global frame t = glut.glutGet(glut.GLUT_ELAPSED_TIME) frame = frame + 1 if (frame > 50): frame = frame - 50 print frame
def on_idle(): #Advances frame global frame t = glut.glutGet(glut.GLUT_ELAPSED_TIME) frame = frame + 1 if (frame > 50): frame = frame - 50 print frame
def main(): GLUT.glutInit(argv) GLUT.glutInitDisplayMode(GLUT.GLUT_DOUBLE | GLUT.GLUT_RGBA | GLUT.GLUT_DEPTH | GLUT.GLUT_MULTISAMPLE) larguraTela = GLUT.glutGet(GLUT.GLUT_SCREEN_WIDTH) alturaTela = GLUT.glutGet(GLUT.GLUT_SCREEN_HEIGHT) larguraJanela = round(2 * larguraTela / 3) alturaJanela = round(2 * alturaTela / 3) GLUT.glutInitWindowSize(larguraJanela, alturaJanela) GLUT.glutInitWindowPosition(round((larguraTela - larguraJanela) / 2), round((alturaTela - alturaJanela) / 2)) GLUT.glutCreateWindow(janela) GLUT.glutDisplayFunc(draw) GLUT.glutMouseFunc(clique) GL.glEnable(GL.GL_MULTISAMPLE) GL.glEnable(GL.GL_DEPTH_TEST) GL.glClearColor(*corFundo) GLU.gluPerspective(-45, larguraJanela / alturaTela, 0.1, 100.0) GLUT.glutTimerFunc(10, timer, 1) GLUT.glutMainLoop()
def main(): GLUT.glutInit(argv) GLUT.glutInitDisplayMode( GLUT.GLUT_DOUBLE | GLUT.GLUT_RGBA | GLUT.GLUT_DEPTH | GLUT.GLUT_MULTISAMPLE ) screen_width = GLUT.glutGet(GLUT.GLUT_SCREEN_WIDTH) screen_height = GLUT.glutGet(GLUT.GLUT_SCREEN_HEIGHT) window_width = round(2 * screen_width / 3) window_height = round(2 * screen_height / 3) GLUT.glutInitWindowSize(window_width, window_height) GLUT.glutInitWindowPosition( round((screen_width - window_width) / 2), round((screen_height - window_height) / 2) ) GLUT.glutCreateWindow("Textura Esfera Globo") GLUT.glutDisplayFunc(draw) GLUT.glutKeyboardFunc(key_pressed) GLUT.glutMouseFunc(mouse_click) GLUT.glutMotionFunc(mouse_move) LoadTextures() GL.glEnable(GL.GL_MULTISAMPLE) GL.glEnable(GL.GL_DEPTH_TEST) GL.glEnable(GL.GL_TEXTURE_2D) GL.glClearColor(0.0, 0.0, 0.0, 1) GL.glClearDepth(1.0) GL.glDepthFunc(GL.GL_LESS) GL.glShadeModel(GL.GL_SMOOTH) GL.glMatrixMode(GL.GL_PROJECTION) # POSICAO DA CAMERA GLU.gluPerspective(-45, window_width / window_height, 0.1, 100.0) GL.glTranslatef(0.0, 0.0, -10) GL.glMatrixMode(GL.GL_MODELVIEW) GLUT.glutTimerFunc(10, timer, 1) GLUT.glutMainLoop()
def display(self, camera_tr_vec, camera_rot_mat, camera_fov_y, tracked_cam_track_pos_float): """ Draw everything with specified render camera position, projection parameters and tracked camera position :param camera_tr_vec: vec3 position of render camera in global space :param camera_rot_mat: mat3 rotation matrix of render camera in global space :param camera_fov_y: render camera field of view. To be used for building a projection matrix. Use glutGet to calculate current aspect ratio :param tracked_cam_track_pos_float: a frame in which tracked camera model and frustrum should be drawn (see tracked_cam_track_pos for basic task) :return: returns nothing """ GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) view_mat = np.linalg.inv( _get_pose_matrix(data3d.Pose(camera_rot_mat, camera_tr_vec))) aspect_ratio = float(GLUT.glutGet(GLUT.GLUT_WINDOW_WIDTH)) / float( GLUT.glutGet(GLUT.GLUT_WINDOW_HEIGHT)) focus_y = 1.0 / np.tan(camera_fov_y / 2.0) focus_x = focus_y / aspect_ratio near = 0.1 far = 1000.0 projection_mat = np.matrix([ [focus_x, 0, 0, 0], [0, focus_y, 0, 0], [ 0, 0, -(far + near) / (far - near), -2 * far * near / (far - near) ], [0, 0, -1, 0], ]) pv_matrix = projection_mat.dot(view_mat) self._point_cloud_renderer.render(pv_matrix) self._cam_track_line_renderer.render(pv_matrix) self._cam_renderer.render(pv_matrix, tracked_cam_track_pos_float) GLUT.glutSwapBuffers()
def _make_vp(camera_tr_vec, camera_rot_mat, camera_fov_y): v = np.linalg.inv( np.block([[camera_rot_mat, camera_tr_vec.reshape((3, 1))], [np.zeros((1, 3)), np.ones((1, 1))]])) aspect_ratio = GLUT.glutGet(GLUT.GLUT_WINDOW_WIDTH) / GLUT.glutGet( GLUT.GLUT_WINDOW_HEIGHT) fy = calc_fy(camera_fov_y) fx = fy / aspect_ratio # camera_fov_x = camera_fov_y / aspect_ratio p = np.zeros((4, 4), dtype=np.float32) p[0, 0] = fx p[1, 1] = fy p[2, 2] = -(FAR + NEAR) / (FAR - NEAR) p[2, 3] = -2 * FAR * NEAR / (FAR - NEAR) p[3, 2] = -1 return p @ v
def handleTime(self, value): try: currentTime = GLUT.glutGet(GLUT.GLUT_ELAPSED_TIME) self.world.update(currentTime=currentTime) self.render() GLUT.glutTimerFunc(self.time_interval, self.handleTime, None) except: traceback.print_exc() sys.exit()
def display(self): self.fps_frame+=1 self.fps_time=glut.glutGet(glut.GLUT_ELAPSED_TIME) if self.fps_time - self.fps_timebase > 1000: print('FPS:%4.2f' % (self.fps_frame*1000.0/(self.fps_time-self.fps_timebase))) self.fps_timebase=self.fps_time self.fps_frame = 0 gl.glClear(self.clear_bit) x,y,w,h=self.viewport gl.glMatrixMode (gl.GL_PROJECTION) gl.glLoadIdentity() if self.isperspect: fovy,aspect,zNear,zFar=self.glu_perspect #print fovy glu.gluPerspective(fovy,w/float(h),zNear,zFar) else: left,right,bottom,top,near,far=self.gl_orthog gl.glOrtho(left, right, bottom, top, near, far) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() eyex,eyey,eyez,centx,centy,centz,upx,upy,upz=self.glu_lookat glu.gluLookAt(eyex,eyey,eyez,centx,centy,centz,upx,upy,upz) self.mouse.applyTransformation() #Add objects self.plot.display() gl.glFlush() glut.glutSwapBuffers()
def _vispy_get_position(self): glut.glutSetWindow(self._id) x = glut.glutGet(glut.GLUT_WINDOW_X) y = glut.glutGet(glut.GLUT_WINDOW_Y) return x, y
def _vispy_get_size(self): glut.glutSetWindow(self._id) w = glut.glutGet(glut.GLUT_WINDOW_WIDTH) h = glut.glutGet(glut.GLUT_WINDOW_HEIGHT) return w, h
def get_position(self): glut.glutSetWindow(self._native_window) self._x = glut.glutGet(glut.GLUT_WINDOW_W) self._y = glut.glutGet(glut.GLUT_WINDOW_Y) return self._x, self._y
def get_size(self): self.activate() self._width = glut.glutGet(glut.GLUT_WINDOW_WIDTH) self._height = glut.glutGet(glut.GLUT_WINDOW_HEIGHT) return self._width, self._height
def _reshape(self, width, height): self._width = glut.glutGet(glut.GLUT_WINDOW_WIDTH) self._height = glut.glutGet(glut.GLUT_WINDOW_HEIGHT) self.dispatch_event("on_resize", self._width, self._height)
def capture_mouse(self, x, y): """Capture mouse drag event.""" self.width = GLUT.glutGet(GLUT.GLUT_WINDOW_WIDTH) self.height = GLUT.glutGet(GLUT.GLUT_WINDOW_HEIGHT) self.first_mouse_position = nds_coord(x, y, self.width, self.height)
frames = frames + 1 if t-t0 > 2500: print "FPS : %.2f (%d frames in %.2f second)" % (frames*1000.0/(t-t0), frames, (t-t0)/1000.0) t0, frames = t,0 glut.glutPostRedisplay() # ----------------------------------------------------------------------------- if __name__ == '__main__': import sys import OpenGL.GLUT as glut from glagg import curve4_bezier from glagg import PathCollection t0, frames = glut.glutGet(glut.GLUT_ELAPSED_TIME), 0 glut.glutInit(sys.argv) glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGB | glut.GLUT_DEPTH) glut.glutCreateWindow("Shapes") glut.glutReshapeWindow(800, 800) glut.glutDisplayFunc(on_display) glut.glutReshapeFunc(on_reshape) glut.glutKeyboardFunc(on_keyboard) glut.glutIdleFunc(on_idle) collection = PathCollection() def heart(): vertices = curve4_bezier( (0.0,-0.5), (0.75,+0.25), (.75,+1.0), (0.0,+0.5) ) n = len(vertices) V = np.zeros((2*n,2)) V[:n] = vertices
t = glut.glutGet( glut.GLUT_ELAPSED_TIME ) frames = frames + 1 if t-t0 > 2500: print "FPS : %.2f (%d frames in %.2f second)" % (frames*1000.0/(t-t0), frames, (t-t0)/1000.0) t0, frames = t,0 glut.glutPostRedisplay() # ----------------------------------------------------------------------------- if __name__ == '__main__': import sys import OpenGL.GLUT as glut from shapes import star, asterisk from glagg import PathCollection t0, frames, t = 0,0,0 t0 = glut.glutGet(glut.GLUT_ELAPSED_TIME) glut.glutInit(sys.argv) glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGB | glut.GLUT_DEPTH) glut.glutInitWindowSize(800, 800) glut.glutCreateWindow("Asterisks") glut.glutDisplayFunc(on_display) glut.glutReshapeFunc(on_reshape) glut.glutKeyboardFunc(on_keyboard) glut.glutIdleFunc(on_idle) collection = PathCollection() vertices = asterisk(n=5) for i in range(2000): collection.append( vertices, closed=True,
def getClientSizeTuple(self): return (GLUT.glutGet(GLUT.GLUT_WINDOW_WIDTH), GLUT.glutGet(GLUT.GLUT_WINDOW_HEIGHT))
def __init__(self, size=None, position=None, title=None, fullscreen=False,enableAlpha=True,pointSize=2): ''' Constructor ''' self._mouse_x = 0 self._mouse_y = 0 self._button = mouse.NONE self._modifiers = None self._motionEventCounter=0 self._time = None self._timer_stack = [] self._timer_date = [] self._title = title or "PyGLer" # FIXME: get default name from a central location self._fullscreen = -1 self.dragSensitivity = 5 # Is there any glut loop already running ? if glut.glutGetWindow( ) == 0: glut.glutInit() glut.glutInitDisplayMode( glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH ) self._interactive = False else: self._interactive = True self._id = glut.glutCreateWindow( self._title ) glut.glutShowWindow() glut.glutDisplayFunc( self.redraw ) glut.glutReshapeFunc( self._reshape ) glut.glutKeyboardFunc( self._keyboard ) glut.glutKeyboardUpFunc( self._keyboard_up ) glut.glutMouseFunc( self._mouse ) glut.glutMotionFunc( self._motion ) glut.glutPassiveMotionFunc( self._passive_motion ) glut.glutVisibilityFunc( self._visibility ) glut.glutEntryFunc( self._entry ) glut.glutSpecialFunc( self._special ) glut.glutSpecialUpFunc( self._special_up ) GL.glEnable(GL.GL_DEPTH_TEST) GL.glEnable(GL.GL_BLEND) if enableAlpha: GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); GL.glPolygonOffset(1, 1); GL.glEnable(GL.GL_POLYGON_OFFSET_FILL); GL.glPointSize(pointSize) GL.glEnable(GL.GL_PROGRAM_POINT_SIZE) if size is not None: width, height = size glut.glutReshapeWindow( width, height ) width = glut.glutGet( glut.GLUT_WINDOW_WIDTH ) height= glut.glutGet( glut.GLUT_WINDOW_HEIGHT ) self._width = width self._height = height if position is not None: x,y = position glut.glutPositionWindow( x, y ) x = glut.glutGet( glut.GLUT_WINDOW_X ) y = glut.glutGet( glut.GLUT_WINDOW_X ) self._x, self._y = x, y # These ones will be used when exiting fullscreen self._saved_width = self._width self._saved_height = self._height self._saved_x = self._x self._saved_y = self._y self._time = glut.glutGet( glut.GLUT_ELAPSED_TIME ) self._fullscreen = fullscreen if fullscreen: self.set_fullscreen(True)