Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Genius Coder (posted 2014-06-05 12:21:24, viewed 133 times)

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   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183  
package game;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import java.io.*;
import java.nio.*;
import org.lwjgl.*;
import org.lwjgl.opengl.*;
import org.lwjgl.util.vector.*;

public class Game {
    public static volatile boolean running = true;
    
    //PLAYER STUFF
    public static Vector3f Camera_Position = new Vector3f(0, 0, 0);
    public static Vector3f Camera_Rotation = new Vector3f(0, 0, 0);
    public static Vector3f CameraDisposition = new Vector3f(0, 0, 0);
    public static float viewDistance = 100f;
    public static int fov = 70;
    public static int VIEW = 0; // 0 = THIRD, 1 = FIRST
    public static int walkingSpeed = 10;
    public static final float zNear = 0.3f;
    public static final float zFar = 1600f;
    public static final boolean fullscreen = false;
    public static final boolean vsync = false;
    public static final boolean printFPS = true;
    public static final int maxLookUp = 85;
    public static final int maxLookDown = -85;
    public static int fps;
    public static int mouseSpeed = 2;
    public static long lastFPS;
    public static long lastFrame;
    
    public static int vboNormalHandle;
    public static int vboVertexHandle;
    public static int vboUVHandle;
    public static Model m;
    
    public static long getTime() {
        return (Sys.getTime() * 1000) / Sys.getTimerResolution();
    }
    public static int getDelta() {
        long currentTime = getTime();
        int delta = (int) (currentTime - lastFrame);
        lastFrame = getTime();
        return delta;
    }
    public static void updateFPS() {
        if (getTime() - lastFPS > 1000) {
           if (printFPS) {
              System.out.println("FPS: " + fps);
              fps = 0;
              lastFPS += 1000;
           }
        }
        fps++;
    }

   public static void main(String[] args) {
       Display_Manager.deploy();
       
       setUpVBOs();
       initCameraPosition();
        getDelta();
        
        lastFPS = getTime();
        while (running) {
            
            render();
            
            Keyboard_Input.detect();
            if (printFPS)                {       updateFPS();       }
            if (vsync)                   {       Display.sync(60);    }
            if (Display.isCloseRequested()) {       running = false;    }   
            Display.update();
        }

        cleanUp();
    }
    
   public static void cleanUp() {
      glDeleteBuffers(vboVertexHandle);
      glDeleteBuffers(vboNormalHandle);
      glDeleteBuffers(vboUVHandle);
        Display.destroy();
        System.exit(0);
    }
    
   public static void initCameraPosition() {
       if (VIEW == 0) {
          CameraDisposition = new Vector3f(0.04f, -9.05f, 4.48f);
          Camera_Rotation = new Vector3f(29.64f, 179.20f, 0.00f);
            Camera_Position.y = Game.CameraDisposition.y;
       } else if (VIEW == 1){
          CameraDisposition = new Vector3f(0.01f, -6.47f, -0.5f);
          Camera_Rotation = new Vector3f(4.64f, 179.50f, 0.00f);
            Camera_Position.y = Game.CameraDisposition.y;
       }
    }
   
   public static void setUpVBOs() {
      vboVertexHandle = glGenBuffers();
      vboNormalHandle = glGenBuffers();
      vboUVHandle = glGenBuffers();
      
      m = null;
      try { m = OBJLoader.loadModel(new File("models/floor.obj"), new File("textures/floor.png"));
      } catch (FileNotFoundException e)       { e.printStackTrace();
      } catch (IOException e)             { e.printStackTrace();}
      
      FloatBuffer vertices = reserveData(m.faces.size() * 9);
      FloatBuffer normals = reserveData(m.faces.size() * 9);
      FloatBuffer UVs = reserveData(m.faces.size() * 9);
      
      for (Face face : m.faces) {
         vertices.put(asFloats3f(m.vertices.get((int) face.vertex.x - 1)));
         vertices.put(asFloats3f(m.vertices.get((int) face.vertex.y - 1)));
         vertices.put(asFloats3f(m.vertices.get((int) face.vertex.z - 1)));
         normals.put(asFloats3f(m.normals.get((int) face.normal.x - 1)));
         normals.put(asFloats3f(m.normals.get((int) face.normal.y - 1)));
         normals.put(asFloats3f(m.normals.get((int) face.normal.z - 1)));
         UVs.put(asFloats2f(m.uvcoord.get((int) face.uv.x - 1)));
         UVs.put(asFloats2f(m.uvcoord.get((int) face.uv.y - 1)));
      }
      vertices.flip();
      normals.flip();
      UVs.flip();

      glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
      glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, vboNormalHandle);
      glBufferData(GL_ARRAY_BUFFER, normals, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, vboUVHandle);
      glBufferData(GL_ARRAY_BUFFER, UVs, GL_STATIC_DRAW);
      
      glBindBuffer(GL_ARRAY_BUFFER, 0);
   }
   
   public static void render() {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);      

        m.texture.bind();
        glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
        glVertexPointer(3, GL_FLOAT, 0, 0L);
        glBindBuffer(GL_ARRAY_BUFFER, vboNormalHandle);
        glNormalPointer(GL_FLOAT, 0, 0L);
        glBindBuffer(GL_ARRAY_BUFFER, vboUVHandle);
        glTexCoordPointer(3, GL_FLOAT, 0, 0);
        
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        
        glColor3f(0.90f, 0.27f, 0.17f);
        //glMaterialf(GL_FRONT, GL_SHININESS, 10f);
        glDrawArrays(GL_TRIANGLES, 0, m.faces.size() * 3);
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_NORMAL_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glUseProgram(0);
        
        glLoadIdentity();
        glRotatef(Camera_Rotation.x, 1, 0, 0);
        glRotatef(Camera_Rotation.y, 0, 1, 0);
        glRotatef(Camera_Rotation.z, 0, 0, 1);
        glTranslatef(Camera_Position.x, Camera_Position.y, Camera_Position.z);
        glFlush();
   }
   
   public static float[] asFloats3f (Vector3f v) {
      return new float[]{v.x, v.y, v.z};
   }
   public static float[] asFloats2f (Vector2f v) {
      return new float[]{v.x, v.y};
   }
   public static FloatBuffer reserveData(int size) {
      FloatBuffer data = BufferUtils.createFloatBuffer(size);
      return data;
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (32 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (58 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!