def crosshairs(): """ Show crosshais in Manipulation Mode Use the OpenGL-Wrapper to draw the image """ imageHeight = windowHeight * 0.05 imageWidth = imageHeight x = windowWidth * 0.5 - imageWidth / 2 y = windowHeight * 0.5 - imageHeight / 2 gl_position = [[x, y], [x + imageWidth, y], [x + imageWidth, y + imageHeight], [x, y + imageHeight]] view_buf = bgl.Buffer(bgl.GL_INT, 4) bgl.glGetIntegerv(bgl.GL_VIEWPORT, view_buf) view = view_buf.to_list() if hasattr(view_buf, "to_list") else view_buf.list # Save the state bgl.glPushAttrib(bgl.GL_ALL_ATTRIB_BITS) # Disable depth test so we always draw over things bgl.glDisable(bgl.GL_DEPTH_TEST) # Disable lighting so everything is shadless bgl.glDisable(bgl.GL_LIGHTING) # Unbinding the texture prevents BGUI frames from somehow picking up on # color of the last used texture bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) # Make sure we're using smooth shading instead of flat bgl.glShadeModel(bgl.GL_SMOOTH) # Setup the matrices bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glPushMatrix() bgl.glLoadIdentity() bgl.gluOrtho2D(0, view[2], 0, view[3]) bgl.glMatrixMode(bgl.GL_MODELVIEW) bgl.glPushMatrix() bgl.glLoadIdentity() bgl.glEnable(bgl.GL_TEXTURE_2D) # Enable alpha blending bgl.glEnable(bgl.GL_BLEND) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) # Bind the texture bgl.glBindTexture(bgl.GL_TEXTURE_2D, crosshairs_id) # Draw the textured quad bgl.glColor4f(1, 1, 1, 1) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) bgl.glBegin(bgl.GL_QUADS) for i in range(4): bgl.glTexCoord2f(texco[i][0], texco[i][1]) bgl.glVertex2f(gl_position[i][0], gl_position[i][1]) bgl.glEnd() bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) bgl.glPopMatrix() bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glPopMatrix() bgl.glPopAttrib()
def draw_callback(self, op, context): # Force Stop if self.is_handler_list_empty(): self.unregister_handler() return bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_LINE_SMOOTH) bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) bgl.glColorMask(bgl.GL_FALSE, bgl.GL_FALSE, bgl.GL_FALSE, bgl.GL_FALSE) bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_FILL) self.fill_batch.draw(self.fill_shader) bgl.glColorMask(bgl.GL_TRUE, bgl.GL_TRUE, bgl.GL_TRUE, bgl.GL_TRUE) bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glDepthMask(bgl.GL_FALSE) bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_LINE) self.line_batch.draw(self.line_shader) bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_FILL) bgl.glDepthMask(bgl.GL_TRUE) bgl.glDisable(bgl.GL_DEPTH_TEST)
def restore_opengl(self): # Restore OpenGL to its default state. bgl.glColor4f(0, 0, 0, 1) bgl.glDepthRange(0, 1) bgl.glLineWidth(1) bgl.glPolygonOffset(0, 0) bgl.glDisable(bgl.GL_BLEND | bgl.GL_POLYGON_OFFSET_FILL)
def draw_callback(self, context): bgl.glPolygonOffset(0, -20) bgl.glEnable(bgl.GL_BLEND) if is_view_transparent(context.space_data): bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glPointSize(10) bgl.glColor4f(1.0, 0.0, 0.0, 0.5) bgl.glBegin(bgl.GL_POINTS) for point in self.points: bgl.glVertex3f(*point.co) bgl.glEnd() bgl.glPointSize(3) bgl.glColor4f(0.0, 1.0, 1.0, 0.5 if is_view_transparent(context.space_data) else 1.0) bgl.glBegin(bgl.GL_POINTS) for point in self.midpoints: bgl.glVertex3f(*point) bgl.glEnd() bgl.glPointSize(1) bgl.glDisable(bgl.GL_BLEND) bgl.glColor4f(0.0, 0.0, 0.0, 1.0) bgl.glPolygonOffset(0, 0)
def crosshairs(): """ Show crosshais in Manipulation Mode Use the OpenGL-Wrapper to draw the image """ imageHeight = windowHeight * 0.05 imageWidth = imageHeight x = windowWidth * 0.5 - imageWidth/2 y = windowHeight * 0.5 - imageHeight/2 gl_position = [[x, y],[x+imageWidth, y],[x+imageWidth, y+imageHeight],[x, y+imageHeight]] view_buf = bgl.Buffer(bgl.GL_INT, 4) bgl.glGetIntegerv(bgl.GL_VIEWPORT, view_buf) view = view_buf.to_list() if hasattr(view_buf, "to_list") else view_buf.list # Save the state bgl.glPushAttrib(bgl.GL_ALL_ATTRIB_BITS) # Disable depth test so we always draw over things bgl.glDisable(bgl.GL_DEPTH_TEST) # Disable lighting so everything is shadless bgl.glDisable(bgl.GL_LIGHTING) # Unbinding the texture prevents BGUI frames from somehow picking up on # color of the last used texture bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) # Make sure we're using smooth shading instead of flat bgl.glShadeModel(bgl.GL_SMOOTH) # Setup the matrices bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glPushMatrix() bgl.glLoadIdentity() bgl.gluOrtho2D(0, view[2], 0, view[3]) bgl.glMatrixMode(bgl.GL_MODELVIEW) bgl.glPushMatrix() bgl.glLoadIdentity() bgl.glEnable(bgl.GL_TEXTURE_2D) # Enable alpha blending bgl.glEnable(bgl.GL_BLEND) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) # Bind the texture bgl.glBindTexture(bgl.GL_TEXTURE_2D, crosshairs_id) # Draw the textured quad bgl.glColor4f(1, 1, 1, 1) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) bgl.glBegin(bgl.GL_QUADS) for i in range(4): bgl.glTexCoord2f(texco[i][0], texco[i][1]) bgl.glVertex2f(gl_position[i][0], gl_position[i][1]) bgl.glEnd() bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) bgl.glPopMatrix() bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glPopMatrix() bgl.glPopAttrib()
def draw_callback(self, op, context): if not self.is_visible(): return # Force Stop wireframe_image = find_bpy_image_by_name(Config.coloring_texture_name) if self.is_handler_list_empty() or \ not self._check_coloring_image(wireframe_image): self.unregister_handler() return bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_LINE_SMOOTH) bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) bgl.glColorMask(bgl.GL_FALSE, bgl.GL_FALSE, bgl.GL_FALSE, bgl.GL_FALSE) bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_FILL) self.fill_batch.draw(self.fill_shader) bgl.glColorMask(bgl.GL_TRUE, bgl.GL_TRUE, bgl.GL_TRUE, bgl.GL_TRUE) bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glDepthMask(bgl.GL_FALSE) bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_LINE) bgl.glEnable(bgl.GL_DEPTH_TEST) if not self._use_simple_shader: # coloring_image.bindcode should not be zero # if we don't want to destroy video driver in Blender if not wireframe_image or wireframe_image.bindcode == 0: self.switch_to_simple_shader() else: bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, wireframe_image.bindcode) self.line_shader.bind() self.line_shader.uniform_int('image', 0) self.line_shader.uniform_float('opacity', self._opacity) self.line_batch.draw(self.line_shader) if self._use_simple_shader: self.simple_line_shader.bind() self.simple_line_shader.uniform_float( 'color', ((*self._colors[0][:3], self._opacity))) self.simple_line_batch.draw(self.simple_line_shader) bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_FILL) bgl.glDepthMask(bgl.GL_TRUE) bgl.glDisable(bgl.GL_DEPTH_TEST)
def draw_batch_face_triangles(batch, color=(1, 0, 0, 0.5)): bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(bgl.GL_FALSE) bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glEnable(bgl.GL_POLYGON_OFFSET_LINE) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) shader3D.bind() shader3D.uniform_float("color", color) batch.draw(shader3D) bgl.glDisable(bgl.GL_BLEND)
def color_placing(): """ Draw the green rectangle via OpenGL-Wrapper """ imageHeight = windowHeight * 0.05 imageWidth = imageHeight x = windowWidth * 0.5 - imageWidth / 2 y = windowHeight * 0.5 - imageHeight / 2 gl_position = [[x, y], [x + imageWidth, y], [x + imageWidth, y + imageHeight], [x, y + imageHeight]] view_buf = bgl.Buffer(bgl.GL_INT, 4) bgl.glGetIntegerv(bgl.GL_VIEWPORT, view_buf) view = view_buf.to_list() if hasattr(view_buf, "to_list") else view_buf.list # Save the state bgl.glPushAttrib(bgl.GL_ALL_ATTRIB_BITS) # Disable depth test so we always draw over things bgl.glDisable(bgl.GL_DEPTH_TEST) # Disable lighting so everything is shadless bgl.glDisable(bgl.GL_LIGHTING) # Make sure we're using smooth shading instead of flat bgl.glShadeModel(bgl.GL_SMOOTH) # Setup the matrices bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glPushMatrix() bgl.glLoadIdentity() bgl.gluOrtho2D(0, view[2], 0, view[3]) bgl.glMatrixMode(bgl.GL_MODELVIEW) bgl.glPushMatrix() bgl.glLoadIdentity() # Enable alpha blending bgl.glEnable(bgl.GL_BLEND) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) # Draw the colored quad bgl.glColor4f(0, 1, 0, 0.25) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) bgl.glBegin(bgl.GL_QUADS) for i in range(4): bgl.glTexCoord2f(texco[i][0], texco[i][1]) bgl.glVertex2f(gl_position[i][0], gl_position[i][1]) bgl.glEnd() bgl.glPopMatrix() bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glPopMatrix() bgl.glPopAttrib()
def color_placing(): """ Draw the green rectangle via OpenGL-Wrapper """ imageHeight = windowHeight * 0.05 imageWidth = imageHeight x = windowWidth * 0.5 - imageWidth/2 y = windowHeight * 0.5 - imageHeight/2 gl_position = [[x, y],[x+imageWidth, y],[x+imageWidth, y+imageHeight],[x, y+imageHeight]] view_buf = bgl.Buffer(bgl.GL_INT, 4) bgl.glGetIntegerv(bgl.GL_VIEWPORT, view_buf) view = view_buf.to_list() if hasattr(view_buf, "to_list") else view_buf.list # Save the state bgl.glPushAttrib(bgl.GL_ALL_ATTRIB_BITS) # Disable depth test so we always draw over things bgl.glDisable(bgl.GL_DEPTH_TEST) # Disable lighting so everything is shadless bgl.glDisable(bgl.GL_LIGHTING) # Make sure we're using smooth shading instead of flat bgl.glShadeModel(bgl.GL_SMOOTH) # Setup the matrices bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glPushMatrix() bgl.glLoadIdentity() bgl.gluOrtho2D(0, view[2], 0, view[3]) bgl.glMatrixMode(bgl.GL_MODELVIEW) bgl.glPushMatrix() bgl.glLoadIdentity() # Enable alpha blending bgl.glEnable(bgl.GL_BLEND) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) # Draw the colored quad bgl.glColor4f(0, 1, 0, 0.25) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) bgl.glBegin(bgl.GL_QUADS) for i in range(4): bgl.glTexCoord2f(texco[i][0], texco[i][1]) bgl.glVertex2f(gl_position[i][0], gl_position[i][1]) bgl.glEnd() bgl.glPopMatrix() bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glPopMatrix() bgl.glPopAttrib()
def draw_pivots3D(poss, radius, color=(1, 1, 1, 1)): bgl.glDisable(bgl.GL_LINE_SMOOTH) bgl.glEnable(bgl.GL_BLEND) bgl.glPointSize(radius * dpm() * 2) bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(bgl.GL_FALSE) bgl.glEnable(bgl.GL_POLYGON_OFFSET_POINT) bgl.glPolygonOffset(1.0, 1.0) shader3D.bind() shader3D.uniform_float("color", color) batch_draw(shader3D, 'POINTS', {"pos": poss}) bgl.glPointSize(1) bgl.glDisable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_POLYGON_OFFSET_POINT)
def draw_faces(context, args): geom, config = args if config.draw_gl_polygonoffset: bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) if config.display_edges: draw_lines_uniform(context, config, geom.verts, geom.edges, config.line4f, config.line_width) if config.display_faces: if config.draw_gl_wireframe: bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_LINE) if config.draw_gl_polygonoffset: bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) if config.shade == "flat": draw_uniform('TRIS', geom.verts, geom.faces, config.face4f) elif config.shade == "facet": draw_smooth(geom.facet_verts, geom.facet_verts_vcols) elif config.shade == "smooth": draw_smooth(geom.verts, geom.smooth_vcols, indices=geom.faces) elif config.shade == 'fragment': if config.draw_fragment_function: config.draw_fragment_function(context, args) else: draw_fragment(context, args) if config.draw_gl_wireframe: bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_FILL) if config.display_verts: draw_uniform('POINTS', geom.verts, None, config.vcol, config.point_size) if config.draw_gl_polygonoffset: # or restore to the state found when entering this function. TODO! bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL)
def draw_Poly3D(context, verts, color=(1, 1, 1, 1), hide_alpha=0.5): bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthMask(bgl.GL_FALSE) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) polys = mathutils.geometry.tessellate_polygon((verts, )) shader3D.bind() shader3D.uniform_float("color", color) batch = batch_draw(shader3D, 'TRIS', {"pos": verts}, indices=polys) if hide_alpha > 0.0: bgl.glDepthFunc(bgl.GL_GREATER) shader3D.uniform_float( "color", (color[0], color[1], color[2], color[3] * hide_alpha)) batch.draw(shader3D) bgl.glDisable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL)
def draw_faces_uniform(context, args): geom, config = args # print(geom.f_faces, config.shade) if config.draw_gl_wireframe: bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_LINE) if config.draw_gl_polygonoffset: bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) if config.shade == "flat": draw_uniform('TRIS', geom.f_verts, geom.f_faces, config.face4f) elif config.shade == "facet": draw_smooth(geom.facet_verts, geom.facet_verts_vcols) elif config.shade == "smooth": draw_smooth(geom.f_verts, geom.smooth_vcols, indices=geom.f_faces) elif config.shade == 'normals': draw_smooth(geom.f_verts, geom.smooth_vnorms, indices=geom.f_faces) if config.draw_gl_wireframe: bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_FILL)
def draw_lines3D(context, verts, color=(1, 1, 1, 1), width: float = 1.0, hide_alpha: float = 1.0, primitiveType='LINE_STRIP'): bgl.glEnable(bgl.GL_LINE_SMOOTH) bgl.glLineWidth(width) bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(bgl.GL_FALSE) bgl.glEnable(bgl.GL_POLYGON_OFFSET_LINE) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) if hide_alpha < 0.99: bgl.glDepthFunc(bgl.GL_LESS) else: bgl.glDepthFunc(bgl.GL_ALWAYS) # shader3D.uniform_float("modelMatrix", Matrix.Identity(4) ) shader3D.bind() matrix = context.region_data.perspective_matrix # shader3D.uniform_float("viewProjectionMatrix", matrix) shader3D.uniform_float("color", color) batch = batch_draw(shader3D, primitiveType, {"pos": verts}) if hide_alpha < 0.99: bgl.glDepthFunc(bgl.GL_GREATER) shader3D.uniform_float( "color", (color[0], color[1], color[2], color[3] * hide_alpha)) batch.draw(shader3D) bgl.glLineWidth(1) bgl.glDisable(bgl.GL_LINE_SMOOTH) bgl.glDisable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_POLYGON_OFFSET_LINE) bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL)
def draw_callback(self, context): bgl.glPolygonOffset(0, -20) bgl.glEnable(bgl.GL_BLEND) bgl.glPointSize(10) bgl.glColor4f(1.0, 0.0, 0.0, 0.5) bgl.glBegin(bgl.GL_POINTS) for point in self.points: bgl.glVertex3f(*point.co) bgl.glEnd() bgl.glPointSize(3) bgl.glColor4f(0.0, 1.0, 1.0, 1.0) bgl.glBegin(bgl.GL_POINTS) for point in self.midpoints: bgl.glVertex3f(*point) bgl.glEnd() bgl.glPointSize(1) bgl.glDisable(bgl.GL_BLEND) bgl.glColor4f(0.0, 0.0, 0.0, 1.0) bgl.glPolygonOffset(0, 0)
def _set(self, instance, value): glPolygonOffset(float(value[0]), float(value[1]))
def view_3d_geom(context, args): """ draws the batches """ geom, config = args bgl.glEnable(bgl.GL_BLEND) if config.draw_polys: if config.draw_gl_wireframe: bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_LINE) if config.draw_gl_polygonoffset: bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) if config.shade_mode == 'fragment': p_batch = batch_for_shader(config.p_shader, 'TRIS', {"position": geom.p_vertices}, indices=geom.p_indices) config.p_shader.bind() matrix = context.region_data.perspective_matrix config.p_shader.uniform_float("viewProjectionMatrix", matrix) config.p_shader.uniform_float("brightness", 0.5) else: p_batch = batch_for_shader(config.p_shader, 'TRIS', { "pos": geom.p_vertices, "color": geom.p_vertex_colors }, indices=geom.p_indices) config.p_shader.bind() p_batch.draw(config.p_shader) if config.draw_gl_polygonoffset: bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) if config.draw_gl_wireframe: bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_FILL) if config.draw_edges: bgl.glLineWidth(config.line_width) if config.draw_dashed: shader = config.dashed_shader batch = batch_for_shader(shader, 'LINES', {"inPos": geom.e_vertices}, indices=geom.e_indices) shader.bind() matrix = context.region_data.perspective_matrix shader.uniform_float("u_mvp", matrix) shader.uniform_float("u_resolution", config.u_resolution) shader.uniform_float("u_dashSize", config.u_dash_size) shader.uniform_float("u_gapSize", config.u_gap_size) shader.uniform_float("m_color", geom.e_vertex_colors[0]) batch.draw(shader) else: e_batch = batch_for_shader(config.e_shader, 'LINES', { "pos": geom.e_vertices, "color": geom.e_vertex_colors }, indices=geom.e_indices) config.e_shader.bind() e_batch.draw(config.e_shader) bgl.glLineWidth(1) if config.draw_verts: bgl.glPointSize(config.point_size) v_batch = batch_for_shader(config.v_shader, 'POINTS', { "pos": geom.v_vertices, "color": geom.points_color }) config.v_shader.bind() v_batch.draw(config.v_shader) bgl.glPointSize(1) bgl.glEnable(bgl.GL_BLEND)
def _set(self, instance, value): bgl.glPolygonOffset(value[0], value[1])
def status_image(): """ Show the corrensponding Image for the status """ imageHeight = windowHeight * 0.075 imageWidth = imageHeight x = windowWidth * 0.35 - imageWidth/2 y = windowHeight * 0.45 - imageHeight/2 gl_position = [[x, y],[x+imageWidth, y],[x+imageWidth, y+imageHeight],[x, y+imageHeight]] cam = logic.getCurrentScene().active_camera # get the suffix of the human to reference the right objects suffix = cam.name[-4:] if cam.name[-4] == "." else "" hand = objects['Hand_Grab.R' + suffix] # select the right Image if hand["selected"]: tex_id = closed_id else: tex_id = open_id view_buf = bgl.Buffer(bgl.GL_INT, 4) bgl.glGetIntegerv(bgl.GL_VIEWPORT, view_buf) view = view_buf.to_list() if hasattr(view_buf, "to_list") else view_buf.list # Save the state bgl.glPushAttrib(bgl.GL_ALL_ATTRIB_BITS) # Disable depth test so we always draw over things bgl.glDisable(bgl.GL_DEPTH_TEST) # Disable lighting so everything is shadless bgl.glDisable(bgl.GL_LIGHTING) # Unbinding the texture prevents BGUI frames from somehow picking up on # color of the last used texture bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) # Make sure we're using smooth shading instead of flat bgl.glShadeModel(bgl.GL_SMOOTH) # Setup the matrices bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glPushMatrix() bgl.glLoadIdentity() bgl.gluOrtho2D(0, view[2], 0, view[3]) bgl.glMatrixMode(bgl.GL_MODELVIEW) bgl.glPushMatrix() bgl.glLoadIdentity() bgl.glEnable(bgl.GL_TEXTURE_2D) # Enable alpha blending bgl.glEnable(bgl.GL_BLEND) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) # Bind the texture bgl.glBindTexture(bgl.GL_TEXTURE_2D, tex_id) # Draw the textured quad bgl.glColor4f(1, 1, 1, 1) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) bgl.glBegin(bgl.GL_QUADS) for i in range(4): bgl.glTexCoord2f(texco[i][0], texco[i][1]) bgl.glVertex2f(gl_position[i][0], gl_position[i][1]) bgl.glEnd() bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) bgl.glPopMatrix() bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glPopMatrix() bgl.glPopAttrib()
def _set(self, instance, value): glPolygonOffset(value[0], value[1])
def draw_stuff(): # print("draw_stuff") # FIXME this should use glPushAttrib from bgl import glColor3f, glVertex3f, glBegin, glEnd, GL_POLYGON, GL_LINES global draw_stuff_dirty, draw_stuff_objects ctx = bpy.context if not len(ctx.selected_objects) and not ctx.object: return if not bpy.context.window_manager.thug_show_face_collision_colors: return VERT_FLAG = FACE_FLAGS["mFD_VERT"] WALLRIDABLE_FLAG = FACE_FLAGS["mFD_WALL_RIDABLE"] TRIGGER_FLAG = FACE_FLAGS["mFD_TRIGGER"] NON_COLLIDABLE_FLAG = FACE_FLAGS["mFD_NON_COLLIDABLE"] bgl.glPushAttrib(bgl.GL_ALL_ATTRIB_BITS) try: _tmp_buf = bgl.Buffer(bgl.GL_FLOAT, 1) bgl.glGetFloatv(bgl.GL_POLYGON_OFFSET_FACTOR, _tmp_buf) old_offset_factor = _tmp_buf[0] bgl.glGetFloatv(bgl.GL_POLYGON_OFFSET_UNITS, _tmp_buf) old_offset_units = _tmp_buf[0] del _tmp_buf objects = set([ob.name for ob in ctx.selected_objects] if ctx.mode == "OBJECT" else [ctx.object.name]) if draw_stuff_objects != objects: draw_stuff_dirty = True # print("draw_stuff2") if not draw_stuff_dirty: bgl.glCallList(draw_stuff_display_list_id) bgl.glPolygonOffset(old_offset_factor, old_offset_units) bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glDisable(bgl.GL_CULL_FACE) return bm = None bgl.glNewList(draw_stuff_display_list_id, bgl.GL_COMPILE_AND_EXECUTE) try: bgl.glCullFace(bgl.GL_BACK) bgl.glEnable(bgl.GL_CULL_FACE) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) #bgl.glEnable(bgl.GL_POLYGON_OFFSET_LINE) bgl.glPolygonOffset(-2, -2) bgl.glEnable(bgl.GL_BLEND) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA); prefs = ctx.user_preferences.addons[ADDON_NAME].preferences bgl.glLineWidth(prefs.line_width) draw_stuff_objects = objects bdobs = {ob.name: ob for ob in bpy.data.objects} for ob in objects: ob = bdobs[ob] if not bm: bm = bmesh.new() if (ob and ob.type == "CURVE" and ob.data.splines and ob.data.splines[-1].points and ob.thug_path_type == "Rail" and ob.thug_rail_connects_to): connects_to = bdobs[ob.thug_rail_connects_to] if (connects_to and connects_to.type == "CURVE" and connects_to.data.splines and connects_to.data.splines[0].points): glBegin(GL_LINES) bgl.glColor4f(*prefs.rail_end_connection_color) v = ob.matrix_world * ob.data.splines[-1].points[-1].co.to_3d() glVertex3f(v[0], v[1], v[2]) v = connects_to.matrix_world * connects_to.data.splines[0].points[0].co.to_3d() glVertex3f(v[0], v[1], v[2]) glEnd() # Draw previews for area lights - tube/sphere lights and area lights if (ob and ob.type == 'LAMP'): if ob.data.thug_light_props.light_type == 'TUBE': if ob.data.thug_light_props.light_end_pos != (0, 0, 0): glBegin(GL_LINES) bgl.glColor4f(1.0, 0.75, 0.25, 1.0) glVertex3f(ob.location[0], ob.location[1], ob.location[2]) glVertex3f(ob.location[0] + ob.data.thug_light_props.light_end_pos[0], ob.location[1] + ob.data.thug_light_props.light_end_pos[1], ob.location[2] + ob.data.thug_light_props.light_end_pos[2]) glEnd() continue elif ob.data.thug_light_props.light_type == 'SPHERE': continue elif ob.data.thug_light_props.light_type == 'AREA': continue else: continue elif (ob and ob.type == 'EMPTY'): if ob.thug_empty_props.empty_type == 'LightVolume' or ob.thug_empty_props.empty_type == 'CubemapProbe': # Draw light volume bbox! bbox, bbox_min, bbox_max, bbox_mid = get_bbox_from_node(ob) # 50% alpha, 2 pixel width line bgl.glEnable(bgl.GL_BLEND) bgl.glColor4f(1.0, 0.0, 0.0, 0.5) bgl.glLineWidth(4) glBegin(bgl.GL_LINE_STRIP) bgl.glVertex3f(*bbox[0]) bgl.glVertex3f(*bbox[1]) bgl.glVertex3f(*bbox[2]) bgl.glVertex3f(*bbox[3]) bgl.glVertex3f(*bbox[0]) bgl.glVertex3f(*bbox[4]) bgl.glVertex3f(*bbox[5]) bgl.glVertex3f(*bbox[6]) bgl.glVertex3f(*bbox[7]) bgl.glVertex3f(*bbox[4]) bgl.glEnd() bgl.glBegin(bgl.GL_LINES) bgl.glVertex3f(*bbox[1]) bgl.glVertex3f(*bbox[5]) bgl.glVertex3f(*bbox[2]) bgl.glVertex3f(*bbox[6]) bgl.glVertex3f(*bbox[3]) bgl.glVertex3f(*bbox[7]) glEnd() if not ob or ob.type != "MESH": continue if ob.mode == "EDIT": bm.free() bm = bmesh.from_edit_mesh(ob.data).copy() else: bm.clear() if ob.modifiers: final_mesh = ob.to_mesh(bpy.context.scene, True, "PREVIEW") try: bm.from_mesh(final_mesh) finally: bpy.data.meshes.remove(final_mesh) else: bm.from_mesh(ob.data) arl = bm.edges.layers.int.get("thug_autorail") if arl: bgl.glColor4f(*prefs.autorail_edge_color) glBegin(GL_LINES) for edge in bm.edges: if edge[arl] == AUTORAIL_NONE: continue for vert in edge.verts: v = ob.matrix_world * vert.co glVertex3f(v[0], v[1], v[2]) glEnd() cfl = bm.faces.layers.int.get("collision_flags") flag_stuff = ((VERT_FLAG, prefs.vert_face_color), (WALLRIDABLE_FLAG, prefs.wallridable_face_color), (TRIGGER_FLAG, prefs.trigger_face_color), (NON_COLLIDABLE_FLAG, prefs.non_collidable_face_color)) if cfl: bmesh.ops.triangulate(bm, faces=bm.faces) for face in bm.faces: drawn_face = False if prefs.show_bad_face_colors: if (face[cfl] & (VERT_FLAG | WALLRIDABLE_FLAG | NON_COLLIDABLE_FLAG) not in (VERT_FLAG, WALLRIDABLE_FLAG, NON_COLLIDABLE_FLAG, 0)): bgl.glColor4f(*prefs.bad_face_color) glBegin(GL_POLYGON) for vert in face.verts: v = ob.matrix_world * vert.co glVertex3f(v[0], v[1], v[2]) glEnd() continue for face_flag, face_color in flag_stuff: if face[cfl] & face_flag and (not drawn_face or prefs.mix_face_colors): bgl.glColor4f(*face_color) drawn_face = True glBegin(GL_POLYGON) for vert in face.verts: v = ob.matrix_world * vert.co glVertex3f(v[0], v[1], v[2]) glEnd() finally: draw_stuff_dirty = False bgl.glEndList() if bm: bm.free() bgl.glPolygonOffset(old_offset_factor, old_offset_units) bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) finally: bgl.glPopAttrib()
def status_image(): """ Show the corrensponding Image for the status """ imageHeight = windowHeight * 0.075 imageWidth = imageHeight x = windowWidth * 0.35 - imageWidth / 2 y = windowHeight * 0.45 - imageHeight / 2 gl_position = [[x, y], [x + imageWidth, y], [x + imageWidth, y + imageHeight], [x, y + imageHeight]] cam = blenderapi.scene().active_camera # get the suffix of the human to reference the right objects suffix = cam.name[-4:] if cam.name[-4] == "." else "" hand = objects['Hand_Grab.R' + suffix] # select the right Image if hand["selected"]: tex_id = closed_id else: tex_id = open_id view_buf = bgl.Buffer(bgl.GL_INT, 4) bgl.glGetIntegerv(bgl.GL_VIEWPORT, view_buf) view = view_buf.to_list() if hasattr(view_buf, "to_list") else view_buf.list # Save the state bgl.glPushAttrib(bgl.GL_ALL_ATTRIB_BITS) # Disable depth test so we always draw over things bgl.glDisable(bgl.GL_DEPTH_TEST) # Disable lighting so everything is shadless bgl.glDisable(bgl.GL_LIGHTING) # Unbinding the texture prevents BGUI frames from somehow picking up on # color of the last used texture bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) # Make sure we're using smooth shading instead of flat bgl.glShadeModel(bgl.GL_SMOOTH) # Setup the matrices bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glPushMatrix() bgl.glLoadIdentity() bgl.gluOrtho2D(0, view[2], 0, view[3]) bgl.glMatrixMode(bgl.GL_MODELVIEW) bgl.glPushMatrix() bgl.glLoadIdentity() bgl.glEnable(bgl.GL_TEXTURE_2D) # Enable alpha blending bgl.glEnable(bgl.GL_BLEND) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) # Bind the texture bgl.glBindTexture(bgl.GL_TEXTURE_2D, tex_id) # Draw the textured quad bgl.glColor4f(1, 1, 1, 1) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) bgl.glBegin(bgl.GL_QUADS) for i in range(4): bgl.glTexCoord2f(texco[i][0], texco[i][1]) bgl.glVertex2f(gl_position[i][0], gl_position[i][1]) bgl.glEnd() bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) bgl.glPopMatrix() bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glPopMatrix() bgl.glPopAttrib()
def draw_pre_view_callback(self): bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(2, 0)
def draw_callback_view3D(): if not main.isEditingUVs(): return settings = bpy.context.scene.uv_highlight if not settings.show_in_viewport: return prefs = bpy.context.user_preferences.addons[__package__].preferences t1 = time.perf_counter() # if not update(): # return t2 = time.perf_counter() obj = bpy.context.active_object mode = bpy.context.scene.tool_settings.uv_select_mode matrix = obj.matrix_world # draw selected # bgl.glColor4f(*prefs.view3d_selection_color_verts_edges) #bgl.glMatrixMode(bgl.GL_MODELVIEW) #bgl.glPushMatrix() #bgl.glLoadIdentity() #print((len(matrix))) #m = bgl.Buffer(bgl.GL_FLOAT, 16, [entry for collumn in matrix for entry in collumn] ) #bgl.glLoadMatrixf(m) if mode == "VERTEX": bgl.glPointSize(6.0) draw_vertex_array("selected_verts", bgl.GL_POINTS, 3, prefs.view3d_selection_color_verts_edges) elif mode == "EDGE": bgl.glLineWidth(3.0) bgl.glEnable(bgl.GL_POLYGON_OFFSET_LINE) bgl.glPolygonOffset(settings.offset_factor, settings.offset_units) draw_vertex_array("selected_edges", bgl.GL_LINES, 3, prefs.view3d_selection_color_verts_edges) bgl.glDisable(bgl.GL_POLYGON_OFFSET_LINE) else: bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glEnable(bgl.GL_BLEND) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA); bgl.glPolygonOffset(settings.offset_factor, settings.offset_units) draw_vertex_array("selected_faces", bgl.GL_TRIANGLES, 3, prefs.view3d_selection_color_faces) bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPopMatrix() # PRE HIGHLIGHT if settings.show_preselection and main.UV_MOUSE: bgl.glEnable(bgl.GL_BLEND) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glColor4f(*prefs.view3d_preselection_color_verts_edges) if mode == 'VERTEX' and main.closest_vert and main.closest_vert[0]: bgl.glPointSize(7.0) bgl.glEnable(bgl.GL_POLYGON_OFFSET_POINT) bgl.glPolygonOffset(settings.offset_factor, settings.offset_units) bgl.glPolygonOffset(settings.offset_factor, settings.offset_units) bgl.glBegin(bgl.GL_POINTS) co, normal = main.closest_vert[0] bgl.glVertex3f(*(matrix * co)) bgl.glEnd() bgl.glDisable(bgl.GL_POLYGON_OFFSET_POINT) elif mode == 'EDGE' and main.closest_edge and main.closest_edge[0]: bgl.glLineWidth(7.0) bgl.glEnable(bgl.GL_POLYGON_OFFSET_LINE) bgl.glPolygonOffset(settings.offset_factor, settings.offset_units) bgl.glBegin(bgl.GL_LINE_STRIP) for co, normal in main.closest_edge[0]: bgl.glVertex3f(*(matrix * co)) bgl.glEnd() bgl.glDisable(bgl.GL_POLYGON_OFFSET_LINE) # draw FACE and ISLAND elif main.closest_face and main.closest_face[0]: bgl.glEnable(bgl.GL_CULL_FACE) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(settings.offset_factor, settings.offset_units) bgl.glEnable(bgl.GL_BLEND) bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE) draw_vertex_array("closest_faces", bgl.GL_TRIANGLES, 3, prefs.view3d_preselection_color_faces) bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) restore_opengl_defaults() t3 = time.perf_counter()