open-axiom repository from github
/*1Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.2All rights reserved.3Copyright (C) 2007-2011, Gabriel Dos Reis.4All rights reserved.56Redistribution and use in source and binary forms, with or without7modification, are permitted provided that the following conditions are8met:910- Redistributions of source code must retain the above copyright11notice, this list of conditions and the following disclaimer.1213- Redistributions in binary form must reproduce the above copyright14notice, this list of conditions and the following disclaimer in15the documentation and/or other materials provided with the16distribution.1718- Neither the name of The Numerical Algorithms Group Ltd. nor the19names of its contributors may be used to endorse or promote products20derived from this software without specific prior written permission.2122THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS23IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED24TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A25PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER26OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,27EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,28PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR29PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF30LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING31NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS32SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.33*/3435#define _MAIN3D_C36#include "openaxiom-c-macros.h"3738#include <string.h>39#include <stdio.h>40#include <stdlib.h>41#include <unistd.h>42#include <signal.h>4344#include "cfuns.h"45#include "header.h"46#include "cpanel.h"47#include "process.h"48#include "bsdsignal.h"49#include "util.H1"50#include "Gfun.H1"51#include "XSpadFill.h"52#include "XShade.h"53#include "all_3d.H1"5455using namespace OpenAxiom;5657/**********************/58/** global variables **/59/**********************/6061unsigned long * spadColors;62Display *dsply;63int scrn;64XFontStruct *globalFont, *buttonFont, *headerFont, *titleFont, *graphFont,65*lightingFont, *volumeFont, *quitFont, *saveFont,*serverFont;66char scaleReport[5];67char deltaXReport[5], deltaYReport[5];68int followMouse=no,69viewportKeyNum=0;70Window rtWindow;71GC globalGC1, globalGC2, anotherGC, globGC, trashGC,72controlMessageGC, lightingGC, volumeGC, quitGC,73saveGC, graphGC, componentGC, opaqueGC, renderGC;74XSizeHints viewSizeHints;75HashTable *table;76Colormap colorMap;77int Socket=1, ack=1;7879GC processGC;80viewPoints *viewport;81controlPanelStruct *control;82XGCValues gcVals;83const char* s;84int someInt;8586/* check /usr/include/X11 for current implementation of87pixels (e.g. BlackPixel()) */88unsigned long foregroundColor, backgroundColor;8990/** totalShades is initially set to totalShadesConst.91If X cannot allocate 8 shades for each hue, total-92Shades is decremented. there is currently only a check for93this value to be positive. --> something to add: change over94to monochrome if totalShades=0. just modify the spadcolors.c95file. spadcolors.c has been modified so that it returns the96value for totalShades. since the return value had previously97been unused, a modification in this way ensures continued98support of other routines calling this function (e.g.99hypertex stuff). **/100101int mono, totalColors, totalSolid, totalDithered, totalHues,102totalSolidShades, totalDitheredAndSolids,totalShades;103104int drawMore;105106int spadMode=no, /* yes if receiving OpenAxiom command and107calling drawViewport */108spadDraw=no; /* yes if drawing viewport for109an OpenAxiom command */110int spadSignalReceived=0; /* yes if current state is a result of111a signal from OpenAxiom */112int inNextEvent=no; /* true just before a call to113XNextEvent */114jmp_buf jumpFlag;115116char errorStr[80];117118view3DStruct viewData;119120Window quitWindow, saveWindow;121122/** variables below assume only one viewport per process **/123124Window lightingWindow, lightingAxes;125float lightPointer[3], tempLightPointer[3];126127int axesXY[3][4];128float axesZ[3][2];129130float lightIntensity=1.0, tempLightIntensity;131float backLightIntensity = 1.0;132133/** used for writing viewport info out to a file **/134char filename[256];135136137/** used for draw viewport routines */138float sinTheta, sinPhi, cosTheta, cosPhi, viewScale,139viewScaleX, viewScaleY, viewScaleZ, reScale;140int xCenter, yCenter;141142XWindowAttributes vwInfo;143XWindowAttributes graphWindowAttrib;144145XPoint *quadMesh;146XImage *imageX;147int *xPts; /* pointer to projected points (x followed by y) */148float transform[4][4], transform1[4][4],149R[4][4], R1[4][4], S[4][4], T[4][4], I[4][4];150float A[4][4], B[4][4], D[4], E[4][4], F[4], array[4][4];151152153int scanline, polyCount;154polyList *scanList[ARRAY_HEIGHT];155float xleft = (float)0 ,xright = (float)ARRAY_WIDTH;156157colorBuffer cBuffer[ARRAY_WIDTH];158float zBuffer[ARRAY_WIDTH];159160float zC, dzdx, lum, point_norm[3];161float intersectColor[2], dcolor;162triple dpt, dnorm;163164/** eyePoint **/165float eyePoint[3];166167/** tube stuff **/168XPoint polygonMesh[20];169170/* bypass the hidden surface algorithm if no rotations, etc */171int saveFlag=no;172int firstTime=yes, noTrans = yes, startup = yes;173int redrawView = no; /* set to yes when returning from174subpanels */175int redoColor = no, pixelSetFlag = no, redoDither = no;176int redoSmooth = no, multiColorFlag = no;177178/* In order to set recalc to true (see draw.h) */179int finishedList=no, zoomed=yes, translated = yes,180changedIntensity, movingLight = no, writeImage = no,181rotated=yes, switchedPerspective, changedEyeDistance,182gotToggle = no;183poly *quickList;184185/** if not connected to OpenAxiom **/186int viewAloned;187188/** for drawing the box **/189viewTriple corners[8], clipCorners[8];190boxSideStruct box[6], clipBox[6];191192/** for freeing up points created frrom split polygons **/193viewTriple *splitPoints;194int resMax=0; /* number of points in the split point resevoir */195196/** view volume stuff **/197Window volumeWindow;198int frustrumVertex;199int doingPanel=CONTROLpanel; /* rewrite titles in proper panel */200int doingVolume;201202int screenX; /* global floating point indicating mouse position203on frustrum screen */204float xClipMinN, xClipMaxN, /* normalized values for205clip volume */206yClipMinN, yClipMaxN,207zClipMinN, zClipMaxN,208clipValue; /* mouse input */209float pzMin, pzMax; /* for a given (theta,phi): calculated in210drawViewport(), used in drawFrustrum() */211212/** B/W shades **/213int maxGreyShade=0;214/** events from the viewport manager **/215char propertyName[14];216char propertyBuffer[256];217218/* global ps variables */219int psInit=no; /* need to call globaInitPs() each run */220GCptr GChead=NULL; /* ptr to head of ps GC linked list */221char *PSfilename; /* output file name used in user directory */222char *envAXIOM; /* used for ps file paths */223224/** Resource database **/225XrmDatabase rDB;226227/** variables used for smooth shading **/228int smoothError = no;229Pixmap viewmap;230int viewmap_valid = 0;231float Cspec = 0.30;232float Cdiff = 0.4;233float Camb = 0.3;234float coeff = 35.0;235float saturation = 0.8;236int smoothHue;237int smoothConst = 50;238239240241int242the_handler(Display *display,XErrorEvent *event)243{244char buffer[512];245XGetErrorText(display,event->error_code,buffer,511);246fprintf(stderr,"%s\n",buffer);247return(0);248}249250int251main(void)252{253254XGCValues controlGCVals;255int i;256257char property[256];258char *prop = &property[0];259char *str_type[20];260XrmValue value;261262263/**** Global inits ****/264splitPoints = NIL(viewTriple);265266/**** Set up display ****/267if ((dsply = XOpenDisplay(oa_getenv("DISPLAY"))) == NULL)268{fprintf(stderr,"Could not open display.\n");exit (-1);}269scrn = DefaultScreen(dsply);270rtWindow = RootWindow(dsply,scrn);271XSetErrorHandler(the_handler);272/* XSynchronize(dsply,False); */273274/**** link Xwindows to viewports - X10 feature ****/275table = XCreateAssocTable(nbuckets);276277/**** Create OpenAxiom color map ****/278totalShades = 0;279totalColors = XInitSpadFill(dsply,scrn,&colorMap,280&totalHues,&totalSolidShades,281&totalDitheredAndSolids,&totalShades);282if (totalColors < 0) {283fprintf(stderr,"ERROR: Could not allocate all the necessary colors.\n");284exitWithAck(RootWindow(dsply,scrn),Window,-1);285}286287288mergeDatabases();289290/*** Determine whether monochrome or color is used ***/291if (XrmGetResource(rDB,"Axiom.3D.monochrome","",str_type,&value) == True){292(void) strncpy(prop,value.addr,(int)value.size);293}294else {295(void) strcpy(prop, "off");296}297298299mono = ((totalSolid == 2) || (strcmp(prop,"on") == 0));300if (mono) maxGreyShade=XInitShades(dsply,scrn) ;301302if (XrmGetResource(rDB,"Axiom.3D.inverse","",str_type,&value) == True){303(void) strncpy(prop,value.addr,(int)value.size);304}305else {306(void) strcpy(prop, "off");307}308309if (mono) {310if (strcmp(prop,"on") == 0) { /* 0 if equal - inverse video */311foregroundColor = white;312backgroundColor = black;313} else { /* off - no inverse video */314foregroundColor = black;315backgroundColor = white;316}317} else { /* inverse of inverse in color (for some strange reason) */318if (strcmp(prop,"on") == 0) { /* 0 if equal - inverse video */319foregroundColor = white;320backgroundColor = black;321} else { /* off - no inverse video */322foregroundColor = black;323backgroundColor = white;324}325}326327/* read default file name for postScript output */328if (XrmGetResource(rDB,"Axiom.3D.postscriptFile","",str_type,&value) == True){329(void) strncpy(prop,value.addr,(int)value.size);330}331else {332(void) strcpy(prop, "axiom3D.ps");333}334PSfilename = (char *)malloc(strlen(prop)+1);335strcpy(PSfilename,prop);336337XSync(dsply,0);338339/**** Open global fonts ****/340serverFont = XQueryFont(dsply,XGContextFromGC(DefaultGC(dsply,scrn)));341342if (XrmGetResource(rDB,"Axiom.3D.messageFont","Axiom.3D.Font",str_type,&value) == True){343(void) strncpy(prop,value.addr,(int)value.size);344}345else {346(void) strcpy(prop,messageFontDefault);347}348if ((globalFont = XLoadQueryFont(dsply, prop)) == NULL) {349fprintf(stderr, "Warning: could not get the %s font for messageFont\n",prop);350globalFont = serverFont;351}352353if (XrmGetResource(rDB,"Axiom.3D.buttonFont","Axiom.3D.Font",str_type,&value) == True){354(void) strncpy(prop,value.addr,(int)value.size);355}356else {357(void) strcpy(prop,buttonFontDefault);358}359if ((buttonFont = XLoadQueryFont(dsply, prop)) == NULL) {360fprintf(stderr, "Warning: could not get the %s font for buttonFont\n",prop);361buttonFont = serverFont;362}363364if (XrmGetResource(rDB,"Axiom.3D.headerFont","Axiom.3D.Font",str_type,&value) == True){365(void) strncpy(prop,value.addr,(int)value.size);366}367else {368(void) strcpy(prop,headerFontDefault);369}370if ((headerFont = XLoadQueryFont(dsply, prop)) == NULL) {371fprintf(stderr, "Warning: could not get the %s font for headerFont\n",prop);372headerFont = serverFont;373}374375if (XrmGetResource(rDB,"Axiom.3D.titleFont","Axiom.3D.Font",str_type,&value) == True){376(void) strncpy(prop,value.addr,(int)value.size);377}378else {379(void) strcpy(prop,titleFontDefault);380}381if ((titleFont = XLoadQueryFont(dsply, prop)) == NULL) {382fprintf(stderr, "Warning: could not get the %s font for titleFont\n",prop);383titleFont = serverFont;384}385386if (XrmGetResource(rDB,"Axiom.3D.lightingFont","Axiom.3D.Font",str_type,&value) == True){387(void) strncpy(prop,value.addr,(int)value.size);388}389else {390(void) strcpy(prop,lightingFontDefault);391}392if ((lightingFont = XLoadQueryFont(dsply, prop)) == NULL) {393fprintf(stderr, "Warning: could not get the %s font for lightingFont\n",prop);394lightingFont = serverFont;395}396397if (XrmGetResource(rDB,"Axiom.3D.volumeFont","Axiom.3D.Font",str_type,&value) == True){398(void) strncpy(prop,value.addr,(int)value.size);399}400else {401(void) strcpy(prop,volumeFontDefault);402}403if ((volumeFont = XLoadQueryFont(dsply, prop)) == NULL) {404fprintf(stderr, "Warning: could not get the %s font for volumeFont\n",prop);405volumeFont = serverFont;406407}408/**** Create widely used Graphic Contexts ****/409410411PSGlobalInit();412/* must initiate before using any G/PS functions */413/* need character name: used as postscript GC variable */414/* need to create ps GCs for all GCs used by drawing in viewWindow */415416/* globalGC1 */417controlGCVals.foreground = monoColor(axesColor);418controlGCVals.background = backgroundColor;419globalGC1 = XCreateGC(dsply,rtWindow,GCForeground |420GCBackground ,&controlGCVals);421carefullySetFont(globalGC1,globalFont);422PSCreateContext(globalGC1, "globalGC1", psNormalWidth, psButtCap,423psMiterJoin, psWhite, psBlack);424425/* controlMessageGC */426controlGCVals.foreground = controlMessageColor;427controlGCVals.background = backgroundColor;428controlMessageGC = XCreateGC(dsply,rtWindow,GCForeground |429GCBackground ,&controlGCVals);430carefullySetFont(controlMessageGC,globalFont);431432/* globalGC2 */433controlGCVals.foreground = monoColor(labelColor);434globalGC2 = XCreateGC(dsply,rtWindow,GCForeground,&controlGCVals);435carefullySetFont(globalGC2,buttonFont);436PSCreateContext(globalGC2, "globalGC2", psNormalWidth, psButtCap,437psMiterJoin, psWhite, psBlack);438439/* trashGC */440controlGCVals.function = GXcopy;441trashGC = XCreateGC(dsply,rtWindow,0 ,&controlGCVals);442carefullySetFont(trashGC,buttonFont);443PSCreateContext(trashGC, "trashGC", psNormalWidth, psButtCap,444psMiterJoin, psWhite, psBlack);445446/* componentGC */447componentGC = XCreateGC(dsply,rtWindow,0 ,&controlGCVals);448carefullySetFont(componentGC,buttonFont);449PSCreateContext(componentGC, "componentGC", psNormalWidth, psButtCap,450psMiterJoin, psWhite, psBlack);451452/* opaqueGC */453opaqueGC = XCreateGC(dsply,rtWindow,0 ,&controlGCVals);454carefullySetFont(opaqueGC,buttonFont);455PSCreateContext(opaqueGC, "opaqueGC", psNormalWidth, psButtCap,456psMiterJoin, psWhite, psBlack);457458/* renderGC */459renderGC = XCreateGC(dsply,rtWindow,0,&controlGCVals);460carefullySetFont(renderGC,buttonFont);461PSCreateContext(renderGC, "renderGC", psNormalWidth, psButtCap,462psMiterJoin, psWhite, psBlack);463464/* globGC */465globGC = XCreateGC(dsply,rtWindow,0,&controlGCVals);466carefullySetFont(globGC,headerFont);467PSCreateContext(globGC, "globGC", psNormalWidth, psButtCap,468psMiterJoin, psWhite, psBlack);469470/* anotherGC */471controlGCVals.line_width = colorWidth;472anotherGC = XCreateGC(dsply,rtWindow,GCBackground | GCLineWidth |473GCFunction ,&controlGCVals);474carefullySetFont(anotherGC,titleFont);475PSCreateContext(anotherGC, "anotherGC", psNormalWidth, psButtCap,476psMiterJoin, psWhite, psBlack);477478/* also create one for rendering (grayscale only for now) */479/* assign arbitrary number to renderGC as 9991 - see header.h */480PSCreateContext(GC9991, "renderGC", psNormalWidth, psButtCap,481psRoundJoin, psWhite, psBlack );482483484/* processGC */485gcVals.background = backgroundColor;486processGC = XCreateGC(dsply,rtWindow,GCBackground |487GCFillStyle,&gcVals);488carefullySetFont(processGC,buttonFont);489490/* lightingGC */491controlGCVals.foreground = monoColor(axesColor);492controlGCVals.background = backgroundColor;493lightingGC = XCreateGC(dsply,rtWindow,GCForeground | GCBackground494,&controlGCVals);495carefullySetFont(lightingGC,lightingFont);496497498/* volumeGC */499volumeGC = XCreateGC(dsply,rtWindow,GCForeground | GCBackground500,&controlGCVals);501carefullySetFont(volumeGC,volumeFont);502503/* quitGC */504quitGC = XCreateGC(dsply,rtWindow,GCForeground | GCBackground505,&controlGCVals);506carefullySetFont(quitGC,buttonFont);507508/* saveGC */509saveGC = XCreateGC(dsply,rtWindow,GCForeground | GCBackground510,&controlGCVals);511carefullySetFont(saveGC,buttonFont);512513514/* graphGC */515graphGC = XCreateGC(dsply,rtWindow,GCForeground | GCBackground516,&controlGCVals);517carefullySetFont(graphGC,buttonFont);518519/**** Get Data from the Viewport Manager ****/520521i = 123; /* Used in viewman, what is this for? */522check(write(Socket,&i,intSize));523524/* Check if I am getting stuff from OpenAxiom or, if I am viewAlone. */525readViewman(&viewAloned,intSize);526readViewman(&viewData,sizeof(view3DStruct));527readViewman(&i,intSize);528529if (!(viewData.title = (char *)saymem("main.c",i,sizeof(char)))) {530fprintf(stderr,"VIEW3D: Fatal Error>> Ran out of memory trying to receive\531the title.\n");532exitWithAck(RootWindow(dsply,scrn),Window,-1);533}534readViewman(viewData.title,i);535536readViewman(&(viewData.lightVec[0]),floatSize);537readViewman(&(viewData.lightVec[1]),floatSize);538readViewman(&(viewData.lightVec[2]),floatSize);539540viewData.scaleDown = yes;541542switch (viewData.typeOf3D) {543544/* Currently, the view3DType information doesn't get sent from545OpenAxiom - all surfaces are alike regardless of how they546were created. We may revert back to receiving this information547in case we want to take advantage of certain properties of548certain surfaces (e.g. z=f(x,y)). */549550case view3DType:551case viewTubeType:552viewport = make3DComponents();553viewData.box = no;554viewData.pointSize = 3;555break;556}; /* switch typeOf3D */557558559/*************************************************560** Do some temporary assignments that would **561** later be coded in the makeViewport routines **562** when the corresponding code has been put **563** into the viewAlone, viewman and spad files. **564*************************************************/565566viewData.distortX = viewData.distortY = viewData.distortZ = 1;567viewData.clipPlane = clipPlaneMin;568viewData.clipStuff = yes;569570xClipMinN = yClipMinN = zClipMinN = 0.0;571xClipMaxN = yClipMaxN = zClipMaxN = 1.0;572573control = viewport->controlPanel;574575bsdSignal(SIGTERM,goodbye,DontRestartSystemCalls);576bsdSignal(SIGSEGV,goodbye,DontRestartSystemCalls);577578/** send acknowledgement to viewport manager**/579i = 345;580581sprintf(errorStr,"sending window info to viewport manager");582check(write(Socket,&(viewport->viewWindow),sizeof(Window)));583584viewmap = XCreatePixmap(dsply,viewport->viewWindow,585vwInfo.width,vwInfo.height,586DisplayPlanes(dsply,scrn));587viewmap_valid = 1;588589processEvents();590591goodbye(-1);592return(0); /* control never gets here but compiler complains */593} /* main() */594595596597void598mergeDatabases(void)599{600XrmDatabase homeDB,serverDB,applicationDB;601char filenamebuf[1024];602char *filename = &filenamebuf[0];603const char *classname = "OpenAxiom";604char name[255];605606(void) XrmInitialize();607(void) strcpy(name, "/usr/lib/X11/app-defaults/");608(void) strcat(name, classname);609applicationDB = XrmGetFileDatabase(name);610(void) XrmMergeDatabases(applicationDB, &rDB);611612if (XResourceManagerString(dsply) != NULL){613serverDB = XrmGetStringDatabase(XResourceManagerString(dsply));614}615else {616(void) strcpy(filename,oa_getenv("HOME"));617(void) strcat(filename,"/.Xdefaults");618serverDB = XrmGetFileDatabase(filename);619}620XrmMergeDatabases(serverDB,&rDB);621if ( oa_getenv ("XENVIRONMENT") == NULL) {622int len;623(void) strcpy(filename,oa_getenv("HOME"));624(void) strcat(filename,"/.Xdefaults-");625len = strlen(filename);626627(void) gethostname(filename+len,1024-len);628629}630else {631(void) strcpy (filename,oa_getenv ("XENVIRONMENT"));632}633homeDB = XrmGetFileDatabase(filename);634XrmMergeDatabases(homeDB,&rDB);635}636637638639