src/render/rViewport.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023   
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #include "rFont.h"
00029 #include "rScreen.h"
00030 #include "rViewport.h"
00031 #include "rConsole.h"
00032 #include "tConfiguration.h"
00033 
00034 #ifndef DEDICATED
00035 #include "rGL.h"
00036 //#include <GL/glu>
00037 #ifdef POWERPAK_DEB
00038 #include "PowerPak/powerdraw.h"
00039 #endif
00040 #endif
00041 
00042 #ifndef DEDICATED
00043 void rViewport::Select(){
00044     if (sr_glOut)
00045         glViewport (GLsizei(sr_screenWidth*left),
00046                     GLsizei(sr_screenHeight*bottom),
00047                     GLsizei(sr_screenWidth*width),
00048                     GLsizei(sr_screenHeight*height));
00049 }
00050 #endif
00051 
00052 
00053 REAL rViewport::UpDownFOV(REAL fov){
00054     REAL ratio=currentScreensetting.aspect*(width*sr_screenWidth)/(height*sr_screenHeight);
00055 
00056     // clamp ratio to 5/3
00057     REAL maxratio = 5.0/3.0;
00058     if (ratio > maxratio)
00059         ratio = maxratio;
00060 
00061     return 360*atan(tan(M_PI*fov/360)/ratio)/M_PI;
00062 }
00063 
00064 void rViewport::Perspective(REAL fov,REAL nnear,REAL ffar,REAL xshift){
00065 #ifndef DEDICATED
00066     if (!sr_glOut)
00067         return;
00068 
00069     // Jonathan's improved version
00070     REAL aspectratio = (height * sr_screenHeight) / (width * sr_screenWidth * currentScreensetting.aspect);
00071     REAL ensureverticalfov = fmax((3.0 / 5.0) * aspectratio, 1.0);
00072     REAL xmul = ensureverticalfov * tan((M_PI / 360.0) * fov);
00073     REAL ymul = ensureverticalfov * aspectratio * xmul;
00074     glMatrixMode(GL_PROJECTION);
00075     xshift *= nnear;
00076     glFrustum(-nnear * xmul + xshift, nnear * xmul + xshift, -nnear * ymul, nnear * ymul, nnear, ffar);
00077     glTranslatef(xshift, 0.f, 0.f);
00078 
00079 #if 0 // Z-Man's old and clumsy version
00080     REAL ratio=currentScreensetting.aspect*(width*sr_screenWidth)/(height*sr_screenHeight);
00081     // REAL udfov=360*atan(tan(M_PI*fov/360)/ratio)/M_PI;
00082     REAL udfov=UpDownFOV(fov);
00083     glMatrixMode(GL_PROJECTION);
00084     gluPerspective(
00085         udfov,
00086         ratio,
00087         nnear,
00088         ffar
00089     );
00090 #endif
00091 
00092 #endif
00093 }
00094 
00095 rViewport rViewport::s_viewportFullscreen(0,0,1,1);
00096 
00097 rViewport rViewport::s_viewportTop(0,.5,1,.5);
00098 rViewport rViewport::s_viewportBottom(0,0,1,.5);
00099 
00100 rViewport rViewport::s_viewportLeft(0,0,.5,1);
00101 rViewport rViewport::s_viewportRight(.5,0,.5,1);
00102 
00103 rViewport rViewport::s_viewportTopLeft(0,.5,.5,.5);
00104 rViewport rViewport::s_viewportBottomLeft(0,0,.5,.5);
00105 rViewport rViewport::s_viewportTopRight(.5,.5,.5,.5);
00106 rViewport rViewport::s_viewportBottomRight(.5,0,.5,.5);
00107 rViewport rViewport::s_viewportDemonstation(.55,.05,.4,.4);
00108 
00109 int   sr_viewportBelongsToPlayer[MAX_VIEWPORTS],
00110 s_newViewportBelongsToPlayer[MAX_VIEWPORTS];
00111 
00112 // ***********************************************************
00113 
00114 
00115 rViewportConfiguration::rViewportConfiguration(rViewport *first)
00116         :num_viewports(1){
00117     viewports[0]=first;
00118 }
00119 
00120 rViewportConfiguration::rViewportConfiguration(rViewport *first,
00121         rViewport *second)
00122         :num_viewports(2){
00123     viewports[0]=first;
00124     viewports[1]=second;
00125 }
00126 
00127 rViewportConfiguration::rViewportConfiguration(rViewport *first,
00128         rViewport *second,
00129         rViewport *third)
00130         :num_viewports(3){
00131     viewports[0]=first;
00132     viewports[1]=second;
00133     viewports[2]=third;
00134 }
00135 
00136 rViewportConfiguration::rViewportConfiguration(rViewport *first,
00137         rViewport *second,
00138         rViewport *third,
00139         rViewport *forth)
00140         :num_viewports(4){
00141     viewports[0]=first;
00142     viewports[1]=second;
00143     viewports[2]=third;
00144     viewports[3]=forth;
00145 }
00146 
00147 #ifndef DEDICATED
00148 void rViewportConfiguration::Select(int i){
00149     if (i>=0 && i <num_viewports)
00150         viewports[i]->Select();
00151 }
00152 #endif
00153 
00154 rViewport * rViewportConfiguration::Port(int i){
00155     if (i>=0 && i <num_viewports)
00156         return viewports[i];
00157     else
00158         return NULL;
00159 }
00160 
00161 static rViewportConfiguration single_vp(&rViewport::s_viewportFullscreen);
00162 static rViewportConfiguration two_vp(&rViewport::s_viewportTop,
00163                                      &rViewport::s_viewportBottom);
00164 static rViewportConfiguration two_b(&rViewport::s_viewportLeft,
00165                                     &rViewport::s_viewportRight);
00166 static rViewportConfiguration three_a(&rViewport::s_viewportTop,
00167                                       &rViewport::s_viewportBottomLeft,
00168                                       &rViewport::s_viewportBottomRight);
00169 static rViewportConfiguration three_b(&rViewport::s_viewportTopLeft,
00170                                       &rViewport::s_viewportTopRight,
00171                                       &rViewport::s_viewportBottom);
00172 static rViewportConfiguration four_vp(&rViewport::s_viewportTopLeft,
00173                                       &rViewport::s_viewportTopRight,
00174                                       &rViewport::s_viewportBottomLeft,
00175                                       &rViewport::s_viewportBottomRight);
00176 
00177 rViewportConfiguration *rViewportConfiguration::s_viewportConfigurations[]={
00178             &single_vp,&two_vp,&two_b,&three_a,&three_b,&four_vp};
00179 
00180 char const * rViewportConfiguration::s_viewportConfigurationNames[]=
00181     {"$viewport_conf_name_0",
00182      "$viewport_conf_name_1",
00183      "$viewport_conf_name_2",
00184      "$viewport_conf_name_3",
00185      "$viewport_conf_name_4",
00186      "$viewport_conf_name_5"};
00187 
00188 const int  rViewportConfiguration::s_viewportNumConfigurations=6;
00189 
00190 
00191 
00192 // *******************************************************
00193 //   Player menu
00194 // *******************************************************
00195 
00196 static int conf_num=0;
00197 int rViewportConfiguration::next_conf_num=0;
00198 
00199 static tConfItem<int> confn("VIEWPORT_CONF",
00200                             rViewportConfiguration::next_conf_num);
00201 
00202 rViewportConfiguration *rViewportConfiguration::CurrentViewportConfiguration(){
00203     if (conf_num<0) conf_num=0;
00204     if (conf_num>=s_viewportNumConfigurations)
00205         conf_num=s_viewportNumConfigurations-1;
00206 
00207     return s_viewportConfigurations[conf_num];
00208 }
00209 
00210 #ifndef DEDICATED
00211 void rViewportConfiguration::DemonstrateViewport(tString *titles){
00212     if (!sr_glOut)
00213         return;
00214 
00215     for(int i=s_viewportConfigurations[next_conf_num]->num_viewports-1;i>=0;i--){
00216         rViewport sub(rViewport::s_viewportDemonstation,*(s_viewportConfigurations[next_conf_num]->Port(i)));
00217         sub.Select();
00218 
00219         glDisable(GL_TEXTURE_2D);
00220         glDisable(GL_DEPTH_TEST);
00221 
00222         glColor3f(.1,.1,.4);
00223         glRectf(-.9,-.9,.9,.9);
00224 
00225         glColor3f(.6,.6,.6);
00226         glBegin(GL_LINE_LOOP);
00227         glVertex2f(-1,-1);
00228         glVertex2f(-1,1);
00229         glVertex2f(1,1);
00230         glVertex2f(1,-1);
00231         glEnd();
00232 
00233         glColor3f(1,1,1);
00234         DisplayText(0,0,.5,titles[i], sr_fontMenu);
00235     }
00236 
00237     rViewport::s_viewportFullscreen.Select();
00238 }
00239 #endif
00240 
00241 
00242 rViewport * rViewportConfiguration::CurrentViewport(int i){
00243     return CurrentViewportConfiguration()->Port(i);
00244 }
00245 
00246 void rViewportConfiguration::UpdateConf(){
00247     conf_num = next_conf_num;
00248 }
00249 
00250 
00251 static int vpb_dir[MAX_VIEWPORTS];
00252 
00253 void rViewport::CorrectViewport(int i, int MAX_PLAYERS){
00254     if (vpb_dir[i]!=1 && vpb_dir[i]!=-1)
00255         vpb_dir[i]=1;
00256     s_newViewportBelongsToPlayer[i]+=MAX_PLAYERS-vpb_dir[i];
00257     s_newViewportBelongsToPlayer[i]%=MAX_PLAYERS;
00258     bool again;
00259     do{
00260         s_newViewportBelongsToPlayer[i]+=MAX_PLAYERS+vpb_dir[i];
00261         s_newViewportBelongsToPlayer[i]%=MAX_PLAYERS;
00262         again=false;
00263         int starta=rViewportConfiguration::s_viewportConfigurations[rViewportConfiguration::next_conf_num]->num_viewports-1;
00264         int startb=rViewportConfiguration::s_viewportConfigurations[     conf_num]->num_viewports-1;
00265         if (starta>startb)
00266             startb=starta;
00267         for(int j=starta;j>=0;j--)
00268             if (i!=j && s_newViewportBelongsToPlayer[i]
00269                     ==s_newViewportBelongsToPlayer[j])
00270                 again=true;
00271     } while(again);
00272 }
00273 
00274 void rViewport::CorrectViewports(int MAX_PLAYERS){
00275     for (int i=rViewportConfiguration::s_viewportConfigurations[conf_num]->num_viewports-1;i>=0;i--)
00276         CorrectViewport(i, MAX_PLAYERS);
00277 }
00278 
00279 
00280 void rViewport::Update(int MAX_PLAYERS){
00281     rViewportConfiguration::UpdateConf();
00282     CorrectViewports(MAX_PLAYERS);
00283 
00284     int i;
00285     for(i=MAX_VIEWPORTS-1;i>=0;i--)
00286         sr_viewportBelongsToPlayer[i]=s_newViewportBelongsToPlayer[i];
00287 }
00288 
00289 void rViewport::SetDirectionOfCorrection(int vp, int dir){
00290     vpb_dir[vp] = dir;
00291 }
00292 
00293 // *******************************************************************************************
00294 // *
00295 // *    CorrectAspectBottom
00296 // *
00297 // *******************************************************************************************
00301 // *******************************************************************************************
00302 
00303 rViewport rViewport::CorrectAspectBottom( void ) const
00304 {
00305     rViewport ret( *this );
00306     ret.height = width * 4.0 / 3.0;
00307 
00308     return ret;
00309 }
00310 
00311 rViewport rViewport::EqualAspectBottom( void ) const
00312 {
00313     rViewport ret( *this );
00314     ret.height = width * sr_screenWidth / sr_screenHeight;
00315 
00316     return ret;
00317 }
00318 
00319 

Generated on Sat Mar 15 22:55:53 2008 for Armagetron Advanced by  doxygen 1.5.4