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

SaturnImage.cpp

Go to the documentation of this file.
00001 /*
00002  * SaturnImage.cpp
00003  *
00004  * Part of Fly! Legacy project
00005  *
00006  * Copyright 2003 Chris Wallace
00007  *
00008  * Fly! Legacy is free software; you can redistribute it and/or modify
00009  *   it under the terms of the GNU General Public License as published by
00010  *   the Free Software Foundation; either version 2 of the License, or
00011  *   (at your option) any later version.
00012  *
00013  * Fly! Legacy 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 Fly! Legacy; if not, write to the Free Software
00020  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00034 #include "../Include/Sky.h"
00035 #include "../Include/Utility.h"
00036 
00037 //
00038 // Pre-draw callback
00039 //
00040 static int saturn_predraw (ssgEntity *e)
00041 {
00042   ssgLeaf *f = (ssgLeaf*)e;
00043   if (f->hasState()) f->getState()->apply();
00044 
00045   glPushAttrib (GL_DEPTH_BUFFER_BIT | GL_FOG_BIT | GL_POINT_BIT);
00046   glDisable (GL_DEPTH_TEST);
00047   glDisable (GL_FOG);
00048   glEnable (GL_POINT_SMOOTH);
00049   glPointSize (2.0);
00050 
00051   return true;
00052 }
00053 
00054 
00055 //
00056 // Post-draw callback
00057 //
00058 static int saturn_postdraw (ssgEntity *e)
00059 {
00060   glPopAttrib ();
00061 
00062   return true;
00063 }
00064 
00065 
00066 CSaturnImage::CSaturnImage (void)
00067 {
00068   // Instantiate SSG entities
00069   top = new ssgTransform;
00070   top->setName ("Saturn");
00071 
00072   // Create simple SSG state
00073   ssgSimpleState *state = new ssgSimpleState ();
00074   state->setShadeModel (GL_SMOOTH);
00075   state->disable (GL_LIGHTING);
00076   state->disable (GL_CULL_FACE);
00077   state->disable (GL_TEXTURE_2D);
00078   state->enable (GL_COLOR_MATERIAL);
00079   state->enable (GL_BLEND);
00080   state->enable (GL_ALPHA_TEST);
00081   state->setColourMaterial (GL_AMBIENT_AND_DIFFUSE);
00082   state->setMaterial (GL_EMISSION, 0, 0, 0, 1);
00083   state->setMaterial (GL_SPECULAR, 0, 0, 0, 1);
00084 
00085   // Default colour is pale yellow
00086   rgba = new ssgColourArray (1);
00087   sgVec4 default_colour;
00088   sgSetVec4 (default_colour, 1.0, 0.99, 0.90, 1.0);
00089   rgba->add (default_colour);
00090 
00091   // Create as single point at the origin
00092   va = new ssgVertexArray (1);
00093   sgVec3 v;
00094   sgSetVec3 (v, 0, 0, 0);
00095   va->add (v);
00096 
00097   // Construct the leaf object
00098   ssgLeaf *leaf = new ssgVtxTable (GL_POINTS, va, NULL, NULL, rgba);
00099   leaf->setState (state);
00100   leaf->setCallback (SSG_CALLBACK_PREDRAW, saturn_predraw);
00101   leaf->setCallback (SSG_CALLBACK_POSTDRAW, saturn_postdraw);
00102   top->addKid (leaf);
00103 }
00104 
00105 
00106 ssgEntity* CSaturnImage::GetSSGEntity (void)
00107 {
00108   return top;
00109 }
00110 
00111 
00112 //
00113 // Repaint the appearance of the image
00114 //
00115 void CSaturnImage::Repaint (float mv, float limit, float factor)
00116 {
00117   float nmag, alpha;
00118   if (mv < limit) {
00119     // This star is brighter than the limiting magnitude
00120     nmag = (limit - mv) / limit;    // Scale brightness to 0.0..1.0
00121     alpha = (nmag * 0.85);        // Scale alpha to 0.0..0.85
00122     alpha += 0.25;            // Bias +25%
00123     alpha *= factor;          // Adjust alpha for ambient light
00124   } else {
00125     // Star is dimmer than the limiting magnitude
00126     alpha = 0.0;
00127   }
00128 
00129   // Clamp alpha to 0.0..1.0
00130   if (alpha > 1.0) { alpha = 1.0; }
00131   if (alpha < 0.0) { alpha = 0.0; }
00132 
00133   float* colour = rgba->get (0);
00134   colour[3] = alpha;
00135 }
00136 
00137 
00138 //
00139 // Reposition the image in the sky
00140 //
00141 // Arguments:
00142 //  pos     3D position of the eye (cartesian coordinates??)
00143 //  lst     Local Sidereal Time, in degrees
00144 //  ra      Right Ascension, in radians
00145 //  dec     Declination, in radians
00146 //  distance  Distance from the eye in world units
00147 //
00148 void CSaturnImage::Reposition (sgVec3 pos, double lst, double lat,
00149                  double ra, double dec, double distance)
00150 {
00151   sgMat4 LST, LAT, RA, DEC, D;
00152   sgVec3 axis;
00153   sgVec3 v;
00154 
00155   // Rotation matrix for latitude
00156   sgSetVec3 (axis, -1, 0, 0);
00157   sgMakeRotMat4 (LAT, 90-lat, axis);
00158 
00159   // Rotation matrix for local sidereal time, converted from h to deg
00160   sgSetVec3 (axis, 0, 0, -1);
00161   sgMakeRotMat4 (LST, (lst * 15), axis);
00162 
00163   // Rotation matrix for right ascension
00164   sgSetVec3 (axis, 0, 0, 1);
00165   sgMakeRotMat4 (RA, RadToDeg (ra), axis);
00166 
00167   // Rotation matrix for declination
00168   sgSetVec3 (axis, 1, 0, 0);
00169   sgMakeRotMat4 (DEC, 90 - RadToDeg (dec), axis);
00170 
00171   // Translate sun distance
00172   sgSetVec3 (v, 0, 0, distance);
00173   sgMakeTransMat4 (D, v);
00174 
00175   // Combine all transforms
00176   sgMat4 T;
00177   sgMakeIdentMat4 (T);
00178   sgPreMultMat4 (T, LAT);
00179   sgPreMultMat4 (T, LST);
00180   sgPreMultMat4 (T, RA);
00181   sgPreMultMat4 (T, DEC);
00182   sgPreMultMat4 (T, D);
00183   
00184   top->setTransform (T);
00185 }
00186 
00187 
SourceForge.net Logo Documentation generated by doxygen