示例#1
0
文件: opengl_render.c 项目: zid/naev
/**
 * @brief Generates an filled circle texture.
 *
 *    @param radius Radius of the circle to generate.
 *    @return The tetxure containing the generated circle.
 */
static glTexture *gl_genCircle( int radius )
{
   int i,j,k, n,m;
   SDL_Surface *sur;
   uint8_t *pix, *buf;
   int h, w;
   double a;

   /* Calculate parameters. */
   w = 2*radius+1;
   h = 2*radius+1;

   /* Create the surface. */
   sur = SDL_CreateRGBSurface( SDL_SWSURFACE, w, h, 32, RGBAMASK );
   pix = sur->pixels;

   /* Generate the circle. */
   SDL_LockSurface( sur );

   /* Create temporary buffer to draw circle in. */
   k = 3;
   buf = malloc( (h*k) * (w*k) );
   for (i=0; i<k*h; i++) {
      for (j=0; j<k*w; j++) {
         if (pow2(i-k*radius)+pow2(j-k*radius) < pow2(k*radius))
            buf[ i*k*w + j] = 0xFF;
      }
   }

   /* Draw the circle with filter. */
   for (i=0; i<h; i++) {
      for (j=0; j<w; j++) {
         /* Calculate blur. */
         a = 0.;
         for (n=0; n<k; n++) {
            for (m=0; m<k; m++) {
               a += buf[ (i*k+n)*k*w + (j*k+m) ];
            }
         }
         a /= k*k;

         /* Set pixel. */
         pix[i*sur->pitch + j*4 + 0] = 0xFF;
         pix[i*sur->pitch + j*4 + 1] = 0xFF;
         pix[i*sur->pitch + j*4 + 2] = 0xFF;
         pix[i*sur->pitch + j*4 + 3] = (uint8_t)a;
      }
   }

   /* CLean up. */
   free(buf);

   SDL_UnlockSurface( sur );

   /* Return texture. */
   return gl_loadImage( sur, OPENGL_TEX_MIPMAPS );
}
示例#2
0
文件: map.c 项目: isfos/naev
/**
 * @brief Generates a texture to represent factions
 *
 * @param radius radius of the disk
 * @return the texture
 */
static glTexture *gl_genFactionDisk( int radius )
{
    int i, j;
    uint8_t *pixels;
    SDL_Surface *sur;
    int dist;
    double alpha;

    /* Calculate parameters. */
    const int w = 2 * radius + 1;
    const int h = 2 * radius + 1;

    /* Create the surface. */
    sur = SDL_CreateRGBSurface( SDL_SRCALPHA | SDL_HWSURFACE, w, h, 32, RGBAMASK );

    pixels = sur->pixels;
    memset(pixels, 0xff, sizeof(uint8_t) * 4 * h * w);

    /* Generate the circle. */
    SDL_LockSurface( sur );

    /* Draw the circle with filter. */
    for (i=0; i<h; i++) {
        for (j=0; j<w; j++) {
            /* Calculate blur. */
            dist = (i - radius) * (i - radius) + (j - radius) * (j - radius);
            alpha = 0.;

            if (dist < radius * radius) {
                /* Computes alpha with an empirically chosen formula.
                 * This formula accounts for the fact that the eyes
                 * has a logarithmic sensitivity to light */
                alpha = 1. * dist / (radius * radius);
                alpha = (exp(1 / (alpha + 1) - 0.5) - 1) * 0xFF;
            }

            /* Sets the pixel alpha which is the forth byte
             * in the pixel representation. */
            pixels[i*sur->pitch + j*4 + 3] = (uint8_t)alpha;
        }
    }

    SDL_UnlockSurface( sur );

    /* Return texture. */
    return gl_loadImage( sur, OPENGL_TEX_MIPMAPS );
}
示例#3
0
/**
 * @brief Generates nebula puffs.
 */
static void nebu_generatePuffs (void)
{
   int i;
   int w,h;
   SDL_Surface *sur;
   float *nebu;

   /* Warn user of what is happening. */
   loadscreen_render( 0.05, "Generating Nebula Puffs..." );

   /* Generate the nebula puffs */
   for (i=0; i<NEBULA_PUFFS; i++) {

      /* Generate the nebula */
      w = h = RNG(20,64);
      nebu = noise_genNebulaPuffMap( w, h, 1. );
      sur = nebu_surfaceFromNebulaMap( nebu, w, h );
      free(nebu);

      /* Load the texture */
      nebu_pufftexs[i] =  gl_loadImage( sur, 0 );
   }
}