Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Includes
- #include <nds.h>
- #include <fat.h>
- #include <filesystem.h>
- #include <stdio.h>
- // Estructura de una textura
- typedef struct{
- u16 *data; // Datos a 16 bits
- u16 size_x; // Tamaño X
- u16 size_y; // Tamaño Y
- int mem; // Memoria asignada por OpenGL
- }I9_FILE;
- // Estructura de una camara
- typedef struct{
- float pos_x, pos_y, pos_z; // Posicion
- float eye_x, eye_y, eye_z; // Donde mira
- float inc_x, inc_y, inc_z: // ¿?
- }I9_CAMERA;
- // Funciones y defines
- void Draw(void); // Dibujar la escena
- void SetCamera(I9_CAMERA camera); // Pon al camara
- I9_FILE LoadTexture(const char* path, u16 x, u16 y);
- void SetTexture(I9_FILE texture);
- I9_FILE textura; // Textura a usar
- // Funcion Main()
- int main(int argc, char **argv){
- // Iniciar 3D y NitroFS
- videoSetMode(MODE_0_3D); // 3D modo 0
- vramSetBankA(VRAM_A_TEXTURE); // Activa el banco A para texturas
- memset((void*)0x06000000, 0, 131072);// Vacialo
- nitroFSInit(); // Inicia NitroFS
- /*************************************************************/
- // Iniciar OPENGL
- glInit();
- glEnable(GL_ANTIALIAS); // Inicia AA
- glClearColor(0, 0, 0, 31); // Color de fondo
- glClearPolyID(63); // ID de los poligonos a 63
- glClearDepth(0x7FFF); // Profundidad maxima
- glViewport(0, 0, 255, 191); // Toda la pantalla de 3D
- glEnable(GL_TEXTURE_2D); // Inicia texturas
- // Inicia proyeccion y perspectiva
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(70, 256.0/192.0, 0.1, 100);
- glPolyFmt(POLY_ALPHA(31) | POLY_CULL_BACK); // No dibuja caras traseras
- // Inicia matriz de modelado
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- /*************************************************************/
- // Inicia camara
- I9_CAMERA maincamera;
- maincamera.pos_x = 0.0f; maincamera.pos_y = 0.0f; maincamera.pos_z = -5.0f;
- maincamera.eye_x = 0.0f; maincamera.eye_y = 0.0f; maincamera.eye_z = 0.0f;
- maincamera.inc_x = 0.0f; maincamera.inc_y = 1.0f; maincamera.inc_z = 0.0f;
- // Cargar textura
- textura = LoadTexture("mitextura.raw", TEXTURE_SIZE_128, TEXTURE_SIZE_128);
- SetTexture(textura);
- // Bucle infinito
- while(1) {
- glPushMatrix();
- SetCamera(maincamera);
- /****************************************/
- // Move camera
- if(!(keysHeld() &KEY_L)){
- if(keysHeld() &KEY_RIGHT){maincamera.eye_x -=0.5f;maincamera.pos_x -=0.5f;}
- if(keysHeld() &KEY_LEFT){maincamera.eye_x +=0.5f;maincamera.pos_x +=0.5f;}
- if(keysHeld() &KEY_UP){maincamera.eye_z +=0.5f;maincamera.pos_z +=0.5f;}
- if(keysHeld() &KEY_DOWN){maincamera.eye_z -=0.5f;maincamera.pos_z -=0.5f;}
- }
- /****************************************/
- // Dibujado
- Draw();
- /****************************************/
- glPopMatrix(1);
- glFlush(0);
- scanKeys();
- swiWaitForVBlank();
- }
- return 0;
- }
- /***************************************************/
- // FUNCIONES
- // Pon la camara
- void SetCamera(I9_CAMERA camera){
- gluLookAt(camera.pos_x, camera.pos_y, camera.pos_z,
- camera.eye_x, camera.eye_y, camera.eye_z,
- camera.inc_x, camera.inc_y, camera.inc_z);
- }
- // Carga una textura en RAM
- I9_FILE LoadTexture(const char* path, u16 x, u16 y){
- // Abrir
- FILE *raw_file = fopen(path, "rb");
- if(raw_file == NULL){
- consoleDemoInit();
- iprintf("ERROR: Archivo %s no existe!\n", path);
- }
- // Tamaño
- fseek(raw_file, 0, SEEK_END);
- u16 size = ftell(raw_file);
- rewind(raw_file);
- if(size == 0 || size > 131072){
- consoleDemoInit();
- iprintf("ERROR: Tamano incorrecto!\n");
- }
- // Leer datos
- u16 *buffer = (u16*) calloc ((size*3), sizeof(u16)); // *3 por el tamaño, que es /3
- fread(buffer, 1, (size*3), raw_file);
- fclose(raw_file);
- if(buffer == NULL){
- consoleDemoInit();
- iprintf("ERROR: Buffer es NULL!\n");
- }
- // Pasalo a textura
- I9_FILE bmp;
- bmp.data = buffer;
- bmp.size = size;
- free(buffer);
- buffer = NULL;
- bmp.size_x = x;
- bmp.size_y = y;
- return bmp;
- }
- // Pon una textura
- void SetTexture(I9_FILE texture){
- glBindTexture(0, texture.mem);
- glTexImage2D(0, 0, GL_RGB, texture.size_x, texture.size_y, 0, TEXGEN_TEXCOORD, (u8*)texture.data);
- }
- // Dibujar 3D
- void Draw(void) {
- glBegin(GL_QUADS); // Dibujar cuadrados
- // Usar textura
- glBindTexture(GL_TEXTURE_2D, textura.mem);
- // Cara frontal
- glColor3f(1,1,1);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(-1.0, -1.0, 1.0);
- glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, 1.0);
- glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0);
- glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0);
- // Cara trasera (no dibujada)
- glColor3f(1,0,0);
- glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0);
- glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);
- glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, 1.0, -1.0);
- glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0);
- glEnd();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement