Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

shadermanager.cpp File Reference

#include "gl.h"
#include "glext.h"
#include "shadermanager.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cassert>

Include dependency graph for shadermanager.cpp:

Go to the source code of this file.

Functions

static string BeginLightSourceShadows (const ShaderProperties &props, unsigned int light)
static string DeclareLights (const ShaderProperties &props)
static string DirectionalLight (unsigned int i, const ShaderProperties &props)
static void DumpShaderSource (ostream &out, const std::string &source)
static string FragLightProperty (unsigned int i, char *property)
ShaderManagerGetShaderManager ()
static string IndexedParameter (const char *name, unsigned int index0, unsigned int index1)
static string IndexedParameter (const char *name, unsigned int index)
static string LightDir (unsigned int i)
static string LightProperty (unsigned int i, char *property)
bool operator< (const ShaderProperties &p0, const ShaderProperties &p1)
static string RingShadowTexCoord (unsigned int index)
static string SeparateDiffuse (unsigned int i)
static string SeparateSpecular (unsigned int i)
static string Shadow (unsigned int light, unsigned int shadow)
static string ShadowsForLightSource (const ShaderProperties &props, unsigned int light)
static string TexCoord2D (unsigned int i)

Variables

static const char * errorFragmentShaderSource
static const char * errorVertexShaderSource
ShaderManager g_ShaderManager


Function Documentation

static string BeginLightSourceShadows const ShaderProperties props,
unsigned int  light
[static]
 

Definition at line 496 of file shadermanager.cpp.

References RingShadowTexCoord(), and SeparateDiffuse().

Referenced by ShadowsForLightSource().

00497 {
00498     string source;
00499 
00500     if (props.usesFragmentLighting())
00501     {
00502         if (props.hasShadowsForLight(light))
00503             source += "shadow = 1.0;\n";
00504     }
00505     else
00506     {
00507         source += "shadow = " + SeparateDiffuse(light) + ";\n";
00508     }
00509 
00510     if (props.texUsage & ShaderProperties::RingShadowTexture)
00511     {
00512         source += "shadow *= (1.0 - texture2D(ringTex, vec2(" +
00513             RingShadowTexCoord(light) + ", 0.0)).a);\n";
00514     }
00515 
00516     return source;
00517 }

static string DeclareLights const ShaderProperties props  )  [static]
 

Definition at line 363 of file shadermanager.cpp.

Referenced by ShaderManager::buildRingsVertexShader(), and ShaderManager::buildVertexShader().

00364 {
00365     if (props.nLights == 0)
00366         return string("");
00367 
00368     char lightSourceBuf[128];
00369 
00370 #ifndef USE_GLSL_STRUCTS
00371     string lightSourceDecl;
00372     
00373     for (unsigned int i = 0; i < props.nLights; i++)
00374     {
00375         sprintf(lightSourceBuf,
00376                 "uniform vec3 light%d_direction;\n"
00377                 "uniform vec3 light%d_diffuse;\n"
00378                 "uniform vec3 light%d_specular;\n"
00379                 "uniform vec3 light%d_halfVector;\n",
00380                 i, i, i, i);
00381         lightSourceDecl += string(lightSourceBuf);
00382     }
00383     
00384     return lightSourceDecl;
00385 #else    
00386     sprintf(lightSourceBuf,
00387             "uniform struct {\n"
00388             "   vec3 direction;\n"
00389             "   vec3 diffuse;\n"
00390             "   vec3 specular;\n"
00391             "   vec3 halfVector;\n"
00392             "} lights[%d];\n",
00393             props.nLights);
00394 
00395     return string(lightSourceBuf);
00396 #endif
00397 }

static string DirectionalLight unsigned int  i,
const ShaderProperties props
[static]
 

Definition at line 442 of file shadermanager.cpp.

References LightProperty(), SeparateDiffuse(), and SeparateSpecular().

Referenced by ShaderManager::buildVertexShader().

