diff options
Diffstat (limited to 'fractal.c')
-rw-r--r-- | fractal.c | 140 |
1 files changed, 129 insertions, 11 deletions
@@ -135,7 +135,7 @@ void generate_koh(int gener, double size, int posx, int posy, double k) int i; koh_node kn = {-1,-1,-1,-1}; - printf("%f %f\n",size,k); + //printf("%f %f\n",size,k); double t_size = size*k; if (gener<0) @@ -150,7 +150,7 @@ void generate_koh(int gener, double size, int posx, int posy, double k) kn.y = posy; - printf("Gener %d posx %d posy %d size %f=>%f\n",gener,posx,posy,size,t_size); + //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); @@ -195,18 +195,47 @@ void Triangle( int x, int y, int w, int h) } // 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" - " gl_Position = vec4(position.xyz, 1.0); \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 = vec4 (1.0, 1.0, 1.0, 1.0 );\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; @@ -225,21 +254,63 @@ void RectPX( int x, int y,int w, int h ) x1, y1, 0.0f }; GLuint vertexPosObject; - printf("x1 %f y1 %f x2 %f y2 %f\n",x1,y1,x2,y2); + //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); - //glClear ( GL_COLOR_BUFFER_BIT ); + + 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); + + //glDisableVertexAttribArray(0); + //glDisableVertexAttribArray(1); glDrawArrays ( GL_TRIANGLE_FAN, 0, 4 ); } -rgba32_t start_col = {1.0,1.0,1.0,0.0}; +rgba32_t start_col = {0.0,1.0,0.0,0.0}; #if __EMSCRIPTEN__ void main_tick() { @@ -247,6 +318,7 @@ void main_tick() { int main_tick() { #endif int i,j; + int update_fractal=0; SDL_Event event; SDL_StartTextInput(); while (SDL_PollEvent(&event)) @@ -259,16 +331,37 @@ int main_tick() { quit = 1; break; } + case SDL_KEYDOWN: + { + switch (event.key.keysym.sym) + { + case SDLK_DOWN: + update_fractal = 1; + break; + case SDLK_UP: + update_fractal = 1; + break; + case SDLK_LEFT: + update_fractal = 1; + break; + case SDLK_RIGHT: + update_fractal = 1; + break; + } + } } } - glClear( GL_COLOR_BUFFER_BIT ); + //glClear( GL_COLOR_BUFFER_BIT ); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glUseProgram(shaderProgram); for (i=0;i<=MAX_GENERATION;i++) { //rgba32_t c = color_gen_shade(start_col, i, MAX_GENERATION); rgba32_t c = color_gen_mainlv(start_col, i, MAX_GENERATION); //static int once=0 + //glColor(1.0f,0.0,0.0,0.0); for (j=0;j<queue_index;j++) { @@ -277,7 +370,8 @@ int main_tick() { { koh_node kn = queue_get(j); printf("j:%d %d %d %d\n",j,kn.x, kn.y, kn.size); - RectPX(kn.x, kn.y, kn.size, kn.size); + //RectPX(kn.x, kn.y, kn.size, kn.size); + RectPXcolor(kn.x, kn.y, kn.size, kn.size, c); } } } @@ -310,6 +404,8 @@ 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); @@ -334,7 +430,7 @@ int main() { printf( "Error initializing OpenGL! \n"); } - glClearColor( 0.f, 0.f, 1.f, 1.f ); + glClearColor( 0.f, 0.f, 0.f, 1.f ); error = glGetError(); if( error != GL_NO_ERROR ) { @@ -348,11 +444,33 @@ int main() 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(); @@ -361,7 +479,7 @@ int main() glLinkProgram(shaderProgram); glUseProgram(shaderProgram); - posAttrib = glGetAttribLocation(shaderProgram, "position"); + posAttrib = glGetAttribLocation(shaderProgram, "vertexPosition"); glEnableVertexAttribArray(posAttrib); //changed from 2 to 3 glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0); |