Ignore:
Timestamp:
05/16/14 05:48:01 (11 years ago)
Author:
epyon
Message:
  • mesh data interface and usage
  • new wavefront importer (old pending removal)
  • updates to test projects
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/render_test/rl.cc

    r231 r238  
    6767        nv::window* m_window;
    6868        nv::texture2d* m_texture;
    69         nv::clear_state m_clear_state;
     69
     70        nv::clear_state  m_clear_state;
    7071        nv::render_state m_render_state;
     72        nv::scene_state  m_scene_state;
    7173
    7274        nv::program* m_char_program;
     
    7577        nv::vertex_array* m_box_va;
    7678        unsigned int m_count;
     79};
     80
     81struct vtx
     82{
     83        nv::i8vec3 position;
     84        nv::i8vec3 color;
     85
     86        vtx( const nv::i8vec3& a_position, const nv::i8vec3& a_color )
     87                : position( a_position ), color( a_color ) {}
    7788};
    7889
     
    99110{
    100111        { // CHARACTER
    101                 nv::mesh cmesh;
    102                 nv::vertex_attribute<nv::i8vec3>::list& vtx = cmesh.add_attribute<nv::i8vec3>("coords")->get();
    103                 nv::vertex_attribute<nv::i8vec3>::list& mat = cmesh.add_attribute<nv::i8vec3>("material")->get();
     112                std::vector< vtx > v;
    104113                int m = 16;     int x = 0; int y = 0; int h = 0;
    105                 vtx.emplace_back( x,   h, y ); 
    106                 vtx.emplace_back( x,   h, y+1 );
    107                 vtx.emplace_back( x+1, h, y+1 );
    108                 vtx.emplace_back( x+1, h, y+1 );
    109                 vtx.emplace_back( x+1, h, y ); 
    110                 vtx.emplace_back( x,   h, y ); 
    111                 mat.insert( mat.end(), 6, nv::i8vec3( m, 1, 0 ) );
     114                nv::i8vec3 mt( m, 1, 0 );
     115                v.emplace_back( nv::i8vec3( x,   h, y ), mt ); 
     116                v.emplace_back( nv::i8vec3( x,   h, y+1 ), mt );
     117                v.emplace_back( nv::i8vec3( x+1, h, y+1 ), mt );
     118                v.emplace_back( nv::i8vec3( x+1, h, y+1 ), mt );
     119                v.emplace_back( nv::i8vec3( x+1, h, y ), mt ); 
     120                v.emplace_back( nv::i8vec3( x,   h, y ), mt ); 
    112121                m_char_program = m_device->create_program( nv::slurp( "char.vert" ), nv::slurp( "char.frag" ) );
    113                 m_char_va      = m_device->create_vertex_array( &cmesh, &(m_char_program->get_attributes()), nv::STATIC_DRAW );
     122                m_char_va      = m_device->create_vertex_array( v, nv::STATIC_DRAW );
    114123        }
    115124
    116125        { // WORLD
    117                 nv::mesh wmesh;
    118                 nv::vertex_attribute<nv::i8vec3>::list& vtx = wmesh.add_attribute<nv::i8vec3>("coords")->get();
    119                 nv::vertex_attribute<nv::i8vec3>::list& mat = wmesh.add_attribute<nv::i8vec3>("material")->get();
    120 
     126                std::vector< vtx > v;
     127                nv::i8vec3 mt;
    121128                for (int i = 0; i < size_x * size_y; ++i )
    122129                {
     
    124131                        int y = i / size_x;
    125132
    126                         vtx.emplace_back( x,   height[i], y   );
    127                         vtx.emplace_back( x,   height[i], y+1 );
    128                         vtx.emplace_back( x+1, height[i], y+1 );
    129                         vtx.emplace_back( x+1, height[i], y+1 );
    130                         vtx.emplace_back( x+1, height[i], y );   
    131                         vtx.emplace_back( x,   height[i], y );   
    132                         mat.insert( mat.end(), 6, nv::i8vec3( map[i], 2, 0 ) );
     133                        mt = nv::i8vec3( map[i], 2, 0 );
     134                        v.emplace_back( nv::i8vec3( x,   height[i], y   ), mt );
     135                        v.emplace_back( nv::i8vec3( x,   height[i], y+1 ), mt );
     136                        v.emplace_back( nv::i8vec3( x+1, height[i], y+1 ), mt );
     137                        v.emplace_back( nv::i8vec3( x+1, height[i], y+1 ), mt );
     138                        v.emplace_back( nv::i8vec3( x+1, height[i], y ), mt );
     139                        v.emplace_back( nv::i8vec3( x,   height[i], y ), mt );
    133140
    134141                        if (i > 0 && height[i-1] != height[i])
     
    139146                                {
    140147                                        m_count += 6;
    141                                         vtx.emplace_back( x, h,     y );   
    142                                         vtx.emplace_back( x, h,     y+1 );
    143                                         vtx.emplace_back( x, h+dir, y+1 );
    144                                         vtx.emplace_back( x, h+dir, y+1 );
    145                                         vtx.emplace_back( x, h+dir, y );   
    146                                         vtx.emplace_back( x, h,     y );   
    147                                         mat.insert( mat.end(), 6, nv::i8vec3( m, -dir, 0 ) );
     148                                        mt = nv::i8vec3( m, -dir, 0 );
     149                                        v.emplace_back( nv::i8vec3( x, h,     y ), mt );
     150                                        v.emplace_back( nv::i8vec3( x, h,     y+1 ), mt );
     151                                        v.emplace_back( nv::i8vec3( x, h+dir, y+1 ), mt );
     152                                        v.emplace_back( nv::i8vec3( x, h+dir, y+1 ), mt );
     153                                        v.emplace_back( nv::i8vec3( x, h+dir, y ), mt );
     154                                        v.emplace_back( nv::i8vec3( x, h,     y ), mt );
    148155                                }
    149156                        }
     
    155162                                for ( int h = height[i-size_x]; h != height[i]; h = h + dir )
    156163                                {
    157                                         vtx.emplace_back( x,   h,     y );
    158                                         vtx.emplace_back( x,   h+dir, y );
    159                                         vtx.emplace_back( x+1, h+dir, y );
    160 
    161                                         vtx.emplace_back( x+1, h+dir, y );
    162                                         vtx.emplace_back( x+1, h,     y );
    163                                         vtx.emplace_back( x,   h,     y );
    164                                         mat.insert( mat.end(), 6, nv::i8vec3( m, -3*dir, 0 ) );
     164                                        mt = nv::i8vec3( m, -3*dir, 0 );
     165                                        v.emplace_back( nv::i8vec3( x,   h,     y ), mt );
     166                                        v.emplace_back( nv::i8vec3( x,   h+dir, y ), mt );
     167                                        v.emplace_back( nv::i8vec3( x+1, h+dir, y ), mt );
     168
     169                                        v.emplace_back( nv::i8vec3( x+1, h+dir, y ), mt );
     170                                        v.emplace_back( nv::i8vec3( x+1, h,     y ), mt );
     171                                        v.emplace_back( nv::i8vec3( x,   h,     y ), mt );
    165172                                }
    166173                        }
     
    168175                }
    169176
    170                 m_count       = vtx.size();
     177                m_count       = v.size();
    171178                m_box_program = m_device->create_program( nv::slurp( "box.vert" ), nv::slurp( "box.frag" ) );
    172                 m_box_va      = m_device->create_vertex_array( &wmesh, &(m_box_program->get_attributes()), nv::STATIC_DRAW );
     179                m_box_va      = m_device->create_vertex_array( v, nv::STATIC_DRAW );
    173180        }
    174181
     
    181188
    182189        glm::vec3 move( 0, 0, 0 );
     190        m_scene_state.get_camera().set_perspective(25.0f, 1.0f*800.0f/600.0f, 0.1f, 100.0f);
    183191
    184192        while(!keypress)
    185193        {
     194                m_scene_state.set_model( glm::translate(glm::mat4(1.0f), glm::vec3(-8.5, 0.0, -8.0)) );
     195                m_scene_state.get_camera().set_lookat(glm::vec3(0.0, 20.0, 5.0) + move, glm::vec3(0.0, 4.0, 0.0) + move, glm::vec3(0.0, 1.0, 0.0));
     196
    186197                m_window->get_context()->clear( m_clear_state );
    187 
    188                 glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(-8.5, 0.0, -8.0));
    189                 glm::mat4 view  = glm::lookAt(glm::vec3(0.0, 20.0, 5.0) + move, glm::vec3(0.0, 4.0, 0.0) + move, glm::vec3(0.0, 1.0, 0.0));
    190                 glm::mat4 projection = glm::perspective(25.0f, 1.0f*800.0f/600.0f, 0.1f, 100.0f);
    191                 glm::mat4 mv         = view * model;
    192 
    193198                m_texture->bind( 0 );
    194                 m_box_program->set_uniform( "matrix_mvp", projection * mv );
    195199                m_box_program->set_uniform( "light", glm::vec3(8.5, 4.5, 6.5) + move );
    196                 m_box_program->set_uniform( "tex", 0 );
    197                 m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_box_program, m_box_va, m_count );
    198 
    199                 m_char_program->set_uniform( "matrix_mvp", projection * mv );
     200                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_scene_state, m_box_program, m_box_va, m_count );
     201
    200202                m_char_program->set_uniform( "pos", move + glm::vec3( 8, 4.1, 6 ) );
    201                 m_char_program->set_uniform( "tex", 0 );
    202                 m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_char_program, m_char_va, 6 );
     203                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_scene_state, m_char_program, m_char_va, 6 );
    203204                m_window->swap_buffers();
    204205
Note: See TracChangeset for help on using the changeset viewer.