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

MercuryImage.cpp

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