Advertisement
Guest User

Vertex Arrays Example

a guest
Oct 20th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. package com.quew8.testapp;
  2.  
  3. import java.nio.FloatBuffer;
  4. import java.nio.IntBuffer;
  5. import org.lwjgl.BufferUtils;
  6. import org.lwjgl.LWJGLException;
  7. import org.lwjgl.opengl.Display;
  8. import org.lwjgl.opengl.DisplayMode;
  9.  
  10. import static org.lwjgl.opengl.GL11.*;
  11. import org.lwjgl.util.glu.GLU;
  12.  
  13. /**
  14.  *
  15.  * @author Quew8
  16.  */
  17. public class TestApplication {
  18.    
  19.     public static void init() throws LWJGLException {
  20.         Display.setDisplayMode(new DisplayMode(800, 600));
  21.         Display.setTitle("Test App");
  22.         Display.create();
  23.        
  24.         glClearColor(1, 0, 0, 0);
  25.     }
  26.    
  27.     public static void loop() {
  28.         int error;
  29.        
  30.         FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(12 * 2);
  31.         vertexBuffer.put(new float[] {
  32.             25, 45, //0
  33.             45, 65, //1
  34.             65, 45, //2
  35.             45, 25, //3
  36.             25, 85, //4
  37.             45, 105, //5
  38.             65, 85, //6
  39.             85, 105, //7
  40.             105, 85, //8
  41.             85, 65, //9
  42.             105, 45, //10
  43.             85, 25, //11
  44.         });
  45.         vertexBuffer.flip();
  46.  
  47.         FloatBuffer colourBuffer = BufferUtils.createFloatBuffer(12 * 3);
  48.         colourBuffer.put(new float[] {
  49.             0, 1, 0, //0
  50.             0, 0, 1, //1
  51.             1, 0, 1, //2
  52.             0, 1, 0, //3
  53.             0, 1, 0, //4
  54.             0, 1, 0, //5
  55.             1, 0, 1, //6
  56.             0, 1, 0, //7
  57.             0, 1, 0, //8
  58.             0, 0, 1, //9
  59.             0, 1, 0, //10
  60.             0, 1, 0, //11
  61.         });
  62.         colourBuffer.flip();
  63.  
  64.         IntBuffer indexBuffer = BufferUtils.createIntBuffer(20);
  65.         indexBuffer.put(new int[] {
  66.             0, 1, 2, 3, // Lower Left
  67.             4, 5, 6, 1, // Upper Left
  68.             7, 8, 9, 6, // Upper Right
  69.             10, 11, 2, 9, // Lower Right
  70.             1, 6, 9, 2, // Middle
  71.         });
  72.         indexBuffer.flip();
  73.        
  74.         while(!Display.isCloseRequested()) {
  75.             glViewport(0, 0, 800, 600);
  76.            
  77.             glClear(GL_COLOR_BUFFER_BIT);
  78.            
  79.             glMatrixMode(GL_PROJECTION);
  80.             glLoadIdentity();
  81.             glOrtho(0, 140, 0, 140, -1, 1);
  82.            
  83.             glMatrixMode(GL_MODELVIEW);
  84.             glLoadIdentity();
  85.            
  86.             glEnableClientState(GL_VERTEX_ARRAY);
  87.             glEnableClientState(GL_COLOR_ARRAY);
  88.            
  89.             glVertexPointer(2, 8, vertexBuffer);
  90.             glColorPointer(3, 12, colourBuffer);
  91.            
  92.             glDrawElements(GL_QUADS, indexBuffer);
  93.            
  94.             glDisableClientState(GL_COLOR_ARRAY);
  95.             glDisableClientState(GL_VERTEX_ARRAY);
  96.            
  97.             if( (error = glGetError()) != GL_NO_ERROR) {
  98.                 System.err.print(GLU.gluGetString(error));
  99.                 throw new RuntimeException("Something Bad Has Happened");
  100.             }
  101.            
  102.             Display.update();
  103.             Display.sync(60);
  104.         }
  105.     }
  106.    
  107.     public static void deinit() {
  108.         Display.destroy();
  109.     }
  110.    
  111.     public static void main(String[] args) throws LWJGLException {
  112.         TestApplication.init();
  113.         TestApplication.loop();
  114.         TestApplication.deinit();
  115.     }
  116.    
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement