import java.awt.Color; import java.awt.Toolkit; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import javax.media.opengl.GL; import fullscreen.FullScreen; import processing.core.PApplet; import processing.opengl.*; public class Fountain extends PApplet { // constants private static final int dim = 200; private static final boolean useFullScreen = false; // variable settings public static float gravity = .6f; public static float spread = 4f; private FullScreen fs; private int stroke_color = 0; // local openGL obj GL gl; float degreesy, degreesx; private double zAxis; public void setup() { size(dim, dim, OPENGL); colorMode(RGB); stroke(stroke_color); background(255); frameRate(30); // OPTIONAL FULLSCREEN if (useFullScreen) { fs = new FullScreen(this); fs.enter(); } // ------------------------------- // OPENGL RELATED // ------------------------------- // init opengl access gl = ((PGraphicsOpenGL) g).gl; gl.glLoadIdentity(); // define rotation agle degreesy = 1; // push projection matrix gl.glMatrixMode(GL.GL_PROJECTION); gl.glFrustum(-1, 1, -1, 1, 1, 1000); // push modelview matrix gl.glMatrixMode(GL.GL_MODELVIEW); zAxis = 175; } /** * draws something on screen/(default)imagebuffer */ public void draw() { fill(255); background(255); // drawMesh(); // drawRectangles(); cube(); degreesy = -((mouseX - (width / 2f)) ); degreesx = -((mouseY - (height / 2f)) ); loadPixels(); { if (mousePressed) { try { boundaryFill(mouseX, mouseY, Color.RED.getRGB(), Color.BLACK.getRGB()); //scanlineFill(Color.RED.getRGB(), Color.BLACK.getRGB()) } catch (StackOverflowError e) { } } } updatePixels(); } // -------------------------- // primitive creation // -------------------------- private void centerpoint() { int pos = (int) ((100) * width + (100)); pixels[pos] = Color.BLUE.getRGB(); } private void cube() { /* int shaderHandle = createShader("frag.c"); int myShader = createShaderProgram(shaderHandle); */ gl.glLoadIdentity(); // OPENGL stack gl.glPushMatrix(); { //gl.glUseProgram(myShader); gl.glTranslated(0, 0, -250); gl.glRotated(-degreesx, 1, 0, 0); gl.glRotated(-degreesy, 0, 1, 0); gl.glTranslated(width / 2, height / 2, zAxis); // PROCESSING stack pushMatrix(); { stroke(0); strokeWeight(2f); box(51.0f); } popMatrix(); } gl.glPopMatrix(); } void drawMesh() { int stepy = height / 10; int stepx = width / 10; int oldx = 0, oldy = height - 1; for (int i = 0; i < height; i += stepy) { line(oldx, height - 1, width - 1, oldy); oldx += stepx; oldy -= stepy; } } void drawRectangles() { pushMatrix(); // translate(0, height/2, 0); rotate(toAngle(degreesy), 1, 0, 0); int rDim = 15; int step = rDim * 2; int posy = (height / 2) - rDim / 2; for (int i = 0; i < width; i += step) { rect(i, 0, rDim, rDim); } popMatrix(); } // -------------------------- // Shader Creation/Management // -------------------------- /** * Creates a new Vertex Shader with given String filename as Shader source * Returns a handle to the shader object. * @param shader source file name * @return int : shader handle */ /* int createShader(String fname) { // create a new shader object int Shader = gl.glCreateShader(GL.GL_VERTEX_SHADER); // get shader source eg read from file String shaderSource = joinString(getShaderSource(fname), "\n"); // assert integrity if (shaderSource == null) return 0; // load shader source gl.glShaderSource(Shader, 1,new String[]{shaderSource}, (int[]) null, 0); gl.glCompileShader(Shader); return Shader; } int createShaderProgram(int... shader) { int shaderProg = gl.glCreateProgram(); for (int i = 0; i < shader.length; i++) { gl.glAttachShader(shaderProg, shader[i]); gl.glLinkProgram(shaderProg); } return shaderProg; } private String[] getShaderSource(String shaderFile) { try { File file = new File(shaderFile); String[] _str = new String[(int)file.length()]; BufferedReader reader = new BufferedReader(new FileReader(shaderFile)); int n = -1; while(reader.ready()) { ++n; _str[n] = reader.readLine(); } reader.close(); return new String[]{_str.toString()}; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } static String joinString(String[] shaderSource, String delim) { String _str = ""; for (int i = 0; i < shaderSource.length; i++) { if(i != 0) { _str += delim; } _str += shaderSource[i]; } return _str; } */ // -------------------------- // key management // -------------------------- public void keyPressed() { if (key == '+') { zAxis += 1; } else if (key == '-') { zAxis -= 1; } else if (key == 'q') { spread += .5; } else if (key == 'e') { if (spread > 0) spread -= .5; } System.out.println(zAxis); } // -------------------------- // region filling // -------------------------- void boundaryFill(int x, int y, int fill, int bound) { // define position int pos = y * width + x; // define sample container int _cclr = 0; // check for correct size if (pos < (pixels.length - 1) && pos > 0) { // load a sample _cclr = pixels[pos]; // System.out.println(Integer.toHexString(_cclr)); // check if sample is already filled or is a bound if (_cclr != fill && _cclr != bound) { pixels[pos] = fill; boundaryFill(x + 1, y, fill, bound); boundaryFill(x, y + 1, fill, bound); boundaryFill(x - 1, y, fill, bound); boundaryFill(x, y - 1, fill, bound); } } } void scanlineFill(int fill, int bound) { boolean fillHere = false; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int pos = y * width + x; // check for bounds if (pixels[pos] != fill) { if (pixels[pos] == bound) { fillHere = false; } else { fillHere = true; } if (fillHere) { pixels[pos] = fill; } } } } } // -------------------------- // math convenience functions // -------------------------- private double toAngle(double i) { return (double) i / 180.0 * Math.PI; } private float toAngle(float i) { return (float) (toAngle((double) i)); } }