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

light.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: light.cpp,v 1.6 2004/04/12 13:36:39 rollmark Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 
00022    You should have received a copy of the GNU General Public License
00023    along with this program; if not, write to the Free Software
00024    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00025 */
00026 #include "light.h"
00027 #include <kerosin/renderserver/renderserver.h>
00028 #include <kerosin/openglserver/openglserver.h>
00029 #include <zeitgeist/logserver/logserver.h>
00030 
00031 /*
00032 // define the symbol BOOL to prevent opcode.h to typedef BOOL
00033 #define BOOL BOOL
00034 #include <opcode/Opcode.h>
00035 */
00036 
00037 using namespace boost;
00038 using namespace kerosin;
00039 using namespace salt;
00040 
00041 Light::Light()
00042     : mDiffuse(1.0f,1.0f,1.0f,1.0f),
00043       mAmbient(0.0f,0.0f,0.0f,1.0f),
00044       mSpecular(1.0f,1.0f,1.0f,1.0f),
00045       mGLLight(-1),
00046       mVP(0),
00047       mFP(0),
00048       mCacheFlushed(true)
00049 {
00050 }
00051 
00052 float Light::GetRadius() const
00053 {
00054     return mRadius;
00055 }
00056 
00057 void Light::SetRadius(float radius)
00058 {
00059     mRadius = radius;
00060     FlushCache();
00061 }
00062 
00063 void Light::SetAmbient(const RGBA& ambient)
00064 {
00065     mAmbient = ambient;
00066 }
00067 
00068 const RGBA& Light::GetAmbient()
00069 {
00070     return mAmbient;
00071 }
00072 
00073 void Light::SetDiffuse(const RGBA& diffuse)
00074 {
00075     mDiffuse = diffuse;
00076 }
00077 
00078 const RGBA& Light::GetDiffuse()
00079 {
00080     return mDiffuse;
00081 }
00082 
00083 void Light::SetSpecular(const RGBA& specular)
00084 {
00085     mSpecular = specular;
00086 }
00087 
00088 const RGBA& Light::GetSpecular()
00089 {
00090     return mSpecular;
00091 }
00092 
00093 void Light::Prepare()
00094 {
00095     if (mGLLight < 0)
00096         {
00097             // no light allocated
00098             return;
00099         }
00100 
00101     const Vector3f& pos = GetWorldTransform().Pos();
00102 
00103     float light_pos[4];
00104     light_pos[0]=pos[0];
00105     light_pos[1]=pos[1];
00106     light_pos[2]=pos[2];
00107     light_pos[3]=1.0f;
00108 
00109     glLightfv(mGLLight, GL_AMBIENT, mAmbient);
00110     glLightfv(mGLLight, GL_DIFFUSE, mDiffuse);
00111     glLightfv(mGLLight, GL_POSITION,light_pos);
00112     glEnable(mGLLight);
00113 
00114     /*
00115       glEnable(GL_VERTEX_PROGRAM_ARB);
00116       glBindProgramARB(GL_VERTEX_PROGRAM_ARB, mVP);
00117 
00118       glEnable(GL_FRAGMENT_PROGRAM_ARB);
00119       glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, mFP);
00120 
00121       // publish light color to fragment program
00122       glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, mDiffuseR, mDiffuseG, mDiffuseB, 1.0f);
00123       glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 1, -1.0f/(mRadius*mRadius), 1.0f, 1.0f, 1.0f);
00124     */
00125 }
00126 
00127 void Light::RenderLitMesh(boost::shared_ptr<StaticMesh> /*mesh*/)
00128 {
00129     /*
00130       if (mCacheFlushed || (GetWorldTransform().Pos()-mOldPos).SquareLength() > 0.1 ||
00131       !mesh->GetWorldTransform().IsEqual(mMeshOldTransform))
00132       {
00133       // if the cache wasn't flushed, we will flush it now
00134       if (!mCacheFlushed) FlushCache();
00135 
00136       // cache the old position
00137       mOldPos = GetWorldTransform().Pos();
00138 
00139       // cache the mesh old position
00140       mMeshOldTransform = mesh->GetWorldTransform();
00141 
00142       // give each derived light a chance to find potentially lit triangles
00143       DeterminePotentiallyLitTriangles(mesh, mTriangles);
00144       //printf("%d\n", mTriangles.size());
00145       // fill cache indices
00146       mesh->FillIndexBuffers(mCachedIndices, mTriangles);
00147 
00148       // mark that we contain valid data
00149       mCacheFlushed = false;
00150       }
00151 
00152       mesh->Render(mCachedIndices);
00153 
00154       glDisable(GL_FRAGMENT_PROGRAM_ARB);
00155       glDisable(GL_VERTEX_PROGRAM_ARB);
00156     */
00157 }
00158 
00159 void Light::FlushCache()
00160 {
00161     const unsigned int n = mCachedIndices.size();
00162     for (unsigned int i=0; i<n; ++i)
00163         {
00164             mCachedIndices[i].Flush();
00165         }
00166     mCacheFlushed = true;
00167 }
00168 
00169 void Light::ComputeBoundingBox()
00170 {
00171     mLocalBoundingBox.minVec.Set(-mRadius,-mRadius,-mRadius);
00172     mLocalBoundingBox.maxVec.Set(mRadius,mRadius,mRadius);
00173 }
00174 
00175 void Light::OnLink()
00176 {
00177     mOpenGLServer = shared_dynamic_cast<OpenGLServer>
00178         (GetCore()->Get("sys/server/opengl"));
00179 
00180     if (mOpenGLServer.get() == 0)
00181         {
00182             GetLog()->Error()
00183                 << "(Light) ERROR: OpenGLServer not found\n";
00184         } else
00185             {
00186                 mGLLight = mOpenGLServer->AllocLight();
00187 
00188                 if (mGLLight < 0)
00189                     {
00190                         GetLog()->Error()
00191                             << "(Light) ERROR: No more OpenGL lights available\n";
00192                     }
00193 
00194 #if 0
00195                 mVP = mOpenGLServer->LoadARBVertexProgram("sys/program/omnilight.vp");
00196                 if (mVP == 0)
00197                     {
00198                         GetLog()->Error()
00199                             << "(Light) ERROR: Could not load vertex program\n";
00200                     }
00201 
00202                 mFP = mOpenGLServer->LoadARBFragmentProgram("sys/program/omnilight.fp");
00203                 if (mFP == 0)
00204                     {
00205                         GetLog()->Error()
00206                             << "(Light) ERROR: Could not load fragment program\n";
00207                     }
00208 #endif
00209 
00210             }
00211 
00212 }
00213 
00214 void Light::OnUnlink()
00215 {
00216     if (
00217         (mOpenGLServer.get() != 0) &&
00218         (mGLLight >= 0)
00219         )
00220         {
00221             mOpenGLServer->PutLight(mGLLight);
00222             mGLLight = -1;
00223         }
00224 
00225     mOpenGLServer.reset();
00226 }
00227 
00228 void Light::DeterminePotentiallyLitTriangles(boost::shared_ptr<StaticMesh>& /*mesh*/, std::vector<unsigned int>& /*triangles*/)
00229 {
00230     /*
00231       Opcode::SphereCache cache;
00232       Sphere sphere;
00233 
00234       sphere.Set(Point(GetWorldTransform().Pos().x(), GetWorldTransform().Pos().y(), GetWorldTransform().Pos().z()), 1.5f*mRadius);
00235       mesh->FindInfluencedTriangles(cache, sphere, triangles);
00236     */
00237 }

Generated on Thu Apr 6 15:25:38 2006 for rcssserver3d by  doxygen 1.4.4