Advertisement
szoltomi

Untitled

Aug 31st, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.16 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package demominer.gui.drawer;
  6.  
  7. import demominer.UI.component.Component;
  8. import demominer.UI.component.Container;
  9. import demominer.resources.Resources;
  10. import demominer.sprite.SpriteImage;
  11. import demominer.sprite.SpriteSheet;
  12. import java.nio.ByteBuffer;
  13. import java.nio.ByteOrder;
  14. import java.nio.IntBuffer;
  15.  
  16.  
  17. import org.lwjgl.LWJGLException;
  18. import org.lwjgl.opengl.Display;
  19. import org.lwjgl.opengl.DisplayMode;
  20. import org.lwjgl.opengl.EXTFramebufferObject;
  21. import org.lwjgl.opengl.GL11;
  22.  
  23. /**
  24.  *
  25.  * @author Szoltomi
  26.  */
  27. public class DrawerMethods extends Thread {
  28.  
  29.     /**
  30.      * Initializes OpenGL.
  31.      *
  32.      * Called before the start of the drawing thread.
  33.      * Only basic OpenGL calls, shouldn't need explanation.
  34.      *
  35.      * @param width the width of the window
  36.      * @param height the height of the window
  37.      */
  38.     public static void initDraw(int width, int height) {
  39.         try {
  40.             Display.setDisplayMode(new DisplayMode(width, height));
  41.             Display.create();
  42.         } catch (LWJGLException e) {
  43.             e.printStackTrace();
  44.             System.exit(0);
  45.         }
  46.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  47.         GL11.glLoadIdentity();
  48.         GL11.glOrtho(0, width, 0, height, -1000, 1000);
  49.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  50.         GL11.glEnable(GL11.GL_BLEND);
  51.         GL11.glEnable(GL11.GL_TEXTURE_2D);
  52.         GL11.glDisable(GL11.GL_CULL_FACE);
  53.         GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
  54.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
  55.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
  56.     }
  57.  
  58.     /**
  59.      * Generates a texture-bound framebuffer object.
  60.      *
  61.      * @param width The width of the FBO and texture
  62.      * @param height The height of the FBO and texture
  63.      * @return An array with the ID of the FBO and the texture. The first element is the ID of the FBO, the second is the ID of the texture.
  64.      */
  65.     public static int[] initFBOTexture(int width, int height) {
  66.         //Most of the code is copied out from a tutorial, and I can partially understand it.
  67.         IntBuffer buffer = ByteBuffer.allocateDirect(1 * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
  68.         EXTFramebufferObject.glGenFramebuffersEXT(buffer);
  69.         int FBOId = buffer.get();
  70.         EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, FBOId);
  71.         //setting up depthbuffer
  72.         int depthbuffer = EXTFramebufferObject.glGenRenderbuffersEXT();
  73.         EXTFramebufferObject.glBindRenderbufferEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, depthbuffer);
  74.         EXTFramebufferObject.glRenderbufferStorageEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT,
  75.                                                       GL11.GL_DEPTH_COMPONENT,
  76.                                                       width, height);
  77.         EXTFramebufferObject.glFramebufferRenderbufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
  78.                                                           EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT,
  79.                                                           EXTFramebufferObject.GL_RENDERBUFFER_EXT, depthbuffer);
  80.         //setting up color buffer
  81.         int texId;
  82.         texId = GL11.glGenTextures();
  83.         GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
  84.         GL11.glTexImage2D(GL11.GL_TEXTURE_2D,
  85.                           0,
  86.                           GL11.GL_RGBA8,
  87.                           width, height,
  88.                           0,
  89.                           GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (IntBuffer) null);
  90.  
  91.         EXTFramebufferObject.glFramebufferTexture2DEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
  92.                                                        EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT,
  93.                                                        GL11.GL_TEXTURE_2D,
  94.                                                        texId, 0);
  95.         EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
  96.  
  97.         //Completeness check.
  98.         int framebuffer = EXTFramebufferObject.glCheckFramebufferStatusEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT);
  99.         switch (framebuffer) {
  100.             case EXTFramebufferObject.GL_FRAMEBUFFER_COMPLETE_EXT:
  101.                 break;
  102.             case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
  103.                 throw new RuntimeException("FrameBuffer: " + FBOId
  104.                                            + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT exception");
  105.             case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
  106.                 throw new RuntimeException("FrameBuffer: " + FBOId
  107.                                            + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT exception");
  108.             case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
  109.                 throw new RuntimeException("FrameBuffer: " + FBOId
  110.                                            + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT exception");
  111.             case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
  112.                 throw new RuntimeException("FrameBuffer: " + FBOId
  113.                                            + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT exception");
  114.             case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
  115.                 throw new RuntimeException("FrameBuffer: " + FBOId
  116.                                            + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT exception");
  117.             case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
  118.                 throw new RuntimeException("FrameBuffer: " + FBOId
  119.                                            + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT exception");
  120.             default:
  121.                 throw new RuntimeException("Unexpected reply from glCheckFramebufferStatusEXT: " + framebuffer);
  122.         }
  123.         return new int[]{FBOId, texId};
  124.  
  125.     }
  126.  
  127.     /**
  128.      * Binds a texture-bound FBO and the texture the FBO is bound to.
  129.      *
  130.      * The array has to contain the two elements returned with initFBOTexture(int width, int height).
  131.      * It binds the FBO and texture, so that further drawing will be done on these.
  132.      *
  133.      * @param fbo An array with two elements: The first is the ID of the FBO, the second is the ID of the texture which the FBO is bound to.
  134.      */
  135.     public static void bindFBO(int[] fbo) {
  136.         GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo[1]);
  137.         EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, fbo[0]);
  138.     }
  139.  
  140.     /**
  141.      * Unbinds the currently bound FBO, so that further drawing will occur on the window.
  142.      */
  143.     public static void unbindFBO() {
  144.         EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
  145.     }
  146.  
  147.     /**
  148.      * Sets the current color and alpha.
  149.      * @param red
  150.      * @param green
  151.      * @param blue
  152.      * @param alpha
  153.      */
  154.     public static void setColor(double red, double green, double blue, double alpha) {
  155.         GL11.glColor4d(red, green, blue, alpha);
  156.     }
  157.  
  158.     /**
  159.      * Method for simple calling of GL11.glEnd();
  160.      */
  161.     public static void drawEnd() {
  162.         GL11.glEnd();
  163.     }
  164.  
  165.     /**
  166.      * Clears the screen.
  167.      */
  168.     public static void clear() {
  169.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
  170.     }
  171.  
  172.     /**
  173.      * Checks if any OpenGL errors have occured, prints it out if did.
  174.      */
  175.     public static void checkErrors() {
  176.         int error = GL11.glGetError();
  177.         if (error != 0) {
  178.             System.out.println(error);
  179.         }
  180.     }
  181.    
  182.         /**
  183.      * Translates and zooms the modelview.
  184.      *
  185.      * @param x translation horizontally, in pixels.
  186.      * @param y translation vertically, in pixels.
  187.      * @param zoom Magnification multiplicator. 1.0 means no magnification
  188.      */
  189.     public static void viewMatrix(long x, long y, double zoom) {
  190.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  191.         GL11.glLoadIdentity();
  192.         GL11.glScaled(zoom, zoom, 0f);
  193.         GL11.glTranslated(x, y, 0);
  194.     }
  195.  
  196.     /**
  197.      * Zooms the modelview in our out.
  198.      *
  199.      * @param Magnification multiplicator. 1.0 means no magnification
  200.      */
  201.     public static void viewZoom(double zoom) {
  202.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  203.         GL11.glLoadIdentity();
  204.         GL11.glScaled(zoom, zoom, 0f);
  205.     }
  206.  
  207.     /**
  208.      * Flips the modelview upside down. Currently unused.
  209.      */
  210.     public static void flip() {
  211.         GL11.glScaled(1f, -1f, 0f);
  212.     }
  213.  
  214.     /**
  215.      * Translates the modelview.
  216.      * @param x translation horizontally, in pixels.
  217.      * @param y translation vertically, in pixels.
  218.      */
  219.     public static void viewTranslation(long x, long y) {
  220.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  221.         GL11.glLoadIdentity();
  222.         GL11.glTranslated(x, y, 0);
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement