/* animates a sphere */ void animateSphere (int id, float time) { /* animate vertices */ Vertex* vertices = (Vertex*) rtcMapBuffer(g_scene,id,RTC_VERTEX_BUFFER); const float rcpNumTheta = rcp((float)numTheta); const float rcpNumPhi = rcp((float)numPhi); const Vec3fa pos = position[id]; const float r = radius[id]; const float f = 2.0f*(1.0f+0.5f*sin(time)); /* loop over all vertices */ #if 1 // enables parallel execution launch_animateSphere(animateSphere,numPhi+1,vertices,rcpNumTheta,rcpNumPhi,pos,r,f); #else for (int phi = 0; phi <numPhi+1; phi++) for (int theta = 0; theta<numTheta; theta++) { Vertex* v = &vertices[phi*numTheta+theta]; const float phif = phi*float(pi)*rcpNumPhi; const float thetaf = theta*2.0f*float(pi)*rcpNumTheta; v->x = pos.x+r*sin(f*phif)*sin(thetaf); v->y = pos.y+r*cos(phif); v->z = pos.z+r*sin(f*phif)*cos(thetaf); } #endif rtcUnmapBuffer(g_scene,id,RTC_VERTEX_BUFFER); /* update mesh */ rtcUpdate (g_scene,id); }
/* called by the C++ code to render */ extern "C" void device_render (int* pixels, const unsigned int width, const unsigned int height, const float time, const ISPCCamera& camera) { float t0 = 0.7f*time; float t1 = 1.5f*time; /* rotate instances around themselves */ LinearSpace3fa xfm; xfm.vx = Vec3fa(cos(t1),0,sin(t1)); xfm.vy = Vec3fa(0,1,0); xfm.vz = Vec3fa(-sin(t1),0,cos(t1)); /* calculate transformations to move instances in cirle */ for (int i=0; i<4; i++) { float t = t0+i*2.0f*float(pi)/4.0f; instance_xfm[i] = AffineSpace3fa(xfm,2.2f*Vec3fa(+cos(t),0.0f,+sin(t))); } /* calculate transformations to properly transform normals */ for (int i=0; i<4; i++) normal_xfm[i] = transposed(rcp(instance_xfm[i].l)); /* set instance transformations */ rtcSetTransform2(g_scene,g_instance0,RTC_MATRIX_COLUMN_MAJOR_ALIGNED16,(float*)&instance_xfm[0],0); rtcSetTransform2(g_scene,g_instance1,RTC_MATRIX_COLUMN_MAJOR_ALIGNED16,(float*)&instance_xfm[1],0); rtcSetTransform2(g_scene,g_instance2,RTC_MATRIX_COLUMN_MAJOR_ALIGNED16,(float*)&instance_xfm[2],0); rtcSetTransform2(g_scene,g_instance3,RTC_MATRIX_COLUMN_MAJOR_ALIGNED16,(float*)&instance_xfm[3],0); /* update scene */ rtcUpdate(g_scene,g_instance0); rtcUpdate(g_scene,g_instance1); rtcUpdate(g_scene,g_instance2); rtcUpdate(g_scene,g_instance3); rtcCommit (g_scene); /* render all pixels */ const int numTilesX = (width +TILE_SIZE_X-1)/TILE_SIZE_X; const int numTilesY = (height+TILE_SIZE_Y-1)/TILE_SIZE_Y; parallel_for(size_t(0),size_t(numTilesX*numTilesY),[&](const range<size_t>& range) { const int threadIndex = (int)TaskScheduler::threadIndex(); for (size_t i=range.begin(); i<range.end(); i++) renderTileTask((int)i,threadIndex,pixels,width,height,time,camera,numTilesX,numTilesY); }); }
/* called by the C++ code to render */ extern "C" void device_render (int* pixels, const int width, const int height, const float time, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p) { /* create identity matrix */ AffineSpace3fa xfm; xfm.l.vx = Vec3fa(1,0,0); xfm.l.vy = Vec3fa(0,1,0); xfm.l.vz = Vec3fa(0,0,1); xfm.p = Vec3fa(0,0,0); float t = 0.7f*time; /* move instances */ xfm.p = 2.0f*Vec3fa(+cos(t),0.0f,+sin(t)); rtcSetTransform(g_scene,g_instance0,RTC_MATRIX_COLUMN_MAJOR_ALIGNED16,(float*)&xfm); xfm.p = 2.0f*Vec3fa(-cos(t),0.0f,-sin(t)); rtcSetTransform(g_scene,g_instance1,RTC_MATRIX_COLUMN_MAJOR_ALIGNED16,(float*)&xfm); xfm.p = 2.0f*Vec3fa(-sin(t),0.0f,+cos(t)); rtcSetTransform(g_scene,g_instance2,RTC_MATRIX_COLUMN_MAJOR_ALIGNED16,(float*)&xfm); xfm.p = 2.0f*Vec3fa(+sin(t),0.0f,-cos(t)); rtcSetTransform(g_scene,g_instance3,RTC_MATRIX_COLUMN_MAJOR_ALIGNED16,(float*)&xfm); /* update scene */ rtcUpdate(g_scene,g_instance0); rtcUpdate(g_scene,g_instance1); rtcUpdate(g_scene,g_instance2); rtcUpdate(g_scene,g_instance3); rtcCommit (g_scene); /* render all pixels */ const int numTilesX = (width +TILE_SIZE_X-1)/TILE_SIZE_X; const int numTilesY = (height+TILE_SIZE_Y-1)/TILE_SIZE_Y; launch_renderTile(numTilesX*numTilesY,pixels,width,height,time,vx,vy,vz,p,numTilesX,numTilesY); }
extern "C" void ispcModified (RTCScene scene, unsigned geomID) { rtcUpdate(scene,geomID); }
void updateInstance (RTCScene scene, Instance* instance) { unsigned int geometry = instance->geometry; instance->world2local = rcp(instance->local2world); rtcUpdate(scene,instance->geometry); }