// kernal that produce ray direction texture void PhotonMapperGPU::EyeKernel() { #ifndef STUB_PHOTONMAPPERGPU // figure out necessary eye vectors Vector3f nn, vv, uu; // nn.set(eyePos.x - look.x, eyePos.y - look.y, eyePos.z - look.z); // make n // uu.set(up.cross(nn)); // make u = up X n // nn.normalize(); uu.normalize(); // make them unit length // vv.set(nn.cross(uu)); // make v = n X u nn = m_pCamera->N(); vv = m_pCamera->V(); uu = m_pCamera->U(); Point3f eyePos = m_pCamera->GetEye(); // firgure out world width and height float sH = m_pCamera->GetNearClipPlane() * tan( m_pCamera->GetViewAngle() * (3.1415/360)); float sW = sH * m_pCamera->GetAspect(); // cout << eyePos.x << " " << eyePos.y << " " << eyePos.z << endl; // cout << sH << " " << sW << endl; CShader& ray = m_ShaderManager->GetShader(RAY); CGparameter& n = ray.GetNamedParameter("IN2.n"); cgGLSetParameter3f(n, nn.X(), nn.Y(), nn.Z()); CGparameter& v = ray.GetNamedParameter("IN2.v"); cgGLSetParameter3f(v, vv.X(), vv.Y(), vv.Z()); CGparameter& u = ray.GetNamedParameter("IN2.u"); cgGLSetParameter3f(u, uu.X(), uu.Y(), uu.Z()); CGparameter& view = ray.GetNamedParameter("IN2.view"); cgGLSetParameter2f(view, m_pixelCols, m_pixelRows); CGparameter& world = ray.GetNamedParameter("IN2.world"); cgGLSetParameter2f(world, sW, sH); CGparameter& nearDist = ray.GetNamedParameter("IN2.nearDist"); cgGLSetParameter1f(nearDist, m_pCamera->GetNearClipPlane()); CGparameter& eye = ray.GetNamedParameter("eyePos"); cgGLSetParameter3f(eye, eyePos.X(), eyePos.Y(), eyePos.Z()); ray.Bind(); ray.Enable(); Workspace(); ray.Disable(); #endif // STUB_PHOTONMAPPERGPU }
// // Check if the given vertex is visible on the current user view // This method is used for rendering vertices labels, and // rendering edges/faces // Returns: true if visible bool edit_topo::isVertexVisible(Point3f v) { float pix; double tx,ty,tz; gluProject(v.X(),v.Y(),v.Z(), mvmatrix,projmatrix,viewport, &tx,&ty,&tz); glReadPixels(tx,ty,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&pix); float ff = fabs(tz - pix); return ((ff < 0.003)); }
// // Get visible vertex nearest to mouse position (from a given vertices list) // Returns: true if visible bool edit_topo::getVisibleVertexNearestToMouse(QList<Vtx> list, Vtx &out) { bool found = false; double minDist = 100000; int minIdx = -1; Point3f t; QList<Vtx> visib; for(int i=0; i<list.count(); i++) if(isVertexVisible(list.at(i).V)) visib.push_back(list.at(i)); QPoint mPos = QPoint(mousePos.x(), mouseRealY); for(int i=0; i<visib.count(); i++) { Point3f p = visib.at(i).V; double tx,ty,tz; gluProject(p.X(),p.Y(),p.Z(), mvmatrix,projmatrix,viewport, &tx,&ty,&tz); QPoint qp = QPoint(tx, ty); // faster distance //double dx = fabs((double)(qp.x() - mPos.x())); //double dy = fabs((double)(qp.y() - mPos.y())); //double dist = (dy > dx) ? 0.41*dx+0.941246*dy : 0.41*dy+0.941246*dx; double dist = sqrt((double)(math::Sqr(qp.x() - mPos.x()) + math::Sqr(qp.y() - mPos.y()))); if(dist<minDist) { minDist = dist; minIdx = i; found = true; } } if(found) { for(int j=0; j<list.count(); j++) if(list.at(j).vName==visib.at(minIdx).vName) { out = list.at(j); return true; } } return false; }
Point3f ConstrainedFire::pointMid(int p1, int p2) { Point3f point; Point3f point1,point2; point1.X() = m_Vertices[p1].position.X(); point1.Y() = m_Vertices[p1].position.Y(); point1.Z() = m_Vertices[p1].position.Z(); point2.X() = m_Vertices[p2].position.X(); point2.Y() = m_Vertices[p2].position.Y(); point2.Z() = m_Vertices[p2].position.Z(); point.X() = (point1.X() + point2.X())*0.5; point.Y() = (point1.Y() + point2.Y())*0.5; point.Z() = (point1.Z() + point2.Z())*0.5; return point; }
Point3f ConstrainedFire::modelCenter(std::vector<int> & source) { Point3f p; float centerX,centerY,centerZ; //模型的中点坐标值 float wid,hei,dep; //原始模型的长度、宽度、深度 int i; //循环控制变量 GLfloat maxx, minx, maxy, miny, maxz, minz; /* get the max/mins */ maxx = minx = m_Vertices[source[0]].position.X(); // not begin at 1+0,1+2,1+3 maxy = miny = m_Vertices[source[0]].position.Y(); maxz = minz = m_Vertices[source[0]].position.Z(); for (i = 1; i < (int)source.size();i++) { if (maxx < m_Vertices[source[i]].position.X()) maxx = m_Vertices[source[i]].position.X(); if (minx > m_Vertices[source[i]].position.X()) minx = m_Vertices[source[i]].position.X(); if (maxy < m_Vertices[source[i]].position.Y()) maxy = m_Vertices[source[i]].position.Y(); if (miny > m_Vertices[source[i]].position.Y()) miny = m_Vertices[source[i]].position.Y(); if (maxz < m_Vertices[source[i]].position.Z()) maxz = m_Vertices[source[i]].position.Z(); if (minz > m_Vertices[source[i]].position.Z()) minz = m_Vertices[source[i]].position.Z(); } wid = maxx-minx; hei = maxy-miny; dep = maxz-minz; /* calculate center of the model */ centerX = (maxx + minx) *0.5; centerY = (maxy + miny) *0.5; centerZ = (maxz + minz) *0.5; p.X()=centerX; p.Y()=centerY; p.Z()=centerZ; return p; }
void AmbientOcclusionPlugin::setCamera(Point3f camDir, Box3f &meshBBox) { cameraDir = camDir; GLfloat d = (meshBBox.Diag()/2.0) * 1.1, k = 0.1f; Point3f eye = meshBBox.Center() + camDir * (d+k); glViewport(0.0, 0.0, depthTexSize, depthTexSize); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-d, d, -d, d, k, k+(2.0*d) ); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(eye.X(), eye.Y(), eye.Z(), meshBBox.Center().X(), meshBBox.Center().Y(), meshBBox.Center().Z(), 0.0, 1.0, 0.0); }
void SdfGpuPlugin::setCamera(Point3f camDir, Box3f &meshBBox) { GLfloat d = (meshBBox.Diag()/2.0), k = 0.1f; Point3f eye = meshBBox.Center() + camDir * (d+k); mScale = 2*k+(2.0*d); glViewport(0.0, 0.0, mPeelingTextureSize, mPeelingTextureSize); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-d, d, -d, d, /*k*/0, mScale ); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(eye.X(), eye.Y(), eye.Z(), meshBBox.Center().X(), meshBBox.Center().Y(), meshBBox.Center().Z(), 0.0, 1.0, 0.0); }
bool ObjReader::LoadObj(char *szFileName) { FILE *file = fopen(szFileName, "rb"); ObjObject * obj = NULL;//模型 char materialFile[MAX_PATH]; char materialStr[MAX_PATH]; char tempStr[MAX_PATH]; char buf[MAX_PATH];//存储读入信息 Point3f position;//顶点信息 Point3f texCoord; Point3f normal; int posIndex;//三角形信息 int texIndex; int norIndex; while(fscanf(file, "%s", buf) != EOF) { //读取数据 switch(buf[0]) { case 'm'://材质库 for(int i=0;i<MAX_PATH;i++)materialFile[i] = 0; for(int i=0;i<MAX_PATH;i++)tempStr[i] = 0; fscanf(file,"%s",tempStr); for(int i=0;i<MAX_PATH;i++) { if(szFileName[i] =='.') { while(szFileName[i]!='\\'&&i>=0)i--; for(int j=0;j<=i;j++) materialFile[j] = szFileName[j];//路径 for(int j=i+1;j<MAX_PATH;j++) materialFile[j] = tempStr[j-i-1];//文件名 break; } } break; case 'u'://材质信息 for(int i=0;i<MAX_PATH;i++)materialStr[i] = 0; fscanf(file, "%s", materialStr); LoadMaterial(materialFile,materialStr,obj);//读取材质信息 break; case 'g'://物体 if(obj == NULL) { obj = new ObjObject;//构造新模型 m_Object.push_back(obj);//存入物体 } if(obj!=NULL && (int)obj->posIndices.size()>0)//构造新模型 { obj = new ObjObject;//构造新模型 m_Object.push_back(obj); } break; case 'v': //如果读取到 v,vt,vn 数据 switch(buf[1]) { case '\0': //顶点坐标 fscanf(file, "%f %f %f", &position.X(),&position.Y(), &position.Z()); Positions.push_back(position);//存储顶点信息 break; case 't'://纹理坐标 fscanf(file, "%f %f %f", &texCoord.X(),&texCoord.Y(), &texCoord.Z()); TexCoords.push_back(texCoord);//存储纹理信息 break; case 'n'://法线 fscanf(file, "%f %f %f", &normal.X(),&normal.Y(), &normal.Z()); Normals.push_back(normal);//存储法线信息 break; } break; case 'f': //如果读取到面数据 fscanf(file, "%s", buf); //obj文件索引从1开始计数 if(sscanf(buf, "%d//%d", &posIndex, &norIndex)==2) //顶点,法线 { obj->posIndices.push_back(posIndex-1); obj->norIndices.push_back(norIndex-1); fscanf(file, "%d//%d", &posIndex, &norIndex); obj->posIndices.push_back(posIndex-1); obj->norIndices.push_back(norIndex-1); fscanf(file, "%d//%d", &posIndex, &norIndex); obj->posIndices.push_back(posIndex-1); obj->norIndices.push_back(norIndex-1); } else if (sscanf(buf, "%d/%d/%d", &posIndex, &texIndex, &norIndex) == 3) //顶点 纹理 法线 { obj->posIndices.push_back(posIndex-1); obj->texIndices.push_back(texIndex-1); obj->norIndices.push_back(norIndex-1); fscanf(file, "%d/%d/%d", &posIndex, &texIndex, &norIndex); obj->posIndices.push_back(posIndex-1); obj->texIndices.push_back(texIndex-1); obj->norIndices.push_back(norIndex-1); fscanf(file, "%d/%d/%d", &posIndex, &texIndex, &norIndex); obj->posIndices.push_back(posIndex-1); obj->texIndices.push_back(texIndex-1); obj->norIndices.push_back(norIndex-1); } else if (sscanf(buf, "%d/%d", &posIndex, &texIndex) == 2) //顶点 纹理 { obj->posIndices.push_back(posIndex-1); obj->texIndices.push_back(texIndex-1); fscanf(file, "%d/%d",&posIndex, &texIndex); obj->posIndices.push_back(posIndex-1); obj->texIndices.push_back(texIndex-1); fscanf(file, "%d/%d", &posIndex, &texIndex); obj->posIndices.push_back(posIndex-1); obj->texIndices.push_back(texIndex-1); } else if(sscanf(buf, "%d", &posIndex) == 1) //顶点 { obj->posIndices.push_back(posIndex-1); fscanf(file, "%d", &posIndex); obj->posIndices.push_back(posIndex-1); fscanf(file, "%d", &posIndex); obj->posIndices.push_back(posIndex-1); } break; default: fgets(buf, sizeof(buf), file);//读入一行 break; } } fclose(file);//关闭文件 return true; }
float ConstrainedFire::pointDist(Point3f point1, Point3f point2) { return sqrt((point1.X()-point2.X())*(point1.X()-point2.X())+(point1.Y()-point2.Y())*(point1.Y()-point2.Y())+(point1.Z()-point2.Z())*(point1.Z()-point2.Z())); }