-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdeform.c
137 lines (101 loc) · 4.03 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
/*
MIT No Attribution
Copyright (c) 2021-2023 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 "head.h"
#include "deform.h"
static const uint8_t SPEED = 2;
static const uint8_t PIXEL_SIZE = 1;
static uint32_t frame;
int8_t *lut;
void
deform_init(hagl_backend_t const *display)
{
/* 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)(HEAD_WIDTH * u)) % HEAD_WIDTH;
const int8_t ty = ((int8_t)(HEAD_HEIGHT * v)) % HEAD_HEIGHT;
*(ptr++) = tx;
*(ptr++) = ty;
}
}
}
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) % HEAD_WIDTH;
v = abs(v) % HEAD_HEIGHT;
/* Get the pixel from texture and put it to the screen. */
const hagl_color_t *color = (hagl_color_t *) (head + HEAD_WIDTH * sizeof(hagl_color_t) * v + sizeof(hagl_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);
}