Changeset 139


Ignore:
Timestamp:
06/29/13 20:32:29 (12 years ago)
Author:
epyon
Message:
  • program - set_opt_uniform added
  • example extended to feature per-pixel lighting, self-illumination mapping, specular mapping and dual custom colors mapping
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/interface/program.hh

    r111 r139  
    155155                        // }
    156156                        ((uniform<T>*)( base ))->set_value( value );
     157                }
     158
     159                template < typename T >
     160                void set_opt_uniform( const string& name, const T& value )
     161                {
     162                        uniform_base* base = try_get_uniform( name );
     163                        if ( base != nullptr )
     164                        {
     165                                // restore typechecking, but remember to accept int for float!
     166                                // if ( /* base->get_type() != type_to_enum<T>::type */ )
     167                                // {
     168                                //              NV_LOG( LOG_ERROR, "Uniform '" << name << "' not found in program!" );
     169                                //              return;
     170                                // }
     171                                ((uniform<T>*)( base ))->set_value( value );
     172                        }
    157173                }
    158174        protected:
  • trunk/tests/objload_test/obj.frag

    r137 r139  
    11#version 120
    2 varying vec2 f_texcoord;
    3 varying float f_diffuse_value;
    4 uniform sampler2D tex;
     2
     3uniform sampler2D diffuse;
     4uniform sampler2D specular;
     5uniform vec4 light_diffuse;
     6uniform vec4 light_specular;
     7uniform vec3 custom_color_1;
     8uniform vec3 custom_color_2;
     9
     10varying vec2 v_texcoord;
     11varying vec3 v_normal;
     12varying vec3 v_light_vector;
     13varying vec3 v_view_vector;
    514 
    615void main(void) {
    7         gl_FragColor = vec4( texture2D( tex, f_texcoord ).rgb * f_diffuse_value, 1.0 );
     16        vec3 nnormal         = normalize( v_normal );
     17        vec3 nlight_vector   = normalize( v_light_vector );
     18        vec3 nview_vector    = normalize( v_view_vector );
     19        vec3 nreflect_vector = reflect( -nlight_vector, nnormal );
     20
     21        float specular_value = clamp( dot( nreflect_vector, nview_vector ), 0.0, 1.0 );
     22        specular_value       = pow( specular_value, 6.0 );
     23        float diffuse_value  = max( dot( nlight_vector, nnormal ), 0.0 );
     24
     25        vec3 diff_texel      = vec3( texture2D( diffuse, v_texcoord ) );       
     26        vec4 spec_texel      = texture2D( specular, v_texcoord );
     27       
     28        float specular_amount = spec_texel.x;
     29        float diffuse_amount  = 1.0 - specular_amount;
     30
     31        vec3 custom_color     = (custom_color_1 * (1.0-spec_texel.z) + custom_color_2 * (1.0-spec_texel.a))*0.4;
     32
     33        float final_specular = specular_amount * specular_value;
     34        float final_diffuse  = diffuse_amount * diffuse_value;
     35
     36        vec3 self_ilum_color = spec_texel.y * diff_texel;
     37        vec3 diffuse_color   = light_diffuse.xyz * final_diffuse * clamp( diff_texel + custom_color, 0.0, 1.0 );
     38        vec3 specular_color  = light_specular.xyz * final_specular;
     39
     40        gl_FragColor = vec4( max( self_ilum_color, diffuse_color + specular_color), 1.0 );
    841}
  • trunk/tests/objload_test/obj.vert

    r137 r139  
    11#version 120
     2
    23attribute vec3 position;
    34attribute vec2 texcoord;
    45attribute vec3 normal;
    5 varying vec2 f_texcoord;
    6 varying float f_diffuse_value;
     6
     7varying vec3 v_normal;
     8varying vec3 v_light_vector;
     9varying vec3 v_view_vector;
     10varying vec2 v_texcoord;
     11
    712uniform mat4 matrix_mvp;
    8 uniform vec3 light;
     13uniform mat4 nv_m_modelview;
     14uniform mat4 nv_m_projection;
     15uniform mat3 nv_m_normal;
     16uniform vec3 light_position;
    917
    1018void main(void) {
    11         vec3 vnormal    = normalize(normal);
    12         vec3 vlight     = normalize(light - position);
    13         float diffuse   = max(dot(vlight, vnormal), 0.0);
    14         f_diffuse_value = diffuse;
    15         f_texcoord      = texcoord;
    16         gl_Position     = matrix_mvp * vec4(position, 1.0);
     19        vec4 vertex     = vec4( position, 1.0 );
     20        vec3 eye_pos    = vec3( nv_m_modelview * vertex );
     21        v_normal        = normalize( nv_m_normal * normal );
     22        v_light_vector  = vec3( normalize( light_position - eye_pos ) );
     23        v_view_vector   = vec3( normalize( -eye_pos ) );
     24
     25        v_texcoord      = texcoord;
     26        gl_Position     = matrix_mvp * vertex;
    1727}
  • trunk/tests/objload_test/objload_test.cc

    r137 r139  
    2424        ~application();
    2525protected:
    26         nv::device* m_device;
    27         nv::window* m_window;
    28         nv::texture2d* m_texture;
    29         nv::clear_state m_clear_state;
    30         nv::render_state m_render_state;
     26        nv::device*       m_device;
     27        nv::window*       m_window;
     28        nv::texture2d*    m_diffuse;
     29        nv::texture2d*    m_specular;
     30        nv::clear_state   m_clear_state;
     31        nv::render_state  m_render_state;
    3132
    3233        nv::vertex_array* m_va;
     
    4041        m_device = new nv::gl_device();
    4142        m_window = m_device->create_window( 800, 600 );
     43        nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::REPEAT );
    4244       
    4345        nv::image_data* sprites = m_device->create_image_data( "diffuse.png" );
    44         nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::REPEAT );
    45         m_texture = m_device->create_texture2d( sprites->get_size(), nv::RGBA, nv::UBYTE, sampler, (void*)sprites->get_data() );
     46        m_diffuse  = m_device->create_texture2d( sprites->get_size(), nv::RGBA, nv::UBYTE, sampler, (void*)sprites->get_data() );
     47        delete sprites;
     48
     49        sprites    = m_device->create_image_data( "specular.png" );
     50        m_specular = m_device->create_texture2d( sprites->get_size(), nv::RGBA, nv::UBYTE, sampler, (void*)sprites->get_data() );
    4651        delete sprites;
    4752
     
    8691                glm::mat4 projection = glm::perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f);
    8792                glm::mat4 mv         = view * model;
     93                glm::mat3 normal_matrix = glm::transpose(glm::inverse(glm::mat3(mv)));
    8894
    89                 m_texture->bind( 0 );
     95                m_diffuse->bind( 0 );
     96                m_specular->bind( 1 );
     97                m_program->set_opt_uniform( "nv_m_modelview", mv );
     98                m_program->set_opt_uniform( "nv_m_projection", projection );
     99                m_program->set_opt_uniform( "nv_m_normal", normal_matrix );
    90100                m_program->set_uniform( "matrix_mvp", projection * mv );
    91                 m_program->set_uniform( "light", glm::vec3(8.5, 8.5, 0) );
    92                 m_program->set_uniform( "tex", 0 );
     101                m_program->set_uniform( "light_position", glm::vec3(12.0, 12.0, 0) );
     102                m_program->set_uniform( "light_diffuse",  glm::vec4(0.7,0.7,0.7,1.0) );
     103                m_program->set_uniform( "light_specular", glm::vec4(1.0,1.0,1.0,1.0) );
     104                m_program->set_uniform( "custom_color_1", glm::vec3(1.0,0.0,0.0) );
     105                m_program->set_uniform( "custom_color_2", glm::vec3(0.0,0.0,1.0) );
     106                m_program->set_uniform( "diffuse", 0 );
     107                m_program->set_uniform( "specular", 1 );
    93108                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_program, m_va, m_count * 3 );
    94109                m_window->swap_buffers();
     
    127142        delete m_program;
    128143        delete m_mesh;
    129         delete m_texture;
     144        delete m_diffuse;
     145        delete m_specular;
    130146        delete m_window;
    131147        delete m_device;
Note: See TracChangeset for help on using the changeset viewer.