Last active
January 23, 2016 06:52
-
-
Save paulhoux/c0d46723c478428f642b to your computer and use it in GitHub Desktop.
Circle of Quads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "cinder/app/App.h" | |
#include "cinder/app/RendererGl.h" | |
#include "cinder/gl/gl.h" | |
#include "cinder/Camera.h" | |
#include "cinder/CameraUi.h" | |
#include "cinder/ip/Checkerboard.h" | |
using namespace ci; | |
using namespace ci::app; | |
struct Quad { | |
mat4 transform; | |
Quad( const vec3 &position ) | |
{ | |
vec3 scale = vec3( 100 ); | |
vec3 target = vec3( 0 ); | |
vec3 up = vec3( 0, 1, 0 ); | |
transform = glm::translate( position ); | |
// Instead of glm::lookAt, we can simply create a rotation from the default orientation | |
// (looking down the positive Z-axis) to the desired one (looking at the target). | |
// The glm::rotation() method creates a quaternion which we can convert to a mat4 and | |
// apply to our transform. | |
transform *= glm::toMat4( glm::rotation( vec3( 0, 0, 1 ), glm::normalize( target - position ) ) ); | |
transform *= glm::scale( scale ); | |
} | |
}; | |
class QuadCircle : public App { | |
public: | |
static void init( App::Settings *settings ); | |
void setup() override; | |
void draw() override; | |
gl::TextureRef _texture; | |
std::vector<Quad> _quads; | |
gl::BatchRef _quadBatch; | |
CameraPersp _camera; | |
CameraUi _camUi; | |
}; | |
void QuadCircle::init( App::Settings *settings ) | |
{ | |
settings->setWindowSize( 1280, 720 ); | |
} | |
void QuadCircle::setup() | |
{ | |
const int num_quads = 12; | |
const double radius = 250.0f; | |
for( auto i = 0; i < num_quads; i++ ) { | |
double angle = (double)i * 2.0f * M_PI / num_quads; | |
double x = cos( angle ) * radius; | |
double y = 0; | |
double z = sin( angle ) * radius; | |
vec3 pos = vec3( x, y, z ); | |
_quads.emplace_back( pos ); | |
} | |
_camera = CameraPersp( getWindowWidth(), getWindowHeight(), 60.0f, 0.1f, 1000.0f ); | |
_camera.lookAt( vec3( 0, 0, radius ), vec3( 0 ) ); | |
_camUi = CameraUi( &_camera, getWindow() ); | |
_texture = gl::Texture::create( ip::checkerboard( 1024, 1024, 128 ) ); | |
_quadBatch = gl::Batch::create( | |
geom::Rect().rect( Rectf( -0.5, -0.5, 0.5, 0.5 ) ), | |
gl::getStockShader( gl::ShaderDef().color().texture() ) ); | |
} | |
void QuadCircle::draw() | |
{ | |
gl::ScopedColor scpColor( Color( 0.8f, 0.8f, 0.3f ) ); | |
gl::ScopedTextureBind scpTex0( _texture ); | |
gl::ScopedDepth scpDepth( true ); | |
gl::ScopedFaceCulling scpCull( true ); | |
gl::ScopedMatrices scpMatrices; | |
gl::clear( Color( 0.0f, 0.0f, 0.0f ) ); | |
gl::setMatrices( _camera ); | |
for( auto &q : _quads ) { | |
gl::setModelMatrix( q.transform ); | |
_quadBatch->draw(); | |
} | |
} | |
CINDER_APP( QuadCircle, RendererGl, QuadCircle::init ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment