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

MarsImage.cpp

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