home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / GLX / cutNpaste / Motif+Xt / glx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.6 KB  |  327 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <X11/Intrinsic.h>
  18. #include <gl.h>
  19. #include "data.h"
  20. #include <gl/sphere.h>
  21. #include <X11/Xirisw/GlxMDraw.h>
  22.  
  23. #define SPH_DEPTH_HI 8
  24. #define SPH_DEPTH_LO 4
  25.  
  26. extern void start_rtn(), rtn(), end_rtn(), 
  27.     start_approach(), approach(), end_approach();
  28.  
  29. static char *molTranslations = "#override\n\
  30.     <Btn1Down>:start_rtn() \n\
  31.     <Btn1Motion>:rtn() \n\
  32.     <Btn1Up>:end_rtn() \n\
  33. ";
  34.  
  35. static XtActionsRec actionsTable[] = {
  36.     {"start_rtn", start_rtn},
  37.     {"rtn", rtn},
  38.     {"end_rtn", end_rtn},
  39.     {"start_approach", start_approach},
  40.     {"approach", approach},
  41.     {"end_approach", end_approach},
  42. };
  43.  
  44. int prevx, prevy;
  45. Coord prevz, zoom = 0.0;
  46.  
  47. Angle xRot = 0, yRot = 0;
  48. int drawAxis = FALSE;
  49. struct element *elementRoot = NULL;
  50. struct molecule *moleculeRoot = NULL;
  51.  
  52. Matrix Identity = {
  53.     1, 0, 0, 0,
  54.     0, 1, 0, 0, 
  55.     0, 0, 1, 0,
  56.     0, 0, 0, 1 };
  57.  
  58. static float stuff[] = {
  59.     SPECULAR, .72, .8, .93,
  60.     SHININESS, 60,
  61.     LMNULL };
  62.  
  63. static float myLight[] = {
  64.     POSITION, 0, .5, 1, 0,
  65.     LMNULL };
  66.  
  67. static float myModel[] = {
  68.     LMNULL };
  69.  
  70. extern void drawScene();
  71.  
  72. /* GL widget creation code */
  73. #define DOUBLEBUFFER TRUE
  74.  
  75. extern Widget molw;
  76. extern XtAppContext app_context;
  77.  
  78. /* define the state for the GL widget */
  79. static GLXconfig glxConfig [] = {
  80.     { GLX_NORMAL, GLX_DOUBLE, TRUE },
  81.     { GLX_NORMAL, GLX_RGB, TRUE },
  82.     { GLX_NORMAL, GLX_ZSIZE, GLX_NOCONFIG },
  83.     { 0, 0, 0 }
  84. };
  85.  
  86. extern XtCallbackProc
  87.     initCB(),
  88.     exposeCB(),
  89.     resizeCB();
  90.  
  91.  
  92. Widget
  93. create_mol_widget(parent)
  94.     Widget parent;
  95. {
  96.     Widget w;
  97.     Arg args[5];
  98.     int n = 0;
  99.     XtTranslations trans = XtParseTranslationTable(molTranslations);
  100.  
  101.     XtAppAddActions(app_context, actionsTable, XtNumber(actionsTable));
  102.  
  103.     /* define the GL state of the window */
  104.     XtSetArg(args[n], GlxNglxConfig, glxConfig); n++;
  105.  
  106.     /* create the widget */
  107.     w = XtCreateWidget("mol", glxMDrawWidgetClass, parent, args, n);
  108.  
  109.     /* add the callbacks */
  110.     XtAddCallback(w, GlxNginitCallback, initCB, NULL);
  111.     XtAddCallback(w, GlxNexposeCallback, exposeCB, NULL);
  112.     XtAddCallback(w, GlxNresizeCallback, resizeCB, NULL);
  113.  
  114.     XtOverrideTranslations(w, trans);
  115.     XtManageChild(w);
  116.  
  117.     return w;
  118. }
  119.  
  120.  
  121. XtCallbackProc
  122. initCB(w, client, call)
  123.     Widget w;
  124.     caddr_t call;
  125.     caddr_t client;
  126. {
  127.     /* Mixed-model equivalent to winset() */
  128.     GLXwinset(XtDisplay(w), XtWindow(w));
  129.  
  130.     if (DOUBLEBUFFER)
  131.     doublebuffer();
  132.     zbuffer(TRUE);
  133.     RGBmode();
  134.     backface(TRUE);
  135.     
  136.     czclear(0x404040, getgdesc(GD_ZMAX));
  137.     if (DOUBLEBUFFER) {
  138.     swapbuffers();
  139.     czclear(0x404040, getgdesc(GD_ZMAX));
  140.     }
  141.     
  142.     mmode(MVIEWING);
  143.     loadmatrix(Identity);
  144.     perspective(600, 5.0/4.0, .25, 15.0);
  145.     translate(0, 0, -4);
  146.     lmdef(DEFMATERIAL, 1, 0, stuff);
  147.     lmdef(DEFLIGHT, 1, 0, myLight);
  148.     lmdef(DEFLMODEL, 1, 0, myModel);
  149.     
  150.     lmbind(MATERIAL, 1);
  151.     lmbind(LMODEL, 1);
  152.     lmbind(LIGHT0, 1);
  153.     
  154.     sphmode(SPH_DEPTH, SPH_DEPTH_HI);
  155.     sphmode(SPH_HEMI, TRUE);
  156.     sphmode(SPH_ORIENT, TRUE);
  157.  
  158. }
  159.  
  160. XtCallbackProc
  161. exposeCB(w, client, call)
  162.     Widget w;
  163.     caddr_t call;
  164.     caddr_t client;
  165. {
  166.     GLXwinset(XtDisplay(w), XtWindow(w));
  167.  
  168.     /* draw the scene */
  169.     drawScene();
  170.  
  171. }
  172.  
  173. XtCallbackProc
  174. resizeCB(w, client_data, call_data)
  175.     Widget w;
  176.     caddr_t client_data;
  177.     GlxDrawCallbackStruct *call_data;
  178. {
  179.     GLXwinset(XtDisplay(w), XtWindow(w));
  180.  
  181.     /* use the new size provided by the ResizeEvent */
  182.     viewport(0, (Screencoord) call_data->width-1,
  183.          0, (Screencoord) call_data->height-1);
  184.  
  185.     /* draw the scene at the new size */
  186.     drawScene();
  187. }
  188.  
  189. void doAxis() {
  190.     static float origin[] = {0, 0, 0};
  191.     static float x[] = {100, 0, 0};
  192.     static float y[] = {0, 100, 0};
  193.     static float z[] = {0, 0, 100};
  194.  
  195.     cpack(0x0000ff);
  196.     bgnline();
  197.     v3f(origin);
  198.     v3f(x);
  199.     endline();
  200.  
  201.     cpack(0x00ff00);
  202.     bgnline();
  203.     v3f(origin);
  204.     v3f(y);
  205.     endline();
  206.  
  207.     cpack(0xff0000);
  208.     bgnline();
  209.     v3f(origin);
  210.     v3f(z);
  211.     endline();
  212. }
  213.  
  214. void drawMolecule(struct molecule *m) {
  215.     struct atom* atoms;
  216.     float xsize, ysize, zsize, maxdim;
  217.     float v[4];
  218.     
  219.     if (!m)
  220.     return;
  221.     xsize = (m->max[0] - m->min[0]);
  222.     ysize = (m->max[1] - m->min[1]);
  223.     zsize = (m->max[2] - m->min[2]);
  224.     maxdim = MAX(MAX(xsize, ysize), zsize);
  225.     
  226.     pushmatrix();
  227.     lmcolor(LMC_AD);
  228.     scale(3/maxdim, 3/maxdim, 3/maxdim);
  229.     translate(-(m->min[0]+xsize/2),
  230.           -(m->min[1]+ysize/2),
  231.           -(m->min[2]+zsize/2));
  232.     atoms = m->atoms;
  233.     while(atoms) {
  234.     v[0] = atoms->xyz[0]; v[1] = atoms->xyz[1]; v[2] = atoms->xyz[2];
  235.     v[3] = atoms->e->radius;
  236.     cpack(atoms->e->color);
  237.     sphdraw(v);
  238.     atoms = atoms->next;
  239.     }
  240.     popmatrix();
  241.     lmcolor(LMC_COLOR);
  242. }
  243.  
  244. void drawScene() {
  245.     pushmatrix();
  246.     czclear(0x404040, getgdesc(GD_ZMAX));
  247.     
  248. /*    translate(0, 0, zoom);*/
  249.     rotate(xRot, 'x');
  250.     rotate(yRot, 'y');
  251.     drawMolecule(moleculeRoot);
  252.     if (drawAxis)
  253.     doAxis();
  254.     
  255.     if (DOUBLEBUFFER)
  256.     swapbuffers();
  257.     popmatrix();
  258. }
  259.  
  260. void
  261. start_rtn(w, event)
  262.     Widget w;
  263.     XEvent *event;
  264. {
  265.     /* go to low-res display */
  266.     sphmode(SPH_DEPTH, SPH_DEPTH_LO); /**/
  267.     prevx = event->xbutton.x;
  268.     prevy = event->xbutton.y;
  269. }
  270.  
  271. void
  272. rtn(w, event)
  273.     Widget w;
  274.     XEvent *event;
  275. {
  276.     int x = event->xbutton.x;
  277.     int y = event->xbutton.y;
  278.  
  279.     yRot += (Angle) 5 * (x - prevx);
  280.     xRot += (Angle) 5 * (y - prevy);
  281.     prevx = x;
  282.     prevy = y;
  283.     drawScene();
  284. }
  285.  
  286. void
  287. end_rtn(w, event)
  288.     Widget w;
  289.     XEvent *event;
  290. {
  291.     /* go back to hi-res display */
  292.     sphmode(SPH_DEPTH, SPH_DEPTH_HI); /**/
  293.     drawScene();
  294. }
  295.  
  296.  
  297. void start_approach(w, event)
  298.     Widget w;
  299.     XEvent *event;
  300. {
  301.     /* go to low-res display */
  302.     sphmode(SPH_DEPTH, SPH_DEPTH_LO); /**/
  303.     prevz = (Coord) event->xbutton.y;
  304. }
  305.  
  306. void approach(w, event)
  307.     Widget w;
  308.     XEvent *event;
  309. {
  310.     int y = event->xbutton.y;
  311.  
  312.     zoom += (Coord) (5 * (prevz - y))/100.0;
  313.     prevz = y;
  314.     drawScene();
  315. }
  316.  
  317. void end_approach(w, event)
  318.     Widget w;
  319.     XEvent *event;
  320. {
  321.     /* go back to hi-res display */
  322.     sphmode(SPH_DEPTH, SPH_DEPTH_HI); /**/
  323.     drawScene();
  324. }
  325.  
  326. void dokeyboard(){}
  327.