Skip to content

Instantly share code, notes, and snippets.

@notlion
Created October 16, 2011 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notlion/1290435 to your computer and use it in GitHub Desktop.
Save notlion/1290435 to your computer and use it in GitHub Desktop.
Test Cinder Ray / Plane Intersection
#include "cinder/app/AppBasic.h"
#include "cinder/gl/gl.h"
#include "cinder/Rand.h"
#include "cinder/Camera.h"
#include "cinder/Perlin.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class test_plane_intersectApp : public AppBasic {
public:
void setup();
void update();
void draw();
void mouseDown(MouseEvent e);
CameraPersp camera;
Perlin perlin;
Vec3f plane_origin, plane_dir;
Ray test_ray;
};
void test_plane_intersectApp::setup()
{
Rand::randomize();
plane_origin = Rand::randVec3f();
plane_dir = Rand::randVec3f();
camera.setPerspective(60, getWindowAspectRatio(), 0.01f, 10.0f);
camera.lookAt(Vec3f(0, 0, 5), Vec3f::zero());
test_ray.setOrigin(Vec3f(0, 2, 0));
}
void test_plane_intersectApp::update()
{
test_ray.setDirection(Vec3f(
perlin.noise(getElapsedSeconds(), 100),
perlin.noise(getElapsedSeconds(), 200) - 1,
perlin.noise(getElapsedSeconds(), 300)
).normalized());
}
void test_plane_intersectApp::draw()
{
gl::clear(Color(0, 0, 0));
gl::setMatrices(camera);
gl::color(Colorf(0.5, 0.5, 0));
gl::drawVector(test_ray.getOrigin(), test_ray.getOrigin() + test_ray.getDirection());
float t;
test_ray.calcPlaneIntersection(plane_origin, plane_dir, &t);
gl::color(Colorf(1, 1, 0));
gl::drawSphere(test_ray.calcPosition(t), 0.05f);
gl::color(Colorf(1, 1, 1));
gl::pushModelView();
gl::multModelView(Matrix44f::createTranslation(plane_origin) * Matrix44f::alignZAxisWithTarget(plane_dir, Vec3f::yAxis()));
gl::drawStrokedRect(Rectf(-1, -1, 1, 1));
gl::drawCoordinateFrame();
gl::popModelView();
}
void test_plane_intersectApp::mouseDown(MouseEvent e)
{
plane_origin = Rand::randVec3f();
plane_dir = Rand::randVec3f();
}
CINDER_APP_BASIC(test_plane_intersectApp, RendererGl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment