File: tex.c

package info (click to toggle)
allegro4.4 2%3A4.4.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,920 kB
  • ctags: 39,951
  • sloc: ansic: 164,225; asm: 17,620; cpp: 3,848; objc: 1,687; sh: 1,131; python: 676; pascal: 179; makefile: 48; perl: 29; lisp: 1
file content (264 lines) | stat: -rw-r--r-- 5,669 bytes parent folder | download | duplicates (8)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*----------------------------------------------------------------
 * tex.c -- Allegro-GL test program -- texturing test
 *----------------------------------------------------------------
 *  Based on alleggl/examp/test.c.
 */




#include <allegro.h>


/* Note: Under Windows, if you include any Windows headers (e.g. GL/gl.h),
 * you must include <winalleg.h> first.  To save you working out when this
 * occurs, `alleggl.h' handles this for you, and includes <GL/gl.h>.  If
 * you need other GL headers, include them afterwards.
 *
 * Also note that user programs, outside of this library distribution,
 * should use <...> notation for `alleggl.h'.
 */

#include "alleggl.h"

void display (void);

struct camera {
	double xangle, yangle, zangle;
	double dist;
};


struct camera camera = {
	0.0, 0.0, 0.0,
	20.0
};


double angle_speed = 5.0;
double dist_speed = 1.0;

int frames = 0;
volatile int secs;

GLuint tex;



void secs_timer(void) 
{ 
	secs++; 
} 
END_OF_FUNCTION(secs_timer);



void set_camera_position (void)
{
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glFrustum (-1.0, 1.0, -1.0, 1.0, 1.0, 40.0);
	glTranslatef (0, 0, -camera.dist);
	glRotatef (camera.xangle, 1, 0, 0);
	glRotatef (camera.yangle, 0, 1, 0);
	glRotatef (camera.zangle, 0, 0, 1);
	glMatrixMode (GL_MODELVIEW);
}



void keyboard (void)
{
	if (key[KEY_LEFT]) camera.yangle += angle_speed;
	if (key[KEY_RIGHT]) camera.yangle -= angle_speed;

	if (key[KEY_UP]) camera.xangle += angle_speed;
	if (key[KEY_DOWN]) camera.xangle -= angle_speed;

	if (key[KEY_PGUP]) camera.dist -= dist_speed;
	if (key[KEY_PGDN]) camera.dist += dist_speed;

	if (key[KEY_A]) {
		glDisable (GL_CULL_FACE); // TODO: remove me!
		algl_alert ("Alert!", NULL, NULL, "OK", NULL, 0, 0);
		glEnable (GL_CULL_FACE); // TODO: remove me!
	}

	set_camera_position();
	display();
}



