Advertisement
Guest User

Untitled

a guest
Oct 21st, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.03 KB | None | 0 0
  1. package com.starshock;
  2.  
  3. import java.io.File;
  4. import java.nio.ByteBuffer;
  5. import java.nio.ByteOrder;
  6. import java.nio.IntBuffer;
  7.  
  8. import org.lwjgl.LWJGLException;
  9. import org.lwjgl.LWJGLUtil;
  10. import org.lwjgl.Sys;
  11. import org.lwjgl.opengl.Display;
  12. import org.lwjgl.opengl.DisplayMode;
  13. import static org.lwjgl.opengl.GL11.*;
  14. import static org.lwjgl.opengl.GL14.*;
  15. import static org.lwjgl.opengl.GL30.*;
  16. import static org.lwjgl.util.glu.GLU.*;
  17.  
  18. public class Main {
  19.  
  20.     static {
  21.         System.setProperty("org.lwjgl.librarypath",
  22.                 new File(new File(System.getProperty("user.dir"), "native"),
  23.                         LWJGLUtil.getPlatformName()).getAbsolutePath());
  24.     }
  25.  
  26.     public static final int S_WIDTH = 320, S_HEIGHT = 200, S_SCALE = 2;
  27.  
  28.     public long lastFrame, lastFPS;
  29.     public int fps;
  30.     public int defaultFBO = 0, drawFBO, drawFBOTexture, drawFBOBuffer;
  31.  
  32.     public void start() {
  33.         try {
  34.             Display.setDisplayMode(new DisplayMode(S_WIDTH * S_SCALE, S_HEIGHT
  35.                     * S_SCALE));
  36.             Display.setTitle("Starhack - FPS: " + fps);
  37.             Display.create();
  38.         } catch (LWJGLException e) {
  39.             e.printStackTrace();
  40.             System.exit(0);
  41.         }
  42.  
  43.         initFBO();
  44.  
  45.         getDelta();
  46.         lastFPS = getTime();
  47.  
  48.         while (!Display.isCloseRequested()) {
  49.             int delta = getDelta();
  50.  
  51.             update(delta);
  52.             renderGL();
  53.  
  54.             Display.update();
  55.             Display.sync(60);
  56.         }
  57.  
  58.         Display.destroy();
  59.     }
  60.  
  61.     public void update(int delta) {
  62.         updateFPS();
  63.     }
  64.  
  65.     public int getDelta() {
  66.         long time = getTime();
  67.         int delta = (int) (time - lastFrame);
  68.         lastFrame = time;
  69.  
  70.         return delta;
  71.     }
  72.  
  73.     public long getTime() {
  74.         return (Sys.getTime() * 1000) / Sys.getTimerResolution();
  75.     }
  76.  
  77.     public void updateFPS() {
  78.         if (getTime() - lastFPS > 1000) {
  79.             Display.setTitle("Starhack - FPS: " + fps);
  80.             fps = 0;
  81.             lastFPS += 1000;
  82.         }
  83.         fps++;
  84.     }
  85.  
  86.     public void initFBO() {
  87.         drawFBO = glGenFramebuffers();
  88.         drawFBOTexture = glGenTextures();
  89.         drawFBOBuffer = glGenRenderbuffers();
  90.  
  91.         glBindFramebuffer(GL_FRAMEBUFFER, drawFBO);
  92.  
  93.         glBindTexture(GL_TEXTURE_2D, drawFBOTexture);
  94.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  95.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  96.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, S_WIDTH, S_HEIGHT, 0,GL_RGBA, GL_INT, (java.nio.ByteBuffer) null);
  97.         glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, drawFBOTexture, 0);
  98.  
  99.  
  100.         glBindRenderbuffer(GL_RENDERBUFFER, drawFBOBuffer);
  101.         glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, S_WIDTH, S_HEIGHT);
  102.         glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER, drawFBOBuffer);
  103.  
  104.         glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO); 
  105.     }
  106.  
  107.     public void renderGL() {
  108.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  109.         glBindTexture(GL_TEXTURE_2D, 0);
  110.         glBindFramebuffer(GL_FRAMEBUFFER, drawFBO);
  111.         ready3D();
  112.         render3D();
  113.         ready2D();
  114.         render2D();
  115.         renderScaledFBO();
  116.     }
  117.  
  118.     public void ready3D() {
  119.         glViewport(0, 0, S_WIDTH, S_HEIGHT);
  120.         glMatrixMode(GL_PROJECTION);
  121.  
  122.         glLoadIdentity();
  123.         gluPerspective(45f, (float) S_WIDTH / S_HEIGHT, 0.0f, 5000.0f);
  124.  
  125.         glMatrixMode(GL_MODELVIEW);
  126.         glLoadIdentity();
  127.  
  128.         glDepthFunc(GL_LEQUAL);
  129.         glEnable(GL_DEPTH_TEST);
  130.     }
  131.  
  132.     public void render3D() {
  133.         glTranslatef(0, 0, -6f);
  134.         glBegin(GL_QUADS);
  135.         glVertex3f(0, 0, 0);
  136.         glVertex3f(2, 0, 0);
  137.         glVertex3f(2, 2, 0);
  138.         glVertex3f(0, 2, 0);
  139.         glEnd();
  140.     }
  141.  
  142.     public void ready2D() {
  143.         glMatrixMode(GL_PROJECTION);
  144.         glLoadIdentity();
  145.  
  146.         gluOrtho2D(0.0f, S_WIDTH, S_HEIGHT, 0.0f);
  147.  
  148.         glMatrixMode(GL_MODELVIEW);
  149.         glLoadIdentity();
  150.         glTranslatef(0.375f, 0.375f, 0.0f);
  151.  
  152.         glDisable(GL_DEPTH_TEST);
  153.     }
  154.  
  155.     public void render2D() {
  156.     }
  157.  
  158.     public void renderScaledFBO() {
  159.         glViewport(0, 0, S_WIDTH * S_SCALE, S_HEIGHT * S_SCALE);
  160.         glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
  161.         glBlitFramebuffer(0, 0, S_WIDTH, S_HEIGHT, 0, 0, S_WIDTH * S_SCALE,
  162.                 S_HEIGHT * S_SCALE, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  163.     }
  164.  
  165.     public static void main(String[] argv) {
  166.         Main main = new Main();
  167.         main.start();
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement