Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GLVectorCanvas extends GLCanvas implements GLEventListener{
- private GL2 gl;
- private GLU glu = new GLU();
- private final ArrayList<Vector> vectors;
- private FPSAnimator animator;
- private final float XYAngle;
- private final float cameraX;
- private final float cameraY;
- private final float cameraHeight;
- public GLVectorCanvas(ArrayList<Vector> vectors, float XYAngle, float cameraX, float cameraY, float cameraHeight) {
- this.vectors = vectors;
- this.XYAngle = XYAngle;
- this.cameraX = cameraX;
- this.cameraY = cameraY;
- this.cameraHeight = cameraHeight;
- }
- @Override
- public void display(GLAutoDrawable drawable) {
- gl = drawable.getGL().getGL2();
- gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
- /* X component of grid*/
- for(int i = 0; i <= 10; i++) {
- gl.glBegin (GL2.GL_LINES);
- gl.glColor3f(1.0f,0.0f,0.0f);
- gl.glVertex3f(1.0f,-1.0f + 0.2f*i,0.0f);
- gl.glVertex3f(-1.0f,-1.0f + 0.2f*i,0.0f);
- gl.glPopMatrix();
- gl.glEnd();
- }
- /* Y component of grid*/
- for(int i = 0; i <= 10; i++) {
- gl.glBegin (GL2.GL_LINES);
- gl.glColor3f(0.0f,0.0f,1.0f);
- gl.glVertex3f(-1.0f + 0.2f*i,1.0f, 0.0f);
- gl.glVertex3f(-1.0f + 0.2f*i,-1.0f, 0.0f);
- gl.glPopMatrix();
- gl.glEnd();
- }
- /* Z Axis*/
- gl.glBegin (GL2.GL_LINES);
- gl.glColor3f(0.0f,1.0f,0.0f);
- gl.glVertex3f(0.0f,0.0f,1.0f);
- gl.glVertex3f(0.0f,0.0f,-1.0f);
- gl.glPopMatrix();
- gl.glEnd();
- /* Z Axis ticks parallel to X axis*/
- gl.glBegin (GL2.GL_LINES);
- gl.glColor3f(0.0f,1.0f,0.0f);
- gl.glVertex3f(-0.2f,0.0f,-1.0f);
- gl.glVertex3f(0.2f,0.0f,-1.0f);
- gl.glPopMatrix();
- gl.glEnd();
- gl.glBegin (GL2.GL_LINES);
- gl.glColor3f(0.0f,1.0f,0.0f);
- gl.glVertex3f(-0.2f,0.0f,1.0f);
- gl.glVertex3f(0.2f,0.0f,1.0f);
- gl.glPopMatrix();
- gl.glEnd();
- /* Z Axis ticks Parallel to Y AXis*/
- gl.glBegin (GL2.GL_LINES);
- gl.glColor3f(0.0f,1.0f,0.0f);
- gl.glVertex3f(0.0f,-0.2f,-1.0f);
- gl.glVertex3f(0.0f,0.2f,-1.0f);
- gl.glPopMatrix();
- gl.glEnd();
- gl.glBegin (GL2.GL_LINES);
- gl.glColor3f(0.0f,1.0f,0.0f);
- gl.glVertex3f(0.0f,-0.2f,1.0f);
- gl.glVertex3f(0.0f,0.2f,1.0f);
- gl.glPopMatrix();
- gl.glEnd();
- /* Draw vectors*/
- for(Vector vector : vectors) {
- gl.glBegin(GL2.GL_LINES);
- gl.glColor3f(1.0f,1.0f,1.0f);
- gl.glVertex3f(0.0f,0.0f,0.0f);
- gl.glVertex3f(vector.getX(),vector.getY(),vector.getZ());
- gl.glPopMatrix();
- gl.glEnd();
- }
- /* End List*/
- gl.glEndList();
- gl.glFinish();
- // Change to projection matrix.
- gl.glMatrixMode(GL2.GL_PROJECTION);
- gl.glLoadIdentity();
- //Calculate components from angle;
- Vector vec = this.rotate3DVector(this.XYAngle, this.cameraX, this.cameraY);
- // Perspective.
- float widthHeightRatio = (float) getWidth() / (float) getHeight();
- glu.gluPerspective(45.0f, widthHeightRatio, 1, 1000);
- glu.gluLookAt(vec.getX(), vec.getY(), this.cameraHeight, 0, 0, 0, 0, 0, 1);
- // Change back to model view matrix.
- gl.glMatrixMode(GL2.GL_MODELVIEW);
- gl.glLoadIdentity();
- }
- @Override
- public void dispose(GLAutoDrawable arg0) {
- //method body
- }
- @Override
- public void init(GLAutoDrawable drawable) {
- gl = drawable.getGL().getGL2();
- glu = new GLU();
- // Global settings.
- gl.glEnable(GL2.GL_DEPTH_TEST);
- gl.glDepthFunc(GL2.GL_LEQUAL);
- gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
- gl.glClearColor(0f, 0f, 0f, 1f);
- animator = new FPSAnimator(this, 24);
- animator.start();
- }
- @Override
- public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
- gl = drawable.getGL().getGL2();
- gl.glViewport(0, 0, width, height);
- }
- /* Rotate 3d vecotr*/
- public Vector rotate3DVector(float angle, float xComp, float yComp) {
- float[] pt = {xComp, yComp};
- AffineTransform.getRotateInstance(Math.toRadians(angle), 0, 0)
- .transform(pt, 0, pt, 0, 1); // specifying to use this double[] to hold coords
- float newX = pt[0];
- float newY = pt[1];
- return new Vector(newX, newY, 0);
- }
- /* Test on device with head*/
- public static void main(String[] args) {
- ArrayList<Vector> list = new ArrayList<>();
- list.add(new Vector(1.0f,0.5f,-0.5f));
- list.add(new Vector(-0.6f,0.2f,0.4f));
- GLVectorCanvas canvas = new GLVectorCanvas(list, 180.0f, 2.0f, 2.0f, -0.5f);
- //System.out.println(canvas.getSize());
- JFrame frame = new JFrame("GLVectorCanvas Frame");
- frame.getContentPane().add(canvas, BorderLayout.CENTER);
- frame.setSize(1000, 1000);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- canvas.requestFocus();
- canvas.addGLEventListener(canvas);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement