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()
Esempio n. 2
0
    def draw_texture(cls, _, context):
        sc = context.scene

        if not cls.is_running(context):
            return

        # no textures are selected
        if sc.muv_texture_projection_tex_image == "None":
            return

        # get texture to be renderred
        img = bpy.data.images[sc.muv_texture_projection_tex_image]

        # setup rendering region
        rect = _get_canvas(context, sc.muv_texture_projection_tex_magnitude)
        positions = [
            [rect.x0, rect.y0],
            [rect.x0, rect.y1],
            [rect.x1, rect.y1],
            [rect.x1, rect.y0]
        ]
        tex_coords = [
            [0.0, 0.0],
            [0.0, 1.0],
            [1.0, 1.0],
            [1.0, 0.0]
        ]

        # OpenGL configuration
        if compat.check_version(2, 80, 0) >= 0:
            bgl.glEnable(bgl.GL_BLEND)
            bgl.glEnable(bgl.GL_TEXTURE_2D)
            bgl.glActiveTexture(bgl.GL_TEXTURE0)
            if img.bindcode:
                bind = img.bindcode
                bgl.glBindTexture(bgl.GL_TEXTURE_2D, bind)
        else:
            bgl.glEnable(bgl.GL_BLEND)
            bgl.glEnable(bgl.GL_TEXTURE_2D)
            if img.bindcode:
                bind = img.bindcode[0]
                bgl.glBindTexture(bgl.GL_TEXTURE_2D, bind)
                bgl.glTexParameteri(bgl.GL_TEXTURE_2D,
                                    bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR)
                bgl.glTexParameteri(bgl.GL_TEXTURE_2D,
                                    bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR)
                bgl.glTexEnvi(
                    bgl.GL_TEXTURE_ENV, bgl.GL_TEXTURE_ENV_MODE,
                    bgl.GL_MODULATE)

        # render texture
        bgl.glBegin(bgl.GL_QUADS)
        bgl.glColor4f(1.0, 1.0, 1.0,
                      sc.muv_texture_projection_tex_transparency)
        for (v1, v2), (u, v) in zip(positions, tex_coords):
            bgl.glTexCoord2f(u, v)
            bgl.glVertex2f(v1, v2)
        bgl.glEnd()
    def draw_texture(cls, _, context):
        sc = context.scene

        if not cls.is_running(context):
            return

        # no textures are selected
        if sc.muv_texture_projection_tex_image == "None":
            return

        # get texture to be renderred
        img = bpy.data.images[sc.muv_texture_projection_tex_image]

        # setup rendering region
        rect = _get_canvas(context, sc.muv_texture_projection_tex_magnitude)
        positions = [
            [rect.x0, rect.y0],
            [rect.x0, rect.y1],
            [rect.x1, rect.y1],
            [rect.x1, rect.y0]
        ]
        tex_coords = [
            [0.0, 0.0],
            [0.0, 1.0],
            [1.0, 1.0],
            [1.0, 0.0]
        ]

        # OpenGL configuration
        if compat.check_version(2, 80, 0) >= 0:
            bgl.glEnable(bgl.GL_BLEND)
            bgl.glEnable(bgl.GL_TEXTURE_2D)
            bgl.glActiveTexture(bgl.GL_TEXTURE0)
            if img.bindcode:
                bind = img.bindcode
                bgl.glBindTexture(bgl.GL_TEXTURE_2D, bind)
        else:
            bgl.glEnable(bgl.GL_BLEND)
            bgl.glEnable(bgl.GL_TEXTURE_2D)
            if img.bindcode:
                bind = img.bindcode[0]
                bgl.glBindTexture(bgl.GL_TEXTURE_2D, bind)
                bgl.glTexParameteri(bgl.GL_TEXTURE_2D,
                                    bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR)
                bgl.glTexParameteri(bgl.GL_TEXTURE_2D,
                                    bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR)
                bgl.glTexEnvi(
                    bgl.GL_TEXTURE_ENV, bgl.GL_TEXTURE_ENV_MODE,
                    bgl.GL_MODULATE)

        # render texture
        bgl.glBegin(bgl.GL_QUADS)
        bgl.glColor4f(1.0, 1.0, 1.0,
                      sc.muv_texture_projection_tex_transparency)
        for (v1, v2), (u, v) in zip(positions, tex_coords):
            bgl.glTexCoord2f(u, v)
            bgl.glVertex2f(v1, v2)
        bgl.glEnd()
Esempio n. 4
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()
Esempio n. 5
0
    def draw(self):
        if self.visible == False: return

        module.post_draw_step += 1

        height = render.getWindowHeight()
        width = render.getWindowWidth()

        bgl.glMatrixMode(bgl.GL_PROJECTION)
        bgl.glLoadIdentity()
        bgl.gluOrtho2D(0, width, 0, height)
        bgl.glMatrixMode(bgl.GL_MODELVIEW)
        bgl.glLoadIdentity()

        # Enable textures
        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)
        bgl.glAlphaFunc(bgl.GL_SRC_ALPHA, 1)

        # Bind the texture
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self._tex_id)

        # Fix position
        w, h = self._size
        bgl.glTranslatef(0, -h, 1)

        #MipLevel
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_BASE_LEVEL, 0)
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAX_LEVEL, 0)
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER,
                            bgl.GL_LINEAR)
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER,
                            bgl.GL_LINEAR)

        # Draw the textured quad
        bgl.glColor4f(*self.color)

        bgl.glBegin(bgl.GL_QUADS)
        self.calculate_glposition()
        for i in range(4):
            bgl.glTexCoord2f(self.texco[i][0], self.texco[i][1])
            bgl.glVertex2f(self.gl_position[i][0], self.gl_position[i][1])
        bgl.glEnd()

        bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)

        bgl.glDisable(bgl.GL_BLEND)
        bgl.glDisable(bgl.GL_TEXTURE_2D)

        bgl.glMatrixMode(bgl.GL_PROJECTION)
        bgl.glPopMatrix()
        bgl.glMatrixMode(bgl.GL_MODELVIEW)
Esempio n. 6
0
	def draw(self):
		if self.visible == False: return
		
		module.post_draw_step += 1
		
		height = render.getWindowHeight()
		width = render.getWindowWidth()
	
		bgl.glMatrixMode(bgl.GL_PROJECTION)
		bgl.glLoadIdentity()
		bgl.gluOrtho2D(0, width, 0, height)
		bgl.glMatrixMode(bgl.GL_MODELVIEW)
		bgl.glLoadIdentity()
	
		# Enable textures
		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)
		bgl.glAlphaFunc(bgl.GL_SRC_ALPHA, 1)

		# Bind the texture
		bgl.glBindTexture(bgl.GL_TEXTURE_2D, self._tex_id)

		# Fix position
		w, h = self._size
		bgl.glTranslatef(0, -h, 1)

		#MipLevel
		bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_BASE_LEVEL, 0);
		bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAX_LEVEL, 0);
		bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR);
		bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR);
		
		# Draw the textured quad
		bgl.glColor4f(*self.color)

		bgl.glBegin(bgl.GL_QUADS)
		self.calculate_glposition()
		for i in range(4):
			bgl.glTexCoord2f(self.texco[i][0], self.texco[i][1])
			bgl.glVertex2f(self.gl_position[i][0], self.gl_position[i][1])
		bgl.glEnd()

		bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
		
		bgl.glDisable(bgl.GL_BLEND)
		bgl.glDisable(bgl.GL_TEXTURE_2D)

		bgl.glMatrixMode(bgl.GL_PROJECTION)
		bgl.glPopMatrix()
		bgl.glMatrixMode(bgl.GL_MODELVIEW)
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()
Esempio n. 8
0
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()
Esempio n. 9
0
def h_draw_texture(id, w, h, bounds, coords):    
    bgl.glEnable(bgl.GL_TEXTURE_2D)
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, id)
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    
    bgl.glColor4f(*(1,1,1,1))
    
    B = bounds
    C = coords
    
    D = [
        (C[0][0]/w, C[0][1]/h),
        (C[1][0]/w, C[1][1]/h),
        (C[2][0]/w, C[2][1]/h),
        (C[3][0]/w, C[3][1]/h),
    ]
    #print(D)
    
    bgl.glBegin(bgl.GL_QUADS)
    bgl.glTexCoord2f(D[0][0], D[0][1])    
    bgl.glVertex2f(B[0], B[1])
    
    bgl.glTexCoord2f(D[1][0], D[1][1])    
    bgl.glVertex2f(B[0]+B[2], B[1])
    
    bgl.glTexCoord2f(D[2][0], D[2][1])    
    bgl.glVertex2f(B[0]+B[2], B[1]+B[3])
    
    bgl.glTexCoord2f(D[3][0], D[3][1])    
    bgl.glVertex2f(B[0], B[1]+B[3])
    bgl.glEnd()

    bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
    bgl.glDisable(bgl.GL_TEXTURE_2D)
Esempio n. 10
0
    def draw(self):
        Widget.draw(self)

        if self.texture is None:
            return

        self.texture.bind()

        bgl.glBegin(bgl.GL_QUADS)
        self.color.apply()

        bgl.glTexCoord2f(0, 0)
        bgl.glVertex2f(self.padding.x, self.padding.h)

        bgl.glTexCoord2f(1, 0)
        bgl.glVertex2f(self.allocation.w - self.padding.w, self.padding.h)

        bgl.glTexCoord2f(1, 1)
        bgl.glVertex2f(self.allocation.w - self.padding.w,
                       self.allocation.h - self.padding.y)

        bgl.glTexCoord2f(0, 1)
        bgl.glVertex2f(self.padding.x, self.allocation.h - self.padding.y)

        bgl.glEnd()
        self.texture.unbind()
Esempio n. 11
0
def draw_image_data(file_path, center_coord):

    bgl.glEnable(bgl.GL_BLEND)
    bgl.glEnable(bgl.GL_TEXTURE_2D)

    image  = load_image_to_data(file_path)
    width = get_image_width(image)
    height = get_image_height(image)

    top_left = (center_coord[0] - width/2, center_coord[1] - height/2)
    top_right = (center_coord[0] + width/2, center_coord[1] - height/2)
    bottom_right = (center_coord[0] + width/2, center_coord[1] + height/2)
    bottom_left = center_coord(0 - width/2, center_coord[1] + height/2)

    bind_image(image)
    
    bgl.glBegin(bgl.GL_QUADS)
    bgl.glTexCoord2f(0.0, 0.0)
    bgl.glVertex2f(*top_left)

    bgl.glTexCoord2f(1.0, 0.0)
    bgl.glVertex2f(*top_right)

    bgl.glTexCoord2f(1.0, 1.0)
    bgl.glVertex2f(*bottom_right)

    bgl.glTexCoord2f(0.0, 1.0)
    bgl.glVertex2f(*bottom_left)
    bgl.glEnd()

    bgl.glDisable(bgl.GL_TEXTURE_2D)
    bgl.glDisable(bgl.GL_BLEND)
Esempio n. 12
0
def draw_callback_mode(self, context):
    # draw mode_title
    mode_title(True, "Object Assembler Mode")
    
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.img.bindcode)
    
    bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST)
    bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST)

    bgl.glLineWidth(1)

    # draw frame
    for icon in self.menu:
        bgl.glColor3f(0.1, 0.1, 0.1)
        bgl.glRecti(
            icon[2][0],icon[2][1],icon[2][2],icon[2][3]
            )

    # icon zeichnen
    for icon in self.menu:
        bgl.glEnable(bgl.GL_TEXTURE_2D)
        bgl.glTexEnvf(bgl.GL_TEXTURE_ENV,bgl.GL_TEXTURE_ENV_MODE, bgl.GL_REPLACE)

        bgl.glBegin(bgl.GL_QUADS)

        bgl.glTexCoord2f(icon[4][0][0], icon[4][0][1])
        bgl.glVertex2f(icon[1][0], icon[1][1])
        
        bgl.glTexCoord2f(icon[4][1][0], icon[4][1][1])
        bgl.glVertex2f(icon[1][0], icon[1][3])
        
        bgl.glTexCoord2f(icon[4][2][0], icon[4][2][1])
        bgl.glVertex2f(icon[1][2], icon[1][3])

        bgl.glTexCoord2f(icon[4][3][0], icon[4][3][1])
        bgl.glVertex2f(icon[1][2], icon[1][1])

        bgl.glEnd()

        bgl.glDisable(bgl.GL_TEXTURE_2D)

    # draw hover effekt
    for icon in self.menu:
        # mouse hover icon
        if mouse_hover_icon(icon[1], self.mouse):
            bgl.glColor3f(0.4, 0.4, 0.4)
            bgl.glLineWidth(2)
            rect_round_corners(icon[3][0], icon[3][1], icon[3][2], icon[3][3])

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
    def draw_texture(self, context):
        wm = context.window_manager
        sc = context.scene
        
        # no texture is selected
        if sc.tex_image == "None":
            return

        # setup rendering region
        rect = get_canvas(context, sc.tex_magnitude)
        positions = [
            [rect.x0, rect.y0],
            [rect.x0, rect.y1],
            [rect.x1, rect.y1],
            [rect.x1, rect.y0]
            ]
        tex_coords = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]]

        # get texture to be renderred
        img = bpy.data.images[sc.tex_image]

        # OpenGL configuration
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glEnable(bgl.GL_TEXTURE_2D)
        if img.bindcode:
            bind = img.bindcode
            bgl.glBindTexture(bgl.GL_TEXTURE_2D, bind)
            bgl.glTexParameteri(
                bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR)
            bgl.glTexParameteri(
                bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR)
            bgl.glTexEnvi(
                bgl.GL_TEXTURE_ENV, bgl.GL_TEXTURE_ENV_MODE, bgl.GL_MODULATE)
        
        # render texture
        bgl.glBegin(bgl.GL_QUADS)
        bgl.glColor4f(1.0, 1.0, 1.0, sc.tex_transparency)
        for (v1, v2), (u, v) in zip(positions, tex_coords):
            bgl.glTexCoord2f(u, v)
            bgl.glVertex2f(v1, v2)
        bgl.glEnd()
Esempio n. 14
0
    def draw(self, w, h):
        if self.nxstretch > 0:
            sx = (w - self.xfixed) / self.nxstretch
            tx = (self.texture.width - self.xfixed) / self.nxstretch

        if self.nystretch > 0:
            sy = (h - self.yfixed) / self.nystretch
            ty = (self.texture.height - self.yfixed) / self.nystretch

        y0 = 0
        ty0 = 0

        l = lambda x, sx: {True: sx, False: x}[x < 0]

        self.texture.bind()

        bgl.glBegin(bgl.GL_QUADS)
        self.color.apply()

        for y in self.yregions:
            x0 = 0
            tx0 = 0

            dy = l(y, sy)
            dty = l(y, ty) / self.texture.height

            for x in self.xregions:
                dx = l(x, sx)
                dtx = l(x, tx) / self.texture.width

                bgl.glTexCoord2f(tx0, ty0)
                bgl.glVertex2f(x0, y0)

                bgl.glTexCoord2f(tx0 + dtx, ty0)
                bgl.glVertex2f(x0 + dx, y0)

                bgl.glTexCoord2f(tx0 + dtx, ty0 + dty)
                bgl.glVertex2f(x0 + dx, y0 + dy)

                bgl.glTexCoord2f(tx0, ty0 + dty)
                bgl.glVertex2f(x0, y0 + dy)

                x0 += dx
                tx0 += dtx

            y0 += dy
            ty0 += dty

        bgl.glEnd()

        self.texture.unbind()
Esempio n. 15
0
    def afficher(self):
        """Affiche l'image"""
        VT = __class__.VT
        gl = __class__.gl
        bgl = __class__.bgl
        math = __class__.math

        if self.ok:

            id = self.VT_img.bindId

            if id:
                bgl.glMatrixMode(bgl.GL_PROJECTION)
                bgl.glLoadIdentity()
                vp = self.viewport
                bgl.glOrtho(vp[0], vp[1], vp[2], vp[3], -1.0, 1.0)

                bgl.glMatrixMode(bgl.GL_MODELVIEW)
                bgl.glLoadIdentity()

                bgl.glEnable(bgl.GL_TEXTURE_2D)
                bgl.glEnable(bgl.GL_BLEND)
                bgl.glBindTexture(bgl.GL_TEXTURE_2D, id)

                bgl.glTranslatef(self.bary[0], self.bary[1], 0)
                bgl.glRotatef(math.degrees(self.angle), 0.0, 0.0, 1.0)
                bgl.glTranslatef(-self.bary[0], -self.bary[1], 0)

                bgl.glBegin(bgl.GL_QUADS)
                col = self.couleur
                for i in range(len(self.texco)):
                    bgl.glColor4f(col[0], col[1], col[2], col[3])
                    bgl.glTexCoord2f(self.texco[i][0], self.texco[i][1])
                    bgl.glVertex2f(self.verco[i][0], self.verco[i][1])
                bgl.glEnd()

                bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
            else:
                self.ok = False

        return
