Ejemplo n.º 1
0
void Renderer::renderMap() {
  record_section("renderMap");
  if (mapSize_ == glm::vec2(0.f)) {
    return;
  }
  auto gridDim = mapSize_ * 2.f;

  auto mapShader = ResourceManager::get()->getShader("map");
  mapShader->makeActive();
  mapShader->uniform4f("color", mapColor_);
  mapShader->uniform2f("mapSize", mapSize_);
  mapShader->uniform2f("gridDim", gridDim);

  // TODO(zack): HACK ALERT this is for fog of war, render as a separate
  // step instead
	if (controller_) {
		controller_->updateMapShader(mapShader);
	}

  // TODO(zack): render map with height/terrain map
  glm::mat4 transform =
    glm::scale(
        glm::mat4(1.f),
        glm::vec3(mapSize_.x, mapSize_.y, 1.f));
  renderRectangleProgram(transform);
}
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);
}