00443 {
00444     string source;
00445 
00446     source += "nDotVP = max(0.0, dot(gl_Normal, " +
00447         LightProperty(i, "direction") + "));\n";
00448     if (props.lightModel == ShaderProperties::SpecularModel)
00449     {
00450         source += "nDotHV = max(0.0, dot(gl_Normal, " +
00451             LightProperty(i, "halfVector") + "));\n";
00452     }
00453 
00454     if (props.usesFragmentLighting())
00455     {
00456         // Diffuse is computed in the fragment shader when fragment lighting
00457         // is enabled.
00458     }
00459     else if (props.usesShadows())
00460     {
00461         // When there are shadows, we need to track the diffuse contributions
00462         // separately for each light.
00463         source += SeparateDiffuse(i) + " = nDotVP;\n";
00464     }
00465     else
00466     {
00467         // Sum the diffuse contribution from all lights
00468         source += "diff.rgb += " + LightProperty(i, "diffuse") + " * nDotVP;\n";
00469     }
00470 
00471     if (props.lightModel == ShaderProperties::SpecularModel)
00472     {
00473         if (props.usesShadows())
00474         {
00475             source += SeparateSpecular(i) +
00476                 " = pow(nDotHV, shininess);\n";
00477         }
00478         else
00479         {
00480             source += "spec.rgb += " + LightProperty(i, "specular") +
00481                 " * (pow(nDotHV, shininess) * nDotVP);\n";
00482         }
00483     }
00484 
00485     if (props.texUsage & ShaderProperties::NightTexture)
00486     {
00487         source += "totalLight += nDotVP;\n";
00488     }
00489 
00490 
00491     return source;
00492 }

static void DumpShaderSource ostream &  out,
const std::string source
[static]
 

Definition at line 339 of file shadermanager.cpp.

Referenced by ShaderManager::buildFragmentShader(), ShaderManager::buildRingsFragmentShader(), ShaderManager::buildRingsVertexShader(), and ShaderManager::buildVertexShader().

00340 {
00341     bool newline = true;
00342     unsigned int lineNumber = 0;
00343 
00344     for (unsigned int i = 0; i < source.length(); i++)
00345     {
00346         if (newline)
00347         {
00348             lineNumber++;
00349             out << setw(3) << lineNumber << ": ";
00350             newline = false;
00351         }
00352 
00353         out << source[i];
00354         if (source[i] == '\n')
00355             newline = true;
00356     }
00357 
00358     out.flush();
00359 }

static string FragLightProperty unsigned int  i,
char *  property
[static]
 

Definition at line 210 of file shadermanager.cpp.

Referenced by ShaderManager::buildFragmentShader(), ShaderManager::buildRingsFragmentShader(), and CelestiaGLProgram::initParameters().

00211 {
00212     char buf[64];
00213     sprintf(buf, "light%s%d", property, i);
00214     return string(buf);
00215 }

ShaderManager& GetShaderManager  ) 
 

Definition at line 38 of file shadermanager.cpp.

References g_ShaderManager.

Referenced by GLSL_RenderContext::makeCurrent(), renderClouds_GLSL(), renderRings_GLSL(), and renderSphere_GLSL().

00039 {
00040     return g_ShaderManager;
00041 }

static string IndexedParameter const char *  name,
unsigned int  index0,
unsigned int  index1
[static]
 

Definition at line 228 of file shadermanager.cpp.

00229 {
00230     char buf[64];
00231     sprintf(buf, "%s%d_%d", name, index0, index1);
00232     return string(buf);
00233 }

static string IndexedParameter const char *  name,
unsigned int  index
[static]
 

Definition at line 219 of file shadermanager.cpp.

Referenced by ShaderManager::buildFragmentShader(), ShaderManager::buildRingsFragmentShader(), ShaderManager::buildRingsVertexShader(), ShaderManager::buildVertexShader(), CelestiaGLProgram::initParameters(), and Shadow().

00220 {
00221     char buf[64];
00222     sprintf(buf, "%s%d", name, index);
00223     return string(buf);
00224 }

static string LightDir unsigned int  i  )  [static]
 

Definition at line 433 of file shadermanager.cpp.

Referenced by ShaderManager::buildFragmentShader(), and ShaderManager::buildVertexShader().

00434 {
00435     char buf[32];
00436     sprintf(buf, "lightDir%d", i);
00437     return string(buf);
00438 }

static string LightProperty unsigned int  i,
char *  property
[static]
 

Definition at line 196 of file shadermanager.cpp.

Referenced by ShaderManager::buildRingsVertexShader(), ShaderManager::buildVertexShader(), DirectionalLight(), and CelestiaGLProgram::initParameters().

00197 {
00198     char buf[64];
00199 
00200 #ifndef USE_GLSL_STRUCTS
00201     sprintf(buf, "light%d_%s", i, property);
00202 #else
00203     sprintf(buf, "lights[%d].%s", i, property);
00204 #endif
00205     return string(buf);
00206 }

bool operator< const ShaderProperties p0,
const ShaderProperties p1
 

Definition at line 102 of file shadermanager.cpp.

