Browse Source

CgShader files added

Shalin Shodhan 21 years ago
parent
commit
0d0b75e738

+ 16 - 5
panda/src/effects/Sources.pp

@@ -1,9 +1,8 @@
 #define OTHER_LIBS interrogatedb:c dconfig:c dtoolconfig:m \
                    dtoolutil:c dtoolbase:c dtool:m
-
+#define USE_PACKAGES cg
 #begin lib_target
   #define TARGET effects
-  
   #define LOCAL_LIBS \
     display gobj putil gsgbase linmath \
     mathutil
@@ -11,13 +10,25 @@
   #define COMBINED_SOURCES $[TARGET]_composite1.cxx         
 
   #define SOURCES \
-    config_effects.h lensFlareNode.I lensFlareNode.h
+    config_effects.h \
+    cgShader.I cgShader.h \
+    cgShaderAttrib.I cgShaderAttrib.h \
+    cgShaderContext.I cgShaderContext.h \
+    lensFlareNode.I lensFlareNode.h
     
   #define INCLUDED_SOURCES \
-    config_effects.cxx lensFlareNode.cxx 
+    config_effects.cxx \
+    cgShader.cxx \
+    cgShaderAttrib.cxx \
+    cgShaderContext.cxx \
+    lensFlareNode.cxx 
 
   #define INSTALL_HEADERS \
-    config_effects.h lensFlareNode.I lensFlareNode.h
+    config_effects.h \
+    cgShader.I cgShader.h \
+    cgShaderAttrib.I cgShaderAttrib.h \
+    cgShaderContext.I cgShaderContext.h \
+    lensFlareNode.I lensFlareNode.h
 
   #define IGATESCAN all
 

+ 144 - 0
panda/src/effects/cgShader.I

@@ -0,0 +1,144 @@
+// Filename: cgShader.I
+// Created by:  sshodhan (10Jul04)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////
+//  Function: CgShader::get_name
+//  Access: Public
+//  Description: Every CgShader object has a name
+////////////////////////////////////////////////////////////////////
+INLINE string CgShader::
+get_name() const {
+  return _name;
+}
+
+////////////////////////////////////////////////////////////////////
+//  Function: CgShader::set_param texture
+//  Access: Public
+//  Description: Store texture in the map to associate with 
+//               param name
+////////////////////////////////////////////////////////////////////
+INLINE void CgShader::
+set_param(const string &pname, Texture *t) {
+  _cg_textures[pname] = t;
+}
+
+////////////////////////////////////////////////////////////////////
+//  Function: CgShader::set_param matrix
+//  Access: Public
+//  Description: Store Matrix type and Transform in the map to associate 
+//               with param name
+////////////////////////////////////////////////////////////////////
+INLINE void CgShader::
+set_param(const string &pname, Matrix_Type m, Transform_Type t) {
+  CGMATRIXDEF cgm;
+  cgm.matrix = m;
+  cgm.transform = t;
+  _cg_matrices[pname] = cgm;
+}
+
+////////////////////////////////////////////////////////////////////
+//  Function: CgShader::set_param 1f
+//  Access: Public
+//  Description: Store 1f in the map to associate with 
+//               param name
+////////////////////////////////////////////////////////////////////
+INLINE void CgShader::
+set_param(const string &pname, float p1f) {
+  _cg_params1f[pname] = p1f;
+}
+
+////////////////////////////////////////////////////////////////////
+//  Function: CgShader::set_param 2f
+//  Access: Public
+//  Description: Store 2f in the map to associate with 
+//               param name
+////////////////////////////////////////////////////////////////////
+INLINE void CgShader::
+set_param(const string &pname, LVector2f p2f) {
+  _cg_params2f[pname] = p2f;
+}
+
+////////////////////////////////////////////////////////////////////
+//  Function: CgShader::set_param 3f
+//  Access: Public
+//  Description: Store 3f in the map to associate with 
+//               param name
+////////////////////////////////////////////////////////////////////
+INLINE void CgShader::
+set_param(const string &pname, LVector3f p3f) {
+  _cg_params3f[pname] = p3f;
+}
+
+
+////////////////////////////////////////////////////////////////////
+//  Function: CgShader::set_param 4f
+//  Access: Public
+//  Description: Store 4f in the map to associate with 
+//               param name
+////////////////////////////////////////////////////////////////////
+INLINE void CgShader::
+set_param(const string &pname, LVector4f p4f) {
+  _cg_params4f[pname] = p4f;
+}
+
+
+////////////////////////////////////////////////////////////////////
+//  Function: CgShader::set_param 1d
+//  Access: Public
+//  Description: Store 1d in the map to associate with 
+//               param name
+////////////////////////////////////////////////////////////////////
+INLINE void CgShader::
+set_param(const string &pname, double p1d) {
+  _cg_params1d[pname] = p1d;
+}
+
+////////////////////////////////////////////////////////////////////
+//  Function: CgShader::set_param 2d
+//  Access: Public
+//  Description: Store 2d in the map to associate with 
+//               param name
+////////////////////////////////////////////////////////////////////
+INLINE void CgShader::
+set_param(const string &pname, LVector2d p2d) {
+  _cg_params2d[pname] = p2d;
+}
+
+////////////////////////////////////////////////////////////////////
+//  Function: CgShader::set_param 3d
+//  Access: Public
+//  Description: Store 3d in the map to associate with 
+//               param name
+////////////////////////////////////////////////////////////////////
+INLINE void CgShader::
+set_param(const string &pname, LVector3d p3d) {
+  _cg_params3d[pname] = p3d;
+}
+
+
+////////////////////////////////////////////////////////////////////
+//  Function: CgShader::set_param 4d
+//  Access: Public
+//  Description: Store 4d in the map to associate with 
+//               param name
+////////////////////////////////////////////////////////////////////
+INLINE void CgShader::
+set_param(const string &pname, LVector4d p4d) {
+  _cg_params4d[pname] = p4d;
+}
+

+ 403 - 0
panda/src/effects/cgShader.cxx

@@ -0,0 +1,403 @@
+// Filename: cgShader.cxx
+// Created by:  sshodhan (10Jul04)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#include "pandabase.h"
+
+#ifdef HAVE_CG
+
+#include "cgShader.h"
+#include "config_effects.h"
+TypeHandle CgShader::_type_handle;
+#include <Cg/cg.h>
+
+
+  
+////////////////////////////////////////////////////////////////////
+//     Function: CgShader::init_cg
+//       Access: Published
+//  Description: Create a cgContext. Note that this is completely
+//               different from Panda's CgShaderContext and its
+//               derived GLCgShaderContext (or Dx9CgShaderContext)
+//               This is a Cg API specific context.
+////////////////////////////////////////////////////////////////////
+bool CgShader::
+init_cg() {
+  // Create a new context for our Cg Program(s)
+  cgContext = cgCreateContext();
+  
+  if (cgContext == NULL) {
+    cerr << "COULD NOT CREATE CG CONTEXT" << "\n" ;
+    return false;
+  }
+
+  return true;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShader::load_shaders
+//       Access: Published
+//  Description: this is called after the instantiating a 
+//           CgShaderContext because we need the profiles
+//           which are initiated by a GL or DX specific
+//           CgShaderContext.
+////////////////////////////////////////////////////////////////////
+bool CgShader::
+load_shaders(){
+  
+  // Load And Compile the shaders from file
+  cgVertexProgram = cgCreateProgramFromFile(cgContext, 
+    CG_SOURCE, _vertex_shader.c_str(), cgVertexProfile, "main", 0);
+  cgFragmentProgram = cgCreateProgramFromFile(cgContext, 
+    CG_SOURCE, _fragment_shader.c_str(), cgFragmentProfile, "main", 0);
+  
+  // Validate Success
+  if (cgVertexProgram == NULL) {
+    // We Need To Determine What Went Wrong
+    CGerror Error = cgGetError();
+    printf("VERTEX SHADER ERROR %s",cgGetErrorString(Error));
+    return false;
+  }
+
+  if (cgFragmentProgram == NULL) {
+    // We Need To Determine What Went Wrong
+    CGerror Error = cgGetError();
+    printf("PIXEL SHADER ERROR %s",cgGetErrorString(Error));
+    return false;
+  }
+
+
+  // Now that our programs are loaded we can actually
+  // access their handles with cgGetNamedParameter
+  // The python programmer specifies parameter names and 
+  // handle names when he calles add_param 
+  // So lets go through all the handles and associate them with
+  // CGparameter objects
+
+  HANDLES::const_iterator param_iter; // Use this to go through all params
+    
+  // Matrix handles
+  // Vertex
+  for (param_iter = _vertex_matrix_handles.begin();
+    param_iter !=_vertex_matrix_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgVertexProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type vert_param(param_iter->first, p);
+    _vertex_matrix_params.insert(vert_param);
+  }
+  // Fragment
+   for (param_iter = _fragment_matrix_handles.begin();
+    param_iter != _fragment_matrix_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgFragmentProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type frag_param(param_iter->first, p);
+    _fragment_matrix_params.insert(frag_param);
+
+  }
+
+
+  // Texture handles
+  // Vertex
+  for (param_iter = _vertex_texture_handles.begin();
+    param_iter != _vertex_texture_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgVertexProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type vert_param(param_iter->first, p);
+    _vertex_texture_params.insert(vert_param);
+  }
+  //Fragment
+  for (param_iter = _fragment_texture_handles.begin();
+    param_iter != _fragment_texture_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgFragmentProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type frag_param(param_iter->first, p);
+    _fragment_texture_params.insert(frag_param);
+  }
+
+  // 1F handles
+  // Vertex
+  for (param_iter = _vertex_1f_handles.begin();
+    param_iter != _vertex_1f_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgVertexProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type vert_param(param_iter->first, p);
+    _vertex_1f_params.insert(vert_param);
+  }
+  //Fragment
+  for (param_iter = _fragment_1f_handles.begin();
+    param_iter != _fragment_1f_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgFragmentProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type frag_param(param_iter->first, p);
+    _fragment_1f_params.insert(frag_param);
+  }
+
+
+  // 2F handles
+  // Vertex
+  for (param_iter = _vertex_2f_handles.begin();
+    param_iter != _vertex_2f_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgVertexProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type vert_param(param_iter->first, p);
+    _vertex_2f_params.insert(vert_param);
+  }
+  //Fragment
+  for (param_iter = _fragment_2f_handles.begin();
+    param_iter != _fragment_2f_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgFragmentProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type frag_param(param_iter->first, p);
+    _fragment_2f_params.insert(frag_param);
+  }
+
+  // 3F handles
+  // Vertex
+  for (param_iter = _vertex_3f_handles.begin();
+    param_iter != _vertex_3f_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgVertexProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type vert_param(param_iter->first, p);
+    _vertex_3f_params.insert(vert_param);
+  }
+  //Fragment
+  for (param_iter = _fragment_3f_handles.begin();
+    param_iter != _fragment_3f_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgFragmentProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type frag_param(param_iter->first, p);
+    _fragment_3f_params.insert(frag_param);
+  }
+
+  // 4F handles
+  // Vertex
+  for (param_iter = _vertex_4f_handles.begin();
+    param_iter != _vertex_4f_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgVertexProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type vert_param(param_iter->first, p);
+    _vertex_4f_params.insert(vert_param);
+  }
+  //Fragment
+  for (param_iter = _fragment_4f_handles.begin();
+    param_iter != _fragment_4f_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgFragmentProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type frag_param(param_iter->first, p);
+    _fragment_4f_params.insert(frag_param);
+  }
+
+  // 1D handles
+  // Vertex
+  for (param_iter = _vertex_1d_handles.begin();
+    param_iter != _vertex_1d_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgVertexProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type vert_param(param_iter->first, p);
+    _vertex_1d_params.insert(vert_param);
+  }
+  //Fragment
+  for (param_iter = _fragment_1d_handles.begin();
+    param_iter != _fragment_1d_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgFragmentProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type frag_param(param_iter->first, p);
+    _fragment_1d_params.insert(frag_param);
+  }
+
+
+  // 2D handles
+  // Vertex
+  for (param_iter = _vertex_2d_handles.begin();
+    param_iter != _vertex_2d_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgVertexProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type vert_param(param_iter->first, p);
+    _vertex_2d_params.insert(vert_param);
+  }
+  //Fragment
+  for (param_iter = _fragment_2d_handles.begin();
+    param_iter != _fragment_2d_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgFragmentProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type frag_param(param_iter->first, p);
+    _fragment_2d_params.insert(frag_param);
+  }
+
+  // 3D handles
+  // Vertex
+  for (param_iter = _vertex_3d_handles.begin();
+    param_iter !=  _vertex_3d_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgVertexProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type vert_param(param_iter->first, p);
+    _vertex_3d_params.insert(vert_param);
+  }
+  //Fragment
+  for (param_iter =  _fragment_3d_handles.begin();
+    param_iter !=  _fragment_3d_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgFragmentProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type frag_param(param_iter->first, p);
+    _fragment_3d_params.insert(frag_param);
+  }
+
+  // 4D handles
+  // Vertex
+  for (param_iter = _vertex_4d_handles.begin();
+    param_iter != _vertex_4d_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgVertexProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type vert_param(param_iter->first, p);
+    _vertex_4d_params.insert(vert_param);
+  }
+  //Fragment
+  for (param_iter = _fragment_4d_handles.begin();
+    param_iter != _fragment_4d_handles.end(); param_iter++) {
+    p = cgGetNamedParameter(cgFragmentProgram, param_iter->second.c_str());
+    CGPARAMETER::value_type frag_param(param_iter->first, p);
+    _fragment_4d_params.insert(frag_param);
+  }
+  return true;
+}
+
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShader::Constructor
+//       Access: Published
+//  Description: Use to construct a new CgShader object.
+//           Pass filenames for the vertex and fragment programs
+////////////////////////////////////////////////////////////////////
+CgShader::
+CgShader(const string &name, const string &vertex_shader,
+  const string &fragment_shader) {
+  _name = name;
+  _vertex_shader = vertex_shader;
+  _fragment_shader = fragment_shader;
+  bool res = init_cg();
+}
+
+  
+////////////////////////////////////////////////////////////////////
+//     Function: CgShader::Destructor
+//       Access: Public
+//  Description: We need to clean up the CGcontext
+////////////////////////////////////////////////////////////////////
+CgShader::
+~CgShader() {
+  cgDestroyContext(cgContext); 
+}
+
+
+
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShader::add_param
+//       Access: Published
+//  Description: Add a new parameter
+//           Specify its name, name in the shader program (handle name)
+//           bind type, parameter type and whether its a vertex
+//           or fragment parameter
+////////////////////////////////////////////////////////////////////
+void CgShader:: 
+add_param(const string &pname, const string &handle_name,
+  Param_Type t, Bind_Type b, bool vert_or_frag) {
+  
+/* We have only per-frame binding
+  if (b == BONCE) {
+    _bind_once.push_back(pname);
+  } else if (b == BFRAME) {
+    _bind_frame.push_back(pname);
+  } else if (b == BVERTEX) {
+    _bind_vertex.push_back(pname);
+  }
+*/
+
+  // Add to the <parameter name, handle name> map 
+  // Later, once we have loaded the cg programs
+  // this handles map is traversed and associations
+  // are made with the CGparameter objects
+  // using the cgGetNamedParameter function
+  if (t == P1F) {
+    if (vert_or_frag) {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _vertex_1f_handles.insert(param_handle);
+    } else {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _fragment_1f_handles.insert(param_handle);
+    }
+  }else if (t == P2F) {
+    if (vert_or_frag) {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _vertex_2f_handles.insert(param_handle);
+    } else {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _fragment_2f_handles.insert(param_handle);
+    }
+  }else if (t == P3F) {
+    if (vert_or_frag) {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _vertex_3f_handles.insert(param_handle);
+    } else {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _fragment_3f_handles.insert(param_handle);
+    }
+  }else if (t == P4F) {
+    if (vert_or_frag) {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _vertex_4f_handles.insert(param_handle);
+    } else {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _fragment_4f_handles.insert(param_handle);
+    }
+  }else if (t == P1D) {
+    if (vert_or_frag) {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _vertex_1d_handles.insert(param_handle);
+    } else {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _fragment_1d_handles.insert(param_handle);
+    }
+  }else if (t == P2D) {
+    if (vert_or_frag) {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _vertex_2d_handles.insert(param_handle);
+    } else {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _fragment_2d_handles.insert(param_handle);
+    }
+  }else if (t == P3D) {
+    if (vert_or_frag) {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _vertex_3d_handles.insert(param_handle);
+    } else {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _fragment_3d_handles.insert(param_handle);
+    }
+  }else if (t == P4D) {
+    if (vert_or_frag) {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _vertex_4d_handles.insert(param_handle);
+    } else {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _fragment_4d_handles.insert(param_handle);
+    }
+  } else if (t == PTEXTURE) {
+    if (vert_or_frag) {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _vertex_texture_handles.insert(param_handle);
+    } else {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _fragment_texture_handles.insert(param_handle);
+    }    
+  } else if (t == PMATRIX) {
+    if (vert_or_frag) {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _vertex_matrix_handles.insert(param_handle);
+    } else {
+      HANDLES::value_type param_handle(pname, handle_name);
+      _fragment_matrix_handles.insert(param_handle);
+    }    
+  }
+  
+}
+
+#endif  // HAVE_CG
+

+ 275 - 0
panda/src/effects/cgShader.h

@@ -0,0 +1,275 @@
+// Filename: cgShader.h
+// Created by:  sshodhan (10Jul04)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef CGSHADER_H
+#define CGSHADER_H
+
+#include "pandabase.h"
+
+#ifdef HAVE_CG
+
+#include "luse.h"
+#include "pmap.h"
+#include "typedWritableReferenceCount.h"
+#include "texture.h"
+#include "dcast.h"
+#include <Cg/cg.h>
+
+
+////////////////////////////////////////////////////////////////////
+//       Class : CgShader
+//      Summary: CgShader object initializes shader programs and
+//               sets up the parameters and their values
+//               A GL or DX CgShaderContext uses this object at bind time
+//               to send parameters to a shader program.
+//                  
+//     Detailed: A CgShader object is created with a name and 
+//               file names for a vertex program and fragment program
+//               It is then passed to a CgShaderAttrib::make function
+//               
+//               Passing parameters to your shader programs is done
+//               in two steps:
+//               1) Add the param, specifying:
+//                  a) Its name and corresponding handle in the Cg program
+//                  b) Its type - matrix/ texture/ number(s)
+//                  c) Its bind type: Right now everything is bound per frame
+//                  d) Whether its for a vertex shader or a fragment shader
+//               2) Set its value(s)
+//
+//               We store maps containing associations between
+//               - parameter names and handle names
+//               - parameter names and parameter objects (Cgparameter)
+//               - parameter names and their actual values
+//               It has different named maps for different types of params
+//               There's also a fragment map and a vertex map for each
+//               These maps are populated and then this object gets
+//               pointed to from a CgShaderContext (right now only GL)
+//               The Shadercontext does the actual "sending" of the params.
+//                  
+//               To make a cg shader variable get its values from a Panda 
+//               program we define it as "uniform" in our cg shader program
+//
+//               We refer to the names of the variables in the .cg shader 
+//               program file as "handles" 
+////////////////////////////////////////////////////////////////////
+class EXPCL_PANDAFX CgShader: public TypedWritableReferenceCount {
+
+PUBLISHED:
+
+  enum Bind_Type {
+    BONCE,
+    BFRAME,
+    BVERTEX,
+  };
+
+  enum Param_Type {
+    PMATRIX,
+    PTEXTURE,
+    P1F,
+    P2F,
+    P3F,
+    P4F,
+    P1D,
+    P2D,
+    P3D,
+    P4D,
+  };
+
+// Cg allows us to pass combinatios any of these four matrices 
+// with any of the four transforms enumerated below
+  enum Matrix_Type {
+      MTXMODELVIEW,
+      MTXPROJECTION,
+      MTXTEXTURE,
+      MTXMODELVIEWPROJECTION,
+  };
+
+  enum Transform_Type {
+      TRFIDENTITY,
+      TRFTRANSPOSE,
+      TRFINVERSE,
+      TRFINVERSE_TRANSPOSE,
+  };
+
+  CgShader(const string &name, const string &v_s , const string &f_s);
+
+  // First step for parameters: Add them and specify name, cg hande name, 
+  // type, bind and vertex/fragment( 1 for vertex program 0 for fragment)
+  void add_param(const string &pname, const string &handle_name,
+    Param_Type t, Bind_Type b, bool vert_or_frag);
+  
+  // Overloaded set_param to be used based on your param type
+  INLINE void set_param(const string &pname, Texture *t);
+  INLINE void set_param(const string &pname, Matrix_Type m, Transform_Type t);
+  INLINE void set_param(const string &pname, float p1f);
+  INLINE void set_param(const string &pname, LVector2f p2f);
+  INLINE void set_param(const string &pname, LVector3f p3f);
+  INLINE void set_param(const string &pname, LVector4f p4f);
+  INLINE void set_param(const string &pname, double p1d);
+  INLINE void set_param(const string &pname, LVector2d p2d);
+  INLINE void set_param(const string &pname, LVector3d p3d);
+  INLINE void set_param(const string &pname, LVector4d p4d);
+  
+public:
+  CGcontext cgContext;// A context to hold our Cg program(s)
+  CGprogram cgVertexProgram;// A handle to the vertex shader .cg file 
+  CGprogram cgFragmentProgram;// A handle to the pixel shader .cg file
+  CGprofile cgVertexProfile;// Vertex profile... different features on various cards
+  CGprofile cgFragmentProfile;// Profile for Pixel Shader
+  CGparameter p; // Used to insert Cgparameter objects into the maps
+
+// Matrix parameters need to know type of Matrix and its transform  
+  typedef struct {
+    Matrix_Type matrix;
+    Transform_Type transform;
+  }CGMATRIXDEF;
+
+  typedef pmap<string, string> HANDLES;
+  typedef pmap<string, CGparameter> CGPARAMETER;
+  typedef pmap<string, PT(Texture) > CGTEXTURE;
+  typedef pmap<string, CGMATRIXDEF> CGMATRIX;
+  typedef pmap<string, float> CGPARAM1F;
+  typedef pmap<string, LVector2f> CGPARAM2F;
+  typedef pmap<string, LVector3f> CGPARAM3F;
+  typedef pmap<string, LVector4f> CGPARAM4F;
+  typedef pmap<string, double> CGPARAM1D;
+  typedef pmap<string, LVector2d> CGPARAM2D;
+  typedef pmap<string, LVector3d> CGPARAM3D;
+  typedef pmap<string, LVector4d> CGPARAM4D;
+
+
+  // add_param adds to these maps
+  CGPARAMETER _vertex_1f_params;
+  CGPARAMETER _fragment_1f_params;
+
+  CGPARAMETER _vertex_2f_params;
+  CGPARAMETER _fragment_2f_params;
+ 
+  CGPARAMETER _vertex_3f_params;
+  CGPARAMETER _fragment_3f_params;
+
+  CGPARAMETER _vertex_4f_params;
+  CGPARAMETER _fragment_4f_params;
+  
+  CGPARAMETER _vertex_1d_params;
+  CGPARAMETER _fragment_1d_params;
+
+  CGPARAMETER _vertex_2d_params;
+  CGPARAMETER _fragment_2d_params;
+
+  CGPARAMETER _vertex_3d_params;
+  CGPARAMETER _fragment_3d_params;
+
+  CGPARAMETER _vertex_4d_params;
+  CGPARAMETER _fragment_4d_params;
+
+  CGPARAMETER _vertex_matrix_params;
+  CGPARAMETER _fragment_matrix_params;
+  
+  CGPARAMETER _vertex_texture_params; 
+  CGPARAMETER _fragment_texture_params;
+  
+  HANDLES _vertex_1f_handles;
+  HANDLES _fragment_1f_handles;
+
+  HANDLES _vertex_2f_handles;
+  HANDLES _fragment_2f_handles;
+  
+  HANDLES _vertex_3f_handles;
+  HANDLES _fragment_3f_handles;
+
+  HANDLES _vertex_4f_handles;
+  HANDLES _fragment_4f_handles;
+  
+  HANDLES _vertex_1d_handles;
+  HANDLES _fragment_1d_handles;
+
+  HANDLES _vertex_2d_handles;
+  HANDLES _fragment_2d_handles;
+
+  HANDLES _vertex_3d_handles;
+  HANDLES _fragment_3d_handles;
+
+  HANDLES _vertex_4d_handles;
+  HANDLES _fragment_4d_handles;
+
+  HANDLES _vertex_matrix_handles;
+  HANDLES _fragment_matrix_handles;
+  
+  HANDLES _vertex_texture_handles; 
+  HANDLES _fragment_texture_handles;
+  
+  
+  // set_param adds to these maps
+  CGTEXTURE _cg_textures;
+  CGMATRIX _cg_matrices;
+  CGPARAM1F _cg_params1f;
+  CGPARAM2F _cg_params2f;
+  CGPARAM3F _cg_params3f;
+  CGPARAM4F _cg_params4f;
+  CGPARAM1D _cg_params1d;
+  CGPARAM2D _cg_params2d;
+  CGPARAM3D _cg_params3d;
+  CGPARAM4D _cg_params4d;  
+
+  INLINE string get_name() const;
+  
+  
+  bool init_cg();// This initializes a CGcontext
+  
+  // After a CgShaderContext (GL or DX) has been created
+  // this function loads vertex and fragment programs
+  // from the hard disk.
+  // Only after this is done can we associate our
+  // Cgparameter objects with the handles
+  // The CgShaderContext then loads these programs
+  // into the GPU
+  bool load_shaders();
+  
+  ~CgShader();
+
+  string _name;
+  string _vertex_shader;
+  string _fragment_shader;
+  
+
+public:
+  static TypeHandle get_class_type() {
+    return _type_handle;
+  }
+  static void init_type() {
+    TypedWritableReferenceCount::init_type();
+    register_type(_type_handle, "CgShader",
+                  TypedWritableReferenceCount::get_class_type());
+  }
+  virtual TypeHandle get_type() const {
+    return get_class_type();
+  }
+  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
+
+private:
+  static TypeHandle _type_handle;
+};
+
+#include "cgShader.I"
+
+#endif  // HAVE_CG
+
+#endif
+
+

+ 53 - 0
panda/src/effects/cgShaderAttrib.I

@@ -0,0 +1,53 @@
+// Filename: cgShaderAttrib.I
+// Created by:  sshodhan (10Jul04)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderAttrib::Constructor
+//       Access: Private
+//  Description: Use CgShaderAttrib::make() to construct a new
+//               CgShaderAttrib object.
+////////////////////////////////////////////////////////////////////
+INLINE CgShaderAttrib::
+CgShaderAttrib() {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderAttrib::is_off
+//       Access: Published
+//  Description: Returns true if the CgShaderAttrib is an 'off'
+//               CgShaderAttrib, indicating that it should disable
+//               the previous shader profiles
+////////////////////////////////////////////////////////////////////
+INLINE bool CgShaderAttrib::
+is_off() const {
+  return _cg_shader == (CgShader *)NULL;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderAttrib::get_cg_shader
+//       Access: Published
+//  Description: If the CgShaderAttrib is not an 'off' CgShaderAttrib,
+//               returns the shader that is associated.  Otherwise,
+//               return NULL.
+////////////////////////////////////////////////////////////////////
+INLINE CgShader *CgShaderAttrib::
+get_cg_shader() const {
+  return _cg_shader;
+}
+

+ 199 - 0
panda/src/effects/cgShaderAttrib.cxx

@@ -0,0 +1,199 @@
+// Filename: cgShaderAttrib.cxx
+// Created by:  sshodhan (10Jul04)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#include "pandabase.h"
+
+#ifdef HAVE_CG
+
+#include "cgShaderAttrib.h"
+#include "config_effects.h"
+#include "graphicsStateGuardianBase.h"
+#include "bamReader.h"
+#include "bamWriter.h"
+#include "datagram.h"
+#include "datagramIterator.h"
+
+
+TypeHandle CgShaderAttrib::_type_handle;
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderAttrib::make
+//       Access: Published, Static
+//  Description: Constructs a new CgShaderAttrib object suitable for
+//               process the indicated geometry with shaders
+////////////////////////////////////////////////////////////////////
+CPT(RenderAttrib) CgShaderAttrib::
+make(CgShader *shader) {
+  CgShaderAttrib *attrib = new CgShaderAttrib;
+  attrib->_cg_shader = shader;
+  attrib->_always_reissue = true; // This makes the CgShader node issue every frame
+  return return_new(attrib);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderAttrib::make_off
+//       Access: Published, Static
+//  Description: Constructs a new CgShaderAttrib object suitable for
+//               rendering geometry with no shader interference
+////////////////////////////////////////////////////////////////////
+CPT(RenderAttrib) CgShaderAttrib::
+make_off() {
+  CgShaderAttrib *attrib = new CgShaderAttrib;
+  return return_new(attrib);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderAttrib::issue
+//       Access: Public, Virtual
+//  Description: Calls the appropriate method on the indicated GSG
+//               to issue the graphics commands appropriate to the
+//               given attribute.  This is normally called
+//               (indirectly) only from
+//               GraphicsStateGuardian::set_state() or modify_state().
+////////////////////////////////////////////////////////////////////
+void CgShaderAttrib::
+issue(GraphicsStateGuardianBase *gsg) const {
+  gsg->issue_cg_shader_bind(this);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderAttrib::make_default_impl
+//       Access: Protected, Virtual
+//  Description: Intended to be overridden by derived CgShaderAttrib
+//               types to specify what the default property for a
+//               TexGenAttrib of this type should be.
+//
+//               This should return a newly-allocated CgShaderAttrib of
+//               the same type that corresponds to whatever the
+//               standard default for this kind of CgShaderAttrib is.
+////////////////////////////////////////////////////////////////////
+RenderAttrib *CgShaderAttrib::
+make_default_impl() const {
+  return new CgShaderAttrib;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderAttrib::compare_to_impl
+//       Access: Protected, Virtual
+//  Description: Intended to be overridden by derived CgShaderAttrib
+//               types to return a unique number indicating whether
+//               this CgShaderAttrib is equivalent to the other one.
+//
+//               This should return 0 if the two CgShaderAttrib objects
+//               are equivalent, a number less than zero if this one
+//               should be sorted before the other one, and a number
+//               greater than zero otherwise.
+//
+//               This will only be called with two CgShaderAttrib
+//               objects whose get_type() functions return the same.
+////////////////////////////////////////////////////////////////////
+int CgShaderAttrib::
+compare_to_impl(const RenderAttrib *other) const {
+  const CgShaderAttrib *cgsa;
+  DCAST_INTO_R(cgsa, other, 0);
+  
+  // Comparing pointers by subtraction is problematic.  Instead of
+  // doing this, we'll just depend on the built-in != and < operators
+  // for comparing pointers.
+  if (_cg_shader != cgsa->_cg_shader) {
+    return _cg_shader < cgsa->_cg_shader ? -1 : 1;
+  }
+  return 0;
+}
+
+
+
+  /*
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderAttrib::output
+//       Access: Public, Virtual
+//  Description: 
+////////////////////////////////////////////////////////////////////
+void CgShaderAttrib::
+output(ostream &out) const {
+  out << get_type() << ":";
+  if (is_off()) {
+    out << "(off)";
+  } else {
+    out << _cg_shader->get_name();
+  }
+}
+
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderAttrib::register_with_read_factory
+//       Access: Public, Static
+//  Description: Tells the BamReader how to create objects of type
+//               CgShaderAttrib.
+////////////////////////////////////////////////////////////////////
+void CgShaderAttrib::
+register_with_read_factory() {
+  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderAttrib::write_datagram
+//       Access: Public, Virtual
+//  Description: Writes the contents of this object to the datagram
+//               for shipping out to a Bam file.
+////////////////////////////////////////////////////////////////////
+void CgShaderAttrib::
+write_datagram(BamWriter *manager, Datagram &dg) {
+  RenderAttrib::write_datagram(manager, dg);
+
+  manager->write_pointer(dg, _cg_shader);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderAttrib::make_from_bam
+//       Access: Protected, Static
+//  Description: This function is called by the BamReader's factory
+//               when a new object of type CgShaderAttrib is encountered
+//               in the Bam file.  It should create the CgShaderAttrib
+//               and extract its information from the file.
+////////////////////////////////////////////////////////////////////
+TypedWritable *CgShaderAttrib::
+make_from_bam(const FactoryParams &params) {
+  CgShaderAttrib *attrib = new CgShaderAttrib;
+  DatagramIterator scan;
+  BamReader *manager;
+
+  parse_params(params, scan, manager);
+  attrib->fillin(scan, manager);
+
+  return attrib;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderAttrib::fillin
+//       Access: Protected
+//  Description: This internal function is called by make_from_bam to
+//               read in all of the relevant data from the BamFile for
+//               the new CgShaderAttrib.
+////////////////////////////////////////////////////////////////////
+void CgShaderAttrib::
+fillin(DatagramIterator &scan, BamReader *manager) {
+  RenderAttrib::fillin(scan, manager);
+
+  // Read the _texture pointer.
+  manager->read_pointer(scan);
+}
+*/
+
+#endif  // HAVE_CG

+ 106 - 0
panda/src/effects/cgShaderAttrib.h

@@ -0,0 +1,106 @@
+// Filename: cgShaderAttrib.h
+// Created by:  sshodhan (10Jul04)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef CGSHADERATTRIB_H
+#define CGSHADERATTRIB_H
+
+#include "pandabase.h"
+
+// In case we don't have HAVE_CG defined, we need to have at least a
+// forward reference to the class so the gsg's issue_cg_shader_bind()
+// method can be compiled.
+class CgShaderAttrib;
+
+#ifdef HAVE_CG
+
+#include "luse.h"
+#include "pmap.h"
+#include "cgShader.h"
+#include "renderAttrib.h"
+#include "typedObject.h"
+#include "typedReferenceCount.h"
+#include "pointerTo.h"
+#include "factoryParam.h"
+#include "dcast.h"
+
+
+
+////////////////////////////////////////////////////////////////////
+//       Class : CgShaderAttrib
+// Description : Cg is Nvidia's high level shading language
+//               Setting this attrib on a node will make that node
+//               pass through a vertex and fragment program on the GPU
+//               These programs must be passed through a CgShader object
+//               to the make function of this attrib
+//               All existing states will collapse
+//               You will need to pass in the right matrices
+//               to transform the object
+//               All textures will have to be passed again manually
+//               and applied by the fragment program.
+//               You can also pass other parameters such as floating
+//               point and double precision numbers
+////////////////////////////////////////////////////////////////////
+class EXPCL_PANDAFX CgShaderAttrib: public RenderAttrib {
+
+private:
+  INLINE CgShaderAttrib();
+
+PUBLISHED:
+  static CPT(RenderAttrib) make(CgShader *shader);
+  static CPT(RenderAttrib) make_off();
+
+  INLINE bool is_off() const;
+  INLINE CgShader *get_cg_shader() const;
+
+public:
+  virtual void issue(GraphicsStateGuardianBase *gsg) const;
+
+protected:
+   virtual RenderAttrib *make_default_impl() const;
+   virtual int compare_to_impl(const RenderAttrib *other) const;
+
+private:
+  PT(CgShader) _cg_shader;
+
+public:
+  static TypeHandle get_class_type() {
+    return _type_handle;
+  }
+  static void init_type() {
+    RenderAttrib::init_type();
+    register_type(_type_handle, "CgShaderAttrib",
+                  RenderAttrib::get_class_type());
+  }
+  virtual TypeHandle get_type() const {
+    return get_class_type();
+  }
+  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
+
+private:
+  static TypeHandle _type_handle;
+};
+
+
+#include "cgShaderAttrib.I"
+
+#endif  // HAVE_CG
+
+#endif
+
+
+

+ 139 - 0
panda/src/effects/cgShaderContext.I

@@ -0,0 +1,139 @@
+// Filename: cgShaderContext.I
+// Created by:  sshodhan (20Jul04)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::Constructor
+//       Access: Published
+//  Description: Use CgShaderContext() to construct a new
+//               CgShaderContext object.
+////////////////////////////////////////////////////////////////////
+INLINE CgShaderContext::
+CgShaderContext(PT(CgShader) cg_shader) :
+  _cg_shader(cg_shader) {
+}
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::set_param
+//       Access: Published
+//  Description: Overloaded version to set 1f parameters
+////////////////////////////////////////////////////////////////////
+INLINE void CgShaderContext::
+set_param(const string &pname, const float value, bool vert_or_frag) {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::set_param
+//       Access: Published
+//  Description: Overloaded version to set 1d parameters
+////////////////////////////////////////////////////////////////////
+INLINE void CgShaderContext::
+set_param(const string &pname, const double value, bool vert_or_frag) {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::set_param
+//       Access: Published
+//  Description: Overloaded version to set 2f parameters
+////////////////////////////////////////////////////////////////////
+INLINE void CgShaderContext::
+set_param(const string &pname, const float value1, const float value2,
+  bool vert_or_frag) {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::set_param
+//       Access: Published
+//  Description: Overloaded version to set 2d parameters
+////////////////////////////////////////////////////////////////////
+INLINE void CgShaderContext::
+set_param(const string &pname, const double value1, const double value2,
+  bool vert_or_frag) {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::set_param
+//       Access: Published
+//  Description: Overloaded version to set 3f parameters
+////////////////////////////////////////////////////////////////////
+INLINE void CgShaderContext::
+set_param(const string &pname, const float value1, const float value2,
+  const float value3, bool vert_or_frag) {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::set_param
+//       Access: Published
+//  Description: Overloaded version to set 3d parameters
+////////////////////////////////////////////////////////////////////
+INLINE void CgShaderContext::
+set_param(const string &pname, const double value1, const double value2, 
+  const double value3, bool vert_or_frag) {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::set_param
+//       Access: Published
+//  Description: Overloaded version to set 4f parameters
+////////////////////////////////////////////////////////////////////
+INLINE void CgShaderContext::
+set_param(const string &pname, const float value1, const float value2, 
+  const float value3, const float value4, bool vert_or_frag) {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::set_param
+//       Access: Published
+//  Description: Overloaded version to set 4d parameters
+////////////////////////////////////////////////////////////////////
+INLINE void CgShaderContext::
+set_param(const string &pname, const double value1, const double value2,
+  const double value3, const double value4, bool vert_or_frag) {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::set_param
+//       Access: Published
+//  Description: Overloaded version to set Texture parameters
+////////////////////////////////////////////////////////////////////
+INLINE void CgShaderContext::
+set_param(const string &pname, Texture *t , bool vert_or_frag, GraphicsStateGuardianBase *gsg) {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::enable_texture_param
+//       Access: Published
+//  Description: Enable a Texture parameter
+////////////////////////////////////////////////////////////////////
+INLINE void CgShaderContext::
+enable_texture_param(const string &pname, bool vert_or_frag) {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::enable_texture_param
+//       Access: Published
+//  Description: Disable a Texture parameter
+////////////////////////////////////////////////////////////////////
+INLINE void CgShaderContext::
+disable_texture_param(const string &pname, bool vert_or_frag) {
+}
+
+
+

+ 68 - 0
panda/src/effects/cgShaderContext.cxx

@@ -0,0 +1,68 @@
+// Filename: cgShaderContext.cxx
+// Created by:  sshodhan (20Jul04)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#include "pandabase.h"
+
+#ifdef HAVE_CG
+
+#include "cgShaderContext.h"
+TypeHandle CgShaderContext::_type_handle;
+
+  
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::init_cg_shader_context
+//       Access: Published
+//  Description: To be overridden by derived class to do appropriate
+//               initialization
+////////////////////////////////////////////////////////////////////
+bool CgShaderContext::
+init_cg_shader_context() {
+  return true;
+}
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::bind
+//       Access: Published
+//  Description: Enable the shaders and actually send parameters
+////////////////////////////////////////////////////////////////////
+void CgShaderContext::
+bind(GraphicsStateGuardianBase *gsg){
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::unbind
+//       Access: Published
+//  Description: Disable the shaders and texture parameters if any
+////////////////////////////////////////////////////////////////////
+void CgShaderContext::
+un_bind() {
+}
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: CgShaderContext::set_param
+//       Access: Published
+//  Description: Overloaded version to set Matrix parameters
+////////////////////////////////////////////////////////////////////
+void CgShaderContext::
+set_param(const string &pname, Matrix_Type m, Transform_Type t,
+  bool vert_or_frag) {
+}
+
+#endif  // HAVE_CG

+ 154 - 0
panda/src/effects/cgShaderContext.h

@@ -0,0 +1,154 @@
+// Filename: cgShaderContext.h
+// Created by:  sshodhan (20Jul04)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef CGSHADERCONTEXT_H
+#define CGSHADERCONTEXT_H
+
+#include "pandabase.h"
+
+#ifdef HAVE_CG
+
+#include "cgShader.h"
+#include "luse.h"
+#include "pmap.h"
+#include "texture.h"
+#include "dcast.h"
+#include "pointerTo.h"
+#include "typedReferenceCount.h"
+#include <Cg/cgGL.h>
+
+////////////////////////////////////////////////////////////////////
+//       Class : CgShaderContext
+// Description : This is a base class to be derived by GLCgShaderContext
+//               DX9CgShaderContext and DX8CgShaderContext
+//               The CgShaderContexts will handle all the
+//               DX/GL API specific shader calls
+//               The CgShader object will do all the API generic stuff
+//               The CgShaderAttrib will be made with a CgShader object
+//               The gsgs will map CgShader objects to CgShaderContext
+//               objects. The CgShader objects will be passed through
+//               the CgShaderAttrib objects and will contain parameter
+//               info.
+////////////////////////////////////////////////////////////////////
+class EXPCL_PANDAFX CgShaderContext: public TypedReferenceCount {
+
+PUBLISHED:
+
+  enum Bind_Type {
+    BONCE,
+    BFRAME,
+    BVERTEX,
+  };
+
+  enum Param_Type {
+    PMATRIX,
+    PTEXTURE,
+    PNUMERIC,
+  };
+
+
+  enum Matrix_Type {
+      M_MODELVIEW,
+      M_PROJECTION,
+      M_TEXTURE,
+      M_MODELVIEW_PROJECTION,
+  };
+
+  enum Transform_Type {
+      T_IDENTITY,
+      T_TRANSPOSE,
+      T_INVERSE,
+      T_INVERSE_TRANSPOSE,
+  };
+
+  INLINE CgShaderContext(PT(CgShader) cg_shader);
+
+public:
+
+  // Overloaded functions to do cgGLSetParameter* on various kinds of parameters
+  INLINE virtual void set_param(const string &pname, const float value,
+    bool vert_or_frag);
+
+  INLINE virtual void set_param(const string &pname, const double value, 
+    bool vert_or_frag);
+
+  INLINE virtual void set_param(const string &pname, const float value1, 
+    const float value2, bool vert_or_frag);
+
+  INLINE virtual void set_param(const string &pname, const double value1, 
+    const double value2, bool vert_or_frag);
+
+  INLINE virtual void set_param(const string &pname, const float value1,
+    const float value2, const float value3, bool vert_or_frag);
+
+  INLINE virtual void set_param(const string &pname, const double value1,
+    const double value2, const double value3, bool vert_or_frag);
+
+  INLINE virtual void set_param(const string &pname, const float value1, 
+    const float value2, const float value3, const float value4,
+      bool vert_or_frag);
+
+  INLINE virtual void set_param(const string &pname, const double value1, 
+      const double value2, const double value3, const double value4,
+        bool vert_or_frag);
+
+  INLINE virtual void set_param(const string &pname, Texture *t, bool vert_or_frag, GraphicsStateGuardianBase *gsg);
+
+  virtual void set_param(const string &pname, Matrix_Type m,
+    Transform_Type t, bool vert_or_frag);
+
+
+  INLINE virtual void enable_texture_param(const string &pname, bool vert_or_frag);
+  
+  INLINE virtual void disable_texture_param(const string &pname, bool vert_or_frag);
+    
+  // Bind and unbind shaders and call the set_param functions 
+  // based on the CgShader object 
+  INLINE virtual void bind(GraphicsStateGuardianBase *gsg);
+  INLINE virtual void un_bind();
+
+  virtual bool init_cg_shader_context();
+
+  // Store a pointer to the CgShader object which contains all the parameter info
+  PT(CgShader) _cg_shader;
+
+public:
+  static TypeHandle get_class_type() {
+    return _type_handle;
+  }
+  static void init_type() {
+    TypedReferenceCount::init_type();
+    register_type(_type_handle, "CgShaderContext",
+                  TypedReferenceCount::get_class_type());
+  }
+  virtual TypeHandle get_type() const {
+    return get_class_type();
+  }
+  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
+
+private:
+  static TypeHandle _type_handle;
+};
+
+#include "cgShaderContext.I"
+
+#endif  // HAVE_CG
+
+#endif
+
+

+ 9 - 0
panda/src/effects/config_effects.cxx

@@ -17,8 +17,12 @@
 ////////////////////////////////////////////////////////////////////
 
 #include "config_effects.h"
+#include "cgShader.h"
+#include "cgShaderAttrib.h"
+#include "cgShaderContext.h"
 #include "lensFlareNode.h"
 
+
 #include "dconfig.h"
 
 Configure(config_effects);
@@ -38,6 +42,11 @@ ConfigureFn(config_effects) {
 ////////////////////////////////////////////////////////////////////
 void
 init_libeffects() {
+#ifdef HAVE_CG
+  CgShader::init_type();
+  CgShaderAttrib::init_type();
+  CgShaderContext::init_type();
+#endif
 #if 0  // temporarily disabled until we can port to new scene graph.
   LensFlareNode::init_type();
   LensFlareNode::register_with_read_factory();

+ 3 - 0
panda/src/effects/effects_composite1.cxx

@@ -1,4 +1,7 @@
 
 #include "config_effects.cxx"
+#include "cgShader.cxx"
+#include "cgShaderAttrib.cxx"
+#include "cgShaderContext.cxx"
 #include "lensFlareNode.cxx"