/*! * @param factor how often the @p lineVector has to be added to @p linePoint to * get the intersection point. If @p factor is between 0 and 1, then the * intersection point is on the line segment that starts at linePoint and ends * with linePoint + lineVector. * @return TRUE if the line intersects with the plane */ bool Plane::intersectLineInternal(const Vector3& linePoint, const Vector3& lineVector, real* factor) const { // http://geometryalgorithms.com/Archive/algorithm_0104/algorithm_0104B.htm#intersect3D_SegPlane() // provides a nice explanation of the maths in here real NdotLine = Vector3::dotProduct(getNormal(), lineVector); if (fabs(NdotLine) <= 0.001) { // intersection still possible, if the line is on the plane. return false; } // now we know the line _does_ intersect with the plane (let's call the point p) // the vector from a point on the plane to p is perpendicular to the plane // normal. // That means their dot product is 0. // // i.e. normal * (point_on_plane - p) = 0 // "p" can also be written as linePoint + a * lineVector, with a being a // certain real number. this makes: // normal * (point_on_plane - (linePoint + a * lineVector)) = 0 // => // normal * (point_on_plane - linePoint) + a * normal * lineVector = 0 // => // a = (normal * (point_on_plane - linePoint)) / (normal * lineVector) real foo = Vector3::dotProduct(getNormal(), calculatePointOnPlane() - linePoint); real a = foo / NdotLine; *factor = a; return true; }
void internal_best_effort_load( objLoader &objLoader, MeshData_t &meshData ) { for (int i=0; i<objLoader.faceCount; i++){ obj_face *facePtr = objLoader.faceList[i]; if ( facePtr->vertex_count==3 ) { for (int coord=0; coord<3; coord++){ meshData.vertices.push_back( getVertex(objLoader, facePtr->vertex_index[coord]) ); if (facePtr->normal_index[coord] != -1 ) meshData.normals.push_back( getNormal(objLoader, facePtr->normal_index[coord]) ); if (facePtr->texture_index[coord] != -1 ) meshData.texcoords.push_back( getTexCoord(objLoader, facePtr->texture_index[coord]) ); } } else if (facePtr->vertex_count==4 ) { for (int coord=0; coord<3; coord++){ meshData.vertices.push_back( getVertex(objLoader, facePtr->vertex_index[coord]) ); if (facePtr->normal_index[coord] != -1 ) meshData.normals.push_back( getNormal(objLoader, facePtr->normal_index[coord]) ); if (facePtr->texture_index[coord] != -1 ) meshData.texcoords.push_back( getTexCoord(objLoader, facePtr->texture_index[coord]) ); } int triangle_two[] = {0,2,3}; for (int coord=0; coord<3; coord++){ int idx2 = triangle_two[coord]; meshData.vertices.push_back( getVertex(objLoader, facePtr->vertex_index[idx2]) ); if (facePtr->normal_index[idx2] != -1 ) meshData.normals.push_back( getNormal(objLoader, facePtr->normal_index[idx2]) ); if (facePtr->texture_index[idx2] != -1 ) meshData.texcoords.push_back( getTexCoord(objLoader, facePtr->texture_index[idx2]) ); } } } // do NOT create indices }
float* Cubo::getNormal(int Xn, int Yn, int Zn){ /* Si bien el cálculo de la normal de la forma genérica funcionaría para * los puntos que no son aristas, las direcciones normales con las aristas * son compartidas en todos los casos. Es óptimo entonces reescribir el método * Además, como las aristas se repiten (esto es transparente para el emparchador), * la orientación de la normal depende de si es la primera vez o segunda que aparece. * Esto es muy dependiente de la implementación, así que, es difícil de entender si, * justamente, no se conoce cómo está escrito el cubito. Por ende, la mejor forma de * calcular la normal es en papel y lápiz, según el Xn (Xn = Yn) */ if (Xn >= cantidadDePuntosBorde()) return getNormal(Xn-cantidadDePuntosBorde(), Yn-cantidadDePuntosBorde(), Zn); if (Xn < 0) return getNormal(Xn+cantidadDePuntosBorde(), Yn+cantidadDePuntosBorde(), Zn); float* normal = new float[3]; for (int i = 0; i < 4; i++){ switch(i){ case 0: if ((i*(paso+1) <= Xn) && (Xn <= i*(paso+1) + paso)){ normal[0] = 0.0; normal[1] = 1.0; normal[2] = 0.0; return normal; } break; case 1: if ((i*(paso+1) <= Xn) && (Xn <= i*(paso+1) + paso)){ normal[0] = -1.0; normal[1] = 0.0; normal[2] = 0.0; return normal; } break; case 2: if ((i*(paso+1) <= Xn) && (Xn <= i*(paso+1) + paso)){ normal[0] = 0.0; normal[1] = -1.0; normal[2] = 0.0; return normal; } break; case 3: if ((i*(paso+1) <= Xn) && (Xn <= i*(paso+1) + paso)){ normal[0] = 1.0; normal[1] = 0.0; normal[2] = 0.0; return normal; } break; default: break; } } //NUNCA debería llegar acá return 0; }
void main(void){ float dxtex = 1.0 / textureSizeX; float dytex = 1.0 / textureSizeY; vec2 st = gl_TexCoord[0].st; // access center pixel and 4 surrounded pixel vec3 center = getNormal(st).rgb; vec3 left = getNormal(st + vec2(dxtex, 0.0)).rgb; vec3 right = getNormal(st + vec2(-dxtex, 0.0)).rgb; vec3 up = getNormal(st + vec2(0.0, -dytex)).rgb; vec3 down = getNormal(st + vec2(0.0, dytex)).rgb; // discrete Laplace operator vec3 laplace = abs(-4.0*center + left + right + up + down); // if one rgb-component of convolution result is over threshold => edge vec4 line = texture2D(normalImage, st); if(laplace.r > normalEdgeThreshold || laplace.g > normalEdgeThreshold || laplace.b > normalEdgeThreshold){ line = vec4(0.0, 0.0, 0.0, 1.0); // => color the pixel green } else { line = vec4(1.0, 1.0, 1.0, 1.0); // black } //end Line; //start Phong //vec3 lightPosition = vec3(100.0, 100.0, 50.0); vec3 lightPosition = gl_LightSource[0].position.xyz; vec3 L = normalize(lightPosition - v); vec3 E = normalize(-v); vec3 R = normalize(-reflect(L,N)); // ambient term vec4 Iamb = ambient; // diffuse term vec4 Idiff = texture2D( normalImage, gl_TexCoord[0].st) * diffuse; //vec4 Idiff = vec4(1.0, 1.0, 1.0, 1.0) * diffuse; Idiff *= max(dot(N,L), 0.0); Idiff = clamp(Idiff, 0.0, 1.0); // specular term vec4 Ispec = specular; Ispec *= pow(max(dot(R,E),0.0), shinyness); Ispec = clamp(Ispec, 0.0, 1.0); vec4 color = Iamb + Idiff; if ( bSpecular == 1 ) color += Ispec; // store previous alpha value float alpha = color.a; // quantize process: multiply by factor, round and divde by factor color = floor(0.5 + (qLevel * color)) / qLevel; // set fragment/pixel color color.a = alpha; gl_FragColor = color * line; }
bool tSegmento::colisionVsPelota(tPelota* pelota, double& tIn, PuntoV2F*& normal) { PuntoV2F* posicion = new PuntoV2F(pelota->getPosicion()); PuntoV2F* sentido = new PuntoV2F(pelota->getSentido()); sentido->sumar(posicion); PuntoV2F *puntocolision = new PuntoV2F(); double tinpre; double modulototal; puntocolision = inteseccionSegmento(posicion,sentido,getVertice(0),getVertice(1)); if(puntocolision!=NULL){ tinpre=(sqrt(pow((puntocolision->getX() - posicion->getX()),2)+pow((puntocolision->getY() - posicion->getY()),2))) ; modulototal =(sqrt(pow((sentido->getX() - posicion->getX()),2)+pow((sentido->getY() - posicion->getY()),2))) ; tIn = tinpre / modulototal; normal = getNormal(0); normal = new PuntoV2F(normal); } if(puntocolision==NULL){ puntocolision = inteseccionSegmento(posicion,sentido,getVertice(1),getVertice(0)); if(puntocolision!=NULL){ tinpre=(sqrt(pow((puntocolision->getX() - posicion->getX()),2)+pow((puntocolision->getY() - posicion->getY()),2))) ; modulototal =(sqrt(pow((sentido->getX() - posicion->getX()),2)+pow((sentido->getY() - posicion->getY()),2))) ; tIn = tinpre / modulototal; normal = getNormal(1); normal = new PuntoV2F(normal); } } return (puntocolision != NULL); }
const CXXCoord CXXCircle::vdwPlaneIntersect(const CXXCoord &A, const CXXCoord &B) const{ CXXCoord AO = getCentreOfVdWCircle() - A; CXXCoord AB = B - A; double frac = (AO*getNormal()) / (AB*getNormal()); AB *= frac; return A + AB; }
void testGetAngleBetweenPlanes() { cout << "Testing GetAngleBetweenPlanes" <<endl; srand(time(NULL)); int numberOfTests = 100; for (int i = 0; i < numberOfTests; i++) { Plane a, b; a.atom1.x = ((double) rand() / RAND_MAX) * 10; a.atom1.y = ((double) rand() / RAND_MAX) * 10; a.atom1.z = ((double) rand() / RAND_MAX) * 10; a.atom2.x = ((double) rand() / RAND_MAX) * 10; a.atom2.y = ((double) rand() / RAND_MAX) * 10; a.atom2.z = ((double) rand() / RAND_MAX) * 10; a.atom3.x = ((double) rand() / RAND_MAX) * 10; a.atom3.y = ((double) rand() / RAND_MAX) * 10; a.atom3.z = ((double) rand() / RAND_MAX) * 10; b.atom1.x = ((double) rand() / RAND_MAX) * 10; b.atom1.y = ((double) rand() / RAND_MAX) * 10; b.atom1.z = ((double) rand() / RAND_MAX) * 10; b.atom2.x = ((double) rand() / RAND_MAX) * 10; b.atom2.y = ((double) rand() / RAND_MAX) * 10; b.atom2.z = ((double) rand() / RAND_MAX) * 10; b.atom3.x = ((double) rand() / RAND_MAX) * 10; b.atom3.y = ((double) rand() / RAND_MAX) * 10; b.atom3.z = ((double) rand() / RAND_MAX) * 10; Point aNormal = getNormal(a); Point bNormal = getNormal(b); double numerator = aNormal.x * bNormal.x + aNormal.y * bNormal.y + aNormal.z * bNormal.z; double aMag = sqrt(aNormal.x * aNormal.x + aNormal.y * aNormal.y + aNormal.z * aNormal.z); double bMag = sqrt(bNormal.x * bNormal.x + bNormal.y * bNormal.y + bNormal.z * bNormal.z); double denominator = aMag * bMag; double thetaR = acos(numerator / denominator); double expectedTheta = radiansToDegrees(thetaR); double testTheta = getAngle(a, b); if (!((testTheta - expectedTheta) / expectedTheta < PRECISION)) { printf("Test GetAngleBetweenPlanes #%d failed. testTheta = %f | expectedTheta = %f.\n", i, testTheta, expectedTheta); } assert((testTheta - expectedTheta) / expectedTheta < PRECISION); } cout << "Testing GetAngleBetweenPlanes Completed\n" <<endl; }
const CXXCoord CXXCircle::accPlaneIntersect(const CXXCoord &A, const CXXCoord &B) const{ CXXCoord AO(getCentreOfCircle() - A); CXXCoord AB(B - A); double frac = (AO*getNormal()) / (AB*getNormal()); AB *= frac; CXXCoord result(A + AB); CXXCoord OC = result - getCentreOfCircle(); double radialLength = OC.get3DLength(); OC *= getRadiusOfCircle()/radialLength; return getCentreOfCircle() + OC; }
// check if one object has collided with another object // returns true if the two objects have collided bool checkCollision(movableObject& obj1, movableObject& obj2) { vector2 diff = vectorSubtract(obj1.position, obj2.position); float mag = getMagnitude(diff); if(mag > 0 && mag < obj1.height){ // collision obj1.speed = multiplyScalar( getNormal(diff), 5); obj2.speed = multiplyScalar( getNormal(diff), -5); return true; } return false; }
void HeightTable::setBaseDistance(QVector3D base, qreal degree) { this->base = base; // find a coordinate that exist on the laser plane. Simple geometry... qreal y = base.z() / tan(degree); // we can always assume one edge along the base going across. normalTop = getNormal(base, QVector3D(base.z(), 0, base.z()), QVector3D(0, y, 0)); normalBottom = getNormal(base, QVector3D(base.z(), 0, base.z()), QVector3D( 0, -y, 0)); }
float SimpleHeightmap::height(float x, float y, vec3& n) const { float fx = x / m_resolution; float fy = y / m_resolution; int ix = (int) floor(fx); fx -= ix; int iy = (int) floor(fy); fy -= iy; int side = fx + fy < 1? 0: 1; float v = side? 1-fy: fx; float w = side? 1-fx: fy; float u = 1 - v - w; n = u * getNormal(ix+side, iy+side) + v * getNormal(ix+1, iy) + w * getNormal(ix, iy+1); return u * getHeight(ix+side, iy+side) + v * getHeight(ix+1, iy) + w * getHeight(ix, iy+1); }
// Descr: evident TEST(GeometryTest, GetAngleBetweenPlanes) { srand(time(NULL)); int numberOfTests = 100; for (int i = 0; i < numberOfTests; i++) { Plane a, b; a.atom1.x = ((double) rand() / RAND_MAX) * 10; a.atom1.y = ((double) rand() / RAND_MAX) * 10; a.atom1.z = ((double) rand() / RAND_MAX) * 10; a.atom2.x = ((double) rand() / RAND_MAX) * 10; a.atom2.y = ((double) rand() / RAND_MAX) * 10; a.atom2.z = ((double) rand() / RAND_MAX) * 10; a.atom3.x = ((double) rand() / RAND_MAX) * 10; a.atom3.y = ((double) rand() / RAND_MAX) * 10; a.atom3.z = ((double) rand() / RAND_MAX) * 10; b.atom1.x = ((double) rand() / RAND_MAX) * 10; b.atom1.y = ((double) rand() / RAND_MAX) * 10; b.atom1.z = ((double) rand() / RAND_MAX) * 10; b.atom2.x = ((double) rand() / RAND_MAX) * 10; b.atom2.y = ((double) rand() / RAND_MAX) * 10; b.atom2.z = ((double) rand() / RAND_MAX) * 10; b.atom3.x = ((double) rand() / RAND_MAX) * 10; b.atom3.y = ((double) rand() / RAND_MAX) * 10; b.atom3.z = ((double) rand() / RAND_MAX) * 10; Point aNormal = getNormal(a); Point bNormal = getNormal(b); double numerator = aNormal.x * bNormal.x + aNormal.y * bNormal.y + aNormal.z * bNormal.z; double aMag = sqrt(aNormal.x * aNormal.x + aNormal.y * aNormal.y + aNormal.z * aNormal.z); double bMag = sqrt(bNormal.x * bNormal.x + bNormal.y * bNormal.y + bNormal.z * bNormal.z); double denominator = aMag * bMag; double thetaR = acos(numerator / denominator); double expectedTheta = radiansToDegrees(thetaR); double testTheta = getAngle(a, b); ASSERT_LT( (testTheta - expectedTheta) / expectedTheta, PRECISION); } }
void Plane::Initialize(D3DXMATRIX world, float height, float width, ID3D11Device* g_Device, ID3D11DeviceContext* g_DeviceContext, IShader* shader ) { D3DXVECTOR3 n1=getNormal(D3DXVECTOR3(0,-1,0), D3DXVECTOR3(0,0,0), D3DXVECTOR3(1,0,0)); D3DXVECTOR3 n2=getNormal(D3DXVECTOR3(0,-1,0), D3DXVECTOR3(1,0,0), D3DXVECTOR3(1,-1,0)); VERTEX::VertexPNC3 mesh[] = { {D3DXVECTOR4(-width, -height,0, 1 ), n1, D3DXVECTOR4(200,200, 200 ,0)}, {D3DXVECTOR4(-width,height,0,1) , n1, D3DXVECTOR4(200,200, 200 ,0)}, {D3DXVECTOR4(width,height,0,1) , n1, D3DXVECTOR4(200,200, 200 ,0)}, {D3DXVECTOR4(-width, -height,0, 1) , n2, D3DXVECTOR4(200,200, 200 ,0)}, {D3DXVECTOR4(width,height,0, 1) , n2, D3DXVECTOR4(200,200, 200 ,0)}, {D3DXVECTOR4(width,-height, 0,1) , n2, D3DXVECTOR4(200,200, 200 ,0)} }; BaseBuffer::BUFFER_INIT_DESC bufferDesc; bufferDesc.dc = D3DShell::self()->getDeviceContext(); bufferDesc.device = D3DShell::self()->getDevice(); bufferDesc.elementSize = sizeof(VERTEX::VertexPNC3); bufferDesc.data = mesh; bufferDesc.nrOfElements = 6; bufferDesc.type = BUFFER_FLAG::TYPE_VERTEX_BUFFER; bufferDesc.usage = BUFFER_FLAG::USAGE_DEFAULT; this->m_VertexBuffer = new BaseBuffer(); if(FAILED(m_VertexBuffer->Initialize(bufferDesc))) { MessageBox(0, L"Could not initialize planeVertexBuffer! Plane.cpp - Initialize", L"Error", MB_OK); } int index []= {0,1,2,3,4,5}; bufferDesc.dc = D3DShell::self()->getDeviceContext(); bufferDesc.device = D3DShell::self()->getDevice(); bufferDesc.elementSize = sizeof(int); bufferDesc.data = index; bufferDesc.nrOfElements = 6; bufferDesc.type = BUFFER_FLAG::TYPE_INDEX_BUFFER; bufferDesc.usage = BUFFER_FLAG::USAGE_DEFAULT; m_IndexBuffer = new BaseBuffer(); if(FAILED(m_IndexBuffer->Initialize(bufferDesc))) { MessageBox(0, L"Could not initialize planeIndexBuffer! Plane.cpp - Initialize", L"Error", MB_OK); } m_world= world; m_shader = shader; }
bool containsPointInclusive(const Triangle<T>& triangle, const Vector<U, 2>& point, double epsilon) { auto a = getNormal(triangle.point(1) - triangle.point(0)) * (point - triangle.point(0)); if (less<T>(a, 0, epsilon)) return false; auto b = getNormal(triangle.point(2) - triangle.point(1)) * (point - triangle.point(1)); if (less<T>(b, 0, epsilon)) return false; auto c = getNormal(triangle.point(0) - triangle.point(2)) * (point - triangle.point(2)); return greaterOrEqual<T>(c, 0, epsilon); }
ofVec3f Terrain::getNormalforPixelPos(int x, int y) { ofVec3f normal; normal.set(0,0,0); normal+= getNormal( ofVec3f(mainPixelSize *x,getHeightForPixelPos(x,y),mainPixelSize* y) , ofVec3f(mainPixelSize *(x+1),getHeightForPixelPos(x+1,y),mainPixelSize* y) , ofVec3f(mainPixelSize *x,getHeightForPixelPos(x,y+1),mainPixelSize* (y+1)) ); normal+= getNormal( ofVec3f(mainPixelSize *x,getHeightForPixelPos(x,y),mainPixelSize* y) , ofVec3f(mainPixelSize *(x),getHeightForPixelPos(x,y-1),mainPixelSize* (y-1)) , ofVec3f(mainPixelSize *(x+1),getHeightForPixelPos((x+1),y+1),mainPixelSize* (y+1)) ); normal+= getNormal( ofVec3f(mainPixelSize *x,getHeightForPixelPos(x,y),mainPixelSize* y) , ofVec3f(mainPixelSize *(x-1),getHeightForPixelPos(x-1,y),mainPixelSize* (y)) , ofVec3f(mainPixelSize *(x-1),getHeightForPixelPos((x-1),y-1),mainPixelSize* (y-1)) ); normal+= getNormal( ofVec3f(mainPixelSize *x,getHeightForPixelPos(x,y),mainPixelSize* y) , ofVec3f(mainPixelSize *(x),getHeightForPixelPos(x,y+1),mainPixelSize* (y+1)) , ofVec3f(mainPixelSize *(x-1),getHeightForPixelPos((x-1),y),mainPixelSize* (y)) ); normal.normalize(); return normal; }
bool Tri::intersect(Ray& _ray, float* thit, Intersection* in) { Ray ray = _ray.transform(inverseTransform); if (!intersectP(ray)) { return false; } if (*thit < t) { return false; } p = ray.getPos() + ray.getDir() * t; w = p - a; vCrossW = glm::cross(v, w); uCrossW = glm::cross(u, w); if (glm::dot(vCrossW, vCrossU) < 0) { return false; } if (glm::dot(uCrossW, uCrossV) < 0) { return false; } beta = glm::length(vCrossW)/denom; gamma = glm::length(uCrossW)/denom; alpha = 1 - beta - gamma; if (!(beta <= 1 && gamma <= 1 && beta + gamma <= 1)) { return false; } *thit = t; in->localGeo.pos = mat4TimesVec3(getTransform(), (ray.getPos() + t * ray.getDir()), 1.0); in->localGeo.normal = getNormal(); //shift position slightly towards normal (epsilon shift) in->localGeo.pos = in->localGeo.pos + in->localGeo.normal * epsilon; in->primitive = this; return true; }
bool CXXCircle::smallabBracketsC(const CXXCircleNode &nodea, const CXXCircleNode &nodeb, const CXXCircleNode &nodec) const { double aCrossBdotN = (nodea.getUnitRadius()^nodeb.getUnitRadius())*getNormal(); double aCrossCdotN = (nodea.getUnitRadius()^nodec.getUnitRadius())*getNormal(); double bCrossCdotN = (nodeb.getUnitRadius()^nodec.getUnitRadius())*getNormal(); bool liesBetween = false; if (aCrossBdotN>0.){ liesBetween = aCrossCdotN>0. && bCrossCdotN<0.; } else { liesBetween = aCrossCdotN<0. && bCrossCdotN>0.; } return liesBetween; }
Real Polygon3::area() const { array_size_t n = vertices.size(); if (n < 3) return 0; if (n == 3) return 0.5f * (vertices[2].position - vertices[1].position).cross(vertices[0].position - vertices[1].position).length(); static Real const EPSILON = 1e-10f; Vector3 normal = getNormal(); if (normal.squaredLength() < EPSILON) return 0; Real a = 0; Vector3 const & last = vertices[n - 1].position; Vector3 prev = last; for (array_size_t p = 0; p < n; ++p) { Vector3 const & v = vertices[p].position; a += (prev.cross(v)).dot(normal); prev = v; } return std::fabs(0.5f * a); }
bool CheckCollision::checkForCollisions(BouncingSquare* shapeA,BouncingSquare* shapeB) { //if(shape.vec4 != NULL) //{ for(int i = 0; i<4; i++) { if(i == 0) { normVec1 = shapeA->vec1; normVec2 = shapeA->vec2; } if(i == 1) { normVec1 = shapeA->vec2; normVec2 = shapeA->vec3; } else if(i == 2) { normVec1 = shapeB->vec1; normVec2 = shapeB->vec2; } else if(i == 3) { normVec1 = shapeB->vec2; normVec2 = shapeB->vec3; } normalVec = getNormal(normVec1,normVec2); projectVecA1 = getProjVec(shapeA->vec1); projectVecA2 = getProjVec(shapeA->vec2); projectVecA3 = getProjVec(shapeA->vec3); projectVecA4 = getProjVec(shapeA->vec4); projectVecA1 += getProjVec(shapeA->centreVec); projectVecA2 += getProjVec(shapeA->centreVec); projectVecA3 += getProjVec(shapeA->centreVec); projectVecA4 += getProjVec(shapeA->centreVec); projectVecB1 = getProjVec(shapeB->vec1); projectVecB2 = getProjVec(shapeB->vec2); projectVecB3 = getProjVec(shapeB->vec3); projectVecB4 = getProjVec(shapeB->vec4); projectVecB1 += getProjVec(shapeB->centreVec); projectVecB2 += getProjVec(shapeB->centreVec); projectVecB3 += getProjVec(shapeB->centreVec); projectVecB4 += getProjVec(shapeB->centreVec); getMinMax(); if (maxVecA < minVecB || maxVecB < minVecA) { return false; } } return true; //} }
// Descr: evident // Implementation details: See gtest/samples for GTest syntax and usage TEST(GeometryTest, GetNormal) { Atom atom1, atom2, atom3; atom1.x = 3.2; atom1.y = 4.6; atom1.z = 0.2; atom2.x = 7.1; atom2.y = 1.4; atom2.z = 10.0; atom3.x = 0.0; atom3.y = 0.0; atom3.z = 0.0; Plane testPlane = createPlane(atom1, atom2, atom3); Point expected = createPoint(45.72, -30.58, -28.18); Point test = getNormal(testPlane); test.x = ((double) ((int) (test.x * 100))) / 100.0; test.y = ((double) ((int) (test.y * 100))) / 100.0; test.z = ((double) ((int) (test.z * 100))) / 100.0; EXPECT_NEAR(expected.x, test.x, .01); EXPECT_NEAR(expected.y, test.y, .01); EXPECT_NEAR(expected.z, test.z, .01); }
void DVRSlice::output(void) { for(UInt32 i = 0; i < numberOfVertices; i++) { std::cerr << "Vertex Number " << i << ": " << vertexData[i][0] << ", " << vertexData[i][1] << ", " << vertexData[i][2] << std::endl; } Vec3f n = getNormal(); std::cerr << "Normal: " << n[0] << ", " << n[1] << ", " << n[2] << std::endl; }
void main( void ){ vec2 q = (gl_FragCoord.xy/resolution.xy) - vec2(0.5,0.5); vec3 ray0 = vec3( 0.0, 0.0, -50.0 ); vec3 hRay = vec3( q.x, q.y, 10.0 ); hRay = normalize( hRay ); float t = rayMarch( ray0, hRay ); vec3 hitpos = ray0 + t*hRay; float val0 = evalFunc ( hitpos ); vec3 normal = getNormal( hitpos, val0 ); normal = normalize( normal ); if( val0<0.0 ) normal*=-1.0; float c_diff = dot ( light_dir, normal ); //float c_spec = dot ( light_dir-hRay, normal ); c_spec*=c_spec; c_spec*=c_spec; c_spec*=c_spec; //float c = c_diff + c_spec; float c = 0.1 + max( 0.0, c_diff ); if( t < (tmax-dtmax) ){ if( val0 > 0.0 ){ gl_FragColor = vec4( 0.0, 0.5*c, c, 1.0 ); }else{ gl_FragColor = vec4( c, 0.5*c, 0.0, 1.0 ); }; }else{ gl_FragColor = vec4( 0.0, 0.0, 0.0, 1.0 ); } }
void Mesh::invertNormals(){ // set all normals to zero for (int i=0; i<numVerts; i++){ Vector3 n = getNormal(i); setNormal(i,-n.x,-n.y,-n.z); } }
void Mesh::normalise(){ for (int i=0; i<numVerts; i++){ Vector3 v = getNormal(i); v.normalise(); setNormal(i,v); } }
void DynFace::draw(void) { glBegin(GL_POLYGON); glNormal3dv(&(*getNormalOS())[0]); for (unsigned int i = 0; i < vertices.size(); i++) glVertex3dv(&(*getVertexOS(i))[0]); glEnd(); #ifdef DRAW_FACE_NORMALS glDisable(GL_LIGHTING); glColor3f(1.0, 0.0, 0.0); hduVector3Dd vertAccum(3,0); for (i = 0; i < vertices.size(); i++) vertAccum += *getVertexOS(i); hduVector3Dd normalTail = vertAccum / (double)vertices.size(); hduVector3Dd normalHead = normalTail + *getNormal(); glBegin(GL_LINES); glVertex3dv(&normalTail[0]); glVertex3dv(&normalHead[0]); glEnd(); glEnable(GL_LIGHTING); #endif }
void RGBDSensor::computePointCurrentPointCloud(PointCloudf& pc, const mat4f& transform /*= mat4f::identity()*/) const { if (!(getColorWidth() == getDepthWidth() && getColorHeight() == getDepthHeight())) throw MLIB_EXCEPTION("invalid dimensions"); for (unsigned int i = 0; i < getDepthWidth()*getDepthHeight(); i++) { unsigned int x = i % getDepthWidth(); unsigned int y = i / getDepthWidth(); vec3f p = depthToSkeleton(x,y); if (p.x != -std::numeric_limits<float>::infinity() && p.x != 0.0f) { vec3f n = getNormal(x,y); if (n.x != -FLT_MAX) { pc.m_points.push_back(p); pc.m_normals.push_back(n); vec4uc c = m_colorRGBX[i]; pc.m_colors.push_back(vec4f(c.z/255.0f, c.y/255.0f, c.x/255.0f, 1.0f)); //there's a swap... dunno why really } } } for (auto& p : pc.m_points) { p = transform * p; } mat4f invTranspose = transform.getInverse().getTranspose(); for (auto& n : pc.m_normals) { n = invTranspose * n; n.normalize(); } }
bool isInHalfSpace(const fwPlane& _plane, const fwVec3d& _point) { SLM_TRACE_FUNC(); fwVec3d normalVec = getNormal(_plane); fwVec3d pos = normalVec * getDistance(_plane); return ((float)(dot(normalVec, _point-pos)) >= 0.0F ? true : false); }
bool Collision::checkCollides(Object* _first, Object* _second, Game* _game) { sf::Vector2f depth(0, 0); sf::Vector2f normal(0, 0); sf::IntRect first = _first->getAABB(); sf::IntRect second = _second->getAABB(); sf::Vector2i firstPos((int)_first->getX() + first.width, (int)_first->getY() + first.height); sf::Vector2i secondPos((int)_second->getX() + second.width, (int)_second->getY() + second.height); int dx = firstPos.x - secondPos.x; int dy = firstPos.y - secondPos.y; int adx = abs(firstPos.x - secondPos.x); int ady = abs(firstPos.y - secondPos.y); int sw = (first.width + second.width); int sh = (first.height + second.height); if((adx < sw) && (ady < sh)) { float invDist = 1.0f / (float)sqrt(dx * dx + dy * dy); normal = getNormal(sf::Vector2f(dx * invDist, dy * invDist)); depth = sf::Vector2f((float)abs(adx - sw), (float)abs(ady - sh)); _first->collisionCallback(depth, normal, _second, _game); _second->collisionCallback(depth, normal, _first, _game); _first->collide(); _second->collide(); return true; } return false; }
// main void main(void) { vec2 uv = gl_FragCoord.xy / iResolution.xy; uv = uv * 2.0 - 1.0; uv.x *= iResolution.x / iResolution.y; float time = iGlobalTime * 0.3; // ray vec3 ang = vec3(sin(time*3.0)*0.1,sin(time)*0.2+0.4,time); if(iMouse.z > 0.0) ang = vec3(0.0,clamp(2.0-iMouse.y*0.01,-0.3,PI),iMouse.x*0.01); mat4 rot = fromEuler(ang); vec3 ori = vec3(0.0,0.2,time*1.0); ori.y += abs(map_detailed(-ori)); vec3 dir = normalize(vec3(uv.xy,-1.0)); dir = rotate(normalize(dir),rot); // tracing vec3 p; float dens = hftracing(ori,dir,p); vec3 dist = p - ori; vec3 n = getNormal(p, dot(dist,dist)*EPSILON_NRM); // color vec3 color = sea_color(p,n,dir,dist); vec3 light = normalize(vec3(0.0,1.0,0.8)); color += vec3(diffuse(n,light,80.0) * SEA_WATER_COLOR) * 0.12; color += vec3(specular(n,light,dir,60.0)); // post color = mix(sky_color(dir),color, clamp(1.0-length(dist)/100.0,0.0,1.0)); color = pow(color,vec3(0.75)); gl_FragColor = vec4(color,1.0); }
/** * Computes the color corresponding to the ray intersection point (if any) * vec3 {x,y,z} ro ray origin * vec3 {u,v,w} rd ray direction */ vec4 computeColor(vec3 ro, vec3 rd) { int i; int k = 0; float t0; float tk0; vec3 ro2; vec4 color; raymarch(ro, rd, i, t0); vec3 p; // Surface point vec3 normal; // Surface normal float t; // Distance traveled by ray from eye if(i < g_rmSteps && t0 >= g_zNear && t0 <= g_zFar) { // Raymarching hit a surface t = t0; p = ro + rd * t0; int matIndex = int(floor(mod(min(min(p.x, p.y), p.z), 4.0))) + 1; for(int n = 0; n < maxMaterial; n++) { if(n == int(matIndex)) { color = u_materials[n]; // white box break; } } normal = getNormal(p); color = colorPass(color, t, p, normal); } else { return u_sky; } return color; }