zoukankan      html  css  js  c++  java
  • 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 ;

  • 相关阅读:
    java中的类修饰符、成员变量修饰符、方法修饰符。
    java集合框架
    计算字符串最后一个单词的长度,单词以空格隔开。 java算法
    写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。java算法
    在使用 interface 声明一个接口时,只可以使用那个修饰符修饰该接口?
    初始化数组的三种方式
    Evolved Policy Gradients
    Improving Generalization in Meta Reinforcement Learning using Learned Objectives
    RL^2: Fast Reinforcement Learning via Slow Reinforcement Learning
    How to Construct Deep Recurrent Neural Networks
  • 原文地址:https://www.cnblogs.com/sunkang/p/2038840.html
Copyright © 2011-2022 走看看