- Timestamp:
- 06/29/13 20:32:29 (12 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/interface/program.hh
r111 r139 155 155 // } 156 156 ((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 } 157 173 } 158 174 protected: -
trunk/tests/objload_test/obj.frag
r137 r139 1 1 #version 120 2 varying vec2 f_texcoord; 3 varying float f_diffuse_value; 4 uniform sampler2D tex; 2 3 uniform sampler2D diffuse; 4 uniform sampler2D specular; 5 uniform vec4 light_diffuse; 6 uniform vec4 light_specular; 7 uniform vec3 custom_color_1; 8 uniform vec3 custom_color_2; 9 10 varying vec2 v_texcoord; 11 varying vec3 v_normal; 12 varying vec3 v_light_vector; 13 varying vec3 v_view_vector; 5 14 6 15 void 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 ); 8 41 } -
trunk/tests/objload_test/obj.vert
r137 r139 1 1 #version 120 2 2 3 attribute vec3 position; 3 4 attribute vec2 texcoord; 4 5 attribute vec3 normal; 5 varying vec2 f_texcoord; 6 varying float f_diffuse_value; 6 7 varying vec3 v_normal; 8 varying vec3 v_light_vector; 9 varying vec3 v_view_vector; 10 varying vec2 v_texcoord; 11 7 12 uniform mat4 matrix_mvp; 8 uniform vec3 light; 13 uniform mat4 nv_m_modelview; 14 uniform mat4 nv_m_projection; 15 uniform mat3 nv_m_normal; 16 uniform vec3 light_position; 9 17 10 18 void 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; 17 27 } -
trunk/tests/objload_test/objload_test.cc
r137 r139 24 24 ~application(); 25 25 protected: 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; 31 32 32 33 nv::vertex_array* m_va; … … 40 41 m_device = new nv::gl_device(); 41 42 m_window = m_device->create_window( 800, 600 ); 43 nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::REPEAT ); 42 44 43 45 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() ); 46 51 delete sprites; 47 52 … … 86 91 glm::mat4 projection = glm::perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f); 87 92 glm::mat4 mv = view * model; 93 glm::mat3 normal_matrix = glm::transpose(glm::inverse(glm::mat3(mv))); 88 94 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 ); 90 100 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 ); 93 108 m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_program, m_va, m_count * 3 ); 94 109 m_window->swap_buffers(); … … 127 142 delete m_program; 128 143 delete m_mesh; 129 delete m_texture; 144 delete m_diffuse; 145 delete m_specular; 130 146 delete m_window; 131 147 delete m_device;
Note: See TracChangeset
for help on using the changeset viewer.