Package eu.svjatoslav.sixth.e3d.gui
Class RenderingContext
java.lang.Object
eu.svjatoslav.sixth.e3d.gui.RenderingContext
- Direct Known Subclasses:
SegmentRenderingContext
Contains all state needed to render a single frame: the pixel buffer, graphics context,
screen dimensions, and mouse event tracking.
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).Debug log buffer for capturing diagnostic output.Developer tools for this rendering context.intNumber of frame that is currently being rendered.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.Global lighting manager for the scene.static final intNumber of horizontal segments for parallel rendering.final int[]Pixels of the rendering area.final doubleScale factor for perspective projection, derived from screen width.final intMaximum Y coordinate (exclusive) to render.final intMinimum Y coordinate (inclusive) to render.final intWidth of the rendering area in pixels. -
Constructor Summary
ConstructorsModifierConstructorDescriptionRenderingContext(int width, int height) Creates a new rendering context for full-screen rendering.RenderingContext(int width, int height, int renderMinY, int renderMaxY) Creates a new rendering context with Y-bounds for segment rendering.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.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:
-
NUM_RENDER_SEGMENTS
public static final int NUM_RENDER_SEGMENTSNumber of horizontal segments for parallel rendering. Each segment is rendered by a separate thread.- See Also:
-
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 final double projectionScaleScale factor for perspective projection, derived from screen width. Used to convert normalized device coordinates to screen pixels. -
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. -
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.
-
-
Constructor Details
-
RenderingContext
public RenderingContext(int width, int height) Creates a new rendering context for full-screen rendering.Equivalent to
RenderingContext(width, height, 0, height).- Parameters:
width- the rendering area width in pixelsheight- the rendering area height in pixels
-
RenderingContext
public RenderingContext(int width, int height, int renderMinY, int renderMaxY) Creates a new rendering context with Y-bounds for segment rendering.Initializes the offscreen image buffer, extracts the raw pixel byte array, and configures anti-aliasing on the Graphics2D context.
- Parameters:
width- the rendering area width in pixelsheight- the rendering area height in pixelsrenderMinY- minimum Y coordinate (inclusive) to renderrenderMaxY- maximum Y coordinate (exclusive) to render
-
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
-
-
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. -
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 NUM_RENDER_SEGMENTS-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
-