Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package demominer.gui.drawer;
- import demominer.UI.component.Component;
- import demominer.UI.component.Container;
- import demominer.resources.Resources;
- import demominer.sprite.SpriteImage;
- import demominer.sprite.SpriteSheet;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.IntBuffer;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import org.lwjgl.opengl.EXTFramebufferObject;
- import org.lwjgl.opengl.GL11;
- /**
- *
- * @author Szoltomi
- */
- public class DrawerMethods extends Thread {
- /**
- * Initializes OpenGL.
- *
- * Called before the start of the drawing thread.
- * Only basic OpenGL calls, shouldn't need explanation.
- *
- * @param width the width of the window
- * @param height the height of the window
- */
- public static void initDraw(int width, int height) {
- try {
- Display.setDisplayMode(new DisplayMode(width, height));
- Display.create();
- } catch (LWJGLException e) {
- e.printStackTrace();
- System.exit(0);
- }
- GL11.glMatrixMode(GL11.GL_PROJECTION);
- GL11.glLoadIdentity();
- GL11.glOrtho(0, width, 0, height, -1000, 1000);
- GL11.glMatrixMode(GL11.GL_MODELVIEW);
- GL11.glEnable(GL11.GL_BLEND);
- GL11.glEnable(GL11.GL_TEXTURE_2D);
- GL11.glDisable(GL11.GL_CULL_FACE);
- GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
- }
- /**
- * Generates a texture-bound framebuffer object.
- *
- * @param width The width of the FBO and texture
- * @param height The height of the FBO and texture
- * @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.
- */
- public static int[] initFBOTexture(int width, int height) {
- //Most of the code is copied out from a tutorial, and I can partially understand it.
- IntBuffer buffer = ByteBuffer.allocateDirect(1 * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
- EXTFramebufferObject.glGenFramebuffersEXT(buffer);
- int FBOId = buffer.get();
- EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, FBOId);
- //setting up depthbuffer
- int depthbuffer = EXTFramebufferObject.glGenRenderbuffersEXT();
- EXTFramebufferObject.glBindRenderbufferEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, depthbuffer);
- EXTFramebufferObject.glRenderbufferStorageEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT,
- GL11.GL_DEPTH_COMPONENT,
- width, height);
- EXTFramebufferObject.glFramebufferRenderbufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
- EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT,
- EXTFramebufferObject.GL_RENDERBUFFER_EXT, depthbuffer);
- //setting up color buffer
- int texId;
- texId = GL11.glGenTextures();
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
- GL11.glTexImage2D(GL11.GL_TEXTURE_2D,
- 0,
- GL11.GL_RGBA8,
- width, height,
- 0,
- GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (IntBuffer) null);
- EXTFramebufferObject.glFramebufferTexture2DEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
- EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT,
- GL11.GL_TEXTURE_2D,
- texId, 0);
- EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
- //Completeness check.
- int framebuffer = EXTFramebufferObject.glCheckFramebufferStatusEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT);
- switch (framebuffer) {
- case EXTFramebufferObject.GL_FRAMEBUFFER_COMPLETE_EXT:
- break;
- case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
- throw new RuntimeException("FrameBuffer: " + FBOId
- + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT exception");
- case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
- throw new RuntimeException("FrameBuffer: " + FBOId
- + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT exception");
- case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
- throw new RuntimeException("FrameBuffer: " + FBOId
- + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT exception");
- case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
- throw new RuntimeException("FrameBuffer: " + FBOId
- + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT exception");
- case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
- throw new RuntimeException("FrameBuffer: " + FBOId
- + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT exception");
- case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
- throw new RuntimeException("FrameBuffer: " + FBOId
- + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT exception");
- default:
- throw new RuntimeException("Unexpected reply from glCheckFramebufferStatusEXT: " + framebuffer);
- }
- return new int[]{FBOId, texId};
- }
- /**
- * Binds a texture-bound FBO and the texture the FBO is bound to.
- *
- * The array has to contain the two elements returned with initFBOTexture(int width, int height).
- * It binds the FBO and texture, so that further drawing will be done on these.
- *
- * @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.
- */
- public static void bindFBO(int[] fbo) {
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo[1]);
- EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, fbo[0]);
- }
- /**
- * Unbinds the currently bound FBO, so that further drawing will occur on the window.
- */
- public static void unbindFBO() {
- EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
- }
- /**
- * Sets the current color and alpha.
- * @param red
- * @param green
- * @param blue
- * @param alpha
- */
- public static void setColor(double red, double green, double blue, double alpha) {
- GL11.glColor4d(red, green, blue, alpha);
- }
- /**
- * Method for simple calling of GL11.glEnd();
- */
- public static void drawEnd() {
- GL11.glEnd();
- }
- /**
- * Clears the screen.
- */
- public static void clear() {
- GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
- }
- /**
- * Checks if any OpenGL errors have occured, prints it out if did.
- */
- public static void checkErrors() {
- int error = GL11.glGetError();
- if (error != 0) {
- System.out.println(error);
- }
- }
- /**
- * Translates and zooms the modelview.
- *
- * @param x translation horizontally, in pixels.
- * @param y translation vertically, in pixels.
- * @param zoom Magnification multiplicator. 1.0 means no magnification
- */
- public static void viewMatrix(long x, long y, double zoom) {
- GL11.glMatrixMode(GL11.GL_MODELVIEW);
- GL11.glLoadIdentity();
- GL11.glScaled(zoom, zoom, 0f);
- GL11.glTranslated(x, y, 0);
- }
- /**
- * Zooms the modelview in our out.
- *
- * @param Magnification multiplicator. 1.0 means no magnification
- */
- public static void viewZoom(double zoom) {
- GL11.glMatrixMode(GL11.GL_MODELVIEW);
- GL11.glLoadIdentity();
- GL11.glScaled(zoom, zoom, 0f);
- }
- /**
- * Flips the modelview upside down. Currently unused.
- */
- public static void flip() {
- GL11.glScaled(1f, -1f, 0f);
- }
- /**
- * Translates the modelview.
- * @param x translation horizontally, in pixels.
- * @param y translation vertically, in pixels.
- */
- public static void viewTranslation(long x, long y) {
- GL11.glMatrixMode(GL11.GL_MODELVIEW);
- GL11.glLoadIdentity();
- GL11.glTranslated(x, y, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement