summaryrefslogtreecommitdiff
path: root/src/modules/stars/stars.c
blob: bff3d8ffd540a378d8a3ab1df724f0d549803a63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>

#include "til.h"
#include "til_fb.h"
#include "til_module_context.h"
#include "til_settings.h"
#include "til_stream.h"
#include "til_tap.h"

#include "draw.h"

/* Copyright (C) 2017-20 Philip J. Freeman <elektron@halo.nu> */

#define DEFAULT_ROT_ADJ	.00003

struct points {
	float			x, y, z;
	struct			points *next;
};

typedef struct stars_context_t {
	til_module_context_t	til_module_context;
	struct			points* points;

	struct {
		til_tap_t		rot_rate;
		til_tap_t		rot_angle;
		til_tap_t		offset_x;
		til_tap_t		offset_y;
		til_tap_t		offset_angle;
	}			taps;

	struct {
		float			rot_rate;
		float			rot_angle;
		float			offset_x;
		float			offset_y;
		float			offset_angle;
	}			vars;

	float			rot_adj;
	float			*rot_rate;
	float			*rot_angle;
	float			*offset_x;
	float			*offset_y;
	float			*offset_angle;
	unsigned		seed;
} stars_context_t;

typedef struct stars_setup_t {
	til_setup_t		til_setup;
	float			rot_adj;
} stars_setup_t;


float get_random_unit_coord(unsigned *seed) {
	return (((float)rand_r(seed)/(float)RAND_MAX)*2.0)-1.0;
}


static void stars_update_taps(stars_context_t *ctxt, til_stream_t *stream, float dt)
{
	if (!til_stream_tap_context(stream, &ctxt->til_module_context, NULL, &ctxt->taps.rot_angle))
		*ctxt->rot_angle += (*ctxt->rot_rate * dt);

	if (!til_stream_tap_context(stream, &ctxt->til_module_context, NULL, &ctxt->taps.rot_rate)) {
		// handle rotation parameters
		if(*ctxt->rot_angle>M_PI_4)
			*ctxt->rot_rate = *ctxt->rot_rate - (ctxt->rot_adj * dt);
		else
			*ctxt->rot_rate = *ctxt->rot_rate + (ctxt->rot_adj * dt);
	}

	/* there's no automation of offset_angle */
	(void) til_stream_tap_context(stream, &ctxt->til_module_context, NULL, &ctxt->taps.offset_angle);

	// handle offset parameters
	if (!til_stream_tap_context(stream, &ctxt->til_module_context, NULL, &ctxt->taps.offset_x)) {
		float tmp_x = (*ctxt->offset_x*cosf(*ctxt->offset_angle * dt))-
				(*ctxt->offset_y*sinf(*ctxt->offset_angle * dt));
		*ctxt->offset_x = tmp_x;
	}

	if (!til_stream_tap_context(stream, &ctxt->til_module_context, NULL, &ctxt->taps.offset_y)) {
		float tmp_y = (*ctxt->offset_x*sinf(*ctxt->offset_angle * dt))+
				(*ctxt->offset_y*cosf(*ctxt->offset_angle * dt));
		*ctxt->offset_y = tmp_y;
	}
}

static til_module_context_t * stars_create_context(const til_module_t *module, til_stream_t *stream, unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
	stars_context_t *ctxt;
	float		z;
	struct points* p_ptr = NULL;

	ctxt = til_module_context_new(module, sizeof(stars_context_t), stream, seed, ticks, n_cpus, setup);
	if (!ctxt)
		return NULL;

	ctxt->points = NULL;
	ctxt->seed = seed;
	ctxt->rot_adj = ((stars_setup_t *)setup)->rot_adj;
	ctxt->vars.offset_x = 0.5;
	ctxt->vars.offset_angle = 0.01;

	ctxt->taps.rot_rate = til_tap_init_float(ctxt, &ctxt->rot_rate, 1, &ctxt->vars.rot_rate, "rot_rate");
	ctxt->taps.rot_angle = til_tap_init_float(ctxt, &ctxt->rot_angle, 1, &ctxt->vars.rot_angle, "rot_angle");
	ctxt->taps.offset_x = til_tap_init_float(ctxt, &ctxt->offset_x, 1, &ctxt->vars.offset_x, "offset_x");
	ctxt->taps.offset_y = til_tap_init_float(ctxt, &ctxt->offset_y, 1, &ctxt->vars.offset_y, "offset_y");
	ctxt->taps.offset_angle = til_tap_init_float(ctxt, &ctxt->offset_angle, 1, &ctxt->vars.offset_angle, "offset_angle");

	//add a bunch of points
	for(z=0.01; z<1; z=z+0.01) {
		for(int i=0; i<rand_r(&ctxt->seed)%16; i++){
			p_ptr = malloc(sizeof(struct points));
			if (!p_ptr)
				return NULL;
			p_ptr->x = get_random_unit_coord(&ctxt->seed);
			p_ptr->y = get_random_unit_coord(&ctxt->seed);
			p_ptr->z = z;
			p_ptr->next = ctxt->points;
			ctxt->points = p_ptr;
		}
	}

	stars_update_taps(ctxt, stream, 0.f);

	return &ctxt->til_module_context;
}

