001/*
002 * Renderer 4. 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         int i = 0;
050         for (final Vertex v : model.vertexList)
051         {
052            System.err.printf("%s: vIndex = %3d, %s\n", stage, i, v.toString());
053            ++i;
054         }
055      }
056   }
057
058
059   /**
060      This method prints a {@link String} representation of the given
061      {@link Model}'s {@link Color} list.
062
063      @param stage  name for the pipeline stage
064      @param model  the {@link Model} whose {@link Color} list is to be printed
065   */
066   public static void logColorList(final String stage, final Model model)
067   {
068      if (debugScene || debugPosition)
069      {
070         int i = 0;
071         for (final Color c : model.colorList)
072         {
073            System.err.printf("%s: cIndex = %3d, %s\n", stage, i, c.toString());
074            ++i;
075         }
076      }
077   }
078
079
080   /**
081      This method prints a {@link String} representation of the given
082      {@link Model}'s {@link Primitive} list.
083
084      @param stage  name for the pipeline stage
085      @param model  the {@link Model} whose primitive list is to be printed
086   */
087   public static void logPrimitiveList(final String stage, final Model model)
088   {
089      if (debugScene || debugPosition)
090      {
091         if ( model.primitiveList.isEmpty() )
092         {
093            System.err.printf("%s: []\n", stage);
094         }
095         else
096         {
097            for (final Primitive p : model.primitiveList)
098            {
099               System.err.printf("%s: %s\n", stage, p.toString());
100            }
101         }
102      }
103   }
104
105
106   /**
107      This method prints a {@link String} representation of the given
108      {@link Primitive}.
109
110      @param stage  name for the pipeline stage
111      @param model  {@link Model} that the {@link Primitive} {@code ls} comes from
112      @param p      {@link Primitive} whose string representation is to be printed
113   */
114   public static void logPrimitive(final String stage,
115                                   final Model model,
116                                   final Primitive p)
117   {
118      if (debugScene || debugPosition)
119      {
120         System.err.printf("%s: %s\n", stage, p.toString());
121         for (final int vIndex : p.vIndexList)
122         {
123            final Vertex v = model.vertexList.get(vIndex);
124            System.err.printf("   vIndex = %3d, %s\n", vIndex, v.toString());
125         }
126         for (final int cIndex : p.cIndexList)
127         {
128            final Color c = model.colorList.get(cIndex);
129            System.err.printf("   cIndex = %3d, %s\n", cIndex, c.toString());
130         }
131      }
132   }
133
134
135   /**
136      This method prints a {@link String} representation of the given pixel
137      from a point that is being rasterized.
138
139      @param clippedMessage  {@link String} specifying if the pixel was clipped or not
140      @param x_pp   horizontal coordinate of the pixel in the pixel-plane
141      @param y_pp   vertical coordinate of the pixel in the pixel-plane
142      @param x_vp   horizontal coordinate of the pixel in the viewport
143      @param y_vp   vertical coordinate of the pixel in the viewport
144      @param r      red component of the pixel's {@link Color}
145      @param g      green component of the pixel's {@link Color}
146      @param b      blue component of the pixel's {@link Color}
147      @param vp     {@link FrameBuffer.Viewport} that the pixel is being placed in
148   */
149   public static void logPixel(final String clippedMessage,
150                               final double x_pp, final double y_pp,
151                               final int    x_vp, final int    y_vp,
152                               final double r, final double g, final double b,
153                               final FrameBuffer.Viewport vp)
154   {
155      if (debugScene || debugPosition)
156      {
157         final int wVP = vp.getWidthVP();
158         final int hVP = vp.getHeightVP();
159         final int xVP = vp.vp_ul_x;
160         final int yVP = vp.vp_ul_y;
161         final FrameBuffer fb = vp.getFrameBuffer();
162         final int wFB = fb.getWidthFB();
163         final int hFB = fb.getHeightFB();
164         System.err.print(clippedMessage);
165         System.err.printf(
166         "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\n",
167              wFB, hFB,      xVP,   yVP,   wVP, hVP,    x_pp,       y_pp,         x_vp,     y_vp,      r,     g,     b);
168      }
169   }
170
171
172   /**
173      This method prints a {@link String} representation of the given pixel
174      from a "horizontal" line that is being rasterized along the x-axis.
175
176      @param clippedMessage  {@link String} specifying if the pixel was clipped or not
177      @param x_pp   horizontal coordinate of the pixel in the pixel-plane
178      @param y_pp   vertical coordinate of the pixel in the pixel-plane
179      @param x_vp   horizontal coordinate of the pixel in the {@link FrameBuffer.Viewport}
180      @param y_vp   vertical coordinate of the pixel in the {@link FrameBuffer.Viewport}
181      @param r      red component of the pixel's {@link Color}
182      @param g      green component of the pixel's {@link Color}
183      @param b      blue component of the pixel's {@link Color}
184      @param vp     {@link FrameBuffer.Viewport} that the pixel is being placed in
185   */
186   public static void logPixel(final String clippedMessage,
187                               final int x_pp, final double y_pp,
188                               final int x_vp, final int    y_vp,
189                               final float r, final float g, final float b,
190                               final FrameBuffer.Viewport vp)
191   {
192      if (debugScene || debugPosition)
193      {
194         final int wVP = vp.getWidthVP();
195         final int hVP = vp.getHeightVP();
196         final int xVP = vp.vp_ul_x;
197         final int yVP = vp.vp_ul_y;
198         final FrameBuffer fb = vp.getFrameBuffer();
199         final int wFB = fb.getWidthFB();
200         final int hFB = fb.getHeightFB();
201         System.err.print(clippedMessage);
202         System.err.printf(
203            "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",
204                 wFB, hFB,      xVP,   yVP,   wVP, hVP,    x_pp,     y_pp,         x_vp,     y_vp,      r,     g,     b);
205      }
206   }
207
208
209   /**
210      This method prints a {@link String} representation of the given pixel
211      from a "vertical" line that is being rasterized along the y-axis.
212
213      @param clippedMessage  {@link String} specifying if the pixel was clipped or not
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 String clippedMessage,
224                               final double x_pp, final int y_pp,
225                               final int    x_vp, final int y_vp,
226                               final float r, final float g, final float b,
227                               final FrameBuffer.Viewport vp)
228   {
229      if (debugScene || debugPosition)
230      {
231         final int wVP = vp.getWidthVP();
232         final int hVP = vp.getHeightVP();
233         final int xVP = vp.vp_ul_x;
234         final int yVP = vp.vp_ul_y;
235         final FrameBuffer fb = vp.getFrameBuffer();
236         final int wFB = fb.getWidthFB();
237         final int hFB = fb.getHeightFB();
238         System.err.print(clippedMessage);
239         System.err.printf(
240            "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",
241                 wFB, hFB,      xVP,   yVP,   wVP, hVP,    x_pp,       y_pp,       x_vp,     y_vp,      r,     g,     b);
242      }
243   }
244
245
246   /**
247      Log two anti-aliased pixels from a "horizontal" line
248      that is being rasterized along the x-axis.
249
250      @param clippedMessage  {@link String} specifying if the pixel was clipped or not
251      @param x_pp   horizontal coordinate of the pixel in the pixel-plane
252      @param y_pp   vertical coordinate of the pixel in the pixel-plane
253      @param x_vp   horizontal coordinate of the anti-aliased pixels in the {@link FrameBuffer.Viewport}
254      @param y1_vp  vertical coordinate of the first anti-aliased pixel in the {@link FrameBuffer.Viewport}
255      @param y2_vp  vertical coordinate of the second anti-aliased pixel in the {@link FrameBuffer.Viewport}
256      @param r1     red component of the first anti-aliased pixel's {@link Color}
257      @param g1     green component of the first anti-aliased pixel's {@link Color}
258      @param b1     blue component of the first anti-aliased pixel's {@link Color}
259      @param r2     red component of the second anti-aliased pixel's {@link Color}
260      @param g2     green component of the second anti-aliased pixel's {@link Color}
261      @param b2     blue component of the second anti-aliased pixel's {@link Color}
262      @param vp     {@link FrameBuffer.Viewport} that the pixel is being placed in
263   */
264   public static void logPixelsAA(final String clippedMessage,
265                                  final int x_pp, final double y_pp,
266                                  final int x_vp, final int y1_vp, final int y2_vp,
267                                  final float r1, final float g1, final float b1,
268                                  final float r2, final float g2, final float b2,
269                                  final FrameBuffer.Viewport vp)
270   {
271      if (debugScene || debugPosition)
272      {
273         final int wVP = vp.getWidthVP();
274         final int hVP = vp.getHeightVP();
275         final int xVP = vp.vp_ul_x;
276         final int yVP = vp.vp_ul_y;
277         final FrameBuffer fb = vp.getFrameBuffer();
278         final int wFB = fb.getWidthFB();
279         final int hFB = fb.getHeightFB();
280         System.err.print(clippedMessage);
281         System.err.printf(
282         "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",
283              wFB, hFB,      xVP,   yVP,   wVP, hVP,    x_pp,     y_pp,        x_vp,     y1_vp,    r1,    g1,    b1,      y2_vp,    r2,    g2,    b2);
284      }
285   }
286
287
288   /**
289      Log two anti-aliased pixels from a "vertical" line
290      that is being rasterized along the y-axis.
291
292      @param clippedMessage  {@link String} specifying if the pixel was clipped or not
293      @param x_pp   horizontal coordinate of the pixel in the pixel-plane
294      @param y_pp   vertical coordinate of the pixel in the pixel-plane
295      @param x1_vp  horizontal coordinate of the first anti-aliased pixel in the {@link FrameBuffer.Viewport}
296      @param x2_vp  horizontal coordinate of the second anti-aliased pixel in the {@link FrameBuffer.Viewport}
297      @param y_vp   vertical coordinate of the anti-aliased pixels in the {@link FrameBuffer.Viewport}
298      @param r1     red component of the first anti-aliased pixel's {@link Color}
299      @param g1     green component of the first anti-aliased pixel's {@link Color}
300      @param b1     blue component of the first anti-aliased pixel's {@link Color}
301      @param r2     red component of the second anti-aliased pixel's {@link Color}
302      @param g2     green component of the second anti-aliased pixel's {@link Color}
303      @param b2     blue component of the second anti-aliased pixel's {@link Color}
304      @param vp     {@link FrameBuffer.Viewport} that the pixel is being placed in
305   */
306   public static void logPixelsAA(final String clippedMessage,
307                                  final double x_pp, final int y_pp,
308                                  final int x1_vp, final int x2_vp, final int y_vp,
309                                  final float r1, final float g1, final float b1,
310                                  final float r2, final float g2, final float b2,
311                                  final FrameBuffer.Viewport vp)
312   {
313      if (debugScene || debugPosition)
314      {
315         final int wVP = vp.getWidthVP();
316         final int hVP = vp.getHeightVP();
317         final int xVP = vp.vp_ul_x;
318         final int yVP = vp.vp_ul_y;
319         final FrameBuffer fb = vp.getFrameBuffer();
320         final int wFB = fb.getWidthFB();
321         final int hFB = fb.getHeightFB();
322         System.err.print(clippedMessage);
323         System.err.printf(
324         "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",
325              wFB, hFB,      xVP,   yVP,   wVP, hVP,    x_pp,       y_pp,      y_vp,     x1_vp,    r1,    g1,    b1,      x2_vp,    r2,    g2,    b2);
326      }
327   }
328
329
330
331   // Private default constructor to enforce noninstantiable class.
332   // See Item 4 in "Effective Java", 3rdd Ed, Joshua Bloch.
333   private PipelineLogger() {
334      throw new AssertionError();
335   }
336}