Hardware Based Rendering of 3D Geometry

Create Hardware and Render a 3D Geometry

To render a 3D geometry, a renderer, a render target and a camera are required. None of them can work without each other.

  • Renderer - The context that manages the graphics hardware, created by the create_renderer method of the Renderer class. The Python API uses the Vulkan graphics API.
  • Render Target - Defines where the pixels are rendered to, for example a render texture created by the render factory of the renderer.
  • Camera - Defines the position and direction the scene is rendered from, usually attached to a node in the scene.

RenderParameters class configures the render target, like double buffering, color/depth/stencil bits. The create_viewport method of the render target creates a viewport that renders the scene from a camera’s perspective.

Programming Sample for Hardware Rendering

This code example creates a hardware renderer and renders a scene with a cube into a render texture.

from aspose.threed import Scene
from aspose.threed.render import Renderer, RenderParameters
from aspose.threed.entities import Camera, Box
from aspose.threed.utilities import Vector3, RelativeRectangle

# Create a scene with a cube
scene = Scene()
cube = scene.root_node.create_child_node("cube", Box().to_mesh())

# Create a camera
camera = Camera()
scene.root_node.create_child_node("camera", camera)
camera.parent_node.transform.translation = Vector3(5.0, 5.0, 5.0)
camera.look_at = Vector3(0.0, 0.0, 0.0)

# Create the hardware renderer, the renderer uses the Vulkan API
renderer = Renderer.create_renderer()

# Create a render texture as the render target
render_parameters = RenderParameters(True, 32, 24, 8)
render_texture = renderer.render_factory.create_render_texture(render_parameters, 1024, 1024)
# Create a viewport that renders the scene from the camera's perspective
viewport = render_texture.create_viewport(camera, RelativeRectangle(0, 0, 1024, 1024))
# Render the scene into the render texture
renderer.render(render_texture)

Use a Custom Shader

The Python API renderer is based on Vulkan, so custom shaders are provided in SPIR-V format through the SPIRVSource class. GLSL sources can be compiled to SPIR-V binaries with tools like glslangValidator, and the compiled vertex and fragment shaders are then assigned to a SPIRVSource instance.

VertexDeclaration class defines the input format used by the vertex shader, and the create_pipeline method of the render factory creates a preconfigured graphics pipeline from the shader, a RenderState, the vertex declaration and a draw operation.

from aspose.threed.render import Renderer, SPIRVSource, RenderState, DrawOperation, PolygonMode
from aspose.threed.utilities import VertexDeclaration, VertexFieldDataType, VertexFieldSemantic

# Create the renderer
renderer = Renderer.create_renderer()

# The GLSL sources must be compiled to SPIR-V binaries first, e.g. with glslangValidator:
#   glslangValidator -V grid.vert -o grid.vert.spv
#   glslangValidator -V grid.frag -o grid.frag.spv
#
# grid.vert:
#   #version 450 core
#   layout (location = 0) in vec3 position;
#   layout(set = 0, binding = 0) uniform Matrices { mat4 mat_world_view_proj; };
#   void main()
#   {
#       gl_Position = mat_world_view_proj * vec4(position, 1.0f);
#   }
#
# grid.frag:
#   #version 450 core
#   layout(location = 0) out vec4 color;
#   void main()
#   {
#       color = vec4(1.0, 1.0, 1.0, 1.0);
#   }

# Load the compiled SPIR-V binaries
spirv = SPIRVSource()
with open("grid.vert.spv", "rb") as f:
    spirv.vertex_shader = f.read()
with open("grid.frag.spv", "rb") as f:
    spirv.fragment_shader = f.read()

# Define the input format used by the vertex shader
fd = VertexDeclaration()
fd.add_field(VertexFieldDataType.F_VECTOR3, VertexFieldSemantic.POSITION, -1, "position")
# Compile the shader
grid_shader = renderer.render_factory.create_shader_program(spirv)

# Render state for the grid
render_state = renderer.render_factory.create_render_state()
render_state.cull_mode = PolygonMode.NONE
render_state.depth_test = True
render_state.depth_mask = True

# Create a preconfigured graphics pipeline with the shader, render state,
# vertex declaration and draw operation
pipeline = renderer.render_factory.create_pipeline(grid_shader, render_state, fd, DrawOperation.LINES)