Skip to content

Instantly share code, notes, and snippets.

@paulhoux
Last active December 28, 2015 17:29
Show Gist options
  • Save paulhoux/7536160 to your computer and use it in GitHub Desktop.
Save paulhoux/7536160 to your computer and use it in GitHub Desktop.
Shows how to setup a camera, render a grid and control the camera using a mouse.
#include "cinder/app/AppNative.h"
#include "cinder/gl/gl.h"
#include "cinder/Camera.h"
#include "cinder/MayaCamUI.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class CamAndGridApp : public AppNative {
public:
void setup();
void update();
void draw();
void resize();
void mouseDown( MouseEvent event );
void mouseDrag( MouseEvent event );
void keyDown( KeyEvent event );
private:
CameraPersp mCamera;
MayaCamUI mMayaCam;
};
void CamAndGridApp::setup()
{
// initialize the camera
// (note: setting the center of interest last will make
// sure the camera stays focused on that point)
mCamera.setEyePoint( Vec3f(0.0f, 200.0f, -500.0f) );
mCamera.setCenterOfInterestPoint( Vec3f::zero() );
}
void CamAndGridApp::update()
{
// nothing to update in this sample application
}
void CamAndGridApp::draw()
{
// clear the window
gl::clear();
// enable 3D rendering
gl::enableDepthRead();
gl::enableDepthWrite();
// push the matrices, so we can restore them later
gl::pushMatrices();
// apply our camera
gl::setMatrices( mCamera );
// draw a coordinate frame
gl::drawCoordinateFrame( 100.0f, 5.0f, 2.5f );
// draw our grid
const int size = 100;
const int step = 10;
gl::color( Color::white() );
for(int i=-size;i<=size;i+=step) {
gl::drawLine( Vec3f(i, 0.0f, -size), Vec3f(i, 0.0f, size) );
gl::drawLine( Vec3f(-size, 0.0f, i), Vec3f(size, 0.0f, i) );
}
// restore our matrices
gl::popMatrices();
// disable 3D rendering
gl::disableDepthWrite();
gl::disableDepthRead();
}
void CamAndGridApp::resize()
{
// make sure the camera has the correct aspect ratio
mCamera.setAspectRatio( getWindowAspectRatio() );
}
void CamAndGridApp::mouseDown( MouseEvent event )
{
// start interacting with the camera, by telling MayaCam
// where our camera is at this moment, then passing the mouse position
mMayaCam.setCurrentCam( mCamera );
mMayaCam.mouseDown( event.getPos() );
}
void CamAndGridApp::mouseDrag( MouseEvent event )
{
// interact with the camera
mMayaCam.mouseDrag( event.getPos(), event.isLeftDown(), event.isMiddleDown(), event.isRightDown() );
// update our camera
mCamera = mMayaCam.getCamera();
}
void CamAndGridApp::keyDown( KeyEvent event )
{
switch( event.getCode() )
{
case KeyEvent::KEY_SPACE:
// point the camera at the origin again
mCamera.setCenterOfInterestPoint( Vec3f::zero() );
break;
}
}
CINDER_APP_NATIVE( CamAndGridApp, RendererGl )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment