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
| import java.awt.*;
import java.awt.event.*;
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;
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());
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);
}
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);
gl.glEndList();
}
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();
// Si on appuyez sur clique droit alors on utilise la Display List.
if (mouseRButtonDown==true)
{
gl.glColor3f(1.0f,1.0f,1.0f);
System.err.println("Using DL");
gl.glCallList(maDisplayListIndex);
}
else
{
System.err.println("Not Using DL");
cree_pleins_de_lignes(gl,line_Amount);
}
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);
// 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(rand.nextFloat(),rand.nextFloat(),rand.nextFloat());
double x = rand.nextFloat()*width;
double y = rand.nextFloat()*height;
gl.glVertex2d(x,y);
x = rand.nextFloat()*width;
y = rand.nextFloat()*height;
gl.glVertex2d(x,y);
}
gl.glEnd();
}
}
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