static void stars_destroy_context(til_module_context_t *context)
{
	stars_context_t *ctxt = (stars_context_t *)context;
	struct points* p_ptr;
	struct points* last_ptr=NULL;

	for ( p_ptr=ctxt->points; p_ptr != NULL; p_ptr = p_ptr->next)
	{
		if (last_ptr!=NULL)
			free(last_ptr);

		last_ptr=p_ptr;
	}

	free(last_ptr);

	free(context);
}


static void stars_render_fragment(til_module_context_t *context, til_stream_t *stream, unsigned ticks, unsigned cpu, til_fb_fragment_t **fragment_ptr)
{
	stars_context_t		*ctxt = (stars_context_t *)context;
	til_fb_fragment_t	*fragment = *fragment_ptr;

	struct points* iterator;
	struct points* tmp_ptr;
	struct points* last_ptr=NULL;
	float		x, y, pos_x, pos_y, rot_x, rot_y, opacity, x_mult, y_mult, max_radius;
	int		width = fragment->frame_width, height = fragment->frame_height;
	float		dt = ((float)(ticks - context->last_ticks)) * .025f;

	if(width>height) {
		x_mult=1.f;
		y_mult=(float)width/(float)height;
	} else {
		x_mult=(float)height/(float)width;
		y_mult=1.f;
	}

	max_radius=1.f+((width+height)*.001f);

	til_fb_fragment_clear(fragment);

	iterator=ctxt->points;
	for(;;)
	{
		if(iterator == NULL)
			break;

		if(iterator->z >= 1) {
			if(last_ptr == NULL)
				ctxt->points = iterator->next;
			else
				last_ptr->next = iterator->next;
			tmp_ptr = iterator;
			iterator = iterator->next;
			free(tmp_ptr);
			continue;
		}

		x = (iterator->x / (1.f - iterator->z))*x_mult;
		y = (iterator->y / (1.f - iterator->z))*y_mult;

		rot_x = (x*cosf(*ctxt->rot_angle))-(y*sinf(*ctxt->rot_angle));
		rot_y = (x*sinf(*ctxt->rot_angle))+(y*cosf(*ctxt->rot_angle));

		pos_x = ((rot_x+*ctxt->offset_x+1.f)*.5f)*(float)width;
		pos_y = ((rot_y+*ctxt->offset_y+1.f)*.5f)*(float)height;

		if(iterator->z<0.1)
			opacity = iterator->z*10;
		else
			opacity = 1;

		if (pos_x>0 && pos_x<width && pos_y>0 && pos_y<height)
			til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, pos_x, pos_y,
				makergb(0xFF, 0xFF, 0xFF, opacity));

		for(int my_y=floorf(pos_y-max_radius); my_y<=(int)ceilf(pos_y+max_radius); my_y++)
		for(int my_x=floorf(pos_x-max_radius); my_x<=(int)ceilf(pos_x+max_radius); my_x++) {

			//Is the point within our viewing window?
			if (!(my_x>0 && my_x<width && my_y>0 && my_y<height))
				continue;

			//Is the point within the circle?
			if(powf(my_x-pos_x, 2)+powf(my_y-pos_y, 2) > powf((iterator->z*max_radius), 2))
				continue;


			til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, my_x, my_y,
				makergb(0xFF, 0xFF, 0xFF, opacity));

		}

		iterator->z += 0.01 * dt;
		last_ptr=iterator;
		iterator=iterator->next;
	}

	if (dt > 0.f) {
		// add stars at horizon
		for(int i=0; i<rand_r(&ctxt->seed)%16; i++){
			tmp_ptr = malloc(sizeof(struct points));
			if (!tmp_ptr)
				break;
			tmp_ptr->x = get_random_unit_coord(&ctxt->seed);
			tmp_ptr->y = get_random_unit_coord(&ctxt->seed);
			tmp_ptr->z = 0.01;
			tmp_ptr->next = ctxt->points;
			ctxt->points = tmp_ptr;
		}

		stars_update_taps(ctxt, stream, dt);
	}
}

int stars_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup);

til_module_t	stars_module = {
	.create_context  = stars_create_context,
	.destroy_context = stars_destroy_context,
	.render_fragment = stars_render_fragment,
	.setup = stars_setup,
	.name = "stars",
	.description = "Basic starfield",
	.author = "Philip J Freeman <elektron@halo.nu>",
	.flags = TIL_MODULE_OVERLAYABLE,
};

int stars_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup)
{
	const char	*rot_adj;
	const char	*rot_adj_values[] = {
				".0",
				".00001",
				".00003",
				".0001",
				".0003",
				".001",
				NULL
			};
	int		r;

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_spec_t){
							.name = "Rotation rate",
							.key = "rot_adj",
							.regex = "\\.[0-9]+",
							.preferred = TIL_SETTINGS_STR(DEFAULT_ROT_ADJ),
							.values = rot_adj_values,
							.annotations = NULL
						},
						&rot_adj,
						res_setting,
						res_desc);
	if (r)
		return r;

	if (res_setup) {
		stars_setup_t	*setup;

		setup = til_setup_new(settings, sizeof(*setup), NULL, &stars_module);
		if (!setup)
			return -ENOMEM;

		sscanf(rot_adj, "%f", &setup->rot_adj);

		*res_setup = &setup->til_setup;
	}

	return 0;
}
© All Rights Reserved