Esempio n. 16
0
def draw_callback_common(self, context, ots):
    if Pref.use_alpha_clip:
        bgl.glAlphaFunc(bgl.GL_GREATER, 0.1)
        bgl.glEnable(bgl.GL_ALPHA_TEST)

    orig_is_enabled_depthtest = bgl.glIsEnabled(bgl.GL_DEPTH_TEST)
    bgl.glEnable(bgl.GL_DEPTH_TEST)

    bgl.glColor4f(1.0, 1.0, 1.0, 1.0)
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glEnable(bgl.GL_TEXTURE_2D)

    texcos = [(0.,0.), (1.,0.), (1.,1.), (0.,1.)]


    fr = context.scene.frame_current
    for l,obj,tex in ots:
        ps = [obj.matrix_world*v.co for v in obj.data.vertices[0:4]]
        if tex.blendmode == BlendMode.ALPHAOVER:
            bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
            bgl.glBlendEquation(bgl.GL_FUNC_ADD)
        elif tex.blendmode == BlendMode.ADDITIVE:
            bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE)
            bgl.glBlendEquation(bgl.GL_FUNC_ADD)
        tex.bind()
        bgl.glBegin(bgl.GL_QUADS)
        for p,t in zip(ps, texcos):
            bgl.glTexCoord2f(*t)
            bgl.glVertex3f(*p)
        bgl.glEnd()

    #bgl.glDepthMask(bgl.GL_TRUE)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    bgl.glBlendEquation(bgl.GL_FUNC_ADD)
    bgl.glDisable(bgl.GL_TEXTURE_2D)
    bgl.glDisable(bgl.GL_BLEND)
    if Pref.use_alpha_clip:
        bgl.glDisable(bgl.GL_ALPHA_TEST)
    if not orig_is_enabled_depthtest:
        bgl.glDisable(bgl.GL_DEPTH_TEST)
    def draw_texture(self, context):
        wm = context.window_manager
        sc = context.scene

        # no texture is selected
        if sc.tex_image == "None":
            return

        # setup rendering region
        rect = get_canvas(context, sc.tex_magnitude)
        positions = [[rect.x0, rect.y0], [rect.x0, rect.y1],
                     [rect.x1, rect.y1], [rect.x1, rect.y0]]
        tex_coords = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]]

        # get texture to be renderred
        img = bpy.data.images[sc.tex_image]

        # OpenGL configuration
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glEnable(bgl.GL_TEXTURE_2D)
        if img.bindcode:
            bind = img.bindcode
            bgl.glBindTexture(bgl.GL_TEXTURE_2D, bind)
            bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER,
                                bgl.GL_LINEAR)
            bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER,
                                bgl.GL_LINEAR)
            bgl.glTexEnvi(bgl.GL_TEXTURE_ENV, bgl.GL_TEXTURE_ENV_MODE,
                          bgl.GL_MODULATE)

        # render texture
        bgl.glBegin(bgl.GL_QUADS)
        bgl.glColor4f(1.0, 1.0, 1.0, sc.tex_transparency)
        for (v1, v2), (u, v) in zip(positions, tex_coords):
            bgl.glTexCoord2f(u, v)
            bgl.glVertex2f(v1, v2)
        bgl.glEnd()
Esempio n. 18
0
def render_callback(self, context):
    if self.bindcode != None:
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D,
                            bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR)
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D,
                            bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR)
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.bindcode)
        bgl.glEnable(bgl.GL_TEXTURE_2D)
        bgl.glColor4f(1.0, 1.0, 1.0, 1.0)
        bgl.glBegin(bgl.GL_QUADS)
        bgl.glTexCoord2f(0.0, 0.0)
        bgl.glVertex2f(self.x, self.y)
        bgl.glTexCoord2f(1.0, 0.0)
        bgl.glVertex2f(self.x + self.width, self.y)
        bgl.glTexCoord2f(1.0, 1.0)
        bgl.glVertex2f(self.x + self.width, self.y + self.height)
        bgl.glTexCoord2f(0.0, 1.0)
        bgl.glVertex2f(self.x, self.y + self.height)
        bgl.glEnd()
        bgl.glDisable(bgl.GL_TEXTURE_2D)
Esempio n. 19
0
def image_quad(img,color,verts):
    img.gl_load(bgl.GL_NEAREST, bgl.GL_NEAREST)
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, img.bindcode)
    bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST)
    bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST)
    bgl.glEnable(bgl.GL_TEXTURE_2D)
    bgl.glEnable(bgl.GL_BLEND)
    #bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    bgl.glColor4f(color[0], color[1], color[2], color[3])
    bgl.glBegin(bgl.GL_QUADS)
    #http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V51B_HTML/MAN/MAN3/2025____.HTM
    bgl.glTexCoord2f(0,0)
    bgl.glVertex2f(verts[0][0],verts[0][1])
    bgl.glTexCoord2f(0,1)
    bgl.glVertex2f(verts[1][0],verts[1][1])
    bgl.glTexCoord2f(1,1)
    bgl.glVertex2f(verts[2][0],verts[2][1])
    bgl.glTexCoord2f(1,0)
    bgl.glVertex2f(verts[3][0],verts[3][1])
    bgl.glEnd()
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glDisable(bgl.GL_TEXTURE_2D)
Esempio n. 20
0
def image_quad(img,color,verts):
    img.gl_load(bgl.GL_NEAREST, bgl.GL_NEAREST)
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, img.bindcode)
    bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST)
    bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST)
    bgl.glEnable(bgl.GL_TEXTURE_2D)
    bgl.glEnable(bgl.GL_BLEND)
    #bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    bgl.glColor4f(color[0], color[1], color[2], color[3])
    bgl.glBegin(bgl.GL_QUADS)
    #http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V51B_HTML/MAN/MAN3/2025____.HTM
    bgl.glTexCoord2f(0,0)
    bgl.glVertex2f(verts[0][0],verts[0][1])
    bgl.glTexCoord2f(0,1)
    bgl.glVertex2f(verts[1][0],verts[1][1])
    bgl.glTexCoord2f(1,1)
    bgl.glVertex2f(verts[2][0],verts[2][1])
    bgl.glTexCoord2f(1,0)
    bgl.glVertex2f(verts[3][0],verts[3][1])
    bgl.glEnd()
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glDisable(bgl.GL_TEXTURE_2D)
Esempio n. 21
0
    def draw_image(self, image, x, y, w, h, color=(0, 0, 0, 0.1)):
        bgl.glColor4f(0.5, 0.0, 0.5, 0.7)

        # draw main line and handles
        # bgl.glBegin(bgl.GL_LINES)
        bgl.glRectf(x, y, x + w, y + h)
        # bgl.glEnd()
        x1 = x
        y1 = y
        x2 = x + w
        y2 = y + h
        color = [0.5, 0.5, 0.5, 1]

        idx = image.gl_load(bgl.GL_NEAREST, bgl.GL_NEAREST)
        print([i for i in image.bindcode])

        bgl.glBindTexture(bgl.GL_TEXTURE_2D, image.bindcode[0])
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER,
                            bgl.GL_NEAREST)

        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER,
                            bgl.GL_NEAREST)
        bgl.glEnable(bgl.GL_TEXTURE_2D)
        bgl.glEnable(bgl.GL_BLEND)
        #bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
        bgl.glColor4f(color[0], color[1], color[2], color[3])
        bgl.glBegin(bgl.GL_QUADS)
        bgl.glTexCoord2f(0, 0)
        bgl.glVertex2f(x1, y1)
        bgl.glTexCoord2f(0, 1)
        bgl.glVertex2f(x1, y2)
        bgl.glTexCoord2f(1, 1)
        bgl.glVertex2f(x2, y2)
        bgl.glTexCoord2f(1, 0)
        bgl.glVertex2f(x2, y1)
        bgl.glEnd()
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glDisable(bgl.GL_TEXTURE_2D)
    def draw_image(self, image, x, y, w, h, color=(0, 0, 0, 0.1)):
        bgl.glColor4f(0.5, 0.0, 0.5, 0.7)

        # draw main line and handles
        # bgl.glBegin(bgl.GL_LINES)
        bgl.glRectf(x, y, x + w, y + h)
        # bgl.glEnd()
        x1 = x
        y1 = y
        x2 = x + w
        y2 = y + h
        color = [0.5, 0.5, 0.5, 1]

        idx = image.gl_load(bgl.GL_NEAREST, bgl.GL_NEAREST)
        print([i for i in image.bindcode])

        bgl.glBindTexture(bgl.GL_TEXTURE_2D, image.bindcode[0])
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST)

        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST)
        bgl.glEnable(bgl.GL_TEXTURE_2D)
        bgl.glEnable(bgl.GL_BLEND)
        # bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
        bgl.glColor4f(color[0], color[1], color[2], color[3])
        bgl.glBegin(bgl.GL_QUADS)
        bgl.glTexCoord2f(0, 0)
        bgl.glVertex2f(x1, y1)
        bgl.glTexCoord2f(0, 1)
        bgl.glVertex2f(x1, y2)
        bgl.glTexCoord2f(1, 1)
        bgl.glVertex2f(x2, y2)
        bgl.glTexCoord2f(1, 0)
        bgl.glVertex2f(x2, y1)
        bgl.glEnd()
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glDisable(bgl.GL_TEXTURE_2D)
Esempio n. 23
0
def Draw_map_callback(self, context):

    if context.area != Map.view3d_area:
        return
    elif context.area.type == 'PROPERTIES' and \
            context.space_data.context != 'WORLD':
        return

    # Check if window area has changed for sticky zoom
    theMap = Map.object[0]
    if Map.region.width < Map.saved_region_width:
        diff = Map.saved_region_width - Map.region.width
        if theMap.origin.x + theMap.width > Map.saved_region_width:
            if theMap.origin.x > 0:
                theMap.origin.x -= diff
            else:
                theMap.width -= diff
        else:
            if Map.toolProps is not None:
                if Map.toolProps.width > Map.toolProps_width:
                    theMap.origin.x -= diff
                Map.toolProps_width = Map.toolProps.width
            if theMap.origin.x < 0:
                theMap.origin.x += diff
    else:
        diff = Map.region.width - Map.saved_region_width
        if theMap.width > Map.saved_region_width:
            theMap.width += diff
        else:
            if Map.toolProps is not None:
                if Map.toolProps.width < Map.toolProps_width:
                    theMap.origin.x += diff
                Map.toolProps_width = Map.toolProps.width
    theMap.set_dimensions(theMap.width)
    Map.saved_region_width = Map.region.width

    # Latitude and longitude are set to an equidistant
    # cylindrical projection with lat/long 0/0 exactly
    # in the middle of the image.

    zLong = theMap.width / 2
    longFac = zLong / 180
    zLat = theMap.height / 2
    latFac = zLat / 90
    crossChange = True

    if not Map.action == 'PAN':
        x = Map.mouse.x
        y = Map.mouse.y
        if x < theMap.origin.x or x > theMap.origin.x + theMap.width:
            crossChange = False
            x = 0
        else:
            testBoundary = theMap.origin.x + theMap.width
            if testBoundary < Map.region.width:
                rightBoundary = testBoundary
            else:
                rightBoundary = Map.region.width
            if x > rightBoundary:
                crossChange = False
                x = rightBoundary
        cX = x - zLong - theMap.origin.x

        if longFac:
            newLongitude = cX / longFac
        else:
            newLongitude = 0.0

        if y < theMap.origin.y or y < 0:
            crossChange = False
            y = 0
        elif y > theMap.origin.y + theMap.height:
            crossChange = False
            y = theMap.origin.y + theMap.height
        cY = y - zLat - theMap.origin.y

        if latFac:
            newLatitude = cY / latFac
        else:
            newLatitude = 0.0

        if newLatitude == Map.latitude and newLongitude == Map.longitude:
            crossChange = False
        else:
            Map.latitude = newLatitude
            Map.longitude = newLongitude
    else:
        if Map.grab.offset.x < Map.grab.spot.x:
            off = Map.grab.spot.x - Map.grab.offset.x
            theMap.origin.x -= off
        else:
            off = Map.grab.offset.x - Map.grab.spot.x
            theMap.origin.x += off
        if Map.grab.offset.y < Map.grab.spot.y:
            off = Map.grab.spot.y - Map.grab.offset.y
            theMap.origin.y -= off
        else:
            off = Map.grab.offset.y - Map.grab.spot.y
            theMap.origin.y += off
        Map.grab.spot.x = Map.mouse.x
        Map.grab.spot.y = Map.mouse.y

    Lx = theMap.origin.x
    Ly = theMap.origin.y

    # ---------------------
    # Draw a textured quad
    # ---------------------

    if not Map.textureless:
        bgl.glEnable(bgl.GL_BLEND)
        if Map.glImage.bindcode == 0:
            Map.load_gl_image()
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, Map.glImage.bindcode[0])
        bgl.glEnable(bgl.GL_TEXTURE_2D)
        bgl.glColor4f(1.0, 1.0, 1.0, Map.object[0].opacity)
        bgl.glBegin(bgl.GL_QUADS)
        bgl.glTexCoord2f(0.0, 0.0)
        bgl.glVertex2f(Lx, Ly)
        bgl.glTexCoord2f(1.0, 0.0)
        bgl.glVertex2f(Lx + theMap.width, Ly)
        bgl.glTexCoord2f(1.0, 1.0)
        bgl.glVertex2f(Lx + theMap.width, Ly + theMap.height)
        bgl.glTexCoord2f(0.0, 1.0)
        bgl.glVertex2f(Lx, theMap.height + Ly)
        bgl.glEnd()
        bgl.glDisable(bgl.GL_TEXTURE_2D)

    # -----------------------
    # Output text for stats
    # -----------------------
    if Map.action == 'TIME' or Map.action == 'DAY' or Map.showInfo:
        Map.show_text_in_viewport(Map.object[1])

    # ---------------------
    # draw the crosshair
    # ---------------------
    x = theMap.width / 2.0
    if crossChange and not Map.lockCrosshair:
        if Map.action != 'Y':
            Sun.SP.Longitude = newLongitude
    longitude = (Sun.SP.Longitude * x / 180.0) + x

    bgl.glEnable(bgl.GL_BLEND)
    bgl.glEnable(bgl.GL_LINES)
    bgl.glLineWidth(1.0)
    alpha = 1.0 if Map.action == 'Y' else 0.5
    color = (0.894, 0.741, .510, alpha)
    bgl.glColor4f(color[0], color[1], color[2], color[3])
    bgl.glBegin(bgl.GL_LINES)
    bgl.glVertex2f(Lx + longitude, Ly)
    bgl.glVertex2f(Lx + longitude, Ly + theMap.height)
    bgl.glEnd()

    y = theMap.height / 2.0
    if crossChange and not Map.lockCrosshair:
        if Map.action != 'X':
            Sun.SP.Latitude = newLatitude
    latitude = (Sun.SP.Latitude * y / 90.0) + y

    alpha = 1.0 if Map.action == 'X' else 0.5
    color = (0.894, 0.741, .510, alpha)
    bgl.glColor4f(color[0], color[1], color[2], color[3])
    bgl.glBegin(bgl.GL_LINES)
    bgl.glVertex2f(Lx, Ly + latitude)
    bgl.glVertex2f(Lx + theMap.width, Ly + latitude)
    bgl.glEnd()

    # ---------------------
    # draw the border
    # ---------------------
    bgl.glDisable(bgl.GL_BLEND)
    color = (0.6, 0.6, .6, 1.0)
    bgl.glColor4f(color[0], color[1], color[2], color[3])
    bgl.glBegin(bgl.GL_LINE_LOOP)
    bgl.glVertex2f(Lx, Ly)
    bgl.glVertex2f(Lx + theMap.width, Ly)
    bgl.glVertex2f(Lx + theMap.width, Ly + theMap.height)
    bgl.glVertex2f(Lx, theMap.height + Ly)
    bgl.glVertex2f(Lx, Ly)
    bgl.glEnd()

    if not Sun.ShowRiseSet or not Map.lineWidth:
        bgl.glDisable(bgl.GL_LINES)
        bgl.glFlush()
        return

    if Map.action == 'G':
        draw_text_region()

    # ------------------------
    # draw the sunrise, sunset
    # ------------------------

    def draw_angled_line(color, angle, bx, by):
        x = math.cos(angle) * radius
        y = math.sin(angle) * radius
        bgl.glColor4f(color[0], color[1], color[2], color[3])
        bgl.glBegin(bgl.GL_LINES)
        bgl.glVertex2f(bx, by)
        bgl.glVertex2f(bx + x, by + y)
        bgl.glEnd()

    px = Lx + longitude
    py = Ly + latitude

    radius = 30 + Map.lineWidth * 10
    if Sun.RiseSetOK and Map.lineWidth:
        color = (0.2, 0.6, 1.0, 0.9)
        angle = -(degToRad(Sun.Sunrise.azimuth) - math.pi / 2)
        bgl.glLineWidth(Map.lineWidth)
        draw_angled_line(color, angle, px, py)

        color = (0.86, 0.18, 0.18, 0.9)
        angle = -(degToRad(Sun.Sunset.azimuth) - math.pi / 2)
        draw_angled_line(color, angle, px, py)

    # ------------------------
    # draw current time line
    # ------------------------

    if Map.textureless:
        phi = degToRad(Sun.AzNorth) * -1
    else:
        phi = degToRad(Sun.Azimuth) * -1
    x = math.sin(phi) * math.sin(-Sun.Theta) * (radius + 10)
    y = math.sin(Sun.Theta) * math.cos(phi) * (radius + 10)
    night = (0.24, 0.29, 0.94, 0.9)
    day = (0.85, 0.77, 0.60, 0.9)
    if Sun.SolarNoon.elevation < 0.0:
        color = night
    elif Sun.Elevation >= Sun.Sunrise.elevation:
        if Sun.Time >= Sun.Sunset.time and \
                Sun.Elevation <= Sun.Sunset.elevation:
            color = night
        else:
            color = day
    else:
        color = night
    bgl.glLineWidth(Map.lineWidth + 1.0)

    bgl.glColor4f(color[0], color[1], color[2], color[3])
    bgl.glBegin(bgl.GL_LINES)
    bgl.glVertex2f(px, py)
    bgl.glVertex2f(px + x, py + y)
    bgl.glEnd()
    bgl.glLineWidth(1.0)
    bgl.glDisable(bgl.GL_LINES)
    bgl.glFlush()
