]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/hud.cxx
f9b9f3a9749b9cac69fb896ef681d5ed2f79d22b
[flightgear.git] / src / Cockpit / hud.cxx
1 // hud.cxx -- hud defines and prototypes
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  - micheleamerica@geocities.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #include <simgear/compiler.h>
24 #include <simgear/structure/exception.hxx>
25
26 #include <string>
27 #include <fstream>
28
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #include <math.h>
34 #include <stdlib.h>
35 #include <stdio.h>              // char related functions
36 #include <string.h>             // strcmp()
37
38 #include <simgear/constants.h>
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/misc/sg_path.hxx>
41 #include <simgear/props/props_io.hxx>
42
43 #include <osg/GLU>
44
45 #include <Aircraft/aircraft.hxx>
46 //#include <Autopilot/xmlauto.hxx>
47 #include <GUI/new_gui.hxx>           // FGFontCache
48 #include <Main/globals.hxx>
49 #include <Scenery/scenery.hxx>
50 #include <Airports/runways.hxx>
51
52 #include "hud.hxx"
53
54
55 static HUD_Properties *HUDprop = 0;
56
57 static char units[5];
58
59 deque<SGSharedPtr<instr_item> > HUD_deque;
60
61 fgTextList HUD_TextList;
62 fgLineList HUD_LineList;
63 fgLineList HUD_StippleLineList;
64
65 fntRenderer *HUDtext = 0;
66 fntTexFont *HUD_Font = 0;
67 float HUD_TextSize = 0;
68 int HUD_style = 0;
69
70 float HUD_matrix[16];
71
72 int readHud( istream &input );
73 int readInstrument ( const SGPropertyNode * node);
74
75 static void drawHUD(osg::State*);
76 static void fgUpdateHUDVirtual(osg::State*);
77
78
79 class locRECT {
80 public:
81     RECT rect;
82
83     locRECT( UINT left, UINT top, UINT right, UINT bottom);
84     RECT get_rect(void) { return rect; }
85 };
86
87 locRECT :: locRECT( UINT left, UINT top, UINT right, UINT bottom)
88 {
89     rect.left   =  left;
90     rect.top    =  top;
91     rect.right  =  right;
92     rect.bottom =  bottom;
93
94 }
95 // #define DEBUG
96
97
98
99
100 int readInstrument(const SGPropertyNode * node)
101 {
102     static const SGPropertyNode *startup_units_node
103         = fgGetNode("/sim/startup/units");
104
105     instr_item *HIptr;
106
107     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
108         strcpy(units, " ft");
109     } else {
110         strcpy(units, " m");
111     }
112
113     const SGPropertyNode * ladder_group = node->getNode("ladders");
114
115     if (ladder_group != 0) {
116         int nLadders = ladder_group->nChildren();
117         for (int j = 0; j < nLadders; j++) {
118             HIptr = static_cast<instr_item *>(new HudLadder(ladder_group->getChild(j)));
119             HUD_deque.insert(HUD_deque.begin(), HIptr);
120         }
121     }
122
123     const SGPropertyNode * card_group = node->getNode("cards");
124     if (card_group != 0) {
125         int nCards = card_group->nChildren();
126         for (int j = 0; j < nCards; j++) {
127             const char *type = card_group->getChild(j)->getStringValue("type", "gauge");
128
129             if (!strcmp(type, "gauge"))
130                 HIptr = static_cast<instr_item *>(new gauge_instr(card_group->getChild(j)));
131             else if (!strcmp(type, "dial") || !strcmp(type, "tape"))
132                 HIptr = static_cast<instr_item *>(new hud_card(card_group->getChild(j)));
133             else {
134                 SG_LOG(SG_INPUT, SG_WARN, "HUD: unknown 'card' type: " << type);
135                 continue;
136             }
137             HUD_deque.insert(HUD_deque.begin(), HIptr);
138         }
139     }
140
141     const SGPropertyNode * label_group = node->getNode("labels");
142     if (label_group != 0) {
143         int nLabels = label_group->nChildren();
144         for (int j = 0; j < nLabels; j++) {
145             HIptr = static_cast<instr_item *>(new instr_label(label_group->getChild(j)));
146             HUD_deque.insert(HUD_deque.begin(), HIptr);
147         }
148     }
149
150     const SGPropertyNode * tbi_group = node->getNode("tbis");
151     if (tbi_group != 0) {
152         int nTbis = tbi_group->nChildren();
153         for (int j = 0; j < nTbis; j++) {
154             HIptr = static_cast<instr_item *>(new fgTBI_instr(tbi_group->getChild(j)));
155             HUD_deque.insert(HUD_deque.begin(), HIptr);
156         }
157     }
158
159     const SGPropertyNode * rwy_group = node->getNode("runways");
160     if (rwy_group != 0) {
161         int nRwy = rwy_group->nChildren();
162         for (int j = 0; j < nRwy; j++) {
163             HIptr = static_cast<instr_item *>(new runway_instr(rwy_group->getChild(j)));
164             HUD_deque.insert(HUD_deque.begin(), HIptr);
165         }
166     }
167     return 0;
168 } //end readinstrument
169
170
171 int readHud( istream &input )
172 {
173
174     SGPropertyNode root;
175
176     try {
177         readProperties(input, &root);
178     } catch (const sg_exception &e) {
179         guiErrorMessage("Error reading HUD: ", e);
180         return 0;
181     }
182
183
184     SG_LOG(SG_INPUT, SG_INFO, "Read properties for  " <<
185            root.getStringValue("name"));
186
187     if (!root.getNode("depreciated"))
188         SG_LOG(SG_INPUT, SG_ALERT, "WARNING: use of depreciated old HUD");
189
190     HUD_deque.erase( HUD_deque.begin(), HUD_deque.end());
191
192
193     SG_LOG(SG_INPUT, SG_INFO, "Reading Hud instruments");
194
195     const SGPropertyNode * instrument_group = root.getChild("instruments");
196     int nInstruments = instrument_group->nChildren();
197
198     for (int i = 0; i < nInstruments; i++) {
199
200         const SGPropertyNode * node = instrument_group->getChild(i);
201
202         SGPath path( globals->get_fg_root() );
203         path.append(node->getStringValue("path"));
204
205         SG_LOG(SG_INPUT, SG_INFO, "Reading Instrument "
206                << node->getName()
207                << " from "
208                << path.str());
209
210         SGPropertyNode root2;
211         try {
212             readProperties(path.str(), &root2);
213         } catch (const sg_exception &e) {
214             guiErrorMessage("Error reading HUD instrument: ", e);
215             continue;
216         }
217         readInstrument(&root2);
218     }//for loop(i)
219
220     return 0;
221 }
222
223
224 // fgHUDInit
225 //
226 // Constructs a HUD object and then adds in instruments. At the present
227 // the instruments are hard coded into the routine. Ultimately these need
228 // to be defined by the aircraft's instrumentation records so that the
229 // display for a Piper Cub doesn't show the speed range of a North American
230 // mustange and the engine readouts of a B36!
231 //
232 int fgHUDInit( fgAIRCRAFT * /* current_aircraft */ )
233 {
234
235     HUD_style = 1;
236
237     SG_LOG( SG_COCKPIT, SG_INFO, "Initializing current aircraft HUD" );
238
239     string hud_path =
240         fgGetString("/sim/hud/path", "Huds/Default/default.xml");
241     SGPath path(globals->get_fg_root());
242     path.append(hud_path);
243
244     ifstream input(path.c_str());
245     if (!input.good()) {
246         SG_LOG(SG_INPUT, SG_ALERT,
247                "Cannot read Hud configuration from " << path.str());
248     } else {
249         readHud(input);
250         input.close();
251     }
252
253     if ( HUDtext ) {
254         // this chunk of code is not necessarily thread safe if the
255         // compiler optimizer reorders these statements.  Note that
256         // "delete ptr" does not set "ptr = NULL".  We have to do that
257         // ourselves.
258         fntRenderer *tmp = HUDtext;
259         HUDtext = NULL;
260         delete tmp;
261     }
262
263     FGFontCache *fc = globals->get_fontcache();
264     const char* fileName = fgGetString("/sim/hud/font/name", "Helvetica.txf");
265     HUD_Font = fc->getTexFont(fileName);
266     if (!HUD_Font)
267         throw sg_io_exception("/sim/hud/font/name is not a texture font",
268                               sg_location(fileName));
269
270     HUD_TextSize = fgGetFloat("/sim/hud/font/size", 10);
271
272     HUDtext = new fntRenderer();
273     HUDtext->setFont(HUD_Font);
274     HUDtext->setPointSize(HUD_TextSize);
275     HUD_TextList.setFont( HUDtext );
276
277     if (!HUDprop)
278         HUDprop = new HUD_Properties;
279     return 0;  // For now. Later we may use this for an error code.
280
281 }
282
283
284 int fgHUDInit2( fgAIRCRAFT * /* current_aircraft */ )
285 {
286
287     HUD_style = 2;
288
289     SG_LOG( SG_COCKPIT, SG_INFO, "Initializing current aircraft HUD" );
290
291     SGPath path(globals->get_fg_root());
292     path.append("Huds/Minimal/default.xml");
293
294
295     ifstream input(path.c_str());
296     if (!input.good()) {
297         SG_LOG(SG_INPUT, SG_ALERT,
298                "Cannot read Hud configuration from " << path.str());
299     } else {
300         readHud(input);
301         input.close();
302     }
303
304     if (!HUDprop)
305         HUDprop = new HUD_Properties;
306     return 0;  // For now. Later we may use this for an error code.
307
308 }
309 //$$$ End - added, Neetha, 28 Nov 2k
310
311
312 // fgUpdateHUD
313 //
314 // Performs a once around the list of calls to instruments installed in
315 // the HUD object with requests for redraw. Kinda. It will when this is
316 // all C++.
317 //
318 void fgUpdateHUD( osg::State* state ) {
319
320     static const SGPropertyNode *enable3d_node = fgGetNode("/sim/hud/enable3d");
321     if ( HUD_style == 1 && enable3d_node->getBoolValue() ) {
322         fgUpdateHUDVirtual(state);
323         return;
324     }
325
326     static const float normal_aspect = float(640) / float(480);
327     // note: aspect_ratio is Y/X
328     float current_aspect = 1.0f/globals->get_current_view()->get_aspect_ratio();
329     if ( current_aspect > normal_aspect ) {
330         float aspect_adjust = current_aspect / normal_aspect;
331         float adjust = 320.0f*aspect_adjust - 320.0f;
332         fgUpdateHUD( state, -adjust, 0.0f, 640.0f+adjust, 480.0f );
333     } else {
334         float aspect_adjust = normal_aspect / current_aspect;
335         float adjust = 240.0f*aspect_adjust - 240.0f;
336         fgUpdateHUD( state, 0.0f, -adjust, 640.0f, 480.0f+adjust );
337     }
338 }
339
340 void fgUpdateHUDVirtual(osg::State* state)
341 {
342     FGViewer* view = globals->get_current_view();
343
344     // Standard fgfs projection, with essentially meaningless clip
345     // planes (we'll map the whole HUD plane to z=-1)
346     glMatrixMode(GL_PROJECTION);
347     glPushMatrix();
348     glLoadIdentity();
349     gluPerspective(view->get_v_fov(), 1/view->get_aspect_ratio(), 0.1, 10);
350
351     glMatrixMode(GL_MODELVIEW);
352     glPushMatrix();
353     glLoadIdentity();
354
355     // Standard fgfs view direction computation
356     float lookat[3];
357     lookat[0] = -sin(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
358     lookat[1] = tan(SG_DEGREES_TO_RADIANS * view->getPitchOffset_deg());
359     lookat[2] = -cos(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
360     if (fabs(lookat[1]) > 9999)
361         lookat[1] = 9999; // FPU sanity
362     gluLookAt(0, 0, 0, lookat[0], lookat[1], lookat[2], 0, 1, 0);
363
364     // Map the -1:1 square to a 55.0x41.25 degree wide patch at z=1.
365     // This is the default fgfs field of view, which the HUD files are
366     // written to assume.
367     float dx = 0.52056705; // tan(55/2)
368     float dy = dx * 0.75;  // assumes 4:3 aspect ratio
369     float m[16];
370     m[0] = dx; m[4] =  0; m[ 8] = 0; m[12] = 0;
371     m[1] =  0; m[5] = dy; m[ 9] = 0; m[13] = 0;
372     m[2] =  0; m[6] =  0; m[10] = 1; m[14] = 0;
373     m[3] =  0; m[7] =  0; m[11] = 0; m[15] = 1;
374     glMultMatrixf(m);
375
376     // Convert the 640x480 "HUD standard" coordinate space to a square
377     // about the origin in the range [-1:1] at depth of -1
378     glScalef(1./320, 1./240, 1);
379     glTranslatef(-320, -240, -1);
380
381     // Do the deed
382     drawHUD(state);
383
384     // Clean up our mess
385     glMatrixMode(GL_PROJECTION);
386     glPopMatrix();
387     glMatrixMode(GL_MODELVIEW);
388     glPopMatrix();
389 }
390
391
392 void fgUpdateHUD( osg::State* state, GLfloat x_start, GLfloat y_start,
393                   GLfloat x_end, GLfloat y_end )
394 {
395     glMatrixMode(GL_PROJECTION);
396     glPushMatrix();
397     glLoadIdentity();
398     gluOrtho2D(x_start, x_end, y_start, y_end);
399
400     glMatrixMode(GL_MODELVIEW);
401     glPushMatrix();
402     glLoadIdentity();
403
404     drawHUD(state);
405
406     glMatrixMode(GL_PROJECTION);
407     glPopMatrix();
408     glMatrixMode(GL_MODELVIEW);
409     glPopMatrix();
410 }
411
412
413 void drawHUD(osg::State* state)
414 {
415     if ( !HUD_deque.size() ) // Trust everyone, but ALWAYS cut the cards!
416         return;
417
418     HUD_TextList.erase();
419     HUD_LineList.erase();
420     // HUD_StippleLineList.erase();
421
422     glDisable(GL_DEPTH_TEST);
423     glDisable(GL_LIGHTING);
424
425     static const SGPropertyNode *heading_enabled
426         = fgGetNode("/autopilot/locks/heading", true);
427     static const SGPropertyNode *altitude_enabled
428         = fgGetNode("/autopilot/locks/altitude", true);
429
430     static char hud_hdg_text[256];
431     static char hud_wp0_text[256];
432     static char hud_wp1_text[256];
433     static char hud_wp2_text[256];
434     static char hud_alt_text[256];
435
436     glEnable(GL_BLEND);
437     if (HUDprop->isTransparent())
438         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
439     else
440         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
441
442     if (HUDprop->isAntialiased()) {
443         glEnable(GL_LINE_SMOOTH);
444         glAlphaFunc(GL_GREATER, HUDprop->alphaClamp());
445         glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
446         //glLineWidth(1.5);
447     } else {
448         //glLineWidth(1.0);
449     }
450
451     HUDprop->setColor();
452     for_each(HUD_deque.begin(), HUD_deque.end(), HUDdraw());
453
454     //HUD_TextList.add( fgText(40, 10, get_formated_gmt_time(), 0) );
455
456
457     int apY = 480 - 80;
458
459
460     if (strcmp( heading_enabled->getStringValue(), "dg-heading-hold") == 0 ) {
461         snprintf( hud_hdg_text, 256, "hdg = %.1f\n",
462                   fgGetDouble("/autopilot/settings/heading-bug-deg") );
463         HUD_TextList.add( fgText( 40, apY, hud_hdg_text ) );
464         apY -= 15;
465     } else if ( strcmp(heading_enabled->getStringValue(), "true-heading-hold") == 0 ) {
466         snprintf( hud_hdg_text, 256, "hdg = %.1f\n",
467                   fgGetDouble("/autopilot/settings/true-heading-deg") );
468         HUD_TextList.add( fgText( 40, apY, hud_hdg_text ) );
469         apY -= 15;
470
471         string wp0_id = fgGetString( "/autopilot/route-manager/wp[0]/id" );
472         if ( wp0_id.length() > 0 ) {
473             snprintf( hud_wp0_text, 256, "%5s %6.1fnm %s", wp0_id.c_str(),
474                       fgGetDouble( "/autopilot/route-manager/wp[0]/dist" ),
475                       fgGetString( "/autopilot/route-manager/wp[0]/eta" ) );
476             HUD_TextList.add( fgText( 40, apY, hud_wp0_text ) );
477             apY -= 15;
478         }
479         string wp1_id = fgGetString( "/autopilot/route-manager/wp[1]/id" );
480         if ( wp1_id.length() > 0 ) {
481             snprintf( hud_wp1_text, 256, "%5s %6.1fnm %s", wp1_id.c_str(),
482                       fgGetDouble( "/autopilot/route-manager/wp[1]/dist" ),
483                       fgGetString( "/autopilot/route-manager/wp[1]/eta" ) );
484             HUD_TextList.add( fgText( 40, apY, hud_wp1_text ) );
485             apY -= 15;
486         }
487         string wp2_id = fgGetString( "/autopilot/route-manager/wp-last/id" );
488         if ( wp2_id.length() > 0 ) {
489             snprintf( hud_wp2_text, 256, "%5s %6.1fnm %s", wp2_id.c_str(),
490                       fgGetDouble( "/autopilot/route-manager/wp-last/dist" ),
491                       fgGetString( "/autopilot/route-manager/wp-last/eta" ) );
492             HUD_TextList.add( fgText( 40, apY, hud_wp2_text ) );
493             apY -= 15;
494         }
495     }
496
497     if ( strcmp( altitude_enabled->getStringValue(), "altitude-hold" ) == 0 ) {
498         snprintf( hud_alt_text, 256, "alt = %.0f\n",
499                   fgGetDouble("/autopilot/settings/target-altitude-ft") );
500         HUD_TextList.add( fgText( 40, apY, hud_alt_text ) );
501         apY -= 15;
502     } else if ( strcmp( altitude_enabled->getStringValue(), "agl-hold" ) == 0 ){
503         snprintf( hud_alt_text, 256, "agl = %.0f\n",
504                   fgGetDouble("/autopilot/settings/target-agl-ft") );
505         HUD_TextList.add( fgText( 40, apY, hud_alt_text ) );
506         apY -= 15;
507     }
508
509     HUD_TextList.draw();
510     HUD_LineList.draw();
511
512     // glEnable(GL_LINE_STIPPLE);
513     // glLineStipple( 1, 0x00FF );
514     // HUD_StippleLineList.draw();
515     // glDisable(GL_LINE_STIPPLE);
516
517     if (HUDprop->isAntialiased()) {
518         glDisable(GL_ALPHA_TEST);
519         glDisable(GL_LINE_SMOOTH);
520         //glLineWidth(1.0);
521     }
522
523     if (HUDprop->isTransparent())
524         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
525
526     glEnable(GL_DEPTH_TEST);
527     glEnable(GL_LIGHTING);
528 }
529
530
531 void fgTextList::draw()
532 {
533     if (!Font)
534         return;
535
536     vector<fgText>::iterator curString = List.begin();
537     vector<fgText>::iterator lastString = List.end();
538
539     glPushAttrib(GL_COLOR_BUFFER_BIT);
540     glEnable(GL_TEXTURE_2D);
541     glEnable(GL_BLEND);
542     if (HUDprop->isTransparent())
543         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
544     else
545         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
546
547     if (HUDprop->isAntialiased()) {
548         glEnable(GL_ALPHA_TEST);
549         glAlphaFunc(GL_GREATER, HUDprop->alphaClamp());
550     }
551
552     Font->begin();
553     for (; curString != lastString; curString++)
554         curString->Draw(Font);
555     Font->end();
556
557     glDisable(GL_TEXTURE_2D);
558     glPopAttrib();
559 }
560
561
562 // HUD property listener class
563 //
564 HUD_Properties::HUD_Properties() :
565     _current(fgGetNode("/sim/hud/current-color", true)),
566     _visibility(fgGetNode("/sim/hud/visibility", true)),
567     _antialiasing(fgGetNode("/sim/hud/color/antialiased", true)),
568     _transparency(fgGetNode("/sim/hud/color/transparent", true)),
569     _red(fgGetNode("/sim/hud/color/red", true)),
570     _green(fgGetNode("/sim/hud/color/green", true)),
571     _blue(fgGetNode("/sim/hud/color/blue", true)),
572     _alpha(fgGetNode("/sim/hud/color/alpha", true)),
573     _alpha_clamp(fgGetNode("/sim/hud/color/alpha-clamp", true)),
574     _brightness(fgGetNode("/sim/hud/color/brightness", true)),
575     _visible(false),
576     _antialiased(false),
577     _transparent(false),
578     _a(0.67),
579     _cl(0.01)
580 {
581     _visibility->addChangeListener(this);
582     _antialiasing->addChangeListener(this);
583     _transparency->addChangeListener(this);
584     _red->addChangeListener(this);
585     _green->addChangeListener(this);
586     _blue->addChangeListener(this);
587     _alpha->addChangeListener(this);
588     _alpha_clamp->addChangeListener(this);
589     _brightness->addChangeListener(this);
590     _current->addChangeListener(this, true);
591 }
592
593
594 void HUD_Properties::valueChanged(SGPropertyNode *node)
595 {
596     if (!strcmp(node->getName(), "current-color")) {
597         int i = node->getIntValue();
598         if (i < 0)
599             i = 0;
600         SGPropertyNode *n = fgGetNode("/sim/hud/palette", true);
601         if ((n = n->getChild("color", i, false))) {
602             if (n->hasValue("red"))
603                 _red->setFloatValue(n->getFloatValue("red", 1.0));
604             if (n->hasValue("green"))
605                 _green->setFloatValue(n->getFloatValue("green", 1.0));
606             if (n->hasValue("blue"))
607                 _blue->setFloatValue(n->getFloatValue("blue", 1.0));
608             if (n->hasValue("alpha"))
609                 _alpha->setFloatValue(n->getFloatValue("alpha", 0.67));
610             if (n->hasValue("alpha-clamp"))
611                 _alpha_clamp->setFloatValue(n->getFloatValue("alpha-clamp", 0.01));
612             if (n->hasValue("brightness"))
613                 _brightness->setFloatValue(n->getFloatValue("brightness", 0.75));
614             if (n->hasValue("antialiased"))
615                 _antialiasing->setBoolValue(n->getBoolValue("antialiased", false));
616             if (n->hasValue("transparent"))
617                 _transparency->setBoolValue(n->getBoolValue("transparent", false));
618         }
619     }
620     _visible = _visibility->getBoolValue();
621     _transparent = _transparency->getBoolValue();
622     _antialiased = _antialiasing->getBoolValue();
623     float brt = _brightness->getFloatValue();
624     _r = clamp(brt * _red->getFloatValue());
625     _g = clamp(brt * _green->getFloatValue());
626     _b = clamp(brt * _blue->getFloatValue());
627     _a = clamp(_alpha->getFloatValue());
628     _cl = clamp(_alpha_clamp->getFloatValue());
629 }
630
631
632 void HUD_Properties::setColor() const
633 {
634     if (_antialiased)
635         glColor4f(_r, _g, _b, _a);
636     else
637         glColor3f(_r, _g, _b);
638 }
639
640