13#include "background.h"
23#include "nlua_camera.h"
24#include "nlua_colour.h"
36typedef struct background_image_s {
89static int bkg_compare(
const void *p1,
const void *p2 );
101 GLfloat *dust_vertex;
103 NTracingZone( _ctx, 1 );
107 size /=
pow2( conf.zoom_far );
111 w += ( w / conf.zoom_far - 1. );
113 h += ( h / conf.zoom_far - 1. );
119 ndust = (
unsigned int)( size / ( 800. * 600. ) );
122 dust_vertex = malloc(
ndust *
sizeof( GLfloat ) * 3 );
124 for (
unsigned int i = 0; i <
ndust; i++ ) {
126 dust_vertex[3 * i + 0] = RNGF() * w - hw;
127 dust_vertex[3 * i + 1] = RNGF() * h - hh;
129 dust_vertex[3 * i + 2] = RNGF() * 0.6 + 0.2;
139 NTracingZoneEnd( _ctx );
164 NTracingZone( _ctx, 1 );
170 projection = gl_view_matrix;
171 mat4_translate_scale_xy( &projection, SCREEN_W / 2., SCREEN_H / 2., z, z );
174 if ( (
player.p != NULL ) && !player_isFlag( PLAYER_DESTROYED ) &&
175 !player_isFlag( PLAYER_CREATING ) ) {
180 vmod = hypot( dx, dy );
182 if ( pilot_isFlag(
player.p,
183 PILOT_HYPERSPACE ) ) {
185 m =
MAX( 0, HYPERSPACE_DUST_BLUR -
player.p->ptimer );
187 m *= HYPERSPACE_DUST_LENGTH / HYPERSPACE_DUST_BLUR;
188 angle = atan2( dy, dx );
191 }
else if (
dt_mod * vmod > 500. ) {
192 angle = atan2( dy, dx );
193 m = (
dt_mod * vmod ) / 25. - 20.;
200 w += ( w / conf.zoom_far - 1. );
202 h += ( h / conf.zoom_far - 1. );
205 glUseProgram( shaders.dust.program );
206 gl_uniformMat4( shaders.dust.projection, &projection );
209 glUniform3f( shaders.dust.dims, 1. /
gl_screen.scale, 0., 0. );
211 glUniform3f( shaders.dust.dims,
213 glUniform3f( shaders.dust.screen, w, h, 1. /
gl_screen.scale );
214 glUniform1i( shaders.dust.use_lines, !points );
217 glEnableVertexAttribArray( shaders.dust.vertex );
218 glEnableVertexAttribArray( shaders.dust.brightness );
222 GL_FLOAT, 3 *
sizeof( GLfloat ) );
224 2 *
sizeof( GLfloat ), 1, GL_FLOAT,
225 3 *
sizeof( GLfloat ) );
226 glDrawArrays( GL_POINTS, 0,
ndust );
229 glDisableVertexAttribArray( shaders.dust.vertex );
230 glDisableVertexAttribArray( shaders.dust.brightness );
237 NTracingZoneEnd( _ctx );
247 NTracingZone( _ctx, 1 );
251 lua_pushnumber(
naevL, dt );
253 WARN( _(
"Background script 'renderbg' error:\n%s" ),
254 lua_tostring(
naevL, -1 ) );
263 lua_pushnumber(
naevL, dt );
265 WARN( _(
"Background script 'rendermg' error:\n%s" ),
266 lua_tostring(
naevL, -1 ) );
276 lua_pushnumber(
naevL, dt );
278 WARN( _(
"Background script 'renderfg' error:\n%s" ),
279 lua_tostring(
naevL, -1 ) );
284 NTracingZoneEnd( _ctx );
292 NTracingZone( _ctx, 1 );
296 lua_pushnumber(
naevL, dt );
298 WARN( _(
"Background script 'renderov' error:\n%s" ),
299 lua_tostring(
naevL, -1 ) );
304 NTracingZoneEnd( _ctx );
329 double move,
double scale,
double angle,
330 const glColour *col,
int foreground,
331 const glColour *radiosity )
355 bkg->
col = ( col != NULL ) ? *col : cWhite;
365 bkg->
L_idx = L_default.nlights - L_default_const.nlights;
379 if ( gltf_lightSet( bkg->
L_idx, &bkg->
L ) )
395 if ( conf.bg_brightness <= 0. )
399 for (
int i = 0; i <
array_size( bkg_arr ); i++ ) {
400 double cx, cy, x, y, rx, ry, gx, gy, z, m;
409 rx = ( bkg->
x - cx ) * m + gx;
410 ry = ( bkg->
y - cy ) * m + gy;
412 y = ry + SCREEN_H / 2. - z * bkg->
image->
sw / 2.;
413 x = rx + SCREEN_W / 2. - z * bkg->
image->
sh / 2.;
417 col.r = bkg->
col.r * conf.bg_brightness;
418 col.g = bkg->
col.g * conf.bg_brightness;
419 col.b = bkg->
col.b * conf.bg_brightness;
426 if ( bkg->
L_idx >= 0 ) {
427 double d = hypot( rx, ry );
436 bkg->
L.
pos.v[0] = rx;
437 bkg->
L.
pos.v[1] = 2. *
d - 300.;
438 bkg->
L.
pos.v[2] = ry;
439 gltf_lightSet( bkg->
L_idx, &bkg->
L );
455 snprintf( path,
sizeof( path ), BACKGROUND_PATH
"%s.lua", name );
458 env = nlua_newEnv( name );
469 WARN( _(
"Background script '%s' not found." ), path );
475 if ( nlua_dobufenv( env, buf, bufsize, path ) != 0 ) {
476 WARN( _(
"Error loading background file: %s\n"
478 "Most likely Lua file has improper syntax, please check" ),
479 path, lua_tostring(
naevL, -1 ) );
507 NTracingZone( _ctx, 1 );
521 if ( env == LUA_NOREF ) {
522 NTracingZoneEnd( _ctx );
527 nlua_getenv(
naevL, env,
"background" );
528 ret = nlua_pcall( env, 0, 0 );
531 ( lua_isstring(
naevL, -1 ) ) ? lua_tostring(
naevL, -1 ) : NULL;
532 WARN( _(
"Background -> 'background' : %s" ),
533 ( err ) ? err : _(
"unknown error" ) );
543 NTracingZoneEnd( _ctx );
590 for (
int i = 0; i <
array_size( *arr ); i++ ) {
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
#define array_end(array)
Returns a pointer to the end of the reserved memory space.
#define array_create_size(basic_type, capacity)
Creates a new dynamic array of ‘basic_type’ with an initial capacity.
#define array_erase(ptr_array, first, last)
Erases elements in interval [first, last).
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
#define array_grow(ptr_array)
Increases the number of elements by one and returns the last element.
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
#define array_begin(array)
Returns a pointer to the beginning of the reserved memory space.
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
static background_image_t * bkg_image_arr_bk
glTexture ** background_getStarTextures(void)
Returns an array (array.h) of star background images in the system background.
void background_clear(void)
Cleans up the background stuff.
int background_init(void)
Initializes the background system.
static unsigned int bkg_idgen
static int bkg_L_renderov
static background_image_t * bkg_image_arr_ft
void background_initDust(int n)
Initializes background dust.
static int bkg_L_renderbg
void background_renderOverlay(double dt)
Renders the background overlay.
static int bkg_L_rendermg
static nlua_env bkg_def_env
void background_moveDust(double x, double y)
Displaces the dust, useful with camera.
static int bkg_L_renderfg
void background_free(void)
Cleans up and frees memory after the backgrounds.
static void background_clearCurrent(void)
Destroys the current running background script.
static int bkg_compare(const void *p1, const void *p2)
Compares two different backgrounds and sorts them.
static void background_renderImages(background_image_t *bkg_arr)
Renders the background images.
glTexture * background_getAmbientTexture(void)
Returns an overall background image (nebula, for instance), or NULL if none exists.
void background_renderDust(const double dt)
Renders the dustry background.
static void background_clearImgArr(background_image_t **arr)
Clears a background image array.
static void bkg_sort(background_image_t *arr)
Sorts the backgrounds by movement.
static gl_vbo * dust_vertexVBO
static nlua_env background_create(const char *path)
Creates a background Lua state from a script.
void background_render(double dt)
Render the background.
static nlua_env bkg_cur_env
Backgrounds.
static unsigned int ndust
int background_load(const char *name)
Loads a background script by name.
unsigned int background_addImage(const glTexture *image, double x, double y, double move, double scale, double angle, const glColour *col, int foreground, const glColour *radiosity)
Adds a new background image.
void cam_getVel(double *vx, double *vy)
Gets the camera velocity.
void cam_getPos(double *x, double *y)
Gets the camera position.
double cam_getZoom(void)
Gets the camera zoom.
void gui_getOffset(double *x, double *y)
Gets the GUI offset.
Header file with generic functions and naev-specifics.
void * ndata_read(const char *path, size_t *filesize)
Reads a file from the ndata (will be NUL terminated).
int nlua_refenv(nlua_env env, const char *name)
Gets the reference of a global in a lua environment.
int nlua_loadStandard(nlua_env env)
Loads the standard Naev Lua API.
int nlua_loadBackground(nlua_env env)
Loads the graphics library.
int nlua_loadCamera(nlua_env env)
Loads the camera library.
int nlua_loadCol(nlua_env env)
Loads the colour library.
int nlua_loadGFX(nlua_env env)
Loads the graphics library.
int nlua_loadTex(nlua_env env)
Loads the texture library.
void gl_renderTexture(const glTexture *texture, double x, double y, double w, double h, double tx, double ty, double tw, double th, const glColour *c, double angle)
Texture blitting backend.
glTexture * gl_dupTexture(const glTexture *texture)
Duplicates a texture.
void gl_freeTexture(glTexture *texture)
Frees a texture.
void gl_vboDestroy(gl_vbo *vbo)
Destroys a VBO.
void gl_vboActivateAttribOffset(gl_vbo *vbo, GLuint index, GLuint offset, GLint size, GLenum type, GLsizei stride)
Activates a VBO's offset.
gl_vbo * gl_vboCreateStatic(GLsizei size, const void *data)
Creates a stream vbo.
Simple point/sun light model.
Represents a background image like say a Nebula.
Abstraction for rendering sprite sheets.