Esempio n. 24
0
    def draw(self, parent, context):
        #get the addon settings/prefs
        settings = context.user_preferences.addons['piemenus'].preferences

        #grab the biggest text dimensions that we already calced previously
        biggestdimensionX = self.biggest_dim_x
        biggestdimensionY = self.biggest_dim_y

        for it in self.sliders:
            #draw some text above it.
            blf.size(0, self.text_size, self.text_dpi)
            dimension = blf.dimensions(0, it.id)

            x = it.x - dimension[0] * .5
            y = it.y + (it.height + dimension[1]) * 0.5 + 2
            # Draw text
            blf.enable(0, blf.SHADOW)
            blf.shadow_offset(0, 0, 0)
            blf.shadow(0, 5, 0.0, 0.0, 0.0, 1.0)
            bgl.glColor3f(self.rt, self.gt, self.bt)

            blf.position(0, x, y, 0)
            blf.draw(0, it.id)
            blf.disable(0, blf.SHADOW)

            #draw left side one color
            bgl.glColor4f(self.rsSelected, self.gsSelected, self.bsSelected,
                          1.0)
            pmu.draw_outline_or_region(bgl.GL_TRIANGLE_FAN, it.screen_left)

            #draw the right side another color
            bgl.glColor4f(self.rsInner, self.gsInner, self.bsInner, 1.0)
            pmu.draw_outline_or_region(bgl.GL_TRIANGLE_FAN, it.screen_right)

            #Draw box outline
            bgl.glColor4f(self.rsOutline, self.gsOutline, self.bsOutline, 1.0)
            pmu.draw_outline_or_region(bgl.GL_LINE_LOOP, it.screen_left)
            pmu.draw_outline_or_region(bgl.GL_LINE_LOOP, it.screen_right)

            #put the text on top
            blf.enable(0, blf.KERNING_DEFAULT)
            prop_text = str(getattr(it.data, it.prop, 0))[0:4]
            #prop_text = "Test"
            dimensions2 = blf.dimensions(0, prop_text)
            x2 = it.x - dimensions2[0] * 0.5
            y2 = it.y - dimensions2[1] * 0.5
            blf.position(0, x2, y2, 0)
            bgl.glColor4f(self.rt, self.gt, self.bt, 1.0)
            blf.draw(0, prop_text)
            blf.disable(0, blf.KERNING_DEFAULT)

        for it in self.menu_items:
            sel = it == parent.current
            it_poll = it.poll(context)

            # center item on the circle
            #x = (self.menu_x + it.x) - (dimension[0] * 0.5)
            #y = (self.menu_y + it.y) - (dimension[1] * 0.5)

            blf.size(0, self.text_size, self.text_dpi)
            dimension = blf.dimensions(0, it.id)

            #needed for box centering
            x = (it.x) - (biggestdimensionX * 0.5)
            y = (it.y) - (biggestdimensionY * 0.5)

            #needed offset for text centering
            blf.size(0, self.text_size, self.text_dpi)
            dimension = blf.dimensions(0, it.id)
            xt = ((biggestdimensionX - dimension[0]) * 0.5)
            yt = ((biggestdimensionY - dimension[1]) * 0.5)

            # Draw background buttons
            if sel and it_poll:
                bgl.glColor4f(self.ris, self.gis, self.bis, self.ais)
            else:
                bgl.glColor4f(self.ri, self.gi, self.bi, self.ai)

            #self.gl_pie_slice(bgl.GL_POLYGON, 8, it, x,y,30,90) #***
            #http://www.opengl.org/archives/resources/faq/technical/rasterization.htm#rast0120
            #self._round_box(bgl.GL_POLYGON, x - 20, y - 5, x + biggestdimensionX + 20, y + biggestdimensionY + 5)

            if it.screen_poly_bound:
                shape = it.screen_poly_bound
                pmu.draw_outline_or_region(bgl.GL_TRIANGLE_FAN, shape)
                bgl.glColor4f(self.ro, self.go, self.bo, 1.0)
                pmu.draw_outline_or_region(bgl.GL_LINE_LOOP, shape)

            bgl.glColor4f(self.ri, self.gi, self.bi, self.ai)
            #draw the circle
            if settings.clockBool:
                self._circle_(bgl.GL_TRIANGLE_FAN, (self.menu_x),
                              (self.menu_y), 20)

                #draw the circle outline
                bgl.glColor4f(self.ro, self.go, self.bo, 1.0)
                self._circle_(bgl.GL_LINE_LOOP, (self.menu_x), (self.menu_y),
                              20)

                self._pointer_(bgl.GL_TRIANGLE_STRIP, (self.menu_x),
                               (self.menu_y), self.pointerangle)

            # Draw text
            blf.enable(0, blf.SHADOW)
            blf.shadow_offset(0, 0, 0)
            blf.shadow(0, 5, 0.0, 0.0, 0.0, 1.0)
            if it_poll:
                bgl.glColor3f(self.rt, self.gt, self.bt)
            else:  # grayed out
                bgl.glColor3f(0.5, 0.5, 0.5)
            blf.position(0, x + xt, y + yt, 0)
            blf.draw(0, it.id)
            blf.disable(0, blf.SHADOW)

        #bind the named texure to GL_TEXTURE_2D
        #http://stackoverflow.com/questions/11217121/how-to-manage-memory-with-texture-in-opengl
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, settings.pieIconBindcode)
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER,
                            bgl.GL_NEAREST)

        bgl.glEnable(bgl.GL_TEXTURE_2D)
        bgl.glEnable(bgl.GL_BLEND)

        for it in self.menu_items:
            bgl.glColor4f(1, 1, 1, 1)
            if it.icon:
                #place the icon quad
                verts = it.screen_icon_quad

                bgl.glBegin(bgl.GL_QUADS)

                bgl.glTexCoord2f(it.tex_coords[0][0], it.tex_coords[0][1])
                bgl.glVertex2f(verts[0][0], verts[0][1])
                bgl.glTexCoord2f(it.tex_coords[1][0], it.tex_coords[1][1])
                bgl.glVertex2f(verts[1][0], verts[1][1])
                bgl.glTexCoord2f(it.tex_coords[2][0], it.tex_coords[2][1])
                bgl.glVertex2f(verts[2][0], verts[2][1])
                bgl.glTexCoord2f(it.tex_coords[3][0], it.tex_coords[3][1])
                bgl.glVertex2f(verts[3][0], verts[3][1])

                bgl.glEnd()

            #TODO
            #text value in center?
            #labe over top?
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glDisable(bgl.GL_TEXTURE_2D)
Esempio n. 25
0
    def execute(self, refholder):
        # pass
        tr = self.nodeTree.properties.TextureResolution
        print("begining execution " + str(tr))
        # compute A'
        mask = np.array([[0.05, 0.2, 0.05], [0.2, -1, 0.2], [0.05, 0.2, 0.05]])
        # Input data pixels
        A = self.inputs[0].getPixels()
        B = self.inputs[1].getPixels()

        # print(A)

        bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM,
                          self.rdData[self.name]["prev_program"])
        bgl.glUseProgram(self.rdData[self.name]["program"])

        #set any uniforms needed
        bgl.glUniform1f(self.rdData[self.name]["feed_loc"],
                        self.inputs[2].value)
        bgl.glUniform1f(self.rdData[self.name]["kill_loc"],
                        self.inputs[3].value)
        bgl.glUniform1f(self.rdData[self.name]["da_loc"], self.inputs[4].value)
        bgl.glUniform1f(self.rdData[self.name]["db_loc"], self.inputs[5].value)
        bgl.glUniform1f(self.rdData[self.name]["dt_loc"], self.inputs[6].value)
        bgl.glUniform1f(self.rdData[self.name]["step_loc"], 1 / tr)

        bgl.glDisable(bgl.GL_SCISSOR_TEST)
        bgl.glViewport(0, 0, tr, tr)

        bgl.glMatrixMode(bgl.GL_MODELVIEW)
        bgl.glPushMatrix()
        bgl.glLoadIdentity()

        bgl.glMatrixMode(bgl.GL_PROJECTION)
        bgl.glPushMatrix()
        bgl.glLoadIdentity()
        bgl.gluOrtho2D(0, 1, 0, 1)

        bgl.glEnable(bgl.GL_TEXTURE_2D)
        bgl.glActiveTexture(bgl.GL_TEXTURE0)

        channels = []
        if self.channels == '0':
            channels = [0]
        elif self.channels == '1':
            channels = [0, 1, 2]

        for j in channels:
            self.rdData[self.name]["npArray"][:, :, 0] = A[:, :, j]
            self.rdData[self.name]["npArray"][:, :, 1] = B[:, :, j]

            # Caution: Interfacing with Cython requires toList()
            self.rdData[self.name]["image"].pixels = self.rdData[
                self.name]["npArray"].flatten()

            self.rdData[self.name]["image"].gl_load(0, bgl.GL_LINEAR,
                                                    bgl.GL_LINEAR)

            bgl.glBindTexture(bgl.GL_TEXTURE_2D,
                              self.rdData[self.name]["image"].bindcode[0])
            bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S,
                                bgl.GL_REPEAT)
            bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T,
                                bgl.GL_REPEAT)

            for i in range(self.inputs[7].value):
                bgl.glClearDepth(1.0)
                bgl.glClearColor(0.0, 0.0, 0.0, 0.0)
                bgl.glClear(bgl.GL_COLOR_BUFFER_BIT | bgl.GL_DEPTH_BUFFER_BIT)

                bgl.glBegin(bgl.GL_TRIANGLES)

                bgl.glColor3f(1.0, 1.0, 1.0)
                bgl.glTexCoord2f(0.0, 0.0)
                bgl.glVertex2f(-1.0, -1.0)
                bgl.glTexCoord2f(1.0, 0.0)
                bgl.glVertex2f(1.0, -1.0)
                bgl.glTexCoord2f(1.0, 1.0)
                bgl.glVertex2f(1.0, 1.0)

                bgl.glColor3f(1.0, 1.0, 1.0)
                bgl.glTexCoord2f(0.0, 0.0)
                bgl.glVertex2f(-1.0, -1.0)
                bgl.glTexCoord2f(1.0, 1.0)
                bgl.glVertex2f(1.0, 1.0)
                bgl.glTexCoord2f(0.0, 1.0)
                bgl.glVertex2f(-1.0, 1.0)
                bgl.glEnd()

                bgl.glCopyTexImage2D(
                    bgl.GL_TEXTURE_2D,  #target
                    0,  #level
                    bgl.GL_RGBA,  #internalformat
                    0,  #x
                    0,  #y
                    tr,
                    tr,
                    0  #border
                )

                #glFlush glFinish or none here?
                bgl.glFinish()

            bgl.glReadPixels(0, 0, tr, tr, bgl.GL_RGBA, bgl.GL_FLOAT,
                             self.rdData[self.name]["buffer"])
            self.rdData[self.name]["image"].pixels = self.rdData[
                self.name]["buffer"][:]
            #write the image channel
            npImage = np.asarray(self.rdData[self.name]["image"].pixels,
                                 dtype="float")
            self.rdData[self.name]["npArray"] = npImage.reshape(
                tr, tr, self.rdData[self.name]["image"].channels)

            self.outputs[0].setPackedImageFromChannels(
                self.rdData[self.name]["npArray"][:, :, 0], j, flatten=True)
            self.outputs[1].setPackedImageFromChannels(
                self.rdData[self.name]["npArray"][:, :, 1], j, flatten=True)
            self.outputs[2].setPackedImageFromPixels(
                self.rdData[self.name]["npArray"])

            self.inputs[0].setPackedImageFromChannels(
                self.rdData[self.name]["npArray"][:, :, 0], j, flatten=True)
            self.inputs[1].setPackedImageFromChannels(
                self.rdData[self.name]["npArray"][:, :, 1], j, flatten=True)

            # ================================= Test bed
            # self.outputs[0].getTexture().image.copy()
            # self.outputs[1].getTexture().image.copy()
            # self.outputs[2].getTexture().image.copy()

            # nparr = np.asarray(self.outputs[0].getTexture().image.pixels, dtype="float")
            # nparr = nparr.reshape(tr, tr, 4)
            # print(nparr)

            self.rdData[self.name]["image"].gl_free()

        #restore the state so blender wont break
        bgl.glEnable(bgl.GL_SCISSOR_TEST)
        bgl.glUseProgram(self.rdData[self.name]["prev_program"][0])
        bgl.glActiveTexture(bgl.GL_TEXTURE0)
