CG_Lession

struct CGStruct
{
 CGparameter modelViewProj ;
 CGparameter modelViewInverse ;
 CGparameter globalAmbient ;
 CGparameter lightColor ;
 CGparameter lightPosition ;
 CGparameter eyePosition ;
 CGparameter Ke ;
 CGparameter Ka ;
 CGparameter Kd ;
 CGparameter Ks ;
 CGparameter shininess ;
}CGP;

Vertex g_pVertexArray[24] =
{
//    tu   tv     r    g    b    a      nx    ny    nz       x     y     z
    // Front face
    { 0.0f,0.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f, 0.0f, 1.0f,  -1.0f,-1.0f, 1.0f },
    { 1.0f,0.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f, 0.0f, 1.0f,   1.0f,-1.0f, 1.0f },
    { 1.0f,1.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f, 0.0f, 1.0f,   1.0f, 1.0f, 1.0f },
    { 0.0f,1.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f, 0.0f, 1.0f,  -1.0f, 1.0f, 1.0f },
    // Back face
    { 1.0f,0.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f, 0.0f,-1.0f,  -1.0f,-1.0f,-1.0f },
    { 1.0f,1.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f, 0.0f,-1.0f,  -1.0f, 1.0f,-1.0f },
    { 0.0f,1.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f, 0.0f,-1.0f,   1.0f, 1.0f,-1.0f },
    { 0.0f,0.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f, 0.0f,-1.0f,   1.0f,-1.0f,-1.0f },
    // Top face
    { 0.0f,1.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f, 1.0f, 0.0f,  -1.0f, 1.0f,-1.0f },
    { 0.0f,0.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f, 1.0f, 0.0f,  -1.0f, 1.0f, 1.0f },
    { 1.0f,0.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f, 1.0f, 0.0f,   1.0f, 1.0f, 1.0f },
    { 1.0f,1.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f, 1.0f, 0.0f,   1.0f, 1.0f,-1.0f },
    // Bottom face
    { 1.0f,1.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f,-1.0f, 0.0f,  -1.0f,-1.0f,-1.0f },
    { 0.0f,1.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f,-1.0f, 0.0f,   1.0f,-1.0f,-1.0f },
    { 0.0f,0.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f,-1.0f, 0.0f,   1.0f,-1.0f, 1.0f },
    { 1.0f,0.0f,  1.0f,1.0f,1.0f,1.0f,  0.0f,-1.0f, 0.0f,  -1.0f,-1.0f, 1.0f },
    // Right face
    { 1.0f,0.0f,  1.0f,1.0f,1.0f,1.0f,  1.0f, 0.0f, 0.0f,   1.0f,-1.0f,-1.0f },
    { 1.0f,1.0f,  1.0f,1.0f,1.0f,1.0f,  1.0f, 0.0f, 0.0f,   1.0f, 1.0f,-1.0f },
    { 0.0f,1.0f,  1.0f,1.0f,1.0f,1.0f,  1.0f, 0.0f, 0.0f,   1.0f, 1.0f, 1.0f },
    { 0.0f,0.0f,  1.0f,1.0f,1.0f,1.0f,  1.0f, 0.0f, 0.0f,   1.0f,-1.0f, 1.0f },
    // Left face
    { 0.0f,0.0f,  1.0f,1.0f,1.0f,1.0f, -1.0f, 0.0f, 0.0f,  -1.0f,-1.0f,-1.0f },
    { 1.0f,0.0f,  1.0f,1.0f,1.0f,1.0f, -1.0f, 0.0f, 0.0f,  -1.0f,-1.0f, 1.0f },
    { 1.0f,1.0f,  1.0f,1.0f,1.0f,1.0f, -1.0f, 0.0f, 0.0f,  -1.0f, 1.0f, 1.0f },
    { 0.0f,1.0f,  1.0f,1.0f,1.0f,1.0f, -1.0f, 0.0f, 0.0f,  -1.0f, 1.0f,-1.0f }
};

void initShader(void)
{
 //
 // Search for a valid vertex shader profile in this order:
 //
 // CG_PROFILE_ARBVP1 - GL_ARB_vertex_program
 // CG_PROFILE_VP30   - GL_NV_vertex_program2
 // CG_PROFILE_VP20   - GL_NV_vertex_program
 //

   if( cgGLIsProfileSupported(CG_PROFILE_ARBVP1) )
        g_CGprofile = CG_PROFILE_ARBVP1;
    else
  if( cgGLIsProfileSupported(CG_PROFILE_VP30) )
        g_CGprofile = CG_PROFILE_VP30;
 else if( cgGLIsProfileSupported(CG_PROFILE_VP20) )
        g_CGprofile = CG_PROFILE_VP20;
    else
    {
        MessageBox( NULL,"Failed to initialize vertex shader! Hardware doesn't "
           "support any of the vertex shading extensions!",
           "ERROR",MB_OK|MB_ICONEXCLAMATION );
  return;
    }

 // Create the context...
 g_CGcontext = cgCreateContext();

 //
 // Create the vertex shader...
 //
 
 g_CGprogram = cgCreateProgramFromFile( g_CGcontext,
             CG_SOURCE,
             "MyCG.cg",
             g_CGprofile,
             NULL,
             NULL );

 //
 // Load the program using Cg's expanded interface...
 //

 cgGLLoadProgram( g_CGprogram );

 //
 // Bind some parameters by name so we can set them later...
 //
 CGP.modelViewProj = cgGetNamedParameter( g_CGprogram, "modelViewProj" );
 CGP.modelViewInverse = cgGetNamedParameter( g_CGprogram, "modelViewInverse" );
 CGP.eyePosition = cgGetNamedParameter( g_CGprogram, "eyePosition" );
 CGP.Ka = cgGetNamedParameter( g_CGprogram, "Ka" );
 CGP.Kd = cgGetNamedParameter( g_CGprogram, "Kd" );
 CGP.Ke = cgGetNamedParameter( g_CGprogram, "Ke" );
 CGP.Ks = cgGetNamedParameter( g_CGprogram, "Ks" );
 CGP.lightColor = cgGetNamedParameter( g_CGprogram, "lightColor" );
 CGP.lightPosition = cgGetNamedParameter( g_CGprogram, "lightPosition" );
 CGP.shininess= cgGetNamedParameter( g_CGprogram, "shininess" );

}

void setShaderConstants( void )
{
 // This matrix will be used to transform the vertices from model-space to clip-space
 cgGLSetStateMatrixParameter(  CGP.modelViewProj,
            CG_GL_MODELVIEW_PROJECTION_MATRIX,
            CG_GL_MATRIX_IDENTITY );

 // This matrix will be used to transform the normals from model-space to view-space

 cgGLSetStateMatrixParameter( CGP.modelViewInverse ,
           CG_GL_MODELVIEW_MATRIX,
                                CG_GL_MATRIX_INVERSE_TRANSPOSE );

 typedef  float  float3[3] ;
 float3  globalAmbient={ 0.8, 0.8, 0.8 } ;
 float3 lightColor = { 1.0,0.0,0.0 } ;
 float3 Ke ={ 0.8,0.8,0.8} ;
 float3 Ka ={ 1.0,1.0,1.0} ;
 float3 Ks ={ 0.8,0.0,0.5} ;
 float3 Kd ={ 1.0,1.0,1.0} ;
 float shininess[]={5.0f };

 cgGLSetParameter4fv( CGP.globalAmbient ,globalAmbient  );
 cgGLSetParameter4fv( CGP.lightColor , lightColor );
 cgGLSetParameter4fv( CGP.Ka ,Ka  );
 cgGLSetParameter4fv( CGP.Ke ,Ke  );
 cgGLSetParameter4fv( CGP.Ks , Ks );
 cgGLSetParameter4fv( CGP.Kd , Kd );
 cgGLSetParameter4fv( CGP.shininess , shininess );
}

void render( void )
{
 // Clear the screen and the depth buffer
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
 
 glMatrixMode( GL_MODELVIEW );
 glLoadIdentity();
 glTranslatef( 0.0f, 0.0f, -5.0f );
// glRotatef( -g_fSpinY, 1.0f, 0.0f, 0.0f );
// glRotatef( -g_fSpinX, 0.0f, 1.0f, 0.0f );

 setShaderConstants();

 cgGLBindProgram( g_CGprogram );
 cgGLEnableProfile( g_CGprofile );

    glInterleavedArrays( GL_T2F_C4F_N3F_V3F, 0, g_pVertexArray );
    glDrawArrays( GL_QUADS, 0, 24 );

 cgGLDisableProfile( g_CGprofile );

 SwapBuffers( g_hDC );
}

void shutDown(void) 
{

 cgDestroyProgram(g_CGprogram);
 cgDestroyContext(g_CGcontext);

 if( g_hRC != NULL )
 {
  wglMakeCurrent( NULL, NULL );
  wglDeleteContext( g_hRC );
  g_hRC = NULL;       
 }

 if( g_hDC != NULL )
 {
  ReleaseDC( g_hWnd, g_hDC );
  g_hDC = NULL;
 }
}

MyCG.cg :

void main(float4 position  : POSITION,
       float3 normal    : NORMAL ,

       out float4 oPosition : POSITION,
       out float4 color     : COLOR,

       uniform float4x4   modelViewProj,
       uniform float4x4   modelViewInverse,
       uniform float3  globalAmbient,
       uniform float3  lightColor,
       uniform float3  lightPosition,
       uniform float3  eyePosition,
       uniform float3  Ke,
       uniform float3  Ka,
       uniform float3  Kd,
       uniform float3  Ks,
       uniform float   shininess)
{
// position.z=0.0 ;
 oPosition = mul( modelViewProj , position ) ;
 
 float3 P = position.xyz ;
 float3 N = normal ;
 
 float3 emissive = Ke ;
 
 float3 ambient = Ka * globalAmbient ;

 float3 L = normalize(lightPosition-P ) ;
 float diffuseLight =  max( dot(N,L),0 );
 color=float4( 0.0,0.0,0.0,0.0 ) ;
 if(diffuseLight>0.0)
 {
  float3 diffuse = Kd * lightColor * diffuseLight ;
       
  float3 V = normalize( eyePosition - P ) ;
  float3 H = normalize( L+V ) ;
  float3 specular = Ks * lightColor * pow( max(dot(N,H) ,0),shininess ) ;

  color.xyz=diffuse + specular ;
 }

 color.xyz  =color.xyz + ambient+emissive ;
 color.w = 1 ;

原文地址:https://www.cnblogs.com/sunkang/p/2038840.html