]> git.alrj.org Git - bold.git/blob - examples/flow2/flow2.c
Update documentation, add example and install script.
[bold.git] / examples / flow2 / flow2.c
1
2 // Linux x86_64 port of flow2, to be linked with the bold linker.
3 // Ported by Amand Tihon (alrj).
4 // Original comments in flow2 source code copied verbatim
5
6
7 // Chris Thornborrow (auld/sek/taj/copabars)
8 // If you use this code please credit...blahblah
9 // Example OGL + shaders in 1k
10 // Requires crinkler - magnificent tool
11 // VS2005 modifications by benny!weltenkonstrukteur.de from dbf
12 //    Greets!
13 // NOTE: DX will beat this no problem at all due to OpenGL forced
14 // to import shader calls as variables..nontheless we dont need
15 // d3dxblahblah to be loaded on users machine.
16
17 #include "GL/gl.h"
18 #include "SDL.h"
19
20 // NOTE: in glsl it is legal to have a fragment shader without a vertex shader
21 //  Infact ATi/AMD  drivers allow this but unwisely forget to set up variables for
22 // the fragment shader - thus all GLSL programs must have a vertex shader :-(
23 // Thanks ATI/AMD
24
25 // This is pretty dirty...note we do not transform the rectangle but we do use
26 // glRotatef to pass in a value we can use to animate...avoids one more getProcAddress later
27 const GLchar *vsh="\
28 varying vec4 p;\
29 void main(){\
30 p=sin(gl_ModelViewMatrix[1]*9.0);\
31 gl_Position=gl_Vertex;\
32 }";
33
34 // an iterative function for colour
35 const GLchar *fsh="\
36 varying vec4 p;\
37 void main(){\
38 float r,t,j;\
39 vec4 v=gl_FragCoord/400.0-1.0;\
40 r=v.x*p.r;\
41 for(int j=0;j<7;j++){\
42 t=v.x+p.r*p.g;\
43 v.x=t*t-v.y*v.y+r;\
44 v.y=p.g*3.0*t*v.y+v.y;\
45 }\
46 gl_FragColor=vec4(mix(p,vec4(t),max(t,v.x)));\
47 }";
48 //p.g*3.0*t*v.y+i;\
49
50 static void setShaders() {
51
52   GLuint v = glCreateShader(GL_VERTEX_SHADER);
53   GLuint f = glCreateShader(GL_FRAGMENT_SHADER);
54   GLuint p = glCreateProgram();
55
56   glShaderSource(v, 1, &vsh, NULL);
57   glCompileShader(v);
58   glAttachShader(p, v);
59   glShaderSource(f, 1, &fsh, NULL);
60   glCompileShader(f);
61   glAttachShader(p, f);
62   glLinkProgram(p);
63   glUseProgram(p);
64 }
65
66 int main()
67 {
68   static SDL_Event e;
69   SDL_Init(SDL_INIT_VIDEO);
70   SDL_SetVideoMode(1024, 768, 0, SDL_OPENGL);
71
72   SDL_ShowCursor(SDL_DISABLE);
73   setShaders();
74    //**********************
75    // NOW THE MAIN LOOP...
76    //**********************
77    // there is no depth test or clear screen...as we draw in order and cover
78    // the whole area of the screen.
79   do
80   {
81     glRotatef(0.3f, 1, 1, 1);
82     glRecti(-1, -1, 1, 1);
83     SDL_GL_SwapBuffers();
84     
85     SDL_PollEvent(&e);
86     
87     if (e.type == SDL_KEYDOWN)
88       break;
89   } while (e.type != SDL_QUIT);
90
91   return 0;
92 }
93