Esempio n. 26
0
def Draw_hdr_callback(self, context):

    if context.area != Hdr.view3d_area:
        return
    elif context.area.type == 'PROPERTIES' and \
            context.space_data.context != 'WORLD':
        return

    # Check if window area has changed for sticky zoom
    theHdr = Hdr.object[0]
    if Hdr.region.width < Hdr.saved_region_width:
        diff = Hdr.saved_region_width - Hdr.region.width
        if theHdr.origin.x + theHdr.width > Hdr.saved_region_width:
            if theHdr.origin.x > 0:
                theHdr.origin.x -= diff
            else:
                theHdr.width -= diff
        else:
            if Hdr.toolProps is not None:
                if Hdr.toolProps.width > Hdr.toolProps_width:
                    theHdr.origin.x -= diff
                Hdr.toolProps_width = Hdr.toolProps.width
            if theHdr.origin.x < 0:
                theHdr.origin.x += diff
    else:
        diff = Hdr.region.width - Hdr.saved_region_width
        if theHdr.width > Hdr.saved_region_width:
            theHdr.width += diff
        else:
            if Hdr.toolProps is not None:
                if Hdr.toolProps.width < Hdr.toolProps_width:
                    theHdr.origin.x += diff
                Hdr.toolProps_width = Hdr.toolProps.width
    theHdr.set_dimensions(theHdr.width)
    Hdr.saved_region_width = Hdr.region.width

    zAzim = theHdr.width / 2
    azimFac = zAzim / 180
    zElev = theHdr.height / 2
    elevFac = zElev / 90
    crossChange = True

    if not Hdr.action == 'PAN':
        x = Hdr.mouse.x
        y = Hdr.mouse.y
        if x < theHdr.origin.x or x > theHdr.origin.x + theHdr.width:
            crossChange = False
            x = 0
        else:
            testBoundary = theHdr.origin.x + theHdr.width
            if testBoundary < Hdr.region.width:
                rightBoundary = testBoundary
            else:
                rightBoundary = Hdr.region.width
            if x > rightBoundary:
                crossChange = False
                x = rightBoundary
        cX = x - zAzim - theHdr.origin.x

        if azimFac:
            newAzimuth = cX / azimFac
        else:
            newAzimuth = 0.0

        if y < theHdr.origin.y or y < 0:
            crossChange = False
            y = 0
        elif y > theHdr.origin.y + theHdr.height:
            crossChange = False
            y = theHdr.origin.y + theHdr.height
        cY = y - zElev - theHdr.origin.y

        if elevFac:
            newElevation = cY / elevFac
        else:
            newElevation = 0.0

        if newElevation == Hdr.elevation and newAzimuth == Hdr.azimuth:
            crossChange = False
        else:
            Hdr.elevation = newElevation
            Hdr.azimuth = newAzimuth
    else:
        if Hdr.grab.offset.x < Hdr.grab.spot.x:
            off = Hdr.grab.spot.x - Hdr.grab.offset.x
            theHdr.origin.x -= off
        else:
            off = Hdr.grab.offset.x - Hdr.grab.spot.x
            theHdr.origin.x += off
        if Hdr.grab.offset.y < Hdr.grab.spot.y:
            off = Hdr.grab.spot.y - Hdr.grab.offset.y
            theHdr.origin.y -= off
        else:
            off = Hdr.grab.offset.y - Hdr.grab.spot.y
            theHdr.origin.y += off
        Hdr.grab.spot.x = Hdr.mouse.x
        Hdr.grab.spot.y = Hdr.mouse.y

    Lx = theHdr.origin.x
    Ly = theHdr.origin.y

    # ---------------------
    # Draw a textured quad
    # ---------------------

    bgl.glEnable(bgl.GL_BLEND)
    if Hdr.glImage.bindcode == 0:
        Hdr.load_gl_image()
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, Hdr.glImage.bindcode)
    bgl.glEnable(bgl.GL_TEXTURE_2D)
    bgl.glColor4f(1.0, 1.0, 1.0, Hdr.object[0].opacity)
    bgl.glBegin(bgl.GL_QUADS)
    bgl.glTexCoord2f(0.0, 0.0)
    bgl.glVertex2f(Lx, Ly)
    bgl.glTexCoord2f(1.0, 0.0)
    bgl.glVertex2f(Lx + theHdr.width, Ly)
    bgl.glTexCoord2f(1.0, 1.0)
    bgl.glVertex2f(Lx + theHdr.width, Ly + theHdr.height)
    bgl.glTexCoord2f(0.0, 1.0)
    bgl.glVertex2f(Lx, theHdr.height + Ly)
    bgl.glEnd()
    bgl.glDisable(bgl.GL_TEXTURE_2D)

    # ---------------------
    # draw the crosshair
    # ---------------------
    x = theHdr.width / 2.0
    if crossChange and not Hdr.lockCrosshair:
        Sun.SP.HDR_azimuth = degToRad(newAzimuth + 180)
    azimuth = ((radToDeg(Sun.SP.HDR_azimuth) - 180) * x / 180.0) + x

    bgl.glEnable(bgl.GL_BLEND)
    bgl.glEnable(bgl.GL_LINES)
    bgl.glLineWidth(1.0)
    alpha = 0.8
    color = (0.4, 0.4, 0.4, alpha)
    bgl.glColor4f(color[0], color[1], color[2], color[3])
    bgl.glBegin(bgl.GL_LINES)
    bgl.glVertex2f(Lx + azimuth, Ly)
    bgl.glVertex2f(Lx + azimuth, Ly + theHdr.height)
    bgl.glEnd()

    y = theHdr.height / 2.0
    if crossChange and not Hdr.lockCrosshair:
        Sun.SP.HDR_elevation = newElevation
    elevation = (Sun.SP.HDR_elevation * y / 90.0) + y

    bgl.glColor4f(color[0], color[1], color[2], color[3])
    bgl.glBegin(bgl.GL_LINES)
    bgl.glVertex2f(Lx, Ly + elevation)
    bgl.glVertex2f(Lx + theHdr.width, Ly + elevation)
    bgl.glEnd()

    # ---------------------
    # draw the border
    # ---------------------
    bgl.glDisable(bgl.GL_BLEND)
    color = (0.6, 0.6, .6, 1.0)
    bgl.glColor4f(color[0], color[1], color[2], color[3])
    bgl.glBegin(bgl.GL_LINE_LOOP)
    bgl.glVertex2f(Lx, Ly)
    bgl.glVertex2f(Lx + theHdr.width, Ly)
    bgl.glVertex2f(Lx + theHdr.width, Ly + theHdr.height)
    bgl.glVertex2f(Lx, theHdr.height + Ly)
    bgl.glVertex2f(Lx, Ly)
    bgl.glEnd()

    bgl.glLineWidth(1.0)
    bgl.glDisable(bgl.GL_LINES)
    bgl.glFlush()
Esempio n. 27
0
def render_opengl(self, context):
    from math import ceil

    layers = []
    scene = context.scene
    for x in range(0, 20):
        if scene.layers[x] is True:
            layers.extend([x])

    objlist = context.scene.objects
    render_scale = scene.render.resolution_percentage / 100

    width = int(scene.render.resolution_x * render_scale)
    height = int(scene.render.resolution_y * render_scale)

    # I cant use file_format becuase the pdf writer needs jpg format
    # the file_format returns 'JPEG' not 'JPG'
    #     file_format = context.scene.render.image_settings.file_format.lower()
    ren_path = bpy.path.abspath(bpy.context.scene.render.filepath) + ".jpg"

    #     if len(ren_path) > 0:
    #         if ren_path.endswith(os.path.sep):
    #             initpath = os.path.realpath(ren_path) + os.path.sep
    #         else:
    #             (initpath, filename) = os.path.split(ren_path)
    #         outpath = os.path.join(initpath, "ogl_tmp.png")
    #     else:
    #         self.report({'ERROR'}, "Invalid render path")
    #         return False

    img = get_render_image(ren_path)

    if img is None:
        self.report({'ERROR'}, "Invalid render path:" + ren_path)
        return False

    tile_x = 240
    tile_y = 216
    row_num = ceil(height / tile_y)
    col_num = ceil(width / tile_x)

    cut4 = (col_num * tile_x * 4) - width * 4
    totpixel4 = width * height * 4

    viewport_info = bgl.Buffer(bgl.GL_INT, 4)
    bgl.glGetIntegerv(bgl.GL_VIEWPORT, viewport_info)

    img.gl_load(0, bgl.GL_NEAREST, bgl.GL_NEAREST)

    # 2.77 API change
    if bpy.app.version >= (2, 77, 0):
        tex = img.bindcode[0]
    else:
        tex = img.bindcode

    if context.scene.name in bpy.data.images:
        old_img = bpy.data.images[context.scene.name]
        old_img.user_clear()
        bpy.data.images.remove(old_img)

    img_result = bpy.data.images.new(context.scene.name, width, height)

    tmp_pixels = [1] * totpixel4

    #---------- Loop for all tiles
    for row in range(0, row_num):
        for col in range(0, col_num):
            buffer = bgl.Buffer(bgl.GL_FLOAT, width * height * 4)
            bgl.glDisable(
                bgl.GL_SCISSOR_TEST
            )  # if remove this line, get blender screenshot not image
            bgl.glViewport(0, 0, tile_x, tile_y)

            bgl.glMatrixMode(bgl.GL_PROJECTION)
            bgl.glLoadIdentity()

            # defines ortographic view for single tile
            x1 = tile_x * col
            y1 = tile_y * row
            bgl.gluOrtho2D(x1, x1 + tile_x, y1, y1 + tile_y)

            # Clear
            bgl.glClearColor(0.0, 0.0, 0.0, 0.0)
            bgl.glClear(bgl.GL_COLOR_BUFFER_BIT | bgl.GL_DEPTH_BUFFER_BIT)

            bgl.glEnable(bgl.GL_TEXTURE_2D)
            bgl.glBindTexture(bgl.GL_TEXTURE_2D, tex)

            # defines drawing area
            bgl.glBegin(bgl.GL_QUADS)

            bgl.glColor3f(1.0, 1.0, 1.0)
            bgl.glTexCoord2f(0.0, 0.0)
            bgl.glVertex2f(0.0, 0.0)

            bgl.glTexCoord2f(1.0, 0.0)
            bgl.glVertex2f(width, 0.0)

            bgl.glTexCoord2f(1.0, 1.0)
            bgl.glVertex2f(width, height)

            bgl.glTexCoord2f(0.0, 1.0)
            bgl.glVertex2f(0.0, height)

            bgl.glEnd()

            for obj in objlist:
                if obj.mv.type == 'VISDIM_A':
                    for x in range(0, 20):
                        if obj.layers[x] is True:
                            if x in layers:
                                opengl_dim = obj.mv.opengl_dim
                                if not opengl_dim.hide:
                                    draw_dimensions(context, obj, opengl_dim,
                                                    None, None)
                            break

            #---------- copy pixels to temporary area
            bgl.glFinish()
            bgl.glReadPixels(0, 0, width, height, bgl.GL_RGBA, bgl.GL_FLOAT,
                             buffer)  # read image data
            for y in range(0, tile_y):
                # final image pixels position
                p1 = (y * width * 4) + (row * tile_y * width *
                                        4) + (col * tile_x * 4)
                p2 = p1 + (tile_x * 4)
                # buffer pixels position
                b1 = y * width * 4
                b2 = b1 + (tile_x * 4)

                if p1 < totpixel4:  # avoid pixel row out of area
                    if col == col_num - 1:  # avoid pixel columns out of area
                        p2 -= cut4
                        b2 -= cut4

                    tmp_pixels[p1:p2] = buffer[b1:b2]

    img_result.pixels = tmp_pixels[:]
    img.gl_free()

    img.user_clear()
    bpy.data.images.remove(img)
    os.remove(ren_path)
    bgl.glEnable(bgl.GL_SCISSOR_TEST)

    #---------- restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)

    if img_result is not None:
        return img_result
Esempio n. 28
0
def draw_callback(self, context, opt={}):
    center = opt.get('center') or get_center_pos(context)
    zoom = opt.get('zoom') or get_zoom(context)
    back_image_alpha = opt.get('back_image_alpha') or .5
    image_size = opt.get('image_size') or (0,0)
    width,height = image_size

    img = State.img_bake_target
    if img and width*height != 0:
        if not img.bindcode[0]:
            img.gl_load(0, bgl.GL_NEAREST, bgl.GL_NEAREST)
        else:
            img.gl_touch(0)

        bgl.glEnable(bgl.GL_BLEND)
        bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
        bgl.glBlendEquation(bgl.GL_FUNC_ADD)

        bgl.glEnable(bgl.GL_TEXTURE_2D)
        bgl.glColor4f(1,1,1,back_image_alpha)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, img.bindcode[0])

        bgl.glBegin(bgl.GL_QUADS)
        dw = Vector((width,0)) / 2
        dh = Vector((0,height)) / 2
        ps = [center+(-dw-dh)*zoom
             ,center+(dw-dh)*zoom
             ,center+(dw+dh)*zoom
             ,center+(-dw+dh)*zoom
             ]
        ts = [(0,0),(1,0),(1,1),(0,1)]
        for p,t in zip(ps,ts):
            bgl.glTexCoord2f(*t)
            bgl.glVertex2f(*p)
        bgl.glEnd()
        bgl.glDisable(bgl.GL_TEXTURE_2D)

    ##
    #bgl.glEnable(bgl.GL_BLEND)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    bgl.glBlendEquation(bgl.GL_FUNC_ADD)
    for line in State.lines:
        bgl.glColor4f(*(*line.color,1.0))

        ps = line.seq.all()
        if len(ps)<2:
            continue

        ka,kb = ps[0:2]
        a = Vector((ka.x,ka.y))
        b = Vector((kb.x,kb.y))
        t = (b-a).normalized()
        n = Vector((t.y, -t.x))
        prev_u = a+n*ka.radius
        prev_v = a-n*ka.radius

        for ka,kb in zip(ps[:-1], ps[1:]):
            a = Vector((ka.x,ka.y))
            b = Vector((kb.x,kb.y))
            t = (b-a).normalized()
            n = Vector((t.y, -t.x))
            u = b+n*kb.radius
            v = b-n*kb.radius
            bgl.glBegin(bgl.GL_QUADS)
            for p in [prev_u, u, v, prev_v]:
                bgl.glVertex2f(*(center+p*zoom))
            bgl.glEnd()
            prev_u = u
            prev_v = v

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
    bgl.glBlendEquation(bgl.GL_FUNC_ADD)
