Example #1
0
BaseMeshRef SimpleMesh::generateQuad(Rectf dimensions,
                                     Rectf uvCoords = Rectf(0.0f, 0.0f, 1.0f,
                                             1.0f)) {

    // cout << "SimpleMesh::GenerateQuad(); dims: " << dimensions
    //     << " uvCoords: " << uvCoords << endl;

    TriMesh mesh;
    mesh.clear();

    // Vertexes
    mesh.appendVertex(Vec3f(dimensions.x1, dimensions.y1, 0));
    mesh.appendVertex(Vec3f(dimensions.x1, dimensions.y2, 0));
    mesh.appendVertex(Vec3f(dimensions.x2, dimensions.y2, 0));
    mesh.appendVertex(Vec3f(dimensions.x2, dimensions.y1, 0));

    // Vertex Colors
    mesh.appendColorRgb(Color(1.0f, 1.0f, 1.0f));
    mesh.appendColorRgb(Color(1.0f, 1.0f, 1.0f));
    mesh.appendColorRgb(Color(1.0f, 1.0f, 1.0f));
    mesh.appendColorRgb(Color(1.0f, 1.0f, 1.0f));

    // Tex coords
    mesh.appendTexCoord(Vec2f(uvCoords.x1, uvCoords.y1));
    mesh.appendTexCoord(Vec2f(uvCoords.x1, uvCoords.y2));
    mesh.appendTexCoord(Vec2f(uvCoords.x2, uvCoords.y2));
    mesh.appendTexCoord(Vec2f(uvCoords.x2, uvCoords.y1));

    int vert0 = mesh.getNumVertices() - 4;
    int vert1 = mesh.getNumVertices() - 1;
    int vert2 = mesh.getNumVertices() - 2;
    int vert3 = mesh.getNumVertices() - 3;

    mesh.appendTriangle(vert0, vert1, vert3);
    mesh.appendTriangle(vert3, vert1, vert2);

    mesh.recalculateNormals();

    SimpleMeshRef meshWrapper = make_shared<SimpleMesh>(mesh);
    meshWrapper->_bounds = mesh.calcBoundingBox();
    return dynamic_pointer_cast<BaseMesh>(meshWrapper);
}
// dependent on a change in parameters, write new colors to the TriMesh
void GeoDeVisualizerApp::writeColors()
{
    mTriangles.getColorsRGB().clear();
    vector<Color> colors;
    colors.reserve(mTriangles.getNumTriangles());
    
    DisplayMode mode = (DisplayMode)mCurParams.displayMode;
    switch(mode) {
    case Geography:
        {
            colors = mWorld.mGeoColors;
            break;
        }
    case Colonies:
        {
            colors = mWorld.mGeoColors;
            // add sphere of influence and location
            Colony& colRef = mWorld.mColonies[mCurParams.displayColony];
            mCamVars.sceneRotation.set(getAverageVertex(colRef.mLocation), mCam.getEyePoint());
            for (set<u32>::iterator soiIt = colRef.mSphereInfluence.begin(); soiIt != colRef.mSphereInfluence.end(); ++soiIt) {
                colors[*soiIt] = Color(0.2, 0.2, 0.2);
            }
            colors[colRef.mLocation] = Color(1.0, 0.0, 0.0);
            break;
        }
    case Resources:
        {
            
            vector<u32> activeResIdxs;
            for (u32 i = 0; i < mCurParams.numResources; i++) {
                if (mCurParams.showResources[i]) {
                    activeResIdxs.push_back(i);
                }
            }
            if (activeResIdxs.size() > 0) {
                colors.insert(colors.begin(), mTriangles.getNumTriangles(), Color(0.0, 0.0, 0.0));
            } else {
                colors.insert(colors.begin(), mTriangles.getNumTriangles(), Color(0.2, 0.2, 0.2));
                break;
            }
            float numIndices = activeResIdxs.size();
            for(vector<u32>::iterator idxIt = activeResIdxs.begin(); idxIt != activeResIdxs.end(); ++idxIt) {
                vector<f32>& activeResVals = (mWorld.resources())[*idxIt].mVals;
                vector<Color>::iterator colIt = colors.begin();
                for (vector<f32>::iterator valIt = activeResVals.begin(); valIt != activeResVals.end(); ++valIt) {
                    *colIt += (resourceColors[*idxIt] * (*valIt)) / numIndices;
                    colIt++;
                }
            }
            break;
        }
    case Attributes:
        {
            colors.insert(colors.begin(), mTriangles.getNumTriangles(), Color(0.0, 0.0, 0.0));
            vector<u32> activeAttrIdxs;
            for (u32 i = 0; i < NUM_ATTRIBUTES; i++) {
                if (mCurParams.showAttributes[i]) {
                    activeAttrIdxs.push_back(i);
                }
            }
            if (activeAttrIdxs.size() == 0) {
                colors.clear();
                colors.insert(colors.begin(), mTriangles.getNumTriangles(), Color(0.1, 0.1, 0.1));
                break;
            }
            map<string, Color> attrMap;
            attrMap["wetness"] = Color(0.0, 0.0, 1.0);
            attrMap["temperature"] = Color(1.0, 0.0, 0.0);
            attrMap["elevation"] = Color(0.0, 1.0, 0);
            // iterate through all active indices
            for(vector<u32>::iterator idxIt = activeAttrIdxs.begin(); idxIt != activeAttrIdxs.end(); ++idxIt) {
                Color colorVal = attrMap[mWorld.mAttributes[*idxIt].mName];
                vector<Color>::iterator faceColorIt = colors.begin();
                // iterate through all attribute values
                for (vector<f32>::iterator attrIt = mWorld.mAttributes[*idxIt].mVals.begin(); attrIt != mWorld.mAttributes[*idxIt].mVals.end(); ++attrIt) {
                    *faceColorIt += (colorVal * (*attrIt));
                    faceColorIt++;
                }
            }
            break;
        }
    }
    // triple the colors vector so that triangles show up as solid triangles
    for (vector<Color>::iterator colIt = colors.begin(); colIt != colors.end(); ++colIt) {
        mTriangles.appendColorRgb(*colIt);
        mTriangles.appendColorRgb(*colIt);
        mTriangles.appendColorRgb(*colIt);
    }
    mFrame = mTriangles;
    mFrame.getColorsRGB().clear();
    vector<Color> frameColors(colors.size()*3, Color(0.1, 0.1, 0.1));
    mFrame.appendColorsRgb(frameColors.data(), frameColors.size());
}
Example #3
0
void verticesApp::generateCapsule()
{
    
    vector<uint> indices;
    
    double fDeltaRingAngle = (M_PI_2 / mNumRings);
    double fDeltaSegAngle = ((M_PI * 2.0) / mNumSegments);
    
    double sphereRatio = mRadius / (2 * mRadius + mHeight);
    double cylinderRatio = mHeight / (2 * mRadius + mHeight);
    int offset = 0;
    // Top half sphere
    
    // Generate the group of rings for the sphere
    for(unsigned int ring = 0; ring <= mNumRings; ring++ )
    {
        double r0 = mRadius * sinf ( ring * fDeltaRingAngle);
        double y0 = mRadius * cosf (ring * fDeltaRingAngle);
        
        // Generate the group of segments for the current ring
        for(unsigned int seg = 0; seg <= mNumSegments; seg++)
        {
            double x0 = r0 * cosf(seg * fDeltaSegAngle);
            double z0 = r0 * sinf(seg * fDeltaSegAngle);
            
            Vec3f p(x0, 0.5f * mHeight + y0, z0);
            Vec3f n(x0, y0, z0);
            mesh.appendVertex(p);
            mesh.appendNormal(n.normalized());
            mesh.appendTexCoord(Vec2f((double) seg / (double) mNumSegments, (double) ring / (double) mNumRings * sphereRatio));
            mesh.appendColorRgb(Colorf(1.0, 0, 0));
            
            // each vertex (except the last) has six indices pointing to it
            indices.push_back(offset + mNumSegments + 1);
            indices.push_back(offset + mNumSegments);
            indices.push_back(offset);
            indices.push_back(offset + mNumSegments + 1);
            indices.push_back(offset);
            indices.push_back(offset + 1);
            
            offset ++;
        } // end for seg
    } // end for ring
    
    // Cylinder part
    double deltaAngle = ((M_PI * 2.0) / mNumSegments);
    double deltamHeight = mHeight/(double)mNumSegHeight;
    
    for (unsigned short i = 1; i < mNumSegHeight; i++) {
        for (unsigned short j = 0; j<=mNumSegments; j++)
        {
            double x0 = mRadius * cosf(j*deltaAngle);
            double z0 = mRadius * sinf(j*deltaAngle);
            
            Vec3f p(x0, 0.5f*mHeight-i*deltamHeight, z0);
            Vec3f n(x0, 0, z0);
            mesh.appendVertex(p);
            mesh.appendNormal(n.normalized());
            mesh.appendTexCoord(Vec2f(j/(double)mNumSegments, i/(double)mNumSegHeight * cylinderRatio + sphereRatio));
            mesh.appendColorRgb(Colorf(0, 1.0 - (float(j)/float(mNumSegments)), 0));

            
            indices.push_back(offset + mNumSegments + 1);
            indices.push_back(offset + mNumSegments);
            indices.push_back(offset);
            indices.push_back(offset + mNumSegments + 1);
            indices.push_back(offset);
            indices.push_back(offset + 1);
            
            offset ++;
        }
    }

    // Bottom half sphere
    
    // Generate the group of rings for the sphere
    for(unsigned int ring = 0; ring <= mNumRings; ring++)
    {
        double r0 = mRadius * sinf (M_PI_2 + ring * fDeltaRingAngle);
        double y0 =  mRadius * cosf (M_PI_2 + ring * fDeltaRingAngle);
        
        // Generate the group of segments for the current ring
        for(unsigned int seg = 0; seg <= mNumSegments; seg++)
        {
            double x0 = r0 * cosf(seg * fDeltaSegAngle);
            double z0 = r0 * sinf(seg * fDeltaSegAngle);
            
            Vec3f p(x0, -0.5f*mHeight + y0, z0);
            Vec3f n(x0, y0, z0);
            mesh.appendVertex(p);
            mesh.appendNormal(n.normalized());
            mesh.appendTexCoord(Vec2f((double) seg / (double) mNumSegments, (double) ring / (double) mNumRings*sphereRatio + cylinderRatio + sphereRatio));
            mesh.appendColorRgb(Colorf(0, 0, float(ring)/float(mNumRings)));

            
            if (ring != mNumRings) 
            {
                // each vertex (except the last) has six indices pointing to it
                indices.push_back(offset + mNumSegments + 1);
                indices.push_back(offset + mNumSegments);
                indices.push_back(offset);
                indices.push_back(offset + mNumSegments + 1);
                indices.push_back(offset);
                indices.push_back(offset + 1);
            }
            offset ++;
        } // end for seg
    } // end for ring
    
    mesh.appendIndices( &indices[0], indices.size());
}