Example #1
0
texture_output::texture_output(const gl::render_device_ptr& device)
{
    using namespace scm::gl;
    using namespace scm::math;
    using boost::assign::list_of;

    mat4f pass_mvp = mat4f::identity();
    ortho_matrix(pass_mvp, 0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);

    // for fs-quad
    _quad_geom = make_shared<quad_geometry>(device, vec2f(0.0f, 0.0f), vec2f(1.0f, 1.0f));

    _fs_program_color = device->create_program(list_of(device->create_shader(scm::gl::STAGE_VERTEX_SHADER,   fs_vsrc,       "texture_output::fs_vsrc"))
                                                      (device->create_shader(scm::gl::STAGE_FRAGMENT_SHADER, fs_color_fsrc, "texture_output::fs_color_fsrc")),
                                               "texture_output::fs_program_color");

    if(!_fs_program_color) {
        throw (std::runtime_error("texture_output::texture_output(): error generating _fs_program_color program"));
    }
    _fs_program_color->uniform_sampler("in_texture", 0);
    _fs_program_color->uniform("mvp", pass_mvp);

    _fs_program_color_uint = device->create_program(list_of(device->create_shader(scm::gl::STAGE_VERTEX_SHADER,   fs_vsrc,            "texture_output::fs_vsrc"))
                                                           (device->create_shader(scm::gl::STAGE_FRAGMENT_SHADER, fs_color_uint_fsrc, "texture_output::fs_color_uint")),
                                               "texture_output::fs_program_color_uint");

    if(!_fs_program_color_uint) {
        throw (std::runtime_error("texture_output::texture_output(): error generating _fs_program_color_uint program"));
    }
    _fs_program_color_uint->uniform_sampler("in_texture", 0);
    _fs_program_color_uint->uniform("mvp", pass_mvp);

    _fs_program_color_uint8_bit_rev = device->create_program(list_of(device->create_shader(scm::gl::STAGE_VERTEX_SHADER,   fs_vsrc,                     "texture_output::fs_vsrc"))
                                                                    (device->create_shader(scm::gl::STAGE_FRAGMENT_SHADER, fs_color_uint8_bit_rev_fsrc, "texture_output::fs_color_uint8_bit_rev_fsrc")),
                                               "texture_output::fs_program_color_uint8_bit_rev");

    if(!_fs_program_color_uint8_bit_rev) {
        throw (std::runtime_error("texture_output::texture_output(): error generating _fs_program_color_uint8_bit_rev program"));
    }
    _fs_program_color_uint8_bit_rev->uniform_sampler("in_texture", 0);
    _fs_program_color_uint8_bit_rev->uniform("mvp", pass_mvp);
    _fs_program_color_uint8_bit_rev->uniform("in_scale", vec4f(1.0f / 255.0f));

    _fs_program_gray = device->create_program(list_of(device->create_shader(scm::gl::STAGE_VERTEX_SHADER,   fs_vsrc,      "texture_output::fs_vsrc"))
                                                     (device->create_shader(scm::gl::STAGE_FRAGMENT_SHADER, fs_gray_fsrc, "texture_output::fs_gray_fsrc")),
                                               "texture_output::fs_program_gray");

    if(!_fs_program_gray) {
        throw (std::runtime_error("texture_output::texture_output(): error generating _fs_program_gray program"));
    }
    _fs_program_gray->uniform_sampler("in_texture", 0);
    _fs_program_gray->uniform("mvp", pass_mvp);

    _fs_program_gray_uint = device->create_program(list_of(device->create_shader(scm::gl::STAGE_VERTEX_SHADER,   fs_vsrc,           "texture_output::fs_vsrc"))
                                                          (device->create_shader(scm::gl::STAGE_FRAGMENT_SHADER, fs_gray_uint_fsrc, "texture_output::fs_gray_uint_fsrc")),
                                               "texture_output::fs_program_gray_uint");

    if(!_fs_program_gray_uint) {
        throw (std::runtime_error("texture_output::texture_output(): error generating _fs_program_gray_uint program"));
    }
    _fs_program_gray_uint->uniform_sampler("in_texture", 0);
    _fs_program_gray_uint->uniform("mvp", pass_mvp);

    _fs_program_gray_uint8_bit_rev = device->create_program(list_of(device->create_shader(scm::gl::STAGE_VERTEX_SHADER,   fs_vsrc,                    "texture_output::fs_vsrc"))
                                                                   (device->create_shader(scm::gl::STAGE_FRAGMENT_SHADER, fs_gray_uint8_bit_rev_fsrc, "texture_output::fs_gray_uint8_bit_rev_fsrc")),
                                               "texture_output::fs_program_gray_uint8_bit_rev");

    if(!_fs_program_gray_uint8_bit_rev) {
        throw (std::runtime_error("texture_output::texture_output(): error generating _fs_program_gray_uint8_bit_rev program"));
    }
    _fs_program_gray_uint8_bit_rev->uniform_sampler("in_texture", 0);
    _fs_program_gray_uint8_bit_rev->uniform("mvp", pass_mvp);
    _fs_program_gray_uint8_bit_rev->uniform("in_scale", vec4f(1.0f / 255.0f));

    _filter_nearest    = device->create_sampler_state(FILTER_MIN_MAG_NEAREST, WRAP_CLAMP_TO_EDGE);
    _dstate_no_z_write = device->create_depth_stencil_state(false, false);
    _rstate_cull_back  = device->create_rasterizer_state(FILL_SOLID, CULL_BACK);
    _bstate_no_blend   = device->create_blend_state(false, FUNC_ONE, FUNC_ZERO, FUNC_ONE, FUNC_ZERO);
}
Example #2
0
coordinate_cross::coordinate_cross(const gl::render_device_ptr& device,
                                   const float                  line_length)
{
    using namespace scm;
    using namespace scm::gl;
    using namespace scm::math;
    using boost::assign::list_of;

    size_t  num_vertices = 6 * 2; // 6 lines, 2 vertices each
    
    _vertices = device->create_buffer(BIND_VERTEX_BUFFER, USAGE_STREAM_DRAW, num_vertices * sizeof(vertex), 0);

    render_context_ptr ctx = device->main_context();
    {
        vertex* data = static_cast<vertex*>(ctx->map_buffer(_vertices, ACCESS_WRITE_INVALIDATE_BUFFER));

        if (data) {
            float l = line_length;
            int v = 0;
            // pos x
            data[v].pos = vec3f( 0.0f, 0.0f, 0.0f); data[v].col = vec3f(1.0f, 0.0f, 0.0f); ++v;
            data[v].pos = vec3f( l,    0.0f, 0.0f); data[v].col = vec3f(1.0f, 0.0f, 0.0f); ++v;
            // neg x
            data[v].pos = vec3f( 0.0f, 0.0f, 0.0f); data[v].col = vec3f(0.3f, 0.0f, 0.0f); ++v;
            data[v].pos = vec3f(-l,    0.0f, 0.0f); data[v].col = vec3f(0.3f, 0.0f, 0.0f); ++v;
            // pos y
            data[v].pos = vec3f(0.0f, 0.0f, 0.0f);  data[v].col = vec3f(0.0f, 1.0f, 0.0f); ++v;
            data[v].pos = vec3f(0.0f, l,    0.0f);  data[v].col = vec3f(0.0f, 1.0f, 0.0f); ++v;
            // neg y
            data[v].pos = vec3f(0.0f,  0.0f, 0.0f); data[v].col = vec3f(0.0f, 0.3f, 0.0f); ++v;
            data[v].pos = vec3f(0.0f, -l,    0.0f); data[v].col = vec3f(0.0f, 0.3f, 0.0f); ++v;
            // pos z
            data[v].pos = vec3f(0.0f, 0.0f, 0.0f);  data[v].col = vec3f(0.0f, 0.0f, 1.0f); ++v;
            data[v].pos = vec3f(0.0f, 0.0f, l);     data[v].col = vec3f(0.0f, 0.0f, 1.0f); ++v;
            // neg z
            data[v].pos = vec3f(0.0f, 0.0f,  0.0f); data[v].col = vec3f(0.0f, 0.0f, 0.3f); ++v;
            data[v].pos = vec3f(0.0f, 0.0f, -l);    data[v].col = vec3f(0.0f, 0.0f, 0.3f); ++v;
        }
        else {
            scm::err() << "coordinate_cross::coordinate_cross(): error mapping vertex buffer for vertex update." << log::end;
            throw (std::runtime_error("coordinate_cross::coordinate_cross(): error mapping vertex buffer for vertex update."));
        }

        ctx->unmap_buffer(_vertices);

        _vertex_count  = 12;
        _prim_topology = PRIMITIVE_LINE_LIST;
    }

    _coord_program = device->create_program(list_of(device->create_shader(STAGE_VERTEX_SHADER, wire_v_source))
                                                   (device->create_shader(STAGE_FRAGMENT_SHADER, wire_f_source)));

    _vertex_array = device->create_vertex_array(vertex_format(0, 0, TYPE_VEC3F, sizeof(vertex))  // vertex positions
                                                             (0, 3, TYPE_VEC3F, sizeof(vertex)), // vertex colors
                                                list_of(_vertices));

    if (   !_coord_program) {
        scm::err() << "coordinate_cross::coordinate_cross(): error creating shader programs." << log::end;
        throw (std::runtime_error("coordinate_cross::coordinate_cross(): error creating shader programs."));
    }

    _no_blend       = device->create_blend_state(false, FUNC_ONE, FUNC_ZERO, FUNC_ONE, FUNC_ZERO);
    _dstate_less    = device->create_depth_stencil_state(true, true, COMPARISON_LESS);
    _dstate_overlay = device->create_depth_stencil_state(false, false, COMPARISON_LESS);
    _raster_no_cull = device->create_rasterizer_state(FILL_SOLID, CULL_NONE, ORIENT_CCW, true);

    if (   !_no_blend
        || !_dstate_less
        || !_raster_no_cull) {
        scm::err() << "coordinate_cross::coordinate_cross(): error creating state objects." << log::end;
        throw (std::runtime_error("coordinate_cross::coordinate_cross(): error creating state objects."));
    }
}