-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
183456f
commit f1ede12
Showing
13 changed files
with
674 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file added
BIN
+6.97 KB
android/AndroidOpenGLESLessons/res/drawable-xhdpi/ic_lesson_eight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
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
32
android/AndroidOpenGLESLessons/res/raw/per_pixel_fragment_shader_no_tex.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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 = 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); | ||
} | ||
|
27 changes: 27 additions & 0 deletions
27
android/AndroidOpenGLESLessons/res/raw/per_pixel_vertex_shader_no_tex.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
android/AndroidOpenGLESLessons/src/com/learnopengles/android/lesson8/ErrorHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.learnopengles.android.lesson8; | ||
|
||
|
||
interface ErrorHandler { | ||
enum ErrorType { | ||
BUFFER_CREATION_ERROR | ||
} | ||
|
||
void handleError(ErrorType errorType, String cause); | ||
} |
64 changes: 64 additions & 0 deletions
64
...oid/AndroidOpenGLESLessons/src/com/learnopengles/android/lesson8/LessonEightActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.learnopengles.android.lesson8; | ||
|
||
import android.app.Activity; | ||
import android.app.ActivityManager; | ||
import android.content.Context; | ||
import android.content.pm.ConfigurationInfo; | ||
import android.os.Bundle; | ||
import android.util.DisplayMetrics; | ||
import android.view.View; | ||
import android.view.View.OnClickListener; | ||
import android.widget.Button; | ||
|
||
import com.learnopengles.android.R; | ||
|
||
public class LessonEightActivity extends Activity { | ||
private LessonEightGLSurfaceView glSurfaceView; | ||
private LessonEightRenderer renderer; | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
glSurfaceView = new LessonEightGLSurfaceView(this); | ||
|
||
setContentView(glSurfaceView); | ||
|
||
// Check if the system supports OpenGL ES 2.0. | ||
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); | ||
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo(); | ||
final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000; | ||
|
||
if (supportsEs2) { | ||
// Request an OpenGL ES 2.0 compatible context. | ||
glSurfaceView.setEGLContextClientVersion(2); | ||
|
||
final DisplayMetrics displayMetrics = new DisplayMetrics(); | ||
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); | ||
|
||
// Set the renderer to our demo renderer, defined below. | ||
renderer = new LessonEightRenderer(this, glSurfaceView); | ||
glSurfaceView.setRenderer(renderer, displayMetrics.density); | ||
} else { | ||
// This is where you could create an OpenGL ES 1.x compatible | ||
// renderer if you wanted to support both ES 1 and ES 2. | ||
return; | ||
} | ||
} | ||
|
||
@Override | ||
protected void onResume() { | ||
// The activity must call the GL surface view's onResume() on activity | ||
// onResume(). | ||
super.onResume(); | ||
glSurfaceView.onResume(); | ||
} | ||
|
||
@Override | ||
protected void onPause() { | ||
// The activity must call the GL surface view's onPause() on activity | ||
// onPause(). | ||
super.onPause(); | ||
glSurfaceView.onPause(); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...ndroidOpenGLESLessons/src/com/learnopengles/android/lesson8/LessonEightGLSurfaceView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.learnopengles.android.lesson8; | ||
|
||
import android.content.Context; | ||
import android.opengl.GLSurfaceView; | ||
import android.util.AttributeSet; | ||
import android.view.MotionEvent; | ||
import android.widget.Toast; | ||
|
||
import com.learnopengles.android.R; | ||
import com.learnopengles.android.lesson8.ErrorHandler.ErrorType; | ||
|
||
public class LessonEightGLSurfaceView extends GLSurfaceView implements ErrorHandler | ||
{ | ||
private LessonEightRenderer renderer; | ||
|
||
// Offsets for touch events | ||
private float previousX; | ||
private float previousY; | ||
|
||
private float density; | ||
|
||
public LessonEightGLSurfaceView(Context context) | ||
{ | ||
super(context); | ||
} | ||
|
||
public LessonEightGLSurfaceView(Context context, AttributeSet attrs) | ||
{ | ||
super(context, attrs); | ||
} | ||
|
||
@Override | ||
public void handleError(final ErrorType errorType, final String cause) { | ||
// Queue on UI thread. | ||
post(new Runnable() { | ||
@Override | ||
public void run() { | ||
final String text; | ||
|
||
switch (errorType) { | ||
case BUFFER_CREATION_ERROR: | ||
text = String | ||
.format(getContext().getResources().getString( | ||
R.string.lesson_eight_error_could_not_create_vbo), cause); | ||
break; | ||
default: | ||
text = String.format( | ||
getContext().getResources().getString( | ||
R.string.lesson_eight_error_unknown), cause); | ||
} | ||
|
||
Toast.makeText(getContext(), text, Toast.LENGTH_LONG).show(); | ||
|
||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean onTouchEvent(MotionEvent event) | ||
{ | ||
if (event != null) | ||
{ | ||
float x = event.getX(); | ||
float y = event.getY(); | ||
|
||
if (event.getAction() == MotionEvent.ACTION_MOVE) | ||
{ | ||
if (renderer != null) | ||
{ | ||
float deltaX = (x - previousX) / density / 2f; | ||
float deltaY = (y - previousY) / density / 2f; | ||
|
||
renderer.deltaX += deltaX; | ||
renderer.deltaY += deltaY; | ||
} | ||
} | ||
|
||
previousX = x; | ||
previousY = y; | ||
|
||
return true; | ||
} | ||
else | ||
{ | ||
return super.onTouchEvent(event); | ||
} | ||
} | ||
|
||
// Hides superclass method. | ||
public void setRenderer(LessonEightRenderer renderer, float density) | ||
{ | ||
this.renderer = renderer; | ||
this.density = density; | ||
super.setRenderer(renderer); | ||
} | ||
} |
Oops, something went wrong.