Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (467)
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: Roquen (posted 2014-01-24 14:23:05, viewed 120 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   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282  
package wiki.roquen;

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

import org.lwjgl.input.Mouse;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;

/**
 * Tutorial 11: A Rotating Cube
 * 
 * @author Sri Harsha Chilakapati
 */
public class Cube2 extends Game
{
  public ArcBall arcBall;
    
    // VBO Vertex Buffer ID
    int vboVertexID;
    // VBO Color Buffer ID
    int vboColorID;
    
    private FloatBuffer rotM;
    
    /**
     * Initialize
     */
    public void init()
    {
        Display.setTitle("Tutorial 11: A Rotating Cube");
        
        // Initialize OpenGL
        glMatrixMode(GL_PROJECTION);
        gluPerspective(70f, 800f/600f, 1, 1000);
        glViewport(0, 0, Display.getWidth(), Display.getHeight());
        //glDepthRange(1.f, 1000.f);
        
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        
        // Enable Depth Testing
        glEnable(GL_DEPTH_TEST);
        
        // Enable client states
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
        
        // Create Cube vertices
        FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(3*24);
        vertexBuffer.put(new float[]
        {
            // Front face
            -0.5f, +0.5f, +0.5f,
            +0.5f, +0.5f, +0.5f,
            -0.5f, -0.5f, +0.5f,
            +0.5f, -0.5f, +0.5f,

            // Right face
            +0.5f, +0.5f, +0.5f,
            +0.5f, +0.5f, -0.5f,
            +0.5f, -0.5f, +0.5f,
            +0.5f, -0.5f, -0.5f,
            
            // Back face
            +0.5f, +0.5f, -0.5f,
            -0.5f, +0.5f, -0.5f,
            +0.5f, -0.5f, -0.5f,
            -0.5f, -0.5f, -0.5f,
            
            // Left face
            -0.5f, +0.5f, -0.5f,
            -0.5f, +0.5f, +0.5f,
            -0.5f, -0.5f, -0.5f,
            -0.5f, -0.5f, +0.5f,
            
            // Top face
            -0.5f, +0.5f, +0.5f,
            +0.5f, +0.5f, +0.5f,
            -0.5f, +0.5f, -0.5f,
            +0.5f, +0.5f, -0.5f,
            
            // Bottom face
            -0.5f, -0.5f, +0.5f,
            +0.5f, -0.5f, +0.5f,
            -0.5f, -0.5f, -0.5f,
            +0.5f, -0.5f, -0.5f,
        });
        vertexBuffer.rewind();
        
        // Create cube colors
        FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(3*24);
        colorBuffer.put(new float[]
        {
            // Front face
            1, 0, 0,
            1, 0, 0,
            1, 0, 0,
            1, 0, 0,
            
            // Right face
            0, 1, 0,
            0, 1, 0,
            0, 1, 0,
            0, 1, 0,
            
            // Back face
            0, 0, 1,
            0, 0, 1,
            0, 0, 1,
            0, 0, 1,
            
            // Left face
            1, 0, 1,
            1, 0, 1,
            1, 0, 1,
            1, 0, 1,
            
            // Top face
            0, 1, 1,
            0, 1, 1,
            0, 1, 1,
            0, 1, 1,
            
            // Bottom face
            1, 1, 0,
            1, 1, 0,
            1, 1, 0,
            1, 1, 0
        });
        colorBuffer.rewind();
        
        // Create vertex VBO
        vboVertexID = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboVertexID);
        glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        
        // Create color VBO
        vboColorID = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboColorID);
        glBufferData(GL_ARRAY_BUFFER, colorBuffer, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        
        try {
        Mouse.create();
        rotM = BufferUtils.createFloatBuffer(4*4);
        
        rotM.put(1.f);
        rotM.put(0.f);
        rotM.put(0.f);
        rotM.put(0.f);
        
        rotM.put(0.f);
        rotM.put(1.f);
        rotM.put(0.f);
        rotM.put(0.f);
        
        rotM.put(0.f);
        rotM.put(0.f);
        rotM.put(1.f);
        rotM.put(0.f);
        
        rotM.put(0.f);
        rotM.put(0.f);
        rotM.put(0.f);
        rotM.put(1.f);
        
        rotM.rewind();
        
        arcBall = new ArcBall(Display.getWidth(), Display.getHeight());
        
        } catch(Exception e) {}
    }
    
    /**
     * Update logic
     */
    
    float mouseX, mouseY;
    
    public void update(long elapsedTime)
    {
      mouseX = Mouse.getX();
      mouseY = Mouse.getY();
      
      while (Mouse.next()) {
        int id = Mouse.getEventButton();
        
        if (id != -1) {
          if (id == 0) {
            
            if (Mouse.getEventButtonState())
              arcBall.onDown(mouseX, mouseY);
            else
              arcBall.onUp(!isKeyDown(KEY_SPACE));
          }
        }
        else {
          int dx  = Mouse.getEventDX();
          int dy  = Mouse.getEventDY();
          int dw  = Mouse.getEventDWheel();
          
          if ((dx|dy) != 0 && Mouse.isButtonDown(0)) {
            arcBall.onDrag(mouseX, mouseY);
          }
          
          if (dw != 0) {
            //System.out.println("w: " + dw);
          }
        }
      }
      
      if (isKeyDown(KEY_SPACE)) arcBall.reset();
      
        if (isKeyDown(KEY_ESCAPE))
            end();
    }
    
    /**
     * Render to screen
     */
    public void render()
    {
      arcBall.getMatrix(rotM);
      
        // Clean both color and depth buffers
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        // Set the rotation
        glLoadMatrix(rotM);
        
        // Translate into the view
        //glTranslatef(0, 0, -2);
        
        // Bind the vertex VBO
        glBindBuffer(GL_ARRAY_BUFFER, vboVertexID);
        glVertexPointer(3, GL_FLOAT, 0, 0);
        
        // Bind the color VBO
        glBindBuffer(GL_ARRAY_BUFFER, vboColorID);
        glColorPointer(3, GL_FLOAT, 0, 0);
        
        // Draw the cube with triangle strip
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 24);
        
        // Translate back
        //glTranslatef(0, 0, 2);
    }
    
    /**
     * Display resized
     */
    public void resized()
    {
      int w = Display.getWidth();
      int h = Display.getHeight();
      
      if (arcBall != null) arcBall.setBound(w, h);
      glViewport(0, 0, w, h);
    }
    
    /**
     * Dispose resources
     */
    public void dispose()
    {
        glDeleteBuffers(vboVertexID);
        glDeleteBuffers(vboColorID);
    }
    
    public static void main(String[] args)
    {
        new Cube2();
    }

}





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.

xsi3rr4x (81 views)
2014-04-15 18:08:23

BurntPizza (73 views)
2014-04-15 03:46:01

UprightPath (84 views)
2014-04-14 17:39:50

UprightPath (67 views)
2014-04-14 17:35:47

Porlus (84 views)
2014-04-14 15:48:38

tom_mai78101 (107 views)
2014-04-10 04:04:31

BurntPizza (167 views)
2014-04-08 23:06:04

tom_mai78101 (263 views)
2014-04-05 13:34:39

trollwarrior1 (214 views)
2014-04-04 12:06:45

CJLetsGame (223 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!