-
- #include <SDL.h>
- #include <SDL_opengl.h>
- #include <gles2.h>
- #include <gles2ext.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
-
-
- #define ONE_AXIS_X 0x01
- #define ONE_AXIS_Y 0x02
- #define ONE_AXIS_WIDTH ONE_AXIS_X
- #define ONE_AXIS_HIGHT ONE_AXIS_Y
-
- /*
- width,hight result
-
- base scale: 200*200
-
- 0,0 = top left or -1,1
-
- 200,0 ConvertPair: 1.000000, 1.000000 (correct)
- 0,200 ConvertPair: -1.000000, -1.000000 (correct)
- 200,200 ConvertPair: 1.000000, -1.000000 (correct)
- 0,0
-
- hight
- ( -1, 1) | ( 0, 1) ( 1, 1)
- |
- |
- |
- |
- |
- |
- |
- |
- ( -1, 0) | ( 0, 0) ( 1, 0)
- ---------------------+--------------------- width
- |
- |
- |
- |
- |
- |
- |
- |
- |
- ( -1, -1) | ( 0, -1) ( 1, -1)
-
- */
-
-
- void addr(float * name, const char * _name) {
- printf("address of %s is %014p, size is %zu\n", _name, name, sizeof(name));
- printf("address of &%s is %014p, size is %zu\n", _name, &name, sizeof(&name));
- //printf("address of *%s is %014p, size is %zu\n", _name, *name, sizeof(*name));
- }
-
- float inverse(float num) {
- return num<0?-num:-(num);
- }
-
- short inverseshort(short num) {
- return num<0?-num:-(num);
- }
-
- float convert(short num, int hw) {
- //converts axis to 2d float coordinate
- return (float) (num-(hw/2))/(hw/2);
- }
-
- int inversesint(int num) {
- return num<0?-num:-(num);
- }
-
- float convertint(int num, int hw) {
- //converts axis to 2d float coordinate
- return (float) (num-(hw/2))/(hw/2);
- }
-
- struct coordinates
- {
- float x;
- float y;
- };
-
- struct coordinates convertPair(short w, short h, int hw, int FLAGS) {
- struct coordinates xy;
- 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);
- 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);
- else {
- //converts axis pair to 2d float coordinate
- xy.x = FLAGS==ONE_AXIS_X?0:convert(w, hw); // x
- xy.y = FLAGS==ONE_AXIS_Y&&h<0?inverse(inverse(convert(inverseshort(h), hw))):inverse(convert(h, hw)); // y
- printf("%hi, %hi, ConvertPair: %f, %f\n", w, h, xy.x, xy.y);
- }
- return xy;
- }
-
- int reverseBool(int val) {
- if (val == 1 || val == 0) return val^1;
- else return val;
- }
-
- // short line_xy[] = { 800, 800, 400, 400 };
- // should generate: data[] = { 1.000000, -1.000000, 0.000000, 0.000000 };
-
-
- float * convert_array_type_short_to_vertex(short * data, int size, int scale, int * s, int FLAGS) {
- if (FLAGS == ONE_AXIS_X || FLAGS == ONE_AXIS_Y) {
- *s=size*2;
- scale = scale*2;
- }
- else *s=size;
- // data only has hight data, width is i
- // format is h,w
- printf("beginning conversion with a scale of %i and size of %i, with a transformation size of %i\n", scale, size, *s);
- float * data_new = malloc(*s*sizeof(float));
- //memset(data_new, 0, size*(sizeof(float)*2));
-
- int i, z;
- float * y = malloc(*s/2);
- float * x = malloc(*s/2);
- struct coordinates pos;
- for (i = 0; i < size; i++) {
- pos = convertPair(FLAGS==ONE_AXIS_X?data[i]:i, FLAGS==ONE_AXIS_Y?(scale/2)+data[i+1]:i, scale, FLAGS);
- data_new[i] = pos.x;
- data_new[i+1] = pos.y;
- if (FLAGS != ONE_AXIS_X && FLAGS != ONE_AXIS_Y) i++;
- //usleep(150);
- }
- printf("finished conversion with a scale of %i and size of %i, with a transformation size of %i\n", scale, size, *s);
- printf("data[] = { ");
- for(int k = 0;k<size;k++) printf("%i, ",data[k]);
- printf("};\n");
-
- printf("data_new[] = { ");
- for(int k = 0;k<*s;k++) printf("%2g, ",data_new[k]);
- printf("};\n");
-
- return data_new;
- };
-
- /*
- int lines;
- short i = 1;
- short ii = 2;
- short iii = 3;
- short iiii = 4;
- int HxW = 500;
- short d1[] = { 500,500, 250,250, 250,0, 0,250 }; // h and w
- short d2[] = { 500,250, 0,250 }; // h only
- short d3[] = { 500,250, 250,0 }; // w only
- int main(int argc, char *argv[])
- {
- printf("\nHight And Width Data\n");
- convert_array_type_short_to_vertex(d1, 8, HxW, &lines, 0);
- printf("expected result:\n");
- convertPair(d1[0], d1[1], HxW, 0);
- convertPair(d1[2], d1[3], HxW, 0);
- convertPair(d1[4], d1[5], HxW, 0);
- convertPair(d1[6], d1[7], HxW, 0);
- printf("\nHight Data Only\n");
- convert_array_type_short_to_vertex(d2, 4, HxW, &lines, ONE_AXIS_HIGHT);
- printf("expected result:\n");
- convertPair(i, d2[0], HxW, ONE_AXIS_HIGHT);
- convertPair(ii, d2[1], HxW, ONE_AXIS_HIGHT);
- convertPair(iii, d2[2], HxW, ONE_AXIS_HIGHT);
- convertPair(iiii, d2[3], HxW, ONE_AXIS_HIGHT);
- printf("\nWidth Data Only\n");
- convert_array_type_short_to_vertex(d3, 4, HxW, &lines, ONE_AXIS_WIDTH);
- printf("expected result:\n");
- convertPair(d3[0], i, HxW, ONE_AXIS_WIDTH);
- convertPair(d3[1], ii, HxW, ONE_AXIS_WIDTH);
- convertPair(d3[2], iii, HxW, ONE_AXIS_WIDTH);
- convertPair(d3[3], iiii, HxW, ONE_AXIS_WIDTH);
-
- }
-
- */
-
- /*
- #include "clibs/mathlib/mathlib.h"|
- #include "clibs/glmc/glmc.h"
- #include "clibs/math_3d.h"
- */
-
- int h = 800;
- int w = 600;
-
-
- // https://github.com/mattdesl/lwjgl-basics/wiki/GLSL-Versions
-
- /*
- GLSL Versions
-
- OpenGL Version GLSL Version
- 2.0 110
- 2.1 120
- 3.0 130
- 3.1 140
- 3.2 150
- 3.3 330
- 4.0 400
- 4.1 410
- 4.2 420
- 4.3 430
- GLSL ES Versions (Android, iOS, WebGL)
-
- OpenGL ES has its own Shading Language, and the versioning starts fresh. It is based on OpenGL Shading Language version 1.10.
-
- OpenGL ES Version GLSL ES Version
- 2.0 100
- 3.0 300
-
- ES has no core
-
- #version needs to be the first char in the array:
- array[0] = #
- array[1] = v
- array[2] = v
- array[3] = e
- array[4] = r
- array[5] = s
- array[6] = i
- array[7] = o
- array[8] = n
-
- Version 100
-
- Vertex shader:
-
- uniform mat4 projTrans;
-
- attribute vec2 Position;
- attribute vec2 TexCoord;
-
- varying vec2 vTexCoord;
-
- void main() {
- vTexCoord = TexCoord;
- gl_Position = u_projView * vec4(Position, 0.0, 1.0);
- }
-
- Fragment shader:
-
- uniform sampler2D tex0;
-
- varying vec2 vTexCoord;
-
- void main() {
- vec4 color = texture2D(tex0, vTexCoord);
- gl_FragColor = color;
- }
-
- Version 330
-
- 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.
-
- Vertex shader:
-
- #version 330
-
- uniform mat4 projTrans;
-
- layout(location = 0) in vec2 Position;
- layout(location = 1) in vec2 TexCoord;
-
- out vec2 vTexCoord;
-
- void main() {
- vTexCoord = TexCoord;
- gl_Position = u_projView * vec4(Position, 0, 1);
- }
-
- Fragment shader:
-
- #version 330
- uniform sampler2D tex0;
-
- in vec2 vTexCoord;
-
- //use your own output instead of gl_FragColor
- out vec4 fragColor;
-
- void main() {
- //'texture' instead of 'texture2D'
- fragColor = texture(tex0, vTexCoord);
- }
-
- */
-
- // http://www.shaderific.com/glsl-qualifiers/
-
-
-
- const char * Shader_Vertex =
- "#version 100 \n\
- \n\
- attribute vec2 position; \n\
- \n\
- void main() \n\
- { \n\
- gl_Position = vec4(position, 0.0, 1.0); \n\
- }";
-
- const char * Shader_Fragment =
- "#version 100 \n\
- \n\
- void main() \n\
- { \n\
- gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); \n\
- }";
-
- int compile_check(GLuint shader, const char * name) {
- GLint status;
- glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
- status = reverseBool(status);
- char buffer[512];
- glGetShaderInfoLog(shader, 512, NULL, buffer);
- 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);
- return status;
- }
-
- GLuint vao;
- GLuint vbo;
- int initgl(float * name, int size) {
- glGenBuffers(1, &vbo);
-
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glBufferData(GL_ARRAY_BUFFER, size, name, GL_STATIC_DRAW);
-
- GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vertexShader, 1, &Shader_Vertex, NULL);
- glCompileShader(vertexShader);
-
- if (compile_check(vertexShader, "vertex") =! 0) return 1;
-
- GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fragmentShader, 1, &Shader_Fragment, NULL);
- glCompileShader(fragmentShader);
-
- if (compile_check(fragmentShader, "fragment") =! 0) return 1;
-
- GLuint shaderProgram = glCreateProgram();
- glAttachShader(shaderProgram, vertexShader);
- glAttachShader(shaderProgram, fragmentShader);
- //glBindFragDataLocation(shaderProgram, 0, "outColor");
- glLinkProgram(shaderProgram);
- glUseProgram(shaderProgram);
-
- GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
- glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
- glEnableVertexAttribArray(posAttrib);
-
- glGenBuffers(1, &vao);
-
- glBindVertexArrayOES(vao);
-
- /*
- mat4_t m4_ortho (float left, float right, float bottom, float top, float back, float front);
- mat4_t m4_perspective (float vertical_field_of_view_in_deg, float aspect_ratio, float near_view_distance, float far_view_distance);
- mat4_t m4_look_at (vec3_t from, vec3_t to, vec3_t up);
-
- void mathMatrixPerspectivef( float *m, float fovy, float aspect, float zNear, float zFar )
- void mathMatrixOrthof( float *m, float left, float right, float bottom, float top, float nearval, float farval);
- void mathMatrixRotf( float *mat, float angle, float x, float y, float z );
- void mathMatrixRotxf( float *m, float angle );
- void mathMatrixRotyf( float *m, float angle );
- void mathMatrixRotzf( float *m, float angle );
- void mathMatrixTranslatef( float *m, float x, float y, float z );
- */
-
- //
- /**
- m4_ortho(800.0f, 0.0f, 600.0f, 0.0f, 0.1f, 100.0f);
-
- mat4_t model[16];
- mat4_t view[16];
- mat4_t projection[16];
-
- model = mathMatrixRotxf(model, 0.5f);
- view = m4_look_at(0.0f, 0.0f, -3.0f);
- projection = m4_perspective(0.45f, 800.0f/600.0f, 0.1f, 100.0f);
- */
- return 0;
- }
-
- int gl_main(float * line, int line_size) {
- SDL_Window * window;
- SDL_GLContext context;
- SDL_Init(SDL_INIT_VIDEO);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
- SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
- window=SDL_CreateWindow("OpenGL", w, h, w, h, SDL_WINDOW_OPENGL|SDL_WINDOW_ALLOW_HIGHDPI);
- context = SDL_GL_CreateContext(window);
- if(initgl(line, line_size) != 0) return 1;
- SDL_Event windowEvent;
- //float *ptr = glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY_OES);
- int lines = line_size/sizeof(line[0])/2;
- printf("lines to draw = %i\n", lines);
- while(1)
- {
- if (SDL_PollEvent(&windowEvent))
- {
- if (windowEvent.type == SDL_QUIT) break;
- }
- glDrawArrays(GL_LINE_STRIP, 0, lines);
- SDL_GL_SwapWindow(window);
- usleep(500000);
- }
- sleep(100);
- glUnmapBufferOES(GL_ARRAY_BUFFER);
- SDL_GL_DeleteContext(context);
- SDL_Quit();
- return 0;
-
- }
-
- int main(int argc, char *argv[])
- {
- int lines;
- short line_xy[] = { 5, 50, 400, 400 };
- gl_main(convert_array_type_short_to_vertex(line_xy, sizeof(line_xy), 400, &lines, 0), lines);
-
- }
-
-
- /*
- //Original source code found at : http://programmersranch.blogspot.kr/2014/02/sdl2-pixel-drawing.html
-
- #ifdef _OS_ANDROID_
- #error "Android version does not support SDL2 yet."
- #endif
-
- #include <SDL.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <limits.h>
- int h;
- int w;
- int colour;
-
- SDL_Renderer * renderer;
- SDL_Texture * texture;
- SDL_Window * window;
- Uint32 *pixels;
-
- SDL_Rect SrcR;
- SDL_Rect DestR;
-
-
-
- int zoom;
- int pitch;
- int base = 2;
-
-
-
-
- void init() {
- SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 0);
- audioSampleSize = SampleSize * length;
- // h and w may be the problem, not sure
- h = 1000;
- w = 1000;
- pitch = w*sizeof(Uint32);
- colour = 111;
-
- // 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
- zoom = 275; // 3*3 pixel grid is zoom 275
-
- DestR.x = 0;
- DestR.y = 0;
- DestR.w = w;
- DestR.h = h;
-
- SrcR.x = 0;
- SrcR.y = base-1;//((h/2) - (h * 0.5) / zoom)+1;
- SrcR.w = w/zoom;
- SrcR.h = h/zoom;
-
- SDL_Init(SDL_INIT_VIDEO);
- window=SDL_CreateWindow("a", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, 0);
- renderer=SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
- texture=SDL_CreateTexture(renderer,SDL_PIXELFORMAT_ARGB8888,SDL_TEXTUREACCESS_STREAMING,w,h);
- pixels=malloc(sizeof(Uint32) * w * h);
- memset(pixels,255,w*h*4);
- // create preview
- SDL_RenderClear(renderer);
- SDL_UpdateTexture(texture,0,pixels,pitch);
- SDL_RenderCopy(renderer,texture, &SrcR, &DestR);
- SDL_RenderPresent(renderer);
- 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);
- }
-
-
-
-
-
- void putpixel(int x, int y, int colour) {
- if (x > w || x < 0) printf("x is out of bounds\n");
- else if (y > h || y < 0) printf("y is out of bounds\n");
- else {
- int location = (y * w) + x;
- pixels[location]=colour;
- printf("pixels[%i] = %i\n", location, colour) ;
- }
- }
- int len;
- int draw(short * data)
- {
- // draw graph
- int i = 0;
- SDL_RenderClear(renderer);
- while (i<len) {
- //int loc = (h/2) + ( (h/2) * ( (float) data[i]/SHRT_MAX ) );
- //int loc = (h* (h/2) ) + ( data[i]*h );
- int loc = (h/2) + data[i];
- loc = loc - 500 + base;
- printf("data[%i] = %i, loc = %i, ", i, data[i], loc);
- putpixel(0, loc, 111+(i*150000));
- i++;
- }
- SDL_UpdateTexture(texture,0,pixels,pitch);
- SDL_RenderCopy(renderer,texture, &SrcR, &DestR);
- SDL_RenderPresent(renderer);
-
- n();
- SDL_Delay(1000);
- return 0;
- SDL_Quit();
- }
-
- void draw_free(int * pixels, SDL_Renderer * renderer, SDL_Texture * texture, SDL_Window * window) {
- free(pixels);
- SDL_DestroyTexture(texture);
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- SDL_Quit();
- }
-
- */
-