Report problems to ATLAS LXR Team (with time and IP address indicated)

The LXR Cross Referencer

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Architecture: linux ]
Version: head ] [ nightly ] [ GaudiDev ]
  Links to LXR source navigation pages for stable releases [ 12.*.* ]   [ 13.*.* ]   [ 14.*.* ] 

001 /**
002  *
003  * This class is an implementation of Graphics2D (although it actually subclasses
004  * AAbstractGraphics2D). It wraps a GLAutoDrawable object to allow Swing based
005  * components to paint and have their calls translated into OpenGL calls on the
006  * underlying context.
007  * 
008  * Obviously emulating all the functionality of Graphics2D is a challenge
009  * so expect some visual issues initially...
010  * 
011  * @author Adam Davison
012  */
013 
014 package atlantis.canvas;
015 
016 import atlantis.graphics.AAbstractGraphics2D;
017 import com.sun.opengl.util.BufferUtil;
018 import java.awt.*;
019 import java.awt.RenderingHints.Key;
020 import java.awt.color.ColorSpace;
021 import java.awt.geom.AffineTransform;
022 import java.awt.geom.PathIterator;
023 import java.awt.geom.Rectangle2D;
024 import java.awt.image.BufferedImage;
025 import java.awt.image.ComponentColorModel;
026 import java.awt.image.DataBuffer;
027 import java.awt.image.DataBufferByte;
028 import java.awt.image.ImageObserver;
029 import java.awt.image.Raster;
030 import java.awt.image.WritableRaster;
031 import java.nio.ByteBuffer;
032 import java.nio.IntBuffer;
033 import java.text.AttributedCharacterIterator;
034 import javax.media.opengl.*;
035 
036 public class AGLGraphics extends AAbstractGraphics2D {
037 
038     private int m_clipx = 0;
039     private int m_clipy = 0;
040     private int m_clipw = 0;
041     private int m_cliph = 0;
042     private Color m_color = Color.BLACK;
043     private GLAutoDrawable m_d;
044     private GL m_gl;
045     private int m_depth = 1;
046     private ComponentColorModel m_colorModel =
047             new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
048             new int[]{8, 8, 8, 8}, true, false, ComponentColorModel.TRANSLUCENT,
049             DataBuffer.TYPE_BYTE);
050 
051     public AGLGraphics(AGLGraphics old) {
052         m_d = old.m_d;
053         m_gl = m_d.getGL();
054     }
055 
056     /*
057      * Draw shapes, don't actually implement cubic or quadratic curves yet.
058      * For now we only draw straight lines.
059      * Having said that Atlantis uses such a huge number of segments that this
060      * doesn't seem to be an issue for visual quality as of yet...
061      */
062     @Override
063     public void draw(Shape z) {
064         PathIterator pi = z.getPathIterator(null);
065         float[] c = new float[6];
066         float xpos = 0.0f;
067         float ypos = 0.0f;
068         while (!pi.isDone()) {
069             int type = pi.currentSegment(c);
070             switch (type) {
071                 case PathIterator.SEG_MOVETO:
072                     xpos = c[0];
073                     ypos = c[1];
074                     break;
075                 case PathIterator.SEG_CLOSE:
076                     // Ignoring close lines for now I guess...
077                     break;
078                 case PathIterator.SEG_CUBICTO:
079                     drawLine(c[2], c[3], c[4], c[5]);
080                 case PathIterator.SEG_QUADTO:
081                     drawLine(c[0], c[1], c[2], c[3]);
082                 case PathIterator.SEG_LINETO:
083                     // Just do lines...
084                     drawLine(xpos, ypos, c[0], c[1]);
085                     xpos = c[0];
086                     ypos = c[1];
087                     break;
088             }
089             pi.next();
090         }
091     //super.draw(z);
092     }
093 
094     public AGLGraphics(GLAutoDrawable d) {
095         m_d = d;
096         m_gl = m_d.getGL();
097     }
098 
099     /*
100      * This implementation of create()/dispose() is highly thread unsafe
101      * Since it's not trivial to duplicate the GL context we do the best we can
102      * and hopefully it's ok for Atlantis.
103      * 
104      * Essentially where Java2D would create a whole new object which would then
105      * have it's own state, we push the modelview stack. This means that
106      * we can perform transformations and then pop the modelview stack in dispose
107      * to emulate this functionality.
108      * 
109      * Obviously if you call create twice in two separate threads then one of
110      * you is destroying the other one's modelview matrix...
111      */
112     @Override
113     public Graphics create() {
114         m_gl.glMatrixMode(GL.GL_MODELVIEW_MATRIX);
115         m_gl.glPushMatrix();
116         m_depth++;
117         //System.err.println("PUSH: " + depth);
118         //return new AGLGraphics(this); //?????
119         return this;
120     }
121 
122     @Override
123     public void translate(int x, int y) {
124         m_gl.glMatrixMode(GL.GL_MODELVIEW_MATRIX);
125         m_gl.glTranslatef(x, y, 0.0f);
126         return;
127     }
128 
129     @Override
130     public Color getColor() {
131         return m_color;
132     }
133 
134     @Override
135     public void setColor(Color c) {
136         m_color = c;
137         //System.out.println(c.toString());
138         float r = ((float) c.getRed()) / 255.0f;
139         float g = ((float) c.getGreen()) / 255.0f;
140         float b = ((float) c.getBlue()) / 255.0f;
141         float a = ((float) c.getAlpha()) / 255.0f;
142         m_d.getGL().glColor4f(r, g, b, a);
143     }
144 
145     @Override
146     public void setPaintMode() {
147         throw new UnsupportedOperationException("Not supported yet.");
148     }
149 
150     @Override
151     public void setXORMode(Color c1) {
152         throw new UnsupportedOperationException("Not supported yet.");
153     }
154     
155     private Font m_font = Font.decode("Arial-BOLD-18");
156 
157     @Override
158     public Font getFont() {
159         return m_font;
160     }
161 
162     @Override
163     public void setFont(Font font) {
164         m_font = font;
165     }
166 
167     @Override
168     public FontMetrics getFontMetrics(Font f) {
169         throw new UnsupportedOperationException("Not supported yet.");
170     }
171 
172     @Override
173     public Rectangle getClipBounds() {
174         return new Rectangle(m_clipx, m_clipy, m_clipw, m_cliph);
175     }
176 
177     @Override
178     public void clipRect(int x, int y, int width, int height) {
179         throw new UnsupportedOperationException("Not supported yet.");
180     }
181 
182     /*
183      * Sort of ignoring clip here...
184      */
185     @Override
186     public void setClip(int x, int y, int width, int height) {
187         m_clipx = x;
188         m_clipy = y;
189         m_clipw = width;
190         m_cliph = height;
191     //System.out.println("Who cares about clip for now??");
192     //throw new UnsupportedOperationException("Not supported yet.");
193     }
194 
195     @Override
196     public Shape getClip() {
197         throw new UnsupportedOperationException("Not supported yet.");
198     }
199 
200     @Override
201     public void setClip(Shape clip) {
202         throw new UnsupportedOperationException("Not supported yet.");
203     }
204 
205     @Override
206     public void copyArea(int x, int y, int width, int height, int dx, int dy) {
207         throw new UnsupportedOperationException("Not supported yet.");
208     }
209 
210     @Override
211     public void drawLine(int x1, int y1, int x2, int y2) {
212         drawLine((float) x1, (float) y1, (float) x2, (float) y2);
213     }
214 
215     public void drawLine(float x1, float y1, float x2, float y2) {
216         m_gl.glBegin(m_gl.GL_LINES);
217         m_gl.glVertex2f(x1, y1);
218         m_gl.glVertex2f(x2, y2);
219         m_gl.glEnd();
220     }
221 
222     @Override
223     public void fillRect(int x, int y, int width, int height) {
224         GL gl = m_d.getGL();
225 
226         gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL);
227 
228         gl.glBegin(gl.GL_QUADS);
229         gl.glVertex2f(x, y);
230         gl.glVertex2f(x + width, y);
231         gl.glVertex2f(x + width, y + height);
232         gl.glVertex2f(x, y + height);
233         gl.glEnd();
234     }
235 
236     @Override
237     public void clearRect(int x, int y, int width, int height) {
238         throw new UnsupportedOperationException("Not supported yet.");
239     }
240 
241     @Override
242     public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
243         throw new UnsupportedOperationException("Not supported yet.");
244     }
245 
246     @Override
247     public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
248         throw new UnsupportedOperationException("Not supported yet.");
249     }
250 
251     @Override
252     public void drawOval(int x, int y, int width, int height) {
253         throw new UnsupportedOperationException("Not supported yet.");
254 
255     }
256 
257     @Override
258     public void fillOval(int x, int y, int width, int height) {
259         //throw new UnsupportedOperationException("Not supported yet.");
260         return;
261     }
262 
263     @Override
264     public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
265         throw new UnsupportedOperationException("Not supported yet.");
266     }
267 
268     @Override
269     public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
270         throw new UnsupportedOperationException("Not supported yet.");
271     }
272 
273     @Override
274     public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
275         throw new UnsupportedOperationException("Not supported yet.");
276     }
277 
278     @Override
279     public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
280         m_gl.glPolygonMode(m_gl.GL_FRONT_AND_BACK, m_gl.GL_LINE);
281 
282         m_gl.glBegin(m_gl.GL_POLYGON);
283 
284         for (int i = 0; i < nPoints; i++) {
285             m_gl.glVertex2f(xPoints[i], yPoints[i]);
286         }
287 
288         m_gl.glEnd();
289     }
290 
291     public void fillPolygon(double[] xPoints, double[] yPoints, int nPoints) {
292 
293         m_gl.glPolygonMode(m_gl.GL_FRONT_AND_BACK, m_gl.GL_FILL);
294 
295         m_gl.glBegin(m_gl.GL_POLYGON);
296 
297         for (int i = 0; i < nPoints; i++) {
298             m_gl.glVertex2d(xPoints[i], yPoints[i]);
299         }
300 
301         m_gl.glEnd();
302     }
303 
304     /*
305      * Bear in mind that although Java2D is capable of filling arbitrary
306      * polygons, OpenGL/graphics cards in general use a very simple fill
307      * algorithm which is only guaranteed to be valid for convex polygons
308      * If you want to render a concave polygon you have to tessellate it down
309      * into convex ones.
310      * 
311      * So far I've been able to do this by modifying the way geometry is
312      * represented but you can also have a go at tessellating automatically
313      * using GLUT if you really need to...
314      */
315     @Override
316     public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
317         
318         // Prototype code for vertex arrays, actually slower for single polys
319         // but will definitely improve importance if we start passing in big
320         // arrays of polygons, say for complex geometry
321         if (false) {
322             IntBuffer ib = BufferUtil.newIntBuffer(nPoints * 2);
323             for (int i = 0; i < nPoints; i++) {
324                 ib.put(xPoints[i]);
325                 ib.put(yPoints[i]);
326             }
327             ib.rewind();
328             m_gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
329             m_gl.glVertexPointer(2, GL.GL_INT, 0, ib);
330             m_gl.glDrawArrays(GL.GL_POLYGON, 0, nPoints);
331         } else {
332             // Draw the simple way
333             
334             m_gl.glPolygonMode(m_gl.GL_FRONT_AND_BACK, m_gl.GL_FILL);
335             m_gl.glBegin(m_gl.GL_POLYGON);
336 
337             for (int i = 0; i < nPoints; i++) {
338                 m_gl.glVertex2f(xPoints[i], yPoints[i]);
339             }
340 
341             m_gl.glEnd();
342         }
343     }
344 
345     @Override
346     public Stroke getStroke() {
347         return null;
348     }
349 
350     @Override
351     public void setStroke(Stroke z) {
352     //super.setStroke(z);
353     }
354 
355     @Override
356     public void drawString(String str, int x, int y) {
357         //FontRenderContext frc = new FontRenderContext(null, true, true);
358         //GlyphVector gv = m_font.layoutGlyphVector(frc, str.toCharArray(),
359         //        0, str.length(), Font.LAYOUT_LEFT_TO_RIGHT);
360         //int ng = gv.getNumGlyphs();
361         //Rectangle2D r = gv.getVisualBounds();
362         //m_gl.glColor3f(1.0f, 0.0f, 0.0f);
363         //this.fillRect((int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight());
364         //this.setColor(m_color);
365                 
366         WritableRaster test = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
367                 32, 32, 4, null);
368         BufferedImage testi = new BufferedImage(m_colorModel, test, false, null);
369         Graphics testg = testi.getGraphics();
370         FontMetrics fm = testg.getFontMetrics(m_font);
371         Rectangle2D r = fm.getStringBounds(str, testg);
372                 
373         WritableRaster raster =
374                 Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
375                 (int)r.getWidth(), (int)r.getHeight(), 4, null);
376 
377         BufferedImage bi = new BufferedImage(m_colorModel, raster, false, null);
378         
379         Graphics big = bi.getGraphics();
380         //big.setColor(Color.GREEN);
381         //big.fillRect(0, 0, bi.getWidth(), bi.getHeight());
382         big.setColor(m_color);
383         big.setFont(m_font);
384         big.drawString(str, -(int)r.getX(), -(int)r.getY());
385         drawImage(bi, x, (int)(y - r.getHeight()), null);
386     }
387 
388     @Override
389     public void drawString(AttributedCharacterIterator iterator, int x, int y) {
390     //throw new UnsupportedOperationException("Not supported yet.");
391     }
392 
393     @Override
394     public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
395         WritableRaster raster =
396                 Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
397                 img.getWidth(observer), img.getHeight(observer), 4, null);
398 
399         BufferedImage bi = new BufferedImage(m_colorModel, raster, false, null);
400 
401         Graphics2D g = bi.createGraphics();
402         //g.setColor(Color.WHITE);
403         //g.drawLine(0, 0, bi.getWidth(), bi.getHeight());
404         AffineTransform gt = new AffineTransform();
405         gt.translate(0, img.getHeight(observer));
406         gt.scale(1, -1d);
407         g.transform(gt);
408         g.drawImage(img, null, null);
409 
410         DataBufferByte imgbuf = (DataBufferByte) raster.getDataBuffer();
411 
412         m_gl.glRasterPos2i(x, y + bi.getHeight());
413         //System.out.println(img.getWidth(observer) + ":" + img.getHeight(observer));
414         m_gl.glDrawPixels(img.getWidth(observer), img.getHeight(observer),
415                 m_gl.GL_RGBA, m_gl.GL_UNSIGNED_BYTE, ByteBuffer.wrap(imgbuf.getData()));
416 
417         //System.out.println("Ignoring drawImage for now...");
418         return true;
419     //throw new UnsupportedOperationException("Not supported yet.");
420     }
421 
422     @Override
423     public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) {
424         throw new UnsupportedOperationException("Not supported yet.");
425     }
426 
427     @Override
428     public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) {
429         throw new UnsupportedOperationException("Not supported yet.");
430     }
431 
432     @Override
433     public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
434         throw new UnsupportedOperationException("Not supported yet.");
435     }
436 
437     @Override
438     public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) {
439         // Needed to render JComponent backgrounds
440         //throw new UnsupportedOperationException("Not supported yet.");
441         return true;
442     }
443 
444     @Override
445     public Object getRenderingHint(Key z) {
446         return null;
447     }
448 
449     @Override
450     public void setRenderingHint(Key a, Object z) {
451         return;
452     }
453 
454     @Override
455     public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) {
456         throw new UnsupportedOperationException("Not supported yet.");
457     }
458 
459     @Override
460     public void dispose() {
461         m_depth--;
462         if (m_depth == 0) {
463             m_depth = 1;
464         // This happens when the object is really finally destroyed...
465         // When this is actually happening if we pop we'll cause a crash...
466         //System.err.println("DISPOSE CALLED TWICE ON ONE OBJECT!!!");
467         //(new Exception()).printStackTrace();
468         } else {
469             m_gl.glMatrixMode(GL.GL_MODELVIEW_MATRIX);
470             m_gl.glPopMatrix();
471         }
472         return;
473     // We didn't create so no need to dispose... probably bad...
474     //throw new UnsupportedOperationException("Not supported yet.");
475     }
476 
477     @Override
478     public void rotate(double a, double b, double z) {
479         super.rotate(a, b, z);
480     }
481 
482     @Override
483     public void rotate(double z) {
484         m_gl.glMatrixMode(GL.GL_MODELVIEW_MATRIX);
485         m_gl.glRotated(z * 360 / (2 * Math.PI), 0.0, 0.0, 1.0);
486     }
487 }

source navigation ] diff markup ] identifier search ] general search ]

Due to the LXR bug, the updates fail sometimes to remove references to deleted files. The Saturday's full rebuilds fix these problems
This page was automatically generated by the LXR engine. Valid HTML 4.01!