Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging 1.4 into sceneform-samples #5

Open
wants to merge 3 commits into
base: sceneform-samples
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Google ARCore SDK for Android
=============================
ARCore SDK for Android
======================
Copyright (c) 2017 Google Inc. All rights reserved.

This SDK provides APIs for all of the essential AR features like motion tracking, environmental understanding, and light estimation. With these capabilities you can build entirely new AR experiences or enhance existing apps with AR features.
Expand Down
350 changes: 322 additions & 28 deletions libraries/include/arcore_c_api.h

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions samples/augmented_image_c/.gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Android Studio configuration.
# *.iml
# .idea/
#
# # Gradle configuration.
# .gradle/
# build/
#
# # User configuration.
# local.properties
#
# # OS configurations.
# .DS_Store
*.iml
.idea/

# Gradle configuration.
.gradle/
build/

# User configuration.
local.properties

# OS configurations.
.DS_Store
8 changes: 6 additions & 2 deletions samples/augmented_image_c/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ android {
abiFilters "arm64-v8a", "x86"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled false
Expand All @@ -47,8 +51,8 @@ android {

dependencies {
// ARCore library
implementation 'com.google.ar:core:1.2.1'
natives 'com.google.ar:core:1.2.1'
implementation 'com.google.ar:core:1.4.0'
natives 'com.google.ar:core:1.4.0'

implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:design:27.0.2'
Expand Down
78 changes: 78 additions & 0 deletions samples/augmented_image_c/app/src/main/assets/shaders/object.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2018 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.
*/

precision mediump float;
uniform sampler2D u_Texture;
uniform vec4 u_LightingParameters;
uniform vec4 u_MaterialParameters;
uniform vec4 u_ColorCorrectionParameters;
uniform vec4 u_ColorTintParameters;
varying vec3 v_ViewPosition;
varying vec3 v_ViewNormal;
varying vec2 v_TexCoord;

void main() {
// We support approximate sRGB gamma.
const float kGamma = 0.4545454;
const float kInverseGamma = 2.2;
const float kMiddleGrayGamma = 0.466;

// Unpack lighting and material parameters for better naming.
vec3 viewLightDirection = u_LightingParameters.xyz;
vec3 colorShift = u_ColorCorrectionParameters.rgb;
float averagePixelIntensity = u_ColorCorrectionParameters.a;

float materialAmbient = u_MaterialParameters.x;
float materialDiffuse = u_MaterialParameters.y;
float materialSpecular = u_MaterialParameters.z;
float materialSpecularPower = u_MaterialParameters.w;

// Normalize varying parameters, because they are linearly interpolated in
// the vertex shader.
vec3 viewFragmentDirection = normalize(v_ViewPosition);
vec3 viewNormal = normalize(v_ViewNormal);

// Apply inverse SRGB gamma to the texture before making lighting
// calculations.
// Flip the y-texture coordinate to address the texture from top-left.
vec4 objectColor = texture2D(u_Texture,
vec2(v_TexCoord.x, 1.0 - v_TexCoord.y));
objectColor.rgb += u_ColorTintParameters.rgb;
objectColor.rgb = pow(objectColor.rgb, vec3(kInverseGamma));

// Ambient light is unaffected by the light intensity.
float ambient = materialAmbient;

// Approximate a hemisphere light (not a harsh directional light).
float diffuse = materialDiffuse *
0.5 * (dot(viewNormal, viewLightDirection) + 1.0);

// Compute specular light.
vec3 reflectedLightDirection = reflect(viewLightDirection, viewNormal);
float specularStrength = max(0.0, dot(viewFragmentDirection,
reflectedLightDirection));
float specular = materialSpecular *
pow(specularStrength, materialSpecularPower);

vec3 color = objectColor.rgb * (ambient + diffuse) + specular;

// Apply SRGB gamma before writing the fragment color.
color.rgb = pow(color, vec3(kGamma));
// Apply average pixel intensity and color shift
color *= colorShift * (averagePixelIntensity/kMiddleGrayGamma);
gl_FragColor.rgb = color;
gl_FragColor.a = objectColor.a * u_ColorTintParameters.a;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/*
* Copyright 2017 Google Inc. All Rights Reserved.
* Copyright 2018 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
* 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,
Expand All @@ -15,18 +16,16 @@

uniform mat4 u_ModelView;
uniform mat4 u_ModelViewProjection;

attribute vec4 a_Position;
attribute vec3 a_Normal;
attribute vec2 a_TexCoord;

varying vec3 v_ViewPosition;
varying vec3 v_ViewNormal;
varying vec2 v_TexCoord;

void main() {
v_ViewPosition = (u_ModelView * a_Position).xyz;
v_ViewNormal = normalize((u_ModelView * vec4(a_Normal, 0.0)).xyz);
v_TexCoord = a_TexCoord;
gl_Position = u_ModelViewProjection * a_Position;
v_ViewPosition = (u_ModelView * a_Position).xyz;
v_ViewNormal = normalize((u_ModelView * vec4(a_Normal, 0.0)).xyz);
v_TexCoord = a_TexCoord;
gl_Position = u_ModelViewProjection * a_Position;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ constexpr bool kUseSingleImage = false;
AugmentedImageApplication::AugmentedImageApplication(
AAssetManager* asset_manager)
: asset_manager_(asset_manager) {
LOGI("OnCreate()");
}

AugmentedImageApplication::~AugmentedImageApplication() {
Expand Down Expand Up @@ -158,8 +157,8 @@ AugmentedImageApplication::CreateAugmentedImageDatabase() const {
delete[] grayscale_buffer;
} else {
std::string database_buffer;
util::LoadEntireAssetFile(asset_manager_, "sample_database.imgdb",
&database_buffer);
util::LoadFileFromAssetManager(asset_manager_, "sample_database.imgdb",
&database_buffer);

uint8_t* raw_buffer = reinterpret_cast<uint8_t*>(&database_buffer.front());
const ArStatus status = ArAugmentedImageDatabase_deserialize(
Expand All @@ -174,7 +173,7 @@ AugmentedImageApplication::CreateAugmentedImageDatabase() const {
void AugmentedImageApplication::OnSurfaceCreated() {
LOGI("OnSurfaceCreated()");

background_renderer_.InitializeGlContent();
background_renderer_.InitializeGlContent(asset_manager_);
image_renderer_.InitializeGlContent(asset_manager_);
}

Expand Down
32 changes: 8 additions & 24 deletions samples/augmented_image_c/app/src/main/cpp/background_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,13 @@ const GLfloat kUvs[] = {
0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
};

constexpr char kVertexShader[] = R"(
attribute vec4 vertex;
attribute vec2 textureCoords;
varying vec2 v_textureCoords;
void main() {
v_textureCoords = textureCoords;
gl_Position = vertex;
})";

constexpr char kFragmentShader[] = R"(
#extension GL_OES_EGL_image_external : require
precision mediump float;
uniform samplerExternalOES texture;
varying vec2 v_textureCoords;
void main() {
gl_FragColor = texture2D(texture, v_textureCoords);
})";

constexpr char kVertexShaderFilename[] = "shaders/screenquad.vert";
constexpr char kFragmentShaderFilename[] = "shaders/screenquad.frag";
} // namespace

void BackgroundRenderer::InitializeGlContent() {
shader_program_ = util::CreateProgram(kVertexShader, kFragmentShader);

void BackgroundRenderer::InitializeGlContent(AAssetManager* asset_manager) {
shader_program_ = util::CreateProgram(asset_manager, kVertexShaderFilename,
kFragmentShaderFilename);
if (!shader_program_) {
LOGE("Could not create program.");
}
Expand All @@ -66,9 +50,9 @@ void BackgroundRenderer::InitializeGlContent() {
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

uniform_texture_ = glGetUniformLocation(shader_program_, "texture");
attribute_vertices_ = glGetAttribLocation(shader_program_, "vertex");
attribute_uvs_ = glGetAttribLocation(shader_program_, "textureCoords");
uniform_texture_ = glGetUniformLocation(shader_program_, "sTexture");
attribute_vertices_ = glGetAttribLocation(shader_program_, "a_Position");
attribute_uvs_ = glGetAttribLocation(shader_program_, "a_TexCoord");
}

void BackgroundRenderer::Draw(const ArSession* session, const ArFrame* frame) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <android/asset_manager.h>
#include <cstdlib>

#include "arcore_c_api.h"
Expand All @@ -34,7 +35,7 @@ class BackgroundRenderer {

// Sets up OpenGL state. Must be called on the OpenGL thread and before any
// other methods below.
void InitializeGlContent();
void InitializeGlContent(AAssetManager* asset_manager);

// Draws the background image. This methods must be called for every ArFrame
// returned by ArSession_update() to catch display geometry change events.
Expand Down
94 changes: 4 additions & 90 deletions samples/augmented_image_c/app/src/main/cpp/obj_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,103 +19,17 @@

namespace augmented_image {
namespace {

const glm::vec4 kLightDirection(0.0f, 1.0f, 0.0f, 0.0f);
const float kNoTintColor[4] = {0.0f, 0.0f, 0.0f, 1.0f};

constexpr char kVertexShader[] = R"(
uniform mat4 u_ModelView;
uniform mat4 u_ModelViewProjection;

attribute vec4 a_Position;
attribute vec3 a_Normal;
attribute vec2 a_TexCoord;

varying vec3 v_ViewPosition;
varying vec3 v_ViewNormal;
varying vec2 v_TexCoord;

void main() {
v_ViewPosition = (u_ModelView * a_Position).xyz;
v_ViewNormal = normalize((u_ModelView * vec4(a_Normal, 0.0)).xyz);
v_TexCoord = a_TexCoord;
gl_Position = u_ModelViewProjection * a_Position;
})";

constexpr char kFragmentShader[] = R"(
precision mediump float;

uniform sampler2D u_Texture;

uniform vec4 u_LightingParameters;
uniform vec4 u_MaterialParameters;
uniform vec4 u_ColorCorrectionParameters;
uniform vec4 u_ColorTintParameters;

varying vec3 v_ViewPosition;
varying vec3 v_ViewNormal;
varying vec2 v_TexCoord;

void main() {
// We support approximate sRGB gamma.
const float kGamma = 0.4545454;
const float kInverseGamma = 2.2;
const float kMiddleGrayGamma = 0.466;

// Unpack lighting and material parameters for better naming.
vec3 viewLightDirection = u_LightingParameters.xyz;
vec3 colorShift = u_ColorCorrectionParameters.rgb;
float averagePixelIntensity = u_ColorCorrectionParameters.a;

float materialAmbient = u_MaterialParameters.x;
float materialDiffuse = u_MaterialParameters.y;
float materialSpecular = u_MaterialParameters.z;
float materialSpecularPower = u_MaterialParameters.w;

// Normalize varying parameters, because they are linearly interpolated in
// the vertex shader.
vec3 viewFragmentDirection = normalize(v_ViewPosition);
vec3 viewNormal = normalize(v_ViewNormal);

// Apply inverse SRGB gamma to the texture before making lighting
// calculations.
// Flip the y-texture coordinate to address the texture from top-left.
vec4 objectColor = texture2D(u_Texture,
vec2(v_TexCoord.x, 1.0 - v_TexCoord.y));
objectColor.rgb += u_ColorTintParameters.rgb;
objectColor.rgb = pow(objectColor.rgb, vec3(kInverseGamma));

// Ambient light is unaffected by the light intensity.
float ambient = materialAmbient;

// Approximate a hemisphere light (not a harsh directional light).
float diffuse = materialDiffuse *
0.5 * (dot(viewNormal, viewLightDirection) + 1.0);

// Compute specular light.
vec3 reflectedLightDirection = reflect(viewLightDirection, viewNormal);
float specularStrength = max(0.0, dot(viewFragmentDirection,
reflectedLightDirection));
float specular = materialSpecular *
pow(specularStrength, materialSpecularPower);

vec3 color = objectColor.rgb * (ambient + diffuse) + specular;

// Apply SRGB gamma before writing the fragment color.
color.rgb = pow(color, vec3(kGamma));
// Apply average pixel intensity and color shift
color *= colorShift * (averagePixelIntensity/kMiddleGrayGamma);
gl_FragColor.rgb = color;
gl_FragColor.a = objectColor.a * u_ColorTintParameters.a;
}
)";
constexpr char kVertexShaderFilename[] = "shaders/object.vert";
constexpr char kFragmentShaderFilename[] = "shaders/object.frag";
} // namespace

void ObjRenderer::InitializeGlContent(AAssetManager* asset_manager,
const std::string& obj_file_name,
const std::string& png_file_name) {
shader_program_ = util::CreateProgram(kVertexShader, kFragmentShader);

shader_program_ = util::CreateProgram(asset_manager, kVertexShaderFilename,
kFragmentShaderFilename);
if (!shader_program_) {
LOGE("Could not create program.");
}
Expand Down
Loading