Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ntl_lighting.cpp « intern « elbeem « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9bea7a08153f735f7a52efced020ba56cdb88c20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/** \file \ingroup elbeem
 */
/******************************************************************************
 *
 * El'Beem - Free Surface Fluid Simulation with the Lattice Boltzmann Method
 * Copyright 2003-2006 Nils Thuerey
 *
 * a light object
 *
 *****************************************************************************/


#include "ntl_lighting.h"
#include "ntl_ray.h"
#include "ntl_world.h"


/******************************************************************************
 * Default Constructor
 *****************************************************************************/
ntlLightObject::ntlLightObject(ntlRenderGlobals *glob) :
	mpGlob( glob ),
	mActive( 1 ),
	mCastShadows( 1 ),
	mcColor( ntlColor(1.0) ),
	mvPosition( ntlVec3Gfx(0.0) )
{
	// nothing to do...
}


/******************************************************************************
 * Constructor with parameters 
 *****************************************************************************/
ntlLightObject::ntlLightObject(ntlRenderGlobals *glob, const ntlColor& col) :
	mpGlob( glob ),
	mActive( 1 ),
	mCastShadows( 1 ),
	mcColor( col )
{
	// nothing to do...
}



/******************************************************************************
 * Destructor
 *****************************************************************************/
ntlLightObject::~ntlLightObject()
{
	// nothing to do...
}



/******************************************************************************
 * Determine color contribution of a lightsource (Phong model)
 * Specular part is returned in seperate parameter and added later
 *****************************************************************************/
const ntlColor
ntlLightObject::getShadedColor(const ntlRay &reflectedRay, const ntlVec3Gfx lightDir,
															 ntlMaterial *surf, ntlColor &highlight) const
{
  gfxReal ldot = dot(lightDir, reflectedRay.getNormal()); /* equals cos( angle(L,N) ) */
  ntlColor reflected_color = ntlColor(0.0);  /* adds up to total reflected color */
	if(mpGlob->getDebugOut() > 5) errorOut("Lighting dir:"<<lightDir<<"  norm:"<<reflectedRay.getNormal()<<"  "<<ldot );

  /* lambertian reflection model */
  if (ldot > 0.0) {
		//ldot *= -1.0;
    reflected_color += surf->getDiffuseRefl() * (getColor() * ldot );

    /* specular part */
    /* specular reflection only makes sense, when the light is facing the surface,
       as the highlight is supposed to be a reflection of the lightsource, it cannot
       be reflected on surfaces with ldot<=0, as this means the arc between light 
       and normal is more than 90 degrees. If this isn't done, ugly moiree patterns appear
       in the highlights, and refractions have strange patterns due to highlights on the
       inside of the surface */
    gfxReal spec = dot(reflectedRay.getDirection(), lightDir); // equals cos( angle(R,L) )
    if((spec > 0.0) && (surf->getSpecular()>0)) {
      spec = pow( spec, surf->getSpecExponent() ); /* phong exponent */
      highlight += getColor() * surf->getSpecular() * spec;
			//errorOut( " "<< surf->getName() <<" S "<<highlight<<" "<<spec<<" "<<surf->getSpecular()<<" "<<surf->getSpecExponent() );
    }

  }

  return ntlColor(reflected_color);
}


// omni light implementation


/******************************************************************************
 *! prepare shadow maps if necessary 
 *****************************************************************************/
void ntlLightObject::prepare( bool doCaustics )
{
	doCaustics = false; // unused
	if(!mActive) { return; }
}


/******************************************************************************
 * Illuminate the given point on an object
 *****************************************************************************/
ntlColor ntlLightObject::illuminatePoint(ntlRay &reflectedRay, ntlGeometryObject *closest,
																			 ntlColor &highlight )
{
	/* is this light active? */
	if(!mActive) { return ntlColor(0.0); }

	gfxReal visibility = 1.0;   // how much of light is visible
	ntlVec3Gfx intersectionPos = reflectedRay.getOrigin();
	ntlColor current_color = ntlColor(0.0);
	ntlMaterial *clossurf = closest->getMaterial();

	ntlVec3Gfx lightDir = (mvPosition - intersectionPos);
	gfxReal lightDirNorm = normalize(lightDir);

	// where is the lightsource ?
	ntlRay rayOfLight(intersectionPos, lightDir, 0, 1.0, mpGlob );
	
	if( (1) && (mCastShadows)&&(closest->getReceiveShadows()) ) {
		ntlTriangle *tri;
		ntlVec3Gfx triNormal;
		gfxReal trit;
		mpGlob->getRenderScene()->intersectScene(rayOfLight, trit, triNormal, tri, TRI_CASTSHADOWS);
		if(( trit>0 )&&( trit<lightDirNorm )) visibility = 0.0;
		if(mpGlob->getDebugOut() > 5) errorOut("Omni lighting with "<<visibility );
	}
	
	/* is light partly visible ? */
//? visibility=1.;
	if (visibility>0.0) {
		ntlColor highTemp(0.0); // temporary highlight color to multiply highTemp with offFac
		current_color = getShadedColor(reflectedRay, lightDir, clossurf, highTemp) * visibility;
		highlight += highTemp * visibility;
		if(mpGlob->getDebugOut() > 5) errorOut("Omni lighting color "<<current_color );
	}
	return current_color;
}



/******************************************************************************
 * Default constructor
 *****************************************************************************/
ntlMaterial::ntlMaterial( void ) : 
	mName( "default" ),
  mDiffuseRefl(0.5,0.5,0.5),  mAmbientRefl(0.0,0.0,0.0),
  mSpecular(0.0), mSpecExponent(0.0), mMirror(0.0),
  mTransparence(0.0), mRefracIndex(1.05), mTransAdditive(0.0), mTransAttCol(0.0),
	mFresnel( 0 ) { 
  // just do default init...
}



/******************************************************************************
 * Init constructor
 *****************************************************************************/
ntlMaterial::ntlMaterial( string name,
													const ntlColor& Ref, const ntlColor& Amb,
													gfxReal Spec, gfxReal SpecEx, gfxReal Mirr,
													gfxReal Trans, gfxReal Refrac, gfxReal TAdd,
													const ntlColor& Att, int fres)
{
	mName					= name;
	mDiffuseRefl  = Ref;
	mAmbientRefl  = Amb;
	mSpecular     = Spec;
	mSpecExponent = SpecEx;
	mMirror       = Mirr;
	mTransparence = Trans;
	mRefracIndex  = Refrac;
	mTransAdditive = TAdd;
	mTransAttCol   = Att;
	mFresnel 			= fres;
}