summaryrefslogtreecommitdiff
path: root/fractal.c
blob: ab326bdfad0f8379b1be6cae7a40d67a37ff98dc (plain) (blame)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <stdint.h>
#include <time.h>

#if __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <GLES2/gl2.h>
#else
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <GLES2/gl2.h>
#endif

#include "koh.h"

#define SCREEN_WIDTH   320
#define SCREEN_WIDTH2  (SCREEN_WIDTH/2)
#define SCREEN_HEIGHT  320
#define SCREEN_HEIGHT2 (SCREEN_HEIGHT/2)

static int quit=0;
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;

typedef struct rgba32_t
{
    double r,g,b,a;
} rgba32_t;

#define MAX_LEVEL 7
#define QUEUE_SIZE (1+MAX_LEVEL*MAX_LEVEL*MAX_LEVEL*MAX_LEVEL*3)

int queue_index=0;
koh_node koh_queue[QUEUE_SIZE];
koh_config koh_set_config;
int first_run=1;



void queue_init()
{
    int i;
    queue_index = 0;
    for (i=0;i<QUEUE_SIZE;i++)
    {
        koh_queue[i].generaton = 0;
        koh_queue[i].size      = 0;
        koh_queue[i].x         = 0;
        koh_queue[i].y         = 0;
    }
}


void queue_add(koh_node kn)
{
    if (queue_index >= QUEUE_SIZE)
    {
        printf("Cannot add to queue\n");
        return ;
    }
    koh_queue[queue_index] = kn;
    queue_index += 1;
    return;
}


koh_node queue_get(int idx)
{
    koh_node kn = {-1,-1,-1,-1};

    if ((idx<0) || (idx>=QUEUE_SIZE))
    {
        return kn;
    }

    kn = koh_queue[idx];

    return kn;
}

rgba32_t color_gen_shade(rgba32_t original, int gen, int max_gen)
{
    rgba32_t c = {0,0,0,0};
    
    c.r = (1.0*original.r/max_gen)*gen;
    c.g = (1.0*original.g/max_gen)*gen;
    c.b = (1.0*original.b/max_gen)*gen;
    c.a = original.a;    
    
    return c;
}
rgba32_t color_gen_shade_inv(rgba32_t original, int gen, int max_gen)
{
    rgba32_t c = {0,0,0,0};
    
    c.r = original.r-(1.0*original.r/max_gen)*gen;
    c.g = original.g-(1.0*original.g/max_gen)*gen;
    c.b = original.b-(1.0*original.b/max_gen)*gen;
    c.a = original.a;
    
    return c;
}

rgba32_t color_gen_mainlv(rgba32_t original, int gen, int max_gen)
{
    double color_r = 10.0/255;
    double color_g = 20.0/255;
    double color_b = 14.0/255;
    rgba32_t c = {color_r, color_g, color_b,0};

    c.r = 1.0*180/255 - (max_gen-gen)*color_r;
    c.g = 1.0*100/255 - (max_gen-gen)*color_g;
    c.b = 1.0*50/255  - (max_gen-gen)*color_b;
    c.a = original.a;

    return c;
}

rgba32_t color_gen_redish(rgba32_t original, int gen, int max_gen)
{
    double color_r = 20.0/255;
    double color_g = 10.0/255;
    double color_b = 14.0/255;
    rgba32_t c = {color_r, color_g, color_b,0};

    c.r = 1.0*180/255 - (max_gen-gen)*color_r;
    c.g = 1.0*30/255 - (max_gen-gen)*color_g;
    c.b = 1.0*10/255  - (max_gen-gen)*color_b;
    c.a = original.a;

    return c;
}

rgba32_t color_gen_yellowish(rgba32_t original, int gen, int max_gen)
{
    double color_r = 10.0/255;
    double color_g = 20.0/255;
    double color_b = 14.0/255;
    rgba32_t c = {color_r, color_g, color_b,0};

    c.r = 1.0*180/255 - (max_gen-gen)*color_r;
    c.g = 1.0*160/255 - (max_gen-gen)*color_g;
    c.b = 1.0*40/255  - (max_gen-gen)*color_b;
    c.a = original.a;

    return c;
}

