summaryrefslogtreecommitdiff
path: root/metaball.cpp
blob: cbc2b3694eca2687b6ba942baa856fc902688f49 (plain)
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
#include <stdio.h>
#include <unistd.h>
#include "SDL.h"
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glext.h>


static PFNGLGETOBJECTPARAMETERIVARBPROC		eglGetObjectParameterivARB      =NULL;
static PFNGLGETINFOLOGARBPROC			eglGetInfoLogARB		=NULL;
static PFNGLCREATESHADEROBJECTARBPROC		eglCreateShaderObjectARB	=NULL;
static PFNGLSHADERSOURCEARBPROC			eglShaderSourceARB		=NULL;
static PFNGLCOMPILESHADERARBPROC		eglCompileShaderARB		=NULL;
static PFNGLCREATEPROGRAMOBJECTARBPROC		eglCreateProgramObjectARB	=NULL;
static PFNGLATTACHOBJECTARBPROC			eglAttachObjectARB		=NULL;
static PFNGLLINKPROGRAMARBPROC			eglLinkProgramARB		=NULL;
static PFNGLUNIFORM1IARBPROC			eglUniform1iARB			=NULL;
static PFNGLGETUNIFORMLOCATIONARBPROC		eglGetUniformLocationARB	=NULL;
static PFNGLUSEPROGRAMOBJECTARBPROC		eglUseProgramObjectARB		=NULL;

static void vr_glext_glsl_init_exts()
{
	static bool init=false;
	if (init)
		return;
	eglGetObjectParameterivARB=(PFNGLGETOBJECTPARAMETERIVARBPROC)SDL_GL_GetProcAddress("glGetObjectParameterivARB");
	eglGetInfoLogARB=(PFNGLGETINFOLOGARBPROC)SDL_GL_GetProcAddress("glGetInfoLogARB");
	eglCreateShaderObjectARB=(PFNGLCREATESHADEROBJECTARBPROC)SDL_GL_GetProcAddress("glCreateShaderObjectARB");
	eglShaderSourceARB=(PFNGLSHADERSOURCEARBPROC)SDL_GL_GetProcAddress("glShaderSourceARB");
	eglCompileShaderARB=(PFNGLCOMPILESHADERARBPROC)SDL_GL_GetProcAddress("glCompileShaderARB");
	eglCreateProgramObjectARB=(PFNGLCREATEPROGRAMOBJECTARBPROC)SDL_GL_GetProcAddress("glCreateProgramObjectARB");
	eglAttachObjectARB=(PFNGLATTACHOBJECTARBPROC)SDL_GL_GetProcAddress("glAttachObjectARB");
	eglLinkProgramARB=(PFNGLLINKPROGRAMARBPROC)SDL_GL_GetProcAddress("glLinkProgramARB");
	eglUniform1iARB=(PFNGLUNIFORM1IARBPROC)SDL_GL_GetProcAddress("glUniform1iARB");
	eglGetUniformLocationARB=(PFNGLGETUNIFORMLOCATIONARBPROC)SDL_GL_GetProcAddress("glGetUniformLocationARB");
	eglUseProgramObjectARB=(PFNGLUSEPROGRAMOBJECTARBPROC)SDL_GL_GetProcAddress("glUseProgramObjectARB");
}


static void vr_glext_glsl_dump_log(GLhandleARB p,const GLcharARB* shader)
{
	vr_glext_glsl_init_exts();
	GLint log_size;
	eglGetObjectParameterivARB(p, GL_OBJECT_INFO_LOG_LENGTH_ARB, &log_size);
	if (log_size>1)
	{
		GLcharARB* s=(GLcharARB*)malloc((log_size+1)*sizeof(GLcharARB));
		eglGetInfoLogARB(p,log_size,NULL,s);
		printf("============================================================\n");
		printf("Faulty ARB program. Log (%d bytes) : \n%s\n",log_size,s);
		free(s);
		printf("Faulty ARB program : \n");
		int lc=0;
		printf("\n%4d: ",++lc);
		for(int i=0;i<(int)strlen(shader);i++)
		{
			if (shader[i]=='\n')
				printf("\n%4d: ",++lc);
			else
				printf("%c",shader[i]);
		}
		printf("\n");
		printf("============================================================\n");
	}
}

bool vr_glext_glsl_create_program(const GLcharARB* vshader,const GLcharARB* fshader,GLhandleARB* res)
{
	vr_glext_glsl_init_exts();
	GLint ok;

	GLhandleARB vs=eglCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
	GLhandleARB fs=eglCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
	eglShaderSourceARB(vs, 1, &vshader,NULL);
	eglShaderSourceARB(fs, 1, &fshader,NULL);
	eglCompileShaderARB(vs);
	vr_glext_glsl_dump_log(vs,vshader);
	eglGetObjectParameterivARB(vs,GL_OBJECT_COMPILE_STATUS_ARB,&ok);
	if (!ok)
	{
		printf("compiling vertex shader\n");
		return false;
	}
	eglCompileShaderARB(fs);
	vr_glext_glsl_dump_log(fs,fshader);
	eglGetObjectParameterivARB(fs,GL_OBJECT_COMPILE_STATUS_ARB,&ok);
	if (!ok)
	{
		printf("compiling fragment shader\n");
		return false;
	}
	GLhandleARB p = eglCreateProgramObjectARB();
	eglAttachObjectARB(p,vs);
	eglAttachObjectARB(p,fs);
	eglLinkProgramARB(p);
	vr_glext_glsl_dump_log(p,"(Linking Stage)");
	eglGetObjectParameterivARB(p,GL_OBJECT_LINK_STATUS_ARB,&ok);
	if (!ok)
	{
		printf("linking program\n");
		return false;
	}
	*res=p;
	return true;
}

