<commit_before>#include <par.h>
#include <parwin.h>
#include <assert.h>
#include <sds.h>
#include <stdio.h>

#define TOKEN_TABLE(F)          \
    F(P_TEXTURED, "p_textured") \
    F(P_SOLID, "p_solid")       \
    F(A_POSITION, "a_position") \
    F(A_TEXCOORD, "a_texcoord") \
    F(U_MVP, "u_mvp")
TOKEN_TABLE(PAR_TOKEN_DECLARE);

#define ASSET_TABLE(F)                \
    F(SHADER_MARINA, "marina.glsl")   \
    F(TEXTURE_DOGGIES, "doggies.png") \
    F(TEXTURE_ORIGIN, "origin_z02.png")
ASSET_TABLE(PAR_TOKEN_DECLARE);

#define ASSET_LIST(F)   \
    F("marina_z05.png") \
    F("marina_z10.png") \
    F("marina_z15.png") \
    F("marina_z20.png")

#define NUM_LEVELS 4
const float gray = 0.8;
const float fovy = 16 * PAR_TWOPI / 180;
const float lon = -122.3245;
const float lat = 37.8743;
const int levels[NUM_LEVELS] = {5, 10, 15, 20};
par_texture* marina_textures[NUM_LEVELS];
par_texture* origin_texture;
par_texture* doggies_texture;
par_buffer* lines_buffer;
DVector3 translation = {0};
par_mesh* tile_mesh;
par_mesh* photo_mesh;
float tscale;

void init(float winwidth, float winheight, float pixratio)
{
    par_state_clearcolor((Vector4){gray, gray, gray, 1});
    par_shader_load_from_asset(SHADER_MARINA);
    origin_texture = par_texture_from_asset(TEXTURE_ORIGIN);
    int imgwidth, imgheight;
    par_texture_info(origin_texture, &imgwidth, &imgheight);
    assert(imgwidth == imgheight);
    tscale = 1280 / 256;
    par_zcam_init(1, 1, fovy);
    par_zcam_grab_update(0.5, 0.5, 20);
    tile_mesh = par_mesh_rectangle(1, 1);
    translation.x = lon / 360;
    float latrad = lat * PAR_PI / 180;
    float mercN = log(tan((PAR_PI / 4) + (latrad / 2)));
    translation.y = mercN / (2 * PAR_PI);
    for (int i = 0; i < NUM_LEVELS; i++) {
        sds name = sdsnew("marina_z");
        name = sdscatprintf(name, "%02d.png", levels[i]);
        par_token id = par_token_from_string(name);
        marina_textures[i] = par_texture_from_asset(id);
        sdsfree(name);
    }
    doggies_texture = par_texture_from_asset(TEXTURE_DOGGIES);
    par_texture_info(doggies_texture, &imgwidth, &imgheight);
    photo_mesh = par_mesh_rectangle(1, (float) imgheight / imgwidth);
    int nverts = 4;
    int vstride = sizeof(float) * 2;
    lines_buffer = par_buffer_alloc(nverts * vstride, PAR_GPU_ARRAY);
    float* plines = par_buffer_lock(lines_buffer, PAR_WRITE);
    *plines++ = translation.x;
    *plines++ = -1;
    *plines++ = translation.x;
    *plines++ = 1;
    *plines++ = -1;
    *plines++ = translation.y;
    *plines++ = 1;
    *plines++ = translation.y;
    par_buffer_unlock(lines_buffer);
}

int draw()
{
    double scale;
    DMatrix4 view, projection, model;
    Matrix4 mvp;
    par_zcam_dmatrices(&projection, &view);
    par_draw_clear();
    par_shader_bind(P_TEXTURED);
    par_varray_enable(
        par_mesh_coord(tile_mesh), A_POSITION, 2, PAR_FLOAT, 0, 0);
    par_varray_enable(par_mesh_uv(tile_mesh), A_TEXCOORD, 2, PAR_FLOAT, 0, 0);
    scale = tscale / pow(2, 2);
    model = DM4MakeScale((DVector3){scale, scale, scale});
    mvp = M4MakeFromDM4(DM4Mul(projection, DM4Mul(view, model)));
    par_texture_bind(origin_texture, 0);
    par_uniform_matrix4f(U_MVP, &mvp);
    par_draw_one_quad();
    for (int i = 0; i < NUM_LEVELS; i++) {
        scale = tscale / pow(2, levels[i]);
        model = DM4Mul(DM4MakeTranslation(translation),
                DM4MakeScale((DVector3){scale, scale, scale}));
        mvp = M4MakeFromDM4(DM4Mul(projection, DM4Mul(view, model)));
        par_texture_bind(marina_textures[i], 0);
        par_uniform_matrix4f(U_MVP, &mvp);
        par_draw_one_quad();
    }
    par_shader_bind(P_SOLID);
    mvp = M4MakeFromDM4(DM4Mul(projection, view));
    par_uniform_matrix4f(U_MVP, &mvp);
    par_varray_enable(lines_buffer, A_POSITION, 2, PAR_FLOAT, 0, 0);
    par_draw_lines(2);
    par_shader_bind(P_TEXTURED);
    par_varray_enable(
        par_mesh_coord(photo_mesh), A_POSITION, 2, PAR_FLOAT, 0, 0);
    scale = tscale / pow(2, 25);
    model = DM4Mul(DM4MakeTranslation(translation),
            DM4MakeScale((DVector3){scale, scale, scale}));
    mvp = M4MakeFromDM4(DM4Mul(projection, DM4Mul(view, model)));
    par_texture_bind(doggies_texture, 0);
    par_uniform_matrix4f(U_MVP, &mvp);
    par_draw_one_quad();
    return 1;
}

void tick(float winwidth, float winheight, float pixratio, float seconds)
{
    par_zcam_tick(winwidth / winheight, seconds);
}

void dispose()
{
    par_buffer_free(lines_buffer);
    par_mesh_free(tile_mesh);
    par_mesh_free(photo_mesh);
    par_texture_free(origin_texture);
    par_texture_free(doggies_texture);
    for (int i = 0; i < NUM_LEVELS; i++) {
        par_texture_free(marina_textures[i]);
    }
}

void input(par_event evt, float x, float y, float z)
{
    switch (evt) {
    case PAR_EVENT_DOWN:
        par_zcam_grab_begin(x, y);<commit_msg>Progress with precision work.<commit_after>#include <par.h>
#include <parwin.h>
#include <assert.h>
#include <sds.h>
#include <stdio.h>

#define TOKEN_TABLE(F)          \
    F(P_TEXTURED, "p_textured") \
    F(P_SOLID, "p_solid")       \
    F(A_POSITION, "a_position") \
    F(A_TEXCOORD, "a_texcoord") \
    F(U_MVP, "u_mvp")
TOKEN_TABLE(PAR_TOKEN_DECLARE);

#define ASSET_TABLE(F)                \
    F(SHADER_MARINA, "marina.glsl")   \
    F(TEXTURE_DOGGIES, "doggies.png") \
    F(TEXTURE_ORIGIN, "origin_z02.png")
ASSET_TABLE(PAR_TOKEN_DECLARE);

#define ASSET_LIST(F)   \
    F("marina_z05.png") \
    F("marina_z10.png") \
    F("marina_z15.png") \
    F("marina_z20.png")

#define NUM_LEVELS 4
const float gray = 0.8;
const float fovy = 16 * PAR_TWOPI / 180;
const float lon = -122.3245;
const float lat = 37.8743;
const int levels[NUM_LEVELS] = {5, 10, 15, 20};
par_texture* marina_textures[NUM_LEVELS];
par_texture* origin_texture;
par_texture* doggies_texture;
par_buffer* linesfp32_buffer;
par_buffer* linesfp64_buffer;
DVector3 translation = {0};
par_mesh* tile_mesh;
par_mesh* photo_mesh;
float tscale;

// Write out a double using two floats: a low part and a high part.
float* write_fp64(float* dst, double val)
{
    float high = (float) val;
    double low = val - (double) low;
    *dst++ = high;
    *dst++ = low;
    return dst;
}

void init(float winwidth, float winheight, float pixratio)
{
    par_state_clearcolor((Vector4){gray, gray, gray, 1});
    par_shader_load_from_asset(SHADER_MARINA);
    origin_texture = par_texture_from_asset(TEXTURE_ORIGIN);
    int imgwidth, imgheight;
    par_texture_info(origin_texture, &imgwidth, &imgheight);
    assert(imgwidth == imgheight);
    tscale = 1280 / 256;
    par_zcam_init(1, 1, fovy);
    par_zcam_grab_update(0.5, 0.5, 20);
    tile_mesh = par_mesh_rectangle(1, 1);
    translation.x = lon / 360;
    float latrad = lat * PAR_PI / 180;
    float mercN = log(tan((PAR_PI / 4) + (latrad / 2)));
    translation.y = mercN / (2 * PAR_PI);
    for (int i = 0; i < NUM_LEVELS; i++) {
        sds name = sdsnew("marina_z");
        name = sdscatprintf(name, "%02d.png", levels[i]);
        par_token id = par_token_from_string(name);
        marina_textures[i] = par_texture_from_asset(id);
        sdsfree(name);
    }
    doggies_texture = par_texture_from_asset(TEXTURE_DOGGIES);
    par_texture_info(doggies_texture, &imgwidth, &imgheight);
    photo_mesh = par_mesh_rectangle(1, (float) imgheight / imgwidth);
    int nverts = 4;

    // Populate a single-precision buffer of vec2's for the lines.
    int vstride = sizeof(float) * 2;
    linesfp32_buffer = par_buffer_alloc(nverts * vstride, PAR_GPU_ARRAY);
    float* plines = par_buffer_lock(linesfp32_buffer, PAR_WRITE);
    *plines++ = translation.x;
    *plines++ = -1;
    *plines++ = translation.x;
    *plines++ = 1;
    *plines++ = -1;
    *plines++ = translation.y;
    *plines++ = 1;
    *plines++ = translation.y;
    par_buffer_unlock(linesfp32_buffer);

    // Populate a double-precision buffer of vec2's for the lines.
    vstride = sizeof(float) * 4;
    linesfp64_buffer = par_buffer_alloc(nverts * vstride, PAR_GPU_ARRAY);
    plines = par_buffer_lock(linesfp64_buffer, PAR_WRITE);
    plines = write_fp64(plines, translation.x);
    plines = write_fp64(plines, 1);
    plines = write_fp64(plines, translation.x);
    plines = write_fp64(plines, 1);
    plines = write_fp64(plines, -1);
    plines = write_fp64(plines, translation.y);
    plines = write_fp64(plines, 1);
    plines = write_fp64(plines, translation.y);
    par_buffer_unlock(linesfp64_buffer);
}

int draw()
{
    double scale;
    DMatrix4 view, projection, model;
    Matrix4 mvp;
    par_zcam_dmatrices(&projection, &view);
    par_draw_clear();
    par_shader_bind(P_TEXTURED);
    par_varray_enable(
        par_mesh_coord(tile_mesh), A_POSITION, 2, PAR_FLOAT, 0, 0);
    par_varray_enable(par_mesh_uv(tile_mesh), A_TEXCOORD, 2, PAR_FLOAT, 0, 0);
    scale = tscale / pow(2, 2);
    model = DM4MakeScale((DVector3){scale, scale, scale});
    mvp = M4MakeFromDM4(DM4Mul(projection, DM4Mul(view, model)));
    par_texture_bind(origin_texture, 0);
    par_uniform_matrix4f(U_MVP, &mvp);
    par_draw_one_quad();
    for (int i = 0; i < NUM_LEVELS; i++) {
        scale = tscale / pow(2, levels[i]);
        model = DM4Mul(DM4MakeTranslation(translation),
                DM4MakeScale((DVector3){scale, scale, scale}));
        mvp = M4MakeFromDM4(DM4Mul(projection, DM4Mul(view, model)));
        par_texture_bind(marina_textures[i], 0);
        par_uniform_matrix4f(U_MVP, &mvp);
        par_draw_one_quad();
    }
    par_shader_bind(P_SOLID);
    mvp = M4MakeFromDM4(DM4Mul(projection, view));
    par_uniform_matrix4f(U_MVP, &mvp);
    par_varray_enable(linesfp32_buffer, A_POSITION, 2, PAR_FLOAT, 0, 0);
    par_draw_lines(2);
    par_shader_bind(P_TEXTURED);
    par_varray_enable(
        par_mesh_coord(photo_mesh), A_POSITION, 2, PAR_FLOAT, 0, 0);
    scale = tscale / pow(2, 25);
    model = DM4Mul(DM4MakeTranslation(translation),
            DM4MakeScale((DVector3){scale, scale, scale}));
    mvp = M4MakeFromDM4(DM4Mul(projection, DM4Mul(view, model)));
    par_texture_bind(doggies_texture, 0);
    par_uniform_matrix4f(U_MVP, &mvp);
    par_draw_one_quad();
    return 1;
}

void tick(float winwidth, float winheight, float pixratio, float seconds)
{
    par_zcam_tick(winwidth / winheight, seconds);
}

void dispose()
{
    par_buffer_free(linesfp32_buffer);
    par_mesh_free(tile_mesh);
    par_mesh_free(photo_mesh);
    par_texture_free(origin_texture);
    par_texture_free(doggies_texture);
    for (int i = 0; i < NUM_LEVELS; i++) {
        par_texture_free(marina_textures[i]);
    }
}

void input(par_event evt, float x, float y, float z)
{
    switch (evt) {
    case PAR_EVENT_DOWN:
        par_zcam_grab_begin(x, y);
{'fraction_non_alphanumeric': 0.13076278290025148, 'fraction_numerical': 0.02915153208531247, 'mean_word_length': 3.7660896582334664, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '54892', 'n_tokens_mistral': 4360, 'n_tokens_neox': 4153, 'n_words': 789}