[Add] Animated nebulae ftw!!!
This commit is contained in:
parent
06fec494a5
commit
86f7480826
105
src/nebulae.c
105
src/nebulae.c
@ -7,8 +7,11 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "opengl.h"
|
#include "opengl.h"
|
||||||
#include "lfile.h"
|
#include "lfile.h"
|
||||||
|
#include "rng.h"
|
||||||
#include "perlin.h"
|
#include "perlin.h"
|
||||||
|
|
||||||
|
#define NEBU_DT_MAX 1.
|
||||||
|
|
||||||
#define NEBULAE_Z 16 /* Z plane. */
|
#define NEBULAE_Z 16 /* Z plane. */
|
||||||
#define NEBULAE_PATH "gen/nebu_%02d.png"
|
#define NEBULAE_PATH "gen/nebu_%02d.png"
|
||||||
|
|
||||||
@ -18,6 +21,10 @@ static int nebu_w = 0;
|
|||||||
static int nebu_h = 0;
|
static int nebu_h = 0;
|
||||||
static int nebu_pw, nebu_ph;
|
static int nebu_pw, nebu_ph;
|
||||||
|
|
||||||
|
/* Information on rendering. */
|
||||||
|
static int cur_nebu[2] = { 0, 1 };
|
||||||
|
static unsigned int last_render = 0;
|
||||||
|
|
||||||
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);
|
||||||
@ -60,6 +67,7 @@ void nebu_init(void) {
|
|||||||
WARN("Nebulae raw size doesn't match expected! (%dx%d instead of %dx%d)",
|
WARN("Nebulae raw size doesn't match expected! (%dx%d instead of %dx%d)",
|
||||||
nebu_sur->w, nebu_sur->h, nebu_pw, nebu_ph);
|
nebu_sur->w, nebu_sur->h, nebu_pw, nebu_ph);
|
||||||
|
|
||||||
|
/* Prepare to load into OpenGL. */
|
||||||
nebu_sur = gl_prepareSurface(nebu_sur);
|
nebu_sur = gl_prepareSurface(nebu_sur);
|
||||||
if((nebu_sur->w != nebu_pw) || (nebu_sur->h != nebu_ph))
|
if((nebu_sur->w != nebu_pw) || (nebu_sur->h != nebu_ph))
|
||||||
WARN("Nebulae size doesn't match expected! (%dx%d instead of %dx%d)",
|
WARN("Nebulae size doesn't match expected! (%dx%d instead of %dx%d)",
|
||||||
@ -70,6 +78,7 @@ void nebu_init(void) {
|
|||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
|
|
||||||
|
/* Store into opengl saving only alpha channel in video memory. */
|
||||||
SDL_LockSurface(nebu_sur);
|
SDL_LockSurface(nebu_sur);
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, nebu_sur->w, nebu_sur->h,
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, nebu_sur->w, nebu_sur->h,
|
||||||
0, GL_RGBA, GL_UNSIGNED_BYTE, nebu_sur->pixels);
|
0, GL_RGBA, GL_UNSIGNED_BYTE, nebu_sur->pixels);
|
||||||
@ -89,40 +98,94 @@ void nebu_exit(void) {
|
|||||||
|
|
||||||
/* Render the nebulae. */
|
/* Render the nebulae. */
|
||||||
void nebu_render(void) {
|
void nebu_render(void) {
|
||||||
int n;
|
unsigned int t;
|
||||||
double x, y, w, h, tx, ty, tw, th;
|
double dt;
|
||||||
|
double tw, th;
|
||||||
|
GLfloat col[4];
|
||||||
|
int tmp;
|
||||||
|
|
||||||
n = 0;
|
/* Calculate frame to draw. */
|
||||||
|
t = SDL_GetTicks();
|
||||||
|
dt = (t - last_render) / 1000.;
|
||||||
|
if(dt > NEBU_DT_MAX) { /* Time to change. */
|
||||||
|
tmp = cur_nebu[0];
|
||||||
|
cur_nebu[0] += cur_nebu[0] - cur_nebu[1];
|
||||||
|
cur_nebu[1] = tmp;
|
||||||
|
if(cur_nebu[0]+1 > NEBULAE_Z)
|
||||||
|
cur_nebu[0] = NEBULAE_Z - 2;
|
||||||
|
else if(cur_nebu[0] < 0)
|
||||||
|
cur_nebu[0] = 1;
|
||||||
|
|
||||||
x = -SCREEN_W/2.;
|
last_render = t;
|
||||||
y = -SCREEN_H/2.;
|
dt = 0.;
|
||||||
|
}
|
||||||
|
|
||||||
w = SCREEN_W;
|
col[0] = cPurple.r;
|
||||||
h = SCREEN_H;
|
col[1] = cPurple.g;
|
||||||
|
col[2] = cPurple.b;
|
||||||
tx = 0.;
|
col[3] = dt / NEBU_DT_MAX;
|
||||||
ty = 0.;
|
|
||||||
|
|
||||||
tw = (double)nebu_w / (double)nebu_pw;
|
tw = (double)nebu_w / (double)nebu_pw;
|
||||||
th = (double)nebu_h / (double)nebu_ph;
|
th = (double)nebu_h / (double)nebu_ph;
|
||||||
|
|
||||||
|
/* Set up the targets. */
|
||||||
|
/* Texture 0. */
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glEnable(GL_TEXTURE_2D);
|
glEnable(GL_TEXTURE_2D);
|
||||||
/*glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);*/
|
glBindTexture(GL_TEXTURE_2D, nebu_textures[cur_nebu[0]]);
|
||||||
glBindTexture(GL_TEXTURE_2D, nebu_textures[n]);
|
|
||||||
COLOUR(cPurple);
|
/* Texture 1. */
|
||||||
|
glActiveTexture(GL_TEXTURE1);
|
||||||
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, nebu_textures[cur_nebu[1]]);
|
||||||
|
|
||||||
|
/* Prepare it. */
|
||||||
|
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
|
||||||
|
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
|
||||||
|
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_INTERPOLATE);
|
||||||
|
/* Colour. */
|
||||||
|
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, col);
|
||||||
|
|
||||||
|
/* Arguments. */
|
||||||
|
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_CONSTANT);
|
||||||
|
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE1);
|
||||||
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
|
||||||
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
|
||||||
|
/* Arg 1. */
|
||||||
|
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_CONSTANT);
|
||||||
|
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_PREVIOUS);
|
||||||
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
|
||||||
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
|
||||||
|
/* Arg 2. */
|
||||||
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_CONSTANT);
|
||||||
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_CONSTANT);
|
||||||
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);
|
||||||
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);
|
||||||
|
|
||||||
|
/* Now render. */
|
||||||
glBegin(GL_QUADS);
|
glBegin(GL_QUADS);
|
||||||
glTexCoord2d(tx, ty);
|
glMultiTexCoord2d(GL_TEXTURE0, 0., 0.);
|
||||||
glVertex2d(x, y);
|
glMultiTexCoord2d(GL_TEXTURE1, 0., 0.);
|
||||||
|
glVertex2d(-SCREEN_W/2., -SCREEN_H/2.);
|
||||||
|
|
||||||
glTexCoord2d(tx+tw, ty);
|
glMultiTexCoord2d(GL_TEXTURE0, tw, 0.);
|
||||||
glVertex2d(x+w, y);
|
glMultiTexCoord2d(GL_TEXTURE1, tw, 0.);
|
||||||
|
glVertex2d(SCREEN_W/2., -SCREEN_H/2.);
|
||||||
|
|
||||||
glTexCoord2d(tx+tw, ty+th);
|
glMultiTexCoord2d(GL_TEXTURE0, tw, th);
|
||||||
glVertex2d(x+w, y+h);
|
glMultiTexCoord2d(GL_TEXTURE1, tw, th);
|
||||||
|
glVertex2d(SCREEN_W/2., SCREEN_H/2.);
|
||||||
|
|
||||||
glTexCoord2d(tx, ty+th);
|
glMultiTexCoord2d(GL_TEXTURE0, 0., th);
|
||||||
glVertex2d(x, y+h);
|
glMultiTexCoord2d(GL_TEXTURE1, 0., th);
|
||||||
|
glVertex2d(-SCREEN_W/2., SCREEN_H/2.);
|
||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
|
/* Clean up. */
|
||||||
|
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||||
|
glDisable(GL_TEXTURE_2D);
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||||
glDisable(GL_TEXTURE_2D);
|
glDisable(GL_TEXTURE_2D);
|
||||||
|
|
||||||
/* Did anything fail? */
|
/* Did anything fail? */
|
||||||
|
@ -183,7 +183,7 @@ float* noise_genNebulaeMap(const int w, const int h, const int n, float rug) {
|
|||||||
lacunarity = NOISE_DEFAULT_LACUNARITY;
|
lacunarity = NOISE_DEFAULT_LACUNARITY;
|
||||||
zoom = rug * ((float)h/768.)*((float)w/1024.);
|
zoom = rug * ((float)h/768.)*((float)w/1024.);
|
||||||
|
|
||||||
/* Create noiuse and data. */
|
/* Create noise and data. */
|
||||||
noise = noise_new(hurst, lacunarity);
|
noise = noise_new(hurst, lacunarity);
|
||||||
|
|
||||||
nebulae = malloc(sizeof(float)*w*h*n);
|
nebulae = malloc(sizeof(float)*w*h*n);
|
||||||
|
Loading…
Reference in New Issue
Block a user