reMesh* reFBXAsset::importMesh(FbxNode* fbxNode) { qDebug() << "import mesh for" << fbxNode->GetName(); reMesh* mesh = new reMesh; FbxMesh* fmesh = (FbxMesh*) fbxNode->GetNodeAttribute(); FbxVector4* controlPoints = fmesh->GetControlPoints(); for (int i=0; i<fmesh->GetPolygonCount(); i++) { reFace* face = new reFace; for (int j=0; j<fmesh->GetPolygonSize(i); j++) { int vi = fmesh->GetPolygonVertex(i, j); reVertex vertex; vertex.pos = reVec3(controlPoints[vi][0], controlPoints[vi][1], controlPoints[vi][2]); FbxVector4 fNormal; fmesh->GetPolygonVertexNormal(i, j, fNormal); vertex.uv = getUV(fmesh, vi, i, j); vertex.normal = reVec3(fNormal[0], fNormal[1], fNormal[2]); face->vertices.push_back(vertex); } reMaterial* mat = getMaterial(fmesh, i, mesh->materialSet); mesh->addFace(face,mat ? mat->id: -1); } reMeshAsset* meshAsset = new reMeshAsset(meshes); meshAsset->mesh = mesh; meshes->children.push_back(meshAsset); meshAsset->setPath((dataDir().toStdString() + "/" + fbxNode->GetName() + ".mesh").c_str()); mesh->save(dataDir().toStdString() + "/" + fbxNode->GetName() + ".mesh"); return mesh; }
vec4 sampleLayer(sampler2D layer, LayerSettings layerSettings) { /* if (layerSettings.useConstantColor && false) { return layerSettings.constantColor; } */ return texture2D(layer, getUV(layerSettings)); }
bool sphere::hit(const ray& r, float t_min, float t_max, hit_record& rec) const { vec3 oc = r.origin() - center; float v2 = dot(r.direction(), r.direction()); float voc = dot(oc, r.direction()); // 判別式. float discriminant = voc * voc - v2 * (dot(oc, oc) - radius*radius); if (discriminant > 0) { // 手前. float temp = (-voc - sqrt(discriminant)) / v2; if (temp < t_max && temp > t_min) { rec.t = temp; rec.p = r.org + rec.t * r.dir; rec.normal = (rec.p - center); rec.normal.noramlize(); getUV(rec.u, rec.v, rec.normal); rec.mat_ptr = mat_ptr; return true; } // 奥. temp = (-voc + sqrt(discriminant)) / v2; if (temp < t_max && temp > t_min) { rec.t = temp; rec.p = r.org + rec.t * r.dir; rec.normal = (rec.p - center) / radius; rec.normal.noramlize(); getUV(rec.u, rec.v, rec.normal); rec.mat_ptr = mat_ptr; return true; } } return false; }
Colour Checkerboard::getTexture(const IntersectData& intersect, const Eigen::Matrix4d& view)const{ Colour outcolour; double* uv = getUV(intersect,view); outcolour = getTexture(uv); delete[] uv; return outcolour; }
void Application::compute_cylinders(Vertex vertices[]) { for( size_t i = 0; i <= N; ++i ) { for( size_t j = 0; j <= M; ++j ) { float theta = i / static_cast<float>( N ), z = height*j / static_cast<float>( M ); vertices[i + j*( N + 1 )].position = getUV(theta, z); vertices[i + j*( N + 1 )].colour = glm::normalize(vertices[i + j*( N + 1 )].position); } } /*vertices[( N + 1 )*( M + 1 )].position = glm::vec3(height,0.0f,0.0f); vertices[( N + 1 )*( M + 1 )].colour = vertices[( N + 1 )*( M + 1 )].position; vertices[( N + 1 )*( M + 1 ) + 1].position = glm::vec3(height,0.0f,0.0f); vertices[( N + 1 )*( M + 1 ) + 1].colour = glm::normalize(vertices[( N + 1 )*( M + 1 ) + 1].position);*/ }
static VertexData::Vertexes loadVertexes(const aiMesh* mesh) { VertexData::Vertexes vertexes; for (auto i = 0u; i < mesh->mNumVertices; i++) { vertexes.add(createVertex( mesh->mVertices[i], mesh->mNormals[i], getUV(mesh, i, 0) )); } return vertexes; }
/* * This method evaluates if the intersection is inside the polygon or not, in order to to this, it does a parallel projection of the polygon * Then it translate the polygon to the origin, and finally using the x axe, if the number of edges that the x axe touch is odd the intersection is inside the polygon * if is is pair, the intersection is not in the polygon */ int isThePointInsideThePolygon(OBJECT *object, VECTOR *intersection) { LIST_NODE_PTR pointList = ((LIST_NODE_PTR) object->polygon.listOfPointsSplashPtr); LIST_NODE_PTR startNode = pointList; int numberOfWalls = 0; long double u, v; int isTheFirstOne = 1, checkLastOne = 0; POINT currentPoint, nextPoint, previousPoint, lastPoint; //First I have to get u, v values which were used to splash the polygon (Made on the load fase) getUV(&u, &v, &object->normal, intersection); //Second I have to translate the polygon to the origin in order to "Count the walls" while (pointList != NULL) {//I walk the list until the penultimate element, this is because I evaluate the edges and not the points currentPoint.x = u - pointList->data.point.x; currentPoint.y = v - pointList->data.point.y; if (pointList->nextPtr != NULL) { //Case it is not the last point nextPoint.x = u - pointList->nextPtr->data.point.x; nextPoint.y = v - pointList->nextPtr->data.point.y; } else { //Case it is the last point nextPoint.x = u - startNode->data.point.x; nextPoint.y = v - startNode->data.point.y; if (checkLastOne == 1) { long double nextX, nextY, previousX, previousY; previousX = currentPoint.x; previousY = currentPoint.y; nextX = startNode->nextPtr->data.point.x; nextY = startNode->nextPtr->data.point.y; if ((previousY > 0.0 && nextY > 0.0) || (previousY < 0.0 && nextY < 0.0)) { numberOfWalls += 2; } else { numberOfWalls++; } } } //Logic of the corners, If a corner is a minum or a maximun we have to count two if it is a nee we count it only as one if (currentPoint.y == 0.0 && currentPoint.x > 0.0) { long double nextX, nextY, previousX, previousY; if (isTheFirstOne != 1) {//The previous X and Y is in the last node of the list previousX = previousPoint.x; previousY = previousPoint.y; nextX = nextPoint.x; nextY = nextPoint.y; if ((previousY > 0.0 && nextY > 0.0) || (previousY < 0.0 && nextY < 0.0)) { numberOfWalls += 2; } else { numberOfWalls++; } } else { checkLastOne = 1; //Case where I have to check the first corner } } //trivial acceptance. Both U are positive and one v is negative and the another is positive if ((currentPoint.x >= 0.0 && nextPoint.x >= 0.0) && ((currentPoint.y > 0.0 && nextPoint.y < 0.0) || (currentPoint.y < 0.0 && nextPoint.y > 0.0))) { numberOfWalls++; } else if (((currentPoint.x >= 0.0 && nextPoint.x <= 0.0) || (currentPoint.x <= 0.0 && nextPoint.x >= 0.0)) && ((currentPoint.y > 0.0 && nextPoint.y < 0.0) || (currentPoint.y < 0.0 && nextPoint.y > 0.0))) {//Hard Case, One v positive and the another one negative, one u positive and the another one negative long double m = (nextPoint.y - currentPoint.y) / (nextPoint.x - currentPoint.x); long double b = currentPoint.y - (m * currentPoint.x); long double xIntersection = (0 - b) / m; if (xIntersection > 0.0) { numberOfWalls++; } } previousPoint = currentPoint; pointList = pointList->nextPtr; isTheFirstOne = 0; } return (numberOfWalls % 2); }
void UVMapper::apply(Object &obj, Material *materialRef, unsigned short layerRef) { float u,v,s,t; Vector normal; map<unsigned short, set<cvrIndex>, ltushort>::iterator h; /* i -- used to iterate through the set of faces which belong to the given material */ set<cvrIndex>::iterator i; /* j -- used to step through the points of the face which is referenced by i */ vector<cvrIndex>::iterator j; unsigned int seg; obj.buildRefList(); if (obj.mat_reflist[materialRef].empty()) return; /* calculate the uv for the points referenced by this face's pointref vector */ switch (projection_mode) { case UV_PROJECTION_CUBIC: /* cubic projection needs to know the surface normal */ for (seg = 0; seg < obj.mat_reflist[materialRef].size(); seg++) { for (h = obj.mat_reflist[materialRef][seg].begin(); h != obj.mat_reflist[materialRef][seg].end(); h++) { for (i = (*h).second.begin(); i != (*h).second.end(); i++) { /* check and see if the uv's are allocated, if not, allocate them */ if ((unsigned short)obj.faces[(*i)]->uv.size() < layerRef+1) obj.faces[(*i)]->uv.resize(layerRef+1); if (obj.faces[(*i)]->uv[layerRef].size() != obj.faces[(*i)]->pointref.size()) obj.faces[(*i)]->uv[layerRef].resize(obj.faces[(*i)]->pointref.size()); cvrIndex pt_count = 0; /* used to keep track of which UV we're calculating (since pointrefs aren't necessicarily in order) */ for (j = obj.faces[(*i)]->pointref.begin(); j != obj.faces[(*i)]->pointref.end(); j++) { XYZ uvpoint = *obj.points[*j]; uvpoint -= center; /* shift the point by the centerpoint */ /* if we have a rotation, rotate the point to match it */ if (rotation.x || rotation.y || rotation.z) { XYZ npoint = uvpoint; rotatexyz(rotation,uvpoint,npoint); uvpoint = npoint; } /* first we need to check what the most 'dominant' direction of this face is (axis) */ float nx, ny, nz; nx = fabs(obj.faces[(*i)]->face_normal.x); ny = fabs(obj.faces[(*i)]->face_normal.y); nz = fabs(obj.faces[(*i)]->face_normal.z); /* x portion of vector is dominant, we're mapping in the Y/Z plane */ if (nx >= ny && nx >= nz) { /* we use a .5 offset because texture coordinates range from 0->1, so to center it we need to offset by .5 */ s = uvpoint.z / scale.z + 0.5f; /* account for scale here */ t = uvpoint.y / scale.y + 0.5f; u = fract(s); v = fract(t); } /* y portion of vector is dominant, we're mapping in the X/Z plane */ if (ny >= nx && ny >= nz) { s = -uvpoint.x / scale.x + 0.5f; t = uvpoint.z / scale.z + 0.5f; u = fract(s); v = fract(t); } /* z portion of vector is dominant, we're mapping in the X/Y plane */ if (nz >= nx && nz >= ny) { s = -uvpoint.x / scale.x + 0.5f; t = uvpoint.y / scale.y + 0.5f; u = fract(s); v = fract(t); } if (obj.faces[(*i)]->face_normal.x > 0) { u = -u; } if (obj.faces[(*i)]->face_normal.y < 0) { u = -u; } if (obj.faces[(*i)]->face_normal.z > 0) { u = -u; } obj.faces[(*i)]->uv[layerRef][pt_count].u = u; obj.faces[(*i)]->uv[layerRef][pt_count].v = v; pt_count++; /* next point UV please */ } } } } break; default: /* simple XYZ to UV calc will suffice for non-cubic mapping */ for (seg = 0; seg < obj.mat_reflist[materialRef].size(); seg++) { for (h = obj.mat_reflist[materialRef][seg].begin(); h != obj.mat_reflist[materialRef][seg].end(); h++) { for (i = (*h).second.begin(); i != (*h).second.end(); i++) { if ((unsigned short)obj.faces[(*i)]->uv.size() < layerRef+1) obj.faces[(*i)]->uv.resize(layerRef+1); if (obj.faces[(*i)]->uv[layerRef].size() != obj.faces[(*i)]->pointref.size()) obj.faces[(*i)]->uv[layerRef].resize(obj.faces[(*i)]->pointref.size()); cvrIndex pt_count = 0; for (j = obj.faces[(*i)]->pointref.begin(); j != obj.faces[(*i)]->pointref.end(); j++) { getUV(*obj.points[*j], obj.faces[*i]->uv[layerRef][pt_count++]); /* map it, see getUV */ } } } } break; } };