IFloatBuffer* NormalsUtils::createTriangleSmoothNormals(const IFloatBuffer* vertices,
                                                        const IShortBuffer* indices) {

  const int verticesSize = vertices->size();
  IFloatBuffer* normals = IFactory::instance()->createFloatBuffer(verticesSize);
  for (int i = 0; i < verticesSize; i++) {
    normals->rawPut(i, 0);
  }

  const int indicesSize = indices->size();
  for (int i = 0; i < indicesSize; i += 3) {
    const short index0 = indices->get(i);
    const short index1 = indices->get(i + 1);
    const short index2 = indices->get(i + 2);

    const Vector3F normal = calculateNormal(vertices, index0, index1, index2);
    addNormal(normals, index0, normal);
    addNormal(normals, index1, normal);
    addNormal(normals, index2, normal);
  }

  for (int i = 0; i < verticesSize; i += 3) {
    const float x = normals->get(i);
    const float y = normals->get(i + 1);
    const float z = normals->get(i + 2);

    const Vector3F normal = Vector3F(x, y, z).normalized();
    normals->rawPut(i    , normal._x);
    normals->rawPut(i + 1, normal._y);
    normals->rawPut(i + 2, normal._z);
  }

  return normals;
}
Exemple #2
0
/*!
 * \brief Adds triangles for the outer hull
 * \param radius Radius of the robot
 * \param height Height of the robot
 * \param num Number of segments
 * \param angle Angle of the hull start
 * \param angleStep Step size in rad
 */
void Mesh::addRobotCover(float radius, float height, uint num, float angle, float angleStep)
{
    QVector<uint>& group = m_groups["cover"];
    for (uint i = 0; i <= num; i++) {
        // upper vertex row
        addNormal(cos(angle), sin(angle), 0.0f);
        addTexCoord((angle - M_PI_2) / M_PI / 2.0f, 1.0f);
        addVertex(radius * cos(angle), radius * sin(angle),  height / 2.0f);
        m_hull.append(QVector3D(radius * cos(angle), radius * sin(angle),  height / 2.0f));

        // bottom vertex row
        addNormal(cos(angle), sin(angle), 0.0f);
        addTexCoord((angle - M_PI_2) / M_PI / 2.0f, 0.0f);
        addVertex(radius * cos(angle), radius * sin(angle), -height / 2.0f);
        m_hull.append(QVector3D(radius * cos(angle), radius * sin(angle), -height / 2.0f));

        group.append(i * 2 + 0);
        group.append(i * 2 + 1);

        angle += angleStep;
    }

    for (uint i = 0; i < num; i++) {
        addTriangle(i * 2 + 0, i * 2 + 1, i * 2 + 2);
        addTriangle(i * 2 + 2, i * 2 + 1, i * 2 + 3);
    }
}
Exemple #3
0
/*!
 * \brief Adds front plane
 * \param radius Radius of the robot
 * \param height Height of the robot
 * \param angleStart Start angle in rad
 * \param angleStop Stop angle in rad
 */
void Mesh::addRobotFront(float radius, float height, float angleStart, float angleStop)
{
    uint firstIndex = m_vertices.count() / 3;

    addNormal(cos(angleStop), sin(angleStop), 0.0f);
    addTexCoord(0.0f, 1.0f);
    addVertex(radius * cos(angleStop), radius * sin(angleStop),  height / 2.0f);

    addNormal(cos(angleStop), sin(angleStop), 0.0f);
    addTexCoord(0.0f, 0.0f);
    addVertex(radius * cos(angleStop), radius * sin(angleStop), -height / 2.0f);

    addNormal(cos(angleStart), sin(angleStart), 0.0f);
    addTexCoord(1.0f, 1.0f);
    addVertex(radius * cos(angleStart), radius * sin(angleStart),  height / 2.0f);

    addNormal(cos(angleStart), sin(angleStart), 0.0f);
    addTexCoord(1.0f, 0.0f);
    addVertex(radius * cos(angleStart), radius * sin(angleStart), -height / 2.0f);

    addTriangle(firstIndex + 0, firstIndex + 1, firstIndex + 2);
    addTriangle(firstIndex + 2, firstIndex + 1, firstIndex + 3);

    QVector<uint>& group = m_groups["front"];
    group.append(firstIndex + 0);
    group.append(firstIndex + 1);
    group.append(firstIndex + 2);
    group.append(firstIndex + 3);
}
Exemple #4
0
/*!
 * \brief Adds triangles for top/bottom plates
 * \param radius Radius of the robot
 * \param height Height of the robot
 * \param num Number of segments
 * \param angle Angle of the front side
 * \param angleStep Step size in rad
 * \param top If \c true this method creates the top plate. Otherwise the bottom plate is created
 */
