Initial commit, a stable version.
[bnac-editor.git] / src / cl.uchile.dcc.bnac.editor / PortraitGraph.java
blob791e0efaec9908ff7cf346fde73be5e95a99bb77
1 /*
2 * bnac : virtual museum environment creation/manipulation project
3 * Copyright (C) 2010 Felipe CaƱas Sabat
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package cl.uchile.dcc.bnac.editor;
21 import cl.uchile.dcc.bnac.PlacedCapture;
22 import cl.uchile.dcc.bnac.Capture2D;
23 import cl.uchile.dcc.bnac.Attribute;
25 import java.awt.image.BufferedImage;
26 import java.io.File;
27 import javax.imageio.ImageIO;
29 import javax.media.j3d.Appearance;
30 import javax.media.j3d.BoundingBox;
31 import javax.media.j3d.BranchGroup;
32 import javax.media.j3d.Geometry;
33 import javax.media.j3d.ImageComponent2D;
34 import javax.media.j3d.ImageComponent;
35 import javax.media.j3d.Material;
36 import javax.media.j3d.Shape3D;
37 import javax.media.j3d.Texture2D;
38 import javax.media.j3d.Texture;
39 import javax.media.j3d.TextureAttributes;
40 import javax.media.j3d.Transform3D;
41 import javax.media.j3d.TransformGroup;
42 import javax.media.j3d.TransparencyAttributes;
44 import javax.vecmath.Color3f;
45 import javax.vecmath.Point3d;
46 import javax.vecmath.Point2f;
47 import javax.vecmath.Point3f;
48 import javax.vecmath.Vector3d;
49 import javax.vecmath.Vector2f;
50 import javax.vecmath.Vector3f;
52 class PortraitGraph extends Element2DGraph
54 protected PlacedCapture pcap;
55 protected BranchGroup canvasNrm;
56 protected BranchGroup canvasSel;
57 protected boolean selected;
59 public PortraitGraph (Project project, PlacedCapture pc)
61 super(project);
62 pcap = pc;
63 selected = false;
65 Capture2D c = (Capture2D) pcap.capture;
66 dims.set(c.width(), c.height());
67 pos.set(pcap.t);
68 angle = pcap.angle;
69 scale = pcap.scale;
71 frbg = makeFrameGraph();
72 rtg.addChild(frbg);
73 rtg.addChild(ftg);
74 handle.addChild(rtg);
76 buildFace();
77 updatePosition();
78 updateDimensions();
79 updateAngle();
80 updateScale();
83 protected void buildFace ()
85 try {
86 Capture2D c = (Capture2D) pcap.capture;
88 BufferedImage img;
89 img = ImageIO.read(new File(
90 project.getRootPath() +
91 System.getProperty("file.separator") +
92 c.get(Attribute.Resource)));
93 BufferedImage pimg = Util.padToPowerOf2(img);
94 int w = img.getWidth();
95 int h = img.getHeight();
96 int pw = pimg.getWidth();
97 int ph = pimg.getHeight();
99 BnacEditor.trace("tex %dx%d", w, h);
100 Texture2D tex = new Texture2D(Texture.BASE_LEVEL, Texture.RGB,
101 pw, ph);
102 tex.setBoundaryModeS(Texture.CLAMP);
103 tex.setBoundaryModeT(Texture.CLAMP);
104 tex.setImage(0, new ImageComponent2D(
105 ImageComponent.FORMAT_RGB, pimg));
106 TextureAttributes tattr = new TextureAttributes();
107 tattr.setTextureMode(TextureAttributes.MODULATE);
109 Shape3D canvas;
110 Appearance ap;
111 Geometry geom =
112 Util.rectMesh(c.width(), c.height(),
113 -c.width()/2.0f, -c.height()/2.0f,
114 0.0f, divs, true, ((float) w)/pw, ((float) h)/ph);
116 canvasNrm = new BranchGroup();
117 canvasNrm.setCapability(BranchGroup.ALLOW_DETACH);
118 canvasSel = new BranchGroup();
119 canvasSel.setCapability(BranchGroup.ALLOW_DETACH);
121 ap = new Appearance();
122 ap.setTexture(tex);
123 ap.setTextureAttributes(tattr);
124 ap.setMaterial(new Material(
125 new Color3f(0.2f, 0.2f, 0.2f), // ambient
126 new Color3f(0.0f, 0.0f, 0.0f), // emissive
127 new Color3f(0.8f, 0.8f, 0.8f), // diffuse
128 new Color3f(0.0f, 0.0f, 0.0f), // specular
129 0.0f));
130 canvas = new Shape3D(geom, ap);
131 canvas.setPickable(true);
132 canvas.setBounds(new BoundingBox(
133 new Point3d(0.0, 0.0, 0.0),
134 new Point3d(c.width(), c.height(), -depth)));
135 canvas.setName(pcap.getId());
136 canvasNrm.addChild(canvas);
138 ap = new Appearance();
139 ap.setTexture(tex);
140 ap.setTextureAttributes(tattr);
141 ap.setMaterial(new Material(
142 new Color3f(0.0f, 0.0f, 0.0f), // ambient
143 new Color3f(0.8f, 0.0f, 0.0f), // emissive
144 new Color3f(0.0f, 0.0f, 0.0f), // diffuse
145 new Color3f(0.0f, 0.0f, 0.0f), // specular
146 0.0f));
147 TransparencyAttributes tra = new TransparencyAttributes(
148 TransparencyAttributes.FASTEST, 0.5f);
149 ap.setTransparencyAttributes(tra);
151 canvas = new Shape3D(geom, ap);
152 canvas.setPickable(true);
153 canvas.setBounds(new BoundingBox(
154 new Point3d(0.0, 0.0, 0.0),
155 new Point3d(c.width(), c.height(), -depth)));
156 canvas.setName(pcap.getId());
157 canvasSel.addChild(canvas);
159 ftg.addChild(canvasNrm);
160 } catch (Exception e) {
161 BnacEditor.error("when rendering %s", e, pcap.capture.getId());
165 public void select ()
167 if (selected) { return; }
168 BnacEditor.trace("selected %s", getId());
169 selected = true;
170 canvasNrm.detach();
171 ftg.addChild(canvasSel);
174 public void deselect ()
176 if (!selected) { return; }
177 BnacEditor.trace("deselected %s", getId());
178 selected = false;
179 canvasSel.detach();
180 ftg.addChild(canvasNrm);
183 public PlacedCapture getPlacedCapture () { return pcap; }
185 public String getId () { return pcap.getId(); }
187 public void updatePosition ()
189 super.updatePosition();
190 pcap.t.set(pos);
192 public void updateAngle ()
194 super.updateAngle();
195 pcap.angle = angle;
197 public void updateScale ()
199 super.updateScale();
200 pcap.scale = scale;