noisetoy.cc (3660B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #include "app.h" 6 #include "util.h" 7 #include "matrix.h" 8 #include "program.h" 9 #include "debugtext.h" 10 11 #include <math.h> 12 13 #include <SDL_keysym.h> 14 15 class NoiseApp : public App { 16 public: 17 NoiseApp(); 18 int init(void); 19 int render(void); 20 void key(int code); 21 private: 22 unsigned char *data; 23 int w, h; 24 25 int mode; 26 27 Program pgm; 28 29 GLuint tex0; 30 GLuint aVertex, aTexCoord; 31 GLuint uTexture; 32 33 float n; 34 35 DebugText dtxt; 36 37 float scale; 38 float offx,offy; 39 }; 40 41 NoiseApp::NoiseApp() : App(), w(512), h(512) { 42 data = (unsigned char*) malloc(w * h); 43 scale = 1.0; 44 offx = offy = 0; 45 n = 0; 46 mode = 0; 47 } 48 49 static const char *vsrc = R"( 50 attribute vec2 aVertex; 51 attribute vec2 aTexCoord; 52 varying vec2 vTexCoord; 53 void main() { 54 gl_Position = vec4(aVertex, 0.0, 1.0); 55 vTexCoord = aTexCoord; 56 } 57 )"; 58 59 static const char *fsrc = R"( 60 uniform sampler2D uTexture; 61 varying vec2 vTexCoord; 62 void main() { 63 float c = texture2D(uTexture, vTexCoord).a; 64 gl_FragColor = vec4(c, c, c, 1.0); 65 } 66 )"; 67 68 int NoiseApp::init(void) { 69 if (pgm.compileStr(vsrc, fsrc)) 70 return -1; 71 72 aVertex = pgm.getAttribID("aVertex"); 73 aTexCoord = pgm.getAttribID("aTexCoord"); 74 uTexture = pgm.getUniformID("uTexture"); 75 glViewport(0, 0, width(), height()); 76 glClearColor(0, 0, 0, 0); 77 glClearDepth(1.0f); 78 79 glGenTextures(1, &tex0); 80 81 dtxt.init(32, 32); 82 return 0; 83 } 84 85 static GLfloat vtx_xy_uv[] = { 86 -1.0, -1.0, 0.0, 0.0, 87 -1.0, 1.0, 0.0, 1.0, 88 1.0, -1.0, 1.0, 0.0, 89 1.0, 1.0, 1.0, 1.0, 90 }; 91 92 void NoiseApp::key(int code) { 93 switch (code) { 94 case SDLK_x: 95 fprintf(stderr,"saving...\n"); 96 save_png_gray("out.png", data, w, h); 97 break; 98 case SDLK_m: 99 mode++; 100 if (mode == 4) mode = 0; 101 fprintf(stderr, "mode: %d\n", mode); 102 break; 103 } 104 } 105 106 int NoiseApp::render(void) { 107 int x, y; 108 // n += 0.01; 109 110 if (keydown(SDLK_w)) offy += 0.1; 111 if (keydown(SDLK_s)) offy -= 0.1; 112 if (keydown(SDLK_a)) offx -= 0.1; 113 if (keydown(SDLK_d)) offx += 0.1; 114 115 if (keydown(SDLK_r)) scale += 0.01; 116 if (keydown(SDLK_f)) scale -= 0.01; 117 118 for (y = 0; y < h; y++) { 119 for (x = 0; x < w; x++) { 120 float fy = float(y) / float(h) + offy; 121 float fx = float(x) / float(w) + offx; 122 123 fx *= scale; 124 fy *= scale; 125 126 float sn = snoise(fx, fy) * 1.0; 127 128 switch (mode) { 129 case 2: 130 sn += snoise(fx*8.0,fy*8.0) * 0.0625; 131 case 1: 132 sn += snoise(fx*2.0,fy*2.0) * 0.25; 133 sn += snoise(fx*4.0,fy*4.0) * 0.125; 134 break; 135 case 3: 136 sn += snoise(fx*13.3,fy*9.2) * 0.65; 137 break; 138 } 139 140 #if 1 141 if (sn < -1.0) sn = -1.0; 142 if (sn > 1.0) sn = 1.0; 143 #endif 144 145 data[y*w+x] = 127.0 * (sn + 1.0); 146 #if 0 147 if ((sn + (1.0-fy) * 1.0 ) > 0.75) 148 data[y*w+x] = 0xff; 149 else 150 data[y*w+x] = 0x00; 151 #endif 152 } 153 } 154 155 glClear(GL_COLOR_BUFFER_BIT); 156 157 pgm.use(); 158 159 glActiveTexture(GL_TEXTURE0); 160 glBindTexture(GL_TEXTURE_2D, tex0); 161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 162 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 163 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, data); 164 165 glUniform1i(uTexture, 0); 166 167 glBindBuffer(GL_ARRAY_BUFFER, 0); 168 glVertexAttribPointer(aVertex, 2, GL_FLOAT, GL_FALSE, 4*4, vtx_xy_uv); 169 glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 4*4, vtx_xy_uv + 2); 170 171 glEnableVertexAttribArray(aVertex); 172 glEnableVertexAttribArray(aTexCoord); 173 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 174 glDisableVertexAttribArray(aVertex); 175 glDisableVertexAttribArray(aTexCoord); 176 177 dtxt.clear(); 178 dtxt.printf("%d> %f %f %f\n", mode, offx, offy, scale); 179 dtxt.render(); 180 return 0; 181 } 182 183 int main(int argc, char **argv) { 184 NoiseApp app; 185 app.setSize(512, 512); 186 app.setOptions(argc, argv); 187 return app.run(); 188 }