def draw_triangle_strip(self, *data): glBegin(GL_TRIANGLE_STRIP) for normal, vectors in data: glNormal3fv(normal) for vector in vectors: glVertex(vector) glEnd()
def draw_quad_strip(self, *data): glBegin(GL_QUAD_STRIP) for normal, vectors in data: glNormal3fv(normal) for vector in vectors: glVertex(vector) glEnd()
def draw_polygon(self, *data): glBegin(GL_POLYGON) for normal, vectors in data: glNormal3fv(normal) for vector in vectors: glVertex(vector) glEnd()
def draw_filled_rect(origin, dx, dy, color): ## print 'draw_filled_rect',(origin, dx, dy, color) #####@@@@@ glDisable(GL_LIGHTING) # this allows the specified color to work. Otherwise it doesn't work (I always get dark blue). Why??? # guess: if i want lighting, i have to specify a materialcolor, not just a regular color. (and vertex normals) try: len(color) except: print "following exception in len(color) for color = %r" % (color,) # 061212 -- why didn't caller's fix_color catch it? ##k raise if len(color) == 4: glColor4fv(color) if 0 and color[3] != 1.0: print "color has alpha",color ####@@@@ else: glColor3fv(color) ## glRectfv(origin, origin + dx + dy) # won't work for most coords! also, ignores Z. color still not working. glBegin(GL_QUADS) glVertex3fv(origin) #glColor3fv(white)# glVertex3fv(origin + dx) # glColor3fv(white) # hack, see if works - yes! #glColor3fv(color)# glVertex3fv(origin + dx + dy) #glColor3fv(white)# glVertex3fv(origin + dy) glEnd() glEnable(GL_LIGHTING) # should be outside of glEnd! when inside, i got infloop! (not sure that was why; quit/reran after that)
def _paintGLGrid(self): resolutionMillimeters = 1 griddedAreaSize = 100 glLineWidth(1.0) glBegin(GL_LINES) glColor3f(1.0, 1.0, 1.0) glVertex3f(griddedAreaSize, 0, 0) glVertex3f(-griddedAreaSize, 0, 0) glVertex3f(0, griddedAreaSize, 0) glVertex3f(0, -griddedAreaSize, 0) numOfLines = int(griddedAreaSize / resolutionMillimeters) for i in range(numOfLines): glVertex3f(resolutionMillimeters * i, -griddedAreaSize, 0) glVertex3f(resolutionMillimeters * i, griddedAreaSize, 0) glVertex3f(griddedAreaSize, resolutionMillimeters * i, 0) glVertex3f(-griddedAreaSize, resolutionMillimeters * i, 0) glVertex3f(resolutionMillimeters * (-i), -griddedAreaSize, 0) glVertex3f(resolutionMillimeters * (-i), griddedAreaSize, 0) glVertex3f(griddedAreaSize, resolutionMillimeters * (-i), 0) glVertex3f(-griddedAreaSize, resolutionMillimeters * (-i), 0) glEnd()
def drawTexture(self, texture, dx, dy, dw, dh, x, y, w, h, r): ''' texture is an int textureRect is a list of size 4, determines which square to take from the texture drawRect is a list of size 4, is used to determine the drawing size ''' glBindTexture(self.texext, texture) glPushMatrix() glTranslatef(dx + dw / 2, dy + dh / 2, 0) glRotatef(r, 0, 0, 1.0) glTranslatef(-1 * (dx + dw / 2), -1 * (dy + dh / 2), 0) glBegin(GL_QUADS) #Top-left vertex (corner) glTexCoord2f(x, y + h) #image/texture glVertex3f(dx, dy, 0) #screen coordinates #Bottom-left vertex (corner) glTexCoord2f(x + w, y + h) glVertex3f((dx + dw), dy, 0) #Bottom-right vertex (corner) glTexCoord2f(x + w, y) glVertex3f((dx + dw), (dy + dh), 0) #Top-right vertex (corner) glTexCoord2f(x, y) glVertex3f(dx, (dy + dh), 0) glEnd() glPopMatrix()
def make_cube(): glNewList(G_OBJ_CUBE, GL_COMPILE) vertices = [((-0.5, -0.5, -0.5), (-0.5, -0.5, 0.5), (-0.5, 0.5, 0.5), (-0.5, 0.5, -0.5)), ((-0.5, -0.5, -0.5), (-0.5, 0.5, -0.5), (0.5, 0.5, -0.5), (0.5, -0.5, -0.5)), ((0.5, -0.5, -0.5), (0.5, 0.5, -0.5), (0.5, 0.5, 0.5), (0.5, -0.5, 0.5)), ((-0.5, -0.5, 0.5), (0.5, -0.5, 0.5), (0.5, 0.5, 0.5), (-0.5, 0.5, 0.5)), ((-0.5, -0.5, 0.5), (-0.5, -0.5, -0.5), (0.5, -0.5, -0.5), (0.5, -0.5, 0.5)), ((-0.5, 0.5, -0.5), (-0.5, 0.5, 0.5), (0.5, 0.5, 0.5), (0.5, 0.5, -0.5))] normals = [(-1.0, 0.0, 0.0), (0.0, 0.0, -1.0), (1.0, 0.0, 0.0), (0.0, 0.0, 1.0), (0.0, -1.0, 0.0), (0.0, 1.0, 0.0)] glBegin(GL_QUADS) for i in range(6): glNormal3f(normals[i][0], normals[i][1], normals[i][2]) for j in range(4): glVertex3f(vertices[i][j][0], vertices[i][j][1], vertices[i][j][2]) glEnd() glEndList()
def draw_lines(self): """ draw our line segments, using our current style attrs [which are partly nim] """ # find variables which determine our GL state color = self.fix_color(self.linecolor) dashEnabled = self.dashed width = self.linewidth # set temporary GL state (code copied from drawer.drawline) glDisable(GL_LIGHTING) glColor3fv(color) if dashEnabled: glLineStipple(1, 0xAAAA) glEnable(GL_LINE_STIPPLE) if width != 1: glLineWidth(width) # draw the lines if self._closed_state: glBegin(GL_LINE_LOOP) else: glBegin(GL_LINE_STRIP) for pos in self.points: glVertex3fv(pos) # [note from old code: could be pos + origin if that can matter] glEnd() # restore default GL state [some of this might be moved up to callers] if width != 1: glLineWidth(1.0) if dashEnabled: glDisable(GL_LINE_STIPPLE) glEnable(GL_LIGHTING) return
def desenhar(self): glPushMatrix() glLoadIdentity() glTranslatef(*self.posicao) glColor3fv(self.cor) glBegin(GL_LINES) #desenha moldura glVertex3fv((0, 0, 0)) glVertex3fv((self.largura, 0, 0)) glVertex3fv((self.largura, 0, 0)) glVertex3fv((self.largura, self.altura, 0)) glVertex3fv((self.largura, self.altura, 0)) glVertex3fv((0, self.altura, 0)) glVertex3fv((0, self.altura, 0)) glVertex3fv((0, 0, 0)) i = 0 while (i < len(self.pontos) - 1): glVertex3fv((self.pontos[i][0]*self.escala_x, self.pontos[i][1]*self.escala_y, 0)) glVertex3fv((self.pontos[i + 1][0]*self.escala_x, self.pontos[i + 1][1]*self.escala_y, 0)) i += 1 glEnd() glPopMatrix()
def plano_vertical_arriba(self): if self.programa.ajustes.ver_plano_vertical.isChecked(): glBegin(GL_QUADS) glColor(self.programa.ajustes.color_plano_vertical) for vertex in range(4): glVertex(self.vertices_plano_vertical_arriba[vertex]) glEnd()
def _make_list( self ): """generate opengl display list to draw triangles in mesh """ # get available list name self.list_name = glGenLists( 1 ) # start new display list glNewList( self.list_name, GL_COMPILE ) # set material glMaterialfv( GL_FRONT, GL_SPECULAR, self.specular ) glMaterialfv( GL_FRONT, GL_SHININESS, self.shininess ) glMaterialfv( GL_FRONT, GL_DIFFUSE, self.diffuse ) # start list of triangles in mesh glBegin( GL_TRIANGLES ) # for each triangle give normal and 3 vertices for triangle in self.triangles: glNormal3f( *triangle[0] ) for i in range( 1, 4 ): glVertex3f( *triangle[i] ) glEnd() glEndList()
def draw_textured_rect_subtriangle( origin, dx, dy, tex_origin, tex_dx, tex_dy, points ): # 070404 modified from draw_textured_rect """ Like draw_textured_rect, but draw only the sub-triangle of the same rect (textured in the same way), where the subtriangle has relative 2d vertices as specified inside that rect (treating its own coords as each in [0.0, 1.0]). WARNING: depending on the glEnables set up by the caller, the sub-triangle coords might need to be in CCW winding order for the triangle to be visible from the front. """ # e could easily generalize API to polygon, and this implem to convex polygon, if desired ##e WARNING: this function's name and api are likely to be revised; # or we might just replace the whole scheme, using things like Textured(Triangle(...),...) instead, # perhaps implemented by telling OpenGL how to compute the texture coords in a wrapper, then just drawing the triangle. assert len(points) == 3 # and each point should be a V of length 2, or a 2-tuple, with elements convertible to floats -- this is assumed below glEnable(GL_TEXTURE_2D) glBegin(GL_TRIANGLES) for px, py in points: px = float(px) py = float(py) glTexCoord2fv((tex_origin + px * tex_dx + py * tex_dy).tolist()) # glVertex3fv(origin + px * dx + py * dy) glEnd() glDisable(GL_TEXTURE_2D)
def draw_textured_square(w=None, h=None): """ Draws a texture square of 2 x 2 size centered at 0, 0 Make sure to call glEnable(GL_TEXTURE_RECTANGLE_ARB) first. :param w: width of the image in pixels :param h: height of the image in pixels """ if w is None or h is None: glBegin(GL_QUADS) glTexCoord2f(0.0, 0.0) glVertex2f(-1.0, -1.0) # Bottom Left Of The Texture and Quad glTexCoord2f(1.0, 0.0) glVertex2f(1.0, -1.0) # Bottom Right Of The Texture and Quad glTexCoord2f(1.0, 1.0) glVertex2f(1.0, 1.0) # Top Right Of The Texture and Quad glTexCoord2f(0.0, 1.0) glVertex2f(-1.0, 1.0) # Top Left Of The Texture and Quad glEnd() else: glBegin(GL_QUADS) glTexCoord2f(0.0, 0.0) glVertex2f(-1.0, -1.0) # Bottom Left glTexCoord2f(w, 0.0) glVertex2f(1.0, -1.0) # Bottom Right glTexCoord2f(w, h) glVertex2f(1.0, 1.0) # Top Right glTexCoord2f(0.0, h) glVertex2f(-1.0, 1.0) # Top Left glEnd()
def drawrectangle(pt1, pt2, rt, up, color): """ Draws a (hollow) rectangle outline of the given I{color}. @param pt1: First corner of the rectangle. @type pt1: Point @param pt1: Opposite corner of the rectangle. @type pt1: Point @param rt: Right vector of the glpane. @type rt: Unit vector @param up: Right vector of the glpane. @type up: Unit vector @param color: Color @type color: color """ glColor3f(color[0], color[1], color[2]) glDisable(GL_LIGHTING) c2 = pt1 + rt * Numeric.dot(rt, pt2 - pt1) c3 = pt1 + up * Numeric.dot(up, pt2 - pt1) glBegin(GL_LINE_LOOP) glVertex(pt1[0], pt1[1], pt1[2]) glVertex(c2[0], c2[1], c2[2]) glVertex(pt2[0], pt2[1], pt2[2]) glVertex(c3[0], c3[1], c3[2]) glEnd() glEnable(GL_LIGHTING)
def draw_filled_rect(origin, dx, dy, color): ## print 'draw_filled_rect',(origin, dx, dy, color) #####@@@@@ glDisable( GL_LIGHTING ) # this allows the specified color to work. Otherwise it doesn't work (I always get dark blue). Why??? # guess: if i want lighting, i have to specify a materialcolor, not just a regular color. (and vertex normals) try: len(color) except: print "following exception in len(color) for color = %r" % ( color, ) # 061212 -- why didn't caller's fix_color catch it? ##k raise if len(color) == 4: glColor4fv(color) if 0 and color[3] != 1.0: print "color has alpha", color ####@@@@ else: glColor3fv(color) ## glRectfv(origin, origin + dx + dy) # won't work for most coords! also, ignores Z. color still not working. glBegin(GL_QUADS) glVertex3fv(origin) # glColor3fv(white)# glVertex3fv(origin + dx) # glColor3fv(white) # hack, see if works - yes! # glColor3fv(color)# glVertex3fv(origin + dx + dy) # glColor3fv(white)# glVertex3fv(origin + dy) glEnd() glEnable( GL_LIGHTING ) # should be outside of glEnd! when inside, i got infloop! (not sure that was why; quit/reran after that)
def drawPoint(color, point, pointSize=3.0, isRound=True): """ Draw a point using GL_POINTS. @param point: The x,y,z coordinate array/ vector of the point @type point: A or V @param pointSize: The point size to be used by glPointSize @type pointSize: float @param isRound: If True, the point will be drawn round otherwise square @type isRound: boolean """ glDisable(GL_LIGHTING) glColor3fv(color) glPointSize(float(pointSize)) if isRound: glEnable(GL_POINT_SMOOTH) glBegin(GL_POINTS) glVertex3fv(point) glEnd() if isRound: glDisable(GL_POINT_SMOOTH) glEnable(GL_LIGHTING) if pointSize != 1.0: glPointSize(1.0) return
def drawFullWindow(vtColors): """ Draw gradient background color. <vtColors> is a 4 element list specifying colors for the left-down, right-down, right-up, left-up window corners. To draw the full window, the modelview and projection should be set in identity. """ from utilities.constants import GL_FAR_Z glDisable(GL_LIGHTING) glBegin(GL_QUADS) glColor3fv(vtColors[0]) glVertex3f(-1, -1, GL_FAR_Z) glColor3fv(vtColors[1]) glVertex3f(1, -1, GL_FAR_Z) glColor3fv(vtColors[2]) glVertex3f(1, 1, GL_FAR_Z) glColor3fv(vtColors[3]) glVertex3f(-1, 1, GL_FAR_Z) glEnd() glEnable(GL_LIGHTING) return
def _draw_zero_plane_xy(self, color, texture_id=None, scale=1.0): glPushMatrix() # glColor3f(*rgb_to_f(*color)) apply_texture = texture_id is not None if apply_texture: glColor3f(1, 1, 1) glBindTexture(GL_TEXTURE_2D, self._textures[texture_id]) glTexCoord2f(1.0 * scale, 0.0 * scale) else: glColor3f(*rgb_to_f(*color)) glBegin(GL_POLYGON) size = self._size / 2 glVertex3f(size, size, 0) if apply_texture: glTexCoord2f(1.0 * scale, 1.0 * scale) glVertex3f(size, -size, 0) if apply_texture: glTexCoord2f(0.0 * scale, 1.0 * scale) glVertex3f(-size, -size, 0) if apply_texture: glTexCoord2f(0.0 * scale, 0.0 * scale) glVertex3f(-size, size, 0) # if texture is not None: # glBindTexture(GL_TEXTURE_2D, ) glEnd() glPopMatrix()
def _do_paint_rgb24(self, img_data, x, y, width, height, rowstride, options, callbacks): debug( "%s._do_paint_rgb24(x=%d, y=%d, width=%d, height=%d, rowstride=%d)", self, x, y, width, height, rowstride) drawable = self.gl_init() if not drawable: debug("%s._do_paint_rgb24(..) drawable is not set!", self) return False try: self.set_rgb24_paint_state() # Compute alignment and row length row_length = 0 alignment = 1 for a in [2, 4, 8]: # Check if we are a-aligned - ! (var & 0x1) means 2-aligned or better, 0x3 - 4-aligned and so on if (rowstride & a - 1) == 0: alignment = a # If number of extra bytes is greater than the alignment value, # then we also have to set row_length # Otherwise it remains at 0 (= width implicitely) if (rowstride - width * 3) > a: row_length = width + (rowstride - width * 3) / 3 self.gl_marker( "RGB24 update at %d,%d, size %d,%d, stride is %d, row length %d, alignment %d" % (x, y, width, height, rowstride, row_length, alignment)) # Upload data as temporary RGB texture glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_RGB]) glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length) glPixelStorei(GL_UNPACK_ALIGNMENT, alignment) glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0) glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, 4, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data) # Draw textured RGB quad at the right coordinates glBegin(GL_QUADS) glTexCoord2i(0, 0) glVertex2i(x, y) glTexCoord2i(0, height) glVertex2i(x, y + height) glTexCoord2i(width, height) glVertex2i(x + width, y + height) glTexCoord2i(width, 0) glVertex2i(x + width, y) glEnd() # Present update to screen self.present_fbo(drawable) # present_fbo has reset state already finally: drawable.gl_end() return True
def Render(self): from OpenGL.GL import glBegin, glEnd, glVertex2f, GL_TRIANGLES glBegin(GL_TRIANGLES) glVertex2f(self.vertex_a.x, self.vertex_a.y) glVertex2f(self.vertex_b.x, self.vertex_b.y) glVertex2f(self.vertex_c.x, self.vertex_c.y) glEnd()
def draw_bottom_circle(self): glBegin(GL_TRIANGLE_FAN) glVertex3fv(self.bottom_point) for vertex in self.surfaces[1]: glVertex3fv(self.vertices[vertex]) glColor3fv(self.color) glEnd()
def paint(self): glColor3f(0.0, 0.0, 0.6) glLineWidth(1.0); glBegin(GL_LINES) glVertex3f(*self.obj_a.position) glVertex3f(*self.obj_b.position) glEnd()
def _do_paint_rgb24(self, img_data, x, y, w, h, rowstride, options, callbacks): log("do_paint_rgb24(%s bytes, %s, %s, %s, %s, %s, %s, %s)", len(img_data), x, y, w, h, rowstride, options, callbacks) ww, wh = self.size if x+w>ww or y+h>wh: log("do_paint_rgb24: ignoring paint which would overflow the backing area") return drawable = self.gl_init() if not drawable: log("do_paint_rgb24: cannot paint yet..") return try: #cleanup if we were doing yuv previously: if self.pixel_format!=GLPixmapBacking.RGB24: self.remove_shader() self.pixel_format = GLPixmapBacking.RGB24 glEnable(GL_TEXTURE_RECTANGLE_ARB) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[0]) glPixelStorei(GL_UNPACK_ROW_LENGTH, rowstride/3) for texture in (GL_TEXTURE1, GL_TEXTURE2): glActiveTexture(texture) glDisable(GL_TEXTURE_RECTANGLE_ARB) glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, x, y, w, h, GL_RGB, GL_UNSIGNED_BYTE, img_data) glBegin(GL_QUADS) for rx,ry in ((x, y), (x, y+h), (x+w, y+h), (x+w, y)): glTexCoord2i(rx, ry) glVertex2i(rx, ry) glEnd() finally: self.gl_end(drawable)
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 plano_horizontal_detras(self): if self.programa.ajustes.ver_plano_horizontal.isChecked(): glBegin(GL_QUADS) glColor(self.programa.ajustes.color_plano_horizontal) for vertex in range(4): glVertex(self.vertices_plano_horizontal_detras[vertex]) glEnd()
def swapBuffers(self): # first call the swap on the QGLWidget start = long(now() * 1000) self.glw.swapBuffers() #self.glw.makeCurrent() # The following is taken from the PsychToolbox # Draw a single pixel in left-top area of back-buffer. # This will wait/stall the rendering pipeline # until the buffer flip has happened, aka immediately after the VBL has started. # We need the pixel as "synchronization token", so the following glFinish() really # waits for VBL instead of just "falling through" due to the asynchronous nature of # OpenGL: glDrawBuffer(GL_BACK) # We draw our single pixel with an alpha-value of zero - so effectively it doesn't # change the color buffer - just the z-buffer if z-writes are enabled... glColor4f(0.0, 0.0, 0.0, 0.0) glBegin(GL_POINTS) glVertex2i(10, 10) glEnd() # This glFinish() will wait until point drawing is finished, ergo backbuffer was ready # for drawing, ergo buffer swap in sync with start of VBL has happened. glFinish() finish = long(now() * 1000) fdiff = finish - self.last_finish self.last_finish = finish return (start, finish - start, fdiff)
def render_planar_update(self, rx, ry, rw, rh, x_scale=1, y_scale=1): log("%s.render_planar_update%s pixel_format=%s", self, (rx, ry, rw, rh, x_scale, y_scale), self.pixel_format) if self.pixel_format not in ("YUV420P", "YUV422P", "YUV444P", "GBRP"): #not ready to render yet return if self.pixel_format == "GBRP": # Set GL state for planar RGB: change fragment program glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, self.shaders[RGBP2RGB_SHADER]) self.gl_marker("painting planar update, format %s", self.pixel_format) divs = get_subsampling_divs(self.pixel_format) glEnable(GL_FRAGMENT_PROGRAM_ARB) for texture, index in ((GL_TEXTURE0, TEX_Y), (GL_TEXTURE1, TEX_U), (GL_TEXTURE2, TEX_V)): glActiveTexture(texture) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[index]) tw, th = self.texture_size log("%s.render_planar_update(..) texture_size=%s, size=%s", self, self.texture_size, self.size) glBegin(GL_QUADS) for x,y in ((0, 0), (0, rh), (rw, rh), (rw, 0)): ax = min(tw, x) ay = min(th, y) for texture, index in ((GL_TEXTURE0, TEX_Y), (GL_TEXTURE1, TEX_U), (GL_TEXTURE2, TEX_V)): (div_w, div_h) = divs[index] glMultiTexCoord2i(texture, ax//div_w, ay//div_h) glVertex2i(int(rx+ax*x_scale), int(ry+ay*y_scale)) glEnd() for texture in (GL_TEXTURE0, GL_TEXTURE1, GL_TEXTURE2): glActiveTexture(texture) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0) glDisable(GL_FRAGMENT_PROGRAM_ARB) if self.pixel_format == "GBRP": # Reset state to our default (YUV painting) glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, self.shaders[YUV2RGB_SHADER]) glActiveTexture(GL_TEXTURE0)
def drawShadowMaps(lights, layerLoc): """ draws shadow maps for debugging. note that a special shader is required to display the depth-component-only textures """ i = 0 for light in lights: if light.shadowMapArray==None: continue shadowMapArray = light.shadowMapArray shadowMaps = shadowMapArray.shadowMaps glBindTexture( GL_TEXTURE_2D_ARRAY, shadowMapArray.texture.glID ) glTexParameteri( GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_NONE ) for j in range(len(shadowMaps)): glViewport(130*i, 0, 128, 128) glUniform1f(layerLoc, float(j)) glBegin(GL_QUADS) glVertex3f(-1.0, -1.0, 0.0) glVertex3f( 1.0, -1.0, 0.0) glVertex3f( 1.0, 1.0, 0.0) glVertex3f(-1.0, 1.0, 0.0) glEnd() i += 1 if shadowMapArray.textureType=="sampler2DArrayShadow": glTexParameteri( GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE ) else: glTexParameteri( GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_NONE )
def render_yuv_update(self, rx, ry, rw, rh): debug("render_yuv_update %sx%s at %sx%s pixel_format=%s", rw, rh, rx, ry, self.pixel_format) if self.pixel_format not in (YUV420P, YUV422P, YUV444P): #not ready to render yet return self.gl_marker("Painting YUV update") divs = get_subsampling_divs(self.pixel_format) glEnable(GL_FRAGMENT_PROGRAM_ARB) glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, self.yuv_shader[0]) for texture, index in ((GL_TEXTURE0, 0), (GL_TEXTURE1, 1), (GL_TEXTURE2, 2)): glActiveTexture(texture) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[index]) tw, th = self.texture_size debug("render_yuv_update texture_size=%s, size=%s", self.texture_size, self.size) glBegin(GL_QUADS) for x, y in ((rx, ry), (rx, ry + rh), (rx + rw, ry + rh), (rx + rw, ry)): ax = min(tw, x) ay = min(th, y) for texture, index in ((GL_TEXTURE0, 0), (GL_TEXTURE1, 1), (GL_TEXTURE2, 2)): (div_w, div_h) = divs[index] glMultiTexCoord2i(texture, ax / div_w, ay / div_h) glVertex2i(ax, ay) glEnd()
def paint(self): glPushMatrix() position = self.player.getPosition() glTranslate(position.x, position.y, position.z) glRotate(self.player.orientation.y, 0.0, 1.0, 0.0) # Slide back because the pyramid below is centered at 0.5, 0, 0.5 # instead of at the origin. Without this it rotates around its corner # instead of around its center. glTranslate(-0.5, 0, -0.5) glColor(1.0, 1.0, 1.0) glBegin(GL_TRIANGLES) glVertex3f(0.5, 0.0, 0.5) glVertex3f(0.0, 1.0, 0.0) glVertex3f(1.0, 1.0, 0.0) glVertex3f(0.5, 0.0, 0.5) glVertex3f(1.0, 1.0, 0.0) glVertex3f(1.0, 1.0, 1.0) glVertex3f(0.5, 0.0, 0.5) glVertex3f(1.0, 1.0, 1.0) glVertex3f(0.0, 1.0, 1.0) glVertex3f(0.5, 0.0, 0.5) glVertex3f(0.0, 1.0, 1.0) glVertex3f(0.0, 1.0, 0.0) glEnd() glPopMatrix()
def render_planar_update(self, rx, ry, rw, rh, x_scale=1, y_scale=1): log("%s.render_planar_update%s pixel_format=%s", self, (rx, ry, rw, rh, x_scale, y_scale), self.pixel_format) if self.pixel_format not in ("YUV420P", "YUV422P", "YUV444P", "GBRP"): #not ready to render yet return if self.pixel_format == "GBRP": self.set_rgbP_paint_state() self.gl_marker("painting planar update, format %s", self.pixel_format) divs = get_subsampling_divs(self.pixel_format) glEnable(GL_FRAGMENT_PROGRAM_ARB) for texture, index in ((GL_TEXTURE0, 0), (GL_TEXTURE1, 1), (GL_TEXTURE2, 2)): glActiveTexture(texture) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[index]) tw, th = self.texture_size log("%s.render_planar_update(..) texture_size=%s, size=%s", self, self.texture_size, self.size) glBegin(GL_QUADS) for x,y in ((0, 0), (0, rh), (rw, rh), (rw, 0)): ax = min(tw, x) ay = min(th, y) for texture, index in ((GL_TEXTURE0, 0), (GL_TEXTURE1, 1), (GL_TEXTURE2, 2)): (div_w, div_h) = divs[index] glMultiTexCoord2i(texture, ax//div_w, ay//div_h) glVertex2i(int(rx+ax*x_scale), int(ry+ay*y_scale)) glEnd() if self.pixel_format == "GBRP": self.unset_rgbP_paint_state()
def drawArrowHead(color, basePoint, drawingScale, unitBaseVector, unitHeightVector): arrowBase = drawingScale * 0.08 arrowHeight = drawingScale * 0.12 glDisable(GL_LIGHTING) glPushMatrix() glTranslatef(basePoint[0],basePoint[1],basePoint[2]) point1 = V(0, 0, 0) point1 = point1 + unitHeightVector * arrowHeight point2 = unitBaseVector * arrowBase point3 = - unitBaseVector * arrowBase #Draw the arrowheads as filled triangles glColor3fv(color) glBegin(GL_POLYGON) glVertex3fv(point1) glVertex3fv(point2) glVertex3fv(point3) glEnd() glPopMatrix() glEnable(GL_LIGHTING)
def initialize(self): glutPositionWindow(0, 600) GLRealtimeProgram.initialize(self) colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0), (0, 1, 1), (1, 0, 1)] for (i, cloud) in enumerate(self.clouds): point_list = glGenLists(i + 1) self.point_lists.append(point_list) # compile the point cloud glNewList(point_list, GL_COMPILE) glDisable(GL_LIGHTING) glBegin(GL_POINTS) for point in cloud: if len(point) == 2: xyz = point[0] rgb = point[1] else: xyz = point[:3] if len(point) == 4: rgb = point[3] elif len(point) > 4: rgb = point[3:6] else: rgb = None if rgb is not None: glColor3f(*map(lambda x: x / 255.0, rgb)) else: glColor3f(*colors[i % len(colors)]) glVertex3f(*xyz[:3]) glEnd() glEndList() logging.debug("compiled {} points for cloud {}".format(len(cloud), i))
def draw(self, time, line_width=None): if self.hidden: return if time <= self.ts: return time = time * 1e-9 pos_start = self.pos path = self.speed * (time - self.ts * 1e-9) * self.dir # max_end = self.pos + (self.speed * self.te * self.dir) # if not int(self.te) == 0 and time > self.te: # pos_end = max_end # else: # pos_end = self.pos + path pos_end = self.pos + path glPushMatrix() if line_width: glLineWidth(line_width) else: glLineWidth(self.line_width) glColor3f(*self.color) glBegin(GL_LINES) glVertex3f(*pos_start) glVertex3f(*pos_end) glEnd() glPopMatrix()
def draw_me(self): # figure color glColor3ub(DisplayMaster.figure_color[0], DisplayMaster.figure_color[1], DisplayMaster.figure_color[2]) # drawing figure glBegin(GL_QUADS) for coords in self.list_of_vertex: glVertex2f( DisplayMaster.x_center_coordinate + coords[0] * DisplayMaster.scaling_coef, DisplayMaster.y_center_coordinate + coords[1] * DisplayMaster.scaling_coef) glEnd() # border color glColor3ub(0, 0, 0) # drawing border glBegin(GL_LINE_LOOP) for coords in self.list_of_vertex: glVertex2f( DisplayMaster.x_center_coordinate + coords[0] * DisplayMaster.scaling_coef, DisplayMaster.y_center_coordinate + coords[1] * DisplayMaster.scaling_coef) glEnd()
def _recursive_draw(grid_node: nav_map.NavMapGridNode): if grid_node.children is not None: for child in grid_node.children: _recursive_draw(child) else: # leaf node - render as a quad map_alpha = 0.5 cen = grid_node.center half_size = grid_node.size * 0.5 # Draw outline glColor4f(*color_light_gray, 1.0) # fully opaque glBegin(GL_LINE_STRIP) glVertex3f(cen.x + half_size, cen.y + half_size, cen.z) glVertex3f(cen.x + half_size, cen.y - half_size, cen.z) glVertex3f(cen.x - half_size, cen.y - half_size, cen.z) glVertex3f(cen.x - half_size, cen.y + half_size, cen.z) glVertex3f(cen.x + half_size, cen.y + half_size, cen.z) glEnd() # Draw filled contents glColor4f(*color_for_content(grid_node.content), map_alpha) glBegin(GL_TRIANGLE_STRIP) glVertex3f(cen.x + half_size, cen.y - half_size, fill_z) glVertex3f(cen.x + half_size, cen.y + half_size, fill_z) glVertex3f(cen.x - half_size, cen.y - half_size, fill_z) glVertex3f(cen.x - half_size, cen.y + half_size, fill_z) glEnd()
def _line(self, r1, r2): glColor(1, 1, 1) glLineWidth(1) glBegin(GL_LINES) glVertex3f(r1[0], r1[1], 0.0) glVertex3f(r2[0], r2[1], 0.0) glEnd()
def drawPoint(color, point, pointSize = 3.0, isRound = True): """ Draw a point using GL_POINTS. @param point: The x,y,z coordinate array/ vector of the point @type point: A or V @param pointSize: The point size to be used by glPointSize @type pointSize: float @param isRound: If True, the point will be drawn round otherwise square @type isRound: boolean """ glDisable(GL_LIGHTING) glColor3fv(color) glPointSize(float(pointSize)) if isRound: glEnable(GL_POINT_SMOOTH) glBegin(GL_POINTS) glVertex3fv(point) glEnd() if isRound: glDisable(GL_POINT_SMOOTH) glEnable(GL_LIGHTING) if pointSize != 1.0: glPointSize(1.0) return
def show_geometry(): glNewList(1, GL_COMPILE) glBegin(GL_TRIANGLES) glColor3f(1, 1, 1) vnum = dm.np_get_vertices(np_verts) tnum = dm.np_get_triangles_vertices(np_tris) dm.np_get_triangles_intensity(np_int) move_scale(np_verts[:vnum, :]) coords = np_verts[np_tris[:tnum, :], :] mi = coords[:, :, 0].min(axis=0) ma = coords[:, :, 0].max(axis=0) for f, vv in enumerate(coords): v1 = vv[2, :] - vv[0, :] v2 = vv[1, :] - vv[0, :] n = cross(v2, v1).squeeze() n /= norm(n) glNormal3f(*n) c = np_int[f] glColor3f(c, c, c) glVertex3f(*vv[0, :].squeeze()) glVertex3f(*vv[1, :].squeeze()) glVertex3f(*vv[2, :].squeeze()) glEnd() glEndList() return concatenate((mi, ma))
def draw_lines(self): "draw our line segments, using our current style attrs [which are partly nim]" # find variables which determine our GL state color = self.fix_color(self.linecolor) dashEnabled = self.dashed width = self.linewidth # set temporary GL state (code copied from drawer.drawline) glDisable(GL_LIGHTING) glColor3fv(color) if dashEnabled: glLineStipple(1, 0xAAAA) glEnable(GL_LINE_STIPPLE) if width != 1: glLineWidth(width) # draw the lines if self._closed_state: glBegin(GL_LINE_LOOP) else: glBegin(GL_LINE_STRIP) for pos in self.points: glVertex3fv( pos ) # [note from old code: could be pos + origin if that can matter] glEnd() # restore default GL state [some of this might be moved up to callers] if width != 1: glLineWidth(1.0) if dashEnabled: glDisable(GL_LINE_STIPPLE) glEnable(GL_LIGHTING) return
def swapBuffers(self): # first call the swap on the QGLWidget start = long(now()*1000) self.glw.swapBuffers() #self.glw.makeCurrent() # The following is taken from the PsychToolbox # Draw a single pixel in left-top area of back-buffer. # This will wait/stall the rendering pipeline # until the buffer flip has happened, aka immediately after the VBL has started. # We need the pixel as "synchronization token", so the following glFinish() really # waits for VBL instead of just "falling through" due to the asynchronous nature of # OpenGL: glDrawBuffer(GL_BACK) # We draw our single pixel with an alpha-value of zero - so effectively it doesn't # change the color buffer - just the z-buffer if z-writes are enabled... glColor4f(0.0,0.0,0.0,0.0) glBegin(GL_POINTS) glVertex2i(10,10) glEnd() # This glFinish() will wait until point drawing is finished, ergo backbuffer was ready # for drawing, ergo buffer swap in sync with start of VBL has happened. glFinish() finish = long(now()*1000) fdiff = finish - self.last_finish self.last_finish = finish return (start,finish-start,fdiff)
def drawline_worker(params): """ Draw a line. Receive parameters through a sequence so that this function and its parameters can be passed to another function for deferment. Right now this is only ColorSorter.schedule (see below) """ (endpt1, endpt2, dashEnabled, stipleFactor, width, isSmooth) = params ###glDisable(GL_LIGHTING) ###glColor3fv(color) if dashEnabled: glLineStipple(stipleFactor, 0xAAAA) glEnable(GL_LINE_STIPPLE) if width != 1: glLineWidth(width) if isSmooth: glEnable(GL_LINE_SMOOTH) glBegin(GL_LINES) glVertex(endpt1[0], endpt1[1], endpt1[2]) glVertex(endpt2[0], endpt2[1], endpt2[2]) glEnd() if isSmooth: glDisable(GL_LINE_SMOOTH) if width != 1: glLineWidth(1.0) # restore default state if dashEnabled: glDisable(GL_LINE_STIPPLE) ###glEnable(GL_LIGHTING) return
def paintGL(self): glLoadIdentity() gluPerspective(45, self.width / self.height, 0.1, 110.0) #set perspective? glTranslatef( 0, 0, self.zoomLevel) #I used -10 instead of -2 in the PyGame version. glRotatef(self.rotateDegreeV, 1, 0, 0) #I used 2 instead of 1 in the PyGame version. glRotatef(self.rotateDegreeH, 0, 0, 1) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) if len(self.shapes) != 0: glBegin(GL_LINES) for s in self.shapes: glColor3fv(s.color) if s.render and s.drawWires: for e in s.edges: for v in e: glVertex3fv(s.vertices[v]) glEnd() glBegin(GL_QUADS) for s in self.shapes: glColor3fv(s.color) if s.render and s.drawFaces: for f in s.facets: for v in f: glVertex3fv(s.vertices[v]) glEnd()
def get_gl_info_string(glpane): # grantham 20051129 """Return a string containing some useful information about the OpenGL implementation. Use the GL context from the given QGLWidget glpane (by calling glpane.makeCurrent()). """ glpane.makeCurrent() #bruce 070308 added glpane arg and makeCurrent call gl_info_string = '' gl_info_string += 'GL_VENDOR : "%s"\n' % glGetString(GL_VENDOR) gl_info_string += 'GL_VERSION : "%s"\n' % glGetString(GL_VERSION) gl_info_string += 'GL_RENDERER : "%s"\n' % glGetString(GL_RENDERER) gl_info_string += 'GL_EXTENSIONS : "%s"\n' % glGetString(GL_EXTENSIONS) from utilities.debug_prefs import debug_pref, Choice_boolean_False if debug_pref("get_gl_info_string call glAreTexturesResident?", Choice_boolean_False): # Give a practical indication of how much video memory is available. # Should also do this with VBOs. # I'm pretty sure this code is right, but PyOpenGL seg faults in # glAreTexturesResident, so it's disabled until I can figure that # out. [grantham] [bruce 070308 added the debug_pref] all_tex_in = True tex_bytes = '\0' * (512 * 512 * 4) tex_names = [] tex_count = 0 tex_names = glGenTextures(1024) glEnable(GL_TEXTURE_2D) while all_tex_in: glBindTexture(GL_TEXTURE_2D, tex_names[tex_count]) gluBuild2DMipmaps(GL_TEXTURE_2D, 4, 512, 512, GL_RGBA, GL_UNSIGNED_BYTE, tex_bytes) tex_count += 1 glTexCoord2f(0.0, 0.0) glBegin(GL_QUADS) glVertex2f(0.0, 0.0) glVertex2f(1.0, 0.0) glVertex2f(1.0, 1.0) glVertex2f(0.0, 1.0) glEnd() glFinish() residences = glAreTexturesResident(tex_names[:tex_count]) all_tex_in = reduce(lambda a,b: a and b, residences) # bruce 070308 sees this exception from this line: # TypeError: reduce() arg 2 must support iteration glDisable(GL_TEXTURE_2D) glDeleteTextures(tex_names) gl_info_string += "Could create %d 512x512 RGBA resident textures\n" \ % tex_count return gl_info_string
def make_plane(): glNewList(G_OBJ_PLANE, GL_COMPILE) glBegin(GL_LINES) glColor3f(0, 0, 0) for i in xrange(41): glVertex3f(-10.0 + 0.5 * i, 0, -10) glVertex3f(-10.0 + 0.5 * i, 0, 10) glVertex3f(-10.0, 0, -10 + 0.5 * i) glVertex3f(10.0, 0, -10 + 0.5 * i) # Axes glEnd() glLineWidth(5) glBegin(GL_LINES) glColor3f(0.5, 0.7, 0.5) glVertex3f(0.0, 0.0, 0.0) glVertex3f(5, 0.0, 0.0) glEnd() glBegin(GL_LINES) glColor3f(0.5, 0.7, 0.5) glVertex3f(0.0, 0.0, 0.0) glVertex3f(0.0, 5, 0.0) glEnd() glBegin(GL_LINES) glColor3f(0.5, 0.7, 0.5) glVertex3f(0.0, 0.0, 0.0) glVertex3f(0.0, 0.0, 5) glEnd() # Draw the Y. glBegin(GL_LINES) glColor3f(0.0, 0.0, 0.0) glVertex3f(0.0, 5.0, 0.0) glVertex3f(0.0, 5.5, 0.0) glVertex3f(0.0, 5.5, 0.0) glVertex3f(-0.5, 6.0, 0.0) glVertex3f(0.0, 5.5, 0.0) glVertex3f(0.5, 6.0, 0.0) # Draw the Z. glVertex3f(-0.5, 0.0, 5.0) glVertex3f(0.5, 0.0, 5.0) glVertex3f(0.5, 0.0, 5.0) glVertex3f(-0.5, 0.0, 6.0) glVertex3f(-0.5, 0.0, 6.0) glVertex3f(0.5, 0.0, 6.0) # Draw the X. glVertex3f(5.0, 0.0, 0.5) glVertex3f(6.0, 0.0, -0.5) glVertex3f(5.0, 0.0, -0.5) glVertex3f(6.0, 0.0, 0.5) glEnd() glLineWidth(1) glEndList()
def polygon(self, a, b, c, d): # draw a polygon glBegin(GL_POLYGON) glVertex3fv(self.vertices[a]) glVertex3fv(self.vertices[b]) glVertex3fv(self.vertices[c]) glVertex3fv(self.vertices[d]) glEnd()
def draw_line(from_x, from_y, to_x, to_y): """ Draws a line between given points. """ glBegin(GL_LINES) glVertex2f(from_x, from_y) glVertex2f(to_x, to_y) glEnd()
def _rectangle(self, r1, r2): glColor(1, 1, 1) glLineWidth(1) glBegin(GL_LINE_LOOP) glVertex3f(r1[0], r1[1], 0.0) glVertex3f(r2[0], r1[1], 0.0) glVertex3f(r2[0], r2[1], 0.0) glVertex3f(r1[0], r2[1], 0.0) glEnd()