Advertisement
Guest User

Untitled

a guest
Oct 21st, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.84 KB | None | 0 0
  1. package com.starhack;
  2.  
  3. import org.lwjgl.LWJGLException;
  4. import org.lwjgl.LWJGLUtil;
  5. import org.lwjgl.opengl.Display;
  6. import org.lwjgl.opengl.DisplayMode;
  7. import org.lwjgl.opengl.GL14;
  8. import org.lwjgl.opengl.GLContext;
  9.  
  10. import static org.lwjgl.opengl.EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT;
  11. import static org.lwjgl.opengl.EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT;
  12. import static org.lwjgl.opengl.EXTFramebufferObject.GL_FRAMEBUFFER_EXT;
  13. import static org.lwjgl.opengl.EXTFramebufferObject.GL_RENDERBUFFER_EXT;
  14. import static org.lwjgl.opengl.EXTFramebufferObject.glBindFramebufferEXT;
  15. import static org.lwjgl.opengl.EXTFramebufferObject.glBindRenderbufferEXT;
  16. import static org.lwjgl.opengl.EXTFramebufferObject.glFramebufferRenderbufferEXT;
  17. import static org.lwjgl.opengl.EXTFramebufferObject.glFramebufferTexture2DEXT;
  18. import static org.lwjgl.opengl.EXTFramebufferObject.glGenFramebuffersEXT;
  19. import static org.lwjgl.opengl.EXTFramebufferObject.glGenRenderbuffersEXT;
  20. import static org.lwjgl.opengl.EXTFramebufferObject.glRenderbufferStorageEXT;
  21. import static org.lwjgl.opengl.GL11.*;
  22. import static org.lwjgl.util.glu.GLU.*;
  23. import java.io.File;
  24.  
  25. public class Main {
  26.  
  27.     private static boolean gameRunning = true;
  28.     private static int fps;
  29.     private static int colorTextureID;
  30.     private static int framebufferID;
  31.     private static int depthRenderBufferID;
  32.  
  33.     static {
  34.         System.setProperty("org.lwjgl.librarypath",
  35.                 new File(new File(System.getProperty("user.dir"), "native"),
  36.                         LWJGLUtil.getPlatformName()).getAbsolutePath());
  37.     }
  38.  
  39.     public static void main(String[] args) {
  40.         try {
  41.             Display.setDisplayMode(new DisplayMode(640, 400));
  42.             Display.setTitle("Starhack");
  43.             Display.setVSyncEnabled(true);
  44.             Display.create();
  45.         } catch (LWJGLException ex) {
  46.             Display.destroy();
  47.             System.exit(1);
  48.         }
  49.  
  50.         if (!GLContext.getCapabilities().OpenGL11) {
  51.             System.err
  52.             .println("Your OpenGL version doesn't support the required functionality.");
  53.             Display.destroy();
  54.             System.exit(1);
  55.         }
  56.  
  57.         // check if GL_EXT_framebuffer_object can be use on this system
  58.         if (!GLContext.getCapabilities().GL_EXT_framebuffer_object) {
  59.             System.out.println("Your OpenGL version doesn't support the required functionality.");
  60.             System.exit(0);
  61.         } else {
  62.             framebufferID = glGenFramebuffersEXT();
  63.             colorTextureID = glGenTextures();
  64.             depthRenderBufferID = glGenRenderbuffersEXT();
  65.             glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID);
  66.             glBindTexture(GL_TEXTURE_2D, colorTextureID);
  67.             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  68.             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  69.             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, Display.getWidth() / 2, Display.getHeight() / 2, 0, GL_RGBA,
  70.                     GL_INT, (java.nio.ByteBuffer) null);
  71.             glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
  72.                     GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorTextureID, 0);
  73.             glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthRenderBufferID);
  74.             glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT,
  75.                     GL14.GL_DEPTH_COMPONENT24, Display.getWidth() / 2, Display.getHeight() / 2);
  76.             glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
  77.                     GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT,
  78.                     depthRenderBufferID);
  79.             glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  80.         }
  81.  
  82.         long lastLoopTime = System.nanoTime();
  83.         final int TARGET_FPS = 60;
  84.         final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
  85.  
  86.         while (gameRunning) {
  87.             long now = System.nanoTime();
  88.             long updateLength = now - lastLoopTime;
  89.             lastLoopTime = now;
  90.             double delta = updateLength / ((double) OPTIMAL_TIME);
  91.  
  92.             long lastFpsTime = updateLength;
  93.             fps++;
  94.  
  95.             if (lastFpsTime >= 1000000000) {
  96.                 System.out.println("(FPS: " + fps + ")");
  97.                 lastFpsTime = 0;
  98.                 fps = 0;
  99.             }
  100.            
  101.             render();
  102.  
  103.             Display.update();
  104.  
  105.             try {
  106.                 Thread.sleep((lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000);
  107.             } catch (Exception e) {
  108.             }
  109.  
  110.             if (Display.isCloseRequested()) {
  111.                 gameRunning = false;
  112.             }
  113.         }
  114.  
  115.         Display.destroy();
  116.         System.exit(0);
  117.     }
  118.  
  119.     public static void render() {
  120.         render3D();
  121.         render2D();
  122.         glFlush();
  123.     }
  124.  
  125.     public static void ready3D() {
  126.         glViewport(0, 0, Display.getWidth(), Display.getHeight());
  127.         glMatrixMode(GL_PROJECTION);
  128.  
  129.         glLoadIdentity();
  130.         gluPerspective(45, (float) Display.getWidth() / Display.getHeight(),
  131.                 0.1f, 5000.0f);
  132.  
  133.         glMatrixMode(GL_MODELVIEW);
  134.         glLoadIdentity();
  135.  
  136.         glDepthFunc(GL_LEQUAL);
  137.         glEnable(GL_DEPTH_TEST);
  138.     }
  139.  
  140.     public static void ready2D() {
  141.         glMatrixMode(GL_PROJECTION);
  142.         glLoadIdentity();
  143.  
  144.         gluOrtho2D(0.0f, Display.getWidth(), Display.getHeight(), 0.0f);
  145.  
  146.         glMatrixMode(GL_MODELVIEW);
  147.         glLoadIdentity();
  148.         glTranslatef(0.375f, 0.375f, 0.0f);
  149.  
  150.         glDisable(GL_DEPTH_TEST);
  151.     }
  152.  
  153.     public static void render3D() {
  154.         ready3D();
  155.  
  156.         glViewport (0, 0, Display.getWidth() / 2, Display.getHeight() / 2);
  157.  
  158.         glBindTexture(GL_TEXTURE_2D, 0);
  159.         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID);
  160.  
  161.         glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
  162.         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  163.  
  164.         glColor3f(1,1,1);
  165.  
  166.         glTranslatef(0f, 0f, -5f);
  167.         glRotatef(30f, 1f, 1f, 1f);
  168.  
  169.         Cube.wireCube(1);
  170.     }
  171.  
  172.     public static void render2D() {
  173.         ready2D();
  174.        
  175.         glViewport (0, 0, Display.getWidth(), Display.getHeight());
  176.  
  177.         glEnable(GL_TEXTURE_2D);
  178.         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  179.  
  180.         glClearColor (0.0f, 1.0f, 1.0f, 1.0f);
  181.         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  182.  
  183.         glBindTexture(GL_TEXTURE_2D, colorTextureID);
  184.        
  185.         glBegin(GL_QUADS);
  186.         glTexCoord2f(0.0f, 0.0f); glVertex2f(0, Display.getHeight());
  187.         glTexCoord2f(1.0f, 0.0f); glVertex2f(Display.getWidth(), Display.getHeight());
  188.         glTexCoord2f(1.0f, 1.0f); glVertex2f(Display.getWidth(), 0);
  189.         glTexCoord2f(0.0f, 1.0f); glVertex2f(0, 0);
  190.         glEnd();
  191.        
  192.         glDisable(GL_TEXTURE_2D);
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement