/* * MotorJ image loading functions * * (C) 2007-2010 Alejandro Valenzuela Roca * * This file is part of MotorJ, a free framework for videogame creation. * * * * MotorJ is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * MotorJ is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with MotorJ. If not, see . */ /* * Warning: Some of these functions are likely to disappear in next release */ #ifndef NINTENDO_DS #ifndef NINTENDO_WII // FIXME: Must be substituted with Devkitarm's SDL_Image-equivalent porcedures #include "textures.h" void mjEnableSimpleTexturing(alphasettings enableAlpha) { glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); if (enableAlpha == ALPHA_ON) { glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); } glEnable(GL_TEXTURE_2D); } void mjEnableSimpleLighting(void) { GLfloat light_ambient[] = {0.8, 0.8, 0.8, 1.0}; GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat mat_ambient[]= {0.5, 0.5, 0.5, 1.0}; GLfloat mat_diffuse[] = {0.8, 0.8, 0.8, 1.0}; GLfloat mat_specular[] = {0.0, 0.0, 0.0, 1.0}; GLfloat mat_emission[] = {0.7, 0.7, 0.7, 1.0}; GLfloat mat_shininess[] = {0.0}; GLfloat global_ambient[] = {0.7, 0.7, 0.7, 1}; glEnable(GL_LIGHTING); glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightModelfv(GL_LIGHT_MODEL_AMBIENT,global_ambient); glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE); //glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient); //glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse); //glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular); //glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess); glEnable(GL_LIGHT0); glShadeModel (GL_SMOOTH); } void mjDefLightPos() { GLfloat def_light_pos[] = { 0, 1, 1, 0}; glLightfv(GL_LIGHT0, GL_POSITION, def_light_pos); } GLuint mjGLTextureFromFile(const char * filename) { SDL_Surface * tmp; char * proc_filename; GLuint tmpTexName; SDL_RWops *rwop; GLenum format; proc_filename = replace_dir_char(filename); rwop = SDL_RWFromFile(proc_filename, "rb"); tmp = IMG_Load(proc_filename); if (!tmp || !rwop) { debug("Could not open image file %s", proc_filename); return NULL; } // In SDL we trust. In whatever format it chooses to open the file. #ifdef MAC // Mac does things differently >_<* if (tmp->format->BitsPerPixel == 32) { // Either RGBA or BGRA if (IMG_isBMP(rwop) || IMG_isJPG(rwop)) { format = GL_BGRA; } else { format = GL_RGBA; } if (IMG_isPNG(rwop)) { format = GL_BGRA; } } else { // Either RGB or BGR if (IMG_isBMP(rwop) || IMG_isJPG(rwop)) { format = GL_BGR; } else { format = GL_RGB; } } #else if (tmp->format->BitsPerPixel == 32) { // Either RGBA or BGRA if (IMG_isBMP(rwop)) { format = GL_BGRA; } else { format = GL_RGBA; } } else { // Either RGB or BGR if (IMG_isBMP(rwop)) { format = GL_BGR; } else { format = GL_RGB; } } #endif SDL_FreeRW(rwop); glGenTextures(1, &tmpTexName); if (!tmpTexName) return NULL; glBindTexture(GL_TEXTURE_2D, tmpTexName); SDL_LockSurface(tmp); if (format == GL_RGBA || format == GL_BGRA) { if (gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA8, tmp->w, tmp->h, format, GL_UNSIGNED_BYTE, tmp->pixels)) { printf("Error during texture mipmap creation: %s\n",gluErrorString(0)); } //debug("%d x %d RBGA mipmap created for %s",tmp->w, tmp->h, filename); } else { if (gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGB8, tmp->w, tmp->h, format, GL_UNSIGNED_BYTE, tmp->pixels)) { printf("Error during texture mipmap creation: %s\n",gluErrorString(0)); } //debug("%d x %d RGB mipmap created for %s",tmp->w, tmp->h, filename); } SDL_UnlockSurface(tmp); SDL_FreeSurface(tmp); delete proc_filename; return tmpTexName; } GLubyte * carga_bmp_nxn_rgb(int n, char * ruta) { FILE * archivo; size_t bytesl; int errorF; GLubyte * resultado; char * ruta2; char bytesleidos[] = "10008000"; ruta2 = replace_dir_char(ruta); unsigned pos = 0; //pos2 = 0; //debug("abriendo"); //debug(ruta2); if (archivo = fopen(ruta2,"rb")) { //printf("Cargando textura desde %s\n",ruta); resultado = new GLubyte [3*n*n]; //if (resultado) printf("Calloco!\n"); //printf("fread...\n"); /* Desechamos el header file (14 bytes) y el info (40 bytes) y nos vamos directamente a lo que queremos.. */ fread(resultado,sizeof(GLubyte),54,archivo); bytesl = 0; if ((3*n*n) == (bytesl = fread(&resultado[bytesl],sizeof(GLubyte),3*n*n,archivo))){ //printf("Textura cargada con éxito (%ld bytes leidos según esto)\n",bytesl); fclose(archivo); /* Intercambiamos los bytes de B y R. Están invertidos, por razones del formato.. Por alguna razón, mi programa de edición de gráficos (GIMP - http://www.gimp.org/) no genera junk bytes.. */ for (pos = 0; pos < 3*n*n; pos+=3){ GLubyte tmp; tmp = resultado[pos]; resultado[pos] = resultado[pos+2]; resultado[pos+2] = tmp; } //printf("ruta2: %s, 0x%x\n",ruta2,ruta2); delete ruta2; //printf("bakaera no jutsu\n"); return resultado; } else { debug("error, no se leyeron todos los bytes"); if (errorF = ferror(archivo)){ debug("ferror is set"); } //sprintf(bytesleidos,"%d,%d",bytesl,errorF); // debug(bytesleidos); } //printf("error, liberando memoria\n"); fclose(archivo); delete ruta2; delete resultado; //} } //printf("regresando nulo\n"); return NULL; } #endif #endif