Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.starshock;
- import java.io.File;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.IntBuffer;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.LWJGLUtil;
- import org.lwjgl.Sys;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.opengl.GL14.*;
- import static org.lwjgl.opengl.GL30.*;
- import static org.lwjgl.util.glu.GLU.*;
- public class Main {
- static {
- System.setProperty("org.lwjgl.librarypath",
- new File(new File(System.getProperty("user.dir"), "native"),
- LWJGLUtil.getPlatformName()).getAbsolutePath());
- }
- public static final int S_WIDTH = 320, S_HEIGHT = 200, S_SCALE = 2;
- public long lastFrame, lastFPS;
- public int fps;
- public int defaultFBO = 0, drawFBO, drawFBOTexture, drawFBOBuffer;
- public void start() {
- try {
- Display.setDisplayMode(new DisplayMode(S_WIDTH * S_SCALE, S_HEIGHT
- * S_SCALE));
- Display.setTitle("Starhack - FPS: " + fps);
- Display.create();
- } catch (LWJGLException e) {
- e.printStackTrace();
- System.exit(0);
- }
- initFBO();
- getDelta();
- lastFPS = getTime();
- while (!Display.isCloseRequested()) {
- int delta = getDelta();
- update(delta);
- renderGL();
- Display.update();
- Display.sync(60);
- }
- Display.destroy();
- }
- public void update(int delta) {
- updateFPS();
- }
- public int getDelta() {
- long time = getTime();
- int delta = (int) (time - lastFrame);
- lastFrame = time;
- return delta;
- }
- public long getTime() {
- return (Sys.getTime() * 1000) / Sys.getTimerResolution();
- }
- public void updateFPS() {
- if (getTime() - lastFPS > 1000) {
- Display.setTitle("Starhack - FPS: " + fps);
- fps = 0;
- lastFPS += 1000;
- }
- fps++;
- }
- public void initFBO() {
- drawFBO = glGenFramebuffers();
- drawFBOTexture = glGenTextures();
- drawFBOBuffer = glGenRenderbuffers();
- glBindFramebuffer(GL_FRAMEBUFFER, drawFBO);
- glBindTexture(GL_TEXTURE_2D, drawFBOTexture);
- 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, S_WIDTH, S_HEIGHT, 0,GL_RGBA, GL_INT, (java.nio.ByteBuffer) null);
- glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, drawFBOTexture, 0);
- glBindRenderbuffer(GL_RENDERBUFFER, drawFBOBuffer);
- glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, S_WIDTH, S_HEIGHT);
- glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER, drawFBOBuffer);
- glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
- }
- public void renderGL() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glBindTexture(GL_TEXTURE_2D, 0);
- glBindFramebuffer(GL_FRAMEBUFFER, drawFBO);
- ready3D();
- render3D();
- ready2D();
- render2D();
- renderScaledFBO();
- }
- public void ready3D() {
- glViewport(0, 0, S_WIDTH, S_HEIGHT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45f, (float) S_WIDTH / S_HEIGHT, 0.0f, 5000.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glDepthFunc(GL_LEQUAL);
- glEnable(GL_DEPTH_TEST);
- }
- public void render3D() {
- glTranslatef(0, 0, -6f);
- glBegin(GL_QUADS);
- glVertex3f(0, 0, 0);
- glVertex3f(2, 0, 0);
- glVertex3f(2, 2, 0);
- glVertex3f(0, 2, 0);
- glEnd();
- }
- public void ready2D() {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0.0f, S_WIDTH, S_HEIGHT, 0.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(0.375f, 0.375f, 0.0f);
- glDisable(GL_DEPTH_TEST);
- }
- public void render2D() {
- }
- public void renderScaledFBO() {
- glViewport(0, 0, S_WIDTH * S_SCALE, S_HEIGHT * S_SCALE);
- glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
- glBlitFramebuffer(0, 0, S_WIDTH, S_HEIGHT, 0, 0, S_WIDTH * S_SCALE,
- S_HEIGHT * S_SCALE, GL_COLOR_BUFFER_BIT, GL_NEAREST);
- }
- public static void main(String[] argv) {
- Main main = new Main();
- main.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement