summaryrefslogtreecommitdiffstats
path: root/demos/src/jgudemos/FontDrawerDemo.java
blob: 2bf0959506daed9694b0225a097abc7a0c0aefc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright (c) 2007 Erik Tollerud (erik.tollerud@gmail.com) All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * The names of Erik Tollerud, Sun Microsystems, Inc. or the names of
 * contributors may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ERIK TOLLERUD,
 * SUN MICROSYSTEMS, INC. ("SUN"), AND SUN'S LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL ERIK
 * TOLLERUD, SUN, OR SUN'S LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT
 * OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF ERIK
 * TOLLERUD OR SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed or intended for use
 * in the design, construction, operation or maintenance of any nuclear
 * facility.
 */

package jgudemos;

import net.java.joglutils.jogltext.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.Animator;

/**
 * This Demo application uses the Lighting class and the FontDrawer class to render 3D text into a GLJFrame.
 * See the console for keyboard commands and command line option instructions.
 * @author Erik J. Tollerud
 * @created January 12, 2007
 */
public class FontDrawerDemo {
    
    /**
     * Generates {@link net.java.joglutils.GLJFrame}
     * Generates a GLJFrame with a FontDrawer demo. Console output describes input.
     * @param args Command Line argument order: textDepth xRotspeed yRotspeed zRotspeed filled flatnormal
     *
     */
    
    
    static String helpString = "Keyboard Inputs (case sensitive)\n" +
            "r: toggle rotation\n" +
            "R: change rotation speeds\n" +
            "n: toggle flat normals\n" +
            "f: toggle filled text\n" +
            "t: edit text\n" +
            "</>: decrease/increase text depth\n" +
            "F: change font\n" +
            "s: resize font\n" +
            "l: light position\n" +
            "F1/?: Display help";
    
    static float[] light_ambient = { 0.0f, 0.0f, 0.0f, 1.0f };
    static float[] light_diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
    static float[] light_specular = { 0.0f, 0.0f, 0.0f, 0.0f };
    static float[] light_position = { 0.0f, 0.0f, 1.0f, 0.0f };
    static boolean rotating = true;
    
    
    public static void main(String[] args) {
        final float[] rotSteps = {0.0f, 0.3f, 0.0f};
        
        System.out.println("Option Command line argument order (first 4 numerical, last 2 boolean)\ntextDepth xRotspeed yRotspeed zRotspeed filled flatnormal");
        System.out.println(helpString);
        
        final String[] argsFin = args;
        Font font = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()[5];
        final FontDrawer dttf = new FontDrawer(font);
        final StringBuffer upperStr = new StringBuffer("0,0");
        final StringBuffer lowerStr = new StringBuffer("-1,-1");
        
        GLEventListener listener = new GLEventListener() {
            GLU glu;
            float xrot,yrot,zrot;
            float dpth = 0.2f;
            boolean filled = true, fnorm = true;
            //net.java.joglutils.lighting.Light lt;
            //net.java.joglutils.lighting.Material mt;
            
            
            public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
            }
            
            public void init(GLAutoDrawable drawable) {
                drawable.setGL(new DebugGL2(drawable.getGL().getGL2()));
                glu = new GLU();
                switch(argsFin.length) {
                    case 6:
                        fnorm = Boolean.parseBoolean(argsFin[5]);
                    case 5:
                        filled = Boolean.parseBoolean(argsFin[4]);
                    case 4:
                        rotSteps[2] = Float.parseFloat(argsFin[3]);
                    case 3:
                        rotSteps[1] = Float.parseFloat(argsFin[2]);
                    case 2:
                        rotSteps[0] = Float.parseFloat(argsFin[1]);
                    case 1:
                        dpth = Float.parseFloat(argsFin[0]);
                }
                dttf.setDepth(dpth);
                dttf.setFill(filled);
                dttf.setNormal(FontDrawer.NormalMode.FLAT);
                
                xrot = 0;yrot = 0;zrot = 0;
                GL2 gl = drawable.getGL().getGL2();
                // lt = new net.java.joglutils.lighting.Light(gl);
                //mt = new net.java.joglutils.lighting.Material(gl);
                
                // lt.setLightPosition(0,0,1);
                //   lt.setSpecular(new Color(0,0,0));
                //   lt.setAmbient(new Color(0,0,0));
                
                //  lt.enable();
                //  lt.apply();
                //mt.apply();
                gl.glColorMaterial( GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE ) ;
                float[] mamb = {0,0,0,0};
                gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT,mamb,0);
                
                gl.glEnable(GL2.GL_DEPTH_TEST);
                gl.glEnable(GL2.GL_LIGHTING);
                gl.glEnable(GL2.GL_LIGHT0);
                gl.glEnable(GL2.GL_NORMALIZE);
                
                
                
                
                gl.glClearColor(0.3f,0.5f,0.2f,0);
            }
            
