home *** CD-ROM | disk | FTP | other *** search
/ 3D World 7 (Spanish) / 3DWorld_07.iso / pc / artic / w_prog / ejemplo2.c < prev    next >
C/C++ Source or Header  |  1997-06-23  |  3KB  |  114 lines

  1. /*  ejemplo2.c
  2.  *  Este programa textura una imagen de una fotografÌa
  3.  *  sobre dos cuadrados. Se utiliza la opciÛn de texturar
  4.  *  del tipo "clamp", donde la textura se aplica de manera tal que 
  5.  *    si las coordenadas son mayores que 1.0 o menores que 0.0, entonces
  6.  *  toman los valores de 1.0 y 0.0 respectivamente.
  7.  */
  8. #include "glos.h"
  9. #include <stdio.h>
  10. #include <GL/gl.h>
  11. #include <GL/glu.h>
  12. #include <GL/glaux.h>
  13.  
  14.  
  15. void myinit(void);
  16. void CargaImagen(void);
  17. void CALLBACK myReshape(GLsizei w, GLsizei h);
  18. void CALLBACK display(void);
  19.  
  20.  
  21. #define AnchoImagen 256         
  22. #define AltoImagen 256 
  23. GLubyte ColorImagen[AnchoImagen][AltoImagen][3];
  24.  
  25. void CargaImagen(void)
  26. {    //Para cargar una imagen en formato RAW
  27.     int i, j;
  28.     FILE *file;
  29.     file=fopen("image.RAW","rb");
  30.     if (file==NULL) { printf("Error fichero\n"); exit(1); }
  31.  
  32.     for (i = 0; i < AnchoImagen; i++) {
  33.     for (j = 0; j < AnchoImagen; j++) {
  34.     fread(&ColorImagen[i][j], sizeof(char), 3*256*256, file);
  35.     fclose(file);
  36.  
  37.     }
  38.     }
  39.  
  40.  
  41. }
  42.  
  43. void myinit(void)
  44. {    
  45.   glClearColor (0.0, 0.0, 0.0, 0.0);
  46.  
  47.     glRotatef(45.0, 0.0, .0, 1.0);
  48.   glEnable(GL_DEPTH_TEST);
  49.   glDepthFunc(GL_LESS);
  50.  
  51.   CargaImagen();
  52.   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  53.     
  54.     glTexImage2D(GL_TEXTURE_2D, 0, 3, AnchoImagen, 
  55.     AltoImagen, 0, GL_RGB, GL_UNSIGNED_BYTE, 
  56.         &ColorImagen[0][0][0]);
  57.  
  58.   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  59.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  60.     
  61.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  62.   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  63.   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  64.   glEnable(GL_TEXTURE_2D);
  65.   glShadeModel(GL_FLAT);
  66. }
  67.  
  68. void CALLBACK display(void)
  69. {
  70.     
  71.     //a la izq. las coordenadas del recuadro de textura
  72.     //a la derecha las coordenadas de los 4 vertices de los rectangulos en 3D
  73.     
  74.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  75.   glBegin(GL_QUADS);
  76.     
  77.     glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 1.0, 0.0);
  78.   glTexCoord2f(0.0, 1.0); glVertex3f(0.0, -1.0, 0.0);
  79.   glTexCoord2f(1.0, 1.0); glVertex3f(-2.0, -1.0, 0.0);
  80.   glTexCoord2f(1.0, 0.0); glVertex3f(-2.0, 1.0, 0.0);
  81.  
  82.  
  83.     glTexCoord2f(0.0, 0.0); glVertex3f(1.41421, 1.0, -1.41421);
  84.   glTexCoord2f(0.0, 1.0); glVertex3f(1.41421, -1.0, -1.41421);
  85.   glTexCoord2f(1.0, 1.0); glVertex3f(0.0, -1.0, 0.0);
  86.   glTexCoord2f(1.0, 0.0); glVertex3f(0.0, 1.0, 0.0);
  87.  
  88.     glEnd();
  89.   glFlush();
  90. }
  91.  
  92. void CALLBACK myReshape(GLsizei w, GLsizei h)
  93. {
  94.     h = (h == 0) ? 1 : h;
  95.     glViewport(0, 0, w, h);
  96.     glMatrixMode(GL_PROJECTION);
  97.     glLoadIdentity();
  98.     gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30.0);
  99.     glMatrixMode(GL_MODELVIEW);
  100.     glLoadIdentity();
  101.     glTranslatef(0.0, 0.0, -3.6);
  102. }
  103.  
  104. int main(int argc, char** argv)
  105. {
  106.     auxInitDisplayMode (AUX_SINGLE | AUX_RGB | AUX_DEPTH16);
  107.     auxInitPosition (0, 0, 500, 500);
  108.     auxInitWindow ("Textura de mapping del tipo clamp con imagen");
  109.     myinit();
  110.     auxReshapeFunc (myReshape);
  111.     auxMainLoop(display);
  112.     return(0);
  113. }
  114.