Raydium 3D Game Engine

Official forum for everything about Raydium, ManiaDrive, MeMak, ...
It is currently Wed Sep 23, 2020 12:19 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu May 28, 2009 1:32 pm 
Offline
User avatar

Joined: Thu Sep 29, 2005 2:59 pm
Posts: 828
I've been asked for adding more image formats in raydium(world). I'll making a tests with soil, and looks fine.
However merging external image libraries with raydium is not an simple task. The raydium_texture_load_internal is too heavy to allow easy modifications. And so I ask:

Can the creator of this function (maybe xfennec?ouille?) modularize it a bit more?
I think raydium_texture_load_internal could call to raydium_texture_load_internal_tga.
In this way all the TGA related code will be in another function and the load_internal could be flexible enough to allow us to add soil calls here.

Currently I'm doing my test with this base:
Code:
char file2[RAYDIUM_MAX_DIR_LEN];
 raydium_path_resolv(filename,file2,'r');
 raydium_log("SOIL trying to open %s",file2);
 tmpres=SOIL_load_OGL_texture(file2,
      SOIL_LOAD_AUTO,
      SOIL_CREATE_NEW_ID,
      SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT);



Thanks


Top
 Profile  
 
PostPosted: Fri May 29, 2009 9:58 am 
Offline
User avatar

Joined: Thu Sep 29, 2005 2:59 pm
Posts: 828
Well, meanwhile someone do something with the official code, I 've done a (more or less) working test.
It compiles and works with simple textures, and environment, and lightmap textures (as long as I tested). The plain rgb textures and fonts are working wrong. I don't understand well the video textures so I didn't code them...
The good point, as this code uses SOIL you can load almost any image format file (jpeg, tga,psd,dds...).
This should +- work for people asking me for more image files, but there is place for a loooot of improvements and bugfixes.

odyncomp.sh
(just added the library for compiling)

Code:
...
$CC "$1" -g -Wall -DFORCE_LIBRAYDIUM -o test libraydium.so \
-Iraydium/php/ -Isoil/src/ -Iraydium/php/main/ -Iraydium/php/Zend -Iraydium/php/TSRM \
-Iraydium/ode/include/ libSOIL.a $AR_PATH_INCLUDE $AR_PATH_LIBS $LIBWII_PATH_INCLUDE $LIBWII_ADDS $ODYNCOMP_FLAGS
...


texture.c
(Just new include and full rewrite of raydium_texture_load_internal function)
Code:
...
#include "../soil/src/SOIL.h"
...

...

GLuint
raydium_texture_load_internal (char *filename, char *as, signed char faked,
                int faked_tx, int faked_ty, int faked_bpp,
                int or_live_id_fake)
{
//VARIABLES     
  int id = -1;
  int tex_2d = 0;
  int simulate = 0;
  int rgb = 0;
  int reflect = 0;
  int lightmap = 0;
  int hdr = 0;
  int nolight = 0;
  int blended = 0;
  int infox = 0;
  int infoy = 0;
  int infocomp = 0;
  int cutout = 0;
  float r = 0.0f;
  float g = 0.0f;
  float b = 0.0f;
  unsigned char filename2[RAYDIUM_MAX_NAME_LEN];
  unsigned char temp[RAYDIUM_MAX_NAME_LEN];
  unsigned char *img;
  unsigned int soil_flags = SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT;

  /*flags
     SOIL_FLAG_POWER_OF_TWO
     SOIL_FLAG_MIPMAPS
     SOIL_FLAG_TEXTURE_REPEATS
     SOIL_FLAG_MULTIPLY_ALPHA
     SOIL_FLAG_INVERT_Y
     SOIL_FLAG_COMPRESS_TO_DXT
     SOIL_FLAG_DDS_LOAD_DIRECT
   */


//ALREADY LOADED? SKIP
  //check if already loaded
  for (id = 1; id < RAYDIUM_MAX_TEXTURES; id++)
    {
      if (raydium_texture_used[id])
   {
     if (!strcmp (raydium_texture_name[id], as))
       {
         raydium_log ("texture: (internal) %s is duplicated", as);
         return id;
       }
   }
    }
  //is not already loaded

//REPLACE TEXTURE OR NEW TEXTURE
  if (raydium_texture_to_replace)
    {
      id = raydium_texture_to_replace;
      //TODO:WARNING: Not tested!!!!
      raydium_texture_free (raydium_texture_to_replace);
      raydium_texture_to_replace = 0;
    }
  else
    {
      //check if there is one texture slot free
      id = raydium_texture_get_next_free_slot_internal ();
    }
  if ((int) id == -1)      //Not free slot :(
    {
      raydium_log ("texture: No more texture slots left ! (%i max)",
         RAYDIUM_MAX_TEXTURES);
      return 0;
    }

//SIMULATING? (what is this for?....)
  //check if needed to simulate
  if (raydium_window_mode == RAYDIUM_RENDERING_NONE)
    {
      simulate = 1;
    }


//RGB COLOR
  //check if it's a plain color
  strcpy ((char *) temp, filename);
  temp[4] = 0;
//Is an RGB color?
  if (!strcmp ("rgb(", (char *) temp))
    {
      rgb = 1;
      //
      sscanf (filename, "rgb(%f,%f,%f)", &r, &g, &b);
      raydium_texture_rgb[id][0] = r;
      raydium_texture_rgb[id][1] = g;
      raydium_texture_rgb[id][2] = b;
      raydium_texture_rgb[id][3] = 1.f;
      if (r < 0 && g < 0 && b < 0)
   {
     blended = RAYDIUM_TEXTURE_PHANTOM;
     raydium_log ("Tex. num %i is Phantom (depth buffer only)", id);
   }
      else
   {
     blended = 0;
     raydium_log ("Tex. num %i, rgb(%f,%f,%f) is RGB Color", id, r, g,
             b);
   }
    }
  else if (!faked)
//IT'S AN IMAGE
    {
      rgb = 0;
//ENV
      //check if environment map
      strcpy ((char *) temp, filename);
      temp[3] = 0;
      //Check if it's an ENV mapping texture?
      if (!strcmp ("ENV", (char *) temp))
   {
     reflect = 1;
   }

      //check if Nonpoweroftwo textures are used
      if (!raydium_texture_use_npot_textures ||
     (!glutExtensionSupported ("GL_ARB_texture_non_power_of_two") &&
      !glutExtensionSupported ("GL_ARB_texture_rectangle")))
   {
     soil_flags = soil_flags | SOIL_FLAG_POWER_OF_TWO;
   }

//LIGHTMAP     
      if (strstr (filename, ".tri."))
   {
     lightmap = 1;
   }

//DDS
      if (strstr (filename, ".dds"))
   {
     //soil_flags=SOIL_FLAG_INVERT_Y | SOIL_FLAG_MIPMAPS ;
   }

//HDR
      memcpy (temp, filename, 3);
      if (!strcmp ("HDR", (char *) temp))   // TEMP !!
   {
     hdr = 1;
     nolight = 1;
   }

//BOX
      memcpy (temp, filename, 3);
      temp[3] = 0;
      if (!strcmp ("BOX", (char *) temp) || faked)
   {
     soil_flags = soil_flags;
   }
      else
   {
     soil_flags = soil_flags | SOIL_FLAG_TEXTURE_REPEATS;
   }

//LOAD TEXTURE
      //resolve filename     
      raydium_path_resolv (as, filename2, 'r');   //resolv the name of the image
      raydium_log ("SOIL goes to load %s", filename2);

      //GET IMAGE INFO
      img = SOIL_load_image (filename2, &infox, &infoy, &infocomp, 0);
      SOIL_free_image_data (img);   //TODO: Waste of time....       
      raydium_log ("Image has size %dx%d with %d channels", infox, infoy,
         infocomp);

      //load texture into id slot
      if (!simulate)
   id = SOIL_load_OGL_texture
     (filename2, SOIL_LOAD_AUTO, id, soil_flags);

//check texture slot after load
      if (id == 0)
   {
     raydium_log ("About texture file: %s ( %s )", as, filename2);
     raydium_log ("SOIL loading error: '%s'\n", SOIL_last_result ());
   }
      else
   {
     raydium_log ("Textures loaded ok!. Message: %s",
             SOIL_last_result ());
   }
//in this point the texture has been succesfully loaded


//check channels
      switch (infocomp)      //How many channels?
   {
   case 0:
     break;
   case 1:      //GREY
     soil_flags =
       soil_flags | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_MIPMAPS;
     blended = 1;
     break;
   case 2:      // just 2 channels???
     soil_flags = soil_flags | SOIL_FLAG_MIPMAPS;
     break;
   case 3:      //RGB
     soil_flags = soil_flags | SOIL_FLAG_MIPMAPS;
     break;
   case 4:      //RGBA
     soil_flags =
       soil_flags | SOIL_FLAG_MIPMAPS | SOIL_FLAG_MULTIPLY_ALPHA;
     if (temp[3] > 0 && temp[3] < 255)
       blended = 1;
     if (temp[3] == 0)
       cutout = 1;
     break;
   default:
     break;
   }
    }
  else            //It's a faked image
    {
      //TODO: I gues...
      return 0;
    }

  //store its name
  strcpy (raydium_texture_name[id], as);

  //check if it has HDR
  raydium_texture_hdr[id] = hdr;

  //check if the texture ignores the light
  raydium_texture_nolight[id] = nolight;

  //check if the texture is part of an environment
  raydium_texture_env[id] = reflect;

  //check the size of the texture
  raydium_texture_memory[id] = infox * infoy * infocomp;   //TODO:wrong approximation

  //check if it's a lightmap
  raydium_texture_islightmap[id] = lightmap;

  //check if it's a blender texture
  if (cutout && !blended)
    {
      blended = 2;
    }
  raydium_texture_blended[id] = blended;

  //mark its slot as used
  raydium_texture_used[id] = 1;

  //returns id of the used slot       
  return id;
}
...


Xfennec: Indented with (GNU) indent application sorry, no time for manual indentation right now ;)


Top
 Profile  
 
PostPosted: Wed Jun 03, 2009 9:29 pm 
Offline
User avatar

Joined: Sun Mar 16, 2003 2:53 am
Posts: 2591
Location: gnniiiii (Scrat)
The trouble here is that it's not only about alternative texture loading, but also about texture managing (uploading it to OpenGL, creating mipmaps, clamp-to-edge, ...). Splitting raydium_texture_load_internal() in half IS a good idea, for sure, but it's also half of the solution ... We can't reasonably let people deal with their "own" texture formats like this.

A correct solution, IMHO, would be to create some sort of "texture format handlers" where people could register their own extensions. Something like raydium_texture_handler_add("jpg",my_jpeg_loading_handler) where you'll decode jpeg file by yourself from a FILE* and to a colour buffer (or something like that). Current Raydium format (tga, rgb, ...) should be internally managed with handlers too. Handlers would be able to read and write a struct where texture "options" could be defined (lightmap ? HDR ? reflect ? blended ? ...)

Not -that- easy to write, if you want my opinion :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group