Java-Gaming.org Hi !
Featured games (87)
games approved by the League of Dukes
Games in Showcase (649)
Games in Android Showcase (181)
games submitted by our members
Games in WIP (700)
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 155 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
 
KaiHH (142 views)
2016-01-31 23:15:29

sci4me (149 views)
2016-01-23 21:47:05

sci4me (135 views)
2016-01-23 21:46:58

KaiHH (168 views)
2016-01-19 13:26:42

theagentd (255 views)
2016-01-05 17:10:00

ClaasJG (269 views)
2016-01-03 16:58:36

chrisdalke (259 views)
2015-12-28 06:31:21

Guerra2442 (264 views)
2015-12-25 03:42:55

Guerra2442 (266 views)
2015-12-25 03:27:21

theagentd (294 views)
2015-12-21 14:43:24
List of Learning Resources
by SilverTiger
2016-02-05 09:39:47

List of Learning Resources
by SilverTiger
2016-02-05 09:38:38

List of Learning Resources
by SilverTiger
2016-02-05 09:35:50

Rendering resources
by Roquen
2015-11-13 14:37:59

Rendering resources
by Roquen
2015-11-13 14:36:58

Math: Resources
by Roquen
2015-10-22 07:46:10

Networking Resources
by Roquen
2015-10-16 07:12:30

Rendering resources
by Roquen
2015-10-15 07:40:48
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!