Re: pointers to variables

new topic     » goto parent     » topic index » view thread      » older message » newer message

Semi-related to this, fractal computations seem like something that the GPU is highly suitable for. As an example, here's how you could render a static Mandelbrot set using a GLSL shader:

without warning 
 
include eugl.ew 
 
 
constant texID = allocate(4) 
 
atom theta, lastTime	 
atom program, fragmentShader, vertexShader 
atom mytexture, twist 
 
constant fragmentShaderCode = 
"void main() {\n"& 
"vec2 c,z;\n"& 
"float x0,y0,i;\n"& 
"z = vec2(0.0, 0.0);\n"& 
"x0 = gl_FragCoord.x/320.0 - 1.0;\n"& 
"y0 = gl_FragCoord.y/240.0 - 1.0;\n"& 
"c = vec2(x0, y0);\n"& 
"i = 0.0;\n"& 
"while (true) {\n"& 
"  if (length(z) > 2.0) break;\n"& 
"  z = vec2(x0 + z.x*z.x - z.y*z.y + x0, y0 + z.x*z.y*2.0);\n"& 
"  i += 1.0;\n"& 
"  if (i>1000.0) break;\n"& 
"}\n"& 
"if (i>1000.0) i = 0.0;\n"& 
"gl_FragColor = vec4(i, i, i, i) / 75.0;\n"& 
"}" 
 
 
procedure init() 
	theta = 0 
	 
	-- Enable the required extension 
 	if gl_getVersion() >= 2.0 then 
 		-- Enable the features of OpenGL 2.0 (needed for the shader object functions). 
 		gl_enableExtension("GL_VERSION_2_0") 
 	else 
 		if message_box("Sorry, this program requires OpenGL version 2.0 or higher to run", "Error",MB_OK) then 
  		end if 
  		abort(0) 
	end if 
	 
	 
	program  = gl_createProgram() 
	fragmentShader = gl_createShader(GL_FRAGMENT_SHADER) 
 
	-- Load and compile the shaders 
	gl_shaderSource(fragmentShader, 1, fragmentShaderCode, NULL) 
	gl_compileShader(fragmentShader) 
	puts(1,gl_getShaderInfoLog(fragmentShader)&"\n") 
	 
	-- Attach the shaders to the program  
	gl_attachShader(program, fragmentShader) 
 
	-- Link and enable the program 
	gl_linkProgram(program) 
	gl_useProgram(program) 
	 
  	lastTime = time() 
end procedure 
 
 
 
   
procedure animate() 
	atom currTime 
	 
	gl_clearColor(0.4, 0.4, 0.5, 1.0) 
	gl_clear(GL_COLOR_BUFFER_BIT) 
 
	-- Enable the shaders 
	gl_useProgram(program) 
 
	gl_pushMatrix() 
	 
	gl_begin(GL_QUADS) 
		gl_vertex2f({-1.0, -1.0}) 
 
		gl_vertex2f({1.0, -1.0}) 
 
		gl_vertex2f({1.0, 1.0}) 
 
		gl_vertex2f({-1.0, 1.0}) 
	gl_end() 
 
	gl_popMatrix() 
 
	gl_flush() 
 
	-- Disable the shaders 
	gl_useProgram(NULL) 
	 
	if ewSwapBuffers(glhDC) then 
	end if 
 
	currTime = time() 
	theta += ((currTime-lastTime)/0.01667)*0.02 
	lastTime = currTime 
end procedure 
 
 
 
procedure key(integer keycode,integer x,integer y) 
 	if keycode = VK_ESCAPE then 
  		ewPostQuitMessage(0) 
  	end if 
end procedure 
 
 
procedure reshape(integer w,integer h) 
	gl_viewport(0,0,w,h) 
end procedure 
 
 
procedure clean_up() 
	gl_deleteShader(fragmentShader) 
	gl_deleteProgram(program) 
end procedure 
 
 
 
-- Tell EuGL to use double-buffering 
euglInitDisplayMode(EUGL_DOUBLE)		 
 
euglInitFunc(routine_id("init")) 
euglExitFunc(routine_id("clean_up")) 
euglDisplayFunc(routine_id("animate")) 
euglReshapeFunc(routine_id("reshape")) 
euglKeyboardFunc(routine_id("key")) 
 
 
EuGLMain(EUGL_HANDLE_EVENTS,"Mandelbrot shader",640,480)
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu