Line # Revision Author
1 5 guilt #include <stdio.h>
2 #include <allegro.h>
3 #include <alleggl.h>
4 #include <GL/glu.h>
5 8 moose #include <vector>
6
7 5 guilt #define GRID_SIZE 64
8 #define M_PI 3.1415926535897932384626433832795
9
10 #include "cube.h"
11 #include "xgl_vector.h"
12 #include "xgl_quat.h"
13
14 #define rad(d) ((M_PI * (d)) / 180.0)
15 #define deg(r) (((r) / M_PI) * 180.0)
16
17 GLuint Tex;
18 10 moose GLuint Slate;
19 5 guilt
20 /* Parameters controlling the camera and projection state */
21 int viewport_w = 320; /* Viewport Width (pixels) */
22 int viewport_h = 240; /* Viewport Height (pixels) */
23 int fov = 48; /* Field of view (degrees) */
24 float aspect = 1; /* Aspect ratio */
25 8 moose
26 void ViewOrtho(int x, int y) // Set Up An Ortho View
27 {
28 9 moose glDisable(GL_DEPTH_TEST);
29 glDepthMask(GL_FALSE);
30
31 8 moose glMatrixMode(GL_PROJECTION); // Select Projection
32 9 moose // glPushMatrix(); // Push The Matrix
33 8 moose glLoadIdentity(); // Reset The Matrix
34 glOrtho( 0, x , y , 0, -1, 1 ); // Select Ortho Mode
35 glMatrixMode(GL_MODELVIEW); // Select Modelview Matrix
36 9 moose // glPushMatrix(); // Push The Matrix
37 8 moose glLoadIdentity(); // Reset The Matrix
38 }
39 5 guilt
40 8 moose void ViewPerspective(void) // Set Up A Perspective View
41 {
42 glMatrixMode( GL_PROJECTION ); // Select Projection
43 9 moose // glPopMatrix(); // Pop The Matrix
44 8 moose glMatrixMode( GL_MODELVIEW ); // Select Modelview
45 9 moose // glPopMatrix(); // Pop The Matrix
46 glLoadIdentity();
47 gluPerspective((float)fov, (double)SCREEN_W/(double)SCREEN_H, 1.0, 1000.0);
48
49 glEnable(GL_DEPTH_TEST);
50 glDepthMask(GL_TRUE);
51 8 moose }
52 5 guilt
53 8 moose class Thing {
54
55 public:
56 Thing() : x(0.0), y(0.0), z(0.0) {}
57 Thing(float _x, float _y, float _z, float _w, float _h) : x(_x), y(_y), z(_z), w(_w), h(_h) {};
58 virtual ~Thing() {}
59
60 virtual void render();
61 virtual void update();
62
63 private:
64 float x, y, z;
65 float w, h;
66 };
67
68 void Thing::render()
69 {
70 ViewOrtho(SCREEN_W, SCREEN_H);
71
72 9 moose
73 10 moose // glEnable(GL_BLEND);
74 8 moose // glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
75 10 moose // glColor4i( 255, 255, 255, 100 );
76 glBindTexture(GL_TEXTURE_2D, Slate);
77 8 moose
78 10 moose // allegro_gl_printf_ex(font, 1, 1, -1, "Foo is a really long string that I'm trying to print so I can see something hopefully!");
79
80 // glColor4i( 0, 0, 255, 100 );
81 8 moose // glBlendFunc(GL_ONE_MINUS_SRC_ALPHA,GL_ONE_MINUS_SRC_COLOR);
82
83 // glBlendFunc (GL_ONE, GL_ONE);
84 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
85 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_COLOR);
86 10 moose glBindTexture(GL_TEXTURE_2D, Slate);
87 8 moose glBegin(GL_QUADS);
88
89 10 moose glColor4i( 0, 0, 255, 0 );
90
91 glTexCoord2f( 0, 0 );
92 8 moose glVertex2f( x, y );
93 10 moose
94 glTexCoord2f( 0, 1 );
95 8 moose glVertex2f( x, y + h );
96 10 moose
97 glTexCoord2f( 1, 1 );
98 8 moose glVertex2f( x + w, y + h );
99 10 moose
100 glTexCoord2f( 1, 0 );
101 8 moose glVertex2f( x + w, y );
102 // glColor4i( 100, 100, 100, 100 );
103
104 glEnd();
105
106 ViewPerspective();
107 9 moose
108 10 moose // glDisable(GL_BLEND);
109 8 moose }
110
111 void Thing::update()
112 {
113
114 }
115 /*
116 class ModelLoader;
117 class Model {
118 friend class ModelLoader;
119
120 public:
121 Model();
122 virtual ~Model();
123
124 private:
125
126 int vertex_count;
127 double *vertex;
128
129 int normal_count;
130 double *normal;
131
132 int texcoord_count;
133 double *texcoord;
134 };
135
136 class ModelLoader {
137
138 public:
139 Model
140 };
141 */
142 5 guilt class Camera {
143 public:
144 Vector position;
145 Quaternion orientation;
146
147 static Camera *Instance()
148 {
149 if(!instance) {
150 instance = new Camera();
151 }
152 return instance;
153 }
154
155 6 guilt static Camera *Instance(Vector v)
156 {
157 if(!instance) {
158 instance = new Camera(v);
159 }
160 return instance;
161 }
162
163 5 guilt void setup();
164
165 protected:
166 Camera():orientation(identity_quat),position(0,0,4) {}
167 6 guilt Camera(Vector givpos):orientation(identity_quat),position(givpos) {}
168 5 guilt
169 private:
170 static Camera *instance;
171
172 };
173
174 Camera *Camera::instance = 0;
175
176 void Camera::setup()
177 {
178 8 moose glViewport(0, 0, SCREEN_W, SCREEN_H);
179 5 guilt glMatrixMode(GL_PROJECTION);
180 glLoadIdentity();
181 8 moose gluPerspective((float)fov, (double)SCREEN_W/(double)SCREEN_H, 1.0, 1000.0);
182 5 guilt glMatrixMode(GL_MODELVIEW);
183 glLoadIdentity();
184
185 float theta = deg(2 * acos(orientation.w));
186 if (orientation.w < 1.0f && orientation.w > -1.0f) {
187 glRotatef(theta, orientation.x, orientation.y, orientation.z);
188 }
189
190 glTranslatef(-(position.x), -(position.y), -(position.z));
191 }
192
193 6 guilt void draw_cube(double x, double y, double z, double size)
194 5 guilt {
195 6 guilt Cube cube(x, y, z, size);
196 5 guilt cube.Render();
197 }
198
199 void draw_field()
200 {
201 8 moose // glPushMatrix();
202 6 guilt draw_cube(2, -2, -4, 2);
203 draw_cube(-2, 2, -4, 2);
204 draw_cube(-2, -2, -4, 2);
205 draw_cube(2, 2, -4, 2);
206 8 moose // glPopMatrix();
207 5 guilt }
208
209 int do_keyboard()
210 {
211 Camera *cam = Camera::Instance();
212 QUAT q;
213
214 if (key[KEY_LEFT]) {
215 7 moose get_y_rotate_quat(&q, -0.1);
216 5 guilt cam->orientation*=q; //*= order is different from * order
217 }
218 if (key[KEY_RIGHT]) {
219 7 moose get_y_rotate_quat(&q, 0.1);
220 5 guilt cam->orientation*=q; //*= order is different from * order
221 }
222 if(key[KEY_DOWN]) {
223 7 moose cam->position-=(cam->orientation+Vector(0,0,-0.1))/10;
224 5 guilt }
225 if(key[KEY_UP]) {
226 7 moose cam->position+=(cam->orientation+Vector(0,0,-0.1))/10;
227 5 guilt }
228 7 moose if(key[KEY_W]) {
229 cam->position-=(cam->orientation+Vector(0,-0.1,0))/10;
230 }
231 if(key[KEY_S]) {
232 cam->position+=(cam->orientation+Vector(0,-0.1,0))/10;
233 }
234 if(key[KEY_A]) {
235 cam->position-=(cam->orientation+Vector(-0.1,0,0))/10;
236 }
237 if(key[KEY_D]) {
238 cam->position+=(cam->orientation+Vector(-0.1,0,0))/10;
239 }
240 5 guilt if (key[KEY_1]) {
241 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
242 }
243 if(key[KEY_2])
244 {
245 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
246 }
247 return 0;
248 }
249
250 int main ()
251 {
252 BITMAP* temp_bmp = NULL;
253
254 allegro_init();
255
256 install_allegro_gl();
257
258 allegro_gl_clear_settings();
259 allegro_gl_set (AGL_COLOR_DEPTH, 32);
260 9 moose allegro_gl_set (AGL_Z_DEPTH, 24);
261 5 guilt
262 allegro_gl_set (AGL_WINDOWED, TRUE);
263 allegro_gl_set (AGL_DOUBLEBUFFER, 1);
264 allegro_gl_set (AGL_SUGGEST, AGL_COLOR_DEPTH | AGL_Z_DEPTH | AGL_DOUBLEBUFFER | AGL_WINDOWED);
265
266 if (set_gfx_mode(GFX_OPENGL, 640, 480, 0, 0) < 0) {
267 allegro_message ("Error setting OpenGL graphics mode:\n%s\n"
268 "Allegro GL error : %s\n",
269 allegro_error, allegro_gl_error);
270 return -1;
271 }
272
273 install_keyboard();
274 install_timer();
275
276 glEnable(GL_TEXTURE_2D);
277
278 8 moose allegro_gl_set_texture_format(GL_RGBA8);
279 5 guilt
280 10 moose temp_bmp = load_bitmap("slate.bmp", 0);
281 Slate = allegro_gl_make_texture(temp_bmp);
282
283 5 guilt // Load the texture
284 temp_bmp = load_bitmap("wall-texture.bmp", 0);
285 Tex = allegro_gl_make_texture(temp_bmp);
286 glBindTexture(GL_TEXTURE_2D, Tex);
287 10 moose destroy_bitmap(temp_bmp);
288 5 guilt
289 10 moose
290
291 8 moose Camera::Instance()->setup();
292 5 guilt
293 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
294 glClearDepth(1.0f);
295 glEnable(GL_DEPTH_TEST);
296 glDepthFunc(GL_LESS);
297 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
298
299 9 moose // glEnable(GL_BLEND);
300 // glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
301 8 moose //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
302 Thing thing(100, 100, 0, 100, 100);
303 9 moose Thing thing2(150, 150, 1, 100, 100);
304 8 moose
305 font = allegro_gl_convert_allegro_font_ex(font, AGL_FONT_TYPE_DONT_CARE, 2.0, -1);
306
307 6 guilt Camera::Instance(Vector(0,0,4));
308 8 moose while(!key[KEY_ESC]) {
309 9 moose glClearColor(0.5f, 0.2f, 0.5f, 0.0f);
310 8 moose glClearDepth(1.0f);
311 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
312 // glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
313 9 moose // glBlendFunc (GL_ONE, GL_ONE);
314 8 moose do_keyboard();
315 Camera::Instance()->setup();
316 glBindTexture(GL_TEXTURE_2D, Tex);
317 draw_field();
318 10 moose glBindTexture(GL_TEXTURE_2D, Slate);
319 8 moose thing.render();
320 thing2.render();
321 glFlush();
322 allegro_gl_flip();
323 }
324 5 guilt
325 set_gfx_mode (GFX_TEXT, 0, 0, 0, 0);
326
327 return 0;
328 }
329 END_OF_MAIN();