void display (void)
{
	// Clear the RGB buffer and the depth buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Set the modelview matrix to be the identity matrix
	glLoadIdentity();
	// Translate and rotate the object
	glTranslatef(-2.5, 0.0, 0.0);
	glRotatef(-30, 1.0, 0.0, 0.0);
	glRotatef(30, 0.0, 1.0, 0.0);
	glRotatef(30, 0.0, 0.0, 1.0);

	glColor3f(1.0, 0.0, 1.0);

	// Draw the sides of the three-sided pyramid
	glEnable (GL_TEXTURE_2D);
	glBindTexture (GL_TEXTURE_2D, tex);
	glBegin(GL_TRIANGLE_FAN);
			glTexCoord2f (0, 0); glVertex3d(0, 4, 0);
			glTexCoord2f (1, 0); glVertex3d(0, -4, -4);
			glTexCoord2f (1, 1); glVertex3d(-4, -4, 4);
			glTexCoord2f (0, 1); glVertex3d(4, -4, 4);
			glTexCoord2f (1, 0); glVertex3d(0, -4, -4);
	glEnd();

	glColor3f(0.0, 1.0, 1.0);

	// Draw the base of the pyramid
	glBegin(GL_TRIANGLES);
			glTexCoord2f (1, 0); glVertex3d(0, -4, -4);
			glTexCoord2f (0, 1); glVertex3d(4, -4, 4);
			glTexCoord2f (1, 1); glVertex3d(-4, -4, 4);
	glEnd();


	glLoadIdentity();
	glTranslatef(2.5, 0.0, 0.0);
	glRotatef(45, 1.0, 0.0, 0.0);
	glRotatef(45, 0.0, 1.0, 0.0);
	glRotatef(45, 0.0, 0.0, 1.0);

	glColor3f(0.0, 1.0, 0.0);

	glDisable (GL_TEXTURE_2D);
	// Draw the sides of the cube
	glBegin(GL_QUAD_STRIP);
		glVertex3d(3, 3, -3);
		glVertex3d(3, -3, -3);
		glVertex3d(-3, 3, -3);
		glVertex3d(-3, -3, -3);
		glVertex3d(-3, 3, 3);
		glVertex3d(-3, -3, 3);
		glVertex3d(3, 3, 3);
		glVertex3d(3, -3, 3);
		glVertex3d(3, 3, -3);
		glVertex3d(3, -3, -3);
	glEnd();

	glColor3f(0.0, 0.0, 1.0);

	// Draw the top of the cube
	glBegin(GL_QUADS);
		glVertex3d(-3, -3, -3);
		glVertex3d(3, -3, -3);
		glVertex3d(3, -3, 3);
		glVertex3d(-3, -3, 3);
	glEnd();

	/* Bottom is texture-mapped */
	glEnable (GL_TEXTURE_2D);
	glBindTexture (GL_TEXTURE_2D, tex);
	glBegin (GL_QUADS);
		glTexCoord2f (0, 0); glVertex3d(-3, 3, -3);
		glTexCoord2f (1, 0); glVertex3d(-3, 3, 3);
		glTexCoord2f (1, 1); glVertex3d(3, 3, 3);
		glTexCoord2f (0, 1); glVertex3d(3, 3, -3);
	glEnd();

	glFlush();

	allegro_gl_flip();

	frames++;
}



void setup_textures (void)
{
	PALETTE pal;
	BITMAP *bmp, *bmp2;
	int w, h;

	bmp = load_bitmap ("mysha.pcx", pal);
	if (!bmp) {
		allegro_message ("Error loading `mysha.pcx'");
		exit (1);
	}
	w = 128;
	h = 128;
	bmp2 = create_bitmap (w, h);
	stretch_blit (bmp, bmp2, 0, 0, bmp->w, bmp->h, 0, 0, w, h);
	destroy_bitmap (bmp);

	allegro_gl_begin();
	glEnable (GL_TEXTURE_2D);
	glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

	tex = allegro_gl_make_texture(bmp2);

	destroy_bitmap(bmp2);
}



int main ()
{
	allegro_init();

	install_allegro_gl();

	allegro_gl_clear_settings();
	allegro_gl_set(AGL_Z_DEPTH, 16);
	allegro_gl_set(AGL_DOUBLEBUFFER, 1);
	allegro_gl_set(AGL_STENCIL_DEPTH, 0);	
	allegro_gl_set(AGL_RENDERMETHOD, 1);
	allegro_gl_set(AGL_COLOR_DEPTH, 16);
	allegro_gl_set(AGL_SUGGEST, AGL_Z_DEPTH | AGL_DOUBLEBUFFER | AGL_COLOR_DEPTH
	                | AGL_STENCIL_DEPTH | AGL_RENDERMETHOD | AGL_WINDOWED);

	if (set_gfx_mode(GFX_OPENGL, 640, 480, 0, 0) < 0) {
		allegro_message ("Error setting OpenGL graphics mode:\n%s\n"
		                 "Allegro GL error : %s\n",
		                 allegro_error, allegro_gl_error);
		return -1;
	}

	/* We want 24bpp textures */
	allegro_gl_set_texture_format(GL_RGB8);

	install_keyboard();
	install_mouse();
	install_timer();

	LOCK_FUNCTION(secs_timer);
	LOCK_VARIABLE(secs);

	allegro_gl_begin();
	glShadeModel (GL_FLAT);
	glEnable (GL_DEPTH_TEST);
	glCullFace (GL_BACK);
	glEnable (GL_CULL_FACE);
	allegro_gl_end();

	setup_textures();

	install_int (secs_timer, 1000);

	do {
		keyboard();
	} while (!key[KEY_ESC]);

	set_gfx_mode (GFX_TEXT, 0, 0, 0, 0);

	allegro_message("Frames: %i, Seconds: %i, FPS: %f\n",
	                frames, secs, (float)frames / (float)secs);

	return 0;
}
END_OF_MAIN()