Moving from RenderStrategies to RocketRenderer subclasses.

This commit is contained in:
bkuker 2013-01-02 11:26:07 -05:00
parent e8ac5d4dea
commit a857fb93be
7 changed files with 116 additions and 103 deletions

View File

@ -8,12 +8,8 @@ import javax.media.opengl.GL2ES1;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.fixedfunc.GLLightingFunc;
import net.sf.openrocket.rocketcomponent.BodyTube;
import net.sf.openrocket.rocketcomponent.ExternalComponent;
import net.sf.openrocket.rocketcomponent.NoseCone;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.SymmetricComponent;
import net.sf.openrocket.rocketcomponent.Transition;
import net.sf.openrocket.startup.Application;
import net.sf.openrocket.util.Color;
@ -24,31 +20,8 @@ public class FigureRenderStrategy extends RenderStrategy {
super(null);
}
@Override
public boolean isDrawn(RocketComponent c) {
return true;
}
@Override
public boolean isDrawnTransparent(RocketComponent c) {
if (c instanceof BodyTube)
return true;
if (c instanceof NoseCone)
return false;
if (c instanceof SymmetricComponent) {
if (((SymmetricComponent) c).isFilled())
return false;
}
if (c instanceof Transition) {
Transition t = (Transition) c;
return !t.isAftShoulderCapped() && !t.isForeShoulderCapped();
}
return false;
}
private static final HashMap<Class<?>, Color> defaultColorCache = new HashMap<Class<?>, Color>();
@Override
public void preGeometry(GL2 gl, RocketComponent c, float alpha) {
gl.glLightModeli(GL2ES1.GL_LIGHT_MODEL_TWO_SIDE, 1);
Color figureColor = c.getColor();
@ -92,12 +65,6 @@ public class FigureRenderStrategy extends RenderStrategy {
}
@Override
public void postGeometry(GL2 gl, RocketComponent c, float alpha) {
//Nothing to do here
}
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();

View File

@ -0,0 +1,44 @@
package net.sf.openrocket.gui.figure3d;
import javax.media.opengl.GL2;
import net.sf.openrocket.rocketcomponent.BodyTube;
import net.sf.openrocket.rocketcomponent.NoseCone;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.SymmetricComponent;
import net.sf.openrocket.rocketcomponent.Transition;
public class FigureRenderer extends RocketRenderer {
protected FigureRenderer() {
super(new FigureRenderStrategy());
}
@Override
public void renderComponent(GL2 gl, RocketComponent c, float alpha) {
((FigureRenderStrategy)currentStrategy).preGeometry(gl, c, alpha);
cr.renderGeometry(gl, c);
}
@Override
public boolean isDrawn(RocketComponent c) {
return true;
}
@Override
public boolean isDrawnTransparent(RocketComponent c) {
if (c instanceof BodyTube)
return true;
if (c instanceof NoseCone)
return false;
if (c instanceof SymmetricComponent) {
if (((SymmetricComponent) c).isFilled())
return false;
}
if (c instanceof Transition) {
Transition t = (Transition) c;
return !t.isAftShoulderCapped() && !t.isForeShoulderCapped();
}
return false;
}
}

View File

@ -86,17 +86,6 @@ public class RealisticRenderStrategy extends RenderStrategy {
texCache = null;
}
@Override
public boolean isDrawn(RocketComponent c) {
return true;
}
@Override
public boolean isDrawnTransparent(RocketComponent c) {
return false;
}
@Override
public void preGeometry(GL2 gl, RocketComponent c, float alpha) {
if (needClearCache) {
clearCaches(gl);
@ -158,7 +147,6 @@ public class RealisticRenderStrategy extends RenderStrategy {
}
}
@Override
public void postGeometry(GL2 gl, RocketComponent c, float alpha) {
Appearance a = getAppearance(c);
Decal t = a.getTexture();

View File

@ -0,0 +1,31 @@
package net.sf.openrocket.gui.figure3d;
import javax.media.opengl.GL2;
import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.rocketcomponent.RocketComponent;
public class RealisticRenderer extends RocketRenderer {
public RealisticRenderer(OpenRocketDocument doc) {
super(new RealisticRenderStrategy(doc));
}
@Override
public void renderComponent(GL2 gl, RocketComponent c, float alpha) {
((RealisticRenderStrategy)currentStrategy).preGeometry(gl, c, alpha);
cr.renderGeometry(gl, c);
((RealisticRenderStrategy)currentStrategy).postGeometry(gl, c, alpha);
}
@Override
public boolean isDrawn(RocketComponent c) {
return true;
}
@Override
public boolean isDrawnTransparent(RocketComponent c) {
return false;
}
}

View File

@ -1,10 +1,8 @@
package net.sf.openrocket.gui.figure3d;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.rocketcomponent.RocketComponent;
public abstract class RenderStrategy {
@ -14,14 +12,6 @@ public abstract class RenderStrategy {
this.document = document;
}
public abstract boolean isDrawn(RocketComponent c);
public abstract boolean isDrawnTransparent(RocketComponent c);
public abstract void preGeometry(GL2 gl, RocketComponent c, float alpha);
public abstract void postGeometry(GL2 gl, RocketComponent c, float alpha);
public void updateFigure() {
}

View File

@ -21,6 +21,7 @@ import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.GLRunnable;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.fixedfunc.GLLightingFunc;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
@ -87,7 +88,7 @@ public class RocketFigure3d extends JPanel implements GLEventListener {
float[] lightPosition = new float[] { 1, 4, 1, 0 };
RocketRenderer rr = new RocketRenderer();
RocketRenderer rr = new FigureRenderer();
public RocketFigure3d(OpenRocketDocument document, Configuration config) {
this.document = document;
@ -638,13 +639,20 @@ public class RocketFigure3d extends JPanel implements GLEventListener {
this.csl = newListener;
}
public void setType(int t){
public void setType(final int t){
canvas.invoke(true, new GLRunnable() {
@Override
public boolean run(GLAutoDrawable drawable) {
rr.dispose(drawable);
if ( t == TYPE_FIGURE ){
rr.setRenderStrategy(new FigureRenderStrategy());
rr = new FigureRenderer();
} else {
rr.setRenderStrategy(new RealisticRenderStrategy(document));
rr = new RealisticRenderer(document);
}
repaint();
rr.init(drawable);
return false;
}
});
}
}

View File

@ -23,35 +23,23 @@ import net.sf.openrocket.util.Coordinate;
/*
* @author Bill Kuker <bkuker@billkuker.com>
*/
public class RocketRenderer {
public abstract class RocketRenderer {
@SuppressWarnings("unused")
private static final LogHelper log = Application.getLogger();
RenderStrategy currentStrategy = new FigureRenderStrategy();
RenderStrategy nextStrategy;
ComponentRenderer cr;
final RenderStrategy currentStrategy;
final ComponentRenderer cr = new ComponentRenderer();
private final float[] selectedEmissive = { 1, 0, 0, 1 };
private final float[] colorBlack = { 0, 0, 0, 1 };
public void setRenderStrategy(RenderStrategy r) {
nextStrategy = r;
}
private void checkRenderStrategy(GLAutoDrawable drawable) {
if (nextStrategy == null)
return;
currentStrategy.dispose(drawable);
nextStrategy.init(drawable);
currentStrategy = nextStrategy;
nextStrategy = null;
protected RocketRenderer(RenderStrategy s){
currentStrategy = s;
}
public void init(GLAutoDrawable drawable) {
cr = new ComponentRenderer();
cr.init(drawable);
currentStrategy.init(drawable);
}
public void dispose(GLAutoDrawable drawable) {
@ -66,7 +54,6 @@ public class RocketRenderer {
public RocketComponent pick(GLAutoDrawable drawable,
Configuration configuration, Point p, Set<RocketComponent> ignore) {
checkRenderStrategy(drawable);
final GL2 gl = drawable.getGL().getGL2();
gl.glEnable(GL.GL_DEPTH_TEST);
@ -86,7 +73,7 @@ public class RocketRenderer {
(byte) ((pickParts.size() << 4) & 0xF0), (byte) 1);
pickParts.add(c);
if (currentStrategy.isDrawnTransparent(c)) {
if (isDrawnTransparent(c)) {
gl.glEnable(GL.GL_CULL_FACE);
gl.glCullFace(GL.GL_FRONT);
cr.renderGeometry(gl, c);
@ -112,7 +99,6 @@ public class RocketRenderer {
public void render(GLAutoDrawable drawable, Configuration configuration,
Set<RocketComponent> selection) {
checkRenderStrategy(drawable);
if (cr == null)
throw new IllegalStateException(this + " Not Initialized");
@ -155,8 +141,8 @@ public class RocketRenderer {
// Draw all inner components
for (RocketComponent c : configuration) {
if (currentStrategy.isDrawn(c)) {
if (!currentStrategy.isDrawnTransparent(c)) {
if (isDrawn(c)) {
if (!isDrawnTransparent(c)) {
renderComponent(gl, c, 1.0f);
}
}
@ -169,8 +155,8 @@ public class RocketRenderer {
gl.glEnable(GL.GL_CULL_FACE);
gl.glCullFace(GL.GL_FRONT);
for (RocketComponent c : configuration) {
if (currentStrategy.isDrawn(c)) {
if (currentStrategy.isDrawnTransparent(c)) {
if (isDrawn(c)) {
if (isDrawnTransparent(c)) {
renderComponent(gl, c, 1.0f);
}
}
@ -182,8 +168,8 @@ public class RocketRenderer {
gl.glEnable(GL.GL_CULL_FACE);
gl.glCullFace(GL.GL_BACK);
for (RocketComponent c : configuration) {
if (currentStrategy.isDrawn(c)) {
if (currentStrategy.isDrawnTransparent(c)) {
if (isDrawn(c)) {
if (isDrawnTransparent(c)) {
renderComponent(gl, c, 0.2f);
}
}
@ -213,10 +199,9 @@ public class RocketRenderer {
}
public void renderComponent(GL2 gl, RocketComponent c, float alpha) {
currentStrategy.preGeometry(gl, c, alpha);
cr.renderGeometry(gl, c);
currentStrategy.postGeometry(gl, c, alpha);
}
public abstract void renderComponent(GL2 gl, RocketComponent c, float alpha);
public abstract boolean isDrawn(RocketComponent c);
public abstract boolean isDrawnTransparent(RocketComponent c);
}