示例#1
0
def render_offscreen(self, context, width, height, imgname=''):
    assert context.area.type == 'NODE_EDITOR' and context.space_data.tree_type == 'CompositorNodeTree'

    gos = gpu.offscreen.new(width,height)
    gos.bind(True)
    try:
        bgl.glMatrixMode(bgl.GL_PROJECTION)
        bgl.glLoadIdentity()
        bgl.glScalef(1/width*2,1/height*2,1.0)
        bgl.glTranslatef(-width/2,-height/2,0)

        draw_callback(self, context, {
                  'center': Vector((width/2, height/2))
                , 'zoom': 1.0
                , 'back_image_alpha': 1.0
                , 'image_size': (width, height)
                })

        buffer = bgl.Buffer(bgl.GL_FLOAT, width * height * 4)
        x,y = 0,0
        bgl.glReadPixels(x, y, width, height , bgl.GL_RGBA, bgl.GL_FLOAT, buffer)

        out = prepare_blimage(width, height, imgname or Pref.default_output_image_name)
        out.pixels = buffer[:]
    finally:
        gos.unbind(True)
示例#2
0
	def text( self, text, arg1=0, arg2=0, arg3=0 ):
		x = 0
		y = 0
		z = 0
		rx = 0
		ry = 0
		rz = 0

		if type( arg1 ) is mathutils.Vector():
			x = arg1.x
			y = arg1.y
			z = arg1.z
			if type( arg2 ) is mathutils.Vector():
				rx = arg2.x
				ry = arg2.y
				rz = arg2.z

		elif type( arg1 ) is bge.types.KX_GameObject or type( arg1 ) is str:
			if type( arg1 ) is str:
				arg1 = self.getObjectByName( arg1 )
			if arg1 is not 0:
				o = self.getPosition( arg1 )
				x = o.x
				y = o.y
				z = o.z
				# translation vector
				if type( arg2 ) is mathutils.Vector():
					x += arg2.x
					y += arg2.y
					z += arg2.z
				# rotation vector
				if type( arg3 ) is mathutils.Vector():
					rx = arg3.x
					ry = arg3.y
					rz = arg3.z
		
		elif type( arg1 ) is float and type( arg2 ) is float and type( arg3 ) is float:
			x = arg1
			y = arg2
			z = arg3

		elif type( arg1 ) is int and type( arg2 ) is int and type( arg3 ) is int:
			x = arg1
			y = arg2
			z = arg3
		
		width = bge.render.getWindowWidth()
		height = bge.render.getWindowHeight()
		ratiow = 1./width
		ratioh = 1./height
		bgl.glPushMatrix()
		bgl.glTranslatef( x,y,z )
#TODO transform angles to matrix!
		# bgl.glRotate( rx,ry,rz )
		bgl.glScalef( ratiow, ratioh, 0 )
		blf.position( self.font, 0,0,0 )
		blf.size( self.font, self.tsize, 300 )
		bgl.glColor3f( self.tcolor.x, self.tcolor.y, self.tcolor.z )
		blf.draw( self.font, text )
		bgl.glPopMatrix()
示例#3
0
    def bind(self):
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.glid[0])

        if not self.region is None:
            bgl.glMatrixMode(bgl.GL_TEXTURE)
            bgl.glLoadIdentity()
            bgl.glTranslatef(self.region.x, self.region.y, 0)
            bgl.glScalef(self.region.w, self.region.h, 1)
            bgl.glMatrixMode(bgl.GL_MODELVIEW)
示例#4
0
def h_draw_text(fid, text, bounds, color, margin=0, font_size=16, text_align=0, vertical_align=0, shadow=False, clip=True):
    text = str(text)
    width = render.getWindowWidth()
    height = render.getWindowHeight()
    
    #bgl.glColor4f(*(1, 0, 0, 1))
    #h_draw_quad_wire(bounds)
    
    if clip:
        h_clip_begin(bounds)
    
    blf.size(fid, font_size, 72)
    if shadow:
        blf.enable(fid, blf.SHADOW)
        blf.shadow(fid, 3, 0.0, 0.0, 0.0, 1.0)
        blf.shadow_offset(fid, 0, -1)
    else:
        blf.disable(fid, blf.SHADOW)
    bgl.glPushMatrix()
    
    # Fix upside-down text =)
    w, h = blf.dimensions(fid, text)
    bgl.glTranslated(0.0, h, 0.0)
    bgl.glScalef(1.0, -1.0, 1.0)
    
    bgl.glColor4f(*color)

    texts = text.split("\n")
    yn = 0
    if vertical_align == 0:
        yn = margin
    elif vertical_align == 1:
        yn = bounds[3]/2-(h*len(texts))/2
    elif vertical_align == 2:
        yn = (bounds[3]-(h*len(texts)))-margin
    for i in range(len(texts)):            
        texts[i] = texts[i].replace("\t", "        ")
        wl, hl = blf.dimensions(fid, texts[i])
        xn = 0
        
        if text_align == 0:
            xn = margin
        elif text_align == 1:
            xn = bounds[2]/2-wl/2
        elif text_align == 2:
            xn = (bounds[2]-wl)-margin
            
        blf.position(fid, bounds[0]+xn, -bounds[1]-(i*h)-yn, 1)
        
        blf.draw(fid, texts[i])
        
    bgl.glScalef(1.0, 1.0, 1.0)
    bgl.glPopMatrix()
    
    if clip:
        h_clip_end()
示例#5
0
def drawText(context,
             text,
             location,
             text_scale_value=TEXT_SCALE_VALUE,
             *,
             enable_depth=False,
             constant_scale=False):
    if (enable_depth):
        bgl.glEnable(bgl.GL_DEPTH_TEST)
    v3d = context.space_data
    rv3d = v3d.region_3d
    text_scale = text_scale_value * (
        (max(min(MAX_ZOOM_DISTANCE, rv3d.view_distance), MIN_ZOOM_DISTANCE)) /
        FRIENDLY_ZOOM_DISTANCE)

    font_id = 0

    axis, angle = getScreenLookAxis(context, location)

    bgl.glPushMatrix()
    bgl.glTranslatef(location.x, location.y, location.z)

    bgl.glPushMatrix()
    bgl.glRotatef(angle, axis.x, axis.y, axis.z)

    bgl.glPushMatrix()
    if (not constant_scale):
        bgl.glScalef(text_scale, text_scale, text_scale)
    else:
        bgl.glScalef(text_scale_value, text_scale_value, text_scale_value)

    bgl.glPushMatrix()
    bgl.glRotatef(90.0, 1.0, 0.0, 0.0)

    blf.position(font_id, 0.0, 0.0, 0.0)
    blf.size(font_id, 72, 72)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
    blf.draw(0, text)
    #     blf.rotation(0, 1.57);
    bgl.glPopMatrix()

    bgl.glPopMatrix()

    bgl.glPopMatrix()

    bgl.glPopMatrix()

    if (enable_depth):
        bgl.glDisable(bgl.GL_DEPTH_TEST)
示例#6
0
    def info(self, text, arg1=0, y=0, z=0):
        if self.configured is True and self.view_orientation is not 0:

            x = arg1
            if type(arg1) is mathutils.Vector():
                x = arg1.x
                y = arg1.y
                z = arg1.z
            elif type(arg1) is bge.types.KX_GameObject:
                o = self.getPosition(arg1)
                x = o.x
                y = o.y
                z = o.z

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

            ratiow = 1. / width
            ratioh = 1. / height
            ratios = mathutils.Vector(
                (self.view_orientation[0][3], self.view_orientation[1][3],
                 self.view_orientation[2][3])).length
            bgl.glPushMatrix()
            bgl.glTranslatef(x, y, z)
            buf = bgl.Buffer(bgl.GL_FLOAT, [16])
            buf[0] = self.view_orientation[0][0]
            buf[1] = self.view_orientation[0][1]
            buf[2] = self.view_orientation[0][2]
            buf[3] = 0
            buf[4] = self.view_orientation[1][0]
            buf[5] = self.view_orientation[1][1]
            buf[6] = self.view_orientation[1][2]
            buf[7] = 0
            buf[8] = self.view_orientation[2][0]
            buf[9] = self.view_orientation[2][1]
            buf[10] = self.view_orientation[2][2]
            buf[11] = 0
            buf[12] = 0
            buf[13] = 0
            buf[14] = 0
            buf[15] = 1
            bgl.glMultMatrixf(buf)
            bgl.glScalef(ratiow, ratioh, 0)
            blf.position(self.font, 0, 0, 0)
            blf.size(self.font, self.tsize, 300)
            bgl.glColor3f(self.tcolor.x, self.tcolor.y, self.tcolor.z)
            blf.draw(self.font, text)
            bgl.glPopMatrix()
示例#7
0
	def info( self, text, arg1=0, y=0, z=0 ):
		if self.configured is True and self.view_orientation is not 0:
			
			x = arg1
			if type( arg1 ) is mathutils.Vector():
				x = arg1.x
				y = arg1.y
				z = arg1.z
			elif type( arg1 ) is bge.types.KX_GameObject:
				o = self.getPosition( arg1 )
				x = o.x
				y = o.y
				z = o.z

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

			ratiow = 1./width
			ratioh = 1./height
			ratios = mathutils.Vector( ( self.view_orientation[0][3], self.view_orientation[1][3], self.view_orientation[2][3] ) ).length
			bgl.glPushMatrix()
			bgl.glTranslatef( x,y,z )
			buf = bgl.Buffer( bgl.GL_FLOAT, [16] )
			buf[0] = self.view_orientation[0][0]
			buf[1] = self.view_orientation[0][1]
			buf[2] = self.view_orientation[0][2]
			buf[3] = 0
			buf[4] = self.view_orientation[1][0]
			buf[5] = self.view_orientation[1][1]
			buf[6] = self.view_orientation[1][2]
			buf[7] = 0
			buf[8] = self.view_orientation[2][0]
			buf[9] = self.view_orientation[2][1]
			buf[10] = self.view_orientation[2][2]
			buf[11] = 0
			buf[12] = 0
			buf[13] = 0
			buf[14] = 0
			buf[15] = 1
			bgl.glMultMatrixf( buf )
			bgl.glScalef( ratiow, ratioh, 0 )
			blf.position( self.font, 0,0,0 )
			blf.size( self.font, self.tsize, 300 )
			bgl.glColor3f( self.tcolor.x, self.tcolor.y, self.tcolor.z )
			blf.draw( self.font, text )
			bgl.glPopMatrix()
示例#8
0
def drawText(context, text, location):
    v3d = context.space_data;
    rv3d = v3d.region_3d;
    text_scale = TEXT_SCALE_VALUE * ((max(min(MAX_ZOOM_DISTANCE, rv3d.view_distance), MIN_ZOOM_DISTANCE)) / FRIENDLY_ZOOM_DISTANCE);
#     print("RV3D PARAMETERS ::: " , rv3d.view_location, rv3d.view_distance);
#     print('TEXT SCALE USED :::: ', text_scale);
    
    
    font_id = 0;
    
    axis, angle = getScreenLookAxis(context);
    
    bgl.glPushMatrix();
    bgl.glTranslatef(location.x, location.y , location.z);
    
    bgl.glPushMatrix();
    bgl.glRotatef(angle, axis.x, axis.y, axis.z);
    
    bgl.glPushMatrix();
    bgl.glScalef(text_scale,text_scale,text_scale);
    
    bgl.glPushMatrix();
    bgl.glRotatef(90.0, 1.0, 0.0, 0.0);
    
    blf.position(font_id, 0.0, 0.0 , 0.0);
    blf.size(font_id, 72, 72);
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0);
    blf.draw(0, text);
