Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.starhack;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.LWJGLUtil;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import org.lwjgl.opengl.GL14;
- import org.lwjgl.opengl.GLContext;
- import static org.lwjgl.opengl.EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT;
- import static org.lwjgl.opengl.EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT;
- import static org.lwjgl.opengl.EXTFramebufferObject.GL_FRAMEBUFFER_EXT;
- import static org.lwjgl.opengl.EXTFramebufferObject.GL_RENDERBUFFER_EXT;
- import static org.lwjgl.opengl.EXTFramebufferObject.glBindFramebufferEXT;
- import static org.lwjgl.opengl.EXTFramebufferObject.glBindRenderbufferEXT;
- import static org.lwjgl.opengl.EXTFramebufferObject.glFramebufferRenderbufferEXT;
- import static org.lwjgl.opengl.EXTFramebufferObject.glFramebufferTexture2DEXT;
- import static org.lwjgl.opengl.EXTFramebufferObject.glGenFramebuffersEXT;
- import static org.lwjgl.opengl.EXTFramebufferObject.glGenRenderbuffersEXT;
- import static org.lwjgl.opengl.EXTFramebufferObject.glRenderbufferStorageEXT;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.util.glu.GLU.*;
- import java.io.File;
- public class Main {
- private static boolean gameRunning = true;
- private static int fps;
- private static int colorTextureID;
- private static int framebufferID;
- private static int depthRenderBufferID;
- static {
- System.setProperty("org.lwjgl.librarypath",
- new File(new File(System.getProperty("user.dir"), "native"),
- LWJGLUtil.getPlatformName()).getAbsolutePath());
- }
- public static void main(String[] args) {
- try {
- Display.setDisplayMode(new DisplayMode(640, 400));
- Display.setTitle("Starhack");
- Display.setVSyncEnabled(true);
- Display.create();
- } catch (LWJGLException ex) {
- Display.destroy();
- System.exit(1);
- }
- if (!GLContext.getCapabilities().OpenGL11) {
- System.err
- .println("Your OpenGL version doesn't support the required functionality.");
- Display.destroy();
- System.exit(1);
- }
- // check if GL_EXT_framebuffer_object can be use on this system
- if (!GLContext.getCapabilities().GL_EXT_framebuffer_object) {
- System.out.println("Your OpenGL version doesn't support the required functionality.");
- System.exit(0);
- } else {
- framebufferID = glGenFramebuffersEXT();
- colorTextureID = glGenTextures();
- depthRenderBufferID = glGenRenderbuffersEXT();
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID);
- glBindTexture(GL_TEXTURE_2D, colorTextureID);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, Display.getWidth() / 2, Display.getHeight() / 2, 0, GL_RGBA,
- GL_INT, (java.nio.ByteBuffer) null);
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
- GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorTextureID, 0);
- glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthRenderBufferID);
- glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT,
- GL14.GL_DEPTH_COMPONENT24, Display.getWidth() / 2, Display.getHeight() / 2);
- glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
- GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT,
- depthRenderBufferID);
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
- }
- long lastLoopTime = System.nanoTime();
- final int TARGET_FPS = 60;
- final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
- while (gameRunning) {
- long now = System.nanoTime();
- long updateLength = now - lastLoopTime;
- lastLoopTime = now;
- double delta = updateLength / ((double) OPTIMAL_TIME);
- long lastFpsTime = updateLength;
- fps++;
- if (lastFpsTime >= 1000000000) {
- System.out.println("(FPS: " + fps + ")");
- lastFpsTime = 0;
- fps = 0;
- }
- render();
- Display.update();
- try {
- Thread.sleep((lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000);
- } catch (Exception e) {
- }
- if (Display.isCloseRequested()) {
- gameRunning = false;
- }
- }
- Display.destroy();
- System.exit(0);
- }
- public static void render() {
- render3D();
- render2D();
- glFlush();
- }
- public static void ready3D() {
- glViewport(0, 0, Display.getWidth(), Display.getHeight());
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45, (float) Display.getWidth() / Display.getHeight(),
- 0.1f, 5000.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glDepthFunc(GL_LEQUAL);
- glEnable(GL_DEPTH_TEST);
- }
- public static void ready2D() {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0.0f, Display.getWidth(), Display.getHeight(), 0.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(0.375f, 0.375f, 0.0f);
- glDisable(GL_DEPTH_TEST);
- }
- public static void render3D() {
- ready3D();
- glViewport (0, 0, Display.getWidth() / 2, Display.getHeight() / 2);
- glBindTexture(GL_TEXTURE_2D, 0);
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID);
- glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
- glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3f(1,1,1);
- glTranslatef(0f, 0f, -5f);
- glRotatef(30f, 1f, 1f, 1f);
- Cube.wireCube(1);
- }
- public static void render2D() {
- ready2D();
- glViewport (0, 0, Display.getWidth(), Display.getHeight());
- glEnable(GL_TEXTURE_2D);
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
- glClearColor (0.0f, 1.0f, 1.0f, 1.0f);
- glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glBindTexture(GL_TEXTURE_2D, colorTextureID);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f, 0.0f); glVertex2f(0, Display.getHeight());
- glTexCoord2f(1.0f, 0.0f); glVertex2f(Display.getWidth(), Display.getHeight());
- glTexCoord2f(1.0f, 1.0f); glVertex2f(Display.getWidth(), 0);
- glTexCoord2f(0.0f, 1.0f); glVertex2f(0, 0);
- glEnd();
- glDisable(GL_TEXTURE_2D);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement