def set_3d(self): """ Configure OpenGL to draw in 3d. """ width, height = self.get_size() gl.glEnable(gl.GL_DEPTH_TEST) gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(65.0, width / float(height), 0.1, DIST) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() x, y = self.rotation gl.glRotatef(x, 0, 1, 0) gl.glRotatef(-y, math.cos(math.radians(x)), 0, math.sin(math.radians(x))) x, y, z = self.position gl.glTranslatef(-x, -y, -z) gl.glEnable(gl.GL_LIGHTING) gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, GLfloat4(0.05, 0.05, 0.05, 1.0)) gl.glEnable(gl.GL_COLOR_MATERIAL) gl.glColorMaterial(gl.GL_FRONT, gl.GL_AMBIENT_AND_DIFFUSE) #gl.glLightfv(gl.GL_LIGHT1,gl.GL_SPOT_DIRECTION, GLfloat3(0,0,-1)) gl.glLightfv(gl.GL_LIGHT1, gl.GL_AMBIENT, GLfloat4(0.5, 0.5, 0.5, 1.0)) gl.glLightfv(gl.GL_LIGHT1, gl.GL_DIFFUSE, GLfloat4(1.0, 1.0, 1.0, 1.0)) gl.glLightfv(gl.GL_LIGHT1, gl.GL_POSITION, GLfloat4(0.35, 1.0, 0.65, 0.0)) #gl.glLightfv(gl.GL_LIGHT0,gl.GL_SPECULAR, GLfloat4(1,1,1,1)) gl.glDisable(gl.GL_LIGHT0) gl.glEnable(gl.GL_LIGHT1)
def _setup_3d(self): w, h = self.get_size() gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LEQUAL) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_CULL_FACE) viewport = self.get_viewport_size() gl.glViewport(0, 0, max(1, viewport[0]), max(1, viewport[1])) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(*self.perspective) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() y, x = self.rotation gl.glRotatef(x, 0, 1, 0) gl.glRotatef(-y, math.cos(math.radians(x)), 0, math.sin(math.radians(x))) # NOTE: for GL render, its x-z plane is the ground plane, # so we unpack the position using `(x, z, y)` instead of `(x, y, z)` x, z, y = self.position if not self.debug_mode: y += self.perspective_over_drone[0] z += self.perspective_over_drone[1] gl.glTranslatef(-x, -y, -z)
def _update_perspective(self, width, height): try: # for high DPI screens viewport size # will be different then the passed size width, height = self.get_viewport_size() except BaseException: # older versions of pyglet may not have this pass # set the new viewport size gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() # get field of view and Z range from camera camera = self.scene.camera # set perspective from camera data gl.gluPerspective(camera.fov[1], width / float(height), camera.z_near, camera.z_far) gl.glMatrixMode(gl.GL_MODELVIEW) return width, height
def on_resize(width, height): glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(70, 1.0 * width / height, 0.1, 1000.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def set_camera_projection (self): lander_width = self.scaleFactor * self.simulator.lander_width lander_height = self.scaleFactor * -self.simulator.lander.colliders['pos'][1].min() (pad_x, pad_y) = (0.0, lander_height) (lander_x, lander_y) = self.scaleFactor * self.simulator.lander.pos self.display_width = abs(pad_x - lander_x) + 2*lander_width self.display_height = abs(pad_y - lander_y) + 2*lander_width if self.display_width*self.height > self.display_height*self.width: self.display_height = self.display_width * self.height / self.width else: self.display_width = self.display_height * self.width / self.height self.camera_x = (pad_x + lander_x) / 2.0 self.camera_y = (pad_y + lander_y) / 2.0 self.camera_z = self.display_height / (2.0 * math.tan(self.fov_y/2.0)) moon_radius = self.scaleFactor * 1.7371e6 self.camera_near = self.camera_y / math.tan(self.fov_y/2.0) # self.camera_far = 500*self.scaleFactor self.camera_far = math.sqrt(2*moon_radius*self.camera_y) gl.glLoadIdentity() gl.gluPerspective (math.degrees(self.fov_y), self.display_width/self.display_height, self.camera_near, self.camera_far)
def on_resize(width, height): gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60, width/float(height), .1, 8192.) gl.glMatrixMode(gl.GL_MODELVIEW) return pyglet.event.EVENT_HANDLED
def _set_up_perspective(self): glEnable(GL_DEPTH_TEST) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60., self.aspect_ratio, 1, 1000.) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def draw(self, camera): # set up projection matrix gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() gl.gluPerspective(FOVY, camera.aspect(), NEAR_PLANE, FAR_PLANE) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glEnable(gl.GL_DEPTH_TEST) # compute correct clip rect on far plane l, b = camera.position + camera.offset * FAR_PLANE_SCALE r, t = camera.position - camera.offset * FAR_PLANE_SCALE # Create blocks l = int(floor(l / BLOCK_SIZE)) r = int(ceil(r / BLOCK_SIZE)) b = int(floor(b / BLOCK_SIZE)) t = int(ceil(t / BLOCK_SIZE)) for y in range(b, t): for x in range(l, r): c = v(x, y) * BLOCK_SIZE gl.glPushMatrix() gl.glTranslatef(c.x, c.y, 0) self.batch.draw() gl.glPopMatrix() gl.glMatrixMode(gl.GL_PROJECTION) gl.glPopMatrix() gl.glMatrixMode(gl.GL_MODELVIEW) gl.glDisable(gl.GL_DEPTH_TEST)
def on_draw(*args): global time, particles t = time / FRAMES window.clear() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(90, 1, 0.1, 100) gl.glTranslatef(0, 0, -10) gl.glRotatef(t * 360, 0, 1, 0) # gl.glRotatef(t * 360, 1, 0, 0) batch = pyglet.graphics.Batch() if time == FRAMES: exit() points = [] for i in range(K): points.append(gen_point((i + t) / K, i)) # print(points) points_to_lines(points, batch) batch.draw() save_frame(time % FRAMES) time += 1
def display(self, width, height): gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60., width / float(height), .1, 1000.) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.glTranslatef(self.trans[0], self.trans[1], self.trans[2]) gl.glRotatef(self.rot[0], 1.0, 0.0, 0.0) gl.glRotatef(self.rot[1], 0.0, 1.0, 0.0) gl.glRotatef(self.rz, 0, 0, 1) gl.glRotatef(self.ry, 0, 1, 0) gl.glRotatef(self.rx, 1, 0, 0) gl.glEnable(gl.GL_LIGHTING) gl.glColor3f(1, 0, 0) self.torus.draw() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, width, 0, height, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.glDisable(gl.GL_LIGHTING) gl.glColor3f(0, 1, 0) self.lowerlabel.text = 'Rx %.2f Ry %.2f Rz %.2f' % (self.rx, self.ry, self.rz) self.lowerlabel.draw() self.upperlabel.text = time.strftime('Now is %H:%M:%S') self.upperlabel.x = width - self.upperlabel.content_width - 5 self.upperlabel.y = height - self.upperlabel.content_height self.upperlabel.draw()
def on_resize(self, width, height): gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60., width / float(height), .01, 1000.) gl.glMatrixMode(gl.GL_MODELVIEW) self.view['ball'].place([width/2, height/2], (width+height)/2)
def set_3d(self): """ Configure OpenGL to draw in 3d. """ width, height = self.get_size() gl.glEnable(gl.GL_DEPTH_TEST) gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(65.0, width / float(height), 0.1, DIST) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() x, y = self.rotation gl.glRotatef(x, 0, 1, 0) gl.glRotatef(-y, math.cos(math.radians(x)), 0, math.sin(math.radians(x))) x, y, z = self.position gl.glTranslatef(-x, -y, -z) gl.glEnable(gl.GL_LIGHTING) gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, GLfloat4(0.05,0.05,0.05,1.0)) gl.glEnable(gl.GL_COLOR_MATERIAL) gl.glColorMaterial(gl.GL_FRONT, gl.GL_AMBIENT_AND_DIFFUSE) #gl.glLightfv(gl.GL_LIGHT1,gl.GL_SPOT_DIRECTION, GLfloat3(0,0,-1)) gl.glLightfv(gl.GL_LIGHT1, gl.GL_AMBIENT, GLfloat4(0.5,0.5,0.5,1.0)) gl.glLightfv(gl.GL_LIGHT1, gl.GL_DIFFUSE, GLfloat4(1.0,1.0,1.0,1.0)) gl.glLightfv(gl.GL_LIGHT1, gl.GL_POSITION, GLfloat4(0.35,1.0,0.65,0.0)) #gl.glLightfv(gl.GL_LIGHT0,gl.GL_SPECULAR, GLfloat4(1,1,1,1)) gl.glDisable(gl.GL_LIGHT0) gl.glEnable(gl.GL_LIGHT1)
def on_draw(self): self.clear() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() (w, h) = self.get_size() gl.glScalef( float(min(w, h))/w, -float(min(w, h))/h, 1 ) gl.gluPerspective(45.0, 1, 0.1, 1000.0) gl.gluLookAt(0, 0, 2.4, 0, 0, 0, 0, 1, 0) global render_texture render_texture = self.texture for v in self.visions.values(): gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() v.iteration() buf = pyglet.image.get_buffer_manager().get_color_buffer() rawimage = buf.get_image_data() self.texture = rawimage.get_texture() clock.tick()
def set_camera_projection(self): lander_width = self.scaleFactor * self.simulator.lander_width lander_height = self.scaleFactor * -self.simulator.lander.colliders[ 'pos'][1].min() (pad_x, pad_y) = (0.0, lander_height) (lander_x, lander_y) = self.scaleFactor * self.simulator.lander.pos self.display_width = abs(pad_x - lander_x) + 2 * lander_width self.display_height = abs(pad_y - lander_y) + 2 * lander_width if self.display_width * self.height > self.display_height * self.width: self.display_height = self.display_width * self.height / self.width else: self.display_width = self.display_height * self.width / self.height self.camera_x = (pad_x + lander_x) / 2.0 self.camera_y = (pad_y + lander_y) / 2.0 self.camera_z = self.display_height / (2.0 * math.tan(self.fov_y / 2.0)) moon_radius = self.scaleFactor * 1.7371e6 self.camera_near = self.camera_y / math.tan(self.fov_y / 2.0) # self.camera_far = 500*self.scaleFactor self.camera_far = math.sqrt(2 * moon_radius * self.camera_y) gl.glLoadIdentity() gl.gluPerspective(math.degrees(self.fov_y), self.display_width / self.display_height, self.camera_near, self.camera_far)
def predraw(w,h): gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION,vec(1,1,10, 3)) gl.glLightModelfv( gl.GL_LIGHT_MODEL_AMBIENT|gl.GL_LIGHT_MODEL_TWO_SIDE, vec(1,1,1, 1.0) ) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() #glOrtho(-1, 1, -1, 1, -1, 1) #(w,h) = self.get_size() gl.glScalef( float(min(w,h))/w, -float(min(w,h))/h, 1 ) gl.gluPerspective(45.0, 1, 0.1, 1000.0) gl.gluLookAt( camera.x, camera.y, camera.z, 0,0,0, camera.up[0], camera.up[1], camera.up[2] )
def on_resize(self, width, height): gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60., width / float(height), .01, 1000.) gl.glMatrixMode(gl.GL_MODELVIEW) self.view['ball'].place([width / 2, height / 2], (width + height) / 2)
def on_resize(self, width, height): ''' calculate perspective matrix ''' v_ar = width/float(height) usableWidth = int(min(width, height*v_ar)) usableHeight = int(min(height, width/v_ar)) ox = (width - usableWidth) // 2 oy = (height - usableHeight) // 2 glViewport(ox, oy, usableWidth, usableHeight) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60, usableWidth/float(usableHeight), 0.1, 3000.0) ''' set camera position on modelview matrix ''' glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(width/2.0, height/2.0, height/1.1566, width/2.0, height/2.0, 0, 0.0, 1.0, 0.0) ''' update scene controller with size ''' self.controller.resize(width, height) #clears to a grey. glClearColor(0.4,0.4,0.4,0.) return pyglet.event.EVENT_HANDLED
def on_draw(*args): global time, particles t = time / FRAMES window.clear() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(90, 1, 0.1, 100) gl.glTranslatef(0, 0, -10) gl.glRotatef(t * 360, 0, 1, 0) batch = pyglet.graphics.Batch() if time == FRAMES: exit() points = np.empty((K, 3)) for i in range(K): points[i] = gen_point((i + t) / K, i, t) # points = points.reshape(-1) # batch.add(len(points) // 3, pyglet.graphics.GL_POINTS, None, ("v3f", points)) # print(points) points_to_lines(points, batch, 1.2) batch.draw() save_frame(time % FRAMES) time += 1
def on_draw(): pyglet.clock.tick() window.clear() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() (w, h) = window.get_size() gl.glScalef( float(min(w, h))/w, -float(min(w, h))/h, 1 ) gl.gluPerspective(45.0, 1, 0.1, 1000.0) gl.gluLookAt(0, 0, 2.4, 0, 0, 0, 0, 1, 0) for vision in window.visions.values(): gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() vision() buf = pyglet.image.get_buffer_manager().get_color_buffer() rawimage = buf.get_image_data() window.texture = rawimage.get_texture()
def set_projection3D(self): """Sets a 3D projection mantaining the aspect ratio of the original window size""" # virtual (desired) view size vw, vh = self.get_window_size() gl.glViewport(self._offset_x, self._offset_y, self._usable_width, self._usable_height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60, self._usable_width / float(self._usable_height), 0.1, 3000.0) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.gluLookAt( vw / 2.0, vh / 2.0, vh / 1.1566, # eye vw / 2.0, vh / 2.0, 0, # center 0.0, 1.0, 0.0 # up vector )
def select_object(x, y, objects=None): from miru.context import context if objects is None: objects = context.camera.objects # following technique is adapted from # http://www.cse.msu.edu/~cse872/tutorial9.html w = context.window.width h = context.window.height select_buffer = ctypes.cast((100 * gl.GLuint)(), ctypes.POINTER(gl.GLuint)) gl.glSelectBuffer(100, select_buffer) viewport = (4 * gl.GLint)() gl.glGetIntegerv(gl.GL_VIEWPORT, viewport) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() # rotate the camera first angle = context.camera.angle gl.glRotatef(angle.z, 0, 0, 1) gl.glRotatef(angle.y, 0, 1, 0) gl.glRotatef(angle.x, 1, 0, 0) gl.gluPickMatrix(x, y, 3, 3, viewport) gl.glRenderMode(gl.GL_SELECT) gl.gluPerspective(45., w / float(h), 0.1, 1000.) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glInitNames() gl.glPushName(-1) context.camera.render(select_pass=1, visible=objects) gl.glFlush() hits = gl.glRenderMode(gl.GL_RENDER) gl.glPopName() selected = None if hits: try: m = sys.maxint << 100 idx = 0 for i in range(0, 100, 4): if not select_buffer[i]: selected = objects[idx] break m = min(select_buffer[i+1], m) if m == select_buffer[i+1]: idx = select_buffer[i+3] except IndexError: pass context.window.on_resize(context.window.width, context.window.height) return selected
def on_resize(self, width, height): """ Handle resized windows. """ try: # for high DPI screens viewport size # will be different then the passed size width, height = self.get_viewport_size() except BaseException: # older versions of pyglet may not have this pass # set the new viewport size gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() # get field of view from camera fovY = self.scene.camera.fov[1] gl.gluPerspective(fovY, width / float(height), .01, self.scene.scale * 5.0) gl.glMatrixMode(gl.GL_MODELVIEW) self.view['ball'].place([width / 2, height / 2], (width + height) / 2)
def set_3d(width, height): gl.glEnable(gl.GL_DEPTH_TEST) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(65, width / height, 0.1, 60) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity()
def on_resize(self, width, height): glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60., float(width) / height, 1., 100.) glMatrixMode(GL_MODELVIEW) return True
def on_resize(width, height): # Override the default on_resize handler to create a 3D projection glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60., width / float(height), .1, 1000.) glMatrixMode(GL_MODELVIEW) return EVENT_HANDLED
def on_resize(self, width, height, x=0, y=0): gl.glViewport(x, y, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() height = height or 1 gl.gluPerspective(45., width / float(height), 0.1, 1000.) gl.glMatrixMode(gl.GL_MODELVIEW) self._setLightsAndEffects()
def on_resize(self, width, height): super(Window, self).on_resize(width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() glEnable(GL_DEPTH_TEST) gluPerspective(60., self.perspective, 1., 1000.) glMatrixMode(GL_MODELVIEW) return True
def gl_resize(w, h, fov, z_near, z_far): gl.glViewport(0, 0, w, h) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() aspect_ratio = float(w) / float(h) gl.gluPerspective(fov[1], aspect_ratio, z_near, z_far) gl.glMatrixMode(gl.GL_MODELVIEW)
def on_resize(self, width, height): """ The window was resized. """ glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(FOV, width / height, NEAR_PLANE, FAR_PLANE) glMatrixMode(GL_MODELVIEW) glShadeModel(GL_SMOOTH)
def _set_3d_projection(cls): gl.glViewport(director._offset_x, director._offset_y, director._usable_width, director._usable_height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective( 60, 1.0 * director._usable_width / director._usable_height, 0.1, 3000.0) gl.glMatrixMode(gl.GL_MODELVIEW)
def defaultView(self, width, height): self.width = width self.height = height gl.glViewport(0, 0, self.width, self.height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(self.fieldofview, self.width / float(self.height), self.clipnear, self.clipfar) gl.glMatrixMode(gl.GL_MODELVIEW)
def on_resize(self, width, height): width, height = self.get_size() self.label_update() glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45.0, width / float(height), 0.1, 60.0) glMatrixMode(GL_MODELVIEW) return pyglet.event.EVENT_HANDLED
def focus(self, width, height): """Sets the projection matrix. Usefull for window.on_resize event @param width: width of resized window @param height: height of resized window""" self.width = width self.height = height glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() aspect = width/float(height) gluPerspective(self.fovy, aspect, 0.01, 1024.0 )
def set_up_perspective(self): glViewport(0, 0, self.width * 2, self.height * 2) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glEnable(GL_DEPTH_TEST) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60., self.perspective, 1., 1000.) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glRotatef(90, 1, 0, 0)
def focus(self, width, height): """Sets the projection matrix. Usefull for window.on_resize event @param width: width of resized window @param height: height of resized window""" self.width = width self.height = height glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() aspect = width / float(height) gluPerspective(self.fovy, aspect, 0.01, 1024.0)
def on_resize(self, width, height): gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() aspect_ratio = width / height gl.gluPerspective(35, aspect_ratio, 1, 100) # gl.glOrtho(-3, 3, -3, 3, -3, 3) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity()
def setup_projection(self): pgl.glMatrixMode(pgl.GL_PROJECTION) pgl.glLoadIdentity() if self.ortho: # yep, this is pseudo ortho (don't tell anyone) pgl.gluPerspective( 0.3, float(self.window.width)/float(self.window.height), self.min_ortho_dist - 0.01, self.max_ortho_dist + 0.01) else: pgl.gluPerspective( 30.0, float(self.window.width)/float(self.window.height), self.min_dist - 0.01, self.max_dist + 0.01) pgl.glMatrixMode(pgl.GL_MODELVIEW)
def focus(self, width, height): aspect = width / height glMatrixMode(GL_PROJECTION) glLoadIdentity() glViewport(0, 0, width, height) gluPerspective(self._fov * self._zoom, aspect, self._near, self._far) glScalef(self._scalex, self._scaley, self._scalez) glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(self._x, self._y, self._z, self._target.x, self._target.y, self._target.z, 0.0, 1.0, 0.0)
def set_3d(self): width, height = self.get_size() glEnable(GL_DEPTH_TEST) glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(65.0, width / float(height), 0.1, 60.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() x, y = self.rotation glRotatef(x, 0, 1, 0) glRotatef(-y, math.cos(math.radians(x)), 0, math.sin(math.radians(x))) x, y, z = self.position glTranslatef(-x, -y, -z)
def set_3d(self, size): """Configure OpenGL to draw in 3d.""" width, height = size GL.glEnable(GL.GL_DEPTH_TEST) GL.glViewport(0, 0, width, height) GL.glMatrixMode(GL.GL_PROJECTION) GL.glLoadIdentity() GL.gluPerspective(65.0, width / float(height), 0.1, 60.0) GL.glMatrixMode(GL.GL_MODELVIEW) GL.glLoadIdentity() x, y = self.player.rotation GL.glRotatef(x, 0, 1, 0) GL.glRotatef(-y, math.cos(math.radians(x)), 0, math.sin(math.radians(x))) x, y, z = self.player.position GL.glTranslatef(-x, -y, -z)
def setup_projection(self): pgl.glMatrixMode(pgl.GL_PROJECTION) pgl.glLoadIdentity() if self.ortho: # yep, this is pseudo ortho (don't tell anyone) pgl.gluPerspective( 0.3, float(self.window.width) / float(self.window.height), self.min_ortho_dist - 0.01, self.max_ortho_dist + 0.01) else: pgl.gluPerspective( 30.0, float(self.window.width) / float(self.window.height), self.min_dist - 0.01, self.max_dist + 0.01) pgl.glMatrixMode(pgl.GL_MODELVIEW)
def focus(self, width, height): aspect = width/height glMatrixMode(GL_PROJECTION) glLoadIdentity() glViewport(0, 0, width, height) gluPerspective(self._fov*self._zoom, aspect, self._near, self._far) glScalef(self._scalex, self._scaley, self._scalez) glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt( self._x, self._y, self._z, self._target.x, self._target.y, self._target.z, 0.0, 1.0, 0.0)
def set_projection3D(self): """Sets a 3D projection mantaining the aspect ratio of the original window size""" # virtual (desired) view size vw, vh = self.get_window_size() gl.glViewport(self._offset_x, self._offset_y, self._usable_width, self._usable_height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60, self._usable_width / float(self._usable_height), 0.1, 3000.0) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.gluLookAt(vw / 2.0, vh / 2.0, vh / 1.1566, # eye vw / 2.0, vh / 2.0, 0, # center 0.0, 1.0, 0.0 # up vector )
def enable_3d(self): gl.glViewport(0, 0, self.width, self.height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60., self.width / float(self.height), .1, 1000.) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glDepthFunc(gl.GL_LEQUAL) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_CULL_FACE) # update the camera self.camera.look() # TODO: probably find a better place to enable and configure these # TODO TODO: Just use shader-based lighting anyway. gl.glEnable(gl.GL_LIGHTING) gl.glEnable(gl.GL_LIGHT0) gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, (gl.GLfloat * 4)(0, 0, 100, 1))
def apply(self): gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(self.yFov_rad * 180 / pi, self.xFov_rad / self.yFov_rad, self.zNear, self.zFar) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.gluLookAt( self.eye[0], self.eye[1], self.eye[2], self.tgt[0], self.tgt[1], self.tgt[2], self.up[0], self.up[1], self.up[2], )
def on_draw(): # Reset # ===== gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() # Camera view # =========== gl.gluPerspective(70, window.width / window.height, 1, 1000) gl.gluLookAt(2, 1, 1, 0, 0, 0, 0, 0, 1) # Draw the cube # ============= gl.glBegin(gl.GL_QUADS) # Left face gl.glColor3f(1.0, 0, 0) gl.glVertex3f(1, -1, -1) gl.glVertex3f(1, -1, 1) gl.glVertex3f(-1, -1, 1) gl.glVertex3f(-1, -1, -1) # Right face gl.glColor3f(0, 1.0, 0) gl.glVertex3f(-1, -1, 1) gl.glVertex3f(-1, 1, 1) gl.glVertex3f(-1, 1, -1) gl.glVertex3f(-1, -1, -1) #Bottom face gl.glColor3f(0, 0, 1.0) gl.glVertex3f(1, -1, -1) gl.glVertex3f(-1, -1, -1) gl.glVertex3f(-1, 1, -1) gl.glVertex3f(1, 1, -1) gl.glEnd() gl.glColor3f(0, 0, 0) pointer = gl.gluNewQuadric() gl.gluSphere(pointer, 0.1, 20, 20)
def OnReshape(self): """Reshape the OpenGL viewport based on the size of the window""" size = self.GetClientSize() oldwidth, oldheight = self.width, self.height width, height = size.width, size.height if width < 1 or height < 1: return self.width = max(float(width), 1.0) self.height = max(float(height), 1.0) self.OnInitGL(call_reshape = False) glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() if self.orthographic: glOrtho(-width / 2, width / 2, -height / 2, height / 2, -5 * self.dist, 5 * self.dist) else: gluPerspective(60., float(width) / height, 10.0, 3 * self.dist) glTranslatef(0, 0, -self.dist) # Move back glMatrixMode(GL_MODELVIEW) if not self.mview_initialized: self.reset_mview(0.9) self.mview_initialized = True elif oldwidth is not None and oldheight is not None: wratio = self.width / oldwidth hratio = self.height / oldheight factor = min(wratio * self.zoomed_width, hratio * self.zoomed_height) x, y, _ = self.mouse_to_3d(self.width / 2, self.height / 2) self.zoom(factor, (x, y)) self.zoomed_width *= wratio / factor self.zoomed_height *= hratio / factor # Wrap text to the width of the window if self.GLinitialized: self.pygletcontext.set_current() self.update_object_resize()
def _set_view(self): left = int(self._pixel_per_point[0] * self.rect.left) bottom = int(self._pixel_per_point[1] * self.rect.bottom) width = int(self._pixel_per_point[0] * self.rect.width) height = int(self._pixel_per_point[1] * self.rect.height) gl.glPushAttrib(gl.GL_ENABLE_BIT) gl.glEnable(gl.GL_SCISSOR_TEST) gl.glScissor(left, bottom, width, height) self._mode = (gl.GLint)() gl.glGetIntegerv(gl.GL_MATRIX_MODE, self._mode) self._viewport = (gl.GLint * 4)() gl.glGetIntegerv(gl.GL_VIEWPORT, self._viewport) gl.glViewport(left, bottom, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() near = 0.01 far = 1000. gl.gluPerspective(self.scene.camera.fov[1], width / height, near, far) gl.glMatrixMode(gl.GL_MODELVIEW)
def generate_map(self): scene_radius = 95.0 light_to_scene_distance = math.sqrt( light_pos[0]**2 + light_pos[1]**2 + light_pos[2]**2) near_plane = light_to_scene_distance - scene_radius field_of_view = math3d.radians_to_degrees(2.0 * math.atan(scene_radius / float(light_to_scene_distance))) tmp = (gl.GLfloat * 16)() gl.glMatrixMode( gl.GL_PROJECTION ) gl.glLoadIdentity() gl.gluPerspective( field_of_view, 1.0, near_plane, near_plane + (2.0 * scene_radius) ) gl.glGetFloatv( gl.GL_PROJECTION_MATRIX, tmp ) gl.light_projection = euclid.Matrix4() gl.light_projection[0:16] = tmp[:] # Switch to light's point of view gl.glMatrixMode( gl.GL_MODELVIEW ) gl.glLoadIdentity() gl.gluLookAt( self.light.pos.x, self.light.pos.y, self.light.pos.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ) gl.glGetFloatv( gl.GL_MODELVIEW_MATRIX, tmp ) light_mview = euclid.Matrix4() light_mview[0:16] = tmp[:] #glViewport(0, 0, shadow_width, shadow_height) # Clear the depth buffer only gl.glClear( gl.GL_DEPTH_BUFFER_BIT ) # Remember the current shade model gl.glPushAttrib( gl.GL_ENABLE_BIT ) prev_shade_model = cypes.GLint() gl.glGetIntegerv( gl.GL_SHADE_MODEL, prev_shade_model ) # All we care about here is resulting depth values gl.glShadeModel( gl.GL_FLAT ) gl.glDisable( gl.GL_LIGHTING ) gl.glDisable( gl.GL_COLOR_MATERIAL ) gl.glDisable( gl.GL_NORMALIZE ) gl.glColorMask( 0, 0, 0, 0 ) # Overcome imprecision gl.glEnable( gl.GL_POLYGON_OFFSET_FILL ) #draw_models(False) self.context.render( exclude=self.exclude_shadows ) # Copy depth values into depth texture gl.glCopyTexImage2D( gl.GL_TEXTURE_2D, 0, gl.GL_DEPTH_COMPONENT, 0, 0, shadow_width, shadow_height, 0 ) # Restore normal drawing state gl.glShadeModel( gl.GL_SMOOTH ) gl.glEnable( gl.GL_LIGHTING ) gl.glEnable( gl.GL_COLOR_MATERIAL ) gl.glEnable( gl.GL_NORMALIZE ) gl.glColorMask( 1, 1, 1, 1 ) gl.glDisable( gl.GL_POLYGON_OFFSET_FILL ) # Setup up the texture matrix which will be use in eye-linear # texture mapping self.tex_matrix = euclid.Matrix4() self.tex_matrix.translate(0.5, 0.5, 0.5).scale(0.5, 0.5, 0.5) tmp_matrix.scale(0.5, 0.5, 0.5) tex_matrix = (tmp_matrix * light_projection) * light_mview self.tex_matrix.transpose() # Give us immediate access to ctypes arrays self.tex_matrix = ( (gl.GLfloat * 4)(*self.tex_matrix[0:4]), (gl.GLfloat * 4)(*self.tex_matrix[4:8]), (gl.GLfloat * 4)(*self.tex_matrix[8:12]), (gl.GLfloat * 4)(*self.tex_matrix[12:16]) )
def perspective(self): gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(self.fov, self.w / self.h, 0.1, self.far) gl.glMatrixMode(gl.GL_MODELVIEW)
def on_draw(): window.clear() gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_LINE_SMOOTH) width, height = window.get_size() gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60, width / float(height), 0.01, 20) gl.glMatrixMode(gl.GL_TEXTURE) gl.glLoadIdentity() # texcoords are [0..1] and relative to top-left pixel corner, add 0.5 to center gl.glTranslatef(0.5 / image_data.width, 0.5 / image_data.height, 0) # texture size may be increased by pyglet to a power of 2 tw, th = image_data.texture.owner.width, image_data.texture.owner.height gl.glScalef(image_data.width / float(tw), image_data.height / float(th), 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.gluLookAt(0, 0, 0, 0, 0, 1, 0, -1, 0) gl.glTranslatef(0, 0, state.distance) gl.glRotated(state.pitch, 1, 0, 0) gl.glRotated(state.yaw, 0, 1, 0) if any(state.mouse_btns): axes(0.1, 4) gl.glTranslatef(0, 0, -state.distance) gl.glTranslatef(*state.translation) gl.glColor3f(0.5, 0.5, 0.5) gl.glPushMatrix() gl.glTranslatef(0, 0.5, 0.5) grid() gl.glPopMatrix() psz = max(window.get_size()) / float(max(w, h)) if state.scale else 1 gl.glPointSize(psz) distance = (0, 0, 1) if state.attenuation else (1, 0, 0) gl.glPointParameterfv(gl.GL_POINT_DISTANCE_ATTENUATION, (gl.GLfloat * 3)(*distance)) if state.lighting: ldir = [0.5, 0.5, 0.5] # world-space lighting ldir = np.dot(state.rotation, (0, 0, 1)) # MeshLab style lighting ldir = list(ldir) + [0] # w=0, directional light gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, (gl.GLfloat * 4)(*ldir)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, (gl.GLfloat * 3)(1.0, 1.0, 1.0)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, (gl.GLfloat * 3)(0.75, 0.75, 0.75)) gl.glEnable(gl.GL_LIGHT0) gl.glEnable(gl.GL_NORMALIZE) gl.glEnable(gl.GL_LIGHTING) gl.glColor3f(1, 1, 1) texture = image_data.get_texture() gl.glEnable(texture.target) gl.glBindTexture(texture.target, texture.id) gl.glTexParameteri( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST) # comment this to get round points with MSAA on gl.glEnable(gl.GL_POINT_SPRITE) if not state.scale and not state.attenuation: gl.glDisable(gl.GL_MULTISAMPLE) # for true 1px points with MSAA on vertex_list.draw(gl.GL_POINTS) gl.glDisable(texture.target) if not state.scale and not state.attenuation: gl.glEnable(gl.GL_MULTISAMPLE) gl.glDisable(gl.GL_LIGHTING) gl.glColor3f(0.25, 0.25, 0.25) frustum(depth_intrinsics) axes() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, width, 0, height, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.glMatrixMode(gl.GL_TEXTURE) gl.glLoadIdentity() gl.glDisable(gl.GL_DEPTH_TEST) fps_display.draw()
def on_resize(self, width, height): gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.gluPerspective(45.0, 1.0 * width / height, 0.1, 100.0) gl.glMatrixMode(gl.GL_MODELVIEW)
def _set_3d_projection(cls): gl.glViewport(director._offset_x, director._offset_y, director._usable_width, director._usable_height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60, 1.0 * director._usable_width / director._usable_height, 0.1, 3000.0) gl.glMatrixMode(gl.GL_MODELVIEW)
def on_resize(width, height): gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60.0, float(width)/height, 1.0, 100.0) gl.glMatrixMode(gl.GL_MODELVIEW) return True