forked from tuupola/pico_effects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeform.c
142 lines (104 loc) · 4.26 KB
/
deform.c
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
/*
MIT No Attribution
Copyright (c) 2021 Mika Tuupola
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-cut-
Adapted from article by Inigo Quilez:
https://iquilezles.org/www/articles/deform/deform.htm
SPDX-License-Identifier: MIT-0
*/
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#include <hagl.h>
#include "convert.h"
#include "deform.h"
static const uint8_t SPEED = 2;
static const uint8_t PIXEL_SIZE = 1;
static uint32_t frame;
int8_t *lut;
uint8_t deform_texture_width, deform_texture_height;
uint8_t *deform_texture_data;
void deform_init(uint8_t w, uint8_t h, const uint8_t *data888)
{
frame = 0;
/* Allocate memory for lut and store address also to ptr. */
int8_t *ptr = lut = malloc(DISPLAY_HEIGHT * DISPLAY_WIDTH * 2 * sizeof(int8_t));
for (uint16_t j = 0; j < DISPLAY_HEIGHT; j += PIXEL_SIZE) {
for (uint16_t i = 0; i < DISPLAY_WIDTH; i += PIXEL_SIZE) {
const float x = -1.00f + 2.00f * i / DISPLAY_WIDTH;
const float y = -1.00f + 2.00f * j / DISPLAY_HEIGHT;
const float r = sqrtf(x * x + y * y);
const float a = atan2f(y, x);
const float u = cosf(a) / r;
const float v = sinf(a) / r;
// const float u = 0.5 * a / M_PI;
// const float v = sin(7 * r);
// const float u = 0.02 * y + 0.03 * cos(a * 3) / r;
// const float v = 0.02 * x + 0.03 * sin(a * 3) / r;
// const float u = 1 / (r + 0.5 + 0.5 * sin(5 * a));
// const float v = a * 3 / M_PI;
// const float u = x * cos(2 * r) - y * sin(2 * r);
// const float v = y * cos(2 * r) + x * sin(2 * r);
// const float u = 0.3 / (r + 0.5 * x);
// const float v = 3 * a / M_PI;
// const float u = 0.1 * x / (0.11 + r * 0.5);
// const float v = 0.1 * y / (0.11 + r * 0.5);
// const float u = r * cos(a + r);
// const float v = r * sin(a + r);
// const float u = x / fabs(y);
// const float v = 1 / fabs(y);
// const float u = x;
// const float v = y;
/* Target x and y coordinates in the texture. */
const int8_t tx = ((int8_t)(w * u)) % w;
const int8_t ty = ((int8_t)(h * v)) % h;
*(ptr++) = tx;
*(ptr++) = ty;
}
}
deform_texture_width = w;
deform_texture_height = h;
deform_texture_data = convert_888(w, h, data888);
}
void deform_render(hagl_backend_t const *display)
{
int8_t *ptr = lut;
for (uint16_t y = 0; y < DISPLAY_HEIGHT; y += PIXEL_SIZE) {
for (uint16_t x = 0; x < DISPLAY_WIDTH; x += PIXEL_SIZE) {
/* Retrieve texture x and y coordinates for display coordinates. */
int16_t u = *(ptr++) + frame;
int16_t v = *(ptr++) + frame;
u = abs(u) % deform_texture_width;
v = abs(v) % deform_texture_height;
/* Get the pixel from texture and put it to the screen. */
const color_t *color = (color_t*) (deform_texture_data + deform_texture_width * sizeof(color_t) * v + sizeof(color_t) * u);
if (1 == PIXEL_SIZE) {
hagl_put_pixel(display, x, y, *color);
} else {
hagl_fill_rectangle(display, x, y, x + PIXEL_SIZE - 1, y + PIXEL_SIZE - 1, *color);
}
}
}
}
void deform_animate()
{
frame = frame + SPEED;
}
void deform_close()
{
free(lut);
free(deform_texture_data);
}