Ejemplo n.º 1
0
 void clickFunc(int button, int state, int x, int y)
 {
   if (state == GLUT_UP) {
     mouseMode = 0;
     if (button == GLUT_LEFT_BUTTON && glutGetModifiers() == GLUT_ACTIVE_CTRL) {
       Handle<Device::RTCamera> camera = createCamera(AffineSpace3f(g_camSpace.l,g_camSpace.p));
       Vector3f p;
       bool hit = g_device->rtPick(camera, x / float(g_width), y / float(g_height), g_render_scene, p.x, p.y, p.z);
       if (hit) {
         Vector3f delta = p - g_camLookAt;
         Vector3f right = cross(normalize(g_camUp),normalize(g_camLookAt-g_camPos));
         Vector3f offset = dot(delta,right)*right + dot(delta,g_camUp)*g_camUp;
         g_camLookAt = p;
         g_camPos += offset;
         g_camSpace = AffineSpace3f::lookAtPoint(g_camPos, g_camLookAt, g_camUp);
         g_resetAccumulation = true;
       }
     }
     else if (button == GLUT_LEFT_BUTTON && glutGetModifiers() == (GLUT_ACTIVE_CTRL | GLUT_ACTIVE_SHIFT)) {
       Handle<Device::RTCamera> camera = createCamera(AffineSpace3f(g_camSpace.l,g_camSpace.p));
       Vector3f p;
       bool hit = g_device->rtPick(camera, x / float(g_width), y / float(g_height), g_render_scene, p.x, p.y, p.z);
       if (hit) {
         Vector3f v = normalize(g_camLookAt - g_camPos);
         Vector3f d = p - g_camPos;
         g_camLookAt = g_camPos + v*dot(d,v);
         g_camSpace = AffineSpace3f::lookAtPoint(g_camPos, g_camLookAt, g_camUp);
         g_resetAccumulation = true;
       }
     }
   }
   else {
     if (glutGetModifiers() == GLUT_ACTIVE_CTRL) return;
     clickX = x; clickY = y;
     if      (button == GLUT_LEFT_BUTTON && glutGetModifiers() == GLUT_ACTIVE_ALT) mouseMode = 4;
     else if (button == GLUT_LEFT_BUTTON)   mouseMode = 1;
     else if (button == GLUT_MIDDLE_BUTTON) mouseMode = 2;
     else if (button == GLUT_RIGHT_BUTTON)  mouseMode = 3;
   }
 }
Ejemplo n.º 2
0
  Ref<Shape> TriangleMeshWithNormals::transform(const AffineSpace3f& xfm) const
  {
    /*! do nothing for identity matrix */
    if (xfm == AffineSpace3f(one))
      return (Shape*)this;

    /*! create transformed mesh */
    TriangleMeshWithNormals* mesh = new TriangleMeshWithNormals(ty);
    mesh->vertices.resize(vertices.size());
    for (size_t i=0; i<vertices.size(); i++) mesh->vertices[i].p = xfmPoint (xfm,*(Vector3f*)(void*)&vertices[i].p);
    for (size_t i=0; i<vertices.size(); i++) mesh->vertices[i].n = xfmNormal(xfm,*(Vector3f*)(void*)&vertices[i].n);
    mesh->triangles = triangles;
    return mesh;
  }
Ejemplo n.º 3
0
void Renderer::updateCamera() {

    Vec3f p = toEmbree(cam->getPosition());
    Vec3f t = toEmbree(cam->getTargetPos());
    Vec3f u = toEmbree(cam->getUpDir());
    AffineSpace3f camSpace = AffineSpace3f::lookAtPoint(p, t, u);
    AffineSpace3f transform = AffineSpace3f(camSpace.l, camSpace.p);

    device->rtSetTransform(camera, "local2world", copyToArray(transform));
    device->rtSetFloat1(camera, "angle", cam->getFov());
    device->rtSetFloat1(camera, "aspectRatio", float(width) / float(height));
    device->rtSetFloat1(camera, "lensRadius", cam->getRadius());
    device->rtSetFloat1(camera, "focalDistance", (cam->getTargetPos() - cam->getPosition()).length());
    device->rtCommit(camera);
}
Ejemplo n.º 4
0
  Ref<Shape> TriangleMeshFull::transform(const AffineSpace3f& xfm) const
  {
    /*! do nothing for identity matrix */
    if (xfm == AffineSpace3f(one))
      return (Shape*)this;

    /*! create transformed */
    TriangleMeshFull* mesh = new TriangleMeshFull(ty);
    mesh->position.resize(position.size());
    for (size_t i=0; i<position.size(); i++) mesh->position[i] = xfmPoint(xfm,position[i]);
    mesh->motion.resize(motion.size());
    for (size_t i=0; i<motion.size(); i++) mesh->motion[i] = xfmVector(xfm,motion[i]);
    mesh->normal.resize(normal.size()  );
    for (size_t i=0; i<normal.size(); i++) mesh->normal[i] = xfmNormal(xfm,normal[i]);
    mesh->tangent_x.resize(tangent_x.size()  );
    for (size_t i=0; i<tangent_x.size(); i++) mesh->tangent_x[i] = xfmVector(xfm,tangent_x[i]);
    mesh->tangent_y.resize(tangent_y.size()  );
    for (size_t i=0; i<tangent_y.size(); i++) mesh->tangent_y[i] = xfmVector(xfm,tangent_y[i]);
    mesh->texcoord  = texcoord;
    mesh->triangles = triangles;
    return mesh;
  }
Ejemplo n.º 5
0
  void displayFunc(void)
  {
    /* create random geometry for regression test */
    if (g_regression)
      g_render_scene = createRandomScene(g_device,1,random<int>()%1000,random<int>()%1000);

    /* set accumulation mode */
    int accumulate = g_resetAccumulation ? 0 : g_refine;
    g_resetAccumulation = false;

    /* render image */
    Handle<Device::RTCamera> camera = createCamera(AffineSpace3f(g_camSpace.l,g_camSpace.p));

    /* render into framebuffer */
    double t0 = getSeconds();
    g_device->rtRenderFrame(g_renderer,camera,g_render_scene,g_tonemapper,g_frameBuffer,accumulate);
    g_device->rtSwapBuffers(g_frameBuffer);

    /* draw image in OpenGL */
    void* ptr = g_device->rtMapFrameBuffer(g_frameBuffer);
    glRasterPos2i(-1, 1);
    glPixelZoom(1.0f, -1.0f);
    glDrawPixels((GLsizei)g_width,(GLsizei)g_height,GL_RGBA,GL_FLOAT,ptr);
    g_device->rtUnmapFrameBuffer(g_frameBuffer);
    glutSwapBuffers();
    double dt = getSeconds()-t0;

    /* measure rendering time */
    std::ostringstream stream;
    stream.setf(std::ios::fixed, std::ios::floatfield);
    stream.precision(2);
    stream << 1.0f/dt << " fps, ";
    stream.precision(0);
    stream << dt*1000.0f << " ms";
    std::cout << "display " << stream.str() << std::endl;
    glutSetWindowTitle((std::string("Embree: ") + stream.str()).c_str());
  }
Ejemplo n.º 6
0
  void displayFunc(void)
  {
    if (g_pause)
      return;

    if (g_demo) 
    {
      double restart_time = 15.0f;
      double dt = getSeconds()-g_demo_t0;
      if (dt > restart_time) {
        dt -= restart_time;
        g_demo_t0 = getSeconds() - dt;
      }
      float theta = dt * 90.0f / 180.0f * float(pi);
      float phi = 0.0f;
      if (theta >= 2.0f*float(pi)) theta = 2.0f*float(pi);
      
      const Vector3f viewVec = normalize(g_initial_camLookAt - g_initial_camPos);
      const float dist = length(g_initial_camLookAt - g_initial_camPos);
      //const Vector3f dX = normalize(cross(viewVec,g_initial_camUp));
      //const Vector3f dY = normalize(cross(viewVec,dX));
      const Vector3f dY = normalize(g_initial_camUp);
      AffineSpace3f space = g_demo_camSpace;
      //space = AffineSpace3f::rotate(g_initial_camLookAt,dX,phi  ) * space; 
      space = AffineSpace3f::rotate(g_initial_camLookAt,dY,theta) * space; 
      //g_camPos = g_camLookAt-dist*xfmVector(g_camSpace,Vector3f(0,0,1));
      g_camSpace = space;
      
      static int extra_resets = 0;
      if (theta < 2.0f*float(pi)) {
        g_resetAccumulation = true;
        extra_resets = 0;
      } else if (extra_resets < 2) {
        g_resetAccumulation = true;
        extra_resets++;
      }
    }
    
    /* create random geometry for regression test */
    if (g_regression)
      g_render_scene = createRandomScene(g_device,1,random<int>()%100,random<int>()%1000);

    /* set accumulation mode */
    int accumulate = g_resetAccumulation ? 0 : g_refine;
    g_resetAccumulation = false;

    /* render image */
    Handle<Device::RTCamera> camera = createCamera(AffineSpace3f(g_camSpace.l,g_camSpace.p));

    /* render into framebuffer */
    g_device->rtRenderFrame(g_renderer,camera,g_render_scene,g_tonemapper,g_frameBuffer,accumulate);
    g_device->rtSwapBuffers(g_frameBuffer);

    /* draw image in OpenGL */
    void* ptr = g_device->rtMapFrameBuffer(g_frameBuffer);

    glRasterPos2i(-1, 1);
    glPixelZoom(1.0f, -1.0f);

    if (g_format == "RGB_FLOAT32")
      glDrawPixels((GLsizei)g_width,(GLsizei)g_height,GL_RGB,GL_FLOAT,ptr);
    else if (g_format == "RGBA8")
      glDrawPixels((GLsizei)g_width,(GLsizei)g_height,GL_RGBA,GL_UNSIGNED_BYTE,ptr);
    else if (g_format == "RGB8")
      glDrawPixels((GLsizei)g_width,(GLsizei)g_height,GL_RGB,GL_UNSIGNED_BYTE,ptr);
    else 
    throw std::runtime_error("unknown framebuffer format: "+g_format);
                                                    
    glFlush();
    glutSwapBuffers();

    g_device->rtUnmapFrameBuffer(g_frameBuffer);

    /* calculate rendering time */
    double t1 = getSeconds();
    g_dt[frameID % avgFrames] = t1-g_t0; g_t0 = t1;
    frameID++;

    /* print average render time of previous frames */
    size_t num = 0;
    double dt = 0.0f;
    for (size_t i=0; i<avgFrames; i++) {
      if (g_dt[i] != 0.0f) {
        dt += g_dt[i]; num++;
      }
    }
    dt /= num;

    std::ostringstream stream;
    stream.setf(std::ios::fixed, std::ios::floatfield);
    stream.precision(2);
    stream << 1.0f/dt << " fps, ";
    stream.precision(2);
    stream << dt*1000.0f << " ms";
    stream << ", " << g_width << "x" << g_height;
    if (log_display)
      std::cout << "display " << stream.str() << std::endl;
    glutSetWindowTitle((std::string("Embree: ") + stream.str()).c_str());

  }
