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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
| import java.awt.*;
import java.awt.event.*;
import java.nio.FloatBuffer;
import java.util.Random;
import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;
import com.sun.opengl.util.*;
public class PleinDeLignes implements GLEventListener, MouseListener, MouseMotionListener {
public static void main(String[] args) {
Frame frame = new Frame("Gear Demo");
GLCanvas canvas = new GLCanvas();
canvas.addGLEventListener(new PleinDeLignes());
frame.add(canvas);
frame.setSize(800, 600);
final Animator animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// Run this on another thread than the AWT event queue to
// make sure the call to Animator.stop() completes before
// exiting
new Thread(new Runnable() {
public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
frame.setVisible(true);
animator.start();
}
private float angle = 0.0f;
private int prevMouseX, prevMouseY;
private boolean mouseRButtonDown = false;
private int width;
private int height;
private int maDisplayListIndex;
Random rand = new Random();
int line_Amount = 50000;
float [] number = new float[line_Amount*14];
int rndindx = 0;
// Datas
private FloatBuffer vertices; // Vertex Data
private FloatBuffer colors; // Vertex Data
// Vertex Buffer Object Names
private int[] VBOVertices = new int[1]; // Vertex VBO Name
private int[] VBOTexCoords = new int[1];// Texture Coordinate VBO Name
public void init(GLAutoDrawable drawable) {
// Use debug pipeline
drawable.setGL(new DebugGL(drawable.getGL()));
GL gl = drawable.getGL();
System.err.println("INIT GL IS: " + gl.getClass().getName());
System.err.println("Chosen GLCapabilities: " + drawable.getChosenGLCapabilities());
// Check For VBO support
final boolean VBOsupported = gl.isFunctionAvailable("glGenBuffersARB") &&
gl.isFunctionAvailable("glBindBufferARB") &&
gl.isFunctionAvailable("glBufferDataARB") &&
gl.isFunctionAvailable("glDeleteBuffersARB");
if (VBOsupported==false)
{
System.err.println("VBO is not supported");
}
gl.setSwapInterval(1);
// gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, pos, 0);
gl.glEnable(GL.GL_CULL_FACE);
gl.glDisable(GL.GL_LIGHTING);
gl.glDisable(GL.GL_LIGHT0);
gl.glDisable(GL.GL_DEPTH_TEST);
gl.glDisable(GL.GL_NORMALIZE);
drawable.addMouseListener(this);
drawable.addMouseMotionListener(this);
gl.glShadeModel(GL.GL_FLAT);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
for (int i = 0; i < number.length; i++)
{
number[i]=rand.nextFloat();
}
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
float h = (float)height / (float)width;
this.width=width;
this.height=height;
System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR));
System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER));
System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION));
BeginOrtho(gl);
maDisplayListIndex = gl.glGenLists(1);
gl.glNewList(maDisplayListIndex, GL.GL_COMPILE);
cree_pleins_de_lignes(gl,line_Amount);
//cree_pleins_de_lignes_VBO(gl, line_Amount);
gl.glEndList();
cree_pleins_de_lignes_VBO(gl, line_Amount);
}
void BeginOrtho(GL gl)
{
gl.glPushMatrix();
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
new GLU().gluOrtho2D(0,width, height,0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void display(GLAutoDrawable drawable) {
// Get the GL corresponding to the drawable we are animating
GL gl = drawable.getGL();
// Special handling for the case where the GLJPanel is translucent
// and wants to be composited with other Java 2D content
if ((drawable instanceof GLJPanel) &&
!((GLJPanel) drawable).isOpaque() &&
((GLJPanel) drawable).shouldPreserveColorBufferIfTranslucent()) {
gl.glClear(GL.GL_DEPTH_BUFFER_BIT);
} else {
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
}
gl.glTranslatef(width/2, height/2, 0);
gl.glRotated(Math.toRadians(angle+=0.5f), 0,0,1);
gl.glTranslatef(-width/2,-height/2, 0);
gl.glPushMatrix();
gl.glColor3f(1.0f,1.0f,1.0f);
// Si on appuyez sur clique droit alors on utilise la Display List.
if (mouseRButtonDown==true)
{
System.err.println("Using DL");
gl.glCallList(maDisplayListIndex);
}
else
{
System.err.println("Not Using DL");
if (true)
{
// not use vbo
cree_pleins_de_lignes(gl,line_Amount);
}
else
{
// use vbo.
gl.glLineWidth(1.0f);
// Enable Pointers
gl.glEnableClientState(GL.GL_VERTEX_ARRAY); // Enable Vertex Arrays
// Enable Texture Coord Arrays
gl.glEnableClientState(GL.GL_COLOR_ARRAY);
// use vbo
gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, VBOTexCoords[0]);
// Set The TexCoord Pointer To The TexCoord Buffer
gl.glColorPointer(3, GL.GL_FLOAT, 0, 0);
gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, VBOVertices[0]);
// Set The Vertex Pointer To The Vertex Buffer
gl.glVertexPointer(2, GL.GL_FLOAT, 0, 0);
// Render
// Draw All Of The Triangles At Once
gl.glDrawArrays(GL.GL_LINES, 0, line_Amount);
// Disable Pointers
// Disable Vertex Arrays
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
// Disable Texture Coord Arrays
gl.glDisableClientState(GL.GL_COLOR_ARRAY);
}
}
gl.glPopMatrix();
}
/**
* @param gl
*/
private void cree_pleins_de_lignes(GL gl, int nombre)
{
//gl.glEnable(GL.GL_BLEND); // N'exagerons pas non plus ;)
// gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);
//rand.setSeed(1234);
rndindx=0;
// Dessine des line de plus en plus grosse (il y'aura differente taille de lignes en fait).
for (int j = 1; j < 5; j++)
{
gl.glLineWidth(1);
gl.glBegin(GL.GL_LINES);
// Dessine un cinquieme des lignes.
for (int i = 0; i < nombre/5; i++)
{
gl.glColor3f(number[rndindx++],number[rndindx++],number[rndindx++]);
double x = number[rndindx++]*width;
double y = number[rndindx++]*height;
gl.glVertex2d(x,y);
x = number[rndindx++]*width;
y = number[rndindx++]*height;
gl.glVertex2d(x,y);
}
gl.glEnd();
}
}
private void cree_pleins_de_lignes_VBO(GL gl, int nombre)
{
// Allocate Vertex Data
vertices = BufferUtil.newFloatBuffer(line_Amount * 2*2);
// Allocate Tex Coord Data
colors = BufferUtil.newFloatBuffer(line_Amount * 3*2);
//gl.glEnable(GL.GL_BLEND); // N'exagerons pas non plus ;)
// gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);
//rand.setSeed(1234);
rndindx=0;
// Dessine des line de plus en plus grosse (il y'aura differente taille de lignes en fait).
for (int j = 1; j < 5; j++)
{
// gl.glLineWidth(j);
// gl.glBegin(GL.GL_LINES);
// Dessine un cinquieme des lignes.
for (int i = 0; i < nombre/5; i++)
{
// gl.glColor3f(number[rndindx++],number[rndindx++],number[rndindx++]);
colors.put(number[rndindx++]);
colors.put(number[rndindx++]);
colors.put(number[rndindx++]);
float x = number[rndindx++]*width;
float y = number[rndindx++]*height;
// gl.glVertex2d(x,y);
vertices.put(x);
vertices.put(y);
x = number[rndindx++]*width;
y = number[rndindx++]*height;
// gl.glVertex2d(x,y);
colors.put(number[rndindx++]);
colors.put(number[rndindx++]);
colors.put(number[rndindx++]);
vertices.put(x);
vertices.put(y);
}
// gl.glEnd();
}
vertices.flip();
colors.flip();
buildVBOs(gl);
}
private void buildVBOs(GL gl) {
// Generate And Bind The Vertex Buffer
gl.glGenBuffersARB(1, VBOVertices, 0); // Get A Valid Name
gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, VBOVertices[0]); // Bind The Buffer
// Load The Data
gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, line_Amount * 2 * 2 *
BufferUtil.SIZEOF_FLOAT, vertices, GL.GL_STATIC_DRAW_ARB);
// Generate And Bind The Texture Coordinate Buffer
gl.glGenBuffersARB(1, VBOTexCoords, 0); // Get A Valid Name
gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, VBOTexCoords[0]); // Bind The Buffer
// Load The Data
gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, line_Amount * 3 * 2*
BufferUtil.SIZEOF_FLOAT, colors, GL.GL_STATIC_DRAW_ARB);
// Our Copy Of The Data Is No Longer Necessary, It Is Safe In The Graphics Card
vertices = null;
colors = null;
// fUseVBO = true;
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
// Methods required for the implementation of MouseListener
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
prevMouseX = e.getX();
prevMouseY = e.getY();
if ((e.getModifiers() & e.BUTTON3_MASK) != 0) {
mouseRButtonDown = true;
}
}
public void mouseReleased(MouseEvent e) {
if ((e.getModifiers() & e.BUTTON3_MASK) != 0) {
mouseRButtonDown = false;
}
}
public void mouseClicked(MouseEvent e) {}
// Methods required for the implementation of MouseMotionListener
public void mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
Dimension size = e.getComponent().getSize();
}
public void mouseMoved(MouseEvent e) {}
} |
Partager