Пример #1
0
int main()
{
// Allocate memory for object using new, print, and free
    std::string *pstr = new std::string("new string");
    std::cout << *pstr << std::endl;
    delete pstr;

// Allocate memory for array using new[], assign, print, and free
    constexpr std::size_t sz = 5;
    int *pai = new int[sz];

    for(std::size_t i = 0; i != sz; ++i)
        pai[i] = i;

    auto k = pai;
    while(k != pai + sz)
        std::cout << *(k++) << " ";
    std::cout << std::endl;

    delete[] pai;

// Allocate memory for vector using unique_ptr, assign, and print, delete underlying object
    using vsz = std::vector<int>::size_type;

    std::unique_ptr<std::vector<int>> upvec(new std::vector<int>());
    for(vsz i = 0; i != 5; ++i)
        upvec->push_back(i * 2);

    for(auto val : *upvec)
        std::cout << val << " ";
    std::cout << std::endl;

    upvec.reset();

// Allocate memory for vector using shared_ptr, make multiple pointers, assign, print, and free
    std::shared_ptr<std::vector<int>> spvec1, spvec2, spvec3, spvec = std::make_shared<std::vector<int>>();
    spvec1 = spvec2 = spvec3 = spvec;
    for(vsz i = 0; i != 5; ++i){
        spvec1->push_back(i);
        spvec2->push_back(i * 2);
    }
    for(auto val : *spvec3)
        std::cout << val << " ";

    std::cout << std::boolalpha;
    std::cout << "\nThe number of pointers sharing is equal to 1 : " << spvec.unique() << std::endl;
    std::cout << "The number of pointers sharing : " << spvec.use_count() << std::endl;

    spvec1.reset();
    spvec2.reset();
    spvec3.reset();
    spvec.reset();
    return 0;
}
Пример #2
0
/*!
	\param x
	\param y
	\param dir
	\param origin
	\param near_z
*/
void Storm3D_Camera::getRayVector(int x, int y, VC3 &dir, VC3 &origin, float near_z)
{
	D3DXMATRIX pProjection;
	D3DXMATRIX pView;

	D3DXVECTOR3 upvec(this->upvec.x, this->upvec.y, this->upvec.z);

	D3DXVECTOR3 position(this->position.x, this->position.y, this->position.z);
	D3DXVECTOR3 target(this->target.x, this->target.y, this->target.z);
	D3DXMatrixLookAtLH(&pView, &position, &target, &upvec);

	float fov = this->GetFieldOfView();
	Storm3D_SurfaceInfo ss = Storm3D2->GetScreenSize();
	float aspect=(float) ss.width / (float) ss.height;
	float vis_range = this->GetVisibilityRange();

	VC3 pV;

	D3DXMatrixPerspectiveFovLH(&pProjection, fov, aspect, 1.0f, vis_range);

	pV.x = ( ( ( 2.0f * (float)x * ss.width / 1024 ) / ss.width  ) - 1 ) / pProjection._11;
	pV.y = ( ( ( 2.0f * (float)y * ss.height / 768 ) / ss.height ) - 1 ) / pProjection._22;
	pV.z = 1.0f;

	D3DXMATRIX m;
	D3DXMatrixInverse(&m, NULL, &pView);

	VC3 vPickRayDir;
	VC3 vPickRayOrig;

	vPickRayDir.x  = pV.x * m._11 + pV.y * m._21 + pV.z * m._31;
	vPickRayDir.y  = pV.x * m._12 + pV.y * m._22 + pV.z * m._32;
	vPickRayDir.z  = pV.x * m._13 + pV.y * m._23 + pV.z * m._33;
	vPickRayDir.Normalize();
	vPickRayOrig.x = m._41;
	vPickRayOrig.y = m._42;
	vPickRayOrig.z = m._43;

	vPickRayOrig += vPickRayDir * near_z;

	dir.x = vPickRayDir.x;
	dir.y = vPickRayDir.y;
	dir.z = vPickRayDir.z;
	origin.x = vPickRayOrig.x;
	origin.y = vPickRayOrig.y;
	origin.z = vPickRayOrig.z;
}
Пример #3
0
/* 设置摄像机的位置,朝向和向上向量 */
void Camera::setCamera( float eyeX, float eyeY, float eyeZ,
                       float lookX, float lookY, float lookZ,
                       float upX, float upY, float upZ)
{
    /* 构造向量 */
    eye.set(eyeX, eyeY, eyeZ);
    look.set(lookX, lookY, lookZ);
    up.set(upX, upY, upZ);
    Vector3 upvec(up.x-eye.x,up.y-eye.y,up.z-eye.z);
    
    /* 计算n、u、v并归一化*/
//    n.set(eye.x-look.x, eye.y-look.y, eye.z-look.z);
//    u.set(upvec.cross(n).x,upvec.cross(n).y,upvec.cross(n).z);
//    v.set(n.cross(u).x,n.cross(u).y,n.cross(u).z);
    
//    u.normalize();
//    v.normalize();
//    n.normalize();
//    setModelViewMatrix();
}
Пример #4
0
void CmdRaytracingWriteCamera::activated(int iMsg)
{
    const char* ppReturn=0;
    getGuiApplication()->sendMsgToActiveView("GetCamera",&ppReturn);
    if (ppReturn) {
        std::string str(ppReturn);
        if (str.find("PerspectiveCamera") == std::string::npos) {
            int ret = QMessageBox::warning(Gui::getMainWindow(), 
                qApp->translate("CmdRaytracingWriteView","No perspective camera"),
                qApp->translate("CmdRaytracingWriteView","The current view camera is not perspective"
                                " and thus the result of the povray image later might look different to"
                                " what you expect.\nDo you want to continue?"),
                QMessageBox::Yes|QMessageBox::No);
            if (ret != QMessageBox::Yes)
                return;
        }
    }

    SoInput in;
    in.setBuffer((void*)ppReturn,std::strlen(ppReturn));

    SoNode* rootNode;
    SoDB::read(&in,rootNode);

    if (!rootNode || !rootNode->getTypeId().isDerivedFrom(SoCamera::getClassTypeId()))
        throw Base::Exception("CmdRaytracingWriteCamera::activated(): Could not read "
                              "camera information from ASCII stream....\n");

    // root-node returned from SoDB::readAll() has initial zero
    // ref-count, so reference it before we start using it to
    // avoid premature destruction.
    SoCamera * Cam = static_cast<SoCamera*>(rootNode);
    Cam->ref();

    SbRotation camrot = Cam->orientation.getValue();

    SbVec3f upvec(0, 1, 0); // init to default up vector
    camrot.multVec(upvec, upvec);

    SbVec3f lookat(0, 0, -1); // init to default view direction vector
    camrot.multVec(lookat, lookat);

    SbVec3f pos = Cam->position.getValue();
    float Dist = Cam->focalDistance.getValue();

    QStringList filter;
    filter << QObject::tr("Povray(*.pov)");
    filter << QObject::tr("All Files (*.*)");
    QString fn = Gui::FileDialog::getSaveFileName(Gui::getMainWindow(), QObject::tr("Export page"), QString(), filter.join(QLatin1String(";;")));
    if (fn.isEmpty()) 
        return;
    std::string cFullName = (const char*)fn.toUtf8();

    // building up the python string
    std::stringstream out;
    out << "Raytracing.writeCameraFile(\"" << strToPython(cFullName) << "\"," 
        << "(" << pos.getValue()[0]    <<"," << pos.getValue()[1]    <<"," << pos.getValue()[2]    <<")," 
        << "(" << lookat.getValue()[0] <<"," << lookat.getValue()[1] <<"," << lookat.getValue()[2] <<")," ;
    lookat *= Dist;
    lookat += pos;
    out << "(" << lookat.getValue()[0] <<"," << lookat.getValue()[1] <<"," << lookat.getValue()[2] <<")," 
        << "(" << upvec.getValue()[0]  <<"," << upvec.getValue()[1]  <<"," << upvec.getValue()[2]  <<") )" ;

    doCommand(Doc,"import Raytracing");
    doCommand(Gui,out.str().c_str());


    // Bring ref-count of root-node back to zero to cause the
    // destruction of the camera.
    Cam->unref();
}
Пример #5
0
void Scene::splatRecords()
{
  /*irradianceRecord rec1, rec2, rec3;
  rec1.pos[0] = 2.1766;
  rec1.pos[1] = 1.8176;
  rec1.pos[2] = 5.1762;
  rec1.norm[0] = -.573576;
  rec1.norm[1] = 0;
  rec1.norm[2] = -.819152;
  rec1.irradiance[0] = 1;
  rec1.irradiance[1] = 0;
  rec1.irradiance[2] = 0;
  rec1.hmd = 1.0;

  records.clear();
  records.push_back(rec1);
  */
  float eye[3];
  float eyedir[3];
  float up[3] = {0.0, 1.0, 0.0};
  view->getEye(eye);
  view->getCenter(eyedir);

  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, splatBufFBOID);
  glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
  glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
			    GL_TEXTURE_2D, splatTex, 0);
  glViewport(0, 0, windWidth, windHeight);
  glEnable(GL_BLEND);
  glBlendFunc(GL_ONE, GL_ONE);//add all the contributions together
  
  glUseProgram(splatProgram);
  glClear(GL_COLOR_BUFFER_BIT);
  glDisable(GL_LIGHTING);
  glDisable(GL_DEPTH_TEST);
  viewProjSetup(eye, eyedir, up, 45.0, 1.0, defaultFront, defaultBack);

  float color[4] = {1, 0, 1, 1};
  glPointSize(5);
  glColor4fv(color);

  //set up samplers
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D, coordTexBase[0]);
  glActiveTexture(GL_TEXTURE1);
  glBindTexture(GL_TEXTURE_2D, coordTexBase[1]);
  glActiveTexture(GL_TEXTURE2);
  glBindTexture(GL_TEXTURE_2D, coordTexBase[2]);
  
  float ws[2] = {windWidth, windHeight};
  std::cout << splatWindSizeUniform << std::endl;
  glUniform2fv(splatWindSizeUniform, 1, ws);

  glUniform1f(splatAUniform, a);

  glUniform1i(splatWorldPosUniform, 0);
  glUniform1i(splatWorldNormUniform, 1);
  glUniform1i(splatDiffuseUniform, 2);

  for(size_t i = 0; i < records.size(); ++i)
    {
      glUniform1f(splatUniform,records[i].hmd*a);
      glUniform1f(splatHmdUniform, records[i].hmd);
      glBegin(GL_QUADS);
      for(int j = 0; j < 4; ++j)
	{
	  glVertexAttrib1f(splatAttribute, 3 - j);
	  glNormal3fv(records[i].norm);
	  glColor3fv(records[i].irradiance);
	  glVertex3fv(records[i].pos);
	}
      glEnd();
    }

  glFlush();
  //glutSwapBuffers();
  std::cout << records.size() << "records splatted" << std::endl;
  Helpers::getGLErrors("end of splat");
  glIsShader(999);
  glIsTexture(222);
  std::cout << "after first records splatted " << timer.split() << std::endl;
  //now do another frag shader pass
  
  //now read the buffer back and to the CPU traversal
  
  glBindTexture(GL_TEXTURE_2D, splatTex);
  glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, splatBuffer);

  int startNewRecords = records.size();
  float * newpos;
  float * newnorm;

  double modelviewmat[16];
  double projectionmat[16];
  glGetDoublev(GL_MODELVIEW_MATRIX, modelviewmat);
  matrix4x4 modmat(modelviewmat);
  glGetDoublev(GL_PROJECTION_MATRIX, projectionmat);
  matrix4x4 projmat(projectionmat);

  double identity[16] = { 1, 0, 0, 0,
			  0, 1, 0, 0,
			  0, 0, 1, 0,
			  0, 0, 0, 1};

  vector3d xyz, norm, eyeCoord, perpLeft, perpDown;
  float upv[3] = {0, 1, 0}, right[3] = {1, 0, 0};
  vector3d upvec(upv);
  vector3d rightvec(right);

  vector3d botleft, topright;
  vector3d projbotLeft, projtopRight;

  float hmd;

  double x1, y1, z1, x2, y2, z2;//for gluproject

  GLint vp[4] = {0, 0, windWidth, windHeight};

  int xmin, xmax, ymin, ymax;

  for(size_t y = 0; y < windHeight; ++y)
    {
      for(size_t x = 0; x < windWidth; ++x)
	{
	  if(splatBuffer[(y*windWidth +x)*4 + 3] < a)
	    {
	      newpos = &objectCoords[(y*windWidth +x)*3];
	      newnorm = &objectNormals[(y*windWidth +x)*3];
	      generateRecord(newpos, newnorm);

	      hmd = records.back().hmd;

	      //do a simple cpusplat to update nearby weights
	      //mimic the v and f shaders almost exactly
	      xyz = vector3d(newpos);
	      norm = vector3d(newnorm);

	      eyeCoord = modmat.mult(xyz);
	      
	      perpLeft = (((eyeCoord.normalize()).cross(upvec)).normalize()).scale(a*hmd);
	      perpDown = (((eyeCoord.normalize()).cross(rightvec)).normalize()).scale(a*hmd);
	      

	      botleft = eyeCoord + perpLeft + perpDown;
	      topright = eyeCoord - perpLeft - perpDown;
	      
	      gluProject(botleft.xyz[0], botleft.xyz[1], botleft.xyz[2],
			 identity, projectionmat, vp,
			 &x1, &y1, &z1);

	      gluProject(topright.xyz[0], topright.xyz[1], topright.xyz[2],
			 identity, projectionmat, vp,
			 &x2, &y2, &z2);
	      
	      //bottom left is actually bottom right
	      // top right is actually top left
	      //WTF!!

	      xmin = std::max(0, int(x2));
	      xmax = std::min(int(windWidth), int(ceil(x1)));
	      ymin = std::max(0, int(y1));
	      ymax = std::min(int(windHeight), int(ceil(y2)));
	      //std::cout << "updating range: x1 x2, y1 y2: " << xmin <<
	      //' ' << xmax << ' ' << ymin << ' ' << ymax << std::endl;
	      //splat the w coord
	      int offset;
	      float w, dist, sqrtnrm, dot;
	      for(int fragy = ymin; fragy < ymax; ++fragy)
		{
		  for(int fragx = xmin; fragx < xmax; ++fragx)
		    {
		      //compute distance:
		      offset = (fragy*windWidth + fragx)*3;


		      dist = sqrt(pow(newpos[0] - objectCoords[offset], 2) +
				  pow(newpos[1] - objectCoords[offset+1],2)+
				  pow(newpos[2] - objectCoords[offset+2],2));
		      
		      if(dist >= a*hmd)
			continue;
		      dot = newnorm[0]*objectNormals[offset] +
			newnorm[1]*objectNormals[offset +1] +
			newnorm[2]*objectNormals[offset +2];
		      dot = std::min(1.0f, dot);
		      sqrtnrm = sqrt(1.0 - dot);

		      w = 1.0/((dist/hmd) + sqrtnrm);
		      if (w >= 1.0/a)
			{
			  splatBuffer[(fragy*windWidth +fragx)*4 + 3] += w;
			}
		      
		    }

		}

	    }
	}
    }
  std::cout << "cpu traversal" << timer.split() << std::endl;

  std::cout << "about to splat new records" << std::endl;
  glUseProgram(splatProgram);
  //splat the newly added records
  for(size_t i = startNewRecords; i < records.size(); ++i)
    {
      glUniform1f(splatUniform,records[i].hmd*a);
      glUniform1f(splatHmdUniform, records[i].hmd);
      glBegin(GL_QUADS);
      for(int j = 0; j < 4; ++j)
	{
	  glVertexAttrib1f(splatAttribute, 3 - j);
	  glNormal3fv(records[i].norm);
	  glColor3fv(records[i].irradiance);
	  glVertex3fv(records[i].pos);
	}
      glEnd();
    }
  std::cout << records.size() - startNewRecords << " new records created" <<
    std::endl;
  glFlush();


}