2.0
[tar-legacy.git] / MCDV / GradientMap.hpp
1 #pragma once
2 #include <stdint.h>
3 #include <string>
4
5 #include "util.h"
6 #include "interpolation.h"
7
8 struct Color255 {
9 uint8_t r = 0;
10 uint8_t g = 0;
11 uint8_t b = 0;
12 uint8_t a = 255;
13
14 Color255() {
15
16 }
17
18 Color255(std::string src) {
19 std::vector<std::string> strings;
20 strings = split(src);
21
22 if (strings.size() >= 1) r = ::atoi(strings[0].c_str());
23 if (strings.size() >= 2) g = ::atoi(strings[1].c_str());
24 if (strings.size() >= 3) b = ::atoi(strings[2].c_str());
25 if (strings.size() >= 4) a = ::atoi(strings[3].c_str());
26 }
27 };
28
29 class GradientTexture : public Texture{
30 public:
31 Color255 c0;
32 Color255 c1;
33 Color255 c2;
34
35 //unsigned int texture_id;
36
37 GradientTexture(Color255 _c0, Color255 _c1, Color255 _c2) {
38 this->c0 = _c0; this->c1 = _c1; this->c2 = _c2;
39
40 glGenTextures(1, &this->texture_id);
41
42 unsigned char* data = (unsigned char*)malloc(256*4);
43
44 // Do texture generation
45 for (int i = 0; i < 256; i++) {
46 Color255* a = &this->c0;
47 Color255* b = &this->c1;
48
49 if (i >= 128) { a = b; b = &this->c2; }
50
51 data[i * 4 + 0] = lerpT(a->r, b->r, (float)(i % 128) / 128.0f);
52 data[i * 4 + 1] = lerpT(a->g, b->g, (float)(i % 128) / 128.0f);
53 data[i * 4 + 2] = lerpT(a->b, b->b, (float)(i % 128) / 128.0f);
54 data[i * 4 + 3] = lerpT(a->a, b->a, (float)(i % 128) / 128.0f);
55 }
56
57 glBindTexture(GL_TEXTURE_2D, this->texture_id);
58 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
59
60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
62
63 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
65
66 free(data);
67 }
68
69 /*void bindOnSlot(int slot = 0) {
70 glActiveTexture(GL_TEXTURE0 + slot);
71 glBindTexture(GL_TEXTURE_2D, this->texture_id);
72 }*/
73 };