Terragen tutorial page 5

From Odwiki

Jump to: navigation, search

Back to the beginning: TerragenSOP

Build the Geometry Generation Code


        // Make a surface using the Terragen data
        GU_PrimMesh    *mesh;
        GU_PrimNURBSurf    *nurbsurf;
        int num_rows, num_cols;
        float heightScaleFactor = ((float)heightScale/65536);

        // If the user wants a NURBS surface, buld it, else build a mesh surface
        if(mySurfaceType == 1) {
            // Build a NURBS surface
            nurbsurf = GU_PrimNURBSurf::build(gdp, xpts, ypts, 4, 4, 0, 0, 0, 0, GEO_PATCH_QUADS, 0); 
            num_rows = nurbsurf->getNumRows();
            num_cols = nurbsurf->getNumCols();
            if(DEBUG)
                cout << "Built NURBS surface" << endl;
        }
        else {
            // Build a mesh surface
            mesh = GU_PrimMesh::build(gdp, xpts, ypts, GEO_PATCH_QUADS, 0, 0, 0);
            num_rows = mesh->getNumRows();
            num_cols = mesh->getNumCols();
            if(DEBUG)
                cout << "Built Mesh surface" << endl;
        }

        // Add an attribute for our data
         p_elevation = gdp->addPointAttrib("elevation", sizeof(float), GB_ATTRIB_FLOAT, 0);

         // For each row and column of the surface ...
         for (int row = 0; row < num_rows; row++) 
            for (int col = 0; col < num_cols; col++) {
            
                // Append a point
                ppt = gdp->appendPoint();

                // Get the data elev_value for this point and calculate it's "real" elevation
                myElevation = (float)((((elev_value[(row * ypts) + col] + baseHeight) * heightScaleFactor) * myElevScale) + myElevOffset);
                
                // Assign the data value to the point        
                elevation = (float *)ppt->getAttribData(p_elevation);
                *elevation = myElevation;

                // Assign the points position
                ppt->getPos().assign((float)col, myElevation, (float)row, 1);
                
                // Set the point to the surface
                if(mySurfaceType == 1) 
                    (*nurbsurf)(row, col).setPt(ppt);
                else
                    (*mesh)(row, col).setPt(ppt); 
            }

        // We're done
        boss->opEnd();
        }

© 2009 od[force].net | advertise