bool OSLRenderServices::trace(TraceOpt &options, OSL::ShaderGlobals *sg, const OSL::Vec3 &P, const OSL::Vec3 &dPdx, const OSL::Vec3 &dPdy, const OSL::Vec3 &R, const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy) { /* todo: options.shader support, maybe options.traceset */ ShaderData *sd = (ShaderData *)(sg->renderstate); /* setup ray */ Ray ray; ray.P = TO_FLOAT3(P); ray.D = TO_FLOAT3(R); ray.t = (options.maxdist == 1.0e30f) ? FLT_MAX : options.maxdist - options.mindist; ray.time = sd->time; if (options.mindist == 0.0f) { /* avoid self-intersections */ if (ray.P == sd->P) { bool transmit = (dot(sd->Ng, ray.D) < 0.0f); ray.P = ray_offset(sd->P, (transmit) ? -sd->Ng : sd->Ng); } } else { /* offset for minimum distance */ ray.P += options.mindist * ray.D; } /* ray differentials */ ray.dP.dx = TO_FLOAT3(dPdx); ray.dP.dy = TO_FLOAT3(dPdy); ray.dD.dx = TO_FLOAT3(dRdx); ray.dD.dy = TO_FLOAT3(dRdy); /* allocate trace data */ OSLTraceData *tracedata = (OSLTraceData *)sg->tracedata; tracedata->ray = ray; tracedata->setup = false; tracedata->init = true; tracedata->sd.osl_globals = sd->osl_globals; /* Raytrace, leaving out shadow opaque to avoid early exit. */ uint visibility = PATH_RAY_ALL_VISIBILITY - PATH_RAY_SHADOW_OPAQUE; return scene_intersect(sd->osl_globals, ray, visibility, &tracedata->isect); }
bool OSLRenderServices::trace(TraceOpt &options, OSL::ShaderGlobals *sg, const OSL::Vec3 &P, const OSL::Vec3 &dPdx, const OSL::Vec3 &dPdy, const OSL::Vec3 &R, const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy) { /* todo: options.shader support, maybe options.traceset */ ShaderData *sd = (ShaderData *)(sg->renderstate); /* setup ray */ Ray ray; ray.P = TO_FLOAT3(P); ray.D = TO_FLOAT3(R); ray.t = (options.maxdist == 1.0e30)? FLT_MAX: options.maxdist - options.mindist; ray.time = sd->time; if(options.mindist == 0.0f) { /* avoid self-intersections */ if(ray.P == sd->P) { bool transmit = (dot(sd->Ng, ray.D) < 0.0f); ray.P = ray_offset(sd->P, (transmit)? -sd->Ng: sd->Ng); } } else { /* offset for minimum distance */ ray.P += options.mindist*ray.D; } /* ray differentials */ ray.dP.dx = TO_FLOAT3(dPdx); ray.dP.dy = TO_FLOAT3(dPdy); ray.dD.dx = TO_FLOAT3(dRdx); ray.dD.dy = TO_FLOAT3(dRdy); /* allocate trace data */ OSLTraceData *tracedata = (OSLTraceData*)sg->tracedata; tracedata->ray = ray; tracedata->setup = false; tracedata->init = true; /* raytrace */ #ifdef __HAIR__ return scene_intersect(sd->osl_globals, &ray, ~0, &tracedata->isect, NULL, 0.0f, 0.0f); #else return scene_intersect(sd->osl_globals, &ray, ~0, &tracedata->isect); #endif }
void RagdollDemo::clientMoveAndDisplay() { #ifdef GRAPHICS glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); extern GLDebugDrawer gDebugDrawer; #endif if (m_dynamicsWorld) { double delta_x = brain->get_output(0); delta_x *= MOTOR_SCALE; //printf("delta_x: %f\n", delta_x); btVector3 agent_position = move_kinematic_body(agent, btVector3(delta_x, 0, 0)); agent->setActivationState(ACTIVE_TAG); btVector3 target_a_position = move_kinematic_body(target_a, btVector3(target_a_dx, 0, target_a_dz)); btVector3 target_b_position = move_kinematic_body(target_b, btVector3(target_b_dx, 0, target_b_dz)); target_a->setActivationState(ACTIVE_TAG); target_b->setActivationState(ACTIVE_TAG); if (create_draw_file) { draw_fh << agent_position.getX() << " " << agent_position.getZ() << " "; draw_fh << target_a_position.getX() << " " << target_a_position.getZ() << " "; draw_fh << target_b_position.getX() << " " << target_b_position.getZ() << std::endl; } for (int i = 0; i < NUM_RAYS; i++) { double angle = (-RAY_ANGLE / 2) + (i * (RAY_ANGLE / (NUM_RAYS - 1))); btVector3 ray_offset(RAY_LENGTH * sin(angle), 0, RAY_LENGTH * cos(angle)); btVector3 ray_end = agent_position - ray_offset; int touch = raycast(agent_position, ray_end); btVector3 ray = ray_end - agent_position; double sensor = (1.0 - (ray.length() / RAY_LENGTH)) * 10; ray_sensors[i] = sensor; #ifdef GRAPHICS btVector3 color = touch ? btVector3(0, .8, 0) : btVector3(0, 0, 0); gDebugDrawer.drawLine(agent_position, ray_end, color); #endif } if (!target_a_passed && (target_a_position.getZ() >= AGENT_Z)) { target_a_passed = true; target_a_distance = fabs(agent_position.getX() - target_a_position.getX()); } if (!target_b_passed && (target_b_position.getZ() >= AGENT_Z)) { target_b_passed = true; target_b_distance = fabs(agent_position.getX() - target_b_position.getX()); } if (target_a_passed && target_b_passed) { double fitness = target_a_distance + target_b_distance; std::ofstream fh; fh.open(ERROR_FILENAME); fh << fitness; fh.close(); if (create_draw_file) { draw_fh.close(); } exit(0); } frame_count++; m_dynamicsWorld->stepSimulation(TIME_STEP); brain->step(.01); #ifdef TEST_SPEED printf("%d %f\n", frame_count, agent_position.getX()); #endif //optional but useful: debug drawing m_dynamicsWorld->debugDrawWorld(); } //world exists #ifdef GRAPHICS renderme(); glFlush(); glutSwapBuffers(); #endif }