Esempio n. 29
0
def Draw_map_callback(self, context):

    if context.area != Map.view3d_area:
        return
    elif context.area.type == 'PROPERTIES' and \
        context.space_data.context != 'WORLD':
        return

    # Check if window area has changed for sticky zoom
    theMap = Map.object[0]
    if Map.region.width < Map.saved_region_width:
        diff = Map.saved_region_width - Map.region.width
        if theMap.origin.x + theMap.width > Map.saved_region_width:
            if theMap.origin.x > 0:
                theMap.origin.x -= diff
            else:
                theMap.width -= diff
        else:
            if Map.toolProps is not None:
                if Map.toolProps.width > Map.toolProps_width:
                    theMap.origin.x -= diff
                Map.toolProps_width = Map.toolProps.width
            if theMap.origin.x < 0:
                theMap.origin.x += diff
    else:
        diff = Map.region.width - Map.saved_region_width
        if theMap.width > Map.saved_region_width:
            theMap.width += diff
        else:
            if Map.toolProps is not None:
                if Map.toolProps.width < Map.toolProps_width:
                    theMap.origin.x += diff
                Map.toolProps_width = Map.toolProps.width
    theMap.set_dimensions(theMap.width)
    Map.saved_region_width = Map.region.width

    # Latitude and longitude are set to an equidistant
    # cylindrical projection with lat/long 0/0 exactly
    # in the middle of the image.

    zLong = theMap.width / 2
    longFac = zLong / 180
    zLat = theMap.height / 2
    latFac = zLat / 90
    crossChange = True

    if not Map.action == 'PAN':
        x = Map.mouse.x
        y = Map.mouse.y
        if x < theMap.origin.x or x > theMap.origin.x + theMap.width:
            crossChange = False
            x = 0
        else:
            testBoundary = theMap.origin.x + theMap.width
            if testBoundary < Map.region.width:
                rightBoundary = testBoundary
            else:
                rightBoundary = Map.region.width
            if x > rightBoundary:
                crossChange = False
                x = rightBoundary
        cX = x - zLong - theMap.origin.x

        if longFac:
            newLongitude = cX / longFac
        else:
            newLongitude = 0.0

        if y < theMap.origin.y or y < 0:
            crossChange = False
            y = 0
        elif y > theMap.origin.y + theMap.height:
            crossChange = False
            y = theMap.origin.y + theMap.height
        cY = y - zLat - theMap.origin.y

        if latFac:
            newLatitude = cY / latFac
        else:
            newLatitude = 0.0

        if newLatitude == Map.latitude and newLongitude == Map.longitude:
            crossChange = False
        else:
            Map.latitude = newLatitude
            Map.longitude = newLongitude
    else:
        if Map.grab.offset.x < Map.grab.spot.x:
            off = Map.grab.spot.x - Map.grab.offset.x
            theMap.origin.x -= off
        else:
            off = Map.grab.offset.x - Map.grab.spot.x
            theMap.origin.x += off
        if Map.grab.offset.y < Map.grab.spot.y:
            off = Map.grab.spot.y - Map.grab.offset.y
            theMap.origin.y -= off
        else:
            off = Map.grab.offset.y - Map.grab.spot.y
            theMap.origin.y += off
        Map.grab.spot.x = Map.mouse.x
        Map.grab.spot.y = Map.mouse.y

    Lx = theMap.origin.x
    Ly = theMap.origin.y

    # ---------------------
    # Draw a textured quad
    # ---------------------

    if not Map.textureless:
        bgl.glEnable(bgl.GL_BLEND)
        if Map.glImage.bindcode == 0:
            Map.load_gl_image()
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, Map.glImage.bindcode)
        bgl.glEnable(bgl.GL_TEXTURE_2D)
        bgl.glColor4f(1.0, 1.0, 1.0, Map.object[0].opacity)
        bgl.glBegin(bgl.GL_QUADS)
        bgl.glTexCoord2f(0.0, 0.0)
        bgl.glVertex2f(Lx, Ly)
        bgl.glTexCoord2f(1.0, 0.0)
        bgl.glVertex2f(Lx + theMap.width, Ly)
        bgl.glTexCoord2f(1.0, 1.0)
        bgl.glVertex2f(Lx + theMap.width, Ly + theMap.height)
        bgl.glTexCoord2f(0.0, 1.0)
        bgl.glVertex2f(Lx, theMap.height + Ly)
        bgl.glEnd()
        bgl.glDisable(bgl.GL_TEXTURE_2D)

    # -----------------------
    # Output text for stats
    # -----------------------
    if Map.action == 'TIME' or Map.action == 'DAY' or Map.showInfo:
        Map.show_text_in_viewport(Map.object[1])

    # ---------------------
    # draw the crosshair
    # ---------------------
    x = theMap.width / 2.0
    if crossChange and not Map.lockCrosshair:
        if Map.action != 'Y':
            Sun.SP.Longitude = newLongitude
    longitude = (Sun.SP.Longitude * x / 180.0) + x

    bgl.glEnable(bgl.GL_BLEND)
    bgl.glEnable(bgl.GL_LINES)
    bgl.glLineWidth(1.0)
    alpha = 1.0 if Map.action == 'Y' else 0.5
    color = (0.894, 0.741, .510, alpha)
    bgl.glColor4f(color[0], color[1], color[2], color[3])
    bgl.glBegin(bgl.GL_LINES)
    bgl.glVertex2f(Lx + longitude, Ly)
    bgl.glVertex2f(Lx + longitude, Ly + theMap.height)
    bgl.glEnd()

    y = theMap.height / 2.0
    if crossChange and not Map.lockCrosshair:
        if Map.action != 'X':
            Sun.SP.Latitude = newLatitude
    latitude = (Sun.SP.Latitude * y / 90.0) + y

    alpha = 1.0 if Map.action == 'X' else 0.5
    color = (0.894, 0.741, .510, alpha)
    bgl.glColor4f(color[0], color[1], color[2], color[3])
    bgl.glBegin(bgl.GL_LINES)
    bgl.glVertex2f(Lx, Ly + latitude)
    bgl.glVertex2f(Lx + theMap.width, Ly + latitude)
    bgl.glEnd()

    # ---------------------
    # draw the border
    # ---------------------
    bgl.glDisable(bgl.GL_BLEND)
    color = (0.6, 0.6, .6, 1.0)
    bgl.glColor4f(color[0], color[1], color[2], color[3])
    bgl.glBegin(bgl.GL_LINE_LOOP)
    bgl.glVertex2f(Lx, Ly)
    bgl.glVertex2f(Lx + theMap.width, Ly)
    bgl.glVertex2f(Lx + theMap.width, Ly + theMap.height)
    bgl.glVertex2f(Lx, theMap.height + Ly)
    bgl.glVertex2f(Lx, Ly)
    bgl.glEnd()

    if not Sun.ShowRiseSet or not Map.lineWidth:
        bgl.glDisable(bgl.GL_LINES)
        bgl.glFlush()
        return

    if Map.action == 'G':
        draw_text_region()

    # ------------------------
    # draw the sunrise, sunset
    # ------------------------

    def draw_angled_line(color, angle, bx, by):
        x = math.cos(angle) * radius
        y = math.sin(angle) * radius
        bgl.glColor4f(color[0], color[1], color[2], color[3])
        bgl.glBegin(bgl.GL_LINES)
        bgl.glVertex2f(bx, by)
        bgl.glVertex2f(bx + x, by + y)
        bgl.glEnd()

    px = Lx + longitude
    py = Ly + latitude

    radius = 30 + Map.lineWidth * 10
    if Sun.RiseSetOK and Map.lineWidth:
        color = (0.2, 0.6, 1.0, 0.9)
        angle = -(degToRad(Sun.Sunrise.azimuth) - math.pi / 2)
        bgl.glLineWidth(Map.lineWidth)
        draw_angled_line(color, angle, px, py)

        color = (0.86, 0.18, 0.18, 0.9)
        angle = -(degToRad(Sun.Sunset.azimuth) - math.pi / 2)
        draw_angled_line(color, angle, px, py)

    # ------------------------
    # draw current time line
    # ------------------------

    if Map.textureless:
        phi = degToRad(Sun.AzNorth) * -1
    else:
        phi = degToRad(Sun.Azimuth) * -1
    x = math.sin(phi) * math.sin(-Sun.Theta) * (radius + 10)
    y = math.sin(Sun.Theta) * math.cos(phi) * (radius + 10)
    night = (0.24, 0.29, 0.94, 0.9)
    day = (0.85, 0.77, 0.60, 0.9)
    if Sun.SolarNoon.elevation < 0.0:
        color = night
    elif Sun.Elevation >= Sun.Sunrise.elevation:
        if Sun.Time >= Sun.Sunset.time and \
            Sun.Elevation <= Sun.Sunset.elevation:
            color = night
        else:
            color = day
    else:
        color = night
    bgl.glLineWidth(Map.lineWidth + 1.0)

    bgl.glColor4f(color[0], color[1], color[2], color[3])
    bgl.glBegin(bgl.GL_LINES)
    bgl.glVertex2f(px, py)
    bgl.glVertex2f(px + x, py + y)
    bgl.glEnd()
    bgl.glLineWidth(1.0)
    bgl.glDisable(bgl.GL_LINES)
    bgl.glFlush()
Esempio n. 30
0
def draw_preview_callback(self):

    # Search for View_3d window
    area = None
    if bpy.context.area.type != 'VIEW_3D':
        return bpy.context.area
    else:
        for oWindow in bpy.context.window_manager.windows:
            oScreen = oWindow.screen
            for oArea in oScreen.areas:
                if oArea.type == 'VIEW_3D':
                    area = oArea

    modelsPosesColl = bUtils.getPhobosPreferences().models_poses
    activeModelPoseIndex = bpy.context.scene.active_ModelPose

    if (len(modelsPosesColl) > 0) and area:

        # Draw a textured quad
        area_widths = [
            region.width for region in bpy.context.area.regions
            if region.type == 'WINDOW'
        ]
        area_heights = [
            region.height for region in bpy.context.area.regions
            if region.type == 'WINDOW'
        ]
        if (len(area_widths) > 0) and (len(area_heights) > 0):

            active_preview = modelsPosesColl[
                bpy.data.images[activeModelPoseIndex].name]
            im = bpy.data.images[activeModelPoseIndex]

            view_width = area_widths[0]
            view_height = area_heights[0]
            tex_start_x = 50
            tex_end_x = view_width - 50
            tex_start_y = 50
            tex_end_y = view_height - 50
            if im.size[0] < view_width:
                diff = int((view_width - im.size[0]) / 2)
                tex_start_x = diff
                tex_end_x = diff + im.size[0]
            if im.size[1] < view_height:
                diff = int((view_height - im.size[1]) / 2)
                tex_start_y = diff
                tex_end_y = diff + im.size[1]

            # Draw information
            font_id = 0  # XXX, need to find out how best to get this.
            blf.position(font_id, tex_start_x, tex_end_y + 20, 0)
            blf.size(font_id, 20, 72)
            blf.draw(font_id, active_preview.label)

            tex = im.bindcode
            bgl.glEnable(bgl.GL_TEXTURE_2D)
            # if using blender 2.77 change tex to tex[0]
            bgl.glBindTexture(bgl.GL_TEXTURE_2D, tex)

            # Background
            bgl.glEnable(bgl.GL_BLEND)

            bgl.glBegin(bgl.GL_QUADS)
            bgl.glColor4f(0, 0, 0, 0.3)
            bgl.glVertex2i(0, 0)
            bgl.glVertex2i(0, view_height)
            bgl.glVertex2i(view_width, view_height)
            bgl.glVertex2i(view_width, 0)

            # Draw Image
            bgl.glColor4f(1, 1, 1, 1)
            bgl.glTexCoord2f(0, 0)
            bgl.glVertex2i(int(tex_start_x), int(tex_start_y))
            bgl.glTexCoord2f(0, 1)
            bgl.glVertex2i(int(tex_start_x), int(tex_end_y))
            bgl.glTexCoord2f(1, 1)
            bgl.glVertex2i(int(tex_end_x), int(tex_end_y))
            bgl.glTexCoord2f(1, 0)
            bgl.glVertex2i(int(tex_end_x), int(tex_start_y))
            bgl.glEnd()

            # restore opengl defaults
            bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
            bgl.glDisable(bgl.GL_QUADS)
            bgl.glDisable(bgl.GL_BLEND)
            bgl.glDisable(bgl.GL_TEXTURE_2D)
Esempio n. 31
0
def _draw_3dview_report(region):
    """Draws reports in 3d views.

    :param region: region of 3D viewport
    :type region: bpy.types.Region
    """
    pos = region.height - 62

    if _Show3DViewReportOperator.has_lines():

        glEnable(GL_TEXTURE_2D)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glBindTexture(GL_TEXTURE_2D, _Show3DViewReportOperator.get_scs_logo_img_bindcode())

        # draw BT logo
        glBegin(GL_POLYGON)
        glColor3f(1, 1, 1)
        glTexCoord2f(0, 1)
        glVertex3f(_OP_consts.View3DReport.BT_LOGO_AREA[0], region.height - _OP_consts.View3DReport.BT_LOGO_AREA[2], 0)
        glTexCoord2f(1, 1)
        glVertex3f(_OP_consts.View3DReport.BT_LOGO_AREA[1], region.height - _OP_consts.View3DReport.BT_LOGO_AREA[2], 0)
        glTexCoord2f(1, 0)
        glVertex3f(_OP_consts.View3DReport.BT_LOGO_AREA[1], region.height - _OP_consts.View3DReport.BT_LOGO_AREA[3], 0)
        glTexCoord2f(0, 0)
        glVertex3f(_OP_consts.View3DReport.BT_LOGO_AREA[0], region.height - _OP_consts.View3DReport.BT_LOGO_AREA[3], 0)
        glEnd()

        glDisable(GL_TEXTURE_2D)

        # draw version string
        blf.size(0, 10, 72)
        glColor3f(.952, .635, .062)
        blf.position(0, 20, pos, 0)
        blf.draw(0, _info_utils.get_combined_ver_str(only_version_numbers=True))
        pos -= 20

        # draw actual operator title and message if shown
        if _Show3DViewReportOperator.is_shown():

            blf.size(0, 12, 72)
            glColor3f(1, 1, 1)
            blf.shadow(0, 5, 0, 0, 0, 1)

            if _Show3DViewReportOperator.get_title() != "":
                blf.position(0, 20, pos, 0)
                blf.draw(0, _Show3DViewReportOperator.get_title())
                pos -= 15

            blf.enable(0, blf.SHADOW)
            for line in _Show3DViewReportOperator.get_lines():

                # finish printing if running out of space
                if pos - 60 < 0:
                    blf.position(0, 20, pos, 0)
                    blf.draw(0, "...")
                    break

                blf.position(0, 20, pos, 0)
                if "ERROR" in line:
                    blf.shadow(0, 5, 0.5, 0., 0, 1)
                elif "WARNING" in line:
                    blf.shadow(0, 5, 0.3, 0.15, 0, 1)

                blf.draw(0, line)
                pos -= 15

            blf.disable(0, blf.SHADOW)

        # draw control buttons if controls are enabled
        if _Show3DViewReportOperator.has_controls():

            # draw close button
            glColor3f(.4, .4, .4)
            glBegin(GL_POLYGON)
            glVertex3f(_OP_consts.View3DReport.CLOSE_BTN_AREA[0], region.height - _OP_consts.View3DReport.CLOSE_BTN_AREA[2], 0)
            glVertex3f(_OP_consts.View3DReport.CLOSE_BTN_AREA[1], region.height - _OP_consts.View3DReport.CLOSE_BTN_AREA[2], 0)
            glVertex3f(_OP_consts.View3DReport.CLOSE_BTN_AREA[1], region.height - _OP_consts.View3DReport.CLOSE_BTN_AREA[3], 0)
            glVertex3f(_OP_consts.View3DReport.CLOSE_BTN_AREA[0], region.height - _OP_consts.View3DReport.CLOSE_BTN_AREA[3], 0)
            glEnd()

            # draw hide button
            glBegin(GL_POLYGON)
            glVertex3f(_OP_consts.View3DReport.HIDE_BTN_AREA[0], region.height - _OP_consts.View3DReport.HIDE_BTN_AREA[2], 0)
            glVertex3f(_OP_consts.View3DReport.HIDE_BTN_AREA[1], region.height - _OP_consts.View3DReport.HIDE_BTN_AREA[2], 0)
            glVertex3f(_OP_consts.View3DReport.HIDE_BTN_AREA[1], region.height - _OP_consts.View3DReport.HIDE_BTN_AREA[3], 0)
            glVertex3f(_OP_consts.View3DReport.HIDE_BTN_AREA[0], region.height - _OP_consts.View3DReport.HIDE_BTN_AREA[3], 0)
            glEnd()

            # gather texts and positions
            close_btn_text_pos = _OP_consts.View3DReport.CLOSE_BTN_TEXT_POS[int(not _Show3DViewReportOperator.is_shown())]
            close_btn_text = _OP_consts.View3DReport.CLOSE_BTN_TEXT[int(not _Show3DViewReportOperator.is_shown())]

            hide_btn_text_pos = _OP_consts.View3DReport.HIDE_BTN_TEXT_POS[int(not _Show3DViewReportOperator.is_shown())]
            hide_btn_text = _OP_consts.View3DReport.HIDE_BTN_TEXT[int(not _Show3DViewReportOperator.is_shown())]

            blf.size(0, 15, 72)

            # draw close button text
            glColor3f(1, 1, 1)
            blf.position(0, close_btn_text_pos[0], region.height - close_btn_text_pos[1], 0)
            blf.draw(0, close_btn_text)

            # draw hide button text
            blf.position(0, hide_btn_text_pos[0], region.height - hide_btn_text_pos[1], 0)
            blf.draw(0, hide_btn_text)
