#include "camera_gl.h" #include "fileobject.h" #include /*number of houses in street*/ #define MAXHOUSE 20 /*number of streets*/ #define MAXSTREET 2 static FILE *fp; static GLint Height; /*height of window*/ static Camera_GL *TheCamera; static GObject *groundObj; /*root of the entire hierarchy*/ static GLint XC, YC; /*current mouse position*/ short Move = 0; short Rotate = 0; enum { M_PLANAR, M_FASTER, M_SLOWER, M_RFASTER, M_RSLOWER }; int usePlanarConstraint = 1; double velocity = 0.05; double angularVel = 0.005; static void moveVRP(void) /*move the VRP along the VPN*/ { Vector3D n; Point3D vrp; vrp = TheCamera->vrp; /*normalise the vpn - pity this happens also in camera code*/ normaliseVector3D(&TheCamera->vpn,&n); vrp.x += (velocity*n.x)*Move; vrp.y += (velocity*n.y)*Move; if (!usePlanarConstraint) { vrp.z += (velocity*n.z)*Move; } setVRP_GL(TheCamera,vrp.x,vrp.y,vrp.z); } static void rotateVPN(int x, int y) { double dx, dy; /*change in mouse position*/ double nx,ny,nz; /*new vpn*/ RotationMatrix r; /*trial and error value affecting angular velocity*/ /*difference between old and new coordinates*/ dx = angularVel*(x - XC); dy = angularVel*((Height-y)- YC); XC = x; YC = Height-y; /*treat (dx,dy,1) as the new offset VPN, expressed in VC*/ /*transform back to WC, using transpose of RotationMatrix*/ r = TheCamera->R; nx = r.m[0][0]*dx + r.m[0][1]*dy + r.m[0][2]; ny = r.m[1][0]*dx + r.m[1][1]*dy + r.m[1][2]; nz = r.m[2][0]*dx + r.m[2][1]*dy + r.m[2][2]; setVPN_GL(TheCamera,nx,ny,nz); } static void idle(void) /*if nothing else happening*/ { if(Move!=0) { moveVRP(); clickView_GL(TheCamera); /*force a call to display*/ glutPostRedisplay(); } } static void mouseButton(int button, int state, int x, int y) { if(button==GLUT_LEFT_BUTTON){ XC = x; YC = Height - y; if(state==GLUT_DOWN) Rotate = 1; else Rotate = 0; } if(button==GLUT_RIGHT_BUTTON){ if(state==GLUT_DOWN) Move = 1; else Move = 0; } } static void mouseMotion(int x, int y) { if(Rotate){/*for left button*/ rotateVPN(x,y); } if(Move!=0){/*for right button*/ moveVRP(); } clickView_GL(TheCamera); /*force a call to display*/ glutPostRedisplay(); } static void createMainStreet(void) { FaceArray *facearray; GObject *cubeObj,*roofObj, *houseObj[MAXHOUSE],*streetObj[MAXSTREET]; Matrix m; int i; /*create a FaceArray large enough to hold all polygons in the scene*/ facearray = newFaceArray(1024); /*read a ground plane - has max two children*/ groundObj = readObjectFromFile("../Data/plane.dat",facearray,2); /*cube has max two children child*/ cubeObj = readObjectFromFile("../Data/house.dat",facearray,2); /*has no children*/ roofObj = readObjectFromFile("../Data/roof.dat",facearray,0); /*add the cube as a child to the ground*/ addChildToObject(groundObj,cubeObj); /*add the roof as a child to the cube*/ addChildToObject(cubeObj,roofObj); /*set local matrix of roof to be on top of cube*/ setTranslationMatrix(m,0.0,0.0,1.0); setLTMOfObject(roofObj,m); /*now the cube and roof define a house*/ /*make a hierarchy where each house has a child which is the next house further down the street*/ houseObj[0] = cubeObj; setTranslationMatrix(m,0.0,3.0,0.0); for(i=1;i