[Fix] Nebulae is now fully working.

This commit is contained in:
Allanis 2013-07-19 15:01:48 +01:00
parent cd158e5102
commit 06fec494a5
4 changed files with 46 additions and 27 deletions

View File

@ -264,7 +264,7 @@ void conf_parseCLI(int argc, char** argv) {
sound_volume(atof(optarg)); sound_volume(atof(optarg));
break; break;
case 'G': case 'G':
nebu_generate(SCREEN_W, SCREEN_H); nebu_forceGenerate();
break; break;
case 'v': case 'v':
/* By now it has already displayed the version. */ /* By now it has already displayed the version. */

View File

@ -5,29 +5,24 @@
#include "lephisto.h" #include "lephisto.h"
#include "log.h" #include "log.h"
#include "opengl.h"
#include "lfile.h" #include "lfile.h"
#include "perlin.h" #include "perlin.h"
/* -- CUSTOM NEBULAE FORMAT. -- #define NEBULAE_Z 16 /* Z plane. */
* Header (16 byte string).
* Dimesnions (4 byte w and 4 byte h).
* Body (1 byte per pixel).
*/
#define NEBU_FORMAT_HEADER 16 /* Size of header. */
#define NEBU_VERSION "1" /* Will be used for version checking. */
#define NEBULAE_Z 32 /* Z plane. */
#define NEBULAE_PATH "gen/nebu_%02d.png" #define NEBULAE_PATH "gen/nebu_%02d.png"
/* The nebulae textures. */ /* The nebulae textures. */
static GLuint nebu_textures[NEBULAE_Z]; static GLuint nebu_textures[NEBULAE_Z];
static int nebu_w, nebu_h, nebu_pw, nebu_ph; static int nebu_w = 0;
static int nebu_h = 0;
static int nebu_pw, nebu_ph;
static int nebu_checkCompat(const char* file); static int nebu_checkCompat(const char* file);
static void saveNebulae(float* map, const uint32_t w, const uint32_t h, static void saveNebulae(float* map, const uint32_t w, const uint32_t h,
const char* file); const char* file);
static SDL_Surface* loadNebulae(const char* file); static SDL_Surface* loadNebulae(const char* file);
static void nebu_generate(void);
/* Initialize the nebulae. */ /* Initialize the nebulae. */
void nebu_init(void) { void nebu_init(void) {
@ -35,6 +30,10 @@ void nebu_init(void) {
char nebu_file[PATH_MAX]; char nebu_file[PATH_MAX];
SDL_Surface* nebu_sur; SDL_Surface* nebu_sur;
/* Special code to regenerate the nebulae. */
if((nebu_w == -9) && (nebu_h == -9))
nebu_generate();
/* Set expected sizes. */ /* Set expected sizes. */
nebu_w = SCREEN_W; nebu_w = SCREEN_W;
nebu_h = SCREEN_H; nebu_h = SCREEN_H;
@ -50,7 +49,7 @@ void nebu_init(void) {
LOG("No nebulae found, generating (this may take a while)."); LOG("No nebulae found, generating (this may take a while).");
/* So we generate and reload. */ /* So we generate and reload. */
nebu_generate(nebu_w, nebu_h); nebu_generate();
nebu_init(); nebu_init();
return; return;
} }
@ -104,8 +103,8 @@ void nebu_render(void) {
tx = 0.; tx = 0.;
ty = 0.; ty = 0.;
tw = nebu_w / nebu_pw; tw = (double)nebu_w / (double)nebu_pw;
th = nebu_h / nebu_ph; th = (double)nebu_h / (double)nebu_ph;
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
/*glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);*/ /*glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);*/
@ -131,13 +130,22 @@ void nebu_render(void) {
} }
/* Force generation of new nebulae. */ /* Force generation of new nebulae. */
void nebu_generate(const int w, const int h) { void nebu_forceGenerate(void) {
nebu_w = nebu_h = -9;
}
/* Generate the nebulae. */
static void nebu_generate(void) {
int i; int i;
float* nebu; float* nebu;
char nebu_file[PATH_MAX]; char nebu_file[PATH_MAX];
int w, h;
w = SCREEN_W;
h = SCREEN_H;
/* Generate all the nebulae. */ /* Generate all the nebulae. */
nebu = noise_genNebulaeMap(w, h, NEBULAE_Z, 15.); nebu = noise_genNebulaeMap(w, h, NEBULAE_Z, 5.);
lfile_dirMakeExist("gen"); lfile_dirMakeExist("gen");
/* Save each nebulae as an image. */ /* Save each nebulae as an image. */

View File

@ -1,5 +1,4 @@
#pragma once #pragma once
#include "opengl.h"
/* Try to find nebulae to load, or generate if there isn't any. */ /* Try to find nebulae to load, or generate if there isn't any. */
void nebu_init(void); void nebu_init(void);
@ -7,5 +6,6 @@ void nebu_init(void);
void nebu_exit(void); void nebu_exit(void);
void nebu_render(void); void nebu_render(void);
void nebu_generate(const int w, const int h);
void nebu_forceGenerate(void);

View File

@ -160,7 +160,7 @@ static float noise_turbulence(perlin_data_t* noise, float* f, float octaves) {
} }
void noise_delete(perlin_data_t* noise) { void noise_delete(perlin_data_t* noise) {
free((perlin_data_t*)noise); free(noise);
} }
/* Generate a 3d nebulae map of dimensions w,h,n with ruggedness rig. */ /* Generate a 3d nebulae map of dimensions w,h,n with ruggedness rig. */
@ -173,12 +173,15 @@ float* noise_genNebulaeMap(const int w, const int h, const int n, float rug) {
perlin_data_t* noise; perlin_data_t* noise;
float* nebulae;; float* nebulae;;
float value; float value;
float zoom;
float max;
unsigned int* t, s; unsigned int* t, s;
/* Pretty default values. */ /* Pretty default values. */
octaves = 3; octaves = 3;
hurst = NOISE_DEFAULT_HURST; hurst = NOISE_DEFAULT_HURST;
lacunarity = NOISE_DEFAULT_LACUNARITY; lacunarity = NOISE_DEFAULT_LACUNARITY;
zoom = rug * ((float)h/768.)*((float)w/1024.);
/* Create noiuse and data. */ /* Create noiuse and data. */
noise = noise_new(hurst, lacunarity); noise = noise_new(hurst, lacunarity);
@ -195,20 +198,21 @@ float* noise_genNebulaeMap(const int w, const int h, const int n, float rug) {
DEBUG("Generating Nebulae of size %dx%dx%d", w, h, n); DEBUG("Generating Nebulae of size %dx%dx%d", w, h, n);
/* Start to create the nebulae. */ /* Start to create the nebulae. */
max = 0.;
f[2] = 0.; f[2] = 0.;
for(z = 0; z < n; z++) { for(z = 0; z < n; z++) {
for(y = 0; y < h; y++) { for(y = 0; y < h; y++) {
f[1] = rug * (float)y / (float)h; f[1] = zoom * (float)y / (float)h;
for(x = 0; x < w; x++) { for(x = 0; x < w; x++) {
f[0] = rug * (float)x / (float)w; f[0] = zoom * (float)x / (float)w;
value = noise_turbulence(noise, f, octaves); value = noise_turbulence(noise, f, octaves);
if(max < value) max = value;
value = value + 0.3; nebulae[z*w*h + y*w+x] = value;
nebulae[z*w*h + y*w+x] = (value < 1.) ? value : 1.;
} }
} }
f[2] += 0.01; f[2] += 0.01;
@ -219,6 +223,13 @@ float* noise_genNebulaeMap(const int w, const int h, const int n, float rug) {
(z>0) ? t[z] - t[z-1] : t[z] - s); (z>0) ? t[z] - t[z-1] : t[z] - s);
} }
/* Post filtering. */
value = 1. - max;
for(z = 0; x < n; z++)
for(y = 0; y < h; y++)
for(x = 0; x < w; x++)
nebulae[z*w*h + y*w + x] += value;
/* Cleanup. */ /* Cleanup. */
noise_delete(noise); noise_delete(noise);
@ -241,7 +252,7 @@ SDL_Surface* noise_surfaceFromNebulaeMap(float* map, const int w, const int h) {
SDL_LockSurface(sur); SDL_LockSurface(sur);
for(i = 0; i < h*w; i++) { for(i = 0; i < h*w; i++) {
c = map[i]; c = map[i];
pix[i] = RMASK + BMASK + GMASK + (AMASK & (uint32_t)(AMASK*c)); pix[i] = RMASK + BMASK + GMASK + (AMASK & (uint32_t)((double)AMASK*c));
} }
SDL_UnlockSurface(sur); SDL_UnlockSurface(sur);