Esempio n. 32
0
def draw_callback_mode(self, context):
    settings = context.scene.OASettings
    if self.ctrl and not settings.shift:
        mode_title(context, "Replace")
    bgl.glLineWidth(1)

    tool_shelf_width = get_tool_shelf_width(bpy.context)
    # add the offset-width and offset-height for the first time
    if self.menu_offset['first_iteration']:
        for icon in self.menu:
            # iterate over icon, frame and hover
            for i in (1, 2, 3):
                # iterate over lower left and upper right corner
                for j in (0, 2):
                    icon[i][0 + j] += tool_shelf_width
                    icon[i][1 + j] += bpy.context.region.height
        self.menu_offset['first_iteration'] = False

    # add current offset-region-height and offset-tool-shelf-width
    # when the user altered the size of the 3d-view or toggled region_overlap
    elif any((
            self.menu_offset['width'] != tool_shelf_width,
            self.menu_offset['height'] != bpy.context.region.height,
            self.menu_offset['region_overlap'] != bool(
                bpy.context.user_preferences.system.use_region_overlap),
    )):
        for icon in self.menu:
            # iterate over icon, frame and hover
            for i in (1, 2, 3):
                # iterate over lower left and upper right corner
                for j in (0, 2):
                    icon[i][0 + j] += (tool_shelf_width -
                                       self.menu_offset['width'])
                    icon[i][1 + j] += (bpy.context.region.height -
                                       self.menu_offset['height'])

        self.menu_offset['width'] = tool_shelf_width
        self.menu_offset['height'] = bpy.context.region.height
        self.menu_offset['region_overlap'] = bool(
            bpy.context.user_preferences.system.use_region_overlap)

    # draw frame
    bgl.glColor3f(0.1, 0.1, 0.1)
    for icon in self.menu:
        bgl.glRecti(icon[2][0], icon[2][1], icon[2][2], icon[2][3])

    # draw icons
    if settings.valid_icon_file:
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.img.bindcode)
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER,
                            bgl.GL_NEAREST)
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER,
                            bgl.GL_NEAREST)

        for icon in self.menu:
            bgl.glEnable(bgl.GL_TEXTURE_2D)
            bgl.glTexEnvf(bgl.GL_TEXTURE_ENV, bgl.GL_TEXTURE_ENV_MODE,
                          bgl.GL_REPLACE)

            bgl.glBegin(bgl.GL_QUADS)

            bgl.glTexCoord2f(icon[4][0][0], icon[4][0][1])
            bgl.glVertex2f(icon[1][0], icon[1][1])

            bgl.glTexCoord2f(icon[4][1][0], icon[4][1][1])
            bgl.glVertex2f(icon[1][0], icon[1][3])

            bgl.glTexCoord2f(icon[4][2][0], icon[4][2][1])
            bgl.glVertex2f(icon[1][2], icon[1][3])

            bgl.glTexCoord2f(icon[4][3][0], icon[4][3][1])
            bgl.glVertex2f(icon[1][2], icon[1][1])

            bgl.glEnd()

            bgl.glDisable(bgl.GL_TEXTURE_2D)

    else:
        # draw category and model_id if no icon file is provided
        # draw background
        bgl.glColor3f(0.2, 0.2, 0.2)
        for icon in self.menu:
            bgl.glRecti(icon[2][0] + 2, icon[2][1] + 2, icon[2][2] - 2,
                        icon[2][3] - 2)

        font_id = 0
        bgl.glColor4f(1, 1, 1, 1)
        for icon in self.menu:
            blf.position(font_id, icon[1][0] + 1, icon[1][1] + 8, 0)
            blf.size(font_id, int(settings.menu_icon_display_size / 3), 72)
            blf.draw(
                font_id, "{0: >2}".format(icon[0][1]) + "," +
                "{0: >2}".format(icon[0][2]))

    # draw hover effekt
    for icon in self.menu:
        # mouse hover icon
        if mouse_over_icon(icon[1], self.mouse):
            bgl.glColor3f(0.6, 0.6, 0.6)
            bgl.glLineWidth(2)
            rect_round_corners(icon[3][0], icon[3][1], icon[3][2], icon[3][3])

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
Esempio n. 33
0
def draw(icon, x, y, size, alpha=1.0, round_radius=0.0):
    """iconを描画
    :param icon: iconを示す文字列か番号。単色で塗りつぶす場合はRGBAを指定する
    :type icon: str | int | list | tuple
    :param x: 左下座標
    :type x: int
    :param y: 左下座標
    :type y: int
    :type size: int
    :type alpha: int | float
    :param round_radius: 角の丸め半径。size/2で円になる
    :type round_radius: int | float
    """

    if isinstance(icon, (list, tuple)):
        return draw_fill(icon, x, y, size, alpha, round_radius)

    if isinstance(icon, int):
        for name, value in icons.items():
            if value == icon:
                icon = name
                break
        else:
            return False

    if not icon or icon == 'NONE':
        return False
    else:
        tex_data = get_texture(icon)
    if not tex_data:
        print('icon not found', icon)
        return False

    if isinstance(tex_data, types.FunctionType):  # internal_icons用
        tex_data(x, y, size, size, alpha)
        return True

    texture, buf, sx, sy, xmin, ymin, w, h = tex_data
    round_radius = min(size / 2, round_radius)

    if round_radius != 0.0:
        r = round_radius
        pi = math.pi
        a = r
        b = size - r
        coords = []  # -size*0.5 ~ +size*0.5 の範囲
        # 左下
        coords += vagl.draw_arc_get_vectors(a, a, r, pi, pi * 3 / 2, 4)
        # 右下
        coords += vagl.draw_arc_get_vectors(b, a, r, pi * 3 / 2, 0.0, 4)
        # 右上
        coords += vagl.draw_arc_get_vectors(b, b, r, 0.0, pi / 2, 4)
        # 左上
        coords += vagl.draw_arc_get_vectors(a, b, r, pi / 2, pi, 4)
    else:
        coords = [[0, 0], [size, 0], [size, size], [0, size]]

    with vagl.GLSettings.push_attrib(bgl.GL_ENABLE_BIT | bgl.GL_TEXTURE_BIT):
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glEnable(bgl.GL_TEXTURE_2D)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture)

        bgl.glColor4f(1.0, 1.0, 1.0, alpha)

        bgl.glBegin(bgl.GL_POLYGON)
        for co in coords:
            fx = co[0] / size
            fy = co[1] / size
            tx = xmin / sx + max(w, h) / sx * fx
            ty = ymin / sy + max(w, h) / sy * fy
            bgl.glTexCoord2f(tx, ty)
            bgl.glVertex2f(x + co[0], y + co[1])
        bgl.glEnd()

    return True
Esempio n. 34
0
def draw(icon, x, y, size, alpha=1.0, round_radius=0.0):
    """iconを描画
    :param icon: iconを示す文字列か番号。単色で塗りつぶす場合はRGBAを指定する
    :type icon: str | int | list | tuple
    :param x: 左下座標
    :type x: int
    :param y: 左下座標
    :type y: int
    :type size: int
    :type alpha: int | float
    :param round_radius: 角の丸め半径。size/2で円になる
    :type round_radius: int | float
    """

    if isinstance(icon, (list, tuple)):
        return draw_fill(icon, x, y, size, alpha, round_radius)

    if isinstance(icon, int):
        for name, value in icons.items():
            if value == icon:
                icon = name
                break
        else:
            return False

    if not icon or icon == 'NONE':
        return False
    else:
        tex_data = get_texture(icon)
    if not tex_data:
        print('icon not found', icon)
        return False

    if isinstance(tex_data, types.FunctionType):  # internal_icons用
        tex_data(x, y, size, size, alpha)
        return True

    texture, buf, sx, sy, xmin, ymin, w, h = tex_data
    round_radius = min(size / 2, round_radius)

    if round_radius != 0.0:
        r = round_radius
        pi = math.pi
        a = r
        b = size - r
        coords = []  # -size*0.5 ~ +size*0.5 の範囲
        # 左下
        coords += vagl.draw_arc_get_vectors(a, a, r, pi, pi * 3 / 2, 4)
        # 右下
        coords += vagl.draw_arc_get_vectors(b, a, r, pi * 3 / 2, 0.0, 4)
        # 右上
        coords += vagl.draw_arc_get_vectors(b, b, r, 0.0, pi / 2, 4)
        # 左上
        coords += vagl.draw_arc_get_vectors(a, b, r, pi / 2, pi, 4)
    else:
        coords = [[0, 0], [size, 0], [size, size], [0, size]]

    with vagl.GLSettings.push_attrib(bgl.GL_ENABLE_BIT | bgl.GL_TEXTURE_BIT):
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glEnable(bgl.GL_TEXTURE_2D)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture)

        bgl.glColor4f(1.0, 1.0, 1.0, alpha)

        bgl.glBegin(bgl.GL_POLYGON)
        for co in coords:
            fx = co[0] / size
            fy = co[1] / size
            tx = xmin / sx + max(w, h) / sx * fx
            ty = ymin / sy + max(w, h) / sy * fy
            bgl.glTexCoord2f(tx, ty)
            bgl.glVertex2f(x + co[0], y + co[1])
        bgl.glEnd()

    return True
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()
Esempio n. 36
0
def render_main(self, context, animation=False):
    # noinspection PyBroadException,PyBroadException
    try:
        # Get visible layers
        layers = []
        scene = context.scene
        for x in range(0, 20):
            if scene.layers[x] is True:
                layers.extend([x])

        # Get object list
        objlist = context.scene.objects
        # --------------------
        # Get resolution
        # --------------------
        scene = bpy.context.scene
        render_scale = scene.render.resolution_percentage / 100

        width = int(scene.render.resolution_x * render_scale)
        height = int(scene.render.resolution_y * render_scale)

        # ---------------------------------------
        # Get output path
        # ---------------------------------------
        ren_path = bpy.context.scene.render.filepath
        if len(ren_path) > 0:
            if ren_path.endswith(os.path.sep):
                initpath = os.path.realpath(ren_path) + os.path.sep
            else:
                (initpath, filename) = os.path.split(ren_path)
            outpath = os.path.join(initpath, "measureit_tmp_render.png")
        else:
            self.report({'ERROR'},
                        "MeasureIt: Unable to save temporary render image. Define a valid render path")
            return False

        # Get Render Image
        img = get_render_image(outpath)
        if img is None:
            self.report({'ERROR'},
                        "MeasureIt: Unable to save temporary render image. Define a valid render path")
            return False

        # -----------------------------
        # Calculate rows and columns
        # -----------------------------
        tile_x = 240
        tile_y = 216
        row_num = ceil(height / tile_y)
        col_num = ceil(width / tile_x)
        print("MeasureIt: Image divided in " + str(row_num) + "x" + str(col_num) + " tiles")

        # pixels out of visible area
        cut4 = (col_num * tile_x * 4) - width * 4  # pixels aout of drawing area
        totpixel4 = width * height * 4  # total pixels RGBA

        viewport_info = bgl.Buffer(bgl.GL_INT, 4)
        bgl.glGetIntegerv(bgl.GL_VIEWPORT, viewport_info)

        # Load image on memory
        img.gl_load(0, bgl.GL_NEAREST, bgl.GL_NEAREST)
        tex = img.bindcode

        # --------------------------------------------
        # Create output image (to apply texture)
        # --------------------------------------------
        if "measureit_output" in bpy.data.images:
            out_img = bpy.data.images["measureit_output"]
            if out_img is not None:
                out_img.user_clear()
                bpy.data.images.remove(out_img)

        out = bpy.data.images.new("measureit_output", width, height)
        tmp_pixels = [1] * totpixel4

        # --------------------------------
        # Loop for all tiles
        # --------------------------------
        for row in range(0, row_num):
            for col in range(0, col_num):
                buffer = bgl.Buffer(bgl.GL_FLOAT, width * height * 4)
                bgl.glDisable(bgl.GL_SCISSOR_TEST)  # if remove this line, get blender screenshot not image
                bgl.glViewport(0, 0, tile_x, tile_y)

                bgl.glMatrixMode(bgl.GL_PROJECTION)
                bgl.glLoadIdentity()

                # defines ortographic view for single tile
                x1 = tile_x * col
                y1 = tile_y * row
                bgl.gluOrtho2D(x1, x1 + tile_x, y1, y1 + tile_y)

                # Clear
                bgl.glClearColor(0.0, 0.0, 0.0, 0.0)
                bgl.glClear(bgl.GL_COLOR_BUFFER_BIT | bgl.GL_DEPTH_BUFFER_BIT)

                bgl.glEnable(bgl.GL_TEXTURE_2D)
                bgl.glBindTexture(bgl.GL_TEXTURE_2D, tex)

                # defines drawing area
                bgl.glBegin(bgl.GL_QUADS)

                bgl.glColor3f(1.0, 1.0, 1.0)
                bgl.glTexCoord2f(0.0, 0.0)
                bgl.glVertex2f(0.0, 0.0)

                bgl.glTexCoord2f(1.0, 0.0)
                bgl.glVertex2f(width, 0.0)

                bgl.glTexCoord2f(1.0, 1.0)
                bgl.glVertex2f(width, height)

                bgl.glTexCoord2f(0.0, 1.0)
                bgl.glVertex2f(0.0, height)

                bgl.glEnd()

                # -----------------------------
                # Loop to draw all lines
                # -----------------------------
                for myobj in objlist:
                    if myobj.hide is False:
                        if 'MeasureGenerator' in myobj:
                            # verify visible layer
                            for x in range(0, 20):
                                if myobj.layers[x] is True:
                                    if x in layers:
                                        op = myobj.MeasureGenerator[0]
                                        draw_segments(context, myobj, op, None, None)
                                    break

                if scene.measureit_rf is True:
                    bgl.glColor3f(1.0, 1.0, 1.0)
                    rfcolor = scene.measureit_rf_color
                    rfborder = scene.measureit_rf_border
                    rfline = scene.measureit_rf_line

                    bgl.glLineWidth(rfline)
                    bgl.glColor4f(rfcolor[0], rfcolor[1], rfcolor[2], rfcolor[3])

                    x1 = rfborder
                    x2 = width - rfborder
                    y1 = int(math.ceil(rfborder / (width / height)))
                    y2 = height - y1
                    draw_rectangle((x1, y1), (x2, y2))

                # --------------------------------
                # copy pixels to temporary area
                # --------------------------------
                bgl.glFinish()
                bgl.glReadPixels(0, 0, width, height, bgl.GL_RGBA, bgl.GL_FLOAT, buffer)  # read image data
                for y in range(0, tile_y):
                    # final image pixels position
                    p1 = (y * width * 4) + (row * tile_y * width * 4) + (col * tile_x * 4)
                    p2 = p1 + (tile_x * 4)
                    # buffer pixels position
                    b1 = y * width * 4
                    b2 = b1 + (tile_x * 4)

                    if p1 < totpixel4:  # avoid pixel row out of area
                        if col == col_num - 1:  # avoid pixel columns out of area
                            p2 -= cut4
                            b2 -= cut4

                        tmp_pixels[p1:p2] = buffer[b1:b2]

        # -----------------------
        # Copy temporary to final
        # -----------------------
        out.pixels = tmp_pixels[:]  # Assign image data
        img.gl_free()  # free opengl image memory

        # delete image
        img.user_clear()
        bpy.data.images.remove(img)
        # remove temp file
        os.remove(outpath)
        # reset
        bgl.glEnable(bgl.GL_SCISSOR_TEST)
        # -----------------------
        # restore opengl defaults
        # -----------------------
        bgl.glLineWidth(1)
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
        # Saves image
        if out is not None and (scene.measureit_render is True or animation is True):
            ren_path = bpy.context.scene.render.filepath
            filename = "mit_frame"
            if len(ren_path) > 0:
                if ren_path.endswith(os.path.sep):
                    initpath = os.path.realpath(ren_path) + os.path.sep
                else:
                    (initpath, filename) = os.path.split(ren_path)

            ftxt = "%04d" % scene.frame_current
            outpath = os.path.join(initpath, filename + ftxt + ".png")

            save_image(self, outpath, out)

        return True

    except:
        print("Unexpected error:" + str(sys.exc_info()))
        self.report({'ERROR'}, "MeasureIt: Unable to create render image")
        return False
