Package com.peterhi.ui

Source Code of com.peterhi.ui.GLCanvasBase

package com.peterhi.ui;

import java.awt.BorderLayout;
import java.awt.Frame;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLCapabilitiesImmutable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import com.jogamp.opengl.util.FPSAnimator;

public abstract class GLCanvasBase extends Canvas implements GLEventListener, Listener {

  private final Frame frame;
  private final GLCanvas canvas;
  private final FPSAnimator animator;
 
  public GLCanvasBase(Composite parent, int style) {
    super(parent, style | SWT.EMBEDDED);
   
    FillLayout layout = new FillLayout();
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    layout.spacing = 0;
    setLayout(layout);
   
    frame = SWT_AWT.new_Frame(this);
    BorderLayout frameLayout = new BorderLayout();
    frameLayout.setHgap(0);
    frameLayout.setVgap(0);
    frame.setLayout(frameLayout);
   
    GLProfile profile = GLProfile.getDefault();
    GLCapabilitiesImmutable capabilities = new GLCapabilities(profile);
    canvas = new GLCanvas(capabilities);
    canvas.addGLEventListener(this);
    frame.add(canvas, BorderLayout.CENTER);
   
    animator = new FPSAnimator(canvas, UI.FPS, true);
  }
 
  public final boolean isAnimating() {
    return animator.isAnimating();
  }
 
  public final void setAnimating(boolean value) {
    if (value) {
      animator.start();
    } else {
      animator.stop();
    }
  }
 
  @Override
  public void init(GLAutoDrawable drawable) {
    canvas.setAutoSwapBufferMode(false);
    GL gl = drawable.getGL();
    gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    onInit(drawable);
  }

  @Override
  public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    GL gl = drawable.getGL();
    gl.glViewport(x, y, width, height);
    onReshape(drawable, x, y, width, height);
  }
 
  @Override
  public void display(GLAutoDrawable drawable) {
    GL gl = drawable.getGL();
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    onDisplay(drawable);
    canvas.swapBuffers();
  }

  @Override
  public void dispose(GLAutoDrawable drawable) {
    animator.stop();
    onDispose(drawable);
  }
 
  @Override
  public void handleEvent(Event event) {
  }
 
  protected abstract void onInit(GLAutoDrawable drawable);
  protected abstract void onReshape(GLAutoDrawable drawable, int x, int y, int width, int height);
  protected abstract void onDisplay(GLAutoDrawable drawable);
  protected abstract void onDispose(GLAutoDrawable drawable);
}
TOP

Related Classes of com.peterhi.ui.GLCanvasBase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.