Skip to content

Commit

Permalink
Upgrade Grade & Lesson ndk implements (#11)
Browse files Browse the repository at this point in the history
* Upgrade Grade & Lesson two ndk implement

* Lesson three ndk implement

* Lesson four ndk implement

* Lesson five ndk implements

* Lesson six ndk implement

* Lesson seven ndk implement

* Lesson eight ndk implement
  • Loading branch information
biezhihua authored and learnopengles committed Jan 22, 2018
1 parent 8b19393 commit 641fcc2
Show file tree
Hide file tree
Showing 64 changed files with 6,966 additions and 28 deletions.
75 changes: 59 additions & 16 deletions android/AndroidOpenGLESLessonsCpp/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,65 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-rtti -fno-exceptions -Wall")


add_library( # Sets the name of the library.
lesson-lib

# Sets the library as a shared library.
SHARED

# graphics
src/main/cpp/graphics/GLUtils.h
src/main/cpp/graphics/GLUtils.cpp
src/main/cpp/graphics/Matrix.h
src/main/cpp/graphics/Matrix.cpp

# Provides a relative path to your source file(s).
# lesson1
src/main/cpp/lesson1/Native1Lesson.cpp
src/main/cpp/lesson1/Native1Lesson.h
add_library(
# Sets the name of the library.
lesson-lib

# Sets the library as a shared library.
SHARED

# graphics
src/main/cpp/graphics/GLUtils.h
src/main/cpp/graphics/GLUtils.cpp
src/main/cpp/graphics/Matrix.h
src/main/cpp/graphics/Matrix.cpp

# Provides a relative path to your source file(s).
# lesson1
src/main/cpp/lesson1/Native1Lesson.cpp
src/main/cpp/lesson1/Native1Lesson.h

# lesson2
src/main/cpp/lesson2/Native2Lesson.cpp
src/main/cpp/lesson2/Native2Lesson.h

# lesson3
src/main/cpp/lesson3/Native3Lesson.cpp
src/main/cpp/lesson3/Native3Lesson.h

# lesson4
src/main/cpp/lesson4/Native4Lesson.cpp
src/main/cpp/lesson4/Native4Lesson.h

# lesson5
src/main/cpp/lesson5/Native5Lesson.cpp
src/main/cpp/lesson5/Native5Lesson.h

# lesson6
src/main/cpp/lesson6/Native6Lesson.cpp
src/main/cpp/lesson6/Native6Lesson.h

# lesson7
src/main/cpp/lesson7/Native7Lesson.cpp
src/main/cpp/lesson7/Native7Lesson.h
src/main/cpp/lesson7/Cubes.cpp
src/main/cpp/lesson7/Cubes.h
src/main/cpp/lesson7/CubesClientSide.cpp
src/main/cpp/lesson7/CubesClientSide.h
src/main/cpp/lesson7/CubesClientSideWithStride.cpp
src/main/cpp/lesson7/CubesClientSideWithStride.h
src/main/cpp/lesson7/CubesWithVbo.cpp
src/main/cpp/lesson7/CubesWithVbo.h
src/main/cpp/lesson7/CubesWithVboWithStride.cpp
src/main/cpp/lesson7/CubesWithVboWithStride.h
src/main/cpp/lesson7/GenData.cpp
src/main/cpp/lesson7/GenData.h

# lesson8
src/main/cpp/lesson8/Native8Lesson.cpp
src/main/cpp/lesson8/Native8Lesson.h
src/main/cpp/lesson8/HeightMap.cpp
src/main/cpp/lesson8/HeightMap.h
)

include_directories(src/main/cpp/)
Expand Down
14 changes: 9 additions & 5 deletions android/AndroidOpenGLESLessonsCpp/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ apply plugin: 'com.android.application'
def platformVersion = 18 //openGLES 3 min Version

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion 27

defaultConfig {
applicationId "com.learnopengles.android"
minSdkVersion 9
targetSdkVersion 25
applicationId "com.learnopengles.android.cpp"
minSdkVersion 21
targetSdkVersion 27

externalNativeBuild {
cmake {
Expand All @@ -26,3 +25,8 @@ android {
}
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.0.2'
}
36 changes: 33 additions & 3 deletions android/AndroidOpenGLESLessonsCpp/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learnopengles.android"
package="com.learnopengles.android.cpp"
android:versionCode="1"
android:versionName="1.0.0.0">

Expand All @@ -13,16 +13,46 @@
android:icon="@drawable/icon"
android:label="@string/app_name">

<activity android:name="TableOfContents">
<activity android:name="com.learnopengles.android.TableOfContents">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity
android:name=".lesson1.LessonOneActivity"
android:name="com.learnopengles.android.lesson1.LessonOneActivity"
android:label="@string/lesson_one"/>

<activity
android:name="com.learnopengles.android.lesson2.LessonTwoActivity"
android:label="@string/lesson_two"/>

<activity
android:name="com.learnopengles.android.lesson3.LessonThreeActivity"
android:label="@string/lesson_three"/>

<activity
android:name="com.learnopengles.android.lesson4.LessonFourActivity"
android:label="@string/lesson_four"/>

<activity
android:name="com.learnopengles.android.lesson5.LessonFiveActivity"
android:label="@string/lesson_five"/>

<activity
android:name="com.learnopengles.android.lesson6.LessonSixActivity"
android:label="@string/lesson_six"/>

<activity
android:name="com.learnopengles.android.lesson7.LessonSevenActivity"
android:label="@string/lesson_seven"/>

<activity
android:name="com.learnopengles.android.lesson8.LessonEightActivity"
android:label="@string/lesson_eight"/>

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
precision mediump float; // Set the default precision to medium. We don't need as high of a
// precision in the fragment shader.
uniform vec3 u_LightPos; // The position of the light in eye space.

varying vec3 v_Position; // Interpolated position for this fragment.
varying vec4 v_Color; // This is the color from the vertex shader interpolated across the
// triangle per fragment.
varying vec3 v_Normal; // Interpolated normal for this fragment.

// The entry point for our fragment shader.
void main()
{
// Will be used for attenuation.
float distance = length(u_LightPos - v_Position);

// Get a lighting direction vector from the light to the vertex.
vec3 lightVector = normalize(u_LightPos - v_Position);

// Calculate the dot product of the light vector and vertex normal. If the normal and light vector are
// pointing in the same direction then it will get max illumination.
float diffuse;

if (gl_FrontFacing) {
diffuse = max(dot(v_Normal, lightVector), 0.0);
} else {
diffuse = max(dot(-v_Normal, lightVector), 0.0);
}

// Add attenuation.
diffuse = diffuse * (1.0 / (1.0 + (0.10 * distance)));

// Add ambient lighting
diffuse = diffuse + 0.3;

// Multiply the color by the diffuse illumination level to get final output color.
gl_FragColor = (v_Color * diffuse);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
uniform mat4 u_MVPMatrix; // A constant representing the combined model/view/projection matrix.
uniform mat4 u_MVMatrix; // A constant representing the combined model/view matrix.

attribute vec4 a_Position; // Per-vertex position information we will pass in.
attribute vec4 a_Color; // Per-vertex color information we will pass in.
attribute vec3 a_Normal; // Per-vertex normal information we will pass in.

varying vec3 v_Position; // This will be passed into the fragment shader.
varying vec4 v_Color; // This will be passed into the fragment shader.
varying vec3 v_Normal; // This will be passed into the fragment shader.

// The entry point for our vertex shader.
void main()
{
// Transform the vertex into eye space.
v_Position = vec3(u_MVMatrix * a_Position);

// Pass through the color.
v_Color = a_Color;

// Transform the normal's orientation into eye space.
v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));

// gl_Position is a special variable used to store the final position.
// Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
gl_Position = u_MVPMatrix * a_Position;
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ long GLUtils::currentTimeMillis() {

GLuint GLUtils::loadTexture(const char *path) {
GLuint textureId = 0;
jclass utilsClass = sEnv->FindClass("com/bzh/gl/Utils");
jclass utilsClass = sEnv->FindClass("com/learnopengles/android/Utils");
if (utilsClass == NULL) {
LOGE("Couldn't find utils class");
return (GLuint) -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,7 @@ void mx4transform(float x, float y, float z, float w, const float *pM, float *pD
void Matrix::multiplyMV(float *r, const float *lhs, const float *rhs) {
mx4transform(rhs[0], rhs[1], rhs[2], rhs[3], lhs, r);
}

float Matrix::length(float x, float y, float z) {
return (float) sqrt(x * x + y * y + z * z);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

class Matrix {
public:

static const int MATRIX_SIZE = 16;

float mData[MATRIX_SIZE];

Matrix();
Expand Down Expand Up @@ -68,6 +70,16 @@ class Matrix {
const float *rhs);

static void multiplyMV(float *r, const float *lhs, const float *rhs);

/**
* Computes the length of a vector.
*
* @param x x coordinate of a vector
* @param y y coordinate of a vector
* @param z z coordinate of a vector
* @return the length of a vector
*/
static float length(float x, float y, float z);
};

#endif
Loading

0 comments on commit 641fcc2

Please sign in to comment.