Skip to content
Snippets Groups Projects
Commit 3debe4e3 authored by Amit Kumar Mondal's avatar Amit Kumar Mondal
Browse files

Migration of 4D DSE Visualization to new DSE perspective

refs 2310
parent 84849ab5
No related branches found
No related tags found
No related merge requests found
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2012 ForTISS GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.graphicsGL.ui.editor;
import static org.fortiss.tooling.graphicsGL.ui.ToolingGraphicsGLUIActivator.getDefault;
import static org.fortiss.tooling.graphicsGL.ui.util.GLPrimitives.clearCanvasAndLoadIdentity;
import static org.fortiss.tooling.kernel.utils.LoggingUtils.error;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.MouseWheelListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.part.ViewPart;
import org.fortiss.tooling.graphicsGL.ui.camera.Camera;
import org.fortiss.tooling.graphicsGL.ui.draw.LwjglCanvas;
import org.fortiss.tooling.graphicsGL.ui.draw.LwjglRenderThread;
import org.fortiss.tooling.graphicsGL.ui.objects.ViewObjectBase;
import org.fortiss.tooling.graphicsGL.ui.picker.ModelObjectPicker;
import org.lwjgl.LWJGLException;
import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.Color;
/**
* Base class for 3D graphics views. Sub-classes must provide a {@link Camera} and a
* {@link ViewObjectBase}, which are used to render the view content. Sub-classes may react on
* mouse and key events by overriding the respective methods.
* <P>
* The rendering process is started by calling {@link #beginRenderLoop()}, which allows sub-classes
* to apply additional GL setup commands. Sub-classes may change the perspective by overriding
* {@link #getPerspective()} and return a vector with the field of view, Z near distance, and Z far
* distance. Note that all three must be greater zero.
* <P>
* After the rendering is done the picking process starts if both {@link #pickX} and {@link #pickY}
* are greater or equal to zero and smaller than the widget width and height, respectively.
* Sub-classes should update these variables when they want to pick an object from the scene. The
* {@link #pickTargetChanged()} method is called whenever the picked object changes.
*
* @see Graphical3DEditorBase
*
* @author mondal
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: 9675D49183892CE649E1C1C92560AC1A
*/
public abstract class Graphical3DViewBase<T extends EObject> extends ViewPart implements
MouseListener, MouseTrackListener, MouseMoveListener, MouseWheelListener, KeyListener,
Listener {
/** The 3D canvas. */
protected LwjglCanvas canvas;
/** The render thread. */
private LwjglRenderThread renderThread;
/** The object picker. */
private ModelObjectPicker picker = new ModelObjectPicker();
/** The current X position to be used during picking. */
protected int pickX = -1;
/** The current Y position to be used during picking. */
protected int pickY = -1;
/** The current picked object or {@code null} if no pickable object is at the picking location. */
protected Object pickTarget;
/** {@inheritDoc} */
@Override
public final void handleEvent(Event event) {
Rectangle b = canvas.getBounds();
Vector3f p = getPerspective();
canvas.setViewportAndPerspective(b.width, b.height, p.x, p.y, p.z);
}
/** Callback from render thread. */
private Runnable renderCallback = new Runnable() {
@Override
public void run() {
try {
performRendering();
performPicking(getCamera());
} catch(LWJGLException ex) {
error(getDefault(), ex.getMessage(), ex);
}
}
};
/**
* Called when the render loop starts, but after the camera was applied for rendering.
* Sub-classes should make GL calls to setup rendering. The default does nothing.
*/
@SuppressWarnings("unused")
protected void beginRenderLoop() throws LWJGLException {
// do nothing
}
/** Returns the current camera to be used. */
protected abstract Camera getCamera();
/**
* Sub-classes should provide the scene object here. The model object will be used for rendering
* and picking using {@link ViewObjectBase}'s respective methods.
*/
protected abstract ViewObjectBase getSceneModelObject();
/** Notification about the change of the pick target. The default does nothing. */
protected void pickTargetChanged() {
// do nothing
}
/**
* Returns the perspective projection (field of view, near distance, far distance). The default
* uses {@code 45.0f, 0.5f, 400.0f}.
*/
protected Vector3f getPerspective() {
return new Vector3f(45.0f, 0.5f, 400.0f);
}
/** Performs the object picking operation. */
private void performPicking(Camera c) {
// avoid unnecessary picking if mouse is outside the canvas
if(pickX < 0 || pickX > canvas.getBounds().width || pickY < 0 ||
pickY > canvas.getBounds().height) {
return;
}
Object p = picker.pickModelObject(pickX, pickY, getPerspective(), getSceneModelObject(), c);
if(pickTarget != p) {
pickTarget = p;
pickTargetChanged();
}
}
/** Performs the object rendering operation. */
private void performRendering() throws LWJGLException {
clearCanvasAndLoadIdentity(new Color(0.95f, 0.95f, 0.95f, 1.0f));
getCamera().applyForRendering();
beginRenderLoop();
getSceneModelObject().renderObject(getCamera());
}
/** {@inheritDoc} */
@Override
public void createPartControl(Composite parent) {
Composite c = new Composite(parent, SWT.NONE);
c.setLayout(new FillLayout());
GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);
c.setLayoutData(gd);
canvas = new LwjglCanvas(c);
canvas.addResizeHandler(this);
canvas.addMouseListener(this);
canvas.addMouseMoveListener(this);
canvas.addMouseTrackListener(this);
canvas.addMouseWheelListener(this);
canvas.addKeyListener(this);
Rectangle b = canvas.getBounds();
Vector3f p = getPerspective();
canvas.setViewportAndPerspective(b.width, b.height, p.x, p.y, p.z);
renderThread = new LwjglRenderThread(canvas, renderCallback);
renderThread.start();
}
/** {@inheritDoc} */
@Override
public void setFocus() {
canvas.setFocus();
}
/** {@inheritDoc} */
@Override
public void mouseDoubleClick(MouseEvent e) {
// ignore
}
/** {@inheritDoc} */
@Override
public void mouseDown(MouseEvent e) {
// ignore
}
/** {@inheritDoc} */
@Override
public void mouseUp(MouseEvent e) {
// ignore
}
/** {@inheritDoc} */
@Override
public void mouseEnter(MouseEvent e) {
// ignore
}
/**
* {@inheritDoc}
* <P>
* Resets the picking position to (-1, -1).
*/
@Override
public void mouseExit(MouseEvent e) {
pickX = -1;
pickY = -1;
}
/** {@inheritDoc} */
@Override
public void mouseHover(MouseEvent e) {
// ignore
}
/** {@inheritDoc} */
@Override
public void mouseMove(MouseEvent e) {
// ignore
}
/** {@inheritDoc} */
@Override
public void mouseScrolled(MouseEvent e) {
// ignore
}
/** {@inheritDoc} */
@Override
public void keyPressed(KeyEvent e) {
// ignore
}
/** {@inheritDoc} */
@Override
public void keyReleased(KeyEvent e) {
// ignore
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment