diff --git a/app/src/main/java/com/google/ar/core/examples/java/common/framework/RWT/RWTRenderer.java b/app/src/main/java/com/google/ar/core/examples/java/common/framework/RWT/RWTRenderer.java index c6890bc..3eb9d61 100644 --- a/app/src/main/java/com/google/ar/core/examples/java/common/framework/RWT/RWTRenderer.java +++ b/app/src/main/java/com/google/ar/core/examples/java/common/framework/RWT/RWTRenderer.java @@ -64,6 +64,7 @@ @Override public void onSurfaceChanged(GL10 gl, int width, int height) { +// GLES20.glViewport(0, 0, width, height); // if (gc3D == null) { // gc3D = new GraphicsContext3D(gl); // viewer.setGraphicsContext3D(gc3D); @@ -77,7 +78,8 @@ @Override public void onDrawFrame(GL10 gl) { - GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); +// GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); + gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); if(session == null) { return; @@ -88,8 +90,6 @@ Frame frame = session.update(); Camera camera = frame.getCamera(); - backgroundRenderer.draw(frame); - if (viewer == null) { viewer = new Viewer3D(camera); } @@ -102,10 +102,12 @@ } viewer.onDrawFrame(); + backgroundRenderer.draw(gl, frame); + // 3Dモデルのレンダリング -// gc3D.pushMatrix(); + gc3D.pushMatrix(); universe.render(viewer); -// gc3D.popMatrix(); + gc3D.popMatrix(); helloArActivity.onDrawFrame(gl, frame, camera); } catch (CameraNotAvailableException e) { e.printStackTrace(); diff --git a/app/src/main/java/com/google/ar/core/examples/java/common/framework/view3D/Viewer3D.java b/app/src/main/java/com/google/ar/core/examples/java/common/framework/view3D/Viewer3D.java index 0fc7f64..47b9558 100644 --- a/app/src/main/java/com/google/ar/core/examples/java/common/framework/view3D/Viewer3D.java +++ b/app/src/main/java/com/google/ar/core/examples/java/common/framework/view3D/Viewer3D.java @@ -37,15 +37,25 @@ @Override public void surfaceChanged(int width, int height) { -// gc3D.update(width, height, (float)camera.getFieldOfView(), (float)camera.getFrontClipDistance(), (float)camera.getBackClipDistance(), camera.isParallel()); + float focalLength = camera.getImageIntrinsics().getFocalLength()[0]; +// gc3D.update(width, height, (float)camera.getFieldOfView(), (float)camera.getFrontClipDistance(), (float)camera.getBackClipDistance(), false); + gc3D.update(width, height, (float)Math.atan(width/2.0f/focalLength) * 2.0f, 0.1f, 100.0f, false); } + @Override public void onDrawFrame() { -// Position3D eye = camera.getViewPoint(); -// Position3D center = eye.clone().add(camera.getViewLine()); -// Vector3d up = camera.getViewUp(); -// gc3D.update((float)camera.getFieldOfView(), (float)camera.getFrontClipDistance(), (float)camera.getBackClipDistance(), eye, center, up, camera.isParallel()); + float viewPoint[] = camera.getPose().getTranslation(); + float viewLine[] = camera.getPose().getZAxis(); + float viewUp[] = camera.getPose().getYAxis(); + int width = (camera.getImageIntrinsics().getImageDimensions()[0]); + int height = (camera.getImageIntrinsics().getImageDimensions()[1]); + float focalLength = camera.getImageIntrinsics().getFocalLength()[0]; + Position3D eye = new Position3D(viewPoint[0], viewPoint[1], viewPoint[2]); + Position3D center = eye.clone().add(new Vector3d(-viewLine[0], -viewLine[1], -viewLine[2])); + Vector3d up = new Vector3d(viewUp[0], viewUp[1], viewUp[2]); +// gc3D.update(width, height, (float)Math.atan((float)width/2.0f/focalLength) * 2.0f, 0.1f, 100.0f, eye, center, up, false); + } @Override diff --git a/app/src/main/java/com/google/ar/core/examples/java/common/java3d/GraphicsContext3D.java b/app/src/main/java/com/google/ar/core/examples/java/common/java3d/GraphicsContext3D.java index 572f361..28e2377 100644 --- a/app/src/main/java/com/google/ar/core/examples/java/common/java3d/GraphicsContext3D.java +++ b/app/src/main/java/com/google/ar/core/examples/java/common/java3d/GraphicsContext3D.java @@ -1,5 +1,6 @@ package com.google.ar.core.examples.java.common.java3d; +import android.opengl.GLES20; import android.opengl.GLU; import android.opengl.GLUtils; @@ -58,7 +59,8 @@ aspect = (float)width / (float)height; } // ビューポートの設定 - gl.glViewport(0, 0, width, height); +// gl.glViewport(0, 0, width, height); + GLES20.glViewport(0, 0, width, height); // カメラの設定 gl.glMatrixMode(GL10.GL_PROJECTION); // 射影変換 @@ -80,10 +82,14 @@ } } - public void update(float fovx, float zNear, float zFar, Position3D eye, Position3D center, Vector3d up, boolean fParallel) { + public void update(int width, int height, float fovx, float zNear, float zFar, Position3D eye, Position3D center, Vector3d up, boolean fParallel) { // 表示画面とデプスバッファのクリア gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); - + + // ビューポートの設定 + GLES20.glViewport(0, 0, width, height); + aspect = (float)width / (float)height; + // カメラの設定 gl.glMatrixMode(GL10.GL_PROJECTION); // 射影変換 gl.glLoadIdentity(); // 座標の初期化 diff --git a/app/src/main/java/com/google/ar/core/examples/java/common/rendering/BackgroundRenderer.java b/app/src/main/java/com/google/ar/core/examples/java/common/rendering/BackgroundRenderer.java index d0070af..f844c07 100644 --- a/app/src/main/java/com/google/ar/core/examples/java/common/rendering/BackgroundRenderer.java +++ b/app/src/main/java/com/google/ar/core/examples/java/common/rendering/BackgroundRenderer.java @@ -12,19 +12,285 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +//package com.google.ar.core.examples.java.common.rendering; +// +//import android.content.Context; +//import android.opengl.GLES10; +//import android.opengl.GLES11Ext; +//import android.opengl.GLES20; +//import android.opengl.GLSurfaceView; +//import android.support.annotation.NonNull; +//import com.google.ar.core.Coordinates2d; +//import com.google.ar.core.Frame; +//import java.io.IOException; +//import java.nio.ByteBuffer; +//import java.nio.ByteOrder; +//import java.nio.FloatBuffer; +// +//import javax.microedition.khronos.opengles.GL; +//import javax.microedition.khronos.opengles.GL10; +//import javax.microedition.khronos.opengles.GL11; +//import javax.microedition.khronos.opengles.GL11Ext; +// +///** +// * This class renders the AR background from camera feed. It creates and hosts the texture given to +// * ARCore to be filled with the camera image. +// */ +//public class BackgroundRenderer { +// private static final String TAG = BackgroundRenderer.class.getSimpleName(); +// +// // Shader names. +// private static final String VERTEX_SHADER_NAME = "shaders/screenquad.vert"; +// private static final String FRAGMENT_SHADER_NAME = "shaders/screenquad.frag"; +// +// private static final int COORDS_PER_VERTEX = 2; +// private static final int TEXCOORDS_PER_VERTEX = 2; +// private static final int FLOAT_SIZE = 4; +// +// private FloatBuffer quadCoords; +// private FloatBuffer quadTexCoords; +// +// private int quadProgram; +// +// private int quadPositionParam; +// private int quadTexCoordParam; +// private int textureId = -1; +// +// public int getTextureId() { +// return textureId; +// } +// +// /** +// * Allocates and initializes OpenGL resources needed by the background renderer. Must be called on +// * the OpenGL thread, typically in {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, +// * EGLConfig)}. +// * +// * @param context Needed to access shader source. +// */ +// public void createOnGlThread(Context context) throws IOException { +// // Generate the background texture. +// int[] textures = new int[1]; +// GLES20.glGenTextures(1, textures, 0); +// textureId = textures[0]; +// int textureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES; +// GLES20.glBindTexture(textureTarget, textureId); +// GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); +// GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); +// GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR); +// GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); +//// +//// int numVertices = 4; +//// if (numVertices != QUAD_COORDS.length / COORDS_PER_VERTEX) { +//// throw new RuntimeException("Unexpected number of vertices in BackgroundRenderer."); +//// } +//// +//// ByteBuffer bbCoords = ByteBuffer.allocateDirect(QUAD_COORDS.length * FLOAT_SIZE); +//// bbCoords.order(ByteOrder.nativeOrder()); +//// quadCoords = bbCoords.asFloatBuffer(); +//// quadCoords.put(QUAD_COORDS); +//// quadCoords.position(0); +//// +//// ByteBuffer bbTexCoordsTransformed = +//// ByteBuffer.allocateDirect(numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE); +//// bbTexCoordsTransformed.order(ByteOrder.nativeOrder()); +//// quadTexCoords = bbTexCoordsTransformed.asFloatBuffer(); +// +//// int vertexShader = +//// ShaderUtil.loadGLShader(TAG, context, GLES20.GL_VERTEX_SHADER, VERTEX_SHADER_NAME); +//// int fragmentShader = +//// ShaderUtil.loadGLShader(TAG, context, GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER_NAME); +//// +//// quadProgram = GLES20.glCreateProgram(); +//// GLES20.glAttachShader(quadProgram, vertexShader); +//// GLES20.glAttachShader(quadProgram, fragmentShader); +//// GLES20.glLinkProgram(quadProgram); +//// GLES20.glUseProgram(quadProgram); +//// +//// ShaderUtil.checkGLError(TAG, "Program creation"); +//// +//// quadPositionParam = GLES20.glGetAttribLocation(quadProgram, "a_Position"); +//// quadTexCoordParam = GLES20.glGetAttribLocation(quadProgram, "a_TexCoord"); +//// +//// ShaderUtil.checkGLError(TAG, "Program parameters"); +// } +// +// /** +// * Draws the AR background image. The image will be drawn such that virtual content rendered with +// * the matrices provided by {@link com.google.ar.core.Camera#getViewMatrix(float[], int)} and +// * {@link com.google.ar.core.Camera#getProjectionMatrix(float[], int, float, float)} will +// * accurately follow static physical objects. This must be called before drawing virtual +// * content. +// * +// * @param frame The current {@code Frame} as returned by {@link Session#update()}. +// */ +// public void draw(GL10 gl, @NonNull Frame frame) { +// // If display rotation changed (also includes view size change), we need to re-query the uv +// // coordinates for the screen rect, as they may have changed as well. +//// if (frame.hasDisplayGeometryChanged()) { +//// frame.transformCoordinates2d( +//// Coordinates2d.OPENGL_NORMALIZED_DEVICE_COORDINATES, +//// quadCoords, +//// Coordinates2d.TEXTURE_NORMALIZED, +//// quadTexCoords); +//// } +//// +//// if (frame.getTimestamp() == 0) { +//// // Suppress rendering if the camera did not produce the first frame yet. This is to avoid +//// // drawing possible leftover data from previous sessions if the texture is reused. +//// return; +//// } +// int w = frame.getCamera().getImageIntrinsics().getImageDimensions()[0]; +// int h = frame.getCamera().getImageIntrinsics().getImageDimensions()[1]; +// draw(gl, w, h); +// } +// +//// /** +//// * Draws the camera image using the currently configured {@link BackgroundRenderer#quadTexCoords} +//// * image texture coordinates. +//// * +//// *

The image will be center cropped if the camera sensor aspect ratio does not match the screen +//// * aspect ratio, which matches the cropping behavior of {@link +//// * Frame#transformCoordinates2d(Coordinates2d, float[], Coordinates2d, float[])}. +//// */ +//// public void draw( +//// int imageWidth, int imageHeight, float screenAspectRatio, int cameraToDisplayRotation) { +//// // Crop the camera image to fit the screen aspect ratio. +//// float imageAspectRatio = (float) imageWidth / imageHeight; +//// float croppedWidth; +//// float croppedHeight; +//// if (screenAspectRatio < imageAspectRatio) { +//// croppedWidth = imageHeight * screenAspectRatio; +//// croppedHeight = imageHeight; +//// } else { +//// croppedWidth = imageWidth; +//// croppedHeight = imageWidth / screenAspectRatio; +//// } +//// +//// float u = (imageWidth - croppedWidth) / imageWidth * 0.5f; +//// float v = (imageHeight - croppedHeight) / imageHeight * 0.5f; +//// +//// float[] texCoordTransformed; +//// switch (cameraToDisplayRotation) { +//// case 90: +//// texCoordTransformed = new float[] {1 - u, 1 - v, u, 1 - v, 1 - u, v, u, v}; +//// break; +//// case 180: +//// texCoordTransformed = new float[] {1 - u, v, 1 - u, 1 - v, u, v, u, 1 - v}; +//// break; +//// case 270: +//// texCoordTransformed = new float[] {u, v, 1 - u, v, u, 1 - v, 1 - u, 1 - v}; +//// break; +//// case 0: +//// texCoordTransformed = new float[] {u, 1 - v, u, v, 1 - u, 1 - v, 1 - u, v}; +//// break; +//// default: +//// throw new IllegalArgumentException("Unhandled rotation: " + cameraToDisplayRotation); +//// } +//// +//// // Write image texture coordinates. +//// quadTexCoords.position(0); +//// quadTexCoords.put(texCoordTransformed); +//// +//// draw(); +//// } +// +// /** +// * Draws the camera background image using the currently configured {@link +// * BackgroundRenderer#quadTexCoords} image texture coordinates. +// */ +// private void draw(GL10 gl, int width, int height) { +// gl.glDisable(GL10.GL_DEPTH_TEST); +//// gl.glColor4x(0x10000, 0x10000, 0x10000, 0x10000); +// gl.glColor4f(0.0f, 0.0f, 0.0f, 0.0f); +// gl.glClear(GL10.GL_COLOR_BUFFER_BIT); +// +// gl.glDisable(GL10.GL_TEXTURE_2D); +// gl.glPushMatrix(); +// gl.glActiveTexture(GLES20.GL_TEXTURE0); +// gl.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId); +// int rect[] = new int[]{0, 0, width, -height}; +// ((GL11)gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, rect, 0); +// ((GL11Ext)gl).glDrawTexiOES(0, -height, 0, width, height); +// gl.glPopMatrix(); +// +// gl.glEnable(GL10.GL_TEXTURE_2D); +// gl.glEnable(GL10.GL_DEPTH_TEST); +//// // Ensure position is rewound before use. +//// quadTexCoords.position(0); +//// +//// // No need to test or write depth, the screen quad has arbitrary depth, and is expected +//// // to be drawn first. +//// GLES20.glDisable(GLES20.GL_DEPTH_TEST); +//// GLES20.glDepthMask(false); +//// +//// GLES20.glActiveTexture(GLES20.GL_TEXTURE0); +//// GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId); +//// +//// GLES20.glUseProgram(quadProgram); +//// +//// // Set the vertex positions. +//// GLES20.glVertexAttribPointer( +//// quadPositionParam, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, quadCoords); +//// +//// // Set the texture coordinates. +//// GLES20.glVertexAttribPointer( +//// quadTexCoordParam, TEXCOORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, quadTexCoords); +//// +//// // Enable vertex arrays +//// GLES20.glEnableVertexAttribArray(quadPositionParam); +//// GLES20.glEnableVertexAttribArray(quadTexCoordParam); +//// +//// GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); +//// +//// // Disable vertex arrays +//// GLES20.glDisableVertexAttribArray(quadPositionParam); +//// GLES20.glDisableVertexAttribArray(quadTexCoordParam); +//// +//// // Restore the depth state for further drawing. +//// GLES20.glDepthMask(true); +//// GLES20.glEnable(GLES20.GL_DEPTH_TEST); +//// +//// ShaderUtil.checkGLError(TAG, "BackgroundRendererDraw"); +// } +// +// private static final float[] QUAD_COORDS = +// new float[] { +// -1.0f, -1.0f, -1.0f, +1.0f, +1.0f, -1.0f, +1.0f, +1.0f, +// }; +//} + +/* + * Copyright 2017 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.google.ar.core.examples.java.common.rendering; -import android.content.Context; -import android.opengl.GLES11Ext; -import android.opengl.GLES20; -import android.opengl.GLSurfaceView; -import android.support.annotation.NonNull; -import com.google.ar.core.Coordinates2d; -import com.google.ar.core.Frame; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.FloatBuffer; + import android.content.Context; + import android.opengl.GLES11; + import android.opengl.GLES11Ext; + import android.opengl.GLES20; + import android.opengl.GLSurfaceView; + import android.support.annotation.NonNull; + import com.google.ar.core.Coordinates2d; + import com.google.ar.core.Frame; + import java.io.IOException; + import java.nio.ByteBuffer; + import java.nio.ByteOrder; + import java.nio.FloatBuffer; + + import javax.microedition.khronos.opengles.GL10; + import javax.microedition.khronos.opengles.GL11; + import javax.microedition.khronos.opengles.GL11Ext; /** * This class renders the AR background from camera feed. It creates and hosts the texture given to @@ -85,27 +351,27 @@ quadCoords.position(0); ByteBuffer bbTexCoordsTransformed = - ByteBuffer.allocateDirect(numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE); + ByteBuffer.allocateDirect(numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE); bbTexCoordsTransformed.order(ByteOrder.nativeOrder()); quadTexCoords = bbTexCoordsTransformed.asFloatBuffer(); - int vertexShader = - ShaderUtil.loadGLShader(TAG, context, GLES20.GL_VERTEX_SHADER, VERTEX_SHADER_NAME); - int fragmentShader = - ShaderUtil.loadGLShader(TAG, context, GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER_NAME); - - quadProgram = GLES20.glCreateProgram(); - GLES20.glAttachShader(quadProgram, vertexShader); - GLES20.glAttachShader(quadProgram, fragmentShader); - GLES20.glLinkProgram(quadProgram); - GLES20.glUseProgram(quadProgram); - - ShaderUtil.checkGLError(TAG, "Program creation"); - - quadPositionParam = GLES20.glGetAttribLocation(quadProgram, "a_Position"); - quadTexCoordParam = GLES20.glGetAttribLocation(quadProgram, "a_TexCoord"); - - ShaderUtil.checkGLError(TAG, "Program parameters"); +// int vertexShader = +// ShaderUtil.loadGLShader(TAG, context, GLES20.GL_VERTEX_SHADER, VERTEX_SHADER_NAME); +// int fragmentShader = +// ShaderUtil.loadGLShader(TAG, context, GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER_NAME); +// +// quadProgram = GLES20.glCreateProgram(); +// GLES20.glAttachShader(quadProgram, vertexShader); +// GLES20.glAttachShader(quadProgram, fragmentShader); +// GLES20.glLinkProgram(quadProgram); +// GLES20.glUseProgram(quadProgram); +// +// ShaderUtil.checkGLError(TAG, "Program creation"); +// +// quadPositionParam = GLES20.glGetAttribLocation(quadProgram, "a_Position"); +// quadTexCoordParam = GLES20.glGetAttribLocation(quadProgram, "a_TexCoord"); +// +// ShaderUtil.checkGLError(TAG, "Program parameters"); } /** @@ -117,15 +383,15 @@ * * @param frame The current {@code Frame} as returned by {@link Session#update()}. */ - public void draw(@NonNull Frame frame) { + public void draw(GL10 gl, @NonNull Frame frame) { // If display rotation changed (also includes view size change), we need to re-query the uv // coordinates for the screen rect, as they may have changed as well. if (frame.hasDisplayGeometryChanged()) { frame.transformCoordinates2d( - Coordinates2d.OPENGL_NORMALIZED_DEVICE_COORDINATES, - quadCoords, - Coordinates2d.TEXTURE_NORMALIZED, - quadTexCoords); + Coordinates2d.OPENGL_NORMALIZED_DEVICE_COORDINATES, + quadCoords, + Coordinates2d.TEXTURE_NORMALIZED, + quadTexCoords); } if (frame.getTimestamp() == 0) { @@ -134,104 +400,111 @@ return; } - draw(); + draw(gl); } - /** - * Draws the camera image using the currently configured {@link BackgroundRenderer#quadTexCoords} - * image texture coordinates. - * - *

The image will be center cropped if the camera sensor aspect ratio does not match the screen - * aspect ratio, which matches the cropping behavior of {@link - * Frame#transformCoordinates2d(Coordinates2d, float[], Coordinates2d, float[])}. - */ - public void draw( - int imageWidth, int imageHeight, float screenAspectRatio, int cameraToDisplayRotation) { - // Crop the camera image to fit the screen aspect ratio. - float imageAspectRatio = (float) imageWidth / imageHeight; - float croppedWidth; - float croppedHeight; - if (screenAspectRatio < imageAspectRatio) { - croppedWidth = imageHeight * screenAspectRatio; - croppedHeight = imageHeight; - } else { - croppedWidth = imageWidth; - croppedHeight = imageWidth / screenAspectRatio; - } - - float u = (imageWidth - croppedWidth) / imageWidth * 0.5f; - float v = (imageHeight - croppedHeight) / imageHeight * 0.5f; - - float[] texCoordTransformed; - switch (cameraToDisplayRotation) { - case 90: - texCoordTransformed = new float[] {1 - u, 1 - v, u, 1 - v, 1 - u, v, u, v}; - break; - case 180: - texCoordTransformed = new float[] {1 - u, v, 1 - u, 1 - v, u, v, u, 1 - v}; - break; - case 270: - texCoordTransformed = new float[] {u, v, 1 - u, v, u, 1 - v, 1 - u, 1 - v}; - break; - case 0: - texCoordTransformed = new float[] {u, 1 - v, u, v, 1 - u, 1 - v, 1 - u, v}; - break; - default: - throw new IllegalArgumentException("Unhandled rotation: " + cameraToDisplayRotation); - } - - // Write image texture coordinates. - quadTexCoords.position(0); - quadTexCoords.put(texCoordTransformed); - - draw(); - } + /** + * Draws the camera image using the currently configured {@link BackgroundRenderer#quadTexCoords} + * image texture coordinates. + * + *

The image will be center cropped if the camera sensor aspect ratio does not match the screen + * aspect ratio, which matches the cropping behavior of {@link + * Frame#transformCoordinates2d(Coordinates2d, float[], Coordinates2d, float[])}. + */ +// public void draw( +// int imageWidth, int imageHeight, float screenAspectRatio, int cameraToDisplayRotation) { +// // Crop the camera image to fit the screen aspect ratio. +// float imageAspectRatio = (float) imageWidth / imageHeight; +// float croppedWidth; +// float croppedHeight; +// if (screenAspectRatio < imageAspectRatio) { +// croppedWidth = imageHeight * screenAspectRatio; +// croppedHeight = imageHeight; +// } else { +// croppedWidth = imageWidth; +// croppedHeight = imageWidth / screenAspectRatio; +// } +// +// float u = (imageWidth - croppedWidth) / imageWidth * 0.5f; +// float v = (imageHeight - croppedHeight) / imageHeight * 0.5f; +// +// float[] texCoordTransformed; +// switch (cameraToDisplayRotation) { +// case 90: +// texCoordTransformed = new float[] {1 - u, 1 - v, u, 1 - v, 1 - u, v, u, v}; +// break; +// case 180: +// texCoordTransformed = new float[] {1 - u, v, 1 - u, 1 - v, u, v, u, 1 - v}; +// break; +// case 270: +// texCoordTransformed = new float[] {u, v, 1 - u, v, u, 1 - v, 1 - u, 1 - v}; +// break; +// case 0: +// texCoordTransformed = new float[] {u, 1 - v, u, v, 1 - u, 1 - v, 1 - u, v}; +// break; +// default: +// throw new IllegalArgumentException("Unhandled rotation: " + cameraToDisplayRotation); +// } +// +// // Write image texture coordinates. +// quadTexCoords.position(0); +// quadTexCoords.put(texCoordTransformed); +// +// draw(); +// } /** * Draws the camera background image using the currently configured {@link * BackgroundRenderer#quadTexCoords} image texture coordinates. */ - private void draw() { + private void draw(GL10 gl) { + gl.glClear(GL10.GL_COLOR_BUFFER_BIT); + // Ensure position is rewound before use. + quadCoords.position(0); quadTexCoords.position(0); - // No need to test or write depth, the screen quad has arbitrary depth, and is expected - // to be drawn first. - GLES20.glDisable(GLES20.GL_DEPTH_TEST); - GLES20.glDepthMask(false); +// // No need to test or write depth, the screen quad has arbitrary depth, and is expected +// // to be drawn first. + gl.glDisable(GLES20.GL_DEPTH_TEST); + gl.glDepthMask(false); - GLES20.glActiveTexture(GLES20.GL_TEXTURE0); - GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId); + gl.glEnable(GLES11Ext.GL_TEXTURE_EXTERNAL_OES); + //テクスチャ0番をアクティブにする + gl.glActiveTexture(GL10.GL_TEXTURE0); + //テクスチャIDに対応するテクスチャをバインドする + gl.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId); - GLES20.glUseProgram(quadProgram); +// //テクスチャの座標と幅と高さを指定 +// int rect[] = { 0, 640, 480, -640}; +// //テクスチャ画像のどの部分を使うかを指定 +// ((GL11) gl).glTexParameteriv(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GL11Ext.GL_TEXTURE_CROP_RECT_OES, rect, 0); +// //描画 +// ((GL11Ext) gl).glDrawTexiOES( 0, 0, 0, w, h); // Set the vertex positions. - GLES20.glVertexAttribPointer( - quadPositionParam, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, quadCoords); + gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); + gl.glVertexPointer(2, GL10.GL_FLOAT,0, quadCoords); // Set the texture coordinates. - GLES20.glVertexAttribPointer( - quadTexCoordParam, TEXCOORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, quadTexCoords); + gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); + gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, quadTexCoords); - // Enable vertex arrays - GLES20.glEnableVertexAttribArray(quadPositionParam); - GLES20.glEnableVertexAttribArray(quadTexCoordParam); - - GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); + gl.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); // Disable vertex arrays - GLES20.glDisableVertexAttribArray(quadPositionParam); - GLES20.glDisableVertexAttribArray(quadTexCoordParam); + gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); + gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); + + gl.glDisable(GLES11Ext.GL_TEXTURE_EXTERNAL_OES); // Restore the depth state for further drawing. - GLES20.glDepthMask(true); - GLES20.glEnable(GLES20.GL_DEPTH_TEST); - - ShaderUtil.checkGLError(TAG, "BackgroundRendererDraw"); + gl.glDepthMask(true); + gl.glEnable(GLES20.GL_DEPTH_TEST); } private static final float[] QUAD_COORDS = - new float[] { - -1.0f, -1.0f, -1.0f, +1.0f, +1.0f, -1.0f, +1.0f, +1.0f, - }; -} + new float[] { + -1.0f, -1.0f, -1.0f, +1.0f, +1.0f, -1.0f, +1.0f, +1.0f, + }; +} \ No newline at end of file diff --git a/app/src/main/java/com/google/ar/core/examples/java/common/rendering/ObjectRenderer.java b/app/src/main/java/com/google/ar/core/examples/java/common/rendering/ObjectRenderer.java index 9dfc0df..3dff1e0 100644 --- a/app/src/main/java/com/google/ar/core/examples/java/common/rendering/ObjectRenderer.java +++ b/app/src/main/java/com/google/ar/core/examples/java/common/rendering/ObjectRenderer.java @@ -32,6 +32,8 @@ import java.nio.IntBuffer; import java.nio.ShortBuffer; +import javax.microedition.khronos.opengles.GL10; + /** Renders an object loaded from an OBJ file in OpenGL. */ public class ObjectRenderer { private static final String TAG = ObjectRenderer.class.getSimpleName(); diff --git a/app/src/main/java/com/google/ar/core/examples/java/helloar/HelloArActivity.java b/app/src/main/java/com/google/ar/core/examples/java/helloar/HelloArActivity.java index b0f4efd..b9d2c3f 100644 --- a/app/src/main/java/com/google/ar/core/examples/java/helloar/HelloArActivity.java +++ b/app/src/main/java/com/google/ar/core/examples/java/helloar/HelloArActivity.java @@ -121,9 +121,10 @@ // Set up renderer. surfaceView.setPreserveEGLContextOnPause(true); - surfaceView.setEGLContextClientVersion(2); +// surfaceView.setEGLContextClientVersion(2); surfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0); // Alpha used for plane blending. surfaceView.setRenderer(renderer); +// surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); surfaceView.setWillNotDraw(false); @@ -234,19 +235,19 @@ try { // Create the texture and pass it to ARCore session to be filled during update(). backgroundRenderer.createOnGlThread(/*context=*/ this); - planeRenderer.createOnGlThread(/*context=*/ this, "models/trigrid.png"); - pointCloudRenderer.createOnGlThread(/*context=*/ this); - - virtualObject.createOnGlThread(/*context=*/ this, "models/andy.obj", "models/andy.png"); - virtualObject.setMaterialProperties(0.0f, 2.0f, 0.5f, 6.0f); - - virtualObjectShadow.createOnGlThread( - /*context=*/ this, "models/andy_shadow.obj", "models/andy_shadow.png"); - virtualObjectShadow.setBlendMode(BlendMode.Shadow); - virtualObjectShadow.setMaterialProperties(1.0f, 0.0f, 0.0f, 1.0f); - -// renderer.onSurfaceCreated(gl, config); - +// planeRenderer.createOnGlThread(/*context=*/ this, "models/trigrid.png"); +// pointCloudRenderer.createOnGlThread(/*context=*/ this); +// +// virtualObject.createOnGlThread(/*context=*/ this, "models/andy.obj", "models/andy.png"); +// virtualObject.setMaterialProperties(0.0f, 2.0f, 0.5f, 6.0f); +// +// virtualObjectShadow.createOnGlThread( +// /*context=*/ this, "models/andy_shadow.obj", "models/andy_shadow.png"); +// virtualObjectShadow.setBlendMode(BlendMode.Shadow); +// virtualObjectShadow.setMaterialProperties(1.0f, 0.0f, 0.0f, 1.0f); +// +//// renderer.onSurfaceCreated(gl, config); +// } catch (IOException e) { Log.e(TAG, "Failed to read an asset file", e); } @@ -255,7 +256,7 @@ public void onSurfaceChanged(GL10 gl, int width, int height) { displayRotationHelper.onSurfaceChanged(width, height); GLES20.glViewport(0, 0, width, height); - } +} public void onDrawFrame(GL10 gl, Frame frame, Camera camera) { // Clear screen to notify driver it should not load any pixels from previous frame. @@ -296,7 +297,6 @@ // Get projection matrix. float[] projmtx = new float[16]; camera.getProjectionMatrix(projmtx, 0, 0.1f, 100.0f); - // Get camera matrix and draw. float[] viewmtx = new float[16]; camera.getViewMatrix(viewmtx, 0); @@ -307,12 +307,12 @@ final float[] colorCorrectionRgba = new float[4]; frame.getLightEstimate().getColorCorrection(colorCorrectionRgba, 0); - // Visualize tracked points. - // Use try-with-resources to automatically release the point cloud. - try (PointCloud pointCloud = frame.acquirePointCloud()) { - pointCloudRenderer.update(pointCloud); - pointCloudRenderer.draw(viewmtx, projmtx); - } +// // Visualize tracked points. +// // Use try-with-resources to automatically release the point cloud. +// try (PointCloud pointCloud = frame.acquirePointCloud()) { +// pointCloudRenderer.update(pointCloud); +// pointCloudRenderer.draw(viewmtx, projmtx); +// } // No tracking error at this point. If we detected any plane, then hide the // message UI, otherwise show searchingPlane message. @@ -322,27 +322,27 @@ messageSnackbarHelper.showMessage(this, SEARCHING_PLANE_MESSAGE); } - // Visualize planes. - planeRenderer.drawPlanes( - session.getAllTrackables(Plane.class), camera.getDisplayOrientedPose(), projmtx); - - // Visualize anchors created by touch. - float scaleFactor = 1.0f; - for (ColoredAnchor coloredAnchor : anchors) { - if (coloredAnchor.anchor.getTrackingState() != TrackingState.TRACKING) { - continue; - } - // Get the current pose of an Anchor in world space. The Anchor pose is updated - // during calls to session.update() as ARCore refines its estimate of the world. - coloredAnchor.anchor.getPose().toMatrix(anchorMatrix, 0); - - // Update and draw the model and its shadow. - virtualObject.updateModelMatrix(anchorMatrix, scaleFactor); - virtualObjectShadow.updateModelMatrix(anchorMatrix, scaleFactor); - virtualObject.draw(viewmtx, projmtx, colorCorrectionRgba, coloredAnchor.color); - virtualObjectShadow.draw(viewmtx, projmtx, colorCorrectionRgba, coloredAnchor.color); - - } +// // Visualize planes. +// planeRenderer.drawPlanes( +// session.getAllTrackables(Plane.class), camera.getDisplayOrientedPose(), projmtx); +// +// // Visualize anchors created by touch. +// float scaleFactor = 1.0f; +// for (ColoredAnchor coloredAnchor : anchors) { +// if (coloredAnchor.anchor.getTrackingState() != TrackingState.TRACKING) { +// continue; +// } +// // Get the current pose of an Anchor in world space. The Anchor pose is updated +// // during calls to session.update() as ARCore refines its estimate of the world. +// coloredAnchor.anchor.getPose().toMatrix(anchorMatrix, 0); +// +// // Update and draw the model and its shadow. +// virtualObject.updateModelMatrix(anchorMatrix, scaleFactor); +// virtualObjectShadow.updateModelMatrix(anchorMatrix, scaleFactor); +// virtualObject.draw(viewmtx, projmtx, colorCorrectionRgba, coloredAnchor.color); +// virtualObjectShadow.draw(viewmtx, projmtx, colorCorrectionRgba, coloredAnchor.color); +// +// } } catch (Throwable t) { // Avoid crashing the application due to unhandled exceptions.