def draw(self, context): if self._area != context.area: return Globals.drawing.glCheckError('UI_Document.draw: start') time_start = time.time() self.force_clean(context) Globals.drawing.glCheckError('UI_Document.draw: setting options') ScissorStack.start(context) bgl.glClearColor(0, 0, 0, 0) bgl.glBlendColor(0, 0, 0, 0) # bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_SCISSOR_TEST) bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glClear(bgl.GL_DEPTH_BUFFER_BIT) Globals.drawing.glCheckError('UI_Document.draw: drawing') self._body.draw() ScissorStack.end() self._draw_count += 1 self._draw_time += time.time() - time_start if self._draw_count % 100 == 0: fps = (self._draw_count / self._draw_time) if self._draw_time > 0 else float('inf') self._draw_fps = fps # print('~%f fps (%f / %d = %f)' % (self._draw_fps, self._draw_time, self._draw_count, self._draw_time / self._draw_count)) self._draw_count = 0 self._draw_time = 0 Globals.drawing.glCheckError('UI_Document.draw: done')
def drawLine(points, loop=False, color=(0.1, 0.4, 0.8, 0.8), thickness=2): bgl.glLineWidth(thickness) bgl.glBlendColor(color[0], color[1], color[2], color[3]) bgl.glBegin(bgl.GL_LINE_STRIP) for p in points: bgl.glVertex3f(p[0], p[1], p[2]) if loop: p = points[0] bgl.glVertex3f(p[0], p[1], p[2]) bgl.glEnd() return points
def drawTime(): font_size = 20 (r, g, b, alpha) = (1.0, 1.0, 1.0, 1.0) blf.size(0, font_size, 72) (pos_x, pos_y) = getTextLocation() blf.position(0, pos_x, pos_y, 0) bgl.glBlendColor(r, g, b, alpha) (hour, minute) = frameToTime(bpy.context.scene.frame_current) if hour >= 24: minute = 0 blf.draw(0, "%02i:%02i" % (min(hour, 24), minute)) return None
def draw_sunpath_callback(self, context): def drawLine(points, loop=False, color=(0.1, 0.4, 0.8, 0.8), thickness=2): bgl.glLineWidth(thickness) bgl.glBlendColor(color[0], color[1], color[2], color[3]) bgl.glBegin(bgl.GL_LINE_STRIP) for p in points: bgl.glVertex3f(p[0], p[1], p[2]) if loop: p = points[0] bgl.glVertex3f(p[0], p[1], p[2]) bgl.glEnd() return points def dayPoints(Month, Day): sc = bpy.context.scene N = sc.Site.northAxis Long = sc.Site.longitude Lat = sc.Site.latitude TimeZone = sc.Site.timezone dt = 4 # timesteps per hour coords = [(0.0, 0.0, 0.0)] * (24 * dt) i = 0 for Hour in range(24): for T in range(dt): Minute = T * (60 / dt) (Az, El) = Solar_Pos(Long, Lat, TimeZone, Month, Day, Hour, Minute) if sc.ODS_SUN.sunpath.flat: if sc.ODS_SUN.sunpath.equi: coords[i] = azElToPolar(radians(Az) + N, radians(El)) else: coords[i] = azElToXYZ(radians(Az) + N, radians(El)) coords[i][2] = 0.0 else: coords[i] = azElToXYZ(radians(Az) + N, radians(El)) # Finally shift the coordinate to the offset centre for j in range(3): coords[i][j] += sc.ODS_SUN.sunpath.pos[j] i += 1 return coords def drawCompassRose(color=(0.8, 0.8, 0.8, 1.0)): def tick(ang, f): X = sin(ang) Y = cos(ang) return [(R * X + O[0], R * Y + O[1], O[2]), (R * f * X + O[0], R * f * Y + O[1], O[2])] sc = bpy.context.scene R = sc.ODS_SUN.arcRadius N = sc.Site.northAxis O = sc.ODS_SUN.sunpath.pos T = [N + (i * 2 * pi / 360) for i in range(360)] # Draw inner concentric circles if sc.ODS_SUN.sunpath.circles: for i in range(1, 9): if sc.ODS_SUN.sunpath.flat and sc.ODS_SUN.sunpath.equi: r = (i * R / 9) else: r = R * sin((pi / 2) * (i / 9)) P = [(r * sin(t) + O[0], r * cos(t) + O[1], O[2]) for t in T] drawLine(P, loop=True, color=color, thickness=0.7) # Draw the thick outer circle (90 degrees) P = [(R * sin(t) + O[0], R * cos(t) + O[1], O[2]) for t in T] drawLine(P, loop=True, color=color) # Draw the ticks drawLine(tick(N, 1.2), color=color) for i in range(8): T = N + (i * 2 * pi / 8) drawLine(tick(T, 1.1), color=color) for i in range(36): T = N + (i * 2 * pi / 36) drawLine(tick(T, 1.05), color=color) return def calcSunPath(): datum = datetime.datetime(2010, 1, 1) for day in range(365): t = datum + datetime.timedelta(day) dp = dayPoints(t.month, t.day) points[day] = dp if t.day == 1: daylines[t.month - 1] = dp for i in range(0, 24): loops[i] = [points[day][i * 4] for day in range(365)] return None def getTextLocation(): context = bpy.context scene = bpy.context.scene X = 62 Y = 4 pos_x = int((context.region.width) - X) pos_y = int(Y) #(context.region.height) return (pos_x, pos_y) def drawTime(): font_size = 20 (r, g, b, alpha) = (1.0, 1.0, 1.0, 1.0) blf.size(0, font_size, 72) (pos_x, pos_y) = getTextLocation() blf.position(0, pos_x, pos_y, 0) bgl.glBlendColor(r, g, b, alpha) (hour, minute) = frameToTime(bpy.context.scene.frame_current) if hour >= 24: minute = 0 blf.draw(0, "%02i:%02i" % (min(hour, 24), minute)) return None def drawSunPath(): for line in daylines: drawLine(line) for line in loops: drawLine(line, loop=True, color=(1.0, 0.8, 0.0, 1.0)) drawCompassRose() return None # Draw the time if bpy.context.scene.ODS_SUN.sunpath.time: drawTime() if not bpy.context.scene.ODS_SUN.sunpath.path: return None # Get & convert the Perspective Matrix of the current view/region. #bgl.glClear(bgl.GL_COLOR_BUFFER_BIT) view3d = bpy.context region = view3d.region_data perspMatrix = region.perspective_matrix tempMat = [perspMatrix[j][i] for i in range(4) for j in range(4)] perspBuff = bgl.Buffer(bgl.GL_FLOAT, 16, tempMat) # --- # Store previous OpenGL settings. # Store MatrixMode MatrixMode_prev = bgl.Buffer(bgl.GL_INT, [1]) bgl.glGetIntegerv(2976, MatrixMode_prev) # bgl.GL_MATRIX_MODE MatrixMode_prev = MatrixMode_prev[0] # Store projection matrix ProjMatrix_prev = bgl.Buffer(bgl.GL_DOUBLE, [16]) bgl.glGetFloatv(2983, ProjMatrix_prev) # bgl.GL_PROJECTION_MATRIX # Store Line width lineWidth_prev = bgl.Buffer(bgl.GL_FLOAT, [1]) bgl.glGetFloatv(bgl.GL_LINE_WIDTH, lineWidth_prev) lineWidth_prev = lineWidth_prev[0] # Store GL_BLEND blend_prev = bgl.Buffer(bgl.GL_BYTE, [1]) bgl.glGetFloatv(bgl.GL_BLEND, blend_prev) blend_prev = blend_prev[0] line_stipple_prev = bgl.Buffer(bgl.GL_BYTE, [1]) bgl.glGetFloatv(2852, line_stipple_prev) # bgl.GL_LINE_STIPPLE line_stipple_prev = line_stipple_prev[0] # Store glColor4f color_prev = bgl.Buffer(bgl.GL_FLOAT, [4]) bgl.glGetFloatv(bgl.GL_COLOR, color_prev) # --- # Prepare for 3D drawing bgl.glLoadIdentity() bgl.glMatrixMode(5889) # bgl.GL_PROJECTION bgl.glLoadMatrixf(perspBuff) bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_LINE_SMOOTH) #bgl.glEnable(bgl.GL_LINE_STIPPLE) if not bpy.context.scene.ODS_SUN.sunpath.xray: bgl.glEnable(bgl.GL_DEPTH_TEST) # Draw the day sunpath lines if bpy.context.scene.ODS_SUN.sunpath.recalc: calcSunPath() drawSunPath() bpy.context.scene.ODS_SUN.sunpath.recalc = False else: drawSunPath() # --- # Restore previous OpenGL settings if not bpy.context.scene.ODS_SUN.sunpath.xray: bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glLoadIdentity() bgl.glMatrixMode(MatrixMode_prev) bgl.glLoadMatrixf(ProjMatrix_prev) bgl.glLineWidth(lineWidth_prev) if not blend_prev: bgl.glDisable(bgl.GL_BLEND) if not line_stipple_prev: bgl.glDisable(2852) # bgl.GL_LINE_STIPPLE bgl.glBlendColor(color_prev[0], color_prev[1], color_prev[2], color_prev[3]) return None
def _set(self, instance, value): glBlendColor(float(value[0]), float(value[1]), float(value[2]), float(value[3]))