open-axiom repository from github
/*1Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.2All rights reserved.3Copyright (C) 2007-2008, 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 _PROJECT3D_C36#include "openaxiom-c-macros.h"37#include <string.h>3839#include "header.h"40#include "draw.h"41#include "mode.h" /* for #define components */4243#include "all_3d.H1"4445/*******************************************46* void project(aViewTriple,someXpoints,i) *47* *48* orthogonal projection for a point *49* setting the ith Xpoint as well *50*******************************************/5152void53project(viewTriple * aViewTriple,XPoint *someXpoints,int i)54{55float Vtmp[4], V[4], V1[4];5657V[0] = aViewTriple->x; V[1] = aViewTriple->y;58V[2] = aViewTriple->z; V[3] = 1.0;5960if (isNaNPoint(V[0], V[1], V[2])) {61(someXpoints+i)->x = aViewTriple->px = NotPoint;62(someXpoints+i)->y = aViewTriple->py = NotPoint;63return;64}6566V[0] -= viewport->transX; V[1] -= viewport->transY;67V[2] -= viewport->transZ;68vectorMatrix4(V,R1,Vtmp);6970matrixMultiply4x4(S,R,transform);71vectorMatrix4(Vtmp,transform,V1);7273aViewTriple->wx = V1[0]; aViewTriple->wy = V1[1];74aViewTriple->wz = V1[2];7576V1[0] *= reScale; V1[1] *= reScale; V1[2] *= reScale;7778aViewTriple->pz = V1[2];79if (viewData.perspective) {80V1[0] *= projPersp(aViewTriple->pz);81V1[1] *= projPersp(aViewTriple->pz);82}8384matrixMultiply4x4(I,T,transform);85vectorMatrix4(V1,transform,V);86V[0] = V[0]*viewScale + xCenter;87V[1] = vwInfo.height - (V[1]*viewScale + yCenter);8889(someXpoints+i)->x = aViewTriple->px = V[0];90(someXpoints+i)->y = aViewTriple->py = V[1];91}929394/***************************************************95* void projectAPoint(aViewTriple) *96* *97* orthogonal projection for a point. sort of *98* like the above, but no Xpoint assignment *99***************************************************/100101void102projectAPoint(viewTriple *aViewTriple)103{104float Vtmp[4], V[4], V1[4];105106V[0] = aViewTriple->x; V[1] = aViewTriple->y;107V[2] = aViewTriple->z; V[3] = 1.0;108109if (isNaNPoint(V[0], V[1], V[2])) {110aViewTriple->px = NotPoint;111aViewTriple->py = NotPoint;112return;113}114115V[0] -= viewport->transX; V[1] -= viewport->transY;116V[2] -= viewport->transZ;117vectorMatrix4(V,R1,Vtmp);118119matrixMultiply4x4(S,R,transform);120vectorMatrix4(Vtmp,transform,V1);121122aViewTriple->wx = V1[0]; aViewTriple->wy = V1[1];123aViewTriple->wz = V1[2];124125V1[0] *= reScale; V1[1] *= reScale; V1[2] *= reScale;126127aViewTriple->pz = V1[2];128if (viewData.perspective) {129V1[0] *= projPersp(aViewTriple->pz);130V1[1] *= projPersp(aViewTriple->pz);131}132133matrixMultiply4x4(I,T,transform);134vectorMatrix4(V1,transform,V);135V[0] = V[0]*viewScale + xCenter;136V[1] = vwInfo.height - (V[1]*viewScale + yCenter);137138aViewTriple->px = V[0];139aViewTriple->py = V[1];140}141142143/***************************144* void projectAllPoints() *145***************************/146147void148projectAllPoints(void)149{150151int i,j,k;152LLPoint *anLLPoint;153LPoint *anLPoint;154int *anIndex;155156anLLPoint = viewData.lllp.llp;157for (i=0; i<viewData.lllp.numOfComponents; i++,anLLPoint++) {158anLPoint = anLLPoint->lp;159for (j=0; j<anLLPoint->numOfLists; j++,anLPoint++) {160anIndex = anLPoint->indices;161for (k=0; k<anLPoint->numOfPoints; k++,anIndex++) {162projectAPoint(refPt3D(viewData,*anIndex));163} /* for points in LPoints (k) */164} /* for LPoints in LLPoints (j) */165} /* for LLPoints in LLLPoints (i) */166167} /* projectAllPoints() */168169170/*******************************171* void projectAllPolys(pList) *172* *173* orthogonal projection of *174* all the polygons in a given *175* list in one go. pz holds *176* the projected depth info *177* for hidden surface removal. *178* Polygons totally outside of *179* the window dimensions after *180* projection are discarded *181* from the list. *182*******************************/183184void185projectAllPolys (poly *pList)186{187188int i,clipped,clippedPz;189float x0=0.0;190float y0=0.0;191float xA=0.0;192float yA=0.0;193float xB=0.0;194float yB=0.0;195int *anIndex;196viewTriple *aPt;197198strcpy(control->message," Projecting Polygons ");199writeControlMessage();200201projectAllPoints();202for (;pList != NIL(poly);pList=pList->next) {203/* totalClip==yes => partialClip==yes (of course) */204pList->totalClipPz = yes; /* start with 1, AND all points with Pz<0 */205pList->partialClipPz = no; /* start with 0, OR any points with Pz<0 */206pList->totalClip = yes; /* same idea, only wrt clip volume */207pList->partialClip = no;208for (i=0,anIndex=pList->indexPtr; i<pList->numpts; i++,anIndex++) {209aPt = refPt3D(viewData,*anIndex);210clipped = outsideClippedBoundary(aPt->x, aPt->y, aPt->z);211pList->totalClip = pList->totalClip && clipped;212pList->partialClip = pList->partialClip || clipped;213clippedPz = behindClipPlane(aPt->pz);214pList->totalClipPz = pList->totalClipPz && clippedPz;215pList->partialClipPz = pList->partialClipPz || clippedPz;216217/* stuff for figuring out normalFacingOut, after the loop */218if (!i) {219x0 = aPt->px; y0 = aPt->py;220} else if (i==1) {221xA = x0 - aPt->px; yA = y0 - aPt->py;222x0 = aPt->px; y0 = aPt->py;223} else if (i==2) {224xB = aPt->px - x0; yB = aPt->py - y0;225}226} /* for i */227/* store face facing info */228/* For now, we shall give faces facing the user a factor of -1,229and faces facing away from the user a factor of +1. this is230to mimic the eye vector (pointing away from the user) dotted231into the surface normal.232This routine is being done because the surface normal in object233space does not transform over to image space linearly and so234has to be recalculated. but the triple product is zero in the235X and Y directions so we just take the Z component, of which,236we just examine the sign. */237if ((x0 = xA*yB - yA*xB) > machine0) pList->normalFacingOut = 1;238else if (x0 < machine0) pList->normalFacingOut = -1;239else pList->normalFacingOut = 0;240241}242strcpy(control->message,viewport->title);243writeControlMessage();244245} /* projectAllPolys */246247248249/*******************************250* void projectAPoly(p) *251* *252* orthogonal projection of *253* all a polygon. pz holds *254* the projected depth info *255* for hidden surface removal *256*******************************/257258259void260projectAPoly (poly *p)261{262263int i,clipped,clippedPz;264float Vtmp[4],V[4],V1[4];265float x0=0.0;266float y0=0.0;267float xA=0.0;268float yA=0.0;269float xB=0.0;270float yB=0.0;271272int *anIndex;273viewTriple *aPt;274275/* totalClip==yes => partialClip==yes */276p->totalClipPz = yes; /* start with 1, AND all points with Pz<0 */277p->partialClipPz = no; /* start with 0, OR any points with Pz<0 */278p->totalClip = yes; /* same idea, only with respect to clip volume */279p->partialClip = no;280for (i=0,anIndex=p->indexPtr; i<p->numpts; i++,anIndex++) {281aPt = refPt3D(viewData,*anIndex);282V[0] = aPt->x; V[1] = aPt->y; V[2] = aPt->z; V[3] = 1.0;283284V[0] -= viewport->transX; V[1] -= viewport->transY;285V[2] -= viewport->transZ;286vectorMatrix4(V,R1,Vtmp);287288matrixMultiply4x4(S,R,transform);289vectorMatrix4(Vtmp,transform,V1);290291aPt->wx = V1[0]; aPt->wy = V1[1]; aPt->wz = V1[2];292293V1[0] *= reScale; V1[1] *= reScale; V1[2] *= reScale;294295aPt->pz = V1[2];296if (viewData.perspective) {297V1[0] *= projPersp(V1[2]);298V1[1] *= projPersp(V1[2]);299}300301matrixMultiply4x4(I,T,transform);302vectorMatrix4(V1,transform,V);303V[0] = V[0]*viewScale + xCenter;304V[1] = vwInfo.height - (V[1]*viewScale + yCenter);305306aPt->px = V[0]; aPt->py = V[1];307308clipped = outsideClippedBoundary(aPt->x, aPt->y, aPt->z);309p->totalClip = p->totalClip && clipped;310p->partialClip = p->partialClip || clipped;311clippedPz = behindClipPlane(aPt->pz);312p->totalClipPz = p->totalClipPz && clippedPz;313p->partialClipPz = p->partialClipPz || clippedPz;314315/* stuff for figuring out normalFacingOut, after the loop */316if (!i) {317x0 = aPt->px; y0 = aPt->py;318} else if (i==1) {319xA = x0 - aPt->px; yA = y0 - aPt->py;320x0 = aPt->px; y0 = aPt->py;321} else if (i==2) {322xB = aPt->px - x0; yB = aPt->py - y0;323}324}325326if ((x0 = xA*yB - yA*xB) > machine0) p->normalFacingOut = 1;327else if (x0 < machine0) p->normalFacingOut = -1;328else p->normalFacingOut = 0;329330} /* projectAPoly */331332333334/**********************************335* void projectStuff(x,y,z,px,py) *336* *337* sort of like the project stuff *338* in tube.c but used exclusively *339* for the functions of two *340* variables. probably will need *341* to be changed later to be more *342* general (i.e. have everybody *343* use the viewTriple point *344* structure). *345**********************************/346347void348projectStuff(float x,float y,float z,int *px,int *py,float *Pz)349{350float tempx,tempy,tempz,temps,V[4],V1[4],stuffScale=100.0;351352tempx = viewport->scaleX;353tempy = viewport->scaleY;354tempz = viewport->scaleZ;355temps = viewScale;356357if (viewport->scaleX > 5.0) viewport->scaleX = 5.0;358if (viewport->scaleY > 5.0) viewport->scaleY = 5.0;359if (viewport->scaleZ > 3.0) viewport->scaleZ = 3.0;360if (viewScale > 5.0) viewScale = 5.0;361362V[0] = x; V[1] = y;363V[2] = z; V[3] = 1.0;364365V[0] -= viewport->transX*stuffScale;366V[1] -= viewport->transY*stuffScale;367V[2] -= viewport->transZ*stuffScale;368369matrixMultiply4x4(S,R,transform);370vectorMatrix4(V,transform,V1);371*Pz = V1[2];372373if (viewData.perspective) {374V1[0] *= projPersp(V1[2]);375V1[1] *= projPersp(V1[2]);376}377378matrixMultiply4x4(I,T,transform);379vectorMatrix4(V1,transform,V);380381V[0] = V[0]*viewScale + xCenter;382V[1] = vwInfo.height - (V[1]*viewScale + yCenter);383384*px = V[0];385*py = V[1];386387viewport->scaleX = tempx;388viewport->scaleY = tempy;389viewport->scaleZ = tempz;390viewScale = temps;391}392393394