[go: up one dir, main page]

Menu

[3347a2]: / caelum / Sky.cc  Maximize  Restore  History

Download this file

162 lines (141 with data), 3.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Sky.cc - Helper class for Caelum.
//
// Copyright (C) 2003 Sam Varner
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "Sky.h"
#include <cassert>
#include <cstdlib>
#include <SDL/SDL.h>
#include <GL/glut.h>
#include "../geometry/Constants.h"
#include "../media/Texture_Image.h"
Sky::Sky (std::string image, int width, int height) :
m_width (width),
m_height (height),
m_z_pos (0.0),
m_z_mag (1.0),
m_z_rot (0.0)
{
// Load the image.
mp_image = new Vamos_Media::Texture_Image (image, true, true);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
// Repeat in the east-west direction.
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
// Clamp in the north-south direction.
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
Sky::~Sky ()
{
delete mp_image;
}
void
Sky::key_press (int key)
{
static const double r_inc = 0.01;
static const double z_inc = 0.01;
static const double m_inc = 0.01;
switch (key)
{
case SDLK_UP:
// Raise the image.
m_z_pos += z_inc;
break;
case SDLK_DOWN:
// Lower the image.
m_z_pos -= z_inc;
break;
case SDLK_RIGHT:
// Shift the image to the right.
m_z_rot += r_inc;
break;
case SDLK_LEFT:
// Shift the image to the left.
m_z_rot -= r_inc;
break;
case SDLK_PAGEUP:
// Magnify in the z-direction.
m_z_mag += m_inc;
break;
case SDLK_PAGEDOWN:
// Shrink in the z-direction.
m_z_mag -= m_inc;
if (m_z_mag <= 0.0)
m_z_mag = 1.0;
break;
case 'q': // fall through
case 27: // escape
std::exit (0);
}
display ();
}
void
Sky::display ()
{
// Clear the entire window.
glViewport (0, 0, m_width * 4, m_height * 3);
glClear (GL_COLOR_BUFFER_BIT);
// Enable sub-windows.
glEnable (GL_SCISSOR_TEST);
// Draw the front view.
int x = 0;
int y = m_height;
glViewport (x, y, m_width, m_height);
glScissor (x, y, m_width, m_height);
// Set the camera.
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (90, 1.0, 0.1, 1.1);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
gluLookAt (0.0, 0.0, 0.0, // camera position
0.1, 0.0, 0.0, // target
0.0, 0.0, 0.1); // up direction
draw ();
// Draw the right view.
x += m_width;
glViewport (x, y, m_width, m_height);
glScissor (x, y, m_width, m_height);
glRotated (90.0, 0.0, 0.0, 1.0);
draw ();
// Draw the back view.
x += m_width;
glViewport (x, y, m_width, m_height);
glScissor (x, y, m_width, m_height);
glRotated (90.0, 0.0, 0.0, 1.0);
draw ();
// Draw the left view.
x += m_width;
glViewport (x, y, m_width, m_height);
glScissor (x, y, m_width, m_height);
glRotated (90.0, 0.0, 0.0, 1.0);
draw ();
// Draw the up view.
x = 0;
y = 0;
glViewport (x, y, m_width, m_height);
glScissor (x, y, m_width, m_height);
glRotated (90.0, 0.0, 0.0, 1.0);
glRotated (90.0, 0.0, -1.0, 0.0);
draw ();
// Draw the down view.
y = m_height*2;
glViewport (x, y, m_width, m_height);
glScissor (x, y, m_width, m_height);
glRotated (180.0, 0.0, -1.0, 0.0);
draw ();
glFlush ();
SDL_GL_SwapBuffers ();
}