1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| public void begin() throws CrashHatException { if(!initQueryGet) { waitForSamples = false; IntBuffer buffer = BufferUtils.createIntBuffer(1); GL15.glGenQueries(buffer); checkID = buffer.get(0); initQueryGet = true; } if(!initialized) { GL11.glEnable(GL11.GL_ALPHA_TEST); GL11.glAlphaFunc(GL11.GL_GEQUAL, 1f); GL11.glEnable(GL11.GL_STENCIL_TEST); GL11.glColorMask(false, false, false, false); initialized = true; }else throw new CrashHatException("begin was called twice (maybe a end is missing)"); } public int intersetcs(float x1, float y1, Image image1, float x2, float y2, Image image2) throws CrashHatException { int tempPixels = -1; if (!initialized) throw new CrashHatException("collision not initialized! must call begin() before using intersets()"); if(!new Rectangle(x1,y1, image1.getWidth(), image1.getHeight()).intersects(new Rectangle(x2,y2, image2.getWidth(), image2.getHeight()))) return 0; GL11.glClear(GL11.GL_STENCIL_BUFFER_BIT); GL11.glStencilFunc(GL11.GL_ALWAYS, 1, 1); GL11.glStencilOp(GL11.GL_REPLACE, GL11.GL_REPLACE, GL11.GL_REPLACE); image1.draw(x1, y1);
GL11.glStencilFunc(GL11.GL_EQUAL, 1, 1); GL11.glStencilOp(GL11.GL_KEEP, GL11.GL_KEEP, GL11.GL_KEEP); GL15.glBeginQuery(GL15.GL_SAMPLES_PASSED, checkID);
image2.draw(x2, y2);
GL15.glEndQuery(GL15.GL_SAMPLES_PASSED); if(waitForSamples) { do { GL15.glGetQueryObject(checkID, GL15.GL_QUERY_RESULT_AVAILABLE, pixelCounts); }while(pixelCounts.get(0) == 0); GL15.glGetQueryObject(checkID, GL15.GL_QUERY_RESULT, pixelCounts); }else { GL15.glGetQueryObject(checkID, GL15.GL_QUERY_RESULT_AVAILABLE, pixelCounts); GL15.glGetQueryObject(checkID, GL15.GL_QUERY_RESULT, pixelCounts); } tempPixels = pixelCounts.get(0); return tempPixels; } public void end() throws CrashHatException { if(initialized) { GL11.glColorMask(true, true, true, true); GL11.glDisable(GL11.GL_STENCIL_TEST); GL11.glDisable(GL11.GL_ALPHA_TEST); initialized = false; }else throw new CrashHatException("end was called twice (maybe a begin is missing)"); } |