#     blf.rotation(0, 1.57);
    bgl.glPopMatrix();
    
    bgl.glPopMatrix();
    
    bgl.glPopMatrix();
    
    bgl.glPopMatrix();
示例#9
0
    def text(self, text, arg1=0, arg2=0, arg3=0):
        x = 0
        y = 0
        z = 0
        rx = 0
        ry = 0
        rz = 0

        if type(arg1) is mathutils.Vector():
            x = arg1.x
            y = arg1.y
            z = arg1.z
            if type(arg2) is mathutils.Vector():
                rx = arg2.x
                ry = arg2.y
                rz = arg2.z

        elif type(arg1) is bge.types.KX_GameObject or type(arg1) is str:
            if type(arg1) is str:
                arg1 = self.getObjectByName(arg1)
            if arg1 is not 0:
                o = self.getPosition(arg1)
                x = o.x
                y = o.y
                z = o.z
                # translation vector
                if type(arg2) is mathutils.Vector():
                    x += arg2.x
                    y += arg2.y
                    z += arg2.z
                # rotation vector
                if type(arg3) is mathutils.Vector():
                    rx = arg3.x
                    ry = arg3.y
                    rz = arg3.z

        elif type(arg1) is float and type(arg2) is float and type(
                arg3) is float:
            x = arg1
            y = arg2
            z = arg3

        elif type(arg1) is int and type(arg2) is int and type(arg3) is int:
            x = arg1
            y = arg2
            z = arg3

        width = bge.render.getWindowWidth()
        height = bge.render.getWindowHeight()
        ratiow = 1. / width
        ratioh = 1. / height
        bgl.glPushMatrix()
        bgl.glTranslatef(x, y, z)
        #TODO transform angles to matrix!
        # bgl.glRotate( rx,ry,rz )
        bgl.glScalef(ratiow, ratioh, 0)
        blf.position(self.font, 0, 0, 0)
        blf.size(self.font, self.tsize, 300)
        bgl.glColor3f(self.tcolor.x, self.tcolor.y, self.tcolor.z)
        blf.draw(self.font, text)
        bgl.glPopMatrix()
示例#10
0
	def draw(self):
		if self.visible == False: return
		module.post_draw_step += 1
		
		cam = self.scene.active_camera
		orth = cam.ortho_scale
		
		height = render.getWindowHeight()
		width = render.getWindowWidth()
		near = cam.near
		far = cam.far
		h = cam.worldPosition.z
		font_id = Label._fontname_id[self._font]
		unit = width/orth
		self._glunit = unit
		rpos = self._position - cam.worldPosition
		
		bgl.glMatrixMode(bgl.GL_PROJECTION)
		bgl.glLoadIdentity()
		bgl.gluOrtho2D(0, width, 0, height)
		bgl.glMatrixMode(bgl.GL_MODELVIEW)
		bgl.glLoadIdentity()
		
		bgl.glEnable(bgl.GL_BLEND)
		bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
		bgl.glAlphaFunc(bgl.GL_SRC_ALPHA, 1)
		
		#Z AXIS
		oh = (far-near)/2
		ortho_unit = 1/oh
		dh = oh

		pos = list([width/2+rpos[0]*unit, height/2+rpos[1]*unit, dh*ortho_unit + rpos[2]*ortho_unit])
		if self._lastscale != self.scale or True:
			blf.size(font_id, int(self.scale.x*unit), 72)
		else:
			if self._lastorth != orth:
				sc = (float(self._lastorth) / float(orth)) * self.scale.x
				bgl.glScalef(sc,sc,1)
				print(str(self._lastorth) + " " + str(orth))
				pos[0] /= sc
				pos[1] /= sc
				
			else:
				self._lastorth = orth
		
		x, y = blf.dimensions(font_id, self._text) #NOTE: Always after blf.size()
		
		if self.align == ALIGN_CENTER:
			pos[0] -= (x)/2 * math.cos(self._rotation.z)
			pos[1] -= x/2 * math.sin(self._rotation.z)
		if self.align == ALIGN_RIGHT:
			pos[0] -= x * math.cos(self._rotation.z)
			pos[1] -= x * math.sin(self._rotation.z)
			
		if self.middle_height == True:
			pos[0] -= y/4 * math.sin(self._rotation.z)
			pos[1] -= y/4 * math.cos(self._rotation.z)
		
		blf.position(font_id, pos[0], pos[1], pos[2])
		blf.enable(font_id, blf.ROTATION)
		if self.rotation.z > 0.01 or self.rotation.z < -0.01:
			blf.rotation(font_id, self._rotation.z)
		else:
			blf.rotation(font_id, 0)
		
		if self.shadow == True:
			blf.position(font_id, pos[0]+self.shadow_offset[0], pos[1]+self.shadow_offset[1], pos[2])
			bgl.glColor4f(*self.shadow_color)
			blf.blur(font_id, self.shadow_blur)
			blf.draw(font_id, self._text)
			blf.position(font_id, pos[0], pos[1], pos[2])
			
		bgl.glColor4f(*self._color)
		blf.blur(font_id, self.blur)
		blf.draw(font_id, self._text)
		
		blf.disable(font_id, blf.ROTATION)
		
		self._lastscale = self.scale
示例#11
0
    def draw(self):
        if self.visible == False: return
        module.post_draw_step += 1

        cam = self.scene.active_camera
        orth = cam.ortho_scale

        height = render.getWindowHeight()
        width = render.getWindowWidth()
        near = cam.near
        far = cam.far
        h = cam.worldPosition.z
        font_id = Label._fontname_id[self._font]
        unit = width / orth
        self._glunit = unit
        rpos = self._position - cam.worldPosition

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

        bgl.glEnable(bgl.GL_BLEND)
        bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
        bgl.glAlphaFunc(bgl.GL_SRC_ALPHA, 1)

        #Z AXIS
        oh = (far - near) / 2
        ortho_unit = 1 / oh
        dh = oh

        pos = list([
            width / 2 + rpos[0] * unit, height / 2 + rpos[1] * unit,
            dh * ortho_unit + rpos[2] * ortho_unit
        ])
        if self._lastscale != self.scale or True:
            blf.size(font_id, int(self.scale.x * unit), 72)
        else:
            if self._lastorth != orth:
                sc = (float(self._lastorth) / float(orth)) * self.scale.x
                bgl.glScalef(sc, sc, 1)
                print(str(self._lastorth) + " " + str(orth))
                pos[0] /= sc
                pos[1] /= sc

            else:
                self._lastorth = orth

        x, y = blf.dimensions(font_id,
                              self._text)  #NOTE: Always after blf.size()

        if self.align == ALIGN_CENTER:
            pos[0] -= (x) / 2 * math.cos(self._rotation.z)
            pos[1] -= x / 2 * math.sin(self._rotation.z)
        if self.align == ALIGN_RIGHT:
            pos[0] -= x * math.cos(self._rotation.z)
            pos[1] -= x * math.sin(self._rotation.z)

        if self.middle_height == True:
            pos[0] -= y / 4 * math.sin(self._rotation.z)
            pos[1] -= y / 4 * math.cos(self._rotation.z)

        blf.position(font_id, pos[0], pos[1], pos[2])
        blf.enable(font_id, blf.ROTATION)
        if self.rotation.z > 0.01 or self.rotation.z < -0.01:
            blf.rotation(font_id, self._rotation.z)
        else:
            blf.rotation(font_id, 0)

        if self.shadow == True:
            blf.position(font_id, pos[0] + self.shadow_offset[0],
                         pos[1] + self.shadow_offset[1], pos[2])
            bgl.glColor4f(*self.shadow_color)
            blf.blur(font_id, self.shadow_blur)
            blf.draw(font_id, self._text)
            blf.position(font_id, pos[0], pos[1], pos[2])

        bgl.glColor4f(*self._color)
        blf.blur(font_id, self.blur)
        blf.draw(font_id, self._text)

        blf.disable(font_id, blf.ROTATION)

        self._lastscale = self.scale