rgba32_t color_switch(int pattern_id, rgba32_t original, int gen, int max_gen)
{
    switch(pattern_id)
    {
        case 0:
            return color_gen_shade(original, gen, max_gen);
        case 1:
            return color_gen_shade_inv(original, gen, max_gen);
        case 2:
            return color_gen_mainlv(original, gen, max_gen);
        case 3:
            return color_gen_yellowish(original, gen, max_gen);
        case 4:
            return color_gen_redish(original, gen, max_gen);
        default:
            return color_gen_mainlv(original, gen, max_gen);
    }

    return color_gen_mainlv(original, gen, max_gen);
}

void generate_koh(int gener, double size, int posx, int posy, double k)
{
    int i;
    koh_node kn = {-1,-1,-1,-1};

    double t_size = size*k;

    if (gener<0)
    {
        return;
    }

    kn.generaton = gener;
    kn.size      = size;
    kn.x         = posx;
    kn.y         = posy;

    
    //printf("Gener %d posx %d posy %d size %f=>%f\n",gener,posx,posy,size,t_size);
    queue_add(kn);

    generate_koh(gener-1, t_size, posx-t_size/2,      posy-t_size/2,      k);
    generate_koh(gener-1, t_size, posx-t_size/2,      posy+size-t_size/2, k);
    generate_koh(gener-1, t_size, posx+size-t_size/2, posy+size-t_size/2, k);
    generate_koh(gener-1, t_size, posx+size-t_size/2, posy-t_size/2,      k);
}

void Triangle( int x, int y, int w, int h)
{
    // An array of 3 vectors which represents 3 vertices
    static const GLfloat g_vertex_buffer_data[] = {
    -1.0f, -1.0f, 0.0f,
    1.0f, -1.0f, 0.0f,
    0.0f,  1.0f, 0.0f,
    };

    // This will identify our vertex buffer
    GLuint vertexbuffer;
    // Generate 1 buffer, put the resulting identifier in vertexbuffer
    glGenBuffers(1, &vertexbuffer);
    // The following commands will talk about our 'vertexbuffer' buffer
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    // Give our vertices to OpenGL.
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

    // 1st attribute buffer : vertices
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glVertexAttribPointer(
    0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
    3,                  // size
    GL_FLOAT,           // type
    GL_FALSE,           // normalized?
    0,                  // stride
    (void*)0            // array buffer offset
    );
    // Draw the triangle !
    glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
    glDisableVertexAttribArray(0);

}

// Shader sources
#if 1
const GLchar* vertexSource =
    "#version 100 \n"
    "attribute vec4 position;    \n"
    "attribute vec4 colour;      \n"
    "varying vec4 interpolated_colour;\n"
    "void main()                  \n"
    "{                            \n"
    "   interpolated_colour = colour;\n"
    "   gl_Position = position;  \n"
    "   \n"
    "}                            \n";
#else
const GLchar* vertexSource = 
"layout(location = 0) in vec3 vertexPosition;\n"
"layout(location = 1) in vec3 vertexColor;\n"
"out vec3 fragmentColor;\n"
"uniform mat4 MVP;\n"
"void main(){	\n"
"	gl_Position =  vec4(vertexPosition,1);\n"
"	fragmentColor = vertexColor;\n"
"}\n";
#endif

#if 1
const GLchar* fragmentSource =
    "#version 100 \n"
    "precision mediump float;\n"
    "varying vec4 interpolated_colour;\n"
    "void main()                                  \n"
    "{                                            \n"
    "  gl_FragColor = interpolated_colour;\n"
    "}                                            \n";
#else
const GLchar* fragmentSource =
"in vec3 fragmentColor;\n"
"out vec3 color;\n"
"void main(){\n"
"	color = vec3(0.0,0.0,0.0);\n"
"}\n";
#endif

GLuint vertexShader;
GLuint fragmentShader;
GLuint shaderProgram;
GLint posAttrib;


void RectPX( int x, int y,int w, int h )
{
    GLfloat x1 = (1.0f*x-SCREEN_WIDTH2)/SCREEN_WIDTH2;
    GLfloat y1 = (1.0f*y-SCREEN_HEIGHT2)/SCREEN_HEIGHT2;
    GLfloat x2 = (1.0f*(x+w)-SCREEN_WIDTH2)/SCREEN_WIDTH2;
    GLfloat y2 = (1.0f*(y+h)-SCREEN_HEIGHT2)/SCREEN_HEIGHT2;
    GLfloat vVertices[] = { x1, y2, 0.0f, 
                            x2, y2, 0.0f,
                            x2, y1, 0.0f,
                            x1, y1, 0.0f };
    GLuint vertexPosObject;

    //printf("x1 %f y1 %f x2 %f y2 %f\n",x1,y1,x2,y2);

    glGenBuffers(1, &vertexPosObject);
    glBindBuffer(GL_ARRAY_BUFFER, vertexPosObject);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vVertices), vVertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexPosObject);
    glVertexAttribPointer(0 /* ? */, 3, GL_FLOAT, GL_FALSE, 0, 0);
    
    glDrawArrays (  GL_TRIANGLE_FAN, 0, 4 );

}


void RectPXcolor( int x, int y,int w, int h, rgba32_t c )
{
    GLfloat x1 = (1.0f*x-SCREEN_WIDTH2)/SCREEN_WIDTH2;
    GLfloat y1 = (1.0f*y-SCREEN_HEIGHT2)/SCREEN_HEIGHT2;
    GLfloat x2 = (1.0f*(x+w)-SCREEN_WIDTH2)/SCREEN_WIDTH2;
    GLfloat y2 = (1.0f*(y+h)-SCREEN_HEIGHT2)/SCREEN_HEIGHT2;
    GLfloat vVertices[] = { x1, y2, 0.0f, 
                            x2, y2, 0.0f,
                            x2, y1, 0.0f,
                            x1, y1, 0.0f };
    GLfloat cVertices[] = { c.r, c.g, c.b,
                            c.r, c.g, c.b,
                            c.r, c.g, c.b,
                            c.r, c.g, c.b};
    GLuint vertexPosObject;
    GLuint colorBuffer;

    glGenBuffers(1, &vertexPosObject);
    glBindBuffer(GL_ARRAY_BUFFER, vertexPosObject);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vVertices), vVertices, GL_STATIC_DRAW);

    //printf("!!!x1 %f y1 %f x2 %f y2 %f\n",x1,y1,x2,y2);
    glGenBuffers(1, &colorBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(cVertices), cVertices, GL_STATIC_DRAW);
    
    
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexPosObject);
    glVertexAttribPointer(0 /* ? */, 3, GL_FLOAT, GL_FALSE, 0, 0);


    glEnableVertexAttribArray(1);
    glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
    glVertexAttribPointer(1 /* ? */, 3, GL_FLOAT, GL_FALSE, 0, 0);
    
    glDrawArrays (  GL_TRIANGLE_FAN, 0, 4 );

    glDeleteBuffers( 1, &vertexPosObject );
    glDeleteBuffers( 1, &colorBuffer );

}

void koh_generation_inc()
{
    koh_set_config.max_generations += 1;
    if (koh_set_config.max_generations > MAX_LEVEL-1)
    {
        koh_set_config.max_generations = MAX_LEVEL-1;
    }
}

void koh_generation_dec()
{
    koh_set_config.max_generations -= 1;
    if (koh_set_config.max_generations < 0)
    {
        koh_set_config.max_generations = 0;
    }
}

