#include #include #include #include #include #define ENSURE_SUCCESS( result ) \ if ( (result) != HAPI_RESULT_SUCCESS ) \ { \ std::cout << "Failure at " << __FILE__ << ": " << __LINE__ << std::endl; \ std::cout << getLastError() << std::endl; \ exit( 1 ); \ } static std::string getLastError(); int main( int argc, char ** argv ) { HAPI_CookOptions cookOptions = HAPI_CookOptions_Create(); HAPI_Session session; HAPI_CreateInProcessSession( &session ); ENSURE_SUCCESS( HAPI_Initialize( &session, &cookOptions, true, -1, nullptr, nullptr, nullptr, nullptr, nullptr ) ); int start = 0; int part_id = 0; HAPI_NodeId out_node_id = -1; ENSURE_SUCCESS( HAPI_CreateNode( &session, -1, "sop/heightfield", nullptr, true, &out_node_id ) ); HAPI_GeoInfo out_geo_info = HAPI_GeoInfo_Create(); ENSURE_SUCCESS( HAPI_GetDisplayGeoInfo( &session, out_node_id, &out_geo_info ) ); HAPI_PartInfo out_part_info = HAPI_PartInfo_Create(); ENSURE_SUCCESS( HAPI_GetPartInfo( &session, out_geo_info.nodeId, part_id, &out_part_info ) ); HAPI_VolumeInfo height_volume_info = HAPI_VolumeInfo_Create(); ENSURE_SUCCESS( HAPI_GetVolumeInfo( &session, out_geo_info.nodeId, part_id, &height_volume_info ) ); const int totalsize = ( height_volume_info.xLength * height_volume_info.yLength ); std::vector< float > heightfieldData( totalsize ); std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<> dis(1, 2); for (int n = 0; n < totalsize; ++n) { heightfieldData[n] = (float)dis(gen); } height_volume_info.type = HAPI_VOLUMETYPE_HOUDINI; height_volume_info.storage = HAPI_STORAGETYPE_FLOAT; height_volume_info.tupleSize = 1; height_volume_info.tileSize = 1; height_volume_info.zLength = 1; height_volume_info.hasTaper = false; height_volume_info.xTaper = 0.0; height_volume_info.yTaper = 0.0; ENSURE_SUCCESS( HAPI_SetVolumeInfo( &session, out_geo_info.nodeId, 0, &height_volume_info ) ); ENSURE_SUCCESS( HAPI_SetHeightFieldData( &session, out_geo_info.nodeId, 0, heightfieldData.data(), start, totalsize, "height" ) ); ENSURE_SUCCESS( HAPI_CommitGeo( &session, out_geo_info.nodeId ) ); ENSURE_SUCCESS( HAPI_CookNode( &session, out_geo_info.nodeId, nullptr ) ); ENSURE_SUCCESS( HAPI_SaveHIPFile( &session, "height_field.hip", false ) ); HAPI_Cleanup( &session ); return 0; } static std::string getLastError() { int bufferLength; HAPI_GetStatusStringBufLength( nullptr, HAPI_STATUS_CALL_RESULT, HAPI_STATUSVERBOSITY_ERRORS, &bufferLength ); char * buffer = new char[ bufferLength ]; HAPI_GetStatusString( nullptr, HAPI_STATUS_CALL_RESULT, buffer, bufferLength ); std::string result( buffer ); delete [] buffer; return result; }