This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/doc/python_api/rst/bge.render.rst
Benoit Bolsee 40f1c4f343 BGE: Various render improvements.
bge.logic.setRender(flag) to enable/disable render.
    The render pass is enabled by default but it can be disabled with
    bge.logic.setRender(False).
    Once disabled, the render pass is skipped and a new logic frame starts
    immediately. Note that VSync no longer limits the fps when render is off
    but the 'Use Frame Rate' option in the Render Properties still does.
    To run as many frames as possible, untick the option
    This function is useful when you don't need the default render, e.g.
    when doing offscreen render to an alternate device than the monitor.
    Note that without VSync, you must limit the frame rate by other means.

fbo = bge.render.offScreenCreate(width,height,[,samples=0][,target=bge.render.RAS_OFS_RENDER_BUFFER])
    Use this method to create an offscreen buffer of given size, with given MSAA
    samples and targetting either a render buffer (bge.render.RAS_OFS_RENDER_BUFFER)
    or a texture (bge.render.RAS_OFS_RENDER_TEXTURE). Use the former if you want to
    retrieve the frame buffer on the host and the latter if you want to pass the render
    to another context (texture are proper OGL object, render buffers aren't)
    The object created by this function can only be used as a parameter of the
    bge.texture.ImageRender() constructor to send the the render to the FBO rather
    than to the frame buffer. This is best suited when you want to create a render
    of specific size, or if you need an image with an alpha channel.

bge.texture.<imagetype>.refresh(buffer=None, format="RGBA", ts=-1.0)
    Without arg, the refresh method of the image objects is pretty much a no-op, it
    simply invalidates the image so that on next texture refresh, the image will
    be recalculated.
    It is now possible to pass an optional buffer object to transfer the image (and
    recalculate it if it was invalid) to an external object. The object must implement
    the 'buffer protocol'. The image will be transfered as "RGBA" or "BGRA" pixels
    depending on format argument (only those 2 formats are supported) and ts is an
    optional timestamp in the image depends on it (e.g. VideoFFmpeg playing a video file).
    With this function you don't need anymore to link the image object to a Texture
    object to use: the image object is self-sufficient.

bge.texture.ImageRender(scene, camera, fbo=None)
    Render to buffer is possible by passing a FBO object (see offScreenCreate).

bge.texture.ImageRender.render()
    Allows asynchronous render: call this method to render the scene but without
    extracting the pixels yet. The function returns as soon as the render commands
    have been send to the GPU. The render will proceed asynchronously in the GPU
    while the host can perform other tasks.
    To complete the render, you can either call refresh() directly of refresh the texture
    to which this object is the source. Asynchronous render is useful to achieve optimal
    performance: call render() on frame N and refresh() on frame N+1 to give as much as
    time as possible to the GPU to render the frame while the game engine can perform other tasks.

Support negative scale on camera.
    Camera scale was previously ignored in the BGE.
    It is now injected in the modelview matrix as a vertical or horizontal flip
    of the scene (respectively if scaleY<0 and scaleX<0).
    Note that the actual value of the scale is not used, only the sign.
    This allows to flip the image produced by ImageRender() without any performance
    degradation: the flip is integrated in the render itself.

Optimized image transfer from ImageRender to buffer.
    Previously, images that were transferred to the host were always going through
    buffers in VideoTexture. It is now possible to transfer ImageRender
    images to external buffer without intermediate copy (i.e. directly from OGL to buffer)
    if the attributes of the ImageRender objects are set as follow:
       flip=False, alpha=True, scale=False, depth=False, zbuff=False.
       (if you need to flip the image, use camera negative scale)
2016-06-11 22:05:20 +02:00

11 KiB
Raw Blame History

Rasterizer (bge.render)

Intro

Example of using a bge.types.SCA_MouseSensor, and two bge.types.KX_ObjectActuator to implement MouseLook:

Note

This can also be achieved with the bge.types.KX_MouseActuator.

# To use a mouse movement sensor "Mouse" and a
# motion actuator to mouse look:
import bge

# scale sets the speed of motion
scale = 1.0, 0.5

co = bge.logic.getCurrentController()
obj = co.owner
mouse = co.sensors["Mouse"]
lmotion = co.actuators["LMove"]
wmotion = co.actuators["WMove"]

# Transform the mouse coordinates to see how far the mouse has moved.
def mousePos():
   x = (bge.render.getWindowWidth() / 2 - mouse.position[0]) * scale[0]
   y = (bge.render.getWindowHeight() / 2 - mouse.position[1]) * scale[1]
   return (x, y)

pos = mousePos()

# Set the amount of motion: X is applied in world coordinates...
wmotion.useLocalTorque = False
wmotion.torque = ((0.0, 0.0, pos[0]))

# ...Y is applied in local coordinates
lmotion.useLocalTorque = True
lmotion.torque = ((-pos[1], 0.0, 0.0))

# Activate both actuators
co.activate(lmotion)
co.activate(wmotion)

# Centre the mouse
bge.render.setMousePosition(int(bge.render.getWindowWidth() / 2), int(bge.render.getWindowHeight() / 2))

Constants

bge.render.KX_TEXFACE_MATERIAL

Materials as defined by the texture face settings.

bge.render.KX_BLENDER_MULTITEX_MATERIAL

Materials approximating blender materials with multitexturing.

bge.render.KX_BLENDER_GLSL_MATERIAL

Materials approximating blender materials with GLSL.

bge.render.VSYNC_OFF

Disables vsync

bge.render.VSYNC_ON

Enables vsync

bge.render.VSYNC_ADAPTIVE

Enables adaptive vsync if supported. Adaptive vsync enables vsync if the framerate is above the monitors refresh rate. Otherwise, vsync is diabled if the framerate is too low.

bge.render.LEFT_EYE

Left eye being used during stereoscopic rendering.

bge.render.RIGHT_EYE

Right eye being used during stereoscopic rendering.

bge.render.RAS_OFS_RENDER_BUFFER

The pixel buffer for offscreen render is a RenderBuffer. Argument to offScreenCreate()

bge.render.RAS_OFS_RENDER_TEXTURE

The pixel buffer for offscreen render is a Texture. Argument to offScreenCreate()

Types

class bge.render.RASOffScreen

An off-screen render buffer object.

Use offScreenCreate() to create it. Currently it can only be used in the bge.texture.ImageRender constructor to render on a FBO rather than the default viewport.

width

The width in pixel of the FBO

Type:

integer

height

The height in pixel of the FBO

Type:

integer

color

The underlying OpenGL bind code of the texture object that holds the rendered image, 0 if the FBO is using RenderBuffer. The choice between RenderBuffer and Texture is determined by the target argument of offScreenCreate().

Type:

integer

Functions

bge.render.getWindowWidth()

Gets the width of the window (in pixels)

Return type:

integer

bge.render.getWindowHeight()

Gets the height of the window (in pixels)

Return type:

integer

bge.render.setWindowSize(width, height)

Set the width and height of the window (in pixels). This also works for fullscreen applications.

Note

Only works in the standalone player, not the Blender-embedded player.

Parameters:
  • width (integer) width in pixels

  • height (integer) height in pixels

bge.render.setFullScreen(enable)

Set whether or not the window should be fullscreen.

Note

Only works in the standalone player, not the Blender-embedded player.

Parameters:

enable (bool) True to set full screen, False to set windowed.

bge.render.getFullScreen()

Returns whether or not the window is fullscreen.

Note

Only works in the standalone player, not the Blender-embedded player; there it always returns False.

Return type:

bool

bge.render.getDisplayDimensions()

Get the display dimensions, in pixels, of the display (e.g., the monitor). Can return the size of the entire view, so the combination of all monitors; for example, (3840, 1080) for two side-by-side 1080p monitors.

Return type:

tuple (width, height)

bge.render.makeScreenshot(filename)

Writes an image file with the current displayed frame.

The image is written to filename. The path may be absolute (eg. /home/foo/image) or relative when started with // (eg. //image). Note that absolute paths are not portable between platforms. If the filename contains a #, it will be replaced by an incremental index so that screenshots can be taken multiple times without overwriting the previous ones (eg. image-#).

Settings for the image are taken from the render settings (file format and respective settings, gamma and colospace conversion, etc). The image resolution matches the framebuffer, meaning, the window size and aspect ratio. When running from the standalone player, instead of the embedded player, only PNG files are supported. Additional color conversions are also not supported.

Parameters:

filename (string) path and name of the file to write

bge.render.enableVisibility(visible)

Deprecated; doesnt do anything.

bge.render.showMouse(visible)

Enables or disables the operating system mouse cursor.

Parameters:

visible (boolean)

bge.render.setMousePosition(x, y)

Sets the mouse cursor position.

Parameters:
  • x (integer) X-coordinate in screen pixel coordinates.

  • y (integer) Y-coordinate in screen pixel coordinates.

bge.render.setBackgroundColor(rgba)

Deprecated and no longer functional. Use bge.types.KX_WorldInfo.backgroundColor() instead.

bge.render.setEyeSeparation(eyesep)

Sets the eye separation for stereo mode. Usually Focal Length/30 provides a confortable value.

Parameters:

eyesep (float) The distance between the left and right eye.

bge.render.getEyeSeparation()

Gets the current eye separation for stereo mode.

Return type:

float

bge.render.setFocalLength(focallength)

Sets the focal length for stereo mode. It uses the current camera focal length as initial value.

Parameters:

focallength (float) The focal length.

bge.render.getFocalLength()

Gets the current focal length for stereo mode.

Return type:

float

bge.render.getStereoEye()

Gets the current stereoscopy eye being rendered. This function is mainly used in a bge.types.KX_Scene.pre_draw callback function to customize the camera projection matrices for each stereoscopic eye.

Return type:

LEFT_EYE, RIGHT_EYE

bge.render.setMaterialMode(mode)

Set the material mode to use for OpenGL rendering.

Parameters:

mode (KX_TEXFACE_MATERIAL, KX_BLENDER_MULTITEX_MATERIAL, KX_BLENDER_GLSL_MATERIAL) material mode

Note

Changes will only affect newly created scenes.

bge.render.getMaterialMode(mode)

Get the material mode to use for OpenGL rendering.

Return type:

KX_TEXFACE_MATERIAL, KX_BLENDER_MULTITEX_MATERIAL, KX_BLENDER_GLSL_MATERIAL

bge.render.setGLSLMaterialSetting(setting, enable)

Enables or disables a GLSL material setting.

Parameters:
  • setting (string (lights, shaders, shadows, ramps, nodes, extra_textures))

  • enable (boolean)

bge.render.getGLSLMaterialSetting(setting)

Get the state of a GLSL material setting.

Parameters:

setting (string (lights, shaders, shadows, ramps, nodes, extra_textures))

Return type:

boolean

bge.render.setAnisotropicFiltering(level)

Set the anisotropic filtering level for textures.

Parameters:

level (integer (must be one of 1, 2, 4, 8, 16)) The new anisotropic filtering level to use

Note

Changing this value can cause all textures to be recreated, which can be slow.

bge.render.getAnisotropicFiltering()

Get the anisotropic filtering level used for textures.

Return type:

integer (one of 1, 2, 4, 8, 16)

bge.render.setMipmapping(value)

Change how to use mipmapping.

Note

Changing this value can cause all textures to be recreated, which can be slow.

bge.render.getMipmapping()

Get the current mipmapping setting.

Return type:

RAS_MIPMAP_NONE, RAS_MIPMAP_NEAREST, RAS_MIPMAP_LINEAR

bge.render.drawLine(fromVec, toVec, color)

Draw a line in the 3D scene.

Parameters:
  • fromVec (list [x, y, z]) the origin of the line

  • toVec (list [x, y, z]) the end of the line

  • color (list [r, g, b]) the color of the line

bge.render.enableMotionBlur(factor)

Enable the motion blur effect.

Parameters:

factor (float [0.0 - 1.0]) the ammount of motion blur to display.

bge.render.disableMotionBlur()

Disable the motion blur effect.

bge.render.showFramerate(enable)

Show or hide the framerate.

Parameters:

enable (boolean)

bge.render.showProfile(enable)

Show or hide the profile.

Parameters:

enable (boolean)

bge.render.showProperties(enable)

Show or hide the debug properties.

Parameters:

enable (boolean)

bge.render.autoDebugList(enable)

Enable or disable auto adding debug properties to the debug list.

Parameters:

enable (boolean)

bge.render.clearDebugList()

Clears the debug property list.

bge.render.setVsync(value)

Set the vsync value

Parameters:

value (integer) One of VSYNC_OFF, VSYNC_ON, VSYNC_ADAPTIVE

bge.render.getVsync()

Get the current vsync value

Return type:

One of VSYNC_OFF, VSYNC_ON, VSYNC_ADAPTIVE

bge.render.offScreenCreate(width,height[,samples=0][,target=bge.render.RAS_OFS_RENDER_BUFFER])

Create a Off-screen render buffer object.

Parameters:
  • width (integer) the width of the buffer in pixels

  • height (integer) the height of the buffer in pixels

  • samples (integer) the number of multisample for anti-aliasing (MSAA), 0 to disable MSAA

  • target (integer) the pixel storage: RAS_OFS_RENDER_BUFFER to render on RenderBuffers (the default), RAS_OFS_RENDER_TEXTURE to render on texture. The later is interesting if you want to access the texture directly (see RASOffScreen.color). Otherwise the default is preferable as its more widely supported by GPUs and more efficient. If the GPU does not support MSAA+Texture (e.g. Intel HD GPU), MSAA will be disabled.

Return type:

RASOffScreen