SGscenegraph* sgNewSceneGraph() { SGscenegraph *sg = malloc(sizeof(SGscenegraph)); glGetIntegerv(GL_MAX_LIGHTS, &sg->maxLights); sg->usedLights = 0; obj_array_init(&sg->cams); obj_array_init(&sg->overlays); obj_array_init(&sg->scenes); sg->overlay_shader = sgGetProgram("overlay"); glUseProgram(sg->overlay_shader); sg->modelview_id = glGetUniformLocation(sg->overlay_shader, "ModelViewMatrix"); sg->projection_id = glGetUniformLocation(sg->overlay_shader, "ProjectionMatrix"); sg->tex_id = glGetUniformLocation(sg->overlay_shader, "Tex"); assert(sg->modelview_id != -1); assert(sg->projection_id != -1); assert(sg->tex_id != -1); glUseProgram(0); return sg; }
pl_system_t* pl_new_root_system(pl_world_t *world, const char *name, double m, double gm, double obliquity, double siderealPeriod, double eqRadius, double flattening) { assert(world); pl_system_t *sys = smalloc(sizeof(pl_system_t)); obj_array_init(&sys->orbits); obj_array_init(&sys->astroObjs); obj_array_init(&sys->rigidObjs); sys->name = strdup(name); sys->world = world; sys->parent = NULL; lwcoord_t p; lwc_set(&p, 0.0, 0.0, 0.0); quaternion_t q = q_rot(1.0, 0.0, 0.0, DEG_TO_RAD(obliquity)); sys->orbitalBody = pl_new_obj(world, name, m, gm, &p, q, siderealPeriod, obliquity, eqRadius, flattening); sys->orbitalBody->kepler = NULL; world->rootSys = sys; pl_sys_set_current_pos(sys); return sys; }
pl_system_t* pl_new_orbital_object(pl_world_t *world, const char *name, double m, double gm, double orbitPeriod, double obliquity, double siderealPeriod, double semiMaj, double semiMin, double inc, double ascendingNode, double argOfPeriapsis, double meanAnomaly, double eqRadius, double flattening) { assert(world); pl_system_t *sys = smalloc(sizeof(pl_system_t)); obj_array_init(&sys->orbits); obj_array_init(&sys->astroObjs); obj_array_init(&sys->rigidObjs); sys->name = strdup(name); sys->world = world; sys->parent = NULL; sys->orbitalPeriod = orbitPeriod; // TODO: Stack allocation based on untrusted length should not be here char orbitName[strlen(name) + strlen(" Orbit") + 1]; strcpy(orbitName, name); // safe as size is checked in allocation strcat(orbitName, " Orbit"); lwcoord_t p; lwc_set(&p, 0.0, 0.0, 0.0); // TODO: Cleanup this quaternion // Z-X-Z for euler angles quaternion_t q = q_rot(0.0, 0.0, 1.0, DEG_TO_RAD(ascendingNode)); q = q_mul(q, q_rot(1.0, 0.0, 0.0, DEG_TO_RAD(inc))); q = q_mul(q, q_rot(0.0, 0.0, 1.0, DEG_TO_RAD(argOfPeriapsis))); // TODO: Correct axis? q = q_mul(q, q_rot(1.0, 0.0, 0.0, DEG_TO_RAD(obliquity))); sys->orbitalBody = pl_new_obj_in_sys(sys/*world->rootSys*/, name, m, gm, &p, q, siderealPeriod, obliquity, eqRadius, flattening); sys->orbitalBody->kepler = plNewKeplerElements(sqrt((semiMaj*semiMaj-semiMin*semiMin)/(semiMaj*semiMaj)), semiMaj, inc, ascendingNode, argOfPeriapsis, meanAnomaly); return sys; }
sim_class_t* sim_register_class_(const char *super, const char *name, void *(*alloc)(sim_class_t *cls), void (*init)(sim_class_t *cls, void *obj, void *arg), void (*dealloc)(void *obj), void (*restored)(void *obj)) { sim_class_t *cls = malloc(sizeof(sim_class_t)); if (super) cls->super = sim_class_get(super); else cls->super = NULL; cls->name = strdup(name); cls->alloc = alloc; cls->init = init; if (dealloc) cls->dealloc = dealloc; else cls->dealloc = free; cls->restored = restored; obj_array_init(&cls->fields); cls->key_index_map = avl_str_new(); avl_insert(classes, name, cls); cls->interfaces = avl_str_new(); return cls; }
pl_collisioncontext_t* pl_new_collision_context(double size) { pl_collisioncontext_t *ctxt = smalloc(sizeof(pl_collisioncontext_t)); ctxt->pool = pool_create(sizeof(pl_recgrid_t)); obj_array_init(&ctxt->colls); ctxt->otree = pl_new_recgrid(ctxt, size); // Roughly the heliospause return ctxt; }
PLcollisioncontext* plNewCollisionContext(void) { PLcollisioncontext *ctxt = malloc(sizeof(PLcollisioncontext)); ctxt->pool = pool_create(sizeof(PLrecgrid)); obj_array_init(&ctxt->colls); ctxt->otree = plNewRecgrid(ctxt, plAuToMetres(100.0)); // Roughly the heliospause return ctxt; }
pl_world_t* pl_new_world(const char *name, double m, double gm, double radius, double siderealPeriod, double obliquity, double eqRadius, double flattening, double size) { pl_world_t *world = smalloc(sizeof(pl_world_t)); world->name = strdup(name); world->collCtxt = pl_new_collision_context(size); world->rootSys = pl_new_root_system(world, name, m, gm, obliquity, siderealPeriod, eqRadius, flattening); world->bhut_tree = pl_new_bhut_tree(size); pl_bhut_init_with_celmek(world->bhut_tree); obj_array_init(&world->objs); obj_array_init(&world->root_objs); obj_array_init(&world->partSys); return world; }
pl_recgrid_t* pl_new_recgrid(pl_collisioncontext_t *ctxt, double size) { pl_recgrid_t *recgrid = pool_alloc(ctxt->pool); recgrid->parent = NULL; assert(((uintptr_t)recgrid & 3) == 0); recgrid->size = size; memset(&recgrid->centre, 0, sizeof(lwcoord_t)); memset(recgrid->children, 0, sizeof(pl_recgrid_t *[8])); obj_array_init(&recgrid->objs); return recgrid; }
PLrecgrid* plNewRecgrid(PLcollisioncontext *ctxt, double size) { PLrecgrid *recgrid = pool_alloc(ctxt->pool); recgrid->parent = NULL; assert(((uintptr_t)recgrid & 3) == 0); recgrid->size = size; memset(&recgrid->centre, 0, sizeof(OOlwcoord)); memset(recgrid->children, 0, sizeof(PLrecgrid *[8])); obj_array_init(&recgrid->objs); return recgrid; }
SGscene* sgNewScene(SGscenegraph *sg, const char *name) { assert(name != NULL); SGscene *sc = malloc(sizeof(SGscene)); sc->name = strdup(name); sc->amb[0] = 0.2; sc->amb[1] = 0.2; sc->amb[2] = 0.2; sc->amb[3] = 1.0; sc->sg = sg; sc->lights = calloc(sg->maxLights, sizeof(SGlight*)); obj_array_init(&sc->objs); obj_array_push(&sg->scenes, sc); return sc; }