Skip to content

Commit

Permalink
Fix normal code -- use a cross product.
Browse files Browse the repository at this point in the history
  • Loading branch information
learnopengles committed May 10, 2012
1 parent afc1329 commit 53bad4b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.learnopengles.android.lesson8;


interface ErrorHandler {
enum ErrorType {
BUFFER_CREATION_ERROR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,24 @@ class HeightMap {

// Cheap normal using a derivative of the function.
// The slope for X will be 2X, for Y will be 2Y.
final float xNormal = (-2 * xPosition) / 10f;
final float yNormal = (-2 * yPosition) / 10f;
final float length = Matrix.length(xNormal, yNormal, 1f);

heightMapVertexData[offset++] = xNormal / length;
heightMapVertexData[offset++] = yNormal / length;
heightMapVertexData[offset++] = 1f / length;
// Divide by 10 since the position's Z is also divided by 10.
final float xSlope = (2 * xPosition) / 10f;
final float ySlope = (2 * yPosition) / 10f;

// Calculate the normal using the cross product of the slope.
final float[] planeVectorX = {1f, 0f, xSlope};
final float[] planeVectorY = {0f, 1f, ySlope};
final float[] normalVector = {
(planeVectorX[1] * planeVectorY[2]) - (planeVectorX[2] * planeVectorY[1]),
(planeVectorX[2] * planeVectorY[0]) - (planeVectorX[0] * planeVectorY[2]),
(planeVectorX[0] * planeVectorY[1]) - (planeVectorX[1] * planeVectorY[0])};

// Normalize the normal
final float length = Matrix.length(normalVector[0], normalVector[1], normalVector[2]);

heightMapVertexData[offset++] = normalVector[0] / length;
heightMapVertexData[offset++] = normalVector[1] / length;
heightMapVertexData[offset++] = normalVector[2] / length;

// Add some fancy colors.
heightMapVertexData[offset++] = xRatio;
Expand Down

0 comments on commit 53bad4b

Please sign in to comment.