[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));
break;
case 'G':
nebu_generate(SCREEN_W, SCREEN_H);
nebu_forceGenerate();
break;
case 'v':
/* By now it has already displayed the version. */

View File

@ -5,29 +5,24 @@
#include "lephisto.h"
#include "log.h"
#include "opengl.h"
#include "lfile.h"
#include "perlin.h"
/* -- CUSTOM NEBULAE FORMAT. --
* 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_Z 16 /* Z plane. */
#define NEBULAE_PATH "gen/nebu_%02d.png"
/* The nebulae textures. */
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 void saveNebulae(float* map, const uint32_t w, const uint32_t h,
const char* file);
static SDL_Surface* loadNebulae(const char* file);
static void nebu_generate(void);
/* Initialize the nebulae. */
void nebu_init(void) {
@ -35,6 +30,10 @@ void nebu_init(void) {
char nebu_file[PATH_MAX];
SDL_Surface* nebu_sur;
/* Special code to regenerate the nebulae. */
if((nebu_w == -9) && (nebu_h == -9))
nebu_generate();
/* Set expected sizes. */
nebu_w = SCREEN_W;
nebu_h = SCREEN_H;
@ -50,7 +49,7 @@ void nebu_init(void) {
LOG("No nebulae found, generating (this may take a while).");
/* So we generate and reload. */
nebu_generate(nebu_w, nebu_h);
nebu_generate();
nebu_init();
return;
}
@ -104,8 +103,8 @@ void nebu_render(void) {
tx = 0.;
ty = 0.;
tw = nebu_w / nebu_pw;
th = nebu_h / nebu_ph;
tw = (double)nebu_w / (double)nebu_pw;
th = (double)nebu_h / (double)nebu_ph;
glEnable(GL_TEXTURE_2D);
/*glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);*/
@ -131,13 +130,22 @@ void nebu_render(void) {
}
/* 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;
float* nebu;
char nebu_file[PATH_MAX];
int w, h;
w = SCREEN_W;
h = SCREEN_H;
/* Generate all the nebulae. */
nebu = noise_genNebulaeMap(w, h, NEBULAE_Z, 15.);
nebu = noise_genNebulaeMap(w, h, NEBULAE_Z, 5.);
lfile_dirMakeExist("gen");
/* Save each nebulae as an image. */

View File

@ -1,5 +1,4 @@
#pragma once
#include "opengl.h"
/* Try to find nebulae to load, or generate if there isn't any. */
void nebu_init(void);
@ -7,5 +6,6 @@ void nebu_init(void);
void nebu_exit(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) {
free((perlin_data_t*)noise);
free(noise);
}
/* 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;
float* nebulae;;
float value;
float zoom;
float max;
unsigned int* t, s;
/* Pretty default values. */
octaves = 3;
hurst = NOISE_DEFAULT_HURST;
lacunarity = NOISE_DEFAULT_LACUNARITY;
zoom = rug * ((float)h/768.)*((float)w/1024.);
/* Create noiuse and data. */
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);
/* Start to create the nebulae. */
max = 0.;
f[2] = 0.;
for(z = 0; z < n; z++) {
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++) {
f[0] = rug * (float)x / (float)w;
f[0] = zoom * (float)x / (float)w;
value = noise_turbulence(noise, f, octaves);
if(max < value) max = value;
value = value + 0.3;
nebulae[z*w*h + y*w+x] = (value < 1.) ? value : 1.;
nebulae[z*w*h + y*w+x] = value;
}
}
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);
}
/* 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. */
noise_delete(noise);
@ -241,7 +252,7 @@ SDL_Surface* noise_surfaceFromNebulaeMap(float* map, const int w, const int h) {
SDL_LockSurface(sur);
for(i = 0; i < h*w; 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);