char vshader[] = 
"void main()"
"{"
"	gl_TexCoord[0] = gl_Vertex;"
"	gl_Position = ftransform();"
"}";

char fshader_pass1[] =
"uniform vec2 center;									\n"
"void main ()										\n"
"{											\n"
"	float dist = 400.0 / ( (gl_TexCoord[0].x - center.x)*(gl_TexCoord[0].x - center.x) + (gl_TexCoord[0].y - center.y)*(gl_TexCoord[0].y - center.y));	\n"
"	gl_FragColor = vec4(dist,dist,dist,1.0);						\n"
"}											\n"
;

char fshader_pass2[] =
"uniform sampler2D pass1_tex;								\n"
"void main ()										\n"
"{											\n"
"	const vec2 light=vec2(-0.707, -0.707);						\n"
"	vec4 v=texture2D(pass1_tex, gl_TexCoord[0].xy/vec2(1024.0));			\n"
"	float vv = v.r;									\n"
"	float dx = dFdx(vv);								\n"
"	float dy = dFdy(vv);								\n"
"	vec2 gradient = (vec2(dx,dy))*vec2(2000.0);					\n"
"	float shading = 0.1 + 0.05 * min(max(dot(gradient,light),0.0),200.0);		\n"
"	if (vv>0.1)									\n"
"	{										\n"
"		gl_FragColor = vec4(0.1,0.2,0.6,200.0) * vec4(shading);			\n"
"		return;									\n"
"	}										\n"
"	gl_FragColor = vec4(0.1,0.1,0.2,0.0);						\n"
"}											\n"
;

GLuint render_tex;


#define NUM_BALLS 10
float ball[NUM_BALLS*2];
float balldir[NUM_BALLS*2];

int main(int argc, char* argv[])
{
	int i;

	SDL_Init( SDL_INIT_EVERYTHING );
	SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
	SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
	SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
	SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 0 );
	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
	SDL_SetVideoMode( 1024, 1024, 0, SDL_OPENGL );

	GLhandleARB prog_pass1,prog_pass2;

	if ( !vr_glext_glsl_create_program(vshader,fshader_pass1,&prog_pass1) )
		return 0;

	if ( !vr_glext_glsl_create_program(vshader,fshader_pass2,&prog_pass2) )
		return 0;

	glDisable(GL_CULL_FACE);
	glDisable(GL_BLEND);
	glDisable(GL_DEPTH_TEST);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity ();
	glOrtho(0.,1024., 0.,1024.,-1.0,1.0);
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity ();
	glColor4f(1.0, 1.0, 1.0, 1.0);

	glGenTextures(1,&render_tex);

	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 1);
	glBindTexture(GL_TEXTURE_2D, render_tex);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE32F_ARB, 1024,1024, 0, GL_LUMINANCE, GL_FLOAT, NULL);

	// Attach texture to FBO
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, render_tex, 0);

	i=0;

	srand(42);
	int before = SDL_GetTicks();
	for(int i=0;i<NUM_BALLS*2;i++)
	{
		ball[i] = (float)(rand()%1024);
		balldir[i] = (float)((rand()%10)-5);
	}

	while(i<1000)
	{
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity ();
		glOrtho(0.,1024., 0.,1024.,-1.0,1.0);
		glMatrixMode (GL_MODELVIEW);
		glLoadIdentity ();

		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 1);
		glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
		glViewport(0,0,1024,1024);
		glClearColor(0.0,0.0,0.0,0.0);
		glClear(GL_COLOR_BUFFER_BIT);

		glDisable(GL_TEXTURE_2D);
		glEnable(GL_BLEND);
		glBlendFunc(GL_ONE,GL_ONE);

		glUseProgramObjectARB(prog_pass1);
		for(unsigned b=0;b<NUM_BALLS;b++)
		{
			float x= ball[b*2];
			float y= ball[b*2+1];
			glUniform2fARB(eglGetUniformLocationARB(prog_pass1, "center"),x,y);
			float r = 800.0;
			float minx = x - r;
			float maxx = x + r;
			float miny = y - r;
			float maxy = y + r;

			glBegin(GL_QUADS);
			glVertex2f(minx,miny);
			glVertex2f(maxx,miny);
			glVertex2f(maxx,maxy);
			glVertex2f(minx,maxy);
			glEnd();

			ball[b*2] += balldir[b*2];
			ball[b*2+1] += balldir[b*2+1];

			if ( (ball[b*2]<0) || (ball[b*2]>1024))
				balldir[b*2] = -balldir[b*2];
			if ( (ball[b*2+1]<0) || (ball[b*2+1]>1024))
				balldir[b*2+1] = -balldir[b*2+1];

		}

		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

		glDisable(GL_BLEND);

		glUseProgramObjectARB(prog_pass2);

		glActiveTextureARB( GL_TEXTURE0_ARB );
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, render_tex);
		eglUniform1iARB(eglGetUniformLocationARB(prog_pass2, "pass1_tex"),0);

		glBegin(GL_QUADS);
		glTexCoord2f(0.0, 0.0); glVertex2f(0.0,0.0);
		glTexCoord2f(1.0, 0.0); glVertex2f(1024.0, 0.0);
		glTexCoord2f(1.0, 1.0); glVertex2f(1024.0, 1024.0);
		glTexCoord2f(0.0, 1.0); glVertex2f(0.0, 1024.0);
		glEnd();

		SDL_GL_SwapBuffers();

		i++;
	}
	int after = SDL_GetTicks();
	float time = (after-before)/1000.0;
	printf("%.2f seconds total (avg %.2f fps)\n",time,i/time);

	SDL_Quit();
}