void koh_pos_x_add(int x)
{
    koh_set_config.x += 10;
}

void koh_pos_x_sub(int x)
{
    koh_set_config.x -= 10;
}

void koh_pos_y_add(int y)
{
    koh_set_config.y += 10;
}

void koh_pos_y_sub(int y)
{
    koh_set_config.y -= 10;
}

void koh_pattern_next()
{
    koh_set_config.color_pattern += 1;
    if (koh_set_config.color_pattern>4)
    {
        koh_set_config.color_pattern=4;
    }
}

void koh_pattern_prev()
{
    koh_set_config.color_pattern -= 1;
    if (koh_set_config.color_pattern<0)
        koh_set_config.color_pattern=0;
}

void koh_size_dec()
{
    koh_set_config.size -= 25;
    if (koh_set_config.size<50)
        koh_set_config.size = 50;
}

void koh_size_inc()
{
    koh_set_config.size += 25;
    if (koh_set_config.size>400)
        koh_set_config.size = 400;
}

rgba32_t start_col = {0.0,1.0,0.0,0.0};

#if __EMSCRIPTEN__
void main_tick() {
#else
int main_tick() {
#endif
	int i,j;
    int update_fractal=0;
    SDL_Event event;
	SDL_StartTextInput();
        while (SDL_PollEvent(&event) != 0)
        {
                    
            switch (event.type)
            {
                case SDL_QUIT:
                {
                    quit = 1;
                    break;
                }
                case SDL_KEYDOWN:
                {
                    switch (event.key.keysym.sym)
                    {
                    case 81:
                    case SDLK_DOWN:
                        update_fractal = 1;
                        koh_pos_y_sub(10);
                        break;

                    case 82:
                    case SDLK_UP:
                        update_fractal = 1;
                        koh_pos_y_add(10);
                        break;
                    case 80:
                    case SDLK_LEFT:
                        koh_pos_x_sub(10);
                        update_fractal = 1;
                        printf("sub 10\n");
                        break;
                    case 79:
                    case SDLK_RIGHT:
                        update_fractal = 1;
                        printf("add 10\n");
                        koh_pos_x_add(10);
                        break;

                    case 61:
                    case 46:
                    case SDLK_KP_PLUS:
                    case SDLK_PLUS:
                        update_fractal=1;
                        koh_generation_inc();
                        printf("plus\n");
                        break;

                    case SDLK_MINUS:
                        update_fractal = 1;
                        koh_generation_dec();
                        break;

                    case 21:
                    case SDLK_r:
                        update_fractal = 1;
                        koh_set_config.size = 100.0;
                        koh_set_config.x = SCREEN_WIDTH2-100/2;
                        koh_set_config.y = SCREEN_WIDTH2-100/2;
                        koh_set_config.k = 0.5f;
                        koh_set_config.max_generations = MAX_LEVEL-1;
                        koh_set_config.color_pattern = 0;
                        break;
                    
                    case 20:
                    case SDLK_q:
                        koh_pattern_prev();
                        break;
                    
                    case 26:
                    case SDLK_w:
                        koh_pattern_next();
                        break;
                    
                    case 4:
                    case SDLK_a:
                        update_fractal = 1;
                        koh_size_dec();
                        break;
                    
                    case 22:
                    case SDLK_s:
                        update_fractal = 1;
                        koh_size_inc();
                        break;

                    default:
                        printf("Unknown key %d\n",event.key.keysym.sym);
                        update_fractal = 1;
                    }
                    break;
                }
            }
            //printf("Poll event\n");
        }

        //check for first ever run
        if (first_run == 1)
        {
            first_run = 0;
            update_fractal = 1;
        }

        if (update_fractal == 1)
        {
            queue_init();
            printf("k=%f",koh_set_config.k);
            generate_koh(
                koh_set_config.max_generations, 
                koh_set_config.size, 
                koh_set_config.x, 
                koh_set_config.y, 
                koh_set_config.k
            );
            printf("Regenerated fractal\n");
        }
        
        
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glUseProgram(shaderProgram);

        for (i=0;i<=koh_set_config.max_generations;i++)
        {
            rgba32_t c = color_switch(koh_set_config.color_pattern, start_col, i, MAX_LEVEL);
            for (j=0;j<queue_index;j++)
            {
                
                
                if (koh_queue[j].generaton == i)
                {
                    koh_node kn = queue_get(j);
                    #ifndef __EMSCRIPTEN__
                    if (update_fractal == 1)
                    #endif
                        RectPXcolor(kn.x, kn.y, kn.size, kn.size, c);
                }
            }
        }
        #ifndef __EMSCRIPTEN__
        if (update_fractal == 1)
        #endif
        {
            SDL_GL_SwapWindow( window );
        }
        update_fractal=0;

#if !__EMSCRIPTEN__
	return 0;
#endif
}

void main_loop()
{

#if __EMSCRIPTEN__
    emscripten_set_main_loop(main_tick, 25, 1);
#else
	while (0 == quit)
	{
		main_tick();
	}
#endif
}

int main()
{
    int i,j;
    SDL_Event event;
    GLenum error = GL_NO_ERROR;
    GLint Result = GL_FALSE;
	int InfoLogLength;
    
    printf("Max queue size %d\n", QUEUE_SIZE);
    SDL_Init(SDL_INIT_VIDEO);
    //Use OpenGL 2.1
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 2 );
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 );
    window = SDL_CreateWindow("KOH", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN);
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    SDL_SetRenderDrawColor(renderer, 0xff, 0xaa, 0xaa, 0xaa);
    SDL_GLContext gl_context = SDL_GL_CreateContext(window);
    SDL_GL_SetSwapInterval( 0 );

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);


    //init OpenGL

    error = glGetError();
	if( error != GL_NO_ERROR )
	{
		printf( "Error initializing OpenGL! \n");
	}
    error = glGetError();
	if( error != GL_NO_ERROR )
	{
		printf( "Error initializing OpenGL! \n");
	}
    glClearColor( 0.f, 0.f, 0.f, 1.f );
    error = glGetError();
	if( error != GL_NO_ERROR )
	{
		printf( "Error initializing OpenGL!\n" );
	}

    //set initial values
    const int frac_size=100;
    koh_set_config.size = 100;
    koh_set_config.x = SCREEN_WIDTH2-frac_size/2;
    koh_set_config.y = SCREEN_WIDTH2-frac_size/2;
    koh_set_config.k = 0.5f;
    koh_set_config.max_generations = MAX_LEVEL-1;

    generate_koh(
        koh_set_config.max_generations, 
        koh_set_config.size, 
        koh_set_config.x, 
        koh_set_config.y, 
        koh_set_config.k
    );

    //prepare shaders
    vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexSource, NULL);
    glCompileShader(vertexShader);
    {
        char buf[1024];
        glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &Result);
        glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &InfoLogLength);
        glGetShaderInfoLog(vertexShader, InfoLogLength, NULL, &buf[0]);
        printf("shader %s\n",buf);
        if (Result == GL_FALSE)
        {
            exit(1);
        }
    }

    // Create and compile the fragment shader
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
    glCompileShader(fragmentShader);
    {
        char buf[1024];
        glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &Result);
        glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &InfoLogLength);
        glGetShaderInfoLog(fragmentShader, InfoLogLength, NULL, &buf[0]);
        printf("fragment %s\n",buf);
        if (Result == GL_FALSE)
        {
            exit(1);
        }
    }

    // Link the vertex and fragment shader into a shader program
    shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    glUseProgram(shaderProgram);

    posAttrib = glGetAttribLocation(shaderProgram, "position");
    glEnableVertexAttribArray(posAttrib);
    //changed from 2 to 3
    glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);

    main_loop();

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}