00103 {
00104     if (p0.texUsage < p1.texUsage)
00105         return true;
00106     else if (p1.texUsage < p0.texUsage)
00107         return false;
00108 
00109     if (p0.nLights < p1.nLights)
00110         return true;
00111     else if (p1.nLights < p0.nLights)
00112         return false;
00113 
00114     if (p0.shadowCounts < p1.shadowCounts)
00115         return true;
00116     else if (p1.shadowCounts < p0.shadowCounts)
00117         return false;
00118 
00119     return (p0.lightModel < p1.lightModel);
00120 }

static string RingShadowTexCoord unsigned int  index  )  [static]
 

Definition at line 237 of file shadermanager.cpp.

Referenced by BeginLightSourceShadows(), and ShaderManager::buildVertexShader().

00238 {
00239     char buf[64];
00240     sprintf(buf, "ringShadowTexCoord.%c", "xyzw"[index]);
00241     return string(buf);
00242 }

static string SeparateDiffuse unsigned int  i  )  [static]
 

Definition at line 401 of file shadermanager.cpp.

Referenced by BeginLightSourceShadows(), ShaderManager::buildRingsFragmentShader(), ShaderManager::buildRingsVertexShader(), and DirectionalLight().

00402 {
00403     // Used for packing multiple diffuse factors into the diffuse color.
00404     // It's probably better to use separate float interpolants.  I'll switch
00405     // to this once I verify that shader compilers are smart enough to pack
00406     // multiple scalars into a single vector interpolant.
00407     char buf[32];
00408     sprintf(buf, "diffFactors.%c", "xyzw"[i & 3]);
00409     return string(buf);
00410 }

static string SeparateSpecular unsigned int  i  )  [static]
 

Definition at line 414 of file shadermanager.cpp.

Referenced by ShaderManager::buildFragmentShader(), and DirectionalLight().

00415 {
00416     // Used for packing multiple specular factors into the specular color.
00417     char buf[32];
00418     sprintf(buf, "specFactors.%c", "xyzw"[i & 3]);
00419     return string(buf);
00420 }

static string Shadow unsigned int  light,
unsigned int  shadow
[static]
 

Definition at line 521 of file shadermanager.cpp.

References IndexedParameter().

Referenced by ShaderManager::buildRingsFragmentShader(), and ShadowsForLightSource().

00522 {
00523     string source;
00524 
00525     source += "shadowCenter = " +
00526         IndexedParameter("shadowTexCoord", light, shadow) +
00527         ".st - vec2(0.5, 0.5);\n";
00528     source += "shadowR = clamp(dot(shadowCenter, shadowCenter) * " +
00529         IndexedParameter("shadowScale", light, shadow) + " + " +
00530         IndexedParameter("shadowBias", light, shadow) + ", 0.0, 1.0);\n";
00531     source += "shadow *= sqrt(shadowR);\n";
00532 
00533     return source;
00534 }

static string ShadowsForLightSource const ShaderProperties props,
unsigned int  light
[static]
 

Definition at line 538 of file shadermanager.cpp.

References BeginLightSourceShadows(), and Shadow().

Referenced by ShaderManager::buildFragmentShader().

00539 {
00540     string source = BeginLightSourceShadows(props, light);
00541 
00542     for (unsigned int i = 0; i < props.getShadowCountForLight(light); i++)
00543         source += Shadow(light, i);
00544 
00545     return source;
00546 }

static string TexCoord2D unsigned int  i  )  [static]
 

Definition at line 424 of file shadermanager.cpp.

Referenced by ShaderManager::buildRingsVertexShader(), and ShaderManager::buildVertexShader().

00425 {
00426     char buf[64];
00427     sprintf(buf, "gl_MultiTexCoord%d.st", i);
00428     return string(buf);
00429 }


Variable Documentation

const char* errorFragmentShaderSource [static]
 

Initial value:

    "void main(void) {\n"
    "   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
    "}\n"

Definition at line 31 of file shadermanager.cpp.

Referenced by ShaderManager::buildProgram().

const char* errorVertexShaderSource [static]
 

Initial value:

 
    "void main(void) {\n"
    "   gl_Position = ftransform();\n"
    "}\n"

Definition at line 27 of file shadermanager.cpp.

Referenced by ShaderManager::buildProgram().

ShaderManager g_ShaderManager
 

Definition at line 24 of file shadermanager.cpp.

Referenced by GetShaderManager().


Generated on Sat Jan 14 22:31:12 2006 for Celestia by  doxygen 1.4.1