Пример #1
0
void draw_scene(App *app)
{
    int n = 5;
    for (int zi = 0; zi <= n; zi++)
    for (int xi = 0; xi <= n; xi++)
    {
        float x = Centimeter(-100.0f) + 2.0f * Centimeter(100.0f * xi / n);
        float z = Centimeter(-100.0f * zi);
        int i = zi * n + xi;
        mat4 mat_model = translate(x, 0.0f, z) *
                         scale(Centimeter(5.0f)) *
                         rotateY(0.3f * app->elapsed_time + i * 1.57f) *
                         rotateX(0.2f * app->elapsed_time + i * 3.2f);
        uniformm4(cube, model, mat_model);
        GL_CHECK(glDrawArrays(GL_TRIANGLES, 0, 36));
    }

    mat4 mat_model = translate(0.0f, Centimeter(70.0f), Centimeter(400.0f)) *
                     scale(Centimeter(5.0f)) *
                     rotateY(0.3f * app->elapsed_time) *
                     rotateX(0.05f * app->elapsed_time);
    uniformm4(cube, model, mat_model);
    GL_CHECK(glDrawArrays(GL_TRIANGLES, 0, 36));
}
void app_update_and_render(App *app)
{
    app->current_scene = (int)(app->elapsed_time / 20.0f) % NUM_SCENES;
    Scene scene = app->scenes[app->current_scene];

    float model_scale = animate_model_scale(app->elapsed_time);

    float aspect_ratio = app->window_width / (float)app->window_height;
    mat4 mat_projection = perspective(scene.fov, aspect_ratio, scene.z_near, scene.z_far);
    mat4 mat_cube_model = rotateX(PI / 2.0f) * scale(model_scale);
    mat4 mat_view = animate_camera(app->elapsed_time);

    glEnable(GL_CULL_FACE);
    glFrontFace(GL_CCW);
    glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);
    glDepthRangef(0.0, 1.0);

    glClearDepthf(1.0f);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glBlendEquation(GL_FUNC_ADD);

    ////////////////
    // Draw backdrop
    glDepthMask(GL_FALSE);
    glUseProgram(app->program_backdrop);
    glBindBuffer(GL_ARRAY_BUFFER, app->vbo_quad);
    attribfv(backdrop, position, 2, 0);
    uniform3fv(backdrop,    sun_dir,        scene.sun_dir);
    uniform2f(backdrop,     screen_size,    app->window_width, app->window_height);
    uniform1f(backdrop,     inv_tan_fov,    1.0f / (scene.fov / 2.0f));
    uniformm4(backdrop,     view,           mat_view);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glDepthMask(GL_TRUE);

    //////////////////////////
    // Draw tessellated sphere
    glUseProgram(app->program_mapping);
    glBindBuffer(GL_ARRAY_BUFFER, app->vbo_cube);
    attribfv(mapping, position, 3, 0);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_CUBE_MAP, scene.heightmap);
    uniform1i(mapping, heightmap, 0);

    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_CUBE_MAP, scene.diffusemap);
    uniform1i(mapping, diffusemap, 1);

    uniform1f(mapping, height_scale,        scene.height_scale);
    uniform1f(mapping, use_mip,             scene.use_mip ? 1.0f : 0.0f);
    uniform1f(mapping, max_lod_coverage,    scene.max_lod_coverage);
    uniform2f(mapping, screen_size,         app->window_width, app->window_height);
    uniformm4(mapping, model,               mat_cube_model);
    uniformm4(mapping, view,                mat_view);
    uniformm4(mapping, projection,          mat_projection);
    glPatchParameteri(GL_PATCH_VERTICES, VERTICES_PER_PATCH);
    glDrawArrays(GL_PATCHES, 0, NUM_PATCHES * VERTICES_PER_PATCH);
}