Esempio n. 37
0
    def draw(self, parent, context):
        #get the addon settings/prefs
        settings = context.user_preferences.addons['piemenus'].preferences

        #grab the biggest text dimensions that we already calced previously        
        biggestdimensionX = self.biggest_dim_x
        biggestdimensionY = self.biggest_dim_y
        
        for it in self.sliders:
            #draw some text above it.
            blf.size(0, self.text_size, self.text_dpi)
            dimension = blf.dimensions(0, it.id)

            x = it.x - dimension[0]*.5
            y = it.y + (it.height + dimension[1])* 0.5 + 2
            # Draw text
            blf.enable(0, blf.SHADOW)
            blf.shadow_offset(0, 0, 0)
            blf.shadow(0, 5, 0.0, 0.0, 0.0, 1.0)
            bgl.glColor3f(self.rt, self.gt, self.bt)
            
            blf.position(0, x, y, 0)
            blf.draw(0, it.id)
            blf.disable(0, blf.SHADOW)
            
            

            #draw left side one color
            bgl.glColor4f(self.rsSelected,self.gsSelected, self.bsSelected,1.0)
            pmu.draw_outline_or_region(bgl.GL_TRIANGLE_FAN, it.screen_left)
            
            #draw the right side another color
            bgl.glColor4f(self.rsInner,self.gsInner, self.bsInner,1.0)
            pmu.draw_outline_or_region(bgl.GL_TRIANGLE_FAN, it.screen_right)
            
            #Draw box outline
            bgl.glColor4f(self.rsOutline,self.gsOutline, self.bsOutline,1.0)           
            pmu.draw_outline_or_region(bgl.GL_LINE_LOOP, it.screen_left)
            pmu.draw_outline_or_region(bgl.GL_LINE_LOOP, it.screen_right)
            
            #put the text on top
            blf.enable(0, blf.KERNING_DEFAULT)
            prop_text = str(getattr(it.data, it.prop, 0))[0:4]
            #prop_text = "Test"
            dimensions2 = blf.dimensions(0,prop_text)
            x2 =it.x - dimensions2[0] * 0.5
            y2 = it.y - dimensions2[1] * 0.5
            blf.position(0, x2, y2, 0)
            bgl.glColor4f(self.rt, self.gt, self.bt, 1.0)
            blf.draw(0, prop_text)
            blf.disable(0, blf.KERNING_DEFAULT)
                
        for it in self.menu_items:
            sel = it == parent.current
            it_poll = it.poll(context)
 
            # center item on the circle
            #x = (self.menu_x + it.x) - (dimension[0] * 0.5)
            #y = (self.menu_y + it.y) - (dimension[1] * 0.5)

            blf.size(0, self.text_size, self.text_dpi)
            dimension = blf.dimensions(0, it.id)

            #needed for box centering
            x = (it.x) - (biggestdimensionX * 0.5)
            y = (it.y) - (biggestdimensionY * 0.5)

            #needed offset for text centering
            blf.size(0, self.text_size, self.text_dpi)
            dimension = blf.dimensions(0, it.id)
            xt = ((biggestdimensionX-dimension[0]) * 0.5)
            yt = ((biggestdimensionY-dimension[1]) * 0.5)

            # Draw background buttons
            if sel and it_poll:
                bgl.glColor4f(self.ris, self.gis, self.bis, self.ais)
            else:
                bgl.glColor4f(self.ri, self.gi, self.bi, self.ai)
 
            #self.gl_pie_slice(bgl.GL_POLYGON, 8, it, x,y,30,90) #***
            #http://www.opengl.org/archives/resources/faq/technical/rasterization.htm#rast0120
            #self._round_box(bgl.GL_POLYGON, x - 20, y - 5, x + biggestdimensionX + 20, y + biggestdimensionY + 5)          
            
            if it.screen_poly_bound:
                shape = it.screen_poly_bound            
                pmu.draw_outline_or_region(bgl.GL_TRIANGLE_FAN, shape)
                bgl.glColor4f(self.ro, self.go, self.bo, 1.0)
                pmu.draw_outline_or_region(bgl.GL_LINE_LOOP, shape)
            
            bgl.glColor4f(self.ri, self.gi, self.bi, self.ai)
            #draw the circle
            if settings.clockBool:
                self._circle_(bgl.GL_TRIANGLE_FAN, (self.menu_x), (self.menu_y), 20)
            
                #draw the circle outline
                bgl.glColor4f(self.ro, self.go, self.bo, 1.0)
                self._circle_(bgl.GL_LINE_LOOP, (self.menu_x), (self.menu_y), 20)

                self._pointer_(bgl.GL_TRIANGLE_STRIP, (self.menu_x), (self.menu_y), self.pointerangle )
 
            # Draw text
            blf.enable(0, blf.SHADOW)
            blf.shadow_offset(0, 0, 0)
            blf.shadow(0, 5, 0.0, 0.0, 0.0, 1.0)
            if it_poll:
                bgl.glColor3f(self.rt, self.gt, self.bt)
            else:  # grayed out
                bgl.glColor3f(0.5, 0.5, 0.5)
            blf.position(0, x+xt, y+yt, 0)
            blf.draw(0, it.id)
            blf.disable(0, blf.SHADOW)
        
        
        #bind the named texure to GL_TEXTURE_2D
        #http://stackoverflow.com/questions/11217121/how-to-manage-memory-with-texture-in-opengl
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, settings.pieIconBindcode)
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST)
        
        bgl.glEnable(bgl.GL_TEXTURE_2D)
        bgl.glEnable(bgl.GL_BLEND) 
        
        for it in self.menu_items:
            bgl.glColor4f(1, 1, 1, 1)
            if it.icon:
                #place the icon quad
                verts = it.screen_icon_quad
            
                bgl.glBegin(bgl.GL_QUADS)
            
                bgl.glTexCoord2f(it.tex_coords[0][0],it.tex_coords[0][1])
                bgl.glVertex2f(verts[0][0],verts[0][1])
                bgl.glTexCoord2f(it.tex_coords[1][0],it.tex_coords[1][1])
                bgl.glVertex2f(verts[1][0],verts[1][1])
                bgl.glTexCoord2f(it.tex_coords[2][0],it.tex_coords[2][1])
                bgl.glVertex2f(verts[2][0],verts[2][1])
                bgl.glTexCoord2f(it.tex_coords[3][0],it.tex_coords[3][1])
                bgl.glVertex2f(verts[3][0],verts[3][1])
            
                bgl.glEnd()
            
            #TODO
            #text value in center?
            #labe over top?    
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glDisable(bgl.GL_TEXTURE_2D)   
Esempio n. 38
0
def draw_callback_3d(self, context):
    def draw(ps, cs):
        for p, c in zip(ps, cs):
            bgl.glColor4f(*c)
            bgl.glVertex3f(*p)
        bgl.glColor4f(*cs[0])
        bgl.glVertex3f(*ps[0])

    #bgl.glPushAttrib(bgl.GL_ALL_ATTRIB_BITS) ## #debug - stack context
    try:
        frcurrent = context.scene.frame_current
        for amset in State.data.amsets:
            frprevlimit = amset.dissolve_length
            for fr in range(frcurrent - frprevlimit, frcurrent + 1):
                for aseq in amset.get_ampseqs():
                    amp = aseq.get(fr)
                    if not amp:
                        continue
                    viewvec = State.current_viewvec if Pref.use_billboard else amp.viewvec
                    dfr = frcurrent - fr
                    tfr = 1.0 - dfr / frprevlimit if frprevlimit != 0 else 1.0

                    ##
                    u0 = viewvec.cross(Vector((
                        0, 0,
                        1))).normalized() if abs(viewvec.z) != 1.0 else Vector(
                            (1, 0, 0))
                    v0 = u0.cross(viewvec)
                    size = amp.size
                    u = u0 * size
                    v = v0 * size
                    z_offset = fr / 1000
                    #z_offset = fr/100
                    #z_offset = -dfr/50
                    #z_offset = -dfr/1000
                    #q = amp.location
                    q = amp.location - viewvec * z_offset
                    alpha, quadscale = 1, 1
                    if amset.dissolve_method == AMDissolveMethod.Opacity:
                        #alpha = tfr
                        alpha = tfr**2.0
                    elif amset.dissolve_method == AMDissolveMethod.Size:
                        quadscale = tfr**2.0
                    elif amset.dissolve_method == AMDissolveMethod.OpacitySize:
                        alpha = tfr**2.0
                        quadscale = tfr**2.0
                    elif amset.dissolve_method == AMDissolveMethod.Const:
                        pass

                    u *= quadscale
                    v *= quadscale

                    cs = [(1, 1, 1, alpha), (1, 1, 1, .0), (1, 1, 1, .0),
                          (1, 1, 1, .0)]
                    bgl.glEnable(bgl.GL_BLEND)

                    bgl.glDepthMask(bgl.GL_FALSE)

                    if amset.blendmode == AMBlendMode.AlphaOver:
                        ## alpha over
                        bgl.glBlendFunc(bgl.GL_SRC_ALPHA,
                                        bgl.GL_ONE_MINUS_SRC_ALPHA)
                        #bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE)
                        bgl.glBlendEquation(bgl.GL_FUNC_ADD)
                    elif amset.blendmode == AMBlendMode.Additive:
                        ## additive
                        bgl.glBlendEquation(bgl.GL_FUNC_ADD)
                        bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE)
                    else:
                        print('<!> invalid blendmode: ' + amset.blendmode)

                    if amset.image_texture and amset.image_texture.bindcode[0]:
                        amset.image_texture.gl_touch(0)
                        bgl.glEnable(bgl.GL_TEXTURE_2D)
                        bgl.glColor4f(1.0, 1.0, 1.0, alpha)
                        bgl.glBindTexture(bgl.GL_TEXTURE_2D,
                                          amset.image_texture.bindcode[0])
                        bgl.glBegin(bgl.GL_QUADS)
                        ps = [q - u - v, q + u - v, q + u + v, q - u + v]
                        bgl.glTexCoord2f(0.0, 0.0)
                        bgl.glVertex3f(*ps[0])
                        bgl.glTexCoord2f(1.0, 0.0)
                        bgl.glVertex3f(*ps[1])
                        bgl.glTexCoord2f(1.0, 1.0)
                        bgl.glVertex3f(*ps[2])
                        bgl.glTexCoord2f(0.0, 1.0)
                        bgl.glVertex3f(*ps[3])
                        bgl.glEnd()
                        bgl.glDisable(bgl.GL_TEXTURE_2D)
                    else:
                        bgl.glBegin(bgl.GL_POLYGON)
                        draw([q, q + u, q + u + v, q + v], cs)
                        draw([q, q - u, q - u + v, q + v], cs)
                        draw([q, q + u, q + u - v, q - v], cs)
                        draw([q, q - u, q - u - v, q - v], cs)
                        bgl.glEnd()
                    bgl.glDisable(bgl.GL_BLEND)
                    bgl.glDepthMask(bgl.GL_TRUE)

        # restore opengl defaults
        bgl.glLineWidth(1)
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
    except Exception as e:
        print('error in draw callback')
        print(e)
    def render_main(self, context, objlist, animation=False):
        # noinspection PyBroadException,PyBroadException
        # Save old info
        scene = context.scene
        render = scene.render
        settings = render.image_settings
        depth = settings.color_depth
        settings.color_depth = '8'
        # noinspection PyBroadException
        try:

            # Get visible layers
            layers = []
            for x in range(0, 20):
                if scene.layers[x] is True:
                    layers.extend([x])

            # --------------------
            # Get resolution
            # --------------------
            render_scale = render.resolution_percentage / 100

            width = int(render.resolution_x * render_scale)
            height = int(render.resolution_y * render_scale)
            # ---------------------------------------
            # Get output path
            # ---------------------------------------
            temp_path = path.realpath(bpy.app.tempdir)
            if len(temp_path) > 0:
                outpath = path.join(temp_path, "archipack_tmp_render.png")
            else:
                self.report({
                    'ERROR'
                }, "Archipack: Unable to save temporary render image. Define a valid temp path"
                            )
                settings.color_depth = depth
                return False

            # Get Render Image
            img = self.get_render_image(outpath)
            if img is None:
                self.report({
                    'ERROR'
                }, "Archipack: Unable to save temporary render image. Define a valid temp path"
                            )
                settings.color_depth = depth
                return False

            # -----------------------------
            # Calculate rows and columns
            # -----------------------------
            tile_x = 240
            tile_y = 216
            row_num = ceil(height / tile_y)
            col_num = ceil(width / tile_x)
            print("Archipack: Image divided in " + str(row_num) + "x" +
                  str(col_num) + " tiles")

            # pixels out of visible area
            cut4 = (col_num * tile_x *
                    4) - width * 4  # pixels aout of drawing area
            totpixel4 = width * height * 4  # total pixels RGBA

            viewport_info = bgl.Buffer(bgl.GL_INT, 4)
            bgl.glGetIntegerv(bgl.GL_VIEWPORT, viewport_info)

            # Load image on memory
            img.gl_load(0, bgl.GL_NEAREST, bgl.GL_NEAREST)

            # 2.77 API change
            if bpy.app.version >= (2, 77, 0):
                tex = img.bindcode[0]
            else:
                tex = img.bindcode

            # --------------------------------------------
            # Create output image (to apply texture)
            # --------------------------------------------
            if "archipack_output" in bpy.data.images:
                out_img = bpy.data.images["archipack_output"]
                if out_img is not None:
                    out_img.user_clear()
                    bpy.data.images.remove(out_img)

            out = bpy.data.images.new("archipack_output", width, height)
            tmp_pixels = [1] * totpixel4

            # --------------------------------
            # Loop for all tiles
            # --------------------------------
            for row in range(0, row_num):
                for col in range(0, col_num):
                    buffer = bgl.Buffer(bgl.GL_FLOAT, width * height * 4)
                    bgl.glDisable(
                        bgl.GL_SCISSOR_TEST
                    )  # if remove this line, get blender screenshot not image
                    bgl.glViewport(0, 0, tile_x, tile_y)

                    bgl.glMatrixMode(bgl.GL_PROJECTION)
                    bgl.glLoadIdentity()

                    # defines ortographic view for single tile
                    x1 = tile_x * col
                    y1 = tile_y * row
                    bgl.gluOrtho2D(x1, x1 + tile_x, y1, y1 + tile_y)

                    # Clear
                    bgl.glClearColor(0.0, 0.0, 0.0, 0.0)
                    bgl.glClear(bgl.GL_COLOR_BUFFER_BIT
                                | bgl.GL_DEPTH_BUFFER_BIT)

                    bgl.glEnable(bgl.GL_TEXTURE_2D)
                    bgl.glBindTexture(bgl.GL_TEXTURE_2D, tex)

                    # defines drawing area
                    bgl.glBegin(bgl.GL_QUADS)

                    bgl.glColor3f(1.0, 1.0, 1.0)
                    bgl.glTexCoord2f(0.0, 0.0)
                    bgl.glVertex2f(0.0, 0.0)

                    bgl.glTexCoord2f(1.0, 0.0)
                    bgl.glVertex2f(width, 0.0)

                    bgl.glTexCoord2f(1.0, 1.0)
                    bgl.glVertex2f(width, height)

                    bgl.glTexCoord2f(0.0, 1.0)
                    bgl.glVertex2f(0.0, height)

                    bgl.glEnd()

                    # -----------------------------
                    # Loop to draw all lines
                    # -----------------------------
                    for o, d in objlist:
                        if o.hide is False:
                            # verify visible layer
                            for x in range(0, 20):
                                if o.layers[x] is True:
                                    if x in layers:
                                        context.scene.objects.active = o
                                        # print("%s: %s" % (o.name, d.manip_stack))
                                        manipulators = d.manip_stack
                                        if manipulators is not None:
                                            for m in manipulators:
                                                if m is not None:
                                                    m.draw_callback(
                                                        m,
                                                        context,
                                                        render=True)
                                    break

                    # -----------------------------
                    # Loop to draw all debug
                    # -----------------------------
                    """
                    if scene.archipack_debug is True:
                        selobj = bpy.context.selected_objects
                        for myobj in selobj:
                            if scene.archipack_debug_vertices is True:
                                draw_vertices(context, myobj, None, None)
                            if scene.archipack_debug_faces is True or scene.archipack_debug_normals is True:
                                draw_faces(context, myobj, None, None)
                    """
                    """
                    if scene.archipack_rf is True:
                        bgl.glColor3f(1.0, 1.0, 1.0)
                        rfcolor = scene.archipack_rf_color
                        rfborder = scene.archipack_rf_border
                        rfline = scene.archipack_rf_line

                        bgl.glLineWidth(rfline)
                        bgl.glColor4f(rfcolor[0], rfcolor[1], rfcolor[2], rfcolor[3])

                        x1 = rfborder
                        x2 = width - rfborder
                        y1 = int(ceil(rfborder / (width / height)))
                        y2 = height - y1
                        draw_rectangle((x1, y1), (x2, y2))
                    """
                    # --------------------------------
                    # copy pixels to temporary area
                    # --------------------------------
                    bgl.glFinish()
                    bgl.glReadPixels(0, 0, width, height, bgl.GL_RGBA,
                                     bgl.GL_FLOAT, buffer)  # read image data
                    for y in range(0, tile_y):
                        # final image pixels position
                        p1 = (y * width * 4) + (row * tile_y * width *
                                                4) + (col * tile_x * 4)
                        p2 = p1 + (tile_x * 4)
                        # buffer pixels position
                        b1 = y * width * 4
                        b2 = b1 + (tile_x * 4)

                        if p1 < totpixel4:  # avoid pixel row out of area
                            if col == col_num - 1:  # avoid pixel columns out of area
                                p2 -= cut4
                                b2 -= cut4

                            tmp_pixels[p1:p2] = buffer[b1:b2]

            # -----------------------
            # Copy temporary to final
            # -----------------------
            out.pixels = tmp_pixels[:]  # Assign image data
            img.gl_free()  # free opengl image memory

            # delete image
            img.user_clear()
            bpy.data.images.remove(img)
            # remove temp file
            remove(outpath)
            # reset
            bgl.glEnable(bgl.GL_SCISSOR_TEST)
            # -----------------------
            # restore opengl defaults
            # -----------------------
            bgl.glLineWidth(1)
            bgl.glDisable(bgl.GL_BLEND)
            bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
            # Saves image
            if out is not None:
                # and (scene.archipack_render is True or animation is True):
                ren_path = bpy.context.scene.render.filepath
                filename = "ap_frame"
                if len(ren_path) > 0:
                    if ren_path.endswith(path.sep):
                        initpath = path.realpath(ren_path) + path.sep
                    else:
                        (initpath, filename) = path.split(ren_path)

                ftxt = "%04d" % scene.frame_current
                outpath = path.realpath(
                    path.join(initpath, filename + ftxt + ".png"))

                self.save_image(outpath, out)

            settings.color_depth = depth
            return True

        except:
            settings.color_depth = depth
            print("Unexpected error:" + str(exc_info()))
            self.report({
                'ERROR'
            }, "Archipack: Unable to create render image. Be sure the output render path is correct"
                        )
            return False
