001/* 002 * Renderer 7. The MIT License. 003 * Copyright (c) 2022 rlkraft@pnw.edu 004 * See LICENSE for details. 005*/ 006 007package renderer.pipeline; 008 009import renderer.scene.*; 010import renderer.scene.primitives.*; 011import renderer.framebuffer.*; 012 013import java.awt.Color; 014 015/** 016 Methods used by the pipeline stages to log information. 017*/ 018public class PipelineLogger 019{ 020 public static boolean debugScene = false; 021 public static boolean debugPosition = false; 022 023 /** 024 Use this logger's debug variables to determine 025 if the given message should be printed to stderr. 026 027 @param message {@link String} to output to stderr 028 */ 029 public static void logMessage(final String message) 030 { 031 if (debugScene || debugPosition) 032 { 033 System.err.println( message ); 034 } 035 } 036 037 038 /** 039 This method prints a {@link String} representation of the given 040 {@link Model}'s {@link Vertex} list. 041 042 @param stage name for the pipeline stage 043 @param model the {@link Model} whose vertex list is to be printed 044 */ 045 public static void logVertexList(final String stage, final Model model) 046 { 047 if (debugScene || debugPosition) 048 { 049 if ( model.vertexList.isEmpty() ) 050 { 051 System.err.printf("%s: empty vertex list\n", stage); 052 } 053 else 054 { 055 int i = 0; 056 for (final Vertex v : model.vertexList) 057 { 058 System.err.printf("%s: vIndex = %3d, %s\n", stage, i, v.toString()); 059 ++i; 060 } 061 } 062 } 063 } 064 065 066 /** 067 This method prints a {@link String} representation of the given 068 {@link Model}'s {@link Color} list. 069 070 @param stage name for the pipeline stage 071 @param model the {@link Model} whose {@link Color} list is to be printed 072 */ 073 public static void logColorList(final String stage, final Model model) 074 { 075 if (debugScene || debugPosition) 076 { 077 if ( model.colorList.isEmpty() ) 078 { 079 System.err.printf("%s: empty color list\n", stage); 080 } 081 else 082 { 083 int i = 0; 084 for (final Color c : model.colorList) 085 { 086 System.err.printf("%s: cIndex = %3d, %s\n", stage, i, c.toString()); 087 ++i; 088 } 089 } 090 } 091 } 092 093 094 /** 095 This method prints a {@link String} representation of the given 096 {@link Model}'s {@link Primitive} list. 097 098 @param stage name for the pipeline stage 099 @param model the {@link Model} whose primitive list is to be printed 100 */ 101 public static void logPrimitiveList(final String stage, final Model model) 102 { 103 if (debugScene || debugPosition) 104 { 105 if ( model.primitiveList.isEmpty() ) 106 { 107 System.err.printf("%s: empty primitive list\n", stage); 108 } 109 else 110 { 111 for (final Primitive p : model.primitiveList) 112 { 113 System.err.printf("%s: %s\n", stage, p.toString()); 114 } 115 } 116 } 117 } 118 119 120 /** 121 This method prints a {@link String} representation of the given 122 {@link Primitive}. 123 124 @param stage name for the pipeline stage 125 @param model {@link Model} that the {@link Primitive} {@code ls} comes from 126 @param p {@link Primitive} whose string representation is to be printed 127 */ 128 public static void logPrimitive(final String stage, 129 final Model model, 130 final Primitive p) 131 { 132 if (debugScene || debugPosition) 133 { 134 System.err.printf("%s: %s\n", stage, p.toString()); 135 for (final int vIndex : p.vIndexList) 136 { 137 final Vertex v = model.vertexList.get(vIndex); 138 System.err.printf(" vIndex = %3d, %s\n", vIndex, v.toString()); 139 } 140 for (final int cIndex : p.cIndexList) 141 { 142 final Color c = model.colorList.get(cIndex); 143 System.err.printf(" cIndex = %3d, %s\n", cIndex, c.toString()); 144 } 145 } 146 } 147 148 149 /** 150 This method prints a {@link String} representation of the given pixel 151 from a point that is being rasterized. 152 153 @param clippedMessage {@link String} specifying if the pixel was clipped or not 154 @param x_pp horizontal coordinate of the pixel in the pixel-plane 155 @param y_pp vertical coordinate of the pixel in the pixel-plane 156 @param x_vp horizontal coordinate of the pixel in the viewport 157 @param y_vp vertical coordinate of the pixel in the viewport 158 @param r red component of the pixel's {@link Color} 159 @param g green component of the pixel's {@link Color} 160 @param b blue component of the pixel's {@link Color} 161 @param vp {@link FrameBuffer.Viewport} that the pixel is being placed in 162 */ 163 public static void logPixel(final String clippedMessage, 164 final double x_pp, final double y_pp, 165 final int x_vp, final int y_vp, 166 final double r, final double g, final double b, 167 final FrameBuffer.Viewport vp) 168 { 169 if (debugScene || debugPosition) 170 { 171 final int wVP = vp.getWidthVP(); 172 final int hVP = vp.getHeightVP(); 173 final int xVP = vp.vp_ul_x; 174 final int yVP = vp.vp_ul_y; 175 final FrameBuffer fb = vp.getFrameBuffer(); 176 final int wFB = fb.getWidthFB(); 177 final int hFB = fb.getHeightFB(); 178 System.err.printf( 179 "fb_[w=%d,h=%d] vp_[x=%4d, y=%4d, w=%d,h=%d] (x_pp=%9.4f, y_pp=%9.4f) (x_vp=%4d, y_vp=%4d) r=%.4f g=%.4f b=%.4f", 180 wFB, hFB, xVP, yVP, wVP, hVP, x_pp, y_pp, x_vp, y_vp, r, g, b); 181 System.err.println(clippedMessage); 182 183 } 184 } 185 186 187 /** 188 This method prints a {@link String} representation of the given pixel 189 from a point that is being rasterized. 190 191 @param x_pp horizontal coordinate of the pixel in the pixel-plane 192 @param y_pp vertical coordinate of the pixel in the pixel-plane 193 @param x_vp horizontal coordinate of the pixel in the viewport 194 @param y_vp vertical coordinate of the pixel in the viewport 195 @param r red component of the pixel's {@link Color} 196 @param g green component of the pixel's {@link Color} 197 @param b blue component of the pixel's {@link Color} 198 @param vp {@link FrameBuffer.Viewport} that the pixel is being placed in 199 */ 200 public static void logPixel(final double x_pp, final double y_pp, 201 final int x_vp, final int y_vp, 202 final double r, final double g, final double b, 203 final FrameBuffer.Viewport vp) 204 { 205 // Call the previous version of logPixel() with an empty clippedMessage. 206 logPixel("", x_pp, y_pp, x_vp, y_vp, r, g, b, vp); 207 } 208 209 210 /** 211 This method prints a {@link String} representation of the given pixel 212 from a "horizontal" line that is being rasterized along the x-axis. 213 214 @param x_pp horizontal coordinate of the pixel in the pixel-plane 215 @param y_pp vertical coordinate of the pixel in the pixel-plane 216 @param x_vp horizontal coordinate of the pixel in the {@link FrameBuffer.Viewport} 217 @param y_vp vertical coordinate of the pixel in the {@link FrameBuffer.Viewport} 218 @param r red component of the pixel's {@link Color} 219 @param g green component of the pixel's {@link Color} 220 @param b blue component of the pixel's {@link Color} 221 @param vp {@link FrameBuffer.Viewport} that the pixel is being placed in 222 */ 223 public static void logPixel(final int x_pp, final double y_pp, 224 final int x_vp, final int y_vp, 225 final float r, final float g, final float b, 226 final FrameBuffer.Viewport vp) 227 { 228 if (debugScene || debugPosition) 229 { 230 final int wVP = vp.getWidthVP(); 231 final int hVP = vp.getHeightVP(); 232 final int xVP = vp.vp_ul_x; 233 final int yVP = vp.vp_ul_y; 234 final FrameBuffer fb = vp.getFrameBuffer(); 235 final int wFB = fb.getWidthFB(); 236 final int hFB = fb.getHeightFB(); 237 System.err.printf( 238 "fb_[w=%d,h=%d] vp_[x=%4d, y=%4d, w=%d,h=%d] (x_pp=%4d, y_pp=%9.4f) (x_vp=%4d, y_vp=%4d) r=%.4f g=%.4f b=%.4f\n", 239 wFB, hFB, xVP, yVP, wVP, hVP, x_pp, y_pp, x_vp, y_vp, r, g, b); 240 } 241 } 242 243 244 /** 245 This method prints a {@link String} representation of the given pixel 246 from a "vertical" line that is being rasterized along the y-axis. 247 248 @param x_pp horizontal coordinate of the pixel in the pixel-plane 249 @param y_pp vertical coordinate of the pixel in the pixel-plane 250 @param x_vp horizontal coordinate of the pixel in the {@link FrameBuffer.Viewport} 251 @param y_vp vertical coordinate of the pixel in the {@link FrameBuffer.Viewport} 252 @param r red component of the pixel's {@link Color} 253 @param g green component of the pixel's {@link Color} 254 @param b blue component of the pixel's {@link Color} 255 @param vp {@link FrameBuffer.Viewport} that the pixel is being placed in 256 */ 257 public static void logPixel(final double x_pp, final int y_pp, 258 final int x_vp, final int y_vp, 259 final float r, final float g, final float b, 260 final FrameBuffer.Viewport vp) 261 { 262 if (debugScene || debugPosition) 263 { 264 final int wVP = vp.getWidthVP(); 265 final int hVP = vp.getHeightVP(); 266 final int xVP = vp.vp_ul_x; 267 final int yVP = vp.vp_ul_y; 268 final FrameBuffer fb = vp.getFrameBuffer(); 269 final int wFB = fb.getWidthFB(); 270 final int hFB = fb.getHeightFB(); 271 System.err.printf( 272 "fb_[w=%d,h=%d] vp_[x=%4d, y=%4d, w=%d,h=%d] (x_pp=%9.4f, y_pp=%4d) (x_vp=%4d, y_vp=%4d) r=%.4f g=%.4f b=%.4f\n", 273 wFB, hFB, xVP, yVP, wVP, hVP, x_pp, y_pp, x_vp, y_vp, r, g, b); 274 } 275 } 276 277 278 /** 279 Log two anti-aliased pixels from a "horizontal" line 280 that is being rasterized along the x-axis. 281 282 @param x_pp horizontal coordinate of the pixel in the pixel-plane 283 @param y_pp vertical coordinate of the pixel in the pixel-plane 284 @param x_vp horizontal coordinate of the anti-aliased pixels in the {@link FrameBuffer.Viewport} 285 @param y1_vp vertical coordinate of the first anti-aliased pixel in the {@link FrameBuffer.Viewport} 286 @param y2_vp vertical coordinate of the second anti-aliased pixel in the {@link FrameBuffer.Viewport} 287 @param r1 red component of the first anti-aliased pixel's {@link Color} 288 @param g1 green component of the first anti-aliased pixel's {@link Color} 289 @param b1 blue component of the first anti-aliased pixel's {@link Color} 290 @param r2 red component of the second anti-aliased pixel's {@link Color} 291 @param g2 green component of the second anti-aliased pixel's {@link Color} 292 @param b2 blue component of the second anti-aliased pixel's {@link Color} 293 @param vp {@link FrameBuffer.Viewport} that the pixel is being placed in 294 */ 295 public static void logPixelsAA(final int x_pp, final double y_pp, 296 final int x_vp, final int y1_vp, final int y2_vp, 297 final float r1, final float g1, final float b1, 298 final float r2, final float g2, final float b2, 299 final FrameBuffer.Viewport vp) 300 { 301 if (debugScene || debugPosition) 302 { 303 final int wVP = vp.getWidthVP(); 304 final int hVP = vp.getHeightVP(); 305 final int xVP = vp.vp_ul_x; 306 final int yVP = vp.vp_ul_y; 307 final FrameBuffer fb = vp.getFrameBuffer(); 308 final int wFB = fb.getWidthFB(); 309 final int hFB = fb.getHeightFB(); 310 System.err.printf( 311 "fb_[w=%d,h=%d] vp_[x=%4d, y=%4d, w=%d,h=%d] (x_pp=%4d, y_pp=%9.4f) x_vp=%4d {y_low=%4d r=%.4f g=%.4f b=%.4f} {y_hi =%4d r=%.4f g=%.4f b=%.4f}\n", 312 wFB, hFB, xVP, yVP, wVP, hVP, x_pp, y_pp, x_vp, y1_vp, r1, g1, b1, y2_vp, r2, g2, b2); 313 } 314 } 315 316 317 /** 318 Log two anti-aliased pixels from a "vertical" line 319 that is being rasterized along the y-axis. 320 321 @param x_pp horizontal coordinate of the pixel in the pixel-plane 322 @param y_pp vertical coordinate of the pixel in the pixel-plane 323 @param x1_vp horizontal coordinate of the first anti-aliased pixel in the {@link FrameBuffer.Viewport} 324 @param x2_vp horizontal coordinate of the second anti-aliased pixel in the {@link FrameBuffer.Viewport} 325 @param y_vp vertical coordinate of the anti-aliased pixels in the {@link FrameBuffer.Viewport} 326 @param r1 red component of the first anti-aliased pixel's {@link Color} 327 @param g1 green component of the first anti-aliased pixel's {@link Color} 328 @param b1 blue component of the first anti-aliased pixel's {@link Color} 329 @param r2 red component of the second anti-aliased pixel's {@link Color} 330 @param g2 green component of the second anti-aliased pixel's {@link Color} 331 @param b2 blue component of the second anti-aliased pixel's {@link Color} 332 @param vp {@link FrameBuffer.Viewport} that the pixel is being placed in 333 */ 334 public static void logPixelsAA(final double x_pp, final int y_pp, 335 final int x1_vp, final int x2_vp, final int y_vp, 336 final float r1, final float g1, final float b1, 337 final float r2, final float g2, final float b2, 338 final FrameBuffer.Viewport vp) 339 { 340 if (debugScene || debugPosition) 341 { 342 final int wVP = vp.getWidthVP(); 343 final int hVP = vp.getHeightVP(); 344 final int xVP = vp.vp_ul_x; 345 final int yVP = vp.vp_ul_y; 346 final FrameBuffer fb = vp.getFrameBuffer(); 347 final int wFB = fb.getWidthFB(); 348 final int hFB = fb.getHeightFB(); 349 System.err.printf( 350 "fb_[w=%d,h=%d] vp_[x=%4d, y=%4d, w=%d,h=%d] (x_pp=%9.4f, y_pp=%4d) y_vp=%4d {x_low=%4d r=%.4f g=%.4f b=%.4f} {x_hi =%4d r=%.4f g=%.4f b=%.4f}\n", 351 wFB, hFB, xVP, yVP, wVP, hVP, x_pp, y_pp, y_vp, x1_vp, r1, g1, b1, x2_vp, r2, g2, b2); 352 } 353 } 354 355 356 357 // Private default constructor to enforce noninstantiable class. 358 // See Item 4 in "Effective Java", 3rd Ed, Joshua Bloch. 359 private PipelineLogger() { 360 throw new AssertionError(); 361 } 362}