Skip to content

Commit

Permalink
Use texture matrix for heightmap transformation
Browse files Browse the repository at this point in the history
Finally found the bug in my transformation code, I was multiplying
the texture matrix and the texture coordinates in the wrong order.
  • Loading branch information
drewish committed Jan 21, 2015
1 parent 3738981 commit 1e135c1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 43 deletions.
2 changes: 1 addition & 1 deletion include/Ship.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Ship {
Vec4f mThrusters = Vec4f::zero();
Vec4f mAcc = Vec4f::zero();
Vec4f mVel = Vec4f::zero();
Vec4f mPos = Vec4f(0.5, 0.5, 1, 0.0);
Vec4f mPos = Vec4f(0.0, 0.0, 1, 0.0);
};

#endif /* defined(__AlienLander__Ship__) */
11 changes: 5 additions & 6 deletions resources/vert.glsl
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#version 120

uniform sampler2D tex0;
uniform mat4 texTransform;
uniform float zoom;

void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
vec4 heightCoord = gl_TexCoord[0] * texTransform;
vec4 heightColor = texture2D( tex0, heightCoord.st);
gl_FrontColor = gl_Color;
gl_Position = ftransform(); // gl_ModelViewProjectionMatrix * gl_Vertex;
gl_Position.y += heightColor.r * 20.0 * (1.0 - zoom);
vec4 heightCoord = gl_TextureMatrix[0] * gl_TexCoord[0];
vec4 heightColor = texture2D( tex0, heightCoord.st );
gl_FrontColor = gl_Color; // try: heightColor;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_Position.y += heightColor.r * 20.0 * ( 1.0 - zoom );
}
63 changes: 27 additions & 36 deletions src/AlienLanderApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,17 @@ class AlienLanderApp : public AppNative {
void update();
void draw();


Vec2f mDelta1, mDelta2;

uint mPoints = 21;
uint mLines = 40;

Ship mShip;
SegmentDisplay mDisplay = SegmentDisplay(16, Vec2i(0, 0), 2);


gl::VboMeshRef mMaskMesh;
gl::VboMeshRef mLineMesh;
gl::TextureRef mTexture;
gl::GlslProgRef mShader;
CameraPersp mCamera;
Matrix44f mTexTransform;
float mZoom = 0.5;

Color mBlack = Color::black();
Color mBlue = Color8u(66, 161, 235);
Expand Down Expand Up @@ -95,10 +89,9 @@ void AlienLanderApp::setup()

// setFullScreen( true );
setFrameRate(60);

gl::enableVerticalSync(false);

mCamera.setPerspective( 30.0f, 1.0f, 10.0f, 60.0f );
mCamera.setPerspective( 40.0f, 1.0f, 10.0f, 60.0f );
}

void AlienLanderApp::buildMeshes()
Expand Down Expand Up @@ -184,27 +177,9 @@ void AlienLanderApp::update()
boost::format formatter("%+05f");
mDisplay.update("FPS " + (formatter % fps).str(), (fps > 30) ? mBlue : mRed, mDarkBlue);

// mZoom = math<float>::clamp(mShip.mPos.z, 0.0, 1.0);

float z = math<float>::clamp(mShip.mPos.z, 0.0, 1.0);
// TODO: Need to change the focus point to remain parallel as we descend
mCamera.lookAt( Vec3f( 0.0f, 30.0f * mShip.mPos.z, 20.0f ), Vec3f(0.0,0.0,0.0), Vec3f::yAxis() );

Matrix44f center = Matrix44f(1, 0, 0, +0.5,
0, 1, 0, +0.5,
0, 0, 1, 0,
0, 0, 0, 1);
Matrix44f goback = Matrix44f(1, 0, 0, -0.5,
0, 1, 0, -0.5,
0, 0, 1, 0,
0, 0, 0, 1);
Matrix44f r = Matrix44f::createRotation(Vec3f::zAxis(), mShip.mPos.w);
Matrix44f s = Matrix44f::createScale(mZoom);
Matrix44f t = Matrix44f(1, 0, 0, mShip.mPos.x,
0, 1, 0, mShip.mPos.y,
0, 0, 1, 0,
0, 0, 0, 1);

mTexTransform = goback * r * t * s * center;
mCamera.lookAt( Vec3f( 0.0f, 30.0f * z, 20.0f ), Vec3f(0.0,1.0,0.0), Vec3f::yAxis() );
}

void AlienLanderApp::draw()
Expand All @@ -223,8 +198,18 @@ void AlienLanderApp::draw()
mTexture->enableAndBind();
mShader->bind();
mShader->uniform( "tex0", 0 );
mShader->uniform( "texTransform", mTexTransform );
mShader->uniform( "zoom", mZoom );
mShader->uniform( "zoom", 0.5f /*mZoom*/ );

// Transform the height map via the texture matrix
glMatrixMode( GL_TEXTURE );
glLoadIdentity();

float scale = mShip.mPos.z;
gl::translate( 0.5, 0.5 );
gl::rotate( mShip.mPos.w * 180 / M_PI );
gl::scale( scale, scale );
gl::translate( mShip.mPos.xy() );
gl::translate( -0.5, -0.5 );

uint indiciesInLine = mPoints;
uint indiciesInMask = mPoints * 2;
Expand All @@ -234,15 +219,15 @@ void AlienLanderApp::draw()
gl::drawRange( mLineMesh, i * indiciesInLine, indiciesInLine);

gl::color( Color::gray(0.1) );
// gl::enableWireframe();
gl::drawRange( mMaskMesh, i * indiciesInMask, indiciesInMask);
// gl::disableWireframe();
}

glLoadIdentity();
glMatrixMode( GL_MODELVIEW );

mShader->unbind();
mTexture->unbind();


/*
// Vector pointing north
gl::lineWidth(2);
Expand All @@ -265,14 +250,20 @@ void AlienLanderApp::draw()

void AlienLanderApp::mouseMove( MouseEvent event )
{
// int height = getWindowHeight();
// mRatio = 1 - (math<float>::clamp(event.getY(), 0, height) / height);
// int height = getWindowHeight();
// int width = getWindowWidth();
// mZoom = 1 - (math<float>::clamp(event.getY(), 0, height) / height);
// mAngle = (math<float>::clamp(event.getX(), 0, width) / width);
// 2 * M_PI *
}

void AlienLanderApp::touchesMoved( TouchEvent event )
{
return;
Vec2f mDelta1, mDelta2;

// TODO treat the two deltas as forces acting on a rigid body.
// Accelenration becomse translation
// Acceleration becomes translation
// Torque becomes rotation
// Compression/tension becomes zooming
const vector<TouchEvent::Touch>&touches = event.getTouches();
Expand Down

0 comments on commit 1e135c1

Please sign in to comment.