Exemple #1
0
void init(float winwidth, float winheight, float pixratio)
{
    printf(
        "Spacebar to toggle texture modes.\n"
        "D to toggle auto-zooming demo mode.\n"
        "G to toggle the slippy map grid.\n");
    parg_state_clearcolor((Vector4){0.43, 0.61, 0.8, 1});
    parg_state_cullfaces(1);
    parg_state_depthtest(0);
    parg_shader_load_from_asset(SHADER_DEFAULT);
    parg_zcam_init(1, 1, fovy);
    parg_zcam_set_position(0, 0, STARTZ);
    ocean_texture = parg_texture_from_asset(TEXTURE_OCEAN);
    paper_texture = parg_texture_from_asset(TEXTURE_PAPER);

    // Decode the europe image.
    int* rawdata;
    parg_buffer* colorbuf =
        parg_buffer_slurp_asset(TEXTURE_EUROPE, (void*) &rawdata);
    int width = *rawdata++;
    int height = *rawdata++;
    int ncomps = *rawdata++;
    parg_texture_fliprows(rawdata, width * ncomps, height);

    // Sample the ocean color from one corner of the image.
    int ocean_color = rawdata[0];

    // Perform marching squares and generate a mesh.
    par_msquares_meshlist* mlist =
        par_msquares_color((parg_byte*) rawdata, width, height, 16, ocean_color,
            4, PAR_MSQUARES_SWIZZLE | PAR_MSQUARES_DUAL | PAR_MSQUARES_HEIGHTS |
            PAR_MSQUARES_SIMPLIFY);
    par_msquares_mesh const* mesh;
    mesh = par_msquares_get_mesh(mlist, 0);
    landmass_mesh = parg_mesh_create(
        mesh->points, mesh->npoints, mesh->triangles, mesh->ntriangles);
    mesh = par_msquares_get_mesh(mlist, 1);
    ocean_mesh = parg_mesh_create(
        mesh->points, mesh->npoints, mesh->triangles, mesh->ntriangles);
    parg_buffer_unlock(colorbuf);
    par_msquares_free(mlist);
}
Exemple #2
0
static void create_mesh()
{
    par_msquares_meshlist* mlist = 0;
    float threshold = 0;
    int flags = 0;
    if (state == STATE_GRAY_DEFAULT) {
        float const* graydata = parg_buffer_lock(graybuf, PARG_READ);
        mlist = par_msquares_grayscale(
            graydata, IMGWIDTH, IMGHEIGHT, CELLSIZE, threshold, flags);
        parg_buffer_unlock(graybuf);
    } else if (state == STATE_GRAY_SIMPLIFY) {
        float const* graydata = parg_buffer_lock(graybuf, PARG_READ);
        flags = PAR_MSQUARES_SIMPLIFY;
        mlist = par_msquares_grayscale(
            graydata, IMGWIDTH, IMGHEIGHT, CELLSIZE, threshold, flags);
        parg_buffer_unlock(graybuf);
    } else if (state == STATE_GRAY_INVERT) {
        float const* graydata = parg_buffer_lock(graybuf, PARG_READ);
        flags = PAR_MSQUARES_INVERT;
        mlist = par_msquares_grayscale(
            graydata, IMGWIDTH, IMGHEIGHT, CELLSIZE, threshold, flags);
        parg_buffer_unlock(graybuf);
    } else if (state == STATE_GRAY_DUAL) {
        float const* graydata = parg_buffer_lock(graybuf, PARG_READ);
        flags = PAR_MSQUARES_DUAL;
        mlist = par_msquares_grayscale(
            graydata, IMGWIDTH, IMGHEIGHT, CELLSIZE, threshold, flags);
        parg_buffer_unlock(graybuf);
    } else if (state == STATE_GRAY_HEIGHTS) {
        float const* graydata = parg_buffer_lock(graybuf, PARG_READ);
        flags = PAR_MSQUARES_HEIGHTS;
        mlist = par_msquares_grayscale(
            graydata, IMGWIDTH, IMGHEIGHT, CELLSIZE, threshold, flags);
        parg_buffer_unlock(graybuf);
    } else if (state == STATE_GRAY_DHS) {
        float const* graydata = parg_buffer_lock(graybuf, PARG_READ);
        flags = PAR_MSQUARES_DUAL | PAR_MSQUARES_HEIGHTS | PAR_MSQUARES_SNAP;
        mlist = par_msquares_grayscale(
            graydata, IMGWIDTH, IMGHEIGHT, CELLSIZE, threshold, flags);
        parg_buffer_unlock(graybuf);
    } else if (state == STATE_GRAY_DHSC) {
        float const* graydata = parg_buffer_lock(graybuf, PARG_READ);
        flags = PAR_MSQUARES_DUAL | PAR_MSQUARES_HEIGHTS | PAR_MSQUARES_SNAP |
            PAR_MSQUARES_CONNECT;
        mlist = par_msquares_grayscale(
            graydata, IMGWIDTH, IMGHEIGHT, CELLSIZE, threshold, flags);
        parg_buffer_unlock(graybuf);
    } else if (state == STATE_GRAY_MULTI) {
        float const* graydata = parg_buffer_lock(graybuf, PARG_READ);
        float thresholds[] = {0.0, 0.1};
        flags = PAR_MSQUARES_SIMPLIFY | PAR_MSQUARES_HEIGHTS |
            PAR_MSQUARES_SNAP | PAR_MSQUARES_CONNECT;
        mlist = par_msquares_grayscale_multi(
            graydata, IMGWIDTH, IMGHEIGHT, CELLSIZE, thresholds, 2, flags);
        parg_buffer_unlock(graybuf);
    } else if (state == STATE_COLOR_DEFAULT) {
        parg_byte const* rgbadata = parg_buffer_lock(colorbuf, PARG_READ);
        rgbadata += sizeof(int) * 3;
        mlist = par_msquares_color(
            rgbadata, IMGWIDTH, IMGHEIGHT, CELLSIZE, 0x214562, 4, flags);
        parg_buffer_unlock(colorbuf);
    } else if (state == STATE_COLOR_IH) {
        parg_byte const* rgbadata = parg_buffer_lock(colorbuf, PARG_READ);
        rgbadata += sizeof(int) * 3;
        flags = PAR_MSQUARES_INVERT | PAR_MSQUARES_HEIGHTS;
        mlist = par_msquares_color(
            rgbadata, IMGWIDTH, IMGHEIGHT, CELLSIZE, 0x214562, 4, flags);
        parg_buffer_unlock(colorbuf);
    } else if (state == STATE_COLOR_DHSCSI) {
        parg_byte const* rgbadata = parg_buffer_lock(colorbuf, PARG_READ);
        rgbadata += sizeof(int) * 3;
        flags = PAR_MSQUARES_DUAL | PAR_MSQUARES_HEIGHTS | PAR_MSQUARES_SNAP |
            PAR_MSQUARES_CONNECT | PAR_MSQUARES_SIMPLIFY |
            PAR_MSQUARES_INVERT;
        mlist = par_msquares_color(
            rgbadata, IMGWIDTH, IMGHEIGHT, CELLSIZE, 0x214562, 4, flags);
        parg_buffer_unlock(colorbuf);
    } else if (state == STATE_MULTI_RGB) {
        unsigned dims[2] = {0, 0};
        unsigned char* pixels;
        lodepng_decode_file(
            &pixels, &dims[0], &dims[1], "extern/par/test/rgb.png", LCT_RGB, 8);
        mlist = par_msquares_color_multi(
            pixels, dims[0], dims[1], 16, 3, PAR_MSQUARES_SIMPLIFY);
        free(pixels);
    } else if (state == STATE_MULTI_RGBA) {
        unsigned dims[2] = {0, 0};
        unsigned char* pixels;
        lodepng_decode_file(&pixels, &dims[0], &dims[1],
            "extern/par/test/rgba.png", LCT_RGBA, 8);
        mlist = par_msquares_color_multi(pixels, dims[0], dims[1], 16, 4,
                PAR_MSQUARES_HEIGHTS | PAR_MSQUARES_CONNECT |
                PAR_MSQUARES_SIMPLIFY);
        free(pixels);
    } else if (state == STATE_MULTI_DIAGRAM) {
        parg_byte const* rgbadata = parg_buffer_lock(colorbuf, PARG_READ);
        rgbadata += sizeof(int) * 3;
        mlist = par_msquares_color_multi(rgbadata, IMGWIDTH, IMGHEIGHT,
                CELLSIZE, 4, PAR_MSQUARES_SIMPLIFY | PAR_MSQUARES_HEIGHTS);
        parg_buffer_unlock(colorbuf);
    }

    nmeshes = par_msquares_get_count(mlist);
    printf("%d meshes\n", nmeshes);
    for (int imesh = 0; imesh < nmeshes; imesh++) {
        par_msquares_mesh const* mesh = par_msquares_get_mesh(mlist, imesh);

        // mquares_mesh might have dimensionality of 2 or 3, while parg_mesh
        // only
        // supports the latter.  So, we potentially need to expand the data from
        // vec2 to vec3.

        float* points = mesh->points;
        if (mesh->dim == 2) {
            points = malloc(mesh->npoints * sizeof(float) * 3);
            for (int i = 0; i < mesh->npoints; i++) {
                points[i * 3] = mesh->points[i * 2];
                points[i * 3 + 1] = mesh->points[i * 2 + 1];
                points[i * 3 + 2] = 0;
            }
        }
        meshcolors[imesh] = mesh->color;
        trimesh[imesh] = parg_mesh_create(
            points, mesh->npoints, mesh->triangles, mesh->ntriangles);
        if (mesh->dim == 2) {
            free(points);
        }
    }

    par_msquares_free(mlist);
}