spacepaste

  1.  
  2. #include <SDL.h>
  3. #include <SDL_opengl.h>
  4. #include <gles2.h>
  5. #include <gles2ext.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #define ONE_AXIS_X 0x01
  11. #define ONE_AXIS_Y 0x02
  12. #define ONE_AXIS_WIDTH ONE_AXIS_X
  13. #define ONE_AXIS_HIGHT ONE_AXIS_Y
  14. /*
  15. width,hight result
  16. base scale: 200*200
  17. 0,0 = top left or -1,1
  18. 200,0 ConvertPair: 1.000000, 1.000000 (correct)
  19. 0,200 ConvertPair: -1.000000, -1.000000 (correct)
  20. 200,200 ConvertPair: 1.000000, -1.000000 (correct)
  21. 0,0
  22. hight
  23. ( -1, 1) | ( 0, 1) ( 1, 1)
  24. |
  25. |
  26. |
  27. |
  28. |
  29. |
  30. |
  31. |
  32. ( -1, 0) | ( 0, 0) ( 1, 0)
  33. ---------------------+--------------------- width
  34. |
  35. |
  36. |
  37. |
  38. |
  39. |
  40. |
  41. |
  42. |
  43. ( -1, -1) | ( 0, -1) ( 1, -1)
  44. */
  45. void addr(float * name, const char * _name) {
  46. printf("address of %s is %014p, size is %zu\n", _name, name, sizeof(name));
  47. printf("address of &%s is %014p, size is %zu\n", _name, &name, sizeof(&name));
  48. //printf("address of *%s is %014p, size is %zu\n", _name, *name, sizeof(*name));
  49. }
  50. float inverse(float num) {
  51. return num<0?-num:-(num);
  52. }
  53. short inverseshort(short num) {
  54. return num<0?-num:-(num);
  55. }
  56. float convert(short num, int hw) {
  57. //converts axis to 2d float coordinate
  58. return (float) (num-(hw/2))/(hw/2);
  59. }
  60. int inversesint(int num) {
  61. return num<0?-num:-(num);
  62. }
  63. float convertint(int num, int hw) {
  64. //converts axis to 2d float coordinate
  65. return (float) (num-(hw/2))/(hw/2);
  66. }
  67. struct coordinates
  68. {
  69. float x;
  70. float y;
  71. };
  72. struct coordinates convertPair(short w, short h, int hw, int FLAGS) {
  73. struct coordinates xy;
  74. if ((w > hw || w < 0) && (FLAGS != ONE_AXIS_X && FLAGS != ONE_AXIS_Y)) printf("x is out of bounds (expected %hi, got %hi)\n", hw, w);
  75. else if ((h > hw || h < 0) && (FLAGS != ONE_AXIS_X && FLAGS != ONE_AXIS_Y)) printf("y is out of bounds (expected %hi, got %hi)\n", hw, h);
  76. else {
  77. //converts axis pair to 2d float coordinate
  78. xy.x = FLAGS==ONE_AXIS_X?0:convert(w, hw); // x
  79. xy.y = FLAGS==ONE_AXIS_Y&&h<0?inverse(inverse(convert(inverseshort(h), hw))):inverse(convert(h, hw)); // y
  80. printf("%hi, %hi, ConvertPair: %f, %f\n", w, h, xy.x, xy.y);
  81. }
  82. return xy;
  83. }
  84. int reverseBool(int val) {
  85. if (val == 1 || val == 0) return val^1;
  86. else return val;
  87. }
  88. // short line_xy[] = { 800, 800, 400, 400 };
  89. // should generate: data[] = { 1.000000, -1.000000, 0.000000, 0.000000 };
  90. float * convert_array_type_short_to_vertex(short * data, int size, int scale, int * s, int FLAGS) {
  91. if (FLAGS == ONE_AXIS_X || FLAGS == ONE_AXIS_Y) {
  92. *s=size*2;
  93. scale = scale*2;
  94. }
  95. else *s=size;
  96. // data only has hight data, width is i
  97. // format is h,w
  98. printf("beginning conversion with a scale of %i and size of %i, with a transformation size of %i\n", scale, size, *s);
  99. float * data_new = malloc(*s*sizeof(float));
  100. //memset(data_new, 0, size*(sizeof(float)*2));
  101. int i, z;
  102. float * y = malloc(*s/2);
  103. float * x = malloc(*s/2);
  104. struct coordinates pos;
  105. for (i = 0; i < size; i++) {
  106. pos = convertPair(FLAGS==ONE_AXIS_X?data[i]:i, FLAGS==ONE_AXIS_Y?(scale/2)+data[i+1]:i, scale, FLAGS);
  107. data_new[i] = pos.x;
  108. data_new[i+1] = pos.y;
  109. if (FLAGS != ONE_AXIS_X && FLAGS != ONE_AXIS_Y) i++;
  110. //usleep(150);
  111. }
  112. printf("finished conversion with a scale of %i and size of %i, with a transformation size of %i\n", scale, size, *s);
  113. printf("data[] = { ");
  114. for(int k = 0;k<size;k++) printf("%i, ",data[k]);
  115. printf("};\n");
  116. printf("data_new[] = { ");
  117. for(int k = 0;k<*s;k++) printf("%2g, ",data_new[k]);
  118. printf("};\n");
  119. return data_new;
  120. };
  121. /*
  122. int lines;
  123. short i = 1;
  124. short ii = 2;
  125. short iii = 3;
  126. short iiii = 4;
  127. int HxW = 500;
  128. short d1[] = { 500,500, 250,250, 250,0, 0,250 }; // h and w
  129. short d2[] = { 500,250, 0,250 }; // h only
  130. short d3[] = { 500,250, 250,0 }; // w only
  131. int main(int argc, char *argv[])
  132. {
  133. printf("\nHight And Width Data\n");
  134. convert_array_type_short_to_vertex(d1, 8, HxW, &lines, 0);
  135. printf("expected result:\n");
  136. convertPair(d1[0], d1[1], HxW, 0);
  137. convertPair(d1[2], d1[3], HxW, 0);
  138. convertPair(d1[4], d1[5], HxW, 0);
  139. convertPair(d1[6], d1[7], HxW, 0);
  140. printf("\nHight Data Only\n");
  141. convert_array_type_short_to_vertex(d2, 4, HxW, &lines, ONE_AXIS_HIGHT);
  142. printf("expected result:\n");
  143. convertPair(i, d2[0], HxW, ONE_AXIS_HIGHT);
  144. convertPair(ii, d2[1], HxW, ONE_AXIS_HIGHT);
  145. convertPair(iii, d2[2], HxW, ONE_AXIS_HIGHT);
  146. convertPair(iiii, d2[3], HxW, ONE_AXIS_HIGHT);
  147. printf("\nWidth Data Only\n");
  148. convert_array_type_short_to_vertex(d3, 4, HxW, &lines, ONE_AXIS_WIDTH);
  149. printf("expected result:\n");
  150. convertPair(d3[0], i, HxW, ONE_AXIS_WIDTH);
  151. convertPair(d3[1], ii, HxW, ONE_AXIS_WIDTH);
  152. convertPair(d3[2], iii, HxW, ONE_AXIS_WIDTH);
  153. convertPair(d3[3], iiii, HxW, ONE_AXIS_WIDTH);
  154. }
  155. */
  156. /*
  157. #include "clibs/mathlib/mathlib.h"|
  158. #include "clibs/glmc/glmc.h"
  159. #include "clibs/math_3d.h"
  160. */
  161. int h = 800;
  162. int w = 600;
  163. // https://github.com/mattdesl/lwjgl-basics/wiki/GLSL-Versions
  164. /*
  165. GLSL Versions
  166. OpenGL Version GLSL Version
  167. 2.0 110
  168. 2.1 120
  169. 3.0 130
  170. 3.1 140
  171. 3.2 150
  172. 3.3 330
  173. 4.0 400
  174. 4.1 410
  175. 4.2 420
  176. 4.3 430
  177. GLSL ES Versions (Android, iOS, WebGL)
  178. OpenGL ES has its own Shading Language, and the versioning starts fresh. It is based on OpenGL Shading Language version 1.10.
  179. OpenGL ES Version GLSL ES Version
  180. 2.0 100
  181. 3.0 300
  182. ES has no core
  183. #version needs to be the first char in the array:
  184. array[0] = #
  185. array[1] = v
  186. array[2] = v
  187. array[3] = e
  188. array[4] = r
  189. array[5] = s
  190. array[6] = i
  191. array[7] = o
  192. array[8] = n
  193. Version 100
  194. Vertex shader:
  195. uniform mat4 projTrans;
  196. attribute vec2 Position;
  197. attribute vec2 TexCoord;
  198. varying vec2 vTexCoord;
  199. void main() {
  200. vTexCoord = TexCoord;
  201. gl_Position = u_projView * vec4(Position, 0.0, 1.0);
  202. }
  203. Fragment shader:
  204. uniform sampler2D tex0;
  205. varying vec2 vTexCoord;
  206. void main() {
  207. vec4 color = texture2D(tex0, vTexCoord);
  208. gl_FragColor = color;
  209. }
  210. Version 330
  211. As of GLSL 130+, in and out are used instead of attribute and varying. GLSL 330+ includes other features like layout qualifiers and changes texture2D to texture.
  212. Vertex shader:
  213. #version 330
  214. uniform mat4 projTrans;
  215. layout(location = 0) in vec2 Position;
  216. layout(location = 1) in vec2 TexCoord;
  217. out vec2 vTexCoord;
  218. void main() {
  219. vTexCoord = TexCoord;
  220. gl_Position = u_projView * vec4(Position, 0, 1);
  221. }
  222. Fragment shader:
  223. #version 330
  224. uniform sampler2D tex0;
  225. in vec2 vTexCoord;
  226. //use your own output instead of gl_FragColor
  227. out vec4 fragColor;
  228. void main() {
  229. //'texture' instead of 'texture2D'
  230. fragColor = texture(tex0, vTexCoord);
  231. }
  232. */
  233. // http://www.shaderific.com/glsl-qualifiers/
  234. const char * Shader_Vertex =
  235. "#version 100 \n\
  236. \n\
  237. attribute vec2 position; \n\
  238. \n\
  239. void main() \n\
  240. { \n\
  241. gl_Position = vec4(position, 0.0, 1.0); \n\
  242. }";
  243. const char * Shader_Fragment =
  244. "#version 100 \n\
  245. \n\
  246. void main() \n\
  247. { \n\
  248. gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); \n\
  249. }";
  250. int compile_check(GLuint shader, const char * name) {
  251. GLint status;
  252. glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
  253. status = reverseBool(status);
  254. char buffer[512];
  255. glGetShaderInfoLog(shader, 512, NULL, buffer);
  256. printf("OpenGL Information:\n Shader: %s\n OpenGL Vendor: %s\n OpenGL Renderer: %s\n OpenGL Version: %s\n OpenGL Shading Language Version: %s\n OpenGL Extentions:\n--EXTENTIONS START--\n%s\n--EXTENTIONS END--\n Compiled With Status: %i\n Log:\n--LOG START--\n%s--LOG END--\n\n", name, glGetString(GL_VENDOR), glGetString(GL_RENDERER), glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION), glGetString(GL_EXTENSIONS), status, buffer);
  257. return status;
  258. }
  259. GLuint vao;
  260. GLuint vbo;
  261. int initgl(float * name, int size) {
  262. glGenBuffers(1, &vbo);
  263. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  264. glBufferData(GL_ARRAY_BUFFER, size, name, GL_STATIC_DRAW);
  265. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  266. glShaderSource(vertexShader, 1, &Shader_Vertex, NULL);
  267. glCompileShader(vertexShader);
  268. if (compile_check(vertexShader, "vertex") =! 0) return 1;
  269. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  270. glShaderSource(fragmentShader, 1, &Shader_Fragment, NULL);
  271. glCompileShader(fragmentShader);
  272. if (compile_check(fragmentShader, "fragment") =! 0) return 1;
  273. GLuint shaderProgram = glCreateProgram();
  274. glAttachShader(shaderProgram, vertexShader);
  275. glAttachShader(shaderProgram, fragmentShader);
  276. //glBindFragDataLocation(shaderProgram, 0, "outColor");
  277. glLinkProgram(shaderProgram);
  278. glUseProgram(shaderProgram);
  279. GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
  280. glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  281. glEnableVertexAttribArray(posAttrib);
  282. glGenBuffers(1, &vao);
  283. glBindVertexArrayOES(vao);
  284. /*
  285. mat4_t m4_ortho (float left, float right, float bottom, float top, float back, float front);
  286. mat4_t m4_perspective (float vertical_field_of_view_in_deg, float aspect_ratio, float near_view_distance, float far_view_distance);
  287. mat4_t m4_look_at (vec3_t from, vec3_t to, vec3_t up);
  288. void mathMatrixPerspectivef( float *m, float fovy, float aspect, float zNear, float zFar )
  289. void mathMatrixOrthof( float *m, float left, float right, float bottom, float top, float nearval, float farval);
  290. void mathMatrixRotf( float *mat, float angle, float x, float y, float z );
  291. void mathMatrixRotxf( float *m, float angle );
  292. void mathMatrixRotyf( float *m, float angle );
  293. void mathMatrixRotzf( float *m, float angle );
  294. void mathMatrixTranslatef( float *m, float x, float y, float z );
  295. */
  296. //
  297. /**
  298. m4_ortho(800.0f, 0.0f, 600.0f, 0.0f, 0.1f, 100.0f);
  299. mat4_t model[16];
  300. mat4_t view[16];
  301. mat4_t projection[16];
  302. model = mathMatrixRotxf(model, 0.5f);
  303. view = m4_look_at(0.0f, 0.0f, -3.0f);
  304. projection = m4_perspective(0.45f, 800.0f/600.0f, 0.1f, 100.0f);
  305. */
  306. return 0;
  307. }
  308. int gl_main(float * line, int line_size) {
  309. SDL_Window * window;
  310. SDL_GLContext context;
  311. SDL_Init(SDL_INIT_VIDEO);
  312. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  313. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  314. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  315. SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  316. window=SDL_CreateWindow("OpenGL", w, h, w, h, SDL_WINDOW_OPENGL|SDL_WINDOW_ALLOW_HIGHDPI);
  317. context = SDL_GL_CreateContext(window);
  318. if(initgl(line, line_size) != 0) return 1;
  319. SDL_Event windowEvent;
  320. //float *ptr = glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY_OES);
  321. int lines = line_size/sizeof(line[0])/2;
  322. printf("lines to draw = %i\n", lines);
  323. while(1)
  324. {
  325. if (SDL_PollEvent(&windowEvent))
  326. {
  327. if (windowEvent.type == SDL_QUIT) break;
  328. }
  329. glDrawArrays(GL_LINE_STRIP, 0, lines);
  330. SDL_GL_SwapWindow(window);
  331. usleep(500000);
  332. }
  333. sleep(100);
  334. glUnmapBufferOES(GL_ARRAY_BUFFER);
  335. SDL_GL_DeleteContext(context);
  336. SDL_Quit();
  337. return 0;
  338. }
  339. int main(int argc, char *argv[])
  340. {
  341. int lines;
  342. short line_xy[] = { 5, 50, 400, 400 };
  343. gl_main(convert_array_type_short_to_vertex(line_xy, sizeof(line_xy), 400, &lines, 0), lines);
  344. }
  345. /*
  346. //Original source code found at : http://programmersranch.blogspot.kr/2014/02/sdl2-pixel-drawing.html
  347. #ifdef _OS_ANDROID_
  348. #error "Android version does not support SDL2 yet."
  349. #endif
  350. #include <SDL.h>
  351. #include <stdbool.h>
  352. #include <stdio.h>
  353. #include <limits.h>
  354. int h;
  355. int w;
  356. int colour;
  357. SDL_Renderer * renderer;
  358. SDL_Texture * texture;
  359. SDL_Window * window;
  360. Uint32 *pixels;
  361. SDL_Rect SrcR;
  362. SDL_Rect DestR;
  363. int zoom;
  364. int pitch;
  365. int base = 2;
  366. void init() {
  367. SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 0);
  368. audioSampleSize = SampleSize * length;
  369. // h and w may be the problem, not sure
  370. h = 1000;
  371. w = 1000;
  372. pitch = w*sizeof(Uint32);
  373. colour = 111;
  374. // pixel[7] and pixel[9] should be at the top and bottom of the window with no gaps in between, zoom level is valid from 51 to 52
  375. zoom = 275; // 3*3 pixel grid is zoom 275
  376. DestR.x = 0;
  377. DestR.y = 0;
  378. DestR.w = w;
  379. DestR.h = h;
  380. SrcR.x = 0;
  381. SrcR.y = base-1;//((h/2) - (h * 0.5) / zoom)+1;
  382. SrcR.w = w/zoom;
  383. SrcR.h = h/zoom;
  384. SDL_Init(SDL_INIT_VIDEO);
  385. window=SDL_CreateWindow("a", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, 0);
  386. renderer=SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  387. texture=SDL_CreateTexture(renderer,SDL_PIXELFORMAT_ARGB8888,SDL_TEXTUREACCESS_STREAMING,w,h);
  388. pixels=malloc(sizeof(Uint32) * w * h);
  389. memset(pixels,255,w*h*4);
  390. // create preview
  391. SDL_RenderClear(renderer);
  392. SDL_UpdateTexture(texture,0,pixels,pitch);
  393. SDL_RenderCopy(renderer,texture, &SrcR, &DestR);
  394. SDL_RenderPresent(renderer);
  395. printf("DestR.x = %i\nDestR.y = %i\nDestR.w = %i\nDestR.h = %i\nSrcR.x = %i\nSrcR.y = %i\nSrcR.w = %i\nSrcR.h = %i\nzoom = %i\n", DestR.x, DestR.y, DestR.w, DestR.h, SrcR.x, SrcR.y, SrcR.w, SrcR.h, zoom);
  396. }
  397. void putpixel(int x, int y, int colour) {
  398. if (x > w || x < 0) printf("x is out of bounds\n");
  399. else if (y > h || y < 0) printf("y is out of bounds\n");
  400. else {
  401. int location = (y * w) + x;
  402. pixels[location]=colour;
  403. printf("pixels[%i] = %i\n", location, colour) ;
  404. }
  405. }
  406. int len;
  407. int draw(short * data)
  408. {
  409. // draw graph
  410. int i = 0;
  411. SDL_RenderClear(renderer);
  412. while (i<len) {
  413. //int loc = (h/2) + ( (h/2) * ( (float) data[i]/SHRT_MAX ) );
  414. //int loc = (h* (h/2) ) + ( data[i]*h );
  415. int loc = (h/2) + data[i];
  416. loc = loc - 500 + base;
  417. printf("data[%i] = %i, loc = %i, ", i, data[i], loc);
  418. putpixel(0, loc, 111+(i*150000));
  419. i++;
  420. }
  421. SDL_UpdateTexture(texture,0,pixels,pitch);
  422. SDL_RenderCopy(renderer,texture, &SrcR, &DestR);
  423. SDL_RenderPresent(renderer);
  424. n();
  425. SDL_Delay(1000);
  426. return 0;
  427. SDL_Quit();
  428. }
  429. void draw_free(int * pixels, SDL_Renderer * renderer, SDL_Texture * texture, SDL_Window * window) {
  430. free(pixels);
  431. SDL_DestroyTexture(texture);
  432. SDL_DestroyRenderer(renderer);
  433. SDL_DestroyWindow(window);
  434. SDL_Quit();
  435. }
  436. */
  437.