//------------------------------------------------------------------;
//
//------------------------------------------------------------------;

c_render::c_render () {

}

//------------------------------------------------------------------;
//
//------------------------------------------------------------------;

c_render::~c_render () {

   delete [] e_render;

}

//------------------------------------------------------------------;
//
//------------------------------------------------------------------;

void c_render::m_render ( void ) {


}

//------------------------------------------------------------------;
//
//------------------------------------------------------------------;

void render_mesh ( const nv_mesh    *pmesh, 
                   const ase::model *model, 
                   const vec3       &eye, 
                   const vec3       &light ) {

   int i_times_3;

   e_fx->m_set_mesh_mat(pmesh,model);
   e_fx->m_set_reg_comb();

   glProgramParameter4fNV  ( GL_VERTEX_PROGRAM_NV, 16, light.x, light.y, light.z, 1.0 );  
   glEnable                ( GL_VERTEX_PROGRAM_NV );

   glVertexAttribPointerNV ( 8, 2, GL_FLOAT, 0, pmesh->t );
   glVertexAttribPointerNV ( 3, 3, GL_FLOAT, 0, pmesh->binormal );
   glVertexAttribPointerNV ( 2, 3, GL_FLOAT, 0, pmesh->normal );
   glVertexAttribPointerNV ( 1, 3, GL_FLOAT, 0, pmesh->tangent );
   glVertexAttribPointerNV ( 0, 3, GL_FLOAT, 0, pmesh->v );

   glEnableClientState     ( GL_VERTEX_ATTRIB_ARRAY8_NV );
   glEnableClientState     ( GL_VERTEX_ATTRIB_ARRAY3_NV );
   glEnableClientState     ( GL_VERTEX_ATTRIB_ARRAY2_NV );
   glEnableClientState     ( GL_VERTEX_ATTRIB_ARRAY1_NV );
   glEnableClientState     ( GL_VERTEX_ATTRIB_ARRAY0_NV );

   glDrawElements          ( GL_TRIANGLES, pmesh->numf * 3, GL_UNSIGNED_INT, pmesh->f ); 

   glDisableClientState    ( GL_VERTEX_ATTRIB_ARRAY8_NV );
   glDisableClientState    ( GL_VERTEX_ATTRIB_ARRAY3_NV );
   glDisableClientState    ( GL_VERTEX_ATTRIB_ARRAY2_NV );
   glDisableClientState    ( GL_VERTEX_ATTRIB_ARRAY1_NV );
   glDisableClientState    ( GL_VERTEX_ATTRIB_ARRAY0_NV );

   glDisable               ( GL_VERTEX_PROGRAM_NV );
   glDisable               ( GL_REGISTER_COMBINERS_NV);

    if (e_nv->vis_mode) {

	int i;
        float scale = 0.15f;
        glBegin   (GL_LINES);
        glColor3f (1.0,0.0,0.0);
        for (i = 0; i < pmesh->numv; i++) {

            i_times_3 = i * 3;
            glVertex3fv (&pmesh->v[i_times_3]);
            glVertex3f  (pmesh->v[i_times_3] + pmesh->normal[i_times_3] * scale,
                         pmesh->v[i_times_3 + 1] + pmesh->normal[i_times_3 + 1] * scale,
                         pmesh->v[i_times_3 + 2] + pmesh->normal[i_times_3 + 2] * scale);
        }
        glEnd();
    }
}

//------------------------------------------------------------------;
//
//------------------------------------------------------------------;

void render ( const ase::model *model, 
              const vec3       &eye,
              const vec3       &light) {

   nv_mesh_cit it = e_nv->mesh_array.begin();

   while (it != e_nv->mesh_array.end()) {

      render_mesh (*it, model, eye, light);
      it++;
   }
}

//------------------------------------------------------------------;
//
//------------------------------------------------------------------;

void m_display ( void ) {

   glClear        (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glViewport     (0, 0, e_app->l_width, e_app->l_height);
   glMatrixMode   (GL_MODELVIEW);
   glLoadIdentity ();
   glTranslatef   (0, 0, e_nv->cam_z);
   glMultMatrixf  (e_nv->mv.mat_array);
   vec3 eye = vec3_null;
   vec3 light     (10.0,0.0,0.0);

   render (e_nv->model, eye, light);

   glutSwapBuffers();

}
