/src/gosu/ImageDrawOp.java

http://jgosu.googlecode.com/ · Java · 212 lines · 149 code · 42 blank · 21 comment · 19 complexity · d3feb6a8db09e4b57ea712af42ff26f8 MD5 · raw file

  1. package gosu;
  2. import java.awt.geom.Point2D;
  3. import com.sun.opengl.util.texture.Texture;
  4. import com.sun.opengl.util.texture.TextureCoords;
  5. import javax.media.opengl.GL;
  6. import javax.media.opengl.glu.GLU;
  7. public class ImageDrawOp extends DrawOp {
  8. private Image _image;
  9. private Point2D.Double[] _points;
  10. private double _z;
  11. private java.awt.Color[] _colors;
  12. private int _mode;
  13. public ImageDrawOp(Image image, double x, double y, double z) {
  14. this(image, x, y, z, image.getWidth(), image.getHeight(), java.awt.Color.white, Image.DEFAULT);
  15. }
  16. public ImageDrawOp(Image image, double x, double y, double z, double factorX, double factorY) {
  17. this(image, x, y, z, (int) (image.getWidth() * factorX), (int) (image.getHeight() * factorY), java.awt.Color.white, Image.DEFAULT);
  18. }
  19. public ImageDrawOp(Image image, double x, double y, double z, double factorX, double factorY, Color color, int mode) {
  20. this(image, x, y, z, (int) (image.getWidth() * factorX), (int) (image.getHeight() * factorY), color.getAWTColor(), mode);
  21. }
  22. protected ImageDrawOp(Image image, double x, double y, double z, int width, int height, java.awt.Color color, int mode) {
  23. _image = image;
  24. _points = new Point2D.Double[4];
  25. _points[0] = new Point2D.Double(x, y);
  26. _points[1] = new Point2D.Double(x + width, y);
  27. _points[2] = new Point2D.Double(x + width, y + height);
  28. _points[3] = new Point2D.Double(x, y + height);
  29. _z = z;
  30. _mode = mode;
  31. _colors = new java.awt.Color[] {color, color, color, color};
  32. }
  33. public ImageDrawOp(Image image, double x1, double y1, Color c1,
  34. double x2, double y2, Color c2,
  35. double x3, double y3, Color c3,
  36. double x4, double y4, Color c4,
  37. double z, int mode) {
  38. _image = image;
  39. _points = new Point2D.Double[] {new Point2D.Double(x1, y1),
  40. new Point2D.Double(x2, y2),
  41. new Point2D.Double(x4, y4),
  42. new Point2D.Double(x3, y3)};
  43. _z = z;
  44. _mode = mode;
  45. _colors = new java.awt.Color[] {c1.getAWTColor(), c2.getAWTColor(),
  46. c4.getAWTColor(), c3.getAWTColor()};
  47. }
  48. /**
  49. * Draws a line.
  50. */
  51. public ImageDrawOp(double x1, double y1, Color c1, double x2, double y2, Color c2) {
  52. this(x1, y1, c1, x2, y2, c2, 0, Image.DEFAULT);
  53. }
  54. /**
  55. * Draws a line.
  56. */
  57. public ImageDrawOp(double x1, double y1, Color c1, double x2, double y2, Color c2, double z, int mode) {
  58. _points = new Point2D.Double[] {new Point2D.Double(x1, y1), new Point2D.Double(x2, y2)};
  59. _colors = new java.awt.Color[] {c1.getAWTColor(), c2.getAWTColor()};
  60. _z = z;
  61. _mode = mode;
  62. }
  63. /**
  64. * Draws a triangle.
  65. */
  66. public ImageDrawOp(double x1, double y1, Color c1,
  67. double x2, double y2, Color c2,
  68. double x3, double y3, Color c3) {
  69. this(x1, y1, c1, x2, y2, c2, x3, y3, c3, 0, Image.DEFAULT);
  70. }
  71. /**
  72. * Draws a triangle.
  73. */
  74. public ImageDrawOp(double x1, double y1, Color c1,
  75. double x2, double y2, Color c2,
  76. double x3, double y3, Color c3, double z, int mode) {
  77. _points = new Point2D.Double[] {new Point2D.Double(x1, y1),
  78. new Point2D.Double(x2, y2),
  79. new Point2D.Double(x3, y3)};
  80. _colors = new java.awt.Color[] {c1.getAWTColor(),
  81. c2.getAWTColor(),
  82. c3.getAWTColor()};
  83. _z = z;
  84. _mode = mode;
  85. }
  86. /**
  87. * Draws a quad.
  88. */
  89. public ImageDrawOp(double x1, double y1, Color c1,
  90. double x2, double y2, Color c2,
  91. double x3, double y3, Color c3,
  92. double x4, double y4, Color c4) {
  93. this(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, 0, Image.DEFAULT);
  94. }
  95. /**
  96. * Draws a quad.
  97. */
  98. public ImageDrawOp(double x1, double y1, Color c1,
  99. double x2, double y2, Color c2,
  100. double x3, double y3, Color c3,
  101. double x4, double y4, Color c4, double z, int mode) {
  102. _points = new Point2D.Double[] {new Point2D.Double(x1, y1),
  103. new Point2D.Double(x2, y2),
  104. new Point2D.Double(x4, y4),
  105. new Point2D.Double(x3, y3)};
  106. _colors = new java.awt.Color[] {c1.getAWTColor(),
  107. c2.getAWTColor(),
  108. c4.getAWTColor(),
  109. c3.getAWTColor()};
  110. _z = z;
  111. _mode = mode;
  112. }
  113. public double getZ() { return _z; }
  114. public void execute() {
  115. GL gl = GLU.getCurrentGL();
  116. if (_mode == Image.ADDITIVE) {
  117. gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE);
  118. } else {
  119. gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA);
  120. }
  121. Texture texture = null;
  122. TextureCoords coords = null;
  123. if (_image != null) {
  124. texture = _image.getTexture();
  125. texture.enable();
  126. texture.bind();
  127. //texture.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
  128. //texture.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
  129. if (_image.hasSoftBorders()) {
  130. coords = texture.getSubImageTexCoords(1, 1, _image.getWidth() + 1, _image.getHeight() + 1);
  131. } else {
  132. coords = texture.getImageTexCoords();
  133. }
  134. }
  135. if (_points.length == 2) {
  136. gl.glBegin(GL.GL_LINES);
  137. } else if (_points.length == 3) {
  138. gl.glBegin(GL.GL_TRIANGLES);
  139. } else { // 4
  140. gl.glBegin(GL.GL_QUADS);
  141. }
  142. for (int i = 0; i < _points.length; i++) {
  143. // JOGL textures have premultiplied colors
  144. float a = _colors[i].getAlpha() / 255.0f;
  145. float r = a * _colors[i].getRed() / 255.0f;
  146. float g = a * _colors[i].getGreen() / 255.0f;
  147. float b = a * _colors[i].getBlue() / 255.0f;
  148. gl.glColor4f(r, g, b, a);
  149. if (texture != null) {
  150. switch (i) {
  151. case 0:
  152. gl.glTexCoord2f(coords.left(), coords.top());
  153. break;
  154. case 1:
  155. gl.glTexCoord2f(coords.right(), coords.top());
  156. break;
  157. case 2:
  158. gl.glTexCoord2f(coords.right(), coords.bottom());
  159. break;
  160. case 3:
  161. gl.glTexCoord2f(coords.left(), coords.bottom());
  162. break;
  163. }
  164. }
  165. gl.glVertex2d(_points[i].x, _points[i].y);
  166. }
  167. gl.glEnd();
  168. if (texture != null) texture.disable();
  169. }
  170. }