            public void drawAxis(GL2 gl)  {
                gl.glDisable(GL2.GL_LIGHTING);
                gl.glBegin(GL2.GL_LINES);
                gl.glColor3f(1,0,0);
                gl.glVertex3i(0,0,0);
                gl.glVertex3i(10,0,0);
                gl.glColor3f(0,1,0);
                gl.glVertex3i(0,0,0);
                gl.glVertex3i(0,10,0);
                gl.glColor3f(0,0,1);
                gl.glVertex3i(0,0,0);
                gl.glVertex3i(0,0,10);
                gl.glEnd();
                gl.glEnable(GL2.GL_LIGHTING);
            }
            
            public void display(GLAutoDrawable drawable) {
                GL2 gl = drawable.getGL().getGL2();
                gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
                
                gl.glMatrixMode(GL2.GL_PROJECTION);
                gl.glLoadIdentity();
                //glu.gluPerspective(90,1,0.001,10);
                //gl.glFrustum(-1.5f,1.5f,-1.5f,1.5f,1,5);
                gl.glOrtho(-1.5,1.5,-1.5,1.5,-5,5);
                
                gl.glMatrixMode(GL2.GL_MODELVIEW);
                gl.glLoadIdentity();
                
                //Manual lighting activation before transform
                gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, light_ambient,0);
                gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_diffuse,0);
                gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_specular,0);
                gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position,0);
                
                glu.gluLookAt(0,0,2,0,0,0,0,1,0);
                //drawAxis(gl);
                gl.glRotatef(xrot,1.0f,0,0);
                gl.glRotatef(yrot,0,1.0f,0);
                gl.glRotatef(zrot,0,0,1.0f);
                
                
                
                if (rotating) {
                    xrot+=rotSteps[0];
                    yrot+=rotSteps[1];
                    zrot+=rotSteps[2];
                }
                
                
                
                drawAxis(gl);
                dttf.drawString(upperStr.toString(),glu,gl);
                dttf.drawString(lowerStr.toString(),glu,gl,-0.8f,-0.8f,0);
            }
            
            /**
             * No explicit cleanup necessary.
             */
            public void dispose(GLAutoDrawable drawable) {
            }
            
        };
        final net.java.joglutils.GLJFrame gljf = new net.java.joglutils.GLJFrame("FontDrawerDemo", listener, 600, 600);
        gljf.setDefaultCloseOperation(gljf.EXIT_ON_CLOSE);
        gljf.addKeyListener(new java.awt.event.KeyAdapter() {
            
            public void keyPressed(KeyEvent e) {
                char test = e.getKeyChar();
                switch (e.getKeyChar()) {
                    case 'f':
                        FontDrawer fd = dttf;
                        dttf.setFill(!dttf.isFill());
                        break;
                    case 'n':
                        switch(dttf.getNormal()) {
                            case NONE:
                                dttf.setNormal(FontDrawer.NormalMode.FLAT);
                                break;
                            case FLAT:
                                dttf.setNormal(FontDrawer.NormalMode.AVERAGED);
                                break;
                            case AVERAGED:
                                dttf.setNormal(FontDrawer.NormalMode.NONE);
                                break;
                        }
                        break;
                    case 'r':
                        Animator anim = gljf.getAnimator();
                        if(rotating) {
                            anim.stop();
                            rotating = false;
                        } else {
                            anim.start();
                            rotating = true;
                        }
                        break;
                    case 'R':
                        try {
                            String strRes;
                            strRes = JOptionPane.showInputDialog("X Rotation Speed?",Float.toString(rotSteps[0]));
                            if (strRes != null) {
                                rotSteps[0] = Float.parseFloat(strRes);
                                strRes = JOptionPane.showInputDialog("Y Rotation Speed?",Float.toString(rotSteps[1]));
                                if (strRes != null) {
                                    rotSteps[1] = Float.parseFloat(strRes);
                                    strRes = JOptionPane.showInputDialog("Z Rotation Speed?",Float.toString(rotSteps[2]));
                                    if (strRes != null)
                                        rotSteps[2] = Float.parseFloat(strRes);
                                }
                            }
                        } catch(Exception ignore) {
                        }
                        break;
                    case '>':
                    case '.':
                        float depthStep = dttf.getFont().getSize()/20.0f;
                        dttf.setDepth(dttf.getDepth() + depthStep);
                        break;
                    case '<':
                    case ',':
                        depthStep = dttf.getFont().getSize()/20.0f;
                        dttf.setDepth(dttf.getDepth() - depthStep);
                        break;
                    case 't':
                    case 'T':
                        String up = JOptionPane.showInputDialog("Upper Text:",upperStr.toString());
                        upperStr.delete(0,upperStr.length());
                        upperStr.append(up);
                        String dn = JOptionPane.showInputDialog("Lower Text:",lowerStr.toString());
                        lowerStr.delete(0,lowerStr.length());
                        lowerStr.append(dn);
                        break;
                    case 'F':
                        Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
                        
                        JPanel pan = new JPanel();
                        JComboBox cb = new JComboBox();
                        for (Font f : fonts)
                            cb.addItem(f);
                        cb.setSelectedItem(dttf.getFont());
                        pan.add(cb);
                        net.java.joglutils.JPanelDialog jpd = new net.java.joglutils.JPanelDialog("Choose Font",pan);
                        if (jpd.showAsModal())
                            dttf.setFont((Font)cb.getSelectedItem());
                        break;
                    case 's':
                        Font currFont = dttf.getFont();
                        String resultStr = JOptionPane.showInputDialog("Font Size?",Integer.toString(currFont.getSize()));
                        float targSize = Float.parseFloat(resultStr);
                        dttf.setFont(currFont.deriveFont(currFont.getStyle(),targSize));
                        break;
                    case 'l':
                        try {
                            String xStr = JOptionPane.showInputDialog(null,"Light x-position?",Float.toString(light_position[0]));
                            light_position[0] = Float.parseFloat(xStr);
                            String yStr = JOptionPane.showInputDialog(null,"Light y-position?",Float.toString(light_position[1]));
                            light_position[1] = Float.parseFloat(yStr);
                            String zStr = JOptionPane.showInputDialog(null,"Light z-position?",Float.toString(light_position[2]));
                            light_position[2] = Float.parseFloat(zStr);
                            String wStr = JOptionPane.showInputDialog(null,"Light w-position?",Float.toString(light_position[3]));
                            light_position[3] = Float.parseFloat(wStr);
                        } catch(Exception ignore) {
                        }
                        break;
                    case '?':
                        System.out.println(helpString);
                        JOptionPane.showMessageDialog(null,helpString);
                        break;
                    default:
                        switch (e.getKeyCode()) {
                            case KeyEvent.VK_F1:
                                System.out.println(helpString);
                                JOptionPane.showMessageDialog(null,helpString);
                                break;
                        }
                }
                gljf.repaint();
            }
        });
        gljf.generateAnimator();
        gljf.setVisible(true);
    }
}