public void onSurfaceCreated(GL10 gl, EGLConfig config) { gl.glDisable(GL10.GL_DITHER); gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); gl.glEnable(GL10.GL_CULL_FACE); gl.glCullFace(GL10.GL_BACK); gl.glShadeModel(GL10.GL_SMOOTH); gl.glEnable(GL10.GL_DEPTH_TEST); initGeometry(gl); initLighting(gl); int resid; resid = R.drawable.gimpsun3; m_FlareSource = CT.createTexture(gl, myAppcontext, true, resid); m_LensFlare.createFlares(gl, myAppcontext); }
public void execute(GL10 gl) { float[] paleYellow = {1.0f, 1.0f, 0.3f, 1.0f}; float[] white = {1.0f, 1.0f, 1.0f, 1.0f}; float[] black = {0.0f, 0.0f, 0.0f, 0.0f}; float[] sunPos = {0.0f, 0.0f, 0.0f, 1.0f}; float sunWidth = 0.0f; float sunScreenLoc[] = new float[4]; // xyz and radius float earthScreenLoc[] = new float[4]; // xyz and radius gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glShadeModel(GL10.GL_SMOOTH); gl.glEnable(GL10.GL_LIGHTING); gl.glEnable(GL10.GL_BLEND); gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); gl.glPushMatrix(); gl.glTranslatef( -m_Eyeposition[X_VALUE], -m_Eyeposition[Y_VALUE], -m_Eyeposition[Z_VALUE]); // 1 gl.glLightfv(SS_SUNLIGHT, GL10.GL_POSITION, makeFloatBuffer(sunPos)); gl.glEnable(SS_SUNLIGHT); gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_EMISSION, makeFloatBuffer(paleYellow)); gl.glDisable(GL10.GL_DEPTH_TEST); executePlanet(m_Sun, gl, false, sunScreenLoc); gl.glEnable(GL10.GL_DEPTH_TEST); gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_EMISSION, makeFloatBuffer(black)); gl.glPopMatrix(); if ((m_LensFlare != null) && (sunScreenLoc[Z_INDEX] > 0.0f)) { CGPoint centerRelative = new CGPoint(); CGSize windowSize = new CGSize(); float sunsBodyWidth = 44.0f; // about the width of the sun's bady within the glare in the bitmap, in pixels. float cx, cy; float aspectRatio; float scale = 0f; DisplayMetrics display = myAppcontext.getResources().getDisplayMetrics(); windowSize.width = display.widthPixels; windowSize.height = display.heightPixels; cx = windowSize.width / 2.0f; cy = windowSize.height / 2.0f; aspectRatio = cx / cy; centerRelative.x = sunScreenLoc[X_INDEX] - cx; centerRelative.y = (cy - sunScreenLoc[Y_INDEX]) / aspectRatio; scale = CT.renderTextureAt( gl, centerRelative.x, centerRelative.y, 0f, windowSize, m_FlareSource, sunScreenLoc[RADIUS_INDEX], 1.0f, 1.0f, 1.0f, 1.0f); sunWidth = scale * windowSize.width * sunsBodyWidth / 256.0f; } gl.glEnable(SS_FILLLIGHT2); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glPushMatrix(); gl.glTranslatef(-m_Eyeposition[X_VALUE], -m_Eyeposition[Y_VALUE], -m_Eyeposition[Z_VALUE]); gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, makeFloatBuffer(white)); gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, makeFloatBuffer(white)); executePlanet(m_Earth, gl, true, earthScreenLoc); gl.glPopMatrix(); if ((m_LensFlare != null) && (sunScreenLoc[Z_INDEX] > 0)) { float scale = 1.0f; float delX = origwidth / 2.0f - sunScreenLoc[X_INDEX]; float delY = origheight / 2.0f - sunScreenLoc[Y_INDEX]; float grazeDist = earthScreenLoc[RADIUS_INDEX] + sunWidth; float percentVisible = 1.0f; float vanishDist = earthScreenLoc[RADIUS_INDEX] - sunWidth; float distanceBetweenBodies = (float) sqrt(delX * delX + delY * delY); if ((distanceBetweenBodies > vanishDist) && (distanceBetweenBodies < grazeDist)) { percentVisible = (float) ((distanceBetweenBodies - vanishDist) / sunWidth); if (percentVisible > 1.0) percentVisible = 1.0f; else if (percentVisible < 0.3) percentVisible = .5f; } else if (distanceBetweenBodies > grazeDist) { percentVisible = 1.0f; } else { percentVisible = 0.0f; } scale = STANDARD_FOV / m_FieldOfView; CGPoint source = new CGPoint(); source.x = sunScreenLoc[X_INDEX]; source.y = sunScreenLoc[Y_INDEX]; CGSize winsize = new CGSize(); winsize.width = origwidth; winsize.height = origheight; if (percentVisible > 0.0) { m_LensFlare.execute(gl, winsize, source, scale, percentVisible); } } }
/** * This method encodes name/value pairs and files into a byte array using the multipart/form-data * encoding. The boundary is returned as part of <var>ct_hdr</var>. <br> * Example: * * <PRE> * NVPair[] opts = { new NVPair("option", "doit") }; * NVPair[] file = { new NVPair("comment", "comment.txt") }; * NVPair[] hdrs = new NVPair[1]; * byte[] data = Codecs.mpFormDataEncode(opts, file, hdrs); * con.Post("/cgi-bin/handle-it", data, hdrs); * </PRE> * * <VAR>data</VAR> will look something like the following: * * <PRE> * -----------------------------114975832116442893661388290519 * Content-Disposition: form-data; name="option" * * doit * -----------------------------114975832116442893661388290519 * Content-Disposition: form-data; name="comment"; filename="comment.txt" * Content-Type: text/plain * * Gnus and Gnats are not Gnomes. * -----------------------------114975832116442893661388290519-- * </PRE> * * where the "Gnus and Gnats ..." is the contents of the file <VAR>comment.txt</VAR> in the * current directory. * * <p>If no elements are found in the parameters then a zero-length byte[] is returned and the * content-type is set to <var>application/octet-string</var> (because a multipart must always * have at least one part. * * <p>For files an attempt is made to discover the content-type, and if found a Content-Type * header will be added to that part. The content type is retrieved using * java.net.URLConnection.guessContentTypeFromName() - see java.net.URLConnection.setFileNameMap() * for how to modify that map. Note that under JDK 1.1 by default the map seems to be empty. If * you experience troubles getting the server to accept the data then make sure the fileNameMap is * returning a content-type for each file (this may mean you'll have to set your own). * * @param opts the simple form-data to encode (may be null); for each NVPair the name refers to * the 'name' attribute to be used in the header of the part, and the value is contents of the * part. null elements in the array are ingored. * @param files the files to encode (may be null); for each NVPair the name refers to the 'name' * attribute to be used in the header of the part, and the value is the actual filename (the * file will be read and it's contents put in the body of that part). null elements in the * array are ingored. * @param ct_hdr this returns a new NVPair in the 0'th element which contains name = * "Content-Type", value = "multipart/form-data; boundary=..." (the reason this parameter is * an array is because a) that's the only way to simulate pass-by-reference and b) you need an * array for the headers parameter to the Post() or Put() anyway). The exception to this is * that if no opts or files are given the type is set to "application/octet-stream" instead. * @param mangler the filename mangler, or null if no mangling is to be done. This allows you to * change the name used in the <var>filename</var> attribute of the Content-Disposition * header. Note: the mangler will be invoked twice for each filename. * @return an encoded byte array containing all the opts and files. * @exception IOException If any file operation fails. */ public static final byte[] mpFormDataEncode( NVPair[] opts, NVPair[] files, NVPair[] ct_hdr, FilenameMangler mangler) throws IOException { byte[] boundary = Boundary.getBytes("8859_1"), cont_disp = ContDisp.getBytes("8859_1"), cont_type = ContType.getBytes("8859_1"), filename = FileName.getBytes("8859_1"); int len = 0, hdr_len = boundary.length + cont_disp.length + 1 + 2 + 2; // \r\n -- bnd \r\n C-D: ..; n=".." \r\n \r\n if (opts == null) opts = dummy; if (files == null) files = dummy; // Calculate the length of the data for (int idx = 0; idx < opts.length; idx++) { if (opts[idx] == null) continue; len += hdr_len + opts[idx].getName().length() + opts[idx].getValue().length(); } for (int idx = 0; idx < files.length; idx++) { if (files[idx] == null) continue; File file = new File(files[idx].getValue()); String fname = file.getName(); if (mangler != null) fname = mangler.mangleFilename(fname, files[idx].getName()); if (fname != null) { len += hdr_len + files[idx].getName().length() + filename.length; len += fname.length() + file.length(); String ct = CT.getContentType(file.getName()); if (ct != null) len += cont_type.length + ct.length(); } } if (len == 0) { ct_hdr[0] = new NVPair("Content-Type", "application/octet-stream"); return new byte[0]; } len -= 2; // first CR LF is not written len += boundary.length + 2 + 2; // \r\n -- bnd -- \r\n // Now fill array byte[] res = new byte[len]; int pos = 0; NewBound: for (int new_c = 0x30303030; new_c != 0x7A7A7A7A; new_c++) { pos = 0; // modify boundary in hopes that it will be unique while (!BoundChar.get(new_c & 0xff)) new_c += 0x00000001; while (!BoundChar.get(new_c >> 8 & 0xff)) new_c += 0x00000100; while (!BoundChar.get(new_c >> 16 & 0xff)) new_c += 0x00010000; while (!BoundChar.get(new_c >> 24 & 0xff)) new_c += 0x01000000; boundary[40] = (byte) (new_c & 0xff); boundary[42] = (byte) (new_c >> 8 & 0xff); boundary[44] = (byte) (new_c >> 16 & 0xff); boundary[46] = (byte) (new_c >> 24 & 0xff); int off = 2; int[] bnd_cmp = Util.compile_search(boundary); for (int idx = 0; idx < opts.length; idx++) { if (opts[idx] == null) continue; System.arraycopy(boundary, off, res, pos, boundary.length - off); pos += boundary.length - off; off = 0; int start = pos; System.arraycopy(cont_disp, 0, res, pos, cont_disp.length); pos += cont_disp.length; int nlen = opts[idx].getName().length(); System.arraycopy(opts[idx].getName().getBytes("8859_1"), 0, res, pos, nlen); pos += nlen; res[pos++] = (byte) '"'; res[pos++] = (byte) '\r'; res[pos++] = (byte) '\n'; res[pos++] = (byte) '\r'; res[pos++] = (byte) '\n'; int vlen = opts[idx].getValue().length(); System.arraycopy(opts[idx].getValue().getBytes("8859_1"), 0, res, pos, vlen); pos += vlen; if ((pos - start) >= boundary.length && Util.findStr(boundary, bnd_cmp, res, start, pos) != -1) continue NewBound; } for (int idx = 0; idx < files.length; idx++) { if (files[idx] == null) continue; File file = new File(files[idx].getValue()); String fname = file.getName(); if (mangler != null) fname = mangler.mangleFilename(fname, files[idx].getName()); if (fname == null) continue; System.arraycopy(boundary, off, res, pos, boundary.length - off); pos += boundary.length - off; off = 0; int start = pos; System.arraycopy(cont_disp, 0, res, pos, cont_disp.length); pos += cont_disp.length; int nlen = files[idx].getName().length(); System.arraycopy(files[idx].getName().getBytes("8859_1"), 0, res, pos, nlen); pos += nlen; System.arraycopy(filename, 0, res, pos, filename.length); pos += filename.length; nlen = fname.length(); System.arraycopy(fname.getBytes("8859_1"), 0, res, pos, nlen); pos += nlen; res[pos++] = (byte) '"'; String ct = CT.getContentType(file.getName()); if (ct != null) { System.arraycopy(cont_type, 0, res, pos, cont_type.length); pos += cont_type.length; System.arraycopy(ct.getBytes("8859_1"), 0, res, pos, ct.length()); pos += ct.length(); } res[pos++] = (byte) '\r'; res[pos++] = (byte) '\n'; res[pos++] = (byte) '\r'; res[pos++] = (byte) '\n'; nlen = (int) file.length(); FileInputStream fin = new FileInputStream(file); while (nlen > 0) { int got = fin.read(res, pos, nlen); nlen -= got; pos += got; } fin.close(); if ((pos - start) >= boundary.length && Util.findStr(boundary, bnd_cmp, res, start, pos) != -1) continue NewBound; } break NewBound; } System.arraycopy(boundary, 0, res, pos, boundary.length); pos += boundary.length; res[pos++] = (byte) '-'; res[pos++] = (byte) '-'; res[pos++] = (byte) '\r'; res[pos++] = (byte) '\n'; if (pos != len) throw new Error("Calculated " + len + " bytes but wrote " + pos + " bytes!"); /* the boundary parameter should be quoted (rfc-2046, section 5.1.1) * but too many script authors are not capable of reading specs... * So, I give up and don't quote it. */ ct_hdr[0] = new NVPair( "Content-Type", "multipart/form-data; boundary=" + new String(boundary, 4, boundary.length - 4, "8859_1")); return res; }