Material* SceneParser::parsePhongMaterial() { char token[MAX_PARSER_TOKEN_LENGTH]; Vec3f diffuseColor(1, 1, 1); Vec3f specularColor(0, 0, 0); float exponent = 1; getToken(token); assert(!strcmp(token, "{")); while (1) { getToken(token); if (!strcmp(token, "diffuseColor")) { diffuseColor = readVec3f(); } else if (!strcmp(token, "specularColor")) { specularColor = readVec3f(); } else if (!strcmp(token, "exponent")) { exponent = readFloat(); } else { assert(!strcmp(token, "}")); break; } } Material *answer = new PhongMaterial(diffuseColor, specularColor, exponent); return answer; }
void SceneParser::parseLights() { char token[MAX_PARSER_TOKEN_LENGTH]; getToken(token); assert (!strcmp(token, "{")); // read in the number of objects getToken(token); assert (!strcmp(token, "numLights")); num_lights = readInt(); //lights = new (Light*)[num_lights]; lights = new Light*[num_lights]; // read in the objects int count = 0; while (num_lights > count) { getToken(token); if (!strcmp(token, "DirectionalLight")) { getToken(token); assert (!strcmp(token, "{")); getToken(token); assert (!strcmp(token, "direction")); Vec3f direction = readVec3f(); getToken(token); assert (!strcmp(token, "color")); Vec3f color = readVec3f(); getToken(token); assert (!strcmp(token, "}")); // ++++++++++++++++++++++++++++++++++++++++++++++++++++ // CALLING ASSIGNMENT 2 CODE lights[count] = new DirectionalLight(direction,color); // ++++++++++++++++++++++++++++++++++++++++++++++++++++ count++; } else { printf ("Unknown token in parseGroup: '%s'\n", token); exit(0); } } getToken(token); assert (!strcmp(token, "}")); }
/* Parses the "Background" token */ void scene::parseBackground() { char token[MAX_PARSER_TOKEN_LENGTH]; getToken(token); assert (!strcmp(token, "{")); while (1) { getToken(token); if (!strcmp(token, "}")) { break; } else if (!strcmp(token, "color")) { bgColor = readVec3f(); } else if (!strcmp(token, "ambientLight")) { ambLight = readVec3f(); } else { cout<< "Unknown token in parseBackground: "<<token<<"\n"; assert(0); } } }
Transform* SceneParser::parseTransform() { char token[MAX_PARSER_TOKEN_LENGTH]; Matrix matrix; matrix.SetToIdentity(); Object3D *object = NULL; getToken(token); assert(!strcmp(token, "{")); // read in transformations: // apply to the LEFT side of the current matrix (so the first // transform in the list is the last applied to the object) getToken(token); while (1) { if (!strcmp(token, "Scale")) { matrix *= Matrix::MakeScale(readVec3f()); } else if (!strcmp(token, "UniformScale")) { float s = readFloat(); matrix *= Matrix::MakeScale(Vec3f(s, s, s)); } else if (!strcmp(token, "Translate")) { matrix *= Matrix::MakeTranslation(readVec3f()); } else if (!strcmp(token, "XRotate")) { matrix *= Matrix::MakeXRotation(DegreesToRadians(readFloat())); } else if (!strcmp(token, "YRotate")) { matrix *= Matrix::MakeYRotation(DegreesToRadians(readFloat())); } else if (!strcmp(token, "ZRotate")) { matrix *= Matrix::MakeZRotation(DegreesToRadians(readFloat())); } else if (!strcmp(token, "Rotate")) { getToken(token); assert(!strcmp(token, "{")); Vec3f axis = readVec3f(); float degrees = readFloat(); matrix *= Matrix::MakeAxisRotation(axis, DegreesToRadians(degrees)); getToken(token); assert(!strcmp(token, "}")); } else if (!strcmp(token, "Matrix")) { Matrix matrix2; matrix2.SetToIdentity(); getToken(token); assert(!strcmp(token, "{")); for (int j = 0; j < 4; j++) { for (int i = 0; i < 4; i++) { float v = readFloat(); matrix2.Set(i, j, v); } } getToken(token); assert(!strcmp(token, "}")); matrix = matrix2 * matrix; } else { // otherwise this must be an object, // and there are no more transformations object = parseObject(token); break; } getToken(token); } assert(object != NULL); getToken(token); assert(!strcmp(token, "}")); return new Transform(matrix, object); }
Light* SceneParser::parseDirectionalLight() { char token[MAX_PARSER_TOKEN_LENGTH]; getToken(token); assert(!strcmp(token, "{")); getToken(token); assert(!strcmp(token, "direction")); Vec3f direction = readVec3f(); getToken(token); assert(!strcmp(token, "color")); Vec3f color = readVec3f(); getToken(token); assert(!strcmp(token, "}")); return new DirectionalLight(direction, color); }
Md2Parser::Md2FrameList Md2Parser::parseFrames(const char* begin, const size_t frameCount, const size_t frameVertexCount) { Md2FrameList frames(frameCount, Md2Frame(frameVertexCount)); const char* cursor = begin; for (size_t i = 0; i < frameCount; ++i) { frames[i].scale = readVec3f(cursor); frames[i].offset = readVec3f(cursor); readBytes(cursor, frames[i].name, Md2Layout::FrameNameLength); readVector(cursor, frames[i].vertices); } return frames; }
void scene::parseLights() { char token[MAX_PARSER_TOKEN_LENGTH]; char texname[MAX_PARSER_TOKEN_LENGTH]; getToken(token); assert (!strcmp(token, "{")); /* Loop over each Material */ bool working=true; while (working) { getToken(token); if (!strcmp(token, "}")) { working = false; } else if (!strcmp(token, "Light")) { getToken(token); assert (!strcmp(token, "{")); texname[0] = '\0'; Vec3f position(0,0,0); Vec3f color(1,1,1); while (1) { getToken(token); if (!strcmp(token, "position")) position = readVec3f(); else if (!strcmp(token, "color")) color = readVec3f(); else { assert (!strcmp(token, "}")); break; } } /**********************************************/ /* The call to your own constructor goes here */ /**********************************************/ light temp; temp.position=position; temp.color=color; myLights.push_back(temp); } } }
void SceneParser::parseOrthographicCamera() { char token[MAX_PARSER_TOKEN_LENGTH]; // read in the camera parameters getToken(token); assert(!strcmp(token, "{")); getToken(token); assert(!strcmp(token, "center")); Vec3f center = readVec3f(); getToken(token); assert(!strcmp(token, "direction")); Vec3f direction = readVec3f(); getToken(token); assert(!strcmp(token, "up")); Vec3f up = readVec3f(); getToken(token); assert(!strcmp(token, "size")); float size = readFloat(); getToken(token); assert(!strcmp(token, "}")); camera = (Camera*) new OrthographicCamera(center, direction, up, size); }
Triangle* SceneParser::parseTriangle() { char token[MAX_PARSER_TOKEN_LENGTH]; getToken(token); assert(!strcmp(token, "{")); getToken(token); assert(!strcmp(token, "vertex0")); Vec3f v0 = readVec3f(); getToken(token); assert(!strcmp(token, "vertex1")); Vec3f v1 = readVec3f(); getToken(token); assert(!strcmp(token, "vertex2")); Vec3f v2 = readVec3f(); getToken(token); assert(!strcmp(token, "}")); assert(current_material != NULL); return new Triangle(v0, v1, v2, current_material); }
void SceneParser::parsePerspectiveCamera() { char token[MAX_PARSER_TOKEN_LENGTH]; // read in the camera parameters getToken(token); assert(!strcmp(token, "{")); getToken(token); assert(!strcmp(token, "center")); Vec3f center = readVec3f(); getToken(token); assert(!strcmp(token, "direction")); Vec3f direction = readVec3f(); getToken(token); assert(!strcmp(token, "up")); Vec3f up = readVec3f(); getToken(token); assert(!strcmp(token, "angle")); float angle_degrees = readFloat(); float angle_radians = DegreesToRadians(angle_degrees); getToken(token); assert(!strcmp(token, "}")); camera = (Camera *) new PerspectiveCamera(center, direction, up, angle_radians); }
/* Parse out the "Triangle" token */ triangle* scene::parseTriangle() { char token[MAX_PARSER_TOKEN_LENGTH]; getToken(token); assert (!strcmp(token, "{")); /* Parse out vertex information */ getToken(token); assert (!strcmp(token, "vertex0")); Vec3f v0 = readVec3f(); getToken(token); assert (!strcmp(token, "vertex1")); Vec3f v1 = readVec3f(); getToken(token); assert (!strcmp(token, "vertex2")); Vec3f v2 = readVec3f(); getToken(token); assert (!strcmp(token, "tex_xy_0")); float x0 = 0; float y0 = 0; x0 = readFloat(); y0 = readFloat(); getToken(token); assert (!strcmp(token, "tex_xy_1")); float x1 = 0; float y1 = 0; x1 = readFloat(); y1 = readFloat(); getToken(token); assert (!strcmp(token, "tex_xy_2")); float x2 = 0; float y2 = 0; x2 = readFloat(); y2 = readFloat(); getToken(token); assert (!strcmp(token, "materialIndex")); int mat = readInt(); getToken(token); assert (!strcmp(token, "}")); /**********************************************/ /* The call to your own constructor goes here */ /**********************************************/ return new triangle(v0,v1,v2,x0,x1,x2,y0,y1,y2,mat,this); }
void SceneParser::parseBackground() { char token[MAX_PARSER_TOKEN_LENGTH]; // read in the background color getToken(token); assert (!strcmp(token, "{")); while (1) { getToken(token); if (!strcmp(token, "}")) { break; } else if (!strcmp(token, "color")) { background_color = readVec3f(); } else if (!strcmp(token, "ambientLight")) { ambient_light = readVec3f(); } else { printf ("Unknown token in parseBackground: '%s'\n", token); assert(0); } } }
Triangle* SceneParser::parseTriangle() { char token[MAX_PARSER_TOKEN_LENGTH]; // read in the sphere parameters getToken(token); assert (!strcmp(token, "{")); getToken(token); assert (!strcmp(token, "vertex0")); Vec3f v0 = readVec3f(); getToken(token); assert (!strcmp(token, "vertex1")); Vec3f v1 = readVec3f(); getToken(token); assert (!strcmp(token, "vertex2")); Vec3f v2 = readVec3f(); getToken(token); assert (!strcmp(token, "}")); // ++++++++++++++++++++++++++++++++++++++++++++++++++++ // CALLING ASSIGNMENT 2 CODE // create a new triangle object and return it return new Triangle(v0,v1,v2,current_object_color); // ++++++++++++++++++++++++++++++++++++++++++++++++++++ }
void SceneParser::parseMaterial() { char token[MAX_PARSER_TOKEN_LENGTH]; // change the current object color // scoping for the materials is very simplistic, // and essentially ignores any tree hierarchy getToken(token); assert (!strcmp(token, "{")); getToken(token); assert (!strcmp(token, "diffuseColor")); current_object_color = readVec3f(); getToken(token); assert (!strcmp(token, "}")); }
Sphere* SceneParser::parseSphere() { char token[MAX_PARSER_TOKEN_LENGTH]; getToken(token); assert(!strcmp(token, "{")); getToken(token); assert(!strcmp(token, "center")); Vec3f center = readVec3f(); getToken(token); assert(!strcmp(token, "radius")); float radius = readFloat(); getToken(token); assert(!strcmp(token, "}")); assert(current_material != NULL); return new Sphere(center, radius, current_material); }
Plane* SceneParser::parsePlane() { char token[MAX_PARSER_TOKEN_LENGTH]; getToken(token); assert(!strcmp(token, "{")); getToken(token); assert(!strcmp(token, "normal")); Vec3f normal = readVec3f(); getToken(token); assert(!strcmp(token, "offset")); float offset = readFloat(); getToken(token); assert(!strcmp(token, "}")); assert(current_material != NULL); return new Plane(normal, offset, current_material); }
void SceneParser::parsePhongMaterials() { char token[MAX_PARSER_TOKEN_LENGTH]; // change the current object color // scoping for the materials is very simplistic, // and essentially ignores any tree hierarchy getToken(token); assert (!strcmp(token, "{")); getToken(token); assert (!strcmp(token, "numMaterials")); num_materials = readInt(); materials = new PhongMaterial[num_materials]; current_object_color = readVec3f(); getToken(token); assert (!strcmp(token, "}")); }
void SceneParser::parsePerspectiveCamera() { char token[MAX_PARSER_TOKEN_LENGTH]; // read in the camera parameters getToken(token); assert (!strcmp(token, "{")); getToken(token); assert (!strcmp(token, "center")); Vec3f center = readVec3f(); getToken(token); assert (!strcmp(token, "direction")); Vec3f direction = readVec3f(); getToken(token); assert (!strcmp(token, "up")); Vec3f up = readVec3f(); getToken(token); assert (!strcmp(token, "angle")); float angle_degrees = readFloat(); float angle_radians = DegreesToRadians(angle_degrees); getToken(token); assert (!strcmp(token, "}")); // ++++++++++++++++++++++++++++++++++++++++++++++++++++ // CALLING ASSIGNMENT 2 CODE // create a new perspective camera and return it camera = new PerspectiveCamera(center,direction,up,angle_radians); // ++++++++++++++++++++++++++++++++++++++++++++++++++++ }
Sphere* SceneParser::parseSphere() { char token[MAX_PARSER_TOKEN_LENGTH]; // read in the sphere parameters getToken(token); assert (!strcmp(token, "{")); getToken(token); assert (!strcmp(token, "center")); Vec3f center = readVec3f(); getToken(token); assert (!strcmp(token, "radius")); float radius = readFloat(); getToken(token); assert (!strcmp(token, "}")); return new Sphere(center,radius,current_object_color); }
Material* SceneParser::parseMaterial() { char token[MAX_PARSER_TOKEN_LENGTH]; Vec3f diffuseColor(1,1,1); getToken(token); assert (!strcmp(token, "{")); while (1) { getToken(token); if (!strcmp(token, "diffuseColor")) { diffuseColor = readVec3f(); } else { assert (!strcmp(token, "}")); break; } } Material *answer = new Material(diffuseColor); return answer; }
Plane* SceneParser::parsePlane() { char token[MAX_PARSER_TOKEN_LENGTH]; // read in the sphere parameters getToken(token); assert (!strcmp(token, "{")); getToken(token); assert (!strcmp(token, "normal")); Vec3f normal = readVec3f(); getToken(token); assert (!strcmp(token, "offset")); float offset = readFloat(); getToken(token); assert (!strcmp(token, "}")); // ++++++++++++++++++++++++++++++++++++++++++++++++++++ // CALLING ASSIGNMENT 2 CODE // create a new plane object and return it return new Plane(normal,offset,current_object_color); // ++++++++++++++++++++++++++++++++++++++++++++++++++++ }
/* Parse the "Sphere" token */ sphere* scene::parseSphere() { char token[MAX_PARSER_TOKEN_LENGTH]; getToken(token); assert (!strcmp(token, "{")); getToken(token); assert (!strcmp(token, "materialIndex")); int sphere_material_index = readInt(); getToken(token); assert (!strcmp(token, "center")); Vec3f center = readVec3f(); getToken(token); assert (!strcmp(token, "radius")); float radius = readFloat(); getToken(token); assert (!strcmp(token, "}")); /**********************************************/ /* The call to your own constructor goes here */ /**********************************************/ return new sphere(center,radius,sphere_material_index,this); }
/* Parse the "Materials" token */ void scene::parseMaterials() { char token[MAX_PARSER_TOKEN_LENGTH]; char texname[MAX_PARSER_TOKEN_LENGTH]; getToken(token); assert (!strcmp(token, "{")); /* Loop over each Material */ bool working=true; while (working) { getToken(token); if (!strcmp(token, "}")) { working = false; } else if (!strcmp(token, "Material")) { getToken(token); assert (!strcmp(token, "{")); texname[0] = '\0'; Vec3f diffuseColor(1,1,1); Vec3f specularColor(0,0,0); float shininess = 1; Vec3f transparentColor(0,0,0); Vec3f reflectiveColor(0,0,0); float indexOfRefraction = 1; while (1) { getToken(token); if (!strcmp(token, "textureFilename")) { getToken(token); strcpy(texname, token); } else if (!strcmp(token, "diffuseColor")) diffuseColor = readVec3f(); else if (!strcmp(token, "specularColor")) specularColor = readVec3f(); else if (!strcmp(token, "shininess")) shininess = readFloat(); else if (!strcmp(token, "transparentColor")) transparentColor = readVec3f(); else if (!strcmp(token, "reflectiveColor")) reflectiveColor = readVec3f(); else if (!strcmp(token, "indexOfRefraction")) indexOfRefraction = readFloat(); else { assert (!strcmp(token, "}")); break; } } material temp; temp.diffuseCol=diffuseColor; temp.specularCol=specularColor; temp.shininess=shininess; temp.transparentCol=transparentColor; temp.reflectiveCol=reflectiveColor; temp.refractionIndex=indexOfRefraction; if(strcmp(texname, "NULL")) temp.texture=jpeg_read(texname, NULL); else temp.texture=NULL; myMaterials.push_back(temp); } } }