public void onSurfaceChanged(GL10 gl, int width, int height) { // TODO handle landscape view gl.glViewport(0, 0, width, height); aspectRatio = (float) width / (float) height; setupDraw2D(gl); square = new float[] { -100f * aspectRatio, -100.0f, -1f, 100f * aspectRatio, -100.0f, -1f, -100f * aspectRatio, 100.0f, -1f, 100f * aspectRatio, 100.0f, -1f }; squareBuffer = makeFloatBuffer(square); markerInfo.setScreenSize(width, height); screenHeight = height; screenWidth = width; }
public void onSurfaceCreated(GL10 gl, EGLConfig config) { gl.glClearColor(0, 0, 0, 0); gl.glClearDepthf(1.0f); // enable textures: gl.glEnable(GL10.GL_TEXTURE_2D); int[] textureNames = new int[1]; // generate texture names: gl.glGenTextures(1, textureNames, 0); textureName = textureNames[0]; textureBuffer = makeFloatBuffer(textureCoords); // register unchaught exception handler // TODO Roger's change // Thread.currentThread().setUncaughtExceptionHandler(activity); markerInfo.initGL(gl); if (customRenderer != null) customRenderer.initGL(gl); }
/* (non-Javadoc) * @see android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.khronos.opengles.GL10) */ public final void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); if (DEBUG) gl = (GL10) GLDebugHelper.wrap(gl, GLDebugHelper.CONFIG_CHECK_GL_ERROR, log); setupDraw2D(gl); gl.glDisable(GL10.GL_DEPTH_TEST); gl.glEnable(GL10.GL_TEXTURE_2D); gl.glDisable(GL10.GL_LIGHTING); gl.glBindTexture(GL10.GL_TEXTURE_2D, textureName); // load new preview frame as a texture, if needed if (frameEnqueued) { frameLock.lock(); if (!isTextureInitialized) { initializeTexture(gl); } else { // just update the image // can we just update a portion(non power of two)?...seems to work gl.glTexSubImage2D( GL10.GL_TEXTURE_2D, 0, 0, 0, previewFrameWidth, previewFrameHeight, mode, GL10.GL_UNSIGNED_BYTE, frameData); } frameLock.unlock(); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); frameEnqueued = false; } gl.glColor4f(1, 1, 1, 1f); // draw camera preview frame: gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, squareBuffer); // draw camera square gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); if (customRenderer != null) customRenderer.setupEnv(gl); else { gl.glEnable(GL10.GL_LIGHTING); gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, ambientLightBuffer); gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, diffuseLightBuffer); gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, specularLightBuffer); gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPositionBuffer); gl.glEnable(GL10.GL_LIGHT0); } markerInfo.draw(gl); if (customRenderer != null) customRenderer.draw(gl); // take a screenshot, if desired if (takeScreenshot) { // http://www.anddev.org/how_to_get_opengl_screenshot__useful_programing_hint-t829.html takeScreenshot = false; int[] tmp = new int[screenHeight * screenWidth]; int[] screenshot = new int[screenHeight * screenWidth]; Buffer screenshotBuffer = IntBuffer.wrap(tmp); screenshotBuffer.position(0); gl.glReadPixels( 0, 0, screenWidth, screenHeight, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, screenshotBuffer); for (int i = 0; i < screenHeight; i++) { // remember, that OpenGL bitmap is incompatible with Android bitmap // and so, some correction need. for (int j = 0; j < screenWidth; j++) { int pix = tmp[i * screenWidth + j]; int pb = (pix >> 16) & 0xff; int pr = (pix << 16) & 0x00ff0000; int pix1 = (pix & 0xff00ff00) | pr | pb; screenshot[(screenHeight - i - 1) * screenWidth + j] = pix1; } } this.screenshot = Bitmap.createBitmap(screenshot, screenWidth, screenHeight, Config.RGB_565); screenshotTaken = true; // wake up the waiting method synchronized (screenshotMonitor) { screenshotMonitor.notifyAll(); } } }