#include #include #include #include "../include/camera.h" #include "../include/global.h" using namespace VMI; /* Make it not a power of two to avoid cache thrashing on the chip */ #define CACHE_SIZE 240 #define Pi (GLdouble)3.141592653589793238462643383279502884197 //#define DRAW_DEBUG // just for debugging not necessary Camera *VMI::loadCameras(GLdouble radius, char* filename, GLuint *numCameras) { FILE *fp; Camera *cameras; GLuint num, i; GLfloat eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ, weight, max = 0.0f, min = 0.0f, normal; float u[3], v[3]/*, n[3]*/; if((fp= fopen(filename, "rt"))== NULL) { printf("Can't open file %s\n", filename); exit(1); } printf("Reading file %s\n", filename); fscanf (fp, "%d\n", &num); printf("%d\n", num); cameras = (Camera *)malloc(sizeof(Camera) * num); if (cameras == NULL) { fprintf(stderr, "Error allocating memory\n"); exit(1); } num = 0; while (!feof(fp)) { fscanf(fp, "%f %f %f %f %f %f %f %f %f %f\n", &eyeX, &eyeY, &eyeZ, ¢erX, ¢erY, ¢erZ, &upX, &upY, &upZ, &weight); /*printf("%d -> %f %f %f %f %f %f %f %f %f %f\n",num, eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ, weight);*/ if (eyeX > max) max = eyeX; if (eyeY > max) max = eyeY; if (eyeZ > max) max = eyeZ; if (eyeX < min) min = eyeX; if (eyeY < min) min = eyeY; if (eyeZ < min) min = eyeZ; cameras[num].eyeX = radius * eyeX; cameras[num].eyeY = radius * eyeY; cameras[num].eyeZ = radius * eyeZ; cameras[num].centerX = centerX; cameras[num].centerY = centerY; cameras[num].centerZ = centerZ; cameras[num].upX = upX; cameras[num].upY = upY; cameras[num].upZ = upZ; cameras[num].weight = weight; num++; } //printf("%f %f\n", min, max); // This normalization currently only works if center is vector (0,0,0) normal = ABS(max); if (ABS(min) > normal) normal = ABS(min); for (i=0; i= CACHE_SIZE) slices = CACHE_SIZE-1; if (stacks >= CACHE_SIZE) stacks = CACHE_SIZE-1; if (slices < 2 || stacks < 1 || radius < 0.0) { printf("Error\n"); return; } for (i = 0; i < slices; i++) { angle = 2 * Pi * i / slices; sinCache1a[i] = sin(angle); cosCache1a[i] = cos(angle); } for (j = 0; j <= stacks; j++) { angle = Pi * j / stacks; sinCache1b[j] = radius * sin(angle); cosCache1b[j] = radius * cos(angle); } // Make sure it comes to a point sinCache1b[0] = 0; sinCache1b[stacks] = 0; sinCache1a[slices] = sinCache1a[0]; cosCache1a[slices] = cosCache1a[0]; #ifdef DRAW_DEBUG glBegin(GL_POINTS); #endif for (j = 0; j <= stacks; j++) { sintemp1 = sinCache1b[j]; costemp1 = cosCache1b[j]; for (i = 0; i < slices; i++) { vertices[n][0]= sintemp1 * sinCache1a[i]; vertices[n][1]= sintemp1 * cosCache1a[i]; vertices[n][2]= costemp1; #ifdef DRAW_DEBUG glVertex3fv(vertices[n]); #endif n++; } } #ifdef DRAW_DEBUG glEnd(); #endif copyToCameras(cameras, n, vertices); } /////////////////////////////////////////////////////////////////////////////// void VMI::drawDodecahedron(Camera *cameras,GLdouble r) // any radius in which the polyhedron is inscribed { GLdouble vertices[20][3]; // 20 vertices with x, y, z coordinate GLdouble phiaa = 52.62263590; // the two phi angles needed for generation GLdouble phibb = 10.81231754; GLdouble phia = Pi*phiaa/180.0; // 4 sets of five points each GLdouble phib = Pi*phibb/180.0; GLdouble phic = Pi*(-phibb)/180.0; GLdouble phid = Pi*(-phiaa)/180.0; GLdouble the72 = Pi*72.0/180; GLdouble theb = the72/2.0; // pairs of layers offset 36 degrees GLdouble the = 0.0; int i; #ifdef DRAW_DEBUG int tindex[12][5] = { {0,1,2,3,4}, {0,1,6,10,5}, {1,2,7,11,6}, {2,3,8,12,7}, {3,4,9,13,8}, {4,0,5,14,9}, {15,16,11,6,10}, {16,17,12,7,11}, {17,18,13,8,12}, {18,19,14,9,13}, {19,15,10,5,14} }; #endif if (r < 0.0f) return; for(i=0; i<5; i++) { vertices[i][0]=r*cos(the)*cos(phia); vertices[i][1]=r*sin(the)*cos(phia); vertices[i][2]=r*sin(phia); the = the+the72; } the=0.0; for(i=5; i<10; i++) { vertices[i][0]=r*cos(the)*cos(phib); vertices[i][1]=r*sin(the)*cos(phib); vertices[i][2]=r*sin(phib); the = the+the72; } the = theb; for(i=10; i<15; i++) { vertices[i][0]=r*cos(the)*cos(phic); vertices[i][1]=r*sin(the)*cos(phic); vertices[i][2]=r*sin(phic); the = the+the72; } the=theb; for(i=15; i<20; i++) { vertices[i][0]=r*cos(the)*cos(phid); vertices[i][1]=r*sin(the)*cos(phid); vertices[i][2]=r*sin(phid); the = the+the72; } // map vertices to 12 faces #ifdef DRAW_DEBUG for (i=0; i<12; i++) { glBegin(GL_POINTS); glVertex3fv(&vertices[tindex[i][0]][0]); glVertex3fv(&vertices[tindex[i][1]][0]); glVertex3fv(&vertices[tindex[i][2]][0]); glVertex3fv(&vertices[tindex[i][3]][0]); glVertex3fv(&vertices[tindex[i][4]][0]); glEnd(); } #endif copyToCameras(cameras, 20, vertices); } /////////////////////////////////////////////////////////////////////////////// void VMI::drawOctahedron(Camera *cameras, GLdouble r) // any radius in which the polyhedron is inscribed { GLdouble vertices[6][3]; // 6 vertices with x, y, z coordinate GLdouble phiaa = 0.0; // the phi needed for generation GLdouble phia = Pi*phiaa/180.0; // 1 set of four points GLdouble the90 = Pi*90.0/180; GLdouble the = 0.0; int i; #ifdef DRAW_DEBUG int tindex[8][3] = { {0,1,2}, {0,2,3}, {0,3,4}, {0,4,1}, {5,1,2}, {5,2,3}, {5,3,4}, {5,4,1} }; #endif if (r < 0.0f) return; vertices[0][0]=0.0; vertices[0][1]=0.0; vertices[0][2]=r; vertices[5][0]=0.0; vertices[5][1]=0.0; vertices[5][2]=-r; for(i=1; i<5; i++) { vertices[i][0]=r*cos(the)*cos(phia); vertices[i][1]=r*sin(the)*cos(phia); vertices[i][2]=r*sin(phia); the = the+the90; } // map vertices to 8 faces #ifdef DRAW_DEBUG for (i=0; i<8; i++) { glBegin(GL_POINTS); glVertex3fv(&vertices[tindex[i][0]][0]); glVertex3fv(&vertices[tindex[i][1]][0]); glVertex3fv(&vertices[tindex[i][2]][0]); glEnd(); } #endif copyToCameras(cameras, 6, vertices); } /////////////////////////////////////////////////////////////////////////////// void VMI::drawIcosahedron(Camera *cameras, GLdouble r) // any radius in which the polyhedron is inscribed { GLdouble vertices[12][3]; // 12 vertices with x, y, z coordinates GLdouble phiaa = 26.56505; // phi needed for generation GLdouble phia = Pi*phiaa/180.0; // 2 sets of four points GLdouble theb = Pi*36.0/180.0; // offset second set 36 degrees GLdouble the72 = Pi*72.0/180; // step 72 degrees GLdouble the = 0.0; int i; #ifdef DRAW_DEBUG int tindex[20][3] = { {0,1,2}, {0,2,3}, {0,3,4}, {0,4,5}, {0,5,1}, {11,6,7}, {11,7,8}, {11,8,9}, {11,9,10}, {11,10,6}, {1,2,6}, {2,3,7}, {3,4,8}, {4,5,9}, {5,1,10}, {6,7,2}, {7,8,3}, {8,9,4}, {9,10,5}, {10,6,1} }; #endif if (r < 0.0f) return; vertices[0][0]=0.0; vertices[0][1]=0.0; vertices[0][2]=r; vertices[11][0]=0.0; vertices[11][1]=0.0; vertices[11][2]=-r; for(i=1; i<6; i++) { vertices[i][0]=r*cos(the)*cos(phia); vertices[i][1]=r*sin(the)*cos(phia); vertices[i][2]=r*sin(phia); the = the+the72; } the=theb; for(i=6; i<11; i++) { vertices[i][0]=r*cos(the)*cos(-phia); vertices[i][1]=r*sin(the)*cos(-phia); vertices[i][2]=r*sin(-phia); the = the+the72; } // map vertices to 20 faces #ifdef DRAW_DEBUG for (i=0; i<20; i++) { glBegin(GL_POINTS ); glVertex3fv(&vertices[tindex[i][0]][0]); glVertex3fv(&vertices[tindex[i][1]][0]); glVertex3fv(&vertices[tindex[i][2]][0]); glEnd(); } #endif copyToCameras(cameras, 12, vertices); } void VMI::drawCube(Camera *cameras, GLdouble r) // any radius in which the polyhedron is inscribed { GLdouble vertices[8][3] = { {-1, -1, 1}, // vertex v0 {1, -1, 1}, // vertex v1 {1, -1, -1}, // vertex v2 {-1, -1, -1}, // vertex v3 {-1, 1, 1}, // vertex v4 {1, 1, 1}, // vertex v5 {1, 1, -1}, // vertex v6 {-1, 1, -1}, // vertex v7 }; // 8 vertices with x, y, z coordinates int i; #ifdef DRAW_DEBUG int tindex[12][3] = { {0, 1, 4}, //polygon v0,v1,v4 {1, 5, 4}, //polygon v1,v5,v4 {1, 2, 5}, //polygon v1,v2,v5 {2, 6, 5}, //polygon v2,v6,v5 {2, 3, 6}, //polygon v2,v3,v6 {3, 7, 6}, //polygon v3,v7,v6 {3, 0, 7}, //polygon v3,v0,v7 {0, 4, 7}, //polygon v0,v4,v7 {4, 5, 7}, //polygon v4,v5,v7 {5, 6, 7}, //polygon v5,v6,v7 {3, 2, 0}, //polygon v3,v2,v0 {2, 1, 0}, //polygon v2,v1,v0 }; #endif for(i=0; i<8; i++) { vertices[i][0]*=r; vertices[i][1]*=r; vertices[i][2]*=r; } // map vertices to 12 faces #ifdef DRAW_DEBUG for (i=0; i<12; i++) { glBegin(GL_POINTS ); glVertex3fv(&vertices[tindex[i][0]][0]); glVertex3fv(&vertices[tindex[i][1]][0]); glVertex3fv(&vertices[tindex[i][2]][0]); glEnd(); } #endif copyToCameras(cameras, 8, vertices); }