void Mesh::addRobotPlate(float radius, float height, uint num, float angle, float angleStep, bool top)
{
    QVector<uint>& group = m_groups[top ? "top" : "bottom"];

    const float normal = top ? 1.0f : -1.0f;
    const uint firstIndex = m_vertices.count() / 3;

    addNormal(0.0f, 0.0f, normal);
    addTexCoord(0.5f, 0.5f);
    addVertex(0.0f, 0.0f, normal * height / 2.0f);
    group.append(firstIndex);

    for (uint i = 0; i <= num; i++) {
        addNormal(0.0f, 0.0f, normal);
        addTexCoord(cos(angle - M_PI_2) / 2.0f + 0.5f, sin(angle - M_PI_2) / 2.0f + 0.5f);
        addVertex(radius * cos(angle), radius * sin(angle), normal * height / 2.0f);

        group.append(firstIndex + i + 1);
        angle += normal * angleStep;
    }
    group.append(firstIndex + 1);

    for (uint i = 0; i < num; i++)
        addTriangle(firstIndex, firstIndex + i + 1, firstIndex + i + 2);
    addTriangle(firstIndex, firstIndex + num + 1, firstIndex + 1);
}
Exemple #5
0
void ofxMesh::addFace(ofVec3f a, ofVec3f b, ofVec3f c) {
    ofVec3f normal = ((c - a).cross(b - a)).normalize();
    
	addNormal(normal);
	addVertex(a);
	addNormal(normal);
	addVertex(b);
	addNormal(normal);
	addVertex(c);
    
    addIndex(getNumIndices());
    addIndex(getNumIndices());
    addIndex(getNumIndices());    
}
void QtGLComponent::addTriangle(QVector3D *verts, QVector3D *norms, QVector2D *texCoords)
{
    // Search for match - triangle consists of three verts
    for(GLuint iVertex = 0; iVertex < 3; iVertex++){
        GLuint iMatch = 0;

        for(iMatch = 0; iMatch < (GLuint)m_vertices.size(); iMatch++) {
            if (!qFuzzyCompare(m_vertices.at(iMatch), verts[iVertex]))
                continue;
            if (!qFuzzyCompare(m_normals.at(iMatch), norms[iVertex]))
                continue;

            if (!qFuzzyCompare(m_textureCoords.at(iMatch), texCoords[iVertex]))
                continue;

            // Then add the index only
            addIndex(iMatch);
                break;
        }

        // No match for this vertex, add to end of list
        if((int)iMatch == m_vertices.size()) {
            addIndex(iMatch);
            addVertex(verts[iVertex]);
            addNormal(norms[iVertex]);
            addTextureCoord(texCoords[iVertex]);
        }
    }
}
	void Mesh::calcNormals(){
		// set all normals to zero
		for (int i=0; i<numVerts; i++){
			setNormal(i,0,0,0);
		}

		// add normal for all tris
		for (int i=0; i<numTris; i++){
			 Vector3 v1 = getVertex(triIndices[i*3]); 
			 Vector3 v2 = getVertex(triIndices[i*3+1]); 
			 Vector3 v3 = getVertex(triIndices[i*3+2]);
			 Vector3 normal = (v2-v1).cross(v3-v2);
			 addNormal(triIndices[i*3],normal);			 
			 addNormal(triIndices[i*3+1],normal);
			 addNormal(triIndices[i*3+2],normal);
		}

		// add normal for all quads
		for (int i=0; i<numQuads; i++){
			 Vector3 v1 = getVertex(quadIndices[i*4]); 
			 Vector3 v2 = getVertex(quadIndices[i*4+1]); 
			 Vector3 v3 = getVertex(quadIndices[i*4+2]);
			 Vector3 normal = (v2-v1).cross(v3-v2);
			 addNormal(quadIndices[i*4],normal);			 
			 addNormal(quadIndices[i*4+1],normal);
			 addNormal(quadIndices[i*4+2],normal);
			 addNormal(quadIndices[i*4+3],normal);
		}

		// normalise normals
		normalise();
	}
Exemple #8
0
void Mesh::calculateNormals()
{
    for (auto& t : triangles) {
        vec3 ab = vertices[t.b] - vertices[t.a];
        vec3 ac = vertices[t.c] - vertices[t.a];
        unsigned int n = addNormal(cross(ab, ac));
        t.na = t.nb = t.nc = n;
    }
}
IFloatBuffer* NormalsUtils::createTriangleStripSmoothNormals(const IFloatBuffer* vertices,
                                                             const IShortBuffer* indices) {
  const int verticesSize = vertices->size();
  IFloatBuffer* normals = IFactory::instance()->createFloatBuffer(verticesSize);
  for (int i = 0; i < verticesSize; i++) {
    normals->rawPut(i, 0);
  }

  short index0 = indices->get(0);
  short index1 = indices->get(1);

  const int indicesSize = indices->size();
  for (int i = 2; i < indicesSize; i++) {
    const short index2 = indices->get(i);

    const Vector3F normal = (i % 2 == 0)
    /*                          */ ? calculateNormal(vertices, index0, index1, index2)
    /*                          */ : calculateNormal(vertices, index0, index2, index1);
    addNormal(normals, index0, normal);
    addNormal(normals, index1, normal);
    addNormal(normals, index2, normal);

    index0 = index1;
    index1 = index2;
  }

  //http://stackoverflow.com/questions/3485034/convert-triangle-strips-to-triangles

  for (int i = 0; i < verticesSize; i += 3) {
    const float x = normals->get(i);
    const float y = normals->get(i + 1);
    const float z = normals->get(i + 2);

    const Vector3F normal = Vector3F(x, y, z).normalized();
    normals->rawPut(i    , normal._x);
    normals->rawPut(i + 1, normal._y);
    normals->rawPut(i + 2, normal._z);
  }
  
  return normals;
}
Exemple #10
0
void ofxMesh::buildNormals() {
	vector<ofVec3f>& vertices = getVertices();
	for(int i = 0; i < getNumVertices(); i += 3) {
		ofVec3f normal = getNormal(
                                   getVertices()[i+0],
                                   getVertices()[i+1],
                                   getVertices()[i+2]);
		for(int j = 0; j < 3; j++) {
			addNormal(normal);
		}
	}
}
Exemple #11
0
Slit::Slit(QString name, qreal x, qreal y, qreal angle, qreal radius, qreal slitRadius, OpticalSystem * opticalSystem, QGraphicsItem * parent) :
    Reflector(opticalSystem, parent),
    m_radius(radius),
    m_slitRadius(slitRadius)
{
    prepareGeometryChange();
    addLabel();
    addNormal();
    setName(name);
    setX(x);
    setY(y);
    setRotation(angle);
    build();
}
Exemple #12
0
void updateFile(ObjFile* file, const char* line)
{
    // counts the number of groups, objectes, faces, positions, normals, 
    // texcoords 

    // TODO: trim [line]

    if (line[0] == 'v' && line[1] == ' ')
    {
        addPosition(file, line);
        return;
    }

    if (line[0] == 'v' && line[1] == 'n' && line[2] == ' ')
    {
        addNormal(file, line);
        return;
    }

    if (line[0] == 'v' && line[1] == 't' && line[2] == ' ')
    {
        addTexCoord(file, line);
        return;
    }

    if (line[0] == 'o' && line[1] == ' ')
    {
        addObject(file, line);
        return;
    }

    if (line[0] == 'g' && line[1] == ' ')
    {
        addGroup(file, line);
        return;
    }

    if (line[0] == 'f' && line[1] == ' ')
    {
        addFace(file, line);
        return;
    }

    if (strstr(line, "usemtl") == line) 
    {
        setCurrentMaterial(file, line);
        return;
    }
}
Exemple #13
0
void Object::collideWall(const Segment &s)
{
	/* collide if the wall normal is pointing toward the object center */
	if (s.faces(pos))
	{
		Point p = s.closestPoint(pos);

		Vector d(p, pos);
		float dlen = d.length();

		d.normalize();
		/* shunt the object out from the wall */
		pos += d * (radius - dlen);

		/* if the object collides with a "floor-like" normal, add it */
		if (s.normal.v < 0)
			addNormal(d);

	}
}
eU32 eEditMesh::addWedge(const eVector3 &pos, const eVector3 &normal, const eVector2 &uv, const eColor &col)
{
    return addWedge(addPosition(pos), addNormal(normal), addProperty(uv, col));
}
void GeometryLoader::normalHandler(const std::vector<std::string>& tokens)
{
	addNormal(FLOAT3{ std::stof(tokens[0]), std::stof(tokens[1]), std::stof(tokens[2]) });
}
void MeshBuilder::addNormal(float x, float y, float z) {
    addNormal(Vector3f(x, y, z));
}
Exemple #17
0
void LoftedPolygon::Setfaces(Polygon* face1, Polygon* face2) {
    if(face1->GetMid()[3] > face2->GetMid()[3]) {
        _face1 = face1;
        _face2 = face2;
    }
    else {
        _face1 = face2;
        _face2 = face1;
    }
    vecGLfloat& _v1 = _face1->GetVertexlist();
    vecGLfloat& _v2 = _face2->GetVertexlist();
    int vc1 = _face1->GetVertexCount();
    int vc2 = _face2->GetVertexCount();
    assert(vc1 == vc2);
    Vector a;
    Vector b;
    Vector c;
    for(int i = 0; i < (vc1-1)*3; i+=3) {
        a[0] = _v1[i+3];
        a[1] = _v1[i+4];
        a[2] = _v1[i+5];

        b[0] = _v1[i];
        b[1] = _v1[i+1];
        b[2] = _v1[i+2];

        c[0] = _v2[i];
        c[1] = _v2[i+1];
        c[2] = _v2[i+2];

        Vector norm = Vector::cross(b-a,c-b);
        norm.Normalize();
        addVertex(_v1[i+3],_v1[i+4],_v1[i+5]);
        addVertex(_v1[i],_v1[i+1],_v1[i+2]);
        addVertex(_v2[i],_v2[i+1],_v2[i+2]);
        addVertex(_v2[i+3],_v2[i+4],_v2[i+5]);
        addNormal(norm[0],norm[1],norm[2]);
        addNormal(norm[0],norm[1],norm[2]);
        addNormal(norm[0],norm[1],norm[2]);
        addNormal(norm[0],norm[1],norm[2]);
    }
    a[0] = _v1[0];
    a[1] = _v1[1];
    a[2] = _v1[2];
    b[0] = _v1[vc1*3-3];
    b[1] = _v1[vc1*3-2];
    b[2] = _v1[vc1*3-1];
    c[0] = _v2[vc1*3-3];
    c[1] = _v2[vc1*3-2];
    c[2] = _v2[vc1*3-1];

    Vector norm = Vector::cross(b-a,c-b);
    norm.Normalize();
    addNormal(norm[0],norm[1],norm[2]);
    addNormal(norm[0],norm[1],norm[2]);
    addNormal(norm[0],norm[1],norm[2]);
    addNormal(norm[0],norm[1],norm[2]);
    addVertex(_v1[0],_v1[1],_v1[2]);
    addVertex(_v1[vc1*3-3],_v1[vc1*3-2],_v1[vc1*3-1]);
    addVertex(_v2[vc1*3-3],_v2[vc1*3-2],_v2[vc1*3-1]);
    addVertex(_v2[0],_v2[1],_v2[2]);
    _face1->SetReverse(false);
    _face2->SetReverse(true);
}