Esempio n. 40
0
def draw_preview_callback(self):
    """TODO Missing documentation"""

    # Search for View_3d window
    area = None
    if bpy.context.area.type != 'VIEW_3D':
        return bpy.context.area
    else:
        for oWindow in bpy.context.window_manager.windows:
            oScreen = oWindow.screen
            for oArea in oScreen.areas:
                if oArea.type == 'VIEW_3D':
                    area = oArea

    modelsPosesColl = bUtils.getPhobosPreferences().models_poses
    activeModelPoseIndex = bpy.context.scene.active_ModelPose

    if (len(modelsPosesColl) > 0) and area:

        # Draw a textured quad
        area_widths = [
            region.width for region in bpy.context.area.regions if region.type == 'WINDOW'
        ]
        area_heights = [
            region.height for region in bpy.context.area.regions if region.type == 'WINDOW'
        ]
        if (len(area_widths) > 0) and (len(area_heights) > 0):

            active_preview = modelsPosesColl[bpy.data.images[activeModelPoseIndex].name]
            im = bpy.data.images[activeModelPoseIndex]

            view_width = area_widths[0]
            view_height = area_heights[0]
            tex_start_x = 50
            tex_end_x = view_width - 50
            tex_start_y = 50
            tex_end_y = view_height - 50
            if im.size[0] < view_width:
                diff = int((view_width - im.size[0]) / 2)
                tex_start_x = diff
                tex_end_x = diff + im.size[0]
            if im.size[1] < view_height:
                diff = int((view_height - im.size[1]) / 2)
                tex_start_y = diff
                tex_end_y = diff + im.size[1]

            # Draw information
            font_id = 0  # XXX, need to find out how best to get this.
            blf.position(font_id, tex_start_x, tex_end_y + 20, 0)
            blf.size(font_id, 20, 72)
            blf.draw(font_id, active_preview.label)

            tex = im.bindcode
            bgl.glEnable(bgl.GL_TEXTURE_2D)
            # if using blender 2.77 change tex to tex[0]
            bgl.glBindTexture(bgl.GL_TEXTURE_2D, tex)

            # Background
            bgl.glEnable(bgl.GL_BLEND)

            bgl.glBegin(bgl.GL_QUADS)
            bgl.glColor4f(0, 0, 0, 0.3)
            bgl.glVertex2i(0, 0)
            bgl.glVertex2i(0, view_height)
            bgl.glVertex2i(view_width, view_height)
            bgl.glVertex2i(view_width, 0)

            # Draw Image
            bgl.glColor4f(1, 1, 1, 1)
            bgl.glTexCoord2f(0, 0)
            bgl.glVertex2i(int(tex_start_x), int(tex_start_y))
            bgl.glTexCoord2f(0, 1)
            bgl.glVertex2i(int(tex_start_x), int(tex_end_y))
            bgl.glTexCoord2f(1, 1)
            bgl.glVertex2i(int(tex_end_x), int(tex_end_y))
            bgl.glTexCoord2f(1, 0)
            bgl.glVertex2i(int(tex_end_x), int(tex_start_y))
            bgl.glEnd()

            # restore opengl defaults
            bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
            bgl.glDisable(bgl.GL_QUADS)
            bgl.glDisable(bgl.GL_BLEND)
            bgl.glDisable(bgl.GL_TEXTURE_2D)
Esempio n. 41
0
    def draw_texture(cls, _, context):
        sc = context.scene

        if not cls.is_running(context):
            return

        # no textures are selected
        if sc.muv_texture_projection_tex_image == "None":
            return

        # get texture to be renderred
        img = bpy.data.images[sc.muv_texture_projection_tex_image]

        # setup rendering region
        rect = _get_canvas(context)

        # Apply affine transformation.
        center = mathutils.Vector((
            (rect.x1 + rect.x0) / 2.0,
            (rect.y1 + rect.y0) / 2.0,
            0.0,
        ))
        p1 = mathutils.Vector((rect.x0 - center.x, rect.y0 - center.y, 1.0))
        p2 = mathutils.Vector((rect.x0 - center.x, rect.y1 - center.y, 1.0))
        p3 = mathutils.Vector((rect.x1 - center.x, rect.y1 - center.y, 1.0))
        p4 = mathutils.Vector((rect.x1 - center.x, rect.y0 - center.y, 1.0))
        mat_affine = _create_affine_matrix(
            sc.muv_texture_projection_adjust_window,
            sc.muv_texture_projection_tex_scaling,
            sc.muv_texture_projection_tex_rotation,
            sc.muv_texture_projection_tex_translation)
        p1 = compat.matmul(mat_affine, p1) + center
        p2 = compat.matmul(mat_affine, p2) + center
        p3 = compat.matmul(mat_affine, p3) + center
        p4 = compat.matmul(mat_affine, p4) + center

        positions = [[p1.x, p1.y], [p2.x, p2.y], [p3.x, p3.y], [p4.x, p4.y]]
        tex_coords = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]]

        # OpenGL configuration
        if compat.check_version(2, 80, 0) >= 0:
            bgl.glEnable(bgl.GL_BLEND)
            bgl.glEnable(bgl.GL_TEXTURE_2D)
            bgl.glActiveTexture(bgl.GL_TEXTURE0)
            if img.bindcode:
                bind = img.bindcode
                bgl.glBindTexture(bgl.GL_TEXTURE_2D, bind)
        else:
            bgl.glEnable(bgl.GL_BLEND)
            bgl.glEnable(bgl.GL_TEXTURE_2D)
            if img.bindcode:
                bind = img.bindcode[0]
                bgl.glBindTexture(bgl.GL_TEXTURE_2D, bind)
                bgl.glTexParameteri(bgl.GL_TEXTURE_2D,
                                    bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR)
                bgl.glTexParameteri(bgl.GL_TEXTURE_2D,
                                    bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR)
                bgl.glTexEnvi(bgl.GL_TEXTURE_ENV, bgl.GL_TEXTURE_ENV_MODE,
                              bgl.GL_MODULATE)

        # render texture
        bgl.glBegin(bgl.GL_QUADS)
        bgl.glColor4f(1.0, 1.0, 1.0,
                      sc.muv_texture_projection_tex_transparency)
        for (v1, v2), (u, v) in zip(positions, tex_coords):
            bgl.glTexCoord2f(u, v)
            bgl.glVertex2f(v1, v2)
        bgl.glEnd()
Esempio n. 42
0
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()
Esempio n. 43
0
def draw(icon,
         x,
         y,
         size,
         alpha=1.0,
         round_radius=0.0,
         rotation=0.0,
         poly_line=None):
    """iconを描画
    :param icon: iconを示す文字列か番号。単色で塗りつぶす場合はRGBAを指定する
    :type icon: str | int | list | tuple
    :param x: 左下座標
    :type x: int
    :param y: 左下座標
    :type y: int
    :type size: int
    :type alpha: int | float
    :param round_radius: 角の丸め半径。size/2で円になる
    :type round_radius: int | float
    :param rotation: 回転させて描画する。回転はアイコンの中心が基準。
        時計回りが正
    :type: rotation: 0.0
    :param poly_line: 描画用のポリゴンの絶対座標。x,yからの相対座標ではない。
        [[x, y], ...]
    :type: list | tuple
    """

    if isinstance(icon, (list, tuple)):
        return draw_fill(icon, x, y, size, alpha, round_radius)

    if isinstance(icon, int):
        for name, value in icons.items():
            if value == icon:
                icon = name
                break
        else:
            return False

    if not icon or icon == 'NONE':
        return False
    else:
        tex_data = get_texture(icon)
    if not tex_data:
        print("icon not found", icon)
        return False

    if isinstance(tex_data, types.FunctionType):  # internal_icons用
        tex_data(x, y, size, size, alpha)
        return True

    texture, buf, sx, sy, xmin, ymin, w, h = tex_data
    round_radius = min(size / 2, round_radius)

    if poly_line is not None:
        coords = [(co[0] - x, co[1] - y) for co in poly_line]
    elif round_radius != 0.0:
        r = round_radius
        pi = math.pi
        a = r
        b = size - r
        coords = []  # 0.0 ~ +size の範囲
        # 左下
        coords += vagl.draw_arc_get_vectors(a, a, r, pi, pi * 3 / 2, 4)
        # 右下
        coords += vagl.draw_arc_get_vectors(b, a, r, pi * 3 / 2, 0.0, 4)
        # 右上
        coords += vagl.draw_arc_get_vectors(b, b, r, 0.0, pi / 2, 4)
        # 左上
        coords += vagl.draw_arc_get_vectors(a, b, r, pi / 2, pi, 4)
    else:
        coords = [[0, 0], [size, 0], [size, size], [0, size]]

    with vagl.GLSettings.push_attrib(bgl.GL_ENABLE_BIT | bgl.GL_TEXTURE_BIT):
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glEnable(bgl.GL_TEXTURE_2D)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture)

        bgl.glColor4f(1.0, 1.0, 1.0, alpha)

        bgl.glBegin(bgl.GL_POLYGON)
        for co in coords:
            fx = co[0] / size
            fy = co[1] / size
            tx = xmin / sx + max(w, h) / sx * fx
            ty = ymin / sy + max(w, h) / sy * fy
            bgl.glTexCoord2f(tx, ty)
            if rotation != 0.0:
                f = size / 2
                cx = co[0] - f
                cy = co[1] - f
                sin = math.sin(rotation)
                cos = math.cos(rotation)
                co = [cx * cos - cy * sin + f, cx * sin + cy * cos + f]
            bgl.glVertex2f(x + co[0], y + co[1])
        bgl.glEnd()

    return True
Esempio n. 44
0
def render_opengl(self, context):
    from math import ceil

    layers = []
    scene = context.scene
    for x in range(0, 20):
        if scene.layers[x] is True:
            layers.extend([x])

    objlist = context.scene.objects
    render_scale = scene.render.resolution_percentage / 100

    width = int(scene.render.resolution_x * render_scale)
    height = int(scene.render.resolution_y * render_scale)
    
    # I cant use file_format becuase the pdf writer needs jpg format
    # the file_format returns 'JPEG' not 'JPG'
#     file_format = context.scene.render.image_settings.file_format.lower()
    ren_path = bpy.path.abspath(bpy.context.scene.render.filepath) + ".jpg"
    
#     if len(ren_path) > 0:
#         if ren_path.endswith(os.path.sep):
#             initpath = os.path.realpath(ren_path) + os.path.sep
#         else:
#             (initpath, filename) = os.path.split(ren_path)
#         outpath = os.path.join(initpath, "ogl_tmp.png")
#     else:
#         self.report({'ERROR'}, "Invalid render path")
#         return False

    img = get_render_image(ren_path)
    
    if img is None:
        self.report({'ERROR'}, "Invalid render path:" + ren_path)
        return False

    tile_x = 240
    tile_y = 216
    row_num = ceil(height / tile_y)
    col_num = ceil(width / tile_x)
    
    cut4 = (col_num * tile_x * 4) - width * 4  
    totpixel4 = width * height * 4 

    viewport_info = bgl.Buffer(bgl.GL_INT, 4)
    bgl.glGetIntegerv(bgl.GL_VIEWPORT, viewport_info)
    
    img.gl_load(0, bgl.GL_NEAREST, bgl.GL_NEAREST)

    # 2.77 API change
    if bpy.app.version >= (2, 77, 0):
        tex = img.bindcode[0]
    else:
        tex = img.bindcode
    
    if context.scene.name in bpy.data.images:
        old_img = bpy.data.images[context.scene.name]
        old_img.user_clear()
        bpy.data.images.remove(old_img)
             
    img_result = bpy.data.images.new(context.scene.name, width, height)        
    
    tmp_pixels = [1] * totpixel4

    #---------- Loop for all tiles
    for row in range(0, row_num):
        for col in range(0, col_num):
            buffer = bgl.Buffer(bgl.GL_FLOAT, width * height * 4)
            bgl.glDisable(bgl.GL_SCISSOR_TEST)  # if remove this line, get blender screenshot not image
            bgl.glViewport(0, 0, tile_x, tile_y)

            bgl.glMatrixMode(bgl.GL_PROJECTION)
            bgl.glLoadIdentity()

            # defines ortographic view for single tile
            x1 = tile_x * col
            y1 = tile_y * row
            bgl.gluOrtho2D(x1, x1 + tile_x, y1, y1 + tile_y)

            # Clear
            bgl.glClearColor(0.0, 0.0, 0.0, 0.0)
            bgl.glClear(bgl.GL_COLOR_BUFFER_BIT | bgl.GL_DEPTH_BUFFER_BIT)

            bgl.glEnable(bgl.GL_TEXTURE_2D)
            bgl.glBindTexture(bgl.GL_TEXTURE_2D, tex)

            # defines drawing area
            bgl.glBegin(bgl.GL_QUADS)

            bgl.glColor3f(1.0, 1.0, 1.0)
            bgl.glTexCoord2f(0.0, 0.0)
            bgl.glVertex2f(0.0, 0.0)

            bgl.glTexCoord2f(1.0, 0.0)
            bgl.glVertex2f(width, 0.0)

            bgl.glTexCoord2f(1.0, 1.0)
            bgl.glVertex2f(width, height)

            bgl.glTexCoord2f(0.0, 1.0)
            bgl.glVertex2f(0.0, height)

            bgl.glEnd()

            for obj in objlist:
                if obj.mv.type == 'VISDIM_A':
                    for x in range(0, 20):
                        if obj.layers[x] is True:
                            if x in layers:
                                opengl_dim = obj.mv.opengl_dim
                                if not opengl_dim.hide:
                                    draw_dimensions(context, obj, opengl_dim, None, None)
                            break 

            #---------- copy pixels to temporary area
            bgl.glFinish()
            bgl.glReadPixels(0, 0, width, height, bgl.GL_RGBA, bgl.GL_FLOAT, buffer)  # read image data
            for y in range(0, tile_y):
                # final image pixels position
                p1 = (y * width * 4) + (row * tile_y * width * 4) + (col * tile_x * 4)
                p2 = p1 + (tile_x * 4)
                # buffer pixels position
                b1 = y * width * 4
                b2 = b1 + (tile_x * 4)

                if p1 < totpixel4:  # avoid pixel row out of area
                    if col == col_num - 1:  # avoid pixel columns out of area
                        p2 -= cut4
                        b2 -= cut4

                    tmp_pixels[p1:p2] = buffer[b1:b2]

    img_result.pixels = tmp_pixels[:]
    img.gl_free()

    img.user_clear()
    bpy.data.images.remove(img)
    os.remove(ren_path)
    bgl.glEnable(bgl.GL_SCISSOR_TEST)

    #---------- restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
    
    if img_result is not None:            
        return img_result