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
|
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.BufferUtil;
import java.awt.BorderLayout;
import java.awt.Color;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author dbourgais
*/
public class VBOTestJOGL {
public static void main(String[]args){
GLCanvas canvas = new GLCanvas();
VBOTest demo = new VBOTest();
canvas.addGLEventListener(demo);
final Animator animator = new Animator(canvas);
animator.start();
JFrame frame = new JFrame("VBO");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
canvas.setSize(400, 400);
frame.add(canvas, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
canvas.requestFocus();
}
}
class VBOTest implements GLEventListener{
final private float[]coords = {
-1, 1, 0,
1, 1, 0,
1, -1, 0,
-1, -1, 0
};
final private short[]indices = {
0, 1, 2, 3
};
final private Color color = Color.RED;
/**
* A rotating square!
*/
private int buffer_id = 2;
private FloatBuffer vertices;
private ShortBuffer b_indices;
private FloatBuffer b_color;
private IntBuffer squareBuffers;
public void init(GLAutoDrawable drawable) {
final GL gl = drawable.getGL();
this.vertices = BufferUtil.newFloatBuffer(this.coords.length);
this.vertices.put(coords);
this.b_indices = BufferUtil.newShortBuffer(this.indices.length);
this.b_indices.put(indices);
float[]comp = new float[4];
comp = this.color.getColorComponents(comp);
this.b_color = BufferUtil.newFloatBuffer(4 * comp.length);
for(int i=0, j=0; i<4 * comp.length; i++){
this.b_color.put(comp[j++]);
if(j==4)
j=0;
}
squareBuffers = BufferUtil.newIntBuffer(2);
gl.glGenBuffers(buffer_id, squareBuffers);
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, squareBuffers.get(0));
gl.glBufferData(gl.GL_ARRAY_BUFFER, BufferUtil.SIZEOF_FLOAT, this.vertices, gl.GL_STATIC_DRAW);
gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, squareBuffers.get(1));
gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, BufferUtil.SIZEOF_SHORT, this.b_indices, gl.GL_STATIC_DRAW);
}
public void display(GLAutoDrawable drawable) {
final GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(1, 1, 1, 0);
gl.glMatrixMode(gl.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -5);
if(gl.glIsEnabled(gl.GL_VERTEX_ARRAY)){
gl.glDisableClientState(gl.GL_VERTEX_ARRAY);
}
if(gl.glIsEnabled(gl.GL_COLOR_ARRAY)){
gl.glDisableClientState(gl.GL_COLOR_ARRAY);
}
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, squareBuffers.get(0));
gl.glVertexPointer(3, GL.GL_FLOAT, 0, this.vertices);
gl.glColorPointer(4, GL.GL_FLOAT, 0, this.b_color);
gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
gl.glEnableClientState(gl.GL_COLOR_ARRAY);
gl.glDrawElements(gl.GL_QUADS, 4, gl.GL_UNSIGNED_SHORT, 0);
gl.glDisableClientState(gl.GL_COLOR_ARRAY);
gl.glDisableClientState(gl.GL_VERTEX_ARRAY);
gl.glFlush();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
final int maxZ = 1000;
GL gl = drawable.getGL();
gl.glViewport(0, 0, width, height);
/*For the camera*/
gl.glMatrixMode(gl.GL_PROJECTION);
gl.glLoadIdentity();
//Change this by glOrtho
new GLU().gluPerspective(45., (float) width / height, 1., maxZ);
/*Model & view*/
gl.glMatrixMode(gl.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
} |
Partager