Ejemplo n.º 1
0
static void display() {
  
  // Compute test camera [the one we will use for shadowing]
  Frustum cf(PM_PERSPECTIVE, fovy, aspect, nplane, fplane);
  cf.update();
  
  cam_mv  = Matrix4::MakeTranslate(Vector3(0, 0, -cam_dtt));
  cam_mv *= Matrix4::MakeRotate(-cam_ypr[2], Vector3::UNIT_Z);
  cam_mv *= Matrix4::MakeRotate(-cam_ypr[1], Vector3::UNIT_X);
  cam_mv *= Matrix4::MakeRotate(-cam_ypr[0], Vector3::UNIT_Y);
  cam_mv *= Matrix4::MakeTranslate(-cam_aim);
  cam_imv = cam_mv.getFastInverse();
  cam_proj = cf.getProjectionMatrix();
  
  cam_mv2  = Matrix4::MakeTranslate(Vector3(0, 0, -cam_dtt2));
  cam_mv2 *= Matrix4::MakeRotate(-cam_ypr2[2], Vector3::UNIT_Z);
  cam_mv2 *= Matrix4::MakeRotate(-cam_ypr2[1], Vector3::UNIT_X);
  cam_mv2 *= Matrix4::MakeRotate(-cam_ypr2[0], Vector3::UNIT_Y);
  cam_mv2 *= Matrix4::MakeTranslate(-cam_aim2);
  cam_imv2 = cam_mv2.getFastInverse();
  cam_proj2 = cf.getProjectionMatrix();
  
  // Compute Scene bounding box
  AABox sceneBB;
  unsigned int i;
  Vector3 off1(-10.0f, 0, -5.0f);
  Vector3 off2(2.0f, 0, 10.0f);
  Vector3 off3(10.0, 2.0f, -2.0f);
  for (i=0; i<CubeVertexCount; ++i) {
    sceneBB.merge(Vector3(CubeVertexData[i].pos));
    sceneBB.merge(Vector3(CubeVertexData[i].pos) + off1);
    sceneBB.merge(Vector3(CubeVertexData[i].pos) + off2);
    sceneBB.merge(Vector3(CubeVertexData[i].pos) + off3);
  }
  for (i=0; i<PlaneVertexCount; ++i) {
    sceneBB.merge(Vector3(PlaneVertexData[i].pos));
  }
  
  // Compute light params
  light_pos_eye = Vector3(cam_mv * light_pos);
  
  if (!useAdvShadows) {
    light_mv = Matrix4::MakeLookAt(Vector3(light_pos), light_aim, Vector3::UNIT_Y);
    light_imv = light_mv.getFastInverse();
    Frustum lf(PM_PERSPECTIVE, 120, 1, nplane, fplane);
    lf.update();
    light_proj = lf.getProjectionMatrix();
  } else {
#ifndef NEW_SHADOW
    shadow.setType(stype);
#endif
    shadow.setSceneParameters(sceneBB, sceneBB);
    shadow.setShadowParameters(100.0f, 0.1f, shadowSize, 10000.0f);
    shadow.setCameraParameters(fovy, aspect, nplane, fplane, cam_mv);
    if (ltype == LT_DIR) {
      shadow.setDirLightParameters(light_dir);
    } else if (ltype == LT_SPOT) {
      shadow.setSpotLightParameters(Vector3(light_pos), light_dir, 80, 89);
    } else {
      shadow.setPointLightParameters(Vector3(light_pos));
    }
    shadow.compute();
    light_proj = shadow.getLightProj();
    light_mv = shadow.getLightView();
    light_imv = light_mv.getFastInverse();
  }
  
  // RTT
  FBObind();
  glClearColor(1,1,1,1);
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glDisable(GL_LIGHTING);
  glDisable(GL_TEXTURE_2D);
  glViewport(0, 0, shadowSize, shadowSize);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glMultMatrixf(light_proj);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glMultMatrixf(light_mv);
  cprog->bind();
#ifdef NEW_SHADOW
  //cprog->uniform("LS")->set(shadow.getLightView()); as set in light view, do not need to pass it
  cprog->uniform("depthOffset")->set(0.0f);
#endif
  CubeMesh->render();
  glPushMatrix();
    glTranslatef(off1.x, off1.y, off1.z);
    CubeMesh->render();
  glPopMatrix();
  glPushMatrix();
    glTranslatef(off2.x, off2.y, off2.z);
    CubeMesh->render();
  glPopMatrix();
  glPushMatrix();
    glTranslatef(off3.x, off3.y, off3.z);
    CubeMesh->render();
  glPopMatrix();
  cprog->unbind();
  glEnable(GL_TEXTURE_2D);
  glEnable(GL_LIGHTING);
  FBOunbind();
  
  // RTW
  glClearColor(0,0,0,1);
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  
  // DRAW FIRST VIEWPORT
  //
  glViewport(0, 0, scr_hwidth, scr_height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glMultMatrixf(cam_proj);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glMultMatrixf(cam_mv);
  // show light
  glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
  glPushMatrix();
    glDisable(GL_LIGHTING);
    glDisable(GL_TEXTURE_2D);
    glColor3f(0.8, 0.8, 0);
    glTranslatef(light_pos[0], light_pos[1], light_pos[2]);
    glutSolidSphere(1, 16, 16);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_LIGHTING);
  glPopMatrix();
  // render cube
  MESHsetMaterial(&CubeMaterial);
  glBindTexture(GL_TEXTURE_2D, 0);
  CubeMesh->render();
  glPushMatrix();
    glTranslatef(off1.x, off1.y, off1.z);
    CubeMesh->render();
  glPopMatrix();
  glPushMatrix();
    glTranslatef(off2.x, off2.y, off2.z);
    CubeMesh->render();
  glPopMatrix();
  glPushMatrix();
    glTranslatef(off3.x, off3.y, off3.z);
    CubeMesh->render();
  glPopMatrix();
  // render plane width shadows
  Matrix4 eyeToLightProj = light_proj * light_mv * cam_imv;
  MESHsetMaterial(&PlaneMaterial);
  rprog->bind();
  glBindTexture(GL_TEXTURE_2D, colorMap0);
  rprog->uniform("shadowMap")->set(GLint(0));
  rprog->uniform("depthBias")->set(0.0f); // should be on caster !!!
  rprog->uniform("shadowWidth")->set((GLint)shadowSize);
  rprog->uniform("lightPosEye")->set(light_pos_eye);
  rprog->uniform("eyeToLightProj")->set(eyeToLightProj);
#ifdef NEW_SHADOW
  rprog->uniform("LS")->set(light_mv * cam_imv);
#endif
  PlaneMesh->render();
  /*
  MESHsetMaterial(&CubeMaterial);
  glBindTexture(GL_TEXTURE_2D, 0);
  CubeMesh->render();
  glPushMatrix();
    glTranslatef(off1.x, off1.y, off1.z);
    CubeMesh->render();
  glPopMatrix();
  glPushMatrix();
    glTranslatef(off2.x, off2.y, off2.z);
    CubeMesh->render();
  glPopMatrix();
  glPushMatrix();
    glTranslatef(off3.x, off3.y, off3.z);
    CubeMesh->render();
  glPopMatrix();
  */
  rprog->unbind(); 
  
  // Draw Second viewport
  if (dbview == DV_HULLS) {
    glViewport(scr_hwidth, 0, scr_hwidth, scr_height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMultMatrixf(cam_proj2);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glMultMatrixf(cam_mv2);
    glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
    glPushMatrix();
      glDisable(GL_LIGHTING);
      glDisable(GL_TEXTURE_2D);
      glColor3f(0.8, 0.8, 0);
      glTranslatef(light_pos[0], light_pos[1], light_pos[2]);
      glutSolidSphere(1, 16, 16);
      glEnable(GL_TEXTURE_2D);
      glEnable(GL_LIGHTING);
    glPopMatrix();
    MESHsetMaterial(&CubeMaterial);
    glBindTexture(GL_TEXTURE_2D, 0);
    CubeMesh->render();
    glPushMatrix();
      glTranslatef(off1.x, off1.y, off1.z);
      CubeMesh->render();
    glPopMatrix();
    glPushMatrix();
      glTranslatef(off2.x, off2.y, off2.z);
      CubeMesh->render();
    glPopMatrix();
    glPushMatrix();
      glTranslatef(off3.x, off3.y, off3.z);
      CubeMesh->render();
    glPopMatrix();
    MESHsetMaterial(&PlaneMaterial);
    PlaneMesh->render();
    // render hulls / frustums
#ifndef NEW_SHADOW
    if (useAdvShadows) {
      Vector3 fc = Vector3(0.8f, 0.5f, 0.2f);
      Vector3 bc = Vector3(0.2f, 0.2f, 0.7f);
      renderHull(shadow.getBodyFocus(), bc, Matrix4::IDENTITY);
      renderHull(shadow.getBodyB(), fc, Matrix4::IDENTITY);
    }
    glPushMatrix();
      glColor3f(0.7f, 0.2f, 0.2f);
      glTranslatef(shadow.getE().x, shadow.getE().y, shadow.getE().z);
      glutSolidSphere(0.1, 16, 16);
    glPopMatrix();
#else
    // new shadow draw stuff
    Vector3 fc = Vector3(0.8f, 0.5f, 0.2f);
    Vector3 bc = Vector3(0.2f, 0.2f, 0.7f);
    ConvexHull3D ch;
    shadow.calcB(NULL, &ch);
    renderHull(ch, bc, Matrix4::IDENTITY);
    shadow.calcLVS(NULL, &ch);
    renderHull(ch, fc, Matrix4::IDENTITY);
    glPushMatrix();
      glColor3f(0.7f, 0.2f, 0.2f);
      glTranslatef(shadow.getE().x, shadow.getE().y, shadow.getE().z);
      glutSolidSphere(0.1, 16, 16);
    glPopMatrix();
#endif
    
  } else if (dbview == DV_PPS) {
    glViewport(scr_hwidth, 0, scr_hwidth, scr_height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPushMatrix();
      glMultMatrixf(light_proj);
      glMultMatrixf(light_mv);
      glDisable(GL_LIGHTING);
      glDisable(GL_DEPTH_TEST); // in PPS, z is inversed !!!
      glDisable(GL_TEXTURE_2D);
      glColor3fv(PlaneMaterial.diff);
      PlaneMesh->render();
      glPushMatrix();
        glColor3f(0.7f, 0.2f, 0.2f);
        glTranslatef(cam_imv(0,3), cam_imv(1,3), cam_imv(2,3));
        glutSolidSphere(0.1, 16, 16);
      glPopMatrix();
      glColor3fv(CubeMaterial.diff);
      CubeMesh->render();
      glPushMatrix();
        glTranslatef(off1.x, off1.y, off1.z);
        CubeMesh->render();
      glPopMatrix();
      glPushMatrix();
        glTranslatef(off2.x, off2.y, off2.z);
        CubeMesh->render();
      glPopMatrix();
      glPushMatrix();
        glTranslatef(off3.x, off3.y, off3.z);
        CubeMesh->render();
      glPopMatrix();
#ifndef NEW_SHADOW
      Vector3 fc = Vector3(0.8f, 0.5f, 0.2f);
      Vector3 bc = Vector3(0.2f, 0.2f, 0.7f);
      renderHull(shadow.getBodyLVS(), fc, Matrix4::IDENTITY);
      renderHull(shadow.getBodyB(), bc, Matrix4::IDENTITY);
#else
      // new shadow stuff
      Vector3 fc = Vector3(0.8f, 0.5f, 0.2f);
      Vector3 bc = Vector3(0.2f, 0.2f, 0.7f);
      ConvexHull3D ch;
      shadow.calcB(NULL, &ch);
      renderHull(ch, bc, Matrix4::IDENTITY);
#endif
      glEnable(GL_DEPTH_TEST);
      glEnable(GL_TEXTURE_2D);
      glEnable(GL_LIGHTING);
    glPopMatrix();
  }
#ifdef NEW_SHADOW
  else if (dbview == DV_NT) {
    std::cout << "Debug TSM" << std::endl;
    drawTSM(shadow.getTSMData());
  }
#endif
  
  /* RENDER QUAD
  glPushMatrix();
  glLoadIdentity();
  glDisable(GL_LIGHTING);
  glActiveTextureARB(GL_TEXTURE0_ARB);
  glBindTexture(GL_TEXTURE_2D, colorMap0);
  float top  = 10 * tan(DegToRad * 0.5 * fovy);
  float left = top * aspect;
  glBegin(GL_QUADS);
    glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 0);
    glVertex3f(-left, -top, -10);
    glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 0);
    glVertex3f(left, -top, -10);
    glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 1);
    glVertex3f(left, top, -10);
    glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 1);
    glVertex3f(-left, top, -10);
  glEnd();
  glEnable(GL_LIGHTING);
  glPopMatrix();
  */
  
  glutSwapBuffers();
}