1 /*
2 
3     OpenGL Tutor : Texture
4 
5 
6 
7     Copyright 1999 by Nate 'm|d' Miller
8 
9     For non-commercial use only!
10 
11 
12 
13     File        -- texture.c
14 
15     Date        -- 6/22/99
16 
17     Author      -- Nate 'm|d' Miller
18 
19     Contact     -- vandals1@home.com
20 
21     Web         -- http://members.home.com/vandals1
22 
23 
24 
25     This is about as simple of a texture mapping tutorial that you will get.
26 
27     The program loads "texture.tga" and displays it onto a quad.
28 
29 
30 
31     If you want to compile this make sure that you link too the following
32 
33     libraries: opegl32.lib glu32.lib glut32.lib
34 
35 */
36 
37 #define APP_NAME                "OpenGL Tutor : Texture"
38 
39 #include "tga.h"
40 
41 #include <stdio.h>
42 
43 #include <stdlib.h>
44 
45 #include "gl/glut.h"
46 
47 
48 
49 int winW = 640; /* window width */
50 
51 int winH = 480; /* window height */
52 
53 
54 
55 /*
56 
57 =============
58 
59 drawFace
60 
61 =============
62 
63 
64 
65 Draws a textured face.  Take note that the texture vertex must come BEFORE the
66 
67 vertex that it will be assigned too.
68 
69 
70 
71 Texture Coordinate Orrientation
72 
73 
74 
75 (0,1)-------------(1,1)
76 
77   |                 |
78 
79   |     Image       |
80 
81   |                 |
82 
83   |                 |
84 
85 (0,0)-------------(1,0)
86 
87 
88 
89 */
90 
drawFace(void)91 void drawFace (void)
92 
93 {
94 
95 //    glEnable (GL_TEXTURE_2D); /* enable texture mapping */
96 
97 //    glBindTexture (GL_TEXTURE_2D, 13); /* bind to our texture, has id of 13 */
98 
99 
100 
101     glBegin (GL_QUADS);
102 
103         //glTexCoord2f (0.0f,0.0f); /* lower left corner of image */
104 
105         glColor3f(1.0, 1.0, 1.0);
106 
107         glVertex3f (-10.0f, -10.0f, 0.0f);
108 
109         //glTexCoord2f (1.0f, 0.0f); /* lower right corner of image */
110 
111         glColor3f(0, 1.0, 1.0);
112 
113         glVertex3f (10.0f, -10.0f, 0.0f);
114 
115         //glTexCoord2f (1.0f, 1.0f); /* upper right corner of image */
116 
117         glColor3f(1.0, 1.0, 0);
118 
119         glVertex3f (10.0f, 10.0f, 0.0f);
120 
121         //glTexCoord2f (0.0f, 1.0f); /* upper left corner of image */
122 
123         glColor3f(1.0, 0, 0);
124 
125         glVertex3f (-10.0f, 10.0f, 0.0f);
126 
127     glEnd ();
128 
129 
130 
131  //   glDisable (GL_TEXTURE_2D); /* disable texture mapping */
132 
133 }
134 
135 
136 
137 
138 
139 
140 
drawSphere(void)141 void drawSphere (void)
142 
143 {
144 
145     /*
146 
147         This sphere is going to be rotated around the *current axis*.  This is
148 
149         because the model was first rotated and then translated.
150 
151     */
152 
153     glPushMatrix ();
154 
155 
156 
157     glRotatef (rotate, axis[0], axis[1], axis[2]);
158 
159 
160 
161     glTranslatef (-15.0, 10.0, 5.0);
162 
163 
164 
165     glColor3f (1.0, 0.0, 0.0);
166 
167 
168 
169     glutWireSphere (5.0, 6.0, 6.0);
170 
171 
172 
173     glPopMatrix ();
174 
175 
176 
177     /*
178 
179         This sphere is going to be rotated around the its own center.  This is
180 
181         because the model was first translated and then rotated.
182 
183     */
184 
185     glPushMatrix ();
186 
187 
188 
189     glTranslatef (5.0, 10.0, 0.0);
190 
191 
192 
193     glRotatef (rotate, axis[0], axis[1], axis[2]);
194 
195 
196 
197     glColor3f (0.0, 1.0, 0.0);
198 
199 
200 
201     glutWireSphere (2.0, 6.0, 6.0);
202 
203 
204 
205     glPopMatrix ();
206 
207 }
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 /*
224 
225 =============
226 
227 glutDisplay
228 
229 =============
230 
231 
232 
233 Our display function
234 
235 */
236 
glutDisplay(void)237 void glutDisplay (void)
238 
239 {
240 
241     if (!winH)
242 
243         return;
244 
245 
246 
247     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
248 
249 
250 
251 	glMatrixMode (GL_MODELVIEW);
252 
253 	glLoadIdentity ();
254 
255 	glTranslatef (0, 0, -50); /* eye position */
256 
257 
258 
259     drawFace ();
260 
261     drawSphere ();
262 
263 
264 
265 	glutSwapBuffers();
266 
267 }
268 
269 /*
270 
271 =============
272 
273 glutResize
274 
275 =============
276 
277 
278 
279 Resize function.  Called when our window is created and resized.
280 
281 */
282 
glutResize(int w,int h)283 void glutResize (int w, int h)
284 
285 {
286 
287 	winW = w;
288 
289 	winH = h;
290 
291 
292 
293     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
294 
295     glViewport (0, 0, winW, winH);
296 
297 
298 
299 	glMatrixMode (GL_PROJECTION);
300 
301     glLoadIdentity ();
302 
303 
304 
305 	gluPerspective (90, winW / winH, 1, 9999);
306 
307 
308 
309 	glutPostRedisplay ();
310 
311 }
312 
313 /*
314 
315 =============
316 
317 glutKeyboard
318 
319 =============
320 
321 
322 
323 Keyboard handler.
324 
325 */
326 
glutKeyboard(unsigned char key,int x,int y)327 void glutKeyboard (unsigned char key, int x, int y)
328 
329 {
330 
331     switch (key)
332 
333     {
334 
335         /* exit the program */
336 
337         case 27:
338 
339         case 'q':
340 
341         case 'Q':
342 
343             exit (1);
344 
345         break;
346 
347     }
348 
349 }
350 
351 /*
352 
353 =============
354 
355 glInit
356 
357 =============
358 
359 
360 
361 Sets up some OpenGL states and loads image.
362 
363 
364 
365 */
366 
glInit(void)367 void glInit (void)
368 
369 {
370 
371   glEnable (GL_DEPTH_TEST);
372 
373   glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
374 
375 
376 
377   loadTGA ("texture.tga", 13);
378 
379 
380 
381 }
382 
main(void)383 void main (void)
384 
385 {
386 
387 	glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_FULLSCREEN);
388 
389 	glutInitWindowSize (winW,winH);
390 
391 	glutCreateWindow (APP_NAME);
392 
393 	glutKeyboardFunc (glutKeyboard);
394 
395 	glutDisplayFunc (glutDisplay);
396 
397 	glutReshapeFunc (glutResize);
398 
399 
400 
401 	glInit ();
402 
403 
404 
405 	glutMainLoop(); // we never return...
406 
407 }