コード例 #1
0
ファイル: coordinate_cross.cpp プロジェクト: 4og/schism
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."));
    }
}