Ejemplo n.º 7
0
  Handle<Device::RTScene> createRandomScene(Device *device, size_t numLights, size_t numObjects, size_t numTriangles)
  {
    std::vector<Device::RTPrimitive> prims;

    //for (size_t i=0; i < numLights; i++)
    prims.push_back(device->rtNewLightPrimitive(createRandomLight(device), NULL, copyToArray(AffineSpace3f(one))));

    for (size_t i=0; i < numObjects; i++) {
      size_t s = numTriangles ? random<int>() % numTriangles : 0;
      prims.push_back(device->rtNewShapePrimitive(createRandomShape(device, s), createRandomMaterial(device), copyToArray(AffineSpace3f(one))));
    }

    Handle<Device::RTScene> scene = device->rtNewScene("default");
    g_device->rtSetString(scene,"accel",g_accel.c_str());
    g_device->rtSetString(scene,"builder",g_builder.c_str());
    g_device->rtSetString(scene,"traverser",g_traverser.c_str());
    for (size_t i=0; i<prims.size(); i++) g_device->rtSetPrimitive(scene,i,prims[i]);
    g_device->rtCommit(scene);
    
    for (size_t i=0; i<prims.size(); i++)
      device->rtDecRef(prims[i]);

    return scene;
  }
Ejemplo n.º 8
0
  void displayFunc(void)
  {
    if (g_pause)
      return;
    
    /* create random geometry for regression test */
    if (g_regression)
      g_render_scene = createRandomScene(g_device,1,random<int>()%100,random<int>()%1000);

    /* set accumulation mode */
    int accumulate = g_resetAccumulation ? 0 : g_refine;
    g_resetAccumulation = false;

    /* render image */
    Handle<Device::RTCamera> camera = createCamera(AffineSpace3f(g_camSpace.l,g_camSpace.p));

    /* render into framebuffer */
    g_device->rtRenderFrame(g_renderer,camera,g_render_scene,g_tonemapper,g_frameBuffer,accumulate);
    g_device->rtSwapBuffers(g_frameBuffer);

    /* draw image in OpenGL */
    void* ptr = g_device->rtMapFrameBuffer(g_frameBuffer);

    // extern double upload_time;
    
    // upload_time = getSeconds() - upload_time;
    // std::cout << "upload " << upload_time << std::endl;
    // exit(0);

    glRasterPos2i(-1, 1);
    glPixelZoom(1.0f, -1.0f);

    if (g_format == "RGB_FLOAT32")
      glDrawPixels((GLsizei)g_width,(GLsizei)g_height,GL_RGB,GL_FLOAT,ptr);
    else if (g_format == "RGBA8")
      glDrawPixels((GLsizei)g_width,(GLsizei)g_height,GL_RGBA,GL_UNSIGNED_BYTE,ptr);
    else if (g_format == "RGB8")
      glDrawPixels((GLsizei)g_width,(GLsizei)g_height,GL_RGB,GL_UNSIGNED_BYTE,ptr);
    else 
    throw std::runtime_error("unknown framebuffer format: "+g_format);
                                                    
    glFlush();
    glutSwapBuffers();

    g_device->rtUnmapFrameBuffer(g_frameBuffer);

    /* calculate rendering time */
    double t1 = getSeconds();
    g_dt[frameID % avgFrames] = t1-g_t0; g_t0 = t1;
    frameID++;

    /* print average render time of previous frames */
    size_t num = 0;
    double dt = 0.0f;
    for (size_t i=0; i<avgFrames; i++) {
      if (g_dt[i] != 0.0f) {
        dt += g_dt[i]; num++;
      }
    }
    dt /= num;

    std::ostringstream stream;
    stream.setf(std::ios::fixed, std::ios::floatfield);
    stream.precision(2);
    stream << 1.0f/dt << " fps, ";
    stream.precision(2);
    stream << dt*1000.0f << " ms";
    stream << ", " << g_width << "x" << g_height;
    if (log_display)
      std::cout << "display " << stream.str() << std::endl;
    glutSetWindowTitle((std::string("Embree: ") + stream.str()).c_str());

  }
Ejemplo n.º 9
0
Handle <Device::RTShape> Renderer::addShape(Device::RTMaterial material, Device::RTShape shape) {
    return addShape(material, shape, AffineSpace3f(one));
}