Skip to content

Commit

Permalink
Merge pull request #555 from googlesamples/textured-teapot
Browse files Browse the repository at this point in the history
Adding textured-teapot sample for github issue #314
  • Loading branch information
ggfan committed Aug 17, 2018
2 parents a00d6b1 + ff7f4f7 commit f6f722f
Show file tree
Hide file tree
Showing 32 changed files with 4,119 additions and 1 deletion.
2 changes: 2 additions & 0 deletions teapots/.gitignore
@@ -0,0 +1,2 @@
**/stb

3 changes: 2 additions & 1 deletion teapots/settings.gradle
@@ -1 +1,2 @@
include ':classic-teapot', ':more-teapots', ':choreographer-30fps'
include ':classic-teapot', ':more-teapots',
':choreographer-30fps', ':textured-teapot'
8 changes: 8 additions & 0 deletions teapots/textured-teapot/README.md
@@ -0,0 +1,8 @@
textured-teapot
==============
This one is a simple enhancement with a texture for classic-teapot
- The teapot vertex coordinators are part of the model files
- CPU side of the code for texturing is in TexturedTeapotRender class
- fragment shader simply textures in and blend
- Texture files are under apk's assets/Textures folder(bmp & tga tested)

38 changes: 38 additions & 0 deletions teapots/textured-teapot/build.gradle
@@ -0,0 +1,38 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion = 28

defaultConfig {
applicationId = 'com.sample.texturedteapot'
minSdkVersion 17
targetSdkVersion 26
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a','x86', 'x86_64'
}
externalNativeBuild {
cmake {
arguments '-DANDROID_TOOLCHAIN=clang',
'-DANDROID_STL=c++_static'
}
}
}
buildTypes {
release {
minifyEnabled = false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path 'src/main/cpp/CMakeLists.txt'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
implementation 'com.android.support.constraint:constraint-layout:1.0.1'
}
Binary file added teapots/textured-teapot/screenshot.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions teapots/textured-teapot/src/main/AndroidManifest.xml
@@ -0,0 +1,32 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.texturedteapot"
android:versionCode="1"
android:versionName="1.0" >

<uses-feature android:glEsVersion="0x00020000"></uses-feature>

<application
android:allowBackup="false"
android:fullBackupContent="false"
android:supportsRtl="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="com.sample.texturedteapot.TeapotApplication"
>

<!-- Our activity is the built-in NativeActivity framework class.
This will take care of integrating with our NDK code. -->
<activity android:name="com.sample.texturedteapot.TeapotNativeActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
<!-- Tell NativeActivity the name of our .so -->
<meta-data android:name="android.app.lib_name"
android:value="TexturedTeapotNativeActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
50 changes: 50 additions & 0 deletions teapots/textured-teapot/src/main/assets/Shaders/ShaderPlain.fsh
@@ -0,0 +1,50 @@
//
// Copyright (C) 2018 The Android Open Source Project
//
// 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.
//
// ShaderPlain.fsh
//

#define USE_PHONG (1)

uniform lowp vec3 vMaterialAmbient;
uniform lowp vec4 vMaterialSpecular;

uniform sampler2D samplerObj;

varying mediump vec2 texCoord;
varying lowp vec4 colorDiffuse;

#if USE_PHONG
uniform highp vec3 vLight0;
varying mediump vec3 position;
varying mediump vec3 normal;
#else
varying lowp vec4 colorSpecular;
#endif

void main()
{
#if USE_PHONG
mediump vec3 halfVector = normalize(-vLight0 + position);
mediump float NdotH = max(dot(normalize(normal), halfVector), 0.0);
mediump float fPower = vMaterialSpecular.w;
mediump float specular = pow(NdotH, fPower);

lowp vec4 colorSpecular = vec4( vMaterialSpecular.xyz * specular, 1 );
gl_FragColor = colorDiffuse * texture2D(samplerObj, texCoord) + colorSpecular;
#else
gl_FragColor = colorDiffuse * texture2D(samplerObj, texCoord) + colorSpecular;
#endif
}
68 changes: 68 additions & 0 deletions teapots/textured-teapot/src/main/assets/Shaders/VS_ShaderPlain.vsh
@@ -0,0 +1,68 @@
//
// Copyright (C) 2018 The Android Open Source Project
//
// 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.
//
// ShaderPlain.vsh
//

#define USE_PHONG (1)

attribute highp vec3 myVertex;
attribute highp vec3 myNormal;
attribute mediump vec2 myUV;
attribute mediump vec4 myBone;

varying mediump vec2 texCoord;
varying lowp vec4 colorDiffuse;

#if USE_PHONG
varying mediump vec3 position;
varying mediump vec3 normal;
#else
varying lowp vec4 colorSpecular;
#endif

uniform highp mat4 uMVMatrix;
uniform highp mat4 uPMatrix;

uniform highp vec3 vLight0;

uniform lowp vec4 vMaterialDiffuse;
uniform lowp vec3 vMaterialAmbient;
uniform lowp vec4 vMaterialSpecular;

void main(void)
{
highp vec4 p = vec4(myVertex,1);
gl_Position = uPMatrix * p;

texCoord = myUV;

highp vec3 worldNormal = vec3(mat3(uMVMatrix[0].xyz, uMVMatrix[1].xyz, uMVMatrix[2].xyz) * myNormal);
highp vec3 ecPosition = p.xyz;

colorDiffuse = dot( worldNormal, normalize(-vLight0+ecPosition) ) * vMaterialDiffuse + vec4( vMaterialAmbient, 1 );

#if USE_PHONG
normal = worldNormal;
position = ecPosition;
#else
highp vec3 halfVector = normalize(ecPosition - vLight0);

highp float NdotH = max(-dot(worldNormal, halfVector), 0.0);
float fPower = vMaterialSpecular.w;
highp float specular = min( pow(NdotH, fPower), 1.0);
colorSpecular = vec4( vMaterialSpecular.xyz * specular, 1 );
#endif
}
Binary file not shown.
Binary file not shown.
80 changes: 80 additions & 0 deletions teapots/textured-teapot/src/main/cpp/AssetUtil.cpp
@@ -0,0 +1,80 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* 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.
*
*/
#include <algorithm>
#include "AssetUtil.h"
#include "android_debug.h"


#define IS_LOW_CHAR(c) ((c) >= 'a' && (c) <= 'z')
#define TO_UPPER_CHAR(c) (c + 'A' - 'a')
void StringToUpper(std::string& str) {
for(auto& ch: str) {
if (IS_LOW_CHAR(ch))
ch = TO_UPPER_CHAR(ch);
}
}
bool AssetEnumerateFileType(AAssetManager * assetManager,
const char* type, std::vector<std::string> & files) {

if (!assetManager || !type || !*type)
return false;

std::string fileType(type);
if (fileType[0] != '.') {
fileType.insert(0, 1, '.');
}
StringToUpper(fileType);

AAssetDir * dir = AAssetManager_openDir(assetManager, "");

const char* name;
while ((name = AAssetDir_getNextFileName(dir))) {
std::string assetName(name);
if (assetName.length() <= fileType.length())
continue;
std::string assetType =
assetName.substr(assetName.length() - fileType.length(),
fileType.length());
StringToUpper(assetType);

if (assetType == fileType) {
files.push_back(assetName);
}
}
LOGI("Found %d PNG Files", static_cast<uint32_t>(files.size()));

AAssetDir_close(dir);
return true;
}

bool AssetReadFile(AAssetManager* assetManager,
std::string& assetName, std::vector<uint8_t>& buf) {
if (!assetName.length())
return false;
AAsset* assetDescriptor = AAssetManager_open(assetManager,
assetName.c_str(),
AASSET_MODE_BUFFER);
ASSERT(assetDescriptor, "%s does not exist in %s",
assetName.c_str(), __FUNCTION__);
size_t fileLength = AAsset_getLength(assetDescriptor);

buf.resize(fileLength);
int64_t readSize = AAsset_read(assetDescriptor, buf.data(), buf.size());

AAsset_close(assetDescriptor);
return (readSize == buf.size());
}
29 changes: 29 additions & 0 deletions teapots/textured-teapot/src/main/cpp/AssetUtil.h
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* 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.
*
*/
#ifndef __ASSET__UTIL_H__
#define __ASSET__UTIL_H__

#include <string>
#include <vector>
#include <android/asset_manager.h>

bool AssetEnumerateFileType(AAssetManager * assetManager,
const char* type, std::vector<std::string> & files);
bool AssetReadFile(AAssetManager* assetManager,
std::string& name, std::vector<uint8_t>& buf);

#endif // __ASSET__UTIL_H__
62 changes: 62 additions & 0 deletions teapots/textured-teapot/src/main/cpp/CMakeLists.txt
@@ -0,0 +1,62 @@
#
# Copyright (C) The Android Open Source Project
#
# 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.
#

cmake_minimum_required(VERSION 3.4.1)
project(TexturedTeapotNativeActivity LANGUAGES C CXX)

# set up common compile options
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -fno-exceptions -fno-rtti")

get_filename_component(commonDir ${CMAKE_CURRENT_SOURCE_DIR}/../../../../common ABSOLUTE)
if ((NOT EXISTS ${commonDir}/stb) OR
(NOT EXISTS ${commonDir}/stb/stb_image.h))
execute_process(COMMAND git clone
https://github.com/nothings/stb.git
stb
WORKING_DIRECTORY ${commonDir})
endif()

# build the ndk-helper library into a common directory
# visible to all 3 projects avoiding duplicate builds.
get_filename_component(ndkHelperSrc ${commonDir}/ndk_helper ABSOLUTE)
get_filename_component(ndkHelperBin ${commonDir}/ndkHelperBin ABSOLUTE)
add_subdirectory(${ndkHelperSrc} ${ndkHelperBin})

# now build app's shared lib
add_library(${PROJECT_NAME}
SHARED
TeapotNativeActivity.cpp
TeapotRenderer.cpp
TexturedTeapotRender.cpp
AssetUtil.cpp
)
set_target_properties(${PROJECT_NAME}
PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)
target_include_directories(${PROJECT_NAME} PRIVATE ${commonDir})

target_compile_options(${PROJECT_NAME} PRIVATE -Wno-unused-function)

# Export ANativeActivity_onCreate(),
# Refer to: https://github.com/android-ndk/ndk/issues/381.
set_target_properties(${PROJECT_NAME}
PROPERTIES LINK_FLAGS "-u ANativeActivity_onCreate")

# add lib dependencies
target_link_libraries(${PROJECT_NAME} PRIVATE NdkHelper)

0 comments on commit f6f722f

Please sign in to comment.