void parse_scene_file(char *filename, Camera *cam) { char c, obj_filename[64]; int w, h; FILE *fp; float sx, sy, sz, rx, ry, rz, dx, dy, dz; // scale factors, rotation angles (degrees), translation factors Transform Tmat, Rmat, Smat, scratchmat, Mmat; Vect P; Light *L; Surface *S; Sphere *Sph; // read file fp = fopen(filename, "r"); // camera position fscanf(fp, "camera %lf %lf %lf %lf %lf %lf %lf %lf %lf\n", &cam->eye[X], &cam->eye[Y], &cam->eye[Z], &cam->center[X], &cam->center[Y], &cam->center[Z], &cam->up[X], &cam->up[Y], &cam->up[Z]); setup_lookat_transform(cam->W2C, cam->eye, cam->center, cam->up); // clip planes fscanf(fp, "clip %lf %lf %lf %lf %lf %lf\n", &cam->clip[LEFT], &cam->clip[RIGHT], &cam->clip[BOTTOM], &cam->clip[TOP], &cam->clip[NEAR], &cam->clip[FAR]); // image dimensions fscanf(fp, "image %i %i\n", &w, &h); cam->im = make_image(w, h); // objects and lights model_list.clear(); model_surf_list.clear(); sphere_list.clear(); light_list.clear(); while (1) { c = fgetc(fp); // end of file if (c == EOF) return; // it's a comment if (c == '#') { do { c = fgetc(fp); printf("eating %c\n", c); } while (c != '\n' && c != EOF); if (c == EOF) return; printf("done\n"); } // it's an object else if (c == 'o') { S = (Surface *) malloc(sizeof(Surface)); fscanf(fp, "bj %s %f %f %f %f %f %f %f %f %f %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf\n", obj_filename, &dx, &dy, &dz, &sx, &sy, &sz, &rx, &ry, &rz, &(S->amb[R]), &(S->amb[G]), &(S->amb[B]), &(S->diff[R]), &(S->diff[G]), &(S->diff[B]), &S->spec[R], &S->spec[G], &S->spec[B], &S->spec_exp, &S->ior, &S->reflectivity, &S->transparency); GLMmodel *model = glmReadOBJ(obj_filename); if (!model) { printf("no such glm model %s\n", obj_filename); exit(1); } // scale and center model on origin glmUnitize(model); // build model transformations (including lookat world -> camera transform) TransformIdentity(Tmat); MATRC(Tmat, 0, 3) = dx; MATRC(Tmat, 1, 3) = dy; MATRC(Tmat, 2, 3) = dz; TransformIdentity(Smat); MATRC(Smat, 0, 0) = sx; MATRC(Smat, 1, 1) = sy; MATRC(Smat, 2, 2) = sz; model->position[0] = dx; model->position[1] = dy; model->position[2] = dz; // ROTATION WOULD GO HERE TransformProd(Tmat, Smat, scratchmat); TransformProd(cam->W2C, scratchmat, Mmat); //TransformPrint(Mmat); // apply transform to model glm_transform(Mmat, model); // calculate normals glmFacetNormals(model); glmVertexNormals(model, 90.0); model->index = model_list.size(); model_list.push_back(model); model_surf_list.push_back(S); } // it's a sphere else if (c == 's') { S = (Surface *) malloc(sizeof(Surface)); Sph = (Sphere *) malloc(sizeof(Sphere)); fscanf(fp, "phere %f %f %f %f %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf\n", &dx, &dy, &dz, // position &sx, // size &(S->amb[R]), &(S->amb[G]), &(S->amb[B]), &(S->diff[R]), &(S->diff[G]), &(S->diff[B]), &S->spec[R], &S->spec[G], &S->spec[B], &S->spec_exp, &S->ior, &S->reflectivity, &S->transparency); Sph->P[X] = dx; Sph->P[Y] = dy; Sph->P[Z] = dz; Sph->radius = sx; Sph->surf = S; sphere_list.push_back(Sph); } // it's a light else if (c == 'l') { L = (Light *) malloc(sizeof(Light)); fscanf(fp, "ight %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf\n", &P[X], &P[Y], &P[Z], &L->amb[R], &L->amb[G], &L->amb[B], &L->diff[R], &L->diff[G], &L->diff[B], &L->spec[R], &L->spec[G], &L->spec[B]); // move to camera coordinates P[W] = 0; TransformVect(cam->W2C, P, L->P); // add to list light_list.push_back(L); } // unknown else if (c == '\n' && c != ' ' && c != '\t') { printf("bad scene syntax %c\n", c); exit(1); } } }
//------------------------------------------------- // Transforms // // //------------------------------------------------- void TransformInitialise(STransform* const pTransform, unsigned int depth) { MatrixStackInitialise(&pTransform->m_transformStack, depth); TransformIdentity(pTransform); }