R3Vector operator-(const R3Point& point1, const R3Point& point2) { return R3Vector(point1.v[0] - point2.v[0], point1.v[1] - point2.v[1], point1.v[2] - point2.v[2]); }
R3Vector operator*(const R3Matrix& a, const R3Vector& v) { // Multiply matrix by vector double x = a.m[0][0] * v.X() + a.m[0][1] * v.Y() + a.m[0][2] * v.Z(); double y = a.m[1][0] * v.X() + a.m[1][1] * v.Y() + a.m[1][2] * v.Z(); double z = a.m[2][0] * v.X() + a.m[2][1] * v.Y() + a.m[2][2] * v.Z(); return R3Vector(x, y, z); }
R3Vector operator-(const R3Vector& vector) { // Return opposite of vector return R3Vector(-vector.v[0], -vector.v[1], -vector.v[2]); }
R3Vector operator+(const R3Vector& vector1, const R3Vector& vector2) { // Return sum of two vectors return R3Vector(vector1.v[0] + vector2.v[0], vector1.v[1] + vector2.v[1], vector1.v[2] + vector2.v[2]); }
R3Vector operator-(const R3Vector& vector1, const R3Vector& vector2) { // Return difference of two vectors return R3Vector(vector1.v[0] - vector2.v[0], vector1.v[1] - vector2.v[1], vector1.v[2] - vector2.v[2]); }
R3Vector operator*(const R3Vector& vector, const double a) { // Return product of vector and scalar return R3Vector(vector.v[0] * a, vector.v[1] * a, vector.v[2] * a); }
R3Vector operator/(const R3Vector& vector, const double a) { // Return vector divided by a scalar assert(a != 0); return R3Vector(vector.v[0]/a, vector.v[1]/a, vector.v[2]/a); }
R3Vector operator*(const R3Vector& vector1, const R3Vector& vector2) { // Return product of two vectors // Entry by entry multiply (not dot or cross product) return R3Vector(vector1.v[0] * vector2.v[0], vector1.v[1] * vector2.v[1], vector1.v[2] * vector2.v[2]); }
R3Point SpawnLocation() { R3Point playerPos = globals.player->GetPosition(); R3Vector playerDir = globals.player->GetDirection(); R3Vector playerLeft = R3Vector(R3posy_vector); playerLeft.Cross(playerDir); return playerPos + Util::SymmetricRandom() * 15.0 * playerLeft + Util::SymmetricRandom() * 15.0 * R3posy_vector + (Util::UnitRandom() + 0.3) * 100.0 * playerDir; }
R3Vector operator*(const R4Matrix& a, const R3Vector& v) { // Multiply matrix by vector RNCoord x = a.m[0][0] * v.X() + a.m[0][1] * v.Y() + a.m[0][2] * v.Z(); RNCoord y = a.m[1][0] * v.X() + a.m[1][1] * v.Y() + a.m[1][2] * v.Z(); RNCoord z = a.m[2][0] * v.X() + a.m[2][1] * v.Y() + a.m[2][2] * v.Z(); return R3Vector(x, y, z); }
R3Vector operator/(const R3Vector& vector1, const R3Vector& vector2) { // Return coordinates of vector1 divided by corresponding coordinates of vector2 assert(vector2.v[0] != 0); assert(vector2.v[1] != 0); assert(vector2.v[2] != 0); return R3Vector(vector1.v[0]/vector2.v[0], vector1.v[1]/vector2.v[1], vector1.v[2]/vector2.v[2]); }
R3Mesh* Terrain::Patch(R3Point center, R2Point size, R2Point dps) { // dps: dots per side R3Mesh *patch = new R3Mesh(); for (unsigned int r = 0; r < dps.Y(); r++) { for (unsigned int c = 0; c < dps.X(); c++) { double dx = (double)c / dps.X() * size.X() - size.X() / 2; double dy = (r + c) % 2 * Util::UnitRandom(); // default if (params.heightMap) { int x = ((double)c / dps.X()) * params.heightMap->Width(); int y = ((double)r / dps.Y()) * params.heightMap->Height(); dy = 25 * params.heightMap->Pixel(x,y).Luminance(); } double dz = (double)r / dps.Y() * size.Y() - size.Y() / 2; R3Point position(center.X() + dx, center.Y() + dy, center.Z() + dz); double tx = position.X() / 80; double ty = position.Z() / 80; //R2Point texcoord = R2Point(tx - ((int) tx), ty - ((int) ty)); //if (texcoord.X() < 0) //texcoord.SetX(1 + texcoord.X()); //if (texcoord.Y() < 0) //texcoord.SetY(1 + texcoord.Y()); patch->CreateVertex(position, R3Vector(0,1,0), R2Point(tx, ty)); // id's go up along x axis, then along y } } for (unsigned int r = 0; r < dps.Y() - 1; r++) { for (unsigned int c = 0; c < dps.X() - 1; c++) { unsigned int cols = (unsigned int)dps.X(); unsigned int k = c + r * cols; vector<R3MeshVertex*> vertices; vertices.push_back(patch->Vertex(k+cols)); vertices.push_back(patch->Vertex(k+1)); vertices.push_back(patch->Vertex(k)); patch->CreateFace(vertices); vector<R3MeshVertex*> vertices2; vertices2.push_back(patch->Vertex(k+cols)); vertices2.push_back(patch->Vertex(k+1+cols)); vertices2.push_back(patch->Vertex(k+1)); patch->CreateFace(vertices2); } } patch->Update(); return patch; }
void R3Ellipse:: Draw(const R3DrawFlags draw_flags) const { // Push matrix R4Matrix matrix = R4identity_matrix; matrix.Transform(cs.Matrix()); matrix.Scale(R3Vector(radii.X(), radii.Y(), 1.0)); matrix.Push(); // Draw unit sphere R3unit_circle.Draw(draw_flags); // Pop matrix matrix.Pop(); }
/* Source file for the R3 circle class */ /* Include files */ #include "R3Shapes/R3Shapes.h" /* Public variables */ const R3Circle R3null_circle(R3Point(0.0, 0.0, 0.0), -1.0, R3Vector(0.0, 0.0, 0.0)); const R3Circle R3zero_circle(R3Point(0.0, 0.0, 0.0), 0.0, R3Vector(0.0, 0.0, 1.0)); const R3Circle R3unit_circle(R3Point(0.0, 0.0, 0.0), 1.0, R3Vector(0.0, 0.0, 1.0)); const R3Circle R3infinite_circle(R3Point(0.0, 0.0, 0.0), RN_INFINITY, R3Vector(0.0, 0.0, 1.0)); /* Unit circle coordinates */ const int R3circle_npoints = 32; RNAngle R3circle_angles[R3circle_npoints]; R3Point R3circle_points[R3circle_npoints]; R2Point R3circle_texcoords[R3circle_npoints]; /* Class type definitions */ RN_CLASS_TYPE_DEFINITIONS(R3Circle);
/* Source file for the R3 light class */ /* Include files */ #include "R3Graphics.h" /* Public variables */ R3DirectionalLight R3null_directional_light; R3DirectionalLight R3default_directional_light(R3Vector(0.0, 0.0, -1.0), RNRgb(1.0, 1.0, 1.0), 1.0, TRUE); /* Class type definitions */ RN_CLASS_TYPE_DEFINITIONS(R3DirectionalLight); /* Public functions */ int R3InitDirectionalLight() { /* Return success */ return TRUE; }
int R3Scene:: Read(const char *filename, R3Node *node) { // Open file FILE *fp; if (!(fp = fopen(filename, "r"))) { fprintf(stderr, "Unable to open file %s", filename); return 0; } // Create array of materials vector<R3Material *> materials; // Create default material R3Material *default_material = new R3Material(); default_material->ka = R3Rgb(0.2, 0.2, 0.2, 1); default_material->kd = R3Rgb(0.5, 0.5, 0.5, 1); default_material->ks = R3Rgb(0.5, 0.5, 0.5, 1); default_material->kt = R3Rgb(0.0, 0.0, 0.0, 1); default_material->emission = R3Rgb(0, 0, 0, 1); default_material->shininess = 10; default_material->indexofrefraction = 1; default_material->texture = NULL; default_material->id = 0; // Create stack of group information const int max_depth = 1024; R3Node *group_nodes[max_depth] = { NULL }; R3Material *group_materials[max_depth] = { NULL }; group_nodes[0] = (node) ? node : root; group_materials[0] = default_material; int depth = 0; // Read body char cmd[128]; int command_number = 1; while (fscanf(fp, "%s", cmd) == 1) { if (cmd[0] == '#') { // Comment -- read everything until end of line do { cmd[0] = fgetc(fp); } while ((cmd[0] >= 0) && (cmd[0] != '\n')); } else if (!strcmp(cmd, "tri")) { // Read data int m; R3Point p1, p2, p3; if (fscanf(fp, "%d%lf%lf%lf%lf%lf%lf%lf%lf%lf", &m, &p1[0], &p1[1], &p1[2], &p2[0], &p2[1], &p2[2], &p3[0], &p3[1], &p3[2]) != 10) { fprintf(stderr, "Unable to read triangle at command %d in file %s\n", command_number, filename); return 0; } // Get material R3Material *material = group_materials[depth]; if (m >= 0) { if (m < (int) materials.size()) { material = materials[m]; } else { fprintf(stderr, "Invalid material id at tri command %d in file %s\n", command_number, filename); return 0; } } // Create mesh R3Mesh *mesh = new R3Mesh(); vector<R3MeshVertex *> vertices; vertices.push_back(mesh->CreateVertex(p1, R3zero_vector, R2zero_point)); vertices.push_back(mesh->CreateVertex(p2, R3zero_vector, R2zero_point)); vertices.push_back(mesh->CreateVertex(p3, R3zero_vector, R2zero_point)); mesh->CreateFace(vertices); // Create shape R3Shape *shape = new R3Shape(); shape->type = R3_MESH_SHAPE; shape->box = NULL; shape->sphere = NULL; shape->cylinder = NULL; shape->cone = NULL; shape->mesh = mesh; shape->segment = NULL; // Create shape node R3Node *node = new R3Node(); node->transformation = R3identity_matrix; node->material = material; node->shape = shape; node->bbox = R3null_box; node->bbox.Union(p1); node->bbox.Union(p2); node->bbox.Union(p3); node->enemy = NULL; // Insert node group_nodes[depth]->bbox.Union(node->bbox); group_nodes[depth]->children.push_back(node); node->parent = group_nodes[depth]; } else if (!strcmp(cmd, "box")) { // Read data int m; R3Point p1, p2; if (fscanf(fp, "%d%lf%lf%lf%lf%lf%lf", &m, &p1[0], &p1[1], &p1[2], &p2[0], &p2[1], &p2[2]) != 7) { fprintf(stderr, "Unable to read box at command %d in file %s\n", command_number, filename); return 0; } // Get material R3Material *material = group_materials[depth]; if (m >= 0) { if (m < (int) materials.size()) { material = materials[m]; } else { fprintf(stderr, "Invalid material id at box command %d in file %s\n", command_number, filename); return 0; } } // Create box R3Box *box = new R3Box(p1, p2); // Create shape R3Shape *shape = new R3Shape(); shape->type = R3_BOX_SHAPE; shape->box = box; shape->sphere = NULL; shape->cylinder = NULL; shape->cone = NULL; shape->mesh = NULL; shape->segment = NULL; // Create shape node R3Node *node = new R3Node(); node->transformation = R3identity_matrix; node->material = material; node->shape = shape; node->bbox = *box; node->enemy = NULL; // Insert node group_nodes[depth]->bbox.Union(node->bbox); group_nodes[depth]->children.push_back(node); node->parent = group_nodes[depth]; } else if (!strcmp(cmd, "sphere")) { // Read data int m; R3Point c; double r; if (fscanf(fp, "%d%lf%lf%lf%lf", &m, &c[0], &c[1], &c[2], &r) != 5) { fprintf(stderr, "Unable to read sphere at command %d in file %s\n", command_number, filename); return 0; } // Get material R3Material *material = group_materials[depth]; if (m >= 0) { if (m < (int) materials.size()) { material = materials[m]; } else { fprintf(stderr, "Invalid material id at sphere command %d in file %s\n", command_number, filename); return 0; } } // Create sphere R3Sphere *sphere = new R3Sphere(c, r); // Create shape R3Shape *shape = new R3Shape(); shape->type = R3_SPHERE_SHAPE; shape->box = NULL; shape->sphere = sphere; shape->cylinder = NULL; shape->cone = NULL; shape->mesh = NULL; shape->segment = NULL; // Create shape node R3Node *node = new R3Node(); node->transformation = R3identity_matrix; node->material = material; node->shape = shape; node->bbox = sphere->BBox(); node->enemy = NULL; // Insert node group_nodes[depth]->bbox.Union(node->bbox); group_nodes[depth]->children.push_back(node); node->parent = group_nodes[depth]; } else if (!strcmp(cmd, "cylinder")) { // Read data int m; R3Point c; double r, h; if (fscanf(fp, "%d%lf%lf%lf%lf%lf", &m, &c[0], &c[1], &c[2], &r, &h) != 6) { fprintf(stderr, "Unable to read cylinder at command %d in file %s\n", command_number, filename); return 0; } // Get material R3Material *material = group_materials[depth]; if (m >= 0) { if (m < (int) materials.size()) { material = materials[m]; } else { fprintf(stderr, "Invalid material id at cyl command %d in file %s\n", command_number, filename); return 0; } } // Create cylinder R3Cylinder *cylinder = new R3Cylinder(c, r, h); // Create shape R3Shape *shape = new R3Shape(); shape->type = R3_CYLINDER_SHAPE; shape->box = NULL; shape->sphere = NULL; shape->cylinder = cylinder; shape->cone = NULL; shape->mesh = NULL; shape->segment = NULL; // Create shape node R3Node *node = new R3Node(); node->transformation = R3identity_matrix; node->material = material; node->shape = shape; node->bbox = cylinder->BBox(); node->enemy = NULL; // Insert node group_nodes[depth]->bbox.Union(node->bbox); group_nodes[depth]->children.push_back(node); node->parent = group_nodes[depth]; } else if (!strcmp(cmd, "mesh")) { // Read data int m; char meshname[256]; if (fscanf(fp, "%d%s", &m, meshname) != 2) { fprintf(stderr, "Unable to parse mesh command %d in file %s\n", command_number, filename); return 0; } // Get material R3Material *material = group_materials[depth]; if (m >= 0) { if (m < (int) materials.size()) { material = materials[m]; } else { fprintf(stderr, "Invalid material id at cone command %d in file %s\n", command_number, filename); return 0; } } // Get mesh filename char buffer[2048]; strcpy(buffer, filename); char *bufferp = strrchr(buffer, '/'); if (bufferp) *(bufferp+1) = '\0'; else buffer[0] = '\0'; strcat(buffer, meshname); // Create mesh R3Mesh *mesh = new R3Mesh(); if (!mesh) { fprintf(stderr, "Unable to allocate mesh\n"); return 0; } // Read mesh file if (!mesh->Read(buffer)) { fprintf(stderr, "Unable to read mesh: %s\n", buffer); return 0; } // Create shape R3Shape *shape = new R3Shape(); shape->type = R3_MESH_SHAPE; shape->box = NULL; shape->sphere = NULL; shape->cylinder = NULL; shape->cone = NULL; shape->mesh = mesh; shape->segment = NULL; // Create shape node R3Node *node = new R3Node(); node->transformation = R3identity_matrix; node->material = material; node->shape = shape; node->bbox = mesh->bbox; node->enemy = NULL; // Insert node group_nodes[depth]->bbox.Union(node->bbox); group_nodes[depth]->children.push_back(node); node->parent = group_nodes[depth]; } //in order to set the arwing node as a global in GlutTest.cpp else if (!strcmp(cmd, "arwing")) { // Read data int m; char meshname[256]; if (fscanf(fp, "%d%s", &m, meshname) != 2) { fprintf(stderr, "Unable to parse mesh command %d in file %s\n", command_number, filename); return 0; } // Get material R3Material *material = group_materials[depth]; if (m >= 0) { if (m < (int) materials.size()) { material = materials[m]; } else { fprintf(stderr, "Invalid material id at cone command %d in file %s\n", command_number, filename); return 0; } } // Get mesh filename char buffer[2048]; strcpy(buffer, filename); char *bufferp = strrchr(buffer, '/'); if (bufferp) *(bufferp+1) = '\0'; else buffer[0] = '\0'; strcat(buffer, meshname); // Create mesh R3Mesh *mesh = new R3Mesh(); if (!mesh) { fprintf(stderr, "Unable to allocate mesh\n"); return 0; } // Read mesh file if (!mesh->Read(buffer)) { fprintf(stderr, "Unable to read mesh: %s\n", buffer); return 0; } // Create shape R3Shape *shape = new R3Shape(); shape->type = R3_MESH_SHAPE; shape->box = NULL; shape->sphere = NULL; shape->cylinder = NULL; shape->cone = NULL; shape->mesh = mesh; shape->segment = NULL; // Create shape node R3Node *node = new R3Node(); node->transformation = R3identity_matrix; node->material = material; node->shape = shape; node->bbox = mesh->bbox; node->enemy = NULL; // Insert node group_nodes[depth]->bbox.Union(node->bbox); group_nodes[depth]->children.push_back(node); node->parent = group_nodes[depth]; arwingNode = node; } /* unneeded //turret - basically a box that shoots else if (!strcmp(cmd, "turret")) { // Read data int m; R3Point p1, p2; if (fscanf(fp, "%d%lf%lf%lf%lf%lf%lf", &m, &p1[0], &p1[1], &p1[2], &p2[0], &p2[1], &p2[2]) != 7) { fprintf(stderr, "Unable to read box at command %d in file %s\n", command_number, filename); return 0; } // Get material R3Material *material = group_materials[depth]; if (m >= 0) { if (m < (int) materials.size()) { material = materials[m]; } else { fprintf(stderr, "Invalid material id at box command %d in file %s\n", command_number, filename); return 0; } } // Create box R3Box *box = new R3Box(p1, p2); // Create shape R3Shape *shape = new R3Shape(); shape->type = R3_BOX_SHAPE; shape->box = box; shape->sphere = NULL; shape->cylinder = NULL; shape->cone = NULL; shape->mesh = NULL; shape->segment = NULL; // Create shape node R3Node *node = new R3Node(); node->transformation = R3identity_matrix; node->cumulativeTransformation = R3identity_matrix; node->material = material; node->shape = shape; node->bbox = *box; node->enemy = new SFEnemy(); //list properties of the turret node->enemy->position = box->Centroid(); node->enemy->projectileSource = box->Centroid(); node->enemy->node = node; // node->enemy->projectileSource.InverseTransform(node->transformation); // node->enemy->projectileSource.SetZ(node->enemy->projectileSource.Z() - .5 * (box->ZMax() - box->ZMin())); enemies.push_back(node->enemy); // Insert node group_nodes[depth]->bbox.Union(node->bbox); group_nodes[depth]->children.push_back(node); node->parent = group_nodes[depth]; } */ //enemy - a mesh that moves and shoots else if (!strcmp(cmd, "enemy")) { // Read data int fixed; int m; float vx, vy, vz; int h; char meshname[256]; float particle_velocity; int firing_rate; if (fscanf(fp, "%d%d%s%f%f%f%d%f%d", &fixed, &m, meshname, &vx, &vy, &vz, &h,&particle_velocity, &firing_rate) != 9) { fprintf(stderr, "Unable to parse enemy command %d in file %s\n", command_number, filename); return 0; } // Get material R3Material *material = group_materials[depth]; if (m >= 0) { if (m < (int) materials.size()) { material = materials[m]; } else { fprintf(stderr, "Invalid material id at cone command %d in file %s\n", command_number, filename); return 0; } } // Get mesh filename char buffer[2048]; strcpy(buffer, filename); char *bufferp = strrchr(buffer, '/'); if (bufferp) *(bufferp+1) = '\0'; else buffer[0] = '\0'; strcat(buffer, meshname); R3Vector *initialVelocity = new R3Vector(vx, vy, vz); // Create mesh R3Mesh *mesh = new R3Mesh(); if (!mesh) { fprintf(stderr, "Unable to allocate mesh\n"); return 0; } // Read mesh file if (!mesh->Read(buffer)) { fprintf(stderr, "Unable to read mesh: %s\n", buffer); return 0; } // Create shape R3Shape *shape = new R3Shape(); shape->type = R3_MESH_SHAPE; shape->box = NULL; shape->sphere = NULL; shape->cylinder = NULL; shape->cone = NULL; shape->mesh = mesh; shape->segment = NULL; // Create shape node R3Node *node = new R3Node(); node->transformation = R3identity_matrix; node->cumulativeTransformation = R3identity_matrix; node->material = material; node->shape = shape; node->bbox = mesh->bbox; node->enemy = new SFEnemy(fixed, mesh, *initialVelocity, h, particle_velocity, firing_rate); node->enemy->position = shape->mesh->Center(); node->enemy->projectileSource = shape->mesh->Center(); node->enemy->node = node; enemies.push_back(node->enemy); // Insert node group_nodes[depth]->bbox.Union(node->bbox); group_nodes[depth]->children.push_back(node); node->parent = group_nodes[depth]; } else if (!strcmp(cmd, "cone")) { // Read data int m; R3Point c; double r, h; if (fscanf(fp, "%d%lf%lf%lf%lf%lf", &m, &c[0], &c[1], &c[2], &r, &h) != 6) { fprintf(stderr, "Unable to read cone at command %d in file %s\n", command_number, filename); return 0; } // Get material R3Material *material = group_materials[depth]; if (m >= 0) { if (m < (int) materials.size()) { material = materials[m]; } else { fprintf(stderr, "Invalid material id at cone command %d in file %s\n", command_number, filename); return 0; } } // Create cone R3Cone *cone = new R3Cone(c, r, h); // Create shape R3Shape *shape = new R3Shape(); shape->type = R3_CONE_SHAPE; shape->box = NULL; shape->sphere = NULL; shape->cylinder = NULL; shape->cone = cone; shape->mesh = NULL; shape->segment = NULL; // Create shape node R3Node *node = new R3Node(); node->transformation = R3identity_matrix; node->material = material; node->shape = shape; node->bbox = cone->BBox(); node->enemy = NULL; // Insert node group_nodes[depth]->bbox.Union(node->bbox); group_nodes[depth]->children.push_back(node); node->parent = group_nodes[depth]; } else if (!strcmp(cmd, "line")) { // Read data int m; R3Point p1, p2; if (fscanf(fp, "%d%lf%lf%lf%lf%lf%lf", &m, &p1[0], &p1[1], &p1[2], &p2[0], &p2[1], &p2[2]) != 7) { fprintf(stderr, "Unable to read line at command %d in file %s\n", command_number, filename); return 0; } // Get material R3Material *material = group_materials[depth]; if (m >= 0) { if (m < (int) materials.size()) { material = materials[m]; } else { fprintf(stderr, "Invalid material id at line command %d in file %s\n", command_number, filename); return 0; } } // Create segment R3Segment *segment = new R3Segment(p1, p2); // Create shape R3Shape *shape = new R3Shape(); shape->type = R3_SEGMENT_SHAPE; shape->box = NULL; shape->sphere = NULL; shape->cylinder = NULL; shape->cone = NULL; shape->mesh = NULL; shape->segment = segment; // Create shape node R3Node *node = new R3Node(); node->transformation = R3identity_matrix; node->material = material; node->shape = shape; node->bbox = segment->BBox(); node->enemy = NULL; // Insert node group_nodes[depth]->bbox.Union(node->bbox); group_nodes[depth]->children.push_back(node); node->parent = group_nodes[depth]; } else if (!strcmp(cmd, "begin")) { // Read data int m; double matrix[16]; if (fscanf(fp, "%d%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", &m, &matrix[0], &matrix[1], &matrix[2], &matrix[3], &matrix[4], &matrix[5], &matrix[6], &matrix[7], &matrix[8], &matrix[9], &matrix[10], &matrix[11], &matrix[12], &matrix[13], &matrix[14], &matrix[15]) != 17) { fprintf(stderr, "Unable to read begin at command %d in file %s\n", command_number, filename); return 0; } // Get material R3Material *material = group_materials[depth]; if (m >= 0) { if (m < (int) materials.size()) { material = materials[m]; } else { fprintf(stderr, "Invalid material id at cone command %d in file %s\n", command_number, filename); return 0; } } // Create new group node R3Node *node = new R3Node(); node->transformation = R3Matrix(matrix); node->material = NULL; node->shape = NULL; node->bbox = R3null_box; node->enemy = NULL; // Push node onto stack depth++; group_nodes[depth] = node; group_materials[depth] = material; } else if (!strcmp(cmd, "end")) { // Pop node from stack R3Node *node = group_nodes[depth]; depth--; // Transform bounding box node->bbox.Transform(node->transformation); // Insert node group_nodes[depth]->bbox.Union(node->bbox); group_nodes[depth]->children.push_back(node); node->parent = group_nodes[depth]; } else if (!strcmp(cmd, "material")) { // Read data R3Rgb ka, kd, ks, kt, e; double n, ir; char texture_name[256]; if (fscanf(fp, "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%s", &ka[0], &ka[1], &ka[2], &kd[0], &kd[1], &kd[2], &ks[0], &ks[1], &ks[2], &kt[0], &kt[1], &kt[2], &e[0], &e[1], &e[2], &n, &ir, texture_name) != 18) { fprintf(stderr, "Unable to read material at command %d in file %s\n", command_number, filename); return 0; } // Create material R3Material *material = new R3Material(); material->ka = ka; material->kd = kd; material->ks = ks; material->kt = kt; material->emission = e; material->shininess = n; material->indexofrefraction = ir; material->texture = NULL; // Read texture if (strcmp(texture_name, "0")) { // Get texture filename char buffer[2048]; strcpy(buffer, filename); char *bufferp = strrchr(buffer, '/'); if (bufferp) *(bufferp+1) = '\0'; else buffer[0] = '\0'; strcat(buffer, texture_name); // Read texture image material->texture = new R2Image(); if (!material->texture->Read(buffer)) { fprintf(stderr, "Unable to read texture from %s at command %d in file %s\n", buffer, command_number, filename); return 0; } } // Insert material materials.push_back(material); } else if (!strcmp(cmd, "dir_light")) { // Read data R3Rgb c; R3Vector d; if (fscanf(fp, "%lf%lf%lf%lf%lf%lf", &c[0], &c[1], &c[2], &d[0], &d[1], &d[2]) != 6) { fprintf(stderr, "Unable to read directional light at command %d in file %s\n", command_number, filename); return 0; } // Normalize direction d.Normalize(); // Create light R3Light *light = new R3Light(); light->type = R3_DIRECTIONAL_LIGHT; light->color = c; light->position = R3Point(0, 0, 0); light->direction = d; light->radius = 0; light->constant_attenuation = 0; light->linear_attenuation = 0; light->quadratic_attenuation = 0; light->angle_attenuation = 0; light->angle_cutoff = M_PI; // Insert light lights.push_back(light); } else if (!strcmp(cmd, "point_light")) { // Read data R3Rgb c; R3Point p; double ca, la, qa; if (fscanf(fp, "%lf%lf%lf%lf%lf%lf%lf%lf%lf", &c[0], &c[1], &c[2], &p[0], &p[1], &p[2], &ca, &la, &qa) != 9) { fprintf(stderr, "Unable to read point light at command %d in file %s\n", command_number, filename); return 0; } // Create light R3Light *light = new R3Light(); light->type = R3_POINT_LIGHT; light->color = c; light->position = p; light->direction = R3Vector(0, 0, 0); light->radius = 0; light->constant_attenuation = ca; light->linear_attenuation = la; light->quadratic_attenuation = qa; light->angle_attenuation = 0; light->angle_cutoff = M_PI; // Insert light lights.push_back(light); } else if (!strcmp(cmd, "spot_light")) { // Read data R3Rgb c; R3Point p; R3Vector d; double ca, la, qa, sc, sd; if (fscanf(fp, "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", &c[0], &c[1], &c[2], &p[0], &p[1], &p[2], &d[0], &d[1], &d[2], &ca, &la, &qa, &sc, &sd) != 14) { fprintf(stderr, "Unable to read point light at command %d in file %s\n", command_number, filename); return 0; } // Normalize direction d.Normalize(); // Create light R3Light *light = new R3Light(); light->type = R3_SPOT_LIGHT; light->color = c; light->position = p; light->direction = d; light->radius = 0; light->constant_attenuation = ca; light->linear_attenuation = la; light->quadratic_attenuation = qa; light->angle_attenuation = sd; light->angle_cutoff = sc; // Insert light lights.push_back(light); } else if (!strcmp(cmd, "area_light")) { // Read data R3Rgb c; R3Point p; R3Vector d; double radius, ca, la, qa; if (fscanf(fp, "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", &c[0], &c[1], &c[2], &p[0], &p[1], &p[2], &d[0], &d[1], &d[2], &radius, &ca, &la, &qa) != 13) { fprintf(stderr, "Unable to read area light at command %d in file %s\n", command_number, filename); return 0; } // Normalize direction d.Normalize(); // Create light R3Light *light = new R3Light(); light->type = R3_AREA_LIGHT; light->color = c; light->position = p; light->direction = d; light->radius = radius; light->constant_attenuation = ca; light->linear_attenuation = la; light->quadratic_attenuation = qa; light->angle_attenuation = 0; light->angle_cutoff = M_PI; // Insert light lights.push_back(light); } else if (!strcmp(cmd, "camera")) { // Read data double px, py, pz, dx, dy, dz, ux, uy, uz, xfov, neardist, fardist; if (fscanf(fp, "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", &px, &py, &pz, &dx, &dy, &dz, &ux, &uy, &uz, &xfov, &neardist, &fardist) != 12) { fprintf(stderr, "Unable to read camera at command %d in file %s\n", command_number, filename); return 0; } // Assign camera camera.eye = R3Point(px, py, pz); camera.towards = R3Vector(dx, dy, dz); camera.towards.Normalize(); camera.up = R3Vector(ux, uy, uz); camera.up.Normalize(); camera.right = camera.towards % camera.up; camera.right.Normalize(); camera.up = camera.right % camera.towards; camera.up.Normalize(); camera.xfov = xfov; camera.yfov = xfov; camera.neardist = neardist; camera.fardist = fardist; } else if (!strcmp(cmd, "include")) { // Read data char scenename[256]; if (fscanf(fp, "%s", scenename) != 1) { fprintf(stderr, "Unable to read include command %d in file %s\n", command_number, filename); return 0; } // Get scene filename char buffer[2048]; strcpy(buffer, filename); char *bufferp = strrchr(buffer, '/'); if (bufferp) *(bufferp+1) = '\0'; else buffer[0] = '\0'; strcat(buffer, scenename); // Read scene from included file if (!Read(buffer, group_nodes[depth])) { fprintf(stderr, "Unable to read included scene: %s\n", buffer); return 0; } } else if (!strcmp(cmd, "background")) { // Read data double r, g, b; if (fscanf(fp, "%lf%lf%lf", &r, &g, &b) != 3) { fprintf(stderr, "Unable to read background at command %d in file %s\n", command_number, filename); return 0; } // Assign background color background = R3Rgb(r, g, b, 1); } else if (!strcmp(cmd, "ambient")) { // Read data double r, g, b; if (fscanf(fp, "%lf%lf%lf", &r, &g, &b) != 3) { fprintf(stderr, "Unable to read ambient at command %d in file %s\n", command_number, filename); return 0; } // Assign ambient color ambient = R3Rgb(r, g, b, 1); } else { fprintf(stderr, "Unrecognized command %d in file %s: %s\n", command_number, filename, cmd); return 0; } // Increment command number command_number++; } // Update bounding box bbox = root->bbox; // Provide default camera if (camera.xfov == 0) { double scene_radius = bbox.DiagonalRadius(); R3Point scene_center = bbox.Centroid(); camera.towards = R3Vector(0, 0, -1); camera.up = R3Vector(0, 1, 0); camera.right = R3Vector(1, 0, 0); camera.eye = scene_center - 3 * scene_radius * camera.towards; camera.xfov = 0.25; camera.yfov = 0.25; camera.neardist = 0.01 * scene_radius; camera.fardist = 100 * scene_radius; } // Provide default lights if (lights.size() == 0) { // Create first directional light R3Light *light = new R3Light(); R3Vector direction(-3,-4,-5); direction.Normalize(); light->type = R3_DIRECTIONAL_LIGHT; light->color = R3Rgb(1,1,1,1); light->position = R3Point(0, 0, 0); light->direction = direction; light->radius = 0; light->constant_attenuation = 0; light->linear_attenuation = 0; light->quadratic_attenuation = 0; light->angle_attenuation = 0; light->angle_cutoff = M_PI; lights.push_back(light); // Create second directional light light = new R3Light(); direction = R3Vector(3,2,3); direction.Normalize(); light->type = R3_DIRECTIONAL_LIGHT; light->color = R3Rgb(0.5, 0.5, 0.5, 1); light->position = R3Point(0, 0, 0); light->direction = direction; light->radius = 0; light->constant_attenuation = 0; light->linear_attenuation = 0; light->quadratic_attenuation = 0; light->angle_attenuation = 0; light->angle_cutoff = M_PI; lights.push_back(light); } // Close file fclose(fp); // Return success return 1; }
void Rails::Update(double dt) { time -= dt; if (time < 0) { int i = -1; int j = -1; time += 1.0/MOVE_SPEED*(double)GetWidth()/TERRAIN_DPS;//*GetWidth()/(double)TERRAIN_SIZE;//TERRAIN_SIZE/(double)TERRAIN_DPS;//*coeff;//(double)GetWidth()*coeff; float greatest = 0; R2Point previousLocation = oldLocation; R2Point firstLocation = currentLocation; R2Point secondLocation = currentLocation; for (int k = 0; k < 5; k++) { float greatest = 0; for (i = -1; i <= 1; i++) { for (j = -1; j <= 1; j++) { if (i == 0 && j == 0) continue; if (params.railMap->Pixel(currentLocation.X()+i,currentLocation.Y()+j)[0] > greatest) { R2Point tempLocation = R2Point(currentLocation.X()+i,currentLocation.Y()+j); if (tempLocation.X() != oldLocation.X() || tempLocation.Y() != oldLocation.Y()) { if (tempLocation.X() != oldoldLocation.X() || tempLocation.Y() != oldoldLocation.Y()) { greatest = params.railMap->Pixel(currentLocation.X()+i,currentLocation.Y()+j)[0]; nextLocation = tempLocation; } } } } } if (k==0) secondLocation = nextLocation; oldoldLocation = oldLocation; oldLocation = currentLocation; currentLocation = nextLocation; } for (i = -1; i <= 1; i++) { for (j = -1; j <= 1; j++) { if (i == 0 && j == 0) continue; if (params.railMap->Pixel(currentLocation.X()+i,currentLocation.Y()+j)[2] > .5) { globals.levelStatus = 1; globals.gsmgr->Stop(); } } } R3Vector v = R3Vector(-(currentLocation.X()-firstLocation.X()), 0, currentLocation.Y()-firstLocation.Y()); targetDirection = acos(-R3zaxis_vector.Dot(v)/v.Length()); targetDirection = targetDirection - (int)(targetDirection/6.28); if (v.X() < 0) { targetDirection = 6.28-targetDirection; } oldoldLocation = previousLocation; oldLocation = firstLocation; currentLocation = secondLocation; } //double coeff = (sqrt(2) - 1)*abs((firstLocation.X()-currentLocation.X())*(currentLocation.Y()-firstLocation.Y())) + 1; //printf("coeff %f\n", coeff); float closeness = 0; if (targetDirection < 0) closeness = (-currentDirection + targetDirection); else closeness = (-currentDirection + targetDirection); currentDirection += ROLL_SPEED*dt * closeness;//abs(closeness)*closeness; if (abs(currentDirection - targetDirection) > .05) { /* if (currentDirection < targetDirection) currentDirection += ROLL_SPEED*dt; if (currentDirection > targetDirection) currentDirection -= ROLL_SPEED*dt; */ } //currentDirection = targetDirection;//abs(closeness)*closeness; globals.player->SetDirection(currentDirection); }
void R3Box:: Draw(const R3DrawFlags draw_flags) const { static R3Vector normals[6] = { R3Vector(-1.0, 0.0, 0.0), R3Vector(1.0, 0.0, 0.0), R3Vector(0.0, -1.0, 0.0), R3Vector(0.0, 1.0, 0.0), R3Vector(0.0, 0.0, -1.0), R3Vector(0.0, 0.0, 1.0) }; static R2Point texcoords[4] = { R2Point(0.0, 0.0), R2Point(1.0, 0.0), R2Point(1.0, 1.0), R2Point(0.0, 1.0) }; static int surface_paths[6][4] = { { 3, 0, 1, 2 }, { 4, 7, 6, 5 }, { 0, 4, 5, 1 }, { 7, 3, 2, 6 }, { 3, 7, 4, 0 }, { 1, 5, 6, 2 } }; static int outline_path[16] = { 0, 1, 2, 3, 0, 4, 5, 6, 7, 4, 5, 1, 2, 6, 7, 3 }; /* Get box corner points */ R3Point corners[8]; corners[0] = Corner(RN_NNN_OCTANT); corners[1] = Corner(RN_NNP_OCTANT); corners[2] = Corner(RN_NPP_OCTANT); corners[3] = Corner(RN_NPN_OCTANT); corners[4] = Corner(RN_PNN_OCTANT); corners[5] = Corner(RN_PNP_OCTANT); corners[6] = Corner(RN_PPP_OCTANT); corners[7] = Corner(RN_PPN_OCTANT); // Draw surface if (draw_flags[R3_SURFACES_DRAW_FLAG]) { for (int i = 0; i < 6; i++) { R3BeginPolygon(); if (draw_flags[R3_SURFACE_NORMALS_DRAW_FLAG]) R3LoadNormal(normals[i]); for (int j = 0; j < 4; j++) { if (draw_flags[R3_VERTEX_TEXTURE_COORDS_DRAW_FLAG]) R3LoadTextureCoords(texcoords[j]); R3LoadPoint(corners[surface_paths[i][j]]); } R3EndPolygon(); } } // Draw edges if (draw_flags[R3_EDGES_DRAW_FLAG]) { R3BeginLine(); for (int i = 0; i < 16; i++) R3LoadPoint(corners[outline_path[i]]); R3EndLine(); } }
/* Source file for the GAPS triad class */ /* Include files */ #include "R3Shapes/R3Shapes.h" /* Public variables */ const R3Triad R3xyz_triad( R3Vector(1.0, 0.0, 0.0), R3Vector(0.0, 1.0, 0.0), R3Vector(0.0, 0.0, 1.0)); /* Public functions */ int R3InitTriad() { /* Return success */ return TRUE; } void R3StopTriad() {