Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (525)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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: CHmSID (posted 2014-05-18 21:51:15, viewed 111 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  
package me.main;

import org.lwjgl.opengl.Display;
//import org.lwjgl.opengl.GL11;

import static org.lwjgl.opengl.GL11.*;
import me.util.Program;
import me.util.Camera;

public class MainProgram {

   public static void main(String[] args) {
      
      Program app = new Program(800, 600, "VBO 3D Test", false);
      
      Camera cam = new Camera(60, 800f / 600f);
      cam.setPosition(5, 5, 5);
      
      Tile t = new Tile();

      while(!Display.isCloseRequested()) {
         
         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
         
         cam.look(0, 0, 0);
         
         t.draw();         
//         glBegin(GL_QUADS);
//         
//         glVertex3d(-0.5, 0, -0.5);
//         glVertex3d(0.5, 0, -0.5);
//         glVertex3d(0.5, 0, 0.5);
//         glVertex3d(-0.5, 0, 0.5);
//         
//         glEnd();
         
         Display.update();
         Display.sync(60);
      }
      
      t.destroy();
      app.destroy();
   }
}


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  
package me.main;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;

public class Tile {
   
   private int vboFloor;
   private FloatBuffer floorVertexData;
   private FloatBuffer floorColorData;
   
   Tile() {

      generateVBO();
   }
   
   private void generateVBO() {
      
      vboFloor = glGenBuffers();
      
      floorVertexData = BufferUtils.createFloatBuffer(4 * 3);
      floorVertexData.put(new float[]{-0.5f, 0, -0.5f,
            -0.5f, 0, 0.5f,
            0.5f, 0, 0.5f,
            0.5f, 0, -0.5f});
      floorVertexData.flip();
      
      glBindBuffer(GL_ARRAY_BUFFER, vboFloor);
      glBufferData(vboFloor, floorVertexData, GL_STATIC_DRAW);
      
      floorColorData = BufferUtils.createFloatBuffer(4 * 3);
      floorColorData.put(new float[]{1, 1, 1,
            1, 1, 1,
            1, 1, 1,
            1, 1, 1});
      floorColorData.flip();
      
      glBindBuffer(GL_ARRAY_BUFFER, vboFloor);
      glBufferData(vboFloor, floorColorData, GL_STATIC_DRAW);
      
      glBindBuffer(GL_ARRAY_BUFFER, 0);
   }
   
   public void draw() {
      
      glBindBuffer(GL_ARRAY_BUFFER, vboFloor);
      glVertexPointer(3, GL_FLOAT, 0, 0L);
      
      glBindBuffer(GL_ARRAY_BUFFER, vboFloor);
      glColorPointer(3, GL_FLOAT, 0, 0L);
      
      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_COLOR_ARRAY);
      glDrawArrays(GL_QUADS, 0, 3);
      glDisableClientState(GL_COLOR_ARRAY);
      glDisableClientState(GL_VERTEX_ARRAY);
      
      glBindBuffer(GL_ARRAY_BUFFER, 0);
      
//      glBegin(GL_QUADS);
//      
//      glVertex3d(-0.5, 0, -0.5);
//      glVertex3d(0.5, 0, -0.5);
//      glVertex3d(0.5, 0, 0.5);
//      glVertex3d(-0.5, 0, 0.5);
//      
//      glEnd();
   }
   
   public void destroy() {
      
      glDeleteBuffers(vboFloor);
   }
}


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  
package me.util;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class Program {

   public Program(int width, int height, String title, boolean fullscreen) {
      
      initialise(width, height, title, fullscreen);
   }
   
   private void initialise(int width, int height, String title, boolean fullscreen) {
      
      //Get all the possible resolutions
      DisplayMode[] modes = {null};
      
      try {
         modes = Display.getAvailableDisplayModes();
      } catch (LWJGLException e1) {
         e1.printStackTrace();
      }
      
      DisplayMode currentMode = null;
      
      //Go throught all possible displays and look for the one we want
      for(int i = 0; i < modes.length; i++) {
         
         if(modes[i].getWidth() == width && modes[i].getHeight() == height && modes[i].isFullscreenCapable()) {
            
            currentMode = modes[i];
         }
      }
      
      //If we didn't find the resolution we looked for, go windowed
      if(currentMode == null) {
         currentMode = new DisplayMode(width, height);
         fullscreen = false;
      }
      
      //Else create the window
      try {
         Display.setDisplayMode(currentMode);
         Display.setFullscreen(fullscreen);
         Display.create();
      } catch (LWJGLException e) {
         e.printStackTrace();
      }
      
      Display.setTitle(title);
   }
   
   public void destroy() {
      
      Display.destroy();
   }
}


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  
package me.util;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;

public class Camera {

   private float x, y, z;
   private float rx, ry, rz;
   private float fov;
   private float aspect;
   
   public Camera() {
      
      this(60, 1600 / 900);
   }
   
   public Camera(float fov, float aspect) {
      
      this(0, 0, 0, 0, 0, 0, fov, aspect);
   }
   
   public Camera(float x, float y, float z, float rx, float ry,
         float rz, float fov, float aspect) {
      
      setPosition(x, y, z);
      setRotation(rx, ry, rz);
      
      this.fov = fov;
      this.aspect = aspect;
      
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(fov, aspect, 1, 1000);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
   }
   
   public void look(float cx, float cy, float cz) {
      
      gluLookAt(x, y, z, cx, cy, cz, 0, 1, 0);
   }
   
   public float getX() {return x; }
   public float getY() {return y; }
   public float getZ() {return z; }
   
   public void setPosition(float x, float y, float z) {
      
      this.x = x;
      this.y = y;
      this.z = z;
   }
   
   public void setRotation(float p, float y, float r) {
      
      this.rx = p;
      this.ry = y;
      this.rz = r;
   }
}





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.

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

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

toopeicgaming1999 (12 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (29 views)
2014-11-25 11:26:43

Gibbo3771 (24 views)
2014-11-24 19:59:16

trollwarrior1 (37 views)
2014-11-22 12:13:56

xFryIx (76 views)
2014-11-13 12:34:49

digdugdiggy (53 views)
2014-11-12 21:11:50
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!