Class RenderingContext
- Direct Known Subclasses:
SegmentRenderingContext
A new RenderingContext is created whenever the view panel is resized.
During rendering, shapes use this context to:
- Access the raw pixel array (
pixels) for direct pixel manipulation - Access the
Graphics2Dcontext (graphics) for Java2D drawing - Read screen dimensions (
width,height) and thecenterCoordinatefor coordinate projection - Use the
projectionScalefactor for perspective projection
The context also manages mouse interaction detection: as shapes are painted back-to-front, each shape can report itself as the object under the mouse cursor. After painting completes, the topmost shape receives the mouse event.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intTheBufferedImagepixel format used for the rendering buffer.final Point2DCenter of the screen in screen space (pixels).Statistics for frustum culling performance tracking.Debug log buffer for capturing diagnostic output.Developer tools for this rendering context.intNumber of frame that is currently being rendered.View frustum for frustum culling.final Graphics2DJava2D graphics context for drawing text, anti-aliased shapes, and other high-level graphics operations onto the render buffer.final intHeight of the rendering area in pixels.intChunk tasks submitted during the last transform phase, across all nesting levels.Global lighting manager for the scene.doubleNear-plane distance in camera-space Z units.final intNumber of horizontal segments (bands) for parallel rendering.final int[]Pixels of the rendering area.Present gate for this framebuffer: fires when the frame currently held in this buffer has been presented to the display (or dropped from the presentation mailbox).doubleScale factor for perspective projection, derived from screen width.intMaximum X coordinate (exclusive) for rendering.final intMaximum Y coordinate (exclusive) to render.intMinimum X coordinate (inclusive) for rendering.final intMinimum Y coordinate (inclusive) to render.Which eye is being rendered in stereo mode.intX offset of the current eye's viewport within the full buffer.intWidth of the viewport for the current eye in stereo mode.final intTile columns per viewport (1 = horizontal bands only).final intTile rows per viewport.Per-frame coordinator for the non-blocking parallel transform fork.longUnique id of the current transform cycle, assigned byShapeCollection.transformShapes()from a global counter.Executor for the parallel transform phase.intWhich projection buffer slot this context writes/reads: 0, 1 or 2.final Point3DWorld-space position of the viewer for this pass, copied from the camera inShapeCollection.transformShapesBegin(eu.svjatoslav.sixth.e3d.gui.ViewPanel, eu.svjatoslav.sixth.e3d.gui.RenderingContext).final intNumber of side-by-side viewports (2 in stereo mode, else 1).final intWidth of the rendering area in pixels. -
Constructor Summary
ConstructorsModifierConstructorDescriptionRenderingContext(int width, int height, int numRenderSegments) Creates a new rendering context for full-screen rendering.RenderingContext(int width, int height, int tilesX, int tilesY, int viewportCount) Creates a new rendering context with a rectangular tile grid.RenderingContext(RenderingContext parent) Creates an independent pass context for one pipeline pass (one eye in stereo): shares the frame's pixel buffer, graphics and services, but owns the per-pass projection fields (center, scale, stereo viewport, slot, frame/cycle stamps).protectedRenderingContext(RenderingContext parent, int renderMinY, int renderMaxY) Protected constructor for creating segment views. -
Method Summary
Modifier and TypeMethodDescriptionvoiddispose()Disposes all Graphics2D resources associated with this context.voidexecuteWithGraphics(Consumer<Graphics2D> operation) Executes a graphics operation in a thread-safe manner.Returns the current object under the mouse cursor.getImage()Returns the backing image whose pixel buffer the rasterizer paints into.Returns the pending mouse event for this frame, ornullif none.getSegmentGraphics(int segmentIndex) Returns the Graphics2D context for a specific render segment.booleanHandles mouse events for components and returns whether a view repaint is needed.voidResets per-frame state in preparation for rendering a new frame.voidsetCurrentObjectUnderMouseCursor(MouseInteractionController currentObjectUnderMouseCursor) Called when given object was detected under mouse cursor, while processingmouseEvent.voidsetMouseEvent(MouseEvent mouseEvent) Sets the mouse event to be processed during this frame's rendering.
-
Field Details
-
bufferedImageType
public static final int bufferedImageTypeTheBufferedImagepixel format used for the rendering buffer. TYPE_INT_RGB provides optimal performance for Java2D blitting.- See Also:
-
numRenderSegments
public final int numRenderSegmentsNumber of horizontal segments (bands) for parallel rendering. Bands are finer than the paint thread count: paint threads steal bands off a shared ticket until all bands are done, so a thread that finishes a cheap band immediately picks up more work. Derived from the render thread count viaViewPanel.setNumRenderThreads(int).Equals
tilesX * tilesY * viewportCount: the tile grid covers one viewport, and in stereo mode a second grid covers the other eye (segment indices for the right eye start attilesX * tilesY). -
tilesX
public final int tilesXTile columns per viewport (1 = horizontal bands only). -
tilesY
public final int tilesYTile rows per viewport. -
viewportCount
public final int viewportCountNumber of side-by-side viewports (2 in stereo mode, else 1). -
graphics
Java2D graphics context for drawing text, anti-aliased shapes, and other high-level graphics operations onto the render buffer. -
pixels
public final int[] pixelsPixels of the rendering area. Each pixel is a single int in RGB format:(r << 16) | (g << 8) | b. -
width
public final int widthWidth of the rendering area in pixels. -
height
public final int heightHeight of the rendering area in pixels. -
centerCoordinate
Center of the screen in screen space (pixels). This is the point where (0,0) coordinate of the world space is rendered. -
projectionScale
public double projectionScaleScale factor for perspective projection, derived from screen width. Used to convert normalized device coordinates to screen pixels. This is mutable to support stereo rendering where each eye has a different viewport width. -
renderMinY
public final int renderMinYMinimum Y coordinate (inclusive) to render. Used for multi-threaded rendering where each thread renders a horizontal segment. -
renderMaxY
public final int renderMaxYMaximum Y coordinate (exclusive) to render. Used for multi-threaded rendering where each thread renders a horizontal segment. -
transformCycleId
public long transformCycleIdUnique id of the current transform cycle, assigned byShapeCollection.transformShapes()from a global counter. UnlikeframeNumber(per-context, can repeat across context instances), this never collides, so per-cycle memoization such as composite subtree weights can safely key on it. -
vertexSlot
public int vertexSlotWhich projection buffer slot this context writes/reads: 0, 1 or 2. Cycles per render pass (per eye in stereo) when the triple-buffered pipeline is active, so the transform phase of a pass never overwrites the vertex state either of the two previous passes' paints may still be reading. Always 0 when the pipeline is off (tests, single-pass rendering). -
nearPlaneDistance
public double nearPlaneDistanceNear-plane distance in camera-space Z units. Polygons whose vertices straddle this plane are clipped against it (new intersection vertices are generated with interpolated UVs); polygons fully behind it are culled. Must be > 0 so the perspective divide stays safe. -
frameNumber
public int frameNumberNumber of frame that is currently being rendered. Every frame has its own number. -
developerTools
Developer tools for this rendering context. Controls diagnostic features like logging and visualization. -
debugLogBuffer
Debug log buffer for capturing diagnostic output. Shapes can log messages here that appear in the Developer Tools panel. -
lightingManager
Global lighting manager for the scene. All shaded polygons use this to calculate lighting. Contains all light sources and ambient light settings for the world. -
stereoEye
Which eye is being rendered in stereo mode. NONE for normal single-view rendering. -
stereoViewportWidth
public int stereoViewportWidthWidth of the viewport for the current eye in stereo mode. Equalswidthwhen not in stereo mode. -
stereoViewportOffsetX
public int stereoViewportOffsetXX offset of the current eye's viewport within the full buffer. 0 for left eye, width/2 for right eye, 0 in normal mode. -
renderMinX
public int renderMinXMinimum X coordinate (inclusive) for rendering. In stereo mode, this isstereoViewportOffsetX. In normal mode, this is 0. -
renderMaxX
public int renderMaxXMaximum X coordinate (exclusive) for rendering. In stereo mode, this isstereoViewportOffsetX+stereoViewportWidth. In normal mode, this iswidth. -
frustum
View frustum for frustum culling. Updated each frame from camera state and screen dimensions. Shapes can test their bounding boxes against this frustum to determine if they are potentially visible before expensive vertex transformations. -
viewerPosition
World-space position of the viewer for this pass, copied from the camera inShapeCollection.transformShapesBegin(eu.svjatoslav.sixth.e3d.gui.ViewPanel, eu.svjatoslav.sixth.e3d.gui.RenderingContext). Used by BSP painter ordering (viewpoint for tree traversal). Fresh instance per context, including per-pass copies, so overlapping pipeline passes each see their own viewpoint. -
cullingStatistics
Statistics for frustum culling performance tracking. Updated each frame: total shapes counted at start, visible shapes incremented during rendering, culled composites tracked during transform. -
transformExecutor
Executor for the parallel transform phase. When non-null, composites with enough children split their render lists into chunks transformed concurrently. When null, the transform phase runs serially on the render thread. -
transformCoordinator
Per-frame coordinator for the non-blocking parallel transform fork. Set byShapeCollection.transformShapes()for the duration of the root transform whentransformExecutoris available; composites at any nesting level submit chunk tasks to it. Null outside the transform phase and when transforming serially. -
presentGate
Present gate for this framebuffer: fires when the frame currently held in this buffer has been presented to the display (or dropped from the presentation mailbox). The render thread installs a fresh gate at the start of each frame that reuses the buffer, and the frame's paint continuation awaits the PREVIOUS gate before writing pixels — without it, painting frame F+3 would overwrite the buffer while the present thread is still blitting frame F from it. -
lastTransformTaskCount
public int lastTransformTaskCountChunk tasks submitted during the last transform phase, across all nesting levels. Diagnostics: proves nested composites forked.
-
-
Constructor Details
-
RenderingContext
public RenderingContext(int width, int height, int numRenderSegments) Creates a new rendering context for full-screen rendering.Equivalent to
RenderingContext(width, height, 0, height, numRenderSegments).- Parameters:
width- the rendering area width in pixelsheight- the rendering area height in pixelsnumRenderSegments- number of parallel render segments (threads)
-
RenderingContext
public RenderingContext(int width, int height, int tilesX, int tilesY, int viewportCount) Creates a new rendering context with a rectangular tile grid.Equivalent to the band-only constructors when
tilesX == 1. In stereo mode (viewportCount == 2) each viewport gets its own tile grid; segment indices for viewport v start atv * tilesX * tilesY.- Parameters:
width- the rendering area width in pixelsheight- the rendering area height in pixelstilesX- tile columns per viewport (1 = bands only)tilesY- tile rows per viewportviewportCount- number of side-by-side viewports (2 = stereo)
-
RenderingContext
Protected constructor for creating segment views. Shares the pixel buffer and graphics context with the parent.- Parameters:
parent- the parent rendering contextrenderMinY- minimum Y coordinate (inclusive) for this segmentrenderMaxY- maximum Y coordinate (exclusive) for this segment
-
RenderingContext
Creates an independent pass context for one pipeline pass (one eye in stereo): shares the frame's pixel buffer, graphics and services, but owns the per-pass projection fields (center, scale, stereo viewport, slot, frame/cycle stamps). The next pass's setup writes to its own copy, so it cannot disturb this pass's in-flight transform chunks or its asynchronous sort/bin/paint continuation.- Parameters:
parent- the frame rendering context to copy from
-
-
Method Details
-
prepareForNewFrameRendering
public void prepareForNewFrameRendering()Resets per-frame state in preparation for rendering a new frame. Increments the frame number and clears the mouse event state. -
getImage
Returns the backing image whose pixel buffer the rasterizer paints into.Exposed for headless rendering: after a transform/sort/paint pass the image holds the finished frame and can be saved or compared directly.
- Returns:
- the backing buffered image
-
getSegmentGraphics
Returns the Graphics2D context for a specific render segment. Each segment's Graphics2D is pre-clipped to its Y bounds.- Parameters:
segmentIndex- the segment index (0 to numRenderSegments-1)- Returns:
- the Graphics2D for that segment
- Throws:
NullPointerException- if called on a segment view (not the main context)
-
dispose
public void dispose()Disposes all Graphics2D resources associated with this context. Should be called when the context is no longer needed (e.g., on resize). -
executeWithGraphics
Executes a graphics operation in a thread-safe manner. This must be used for all Graphics2D operations (text, lines, etc.) during multi-threaded rendering.- Parameters:
operation- the graphics operation to execute
-
getMouseEvent
Returns the pending mouse event for this frame, ornullif none.- Returns:
- the mouse event to process, or
null
-
setMouseEvent
Sets the mouse event to be processed during this frame's rendering.- Parameters:
mouseEvent- the mouse event with position and button information
-
setCurrentObjectUnderMouseCursor
public void setCurrentObjectUnderMouseCursor(MouseInteractionController currentObjectUnderMouseCursor) Called when given object was detected under mouse cursor, while processingmouseEvent. Because objects are rendered back to front. The last method caller will set the top-most object, if there are multiple objects under mouse cursor.- Parameters:
currentObjectUnderMouseCursor- the object that is currently under the mouse cursor
-
getCurrentObjectUnderMouseCursor
Returns the current object under the mouse cursor. Used by segment rendering to collect mouse results.- Returns:
- the current object under mouse cursor, or null
-
handlePossibleComponentMouseEvent
public boolean handlePossibleComponentMouseEvent()Handles mouse events for components and returns whether a view repaint is needed.- Returns:
trueif view update is needed as a consequence of this mouse event
-