Skip to content

Commit

Permalink
Added heightmap lesson.
Browse files Browse the repository at this point in the history
  • Loading branch information
learnopengles committed May 9, 2012
1 parent 183456f commit f1ede12
Show file tree
Hide file tree
Showing 13 changed files with 674 additions and 2 deletions.
3 changes: 3 additions & 0 deletions android/AndroidOpenGLESLessons/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@
<activity
android:label="@string/lesson_seven"
android:name=".lesson7.LessonSevenActivity"/>
<activity
android:label="@string/lesson_eight"
android:name=".lesson8.LessonEightActivity"/>
</application>
</manifest>
Binary file not shown.
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.
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);
}

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;
}
4 changes: 4 additions & 0 deletions android/AndroidOpenGLESLessons/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@
<string name="lesson_seven_not_using_VBOs">Not using VBOs</string>
<string name="lesson_seven_using_stride">Using stride</string>
<string name="lesson_seven_not_using_stride">Not using stride</string>
<string name="lesson_eight">Lesson Eight: An Intro to IBOs</string>
<string name="lesson_eight_subtitle">This lesson looks at index buffer objects (IBOs).</string>
<string name="lesson_eight_error_could_not_create_vbo">Could not create vertex buffer object: %s</string>
<string name="lesson_eight_error_unknown">Unknown error: %s</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.learnopengles.android.lesson5.LessonFiveActivity;
import com.learnopengles.android.lesson6.LessonSixActivity;
import com.learnopengles.android.lesson7.LessonSevenActivity;
import com.learnopengles.android.lesson8.LessonEightActivity;

public class TableOfContents extends ListActivity
{
Expand Down Expand Up @@ -104,6 +105,15 @@ public void onCreate(Bundle savedInstanceState)
activityMapping.put(i++, LessonSevenActivity.class);
}

{
final Map<String, Object> item = new HashMap<String, Object>();
item.put(ITEM_IMAGE, R.drawable.ic_lesson_eight);
item.put(ITEM_TITLE, getText(R.string.lesson_eight));
item.put(ITEM_SUBTITLE, getText(R.string.lesson_eight_subtitle));
data.add(item);
activityMapping.put(i++, LessonEightActivity.class);
}

final SimpleAdapter dataAdapter = new SimpleAdapter(this, data, R.layout.toc_item, new String[] {ITEM_IMAGE, ITEM_TITLE, ITEM_SUBTITLE}, new int[] {R.id.Image, R.id.Title, R.id.SubTitle});
setListAdapter(dataAdapter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public void onCreate(Bundle savedInstanceState) {

mGLSurfaceView = (LessonSevenGLSurfaceView) findViewById(R.id.gl_surface_view);

// We need the

// Check if the system supports OpenGL ES 2.0.
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
Expand Down
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);
}
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();
}
}
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);
}
}
Loading

0 comments on commit f1ede12

Please sign in to comment.