Advertisement
Guest User

Texturas

a guest
Jun 20th, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.87 KB | None | 0 0
  1. // Includes
  2. #include <nds.h>
  3. #include <fat.h>
  4. #include <filesystem.h>
  5. #include <stdio.h>
  6.  
  7. // Estructura de una textura
  8. typedef struct{
  9.     u16 *data;    // Datos a 16 bits
  10.         u16 size_x;   // Tamaño X
  11.         u16 size_y;   // Tamaño Y
  12.         int mem;      // Memoria asignada por OpenGL
  13. }I9_FILE;
  14.  
  15. // Estructura de una camara
  16. typedef struct{
  17.     float pos_x, pos_y, pos_z;  // Posicion
  18.         float eye_x, eye_y, eye_z;  // Donde mira
  19.         float inc_x, inc_y, inc_z:  // ¿?
  20. }I9_CAMERA;
  21.  
  22. // Funciones y defines
  23. void Draw(void);       // Dibujar la escena
  24. void SetCamera(I9_CAMERA camera); // Pon al camara
  25. I9_FILE LoadTexture(const char* path, u16 x, u16 y);
  26. void SetTexture(I9_FILE texture);
  27. I9_FILE textura;       // Textura a usar
  28.  
  29.  
  30. // Funcion Main()
  31. int main(int argc, char **argv){
  32.  
  33.     // Iniciar 3D y NitroFS
  34.     videoSetMode(MODE_0_3D);            // 3D modo 0
  35.     vramSetBankA(VRAM_A_TEXTURE);       // Activa el banco A para texturas
  36.     memset((void*)0x06000000, 0, 131072);// Vacialo
  37.     nitroFSInit();                      // Inicia NitroFS
  38.    
  39.     /*************************************************************/
  40.     // Iniciar OPENGL
  41.     glInit();
  42.     glEnable(GL_ANTIALIAS);  // Inicia AA
  43.     glClearColor(0, 0, 0, 31); // Color de fondo
  44.  
  45.     glClearPolyID(63);  // ID de los poligonos a 63
  46.     glClearDepth(0x7FFF); // Profundidad maxima
  47.     glViewport(0, 0, 255, 191); // Toda la pantalla de 3D
  48.    
  49.     glEnable(GL_TEXTURE_2D); // Inicia texturas
  50.    
  51.     // Inicia proyeccion y perspectiva
  52.     glMatrixMode(GL_PROJECTION);
  53.     glLoadIdentity();
  54.     gluPerspective(70, 256.0/192.0, 0.1, 100);
  55.     glPolyFmt(POLY_ALPHA(31) | POLY_CULL_BACK);  // No dibuja caras traseras
  56.    
  57.     // Inicia matriz de modelado
  58.     glMatrixMode(GL_MODELVIEW);
  59.     glLoadIdentity();
  60.     /*************************************************************/
  61.  
  62.     // Inicia camara
  63.     I9_CAMERA maincamera;
  64.     maincamera.pos_x = 0.0f; maincamera.pos_y = 0.0f; maincamera.pos_z = -5.0f;
  65.     maincamera.eye_x = 0.0f; maincamera.eye_y = 0.0f; maincamera.eye_z = 0.0f;
  66.     maincamera.inc_x = 0.0f; maincamera.inc_y = 1.0f; maincamera.inc_z = 0.0f;
  67.  
  68.     // Cargar textura
  69.     textura = LoadTexture("mitextura.raw", TEXTURE_SIZE_128, TEXTURE_SIZE_128);
  70.     SetTexture(textura);
  71.    
  72.     // Bucle infinito
  73.     while(1) {
  74.         glPushMatrix();
  75.         SetCamera(maincamera);
  76.         /****************************************/
  77.         // Move camera
  78.         if(!(keysHeld() &KEY_L)){
  79.             if(keysHeld() &KEY_RIGHT){maincamera.eye_x -=0.5f;maincamera.pos_x -=0.5f;}
  80.             if(keysHeld() &KEY_LEFT){maincamera.eye_x +=0.5f;maincamera.pos_x +=0.5f;}
  81.             if(keysHeld() &KEY_UP){maincamera.eye_z +=0.5f;maincamera.pos_z +=0.5f;}
  82.             if(keysHeld() &KEY_DOWN){maincamera.eye_z -=0.5f;maincamera.pos_z -=0.5f;}
  83.         }
  84.         /****************************************/
  85.         // Dibujado
  86.         Draw();
  87.         /****************************************/
  88.         glPopMatrix(1);
  89.         glFlush(0);
  90.         scanKeys();
  91.         swiWaitForVBlank();
  92.     }
  93.    
  94.     return 0;
  95.  
  96. }
  97.  
  98. /***************************************************/
  99. // FUNCIONES
  100.  
  101. // Pon la camara
  102. void SetCamera(I9_CAMERA camera){
  103.    
  104.     gluLookAt(camera.pos_x, camera.pos_y, camera.pos_z,
  105.         camera.eye_x, camera.eye_y, camera.eye_z,
  106.         camera.inc_x, camera.inc_y, camera.inc_z);
  107. }
  108.  
  109. // Carga una textura en RAM
  110. I9_FILE LoadTexture(const char* path, u16 x, u16 y){
  111.    
  112.     // Abrir
  113.     FILE *raw_file = fopen(path, "rb");
  114.     if(raw_file == NULL){
  115.         consoleDemoInit();
  116.         iprintf("ERROR: Archivo %s no existe!\n", path);
  117.     }
  118.    
  119.     // Tamaño
  120.     fseek(raw_file, 0, SEEK_END);
  121.     u16 size = ftell(raw_file);
  122.     rewind(raw_file);
  123.     if(size == 0 || size > 131072){
  124.         consoleDemoInit();
  125.         iprintf("ERROR: Tamano incorrecto!\n");
  126.     }
  127.    
  128.     // Leer datos
  129.     u16 *buffer = (u16*) calloc ((size*3), sizeof(u16));  // *3 por el tamaño, que es /3
  130.     fread(buffer, 1, (size*3), raw_file);
  131.     fclose(raw_file);
  132.     if(buffer == NULL){
  133.         consoleDemoInit();
  134.         iprintf("ERROR: Buffer es NULL!\n");
  135.     }
  136.    
  137.     // Pasalo a textura
  138.     I9_FILE bmp;
  139.     bmp.data = buffer;
  140.     bmp.size = size;
  141.     free(buffer);
  142.     buffer = NULL;
  143.     bmp.size_x = x;
  144.     bmp.size_y = y;
  145.    
  146.     return bmp;
  147.  
  148. }
  149.  
  150. // Pon una textura
  151. void SetTexture(I9_FILE texture){
  152.    
  153.     glBindTexture(0, texture.mem);
  154.     glTexImage2D(0, 0, GL_RGB, texture.size_x, texture.size_y, 0, TEXGEN_TEXCOORD, (u8*)texture.data);
  155. }
  156.  
  157. // Dibujar 3D
  158. void Draw(void) {
  159.  
  160.     glBegin(GL_QUADS);  // Dibujar cuadrados
  161.  
  162.     // Usar textura
  163.     glBindTexture(GL_TEXTURE_2D, textura.mem);
  164.  
  165.     // Cara frontal
  166.     glColor3f(1,1,1);
  167.     glTexCoord2f(0.0, 0.0);
  168.     glVertex3f(-1.0, -1.0,  1.0);
  169.     glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0,  1.0);
  170.     glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0,  1.0);
  171.     glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0,  1.0);
  172.     // Cara trasera (no dibujada)
  173.     glColor3f(1,0,0);
  174.     glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0);
  175.     glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0, -1.0);
  176.     glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0, -1.0);
  177.     glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0);
  178.     glEnd();
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement