Last active
August 29, 2015 14:15
-
-
Save paulhoux/ec7871cfcdfda3ba1805 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/AppNative.h" // Contains the AppNative class and the events. | |
#include "cinder/gl/gl.h" // Contains most OpenGL convenience functions. | |
#include "cinder/Camera.h" // For our camera. | |
using namespace ci; | |
using namespace ci::app; | |
using namespace std; | |
class CinderProjectApp : public AppNative { | |
public: | |
//! Called once at the start of the application. | |
void setup() override; | |
//! Called once per frame. Perform your animations here. Don't draw anything. | |
void update() override; | |
//! Called once per frame. Draw your graphics. | |
void draw() override; | |
//! Called when the window is resized. Also called at the start of the application. | |
void resize() override; | |
private: | |
CameraPersp mCamera; | |
Vec3f mRotation; | |
Vec3f mTranslation; | |
}; | |
void CinderProjectApp::setup() | |
{ | |
// Setup the camera. | |
float verticalFieldOfView = 50.0f; | |
float aspectRatio = getWindowAspectRatio(); // See also: resize() | |
float nearPlane = 0.1f; | |
float farPlane = 10.0f; | |
mCamera.setPerspective( verticalFieldOfView, aspectRatio, nearPlane, farPlane ); | |
Vec3f eye = Vec3f( 0.0f, 0.0f, 0.0f ); | |
Vec3f center = Vec3f( 0.0f, 0.0f, 1.0f ); | |
Vec3f up = Vec3f( 0.0f, 1.0f, 0.0f ); | |
mCamera.lookAt( eye, center, up ); | |
// Initialize rotation and translation. | |
mRotation = Vec3f( 10.0f, 20.0f, 30.0f ); // Angles in degrees for each of the axes. | |
mTranslation = Vec3f( 0.0f, 0.0f, 1.0f ); | |
} | |
void CinderProjectApp::update() | |
{ | |
// Let's animate the rotation. | |
float seconds = float( getElapsedSeconds() ); | |
mRotation.x = 180.0f * math<float>::sin( seconds * 0.10f ); | |
mRotation.y = 180.0f * math<float>::sin( seconds * 0.15f ); | |
mRotation.z = 180.0f * math<float>::sin( seconds * 0.12f ); | |
} | |
void CinderProjectApp::draw() | |
{ | |
// Clear the color and depth buffers. | |
gl::clear(); | |
// Enable the depth buffer. | |
gl::enableDepthRead(); | |
gl::enableDepthWrite(); | |
// We are going to adjust scale, rotation and translation, | |
// which will change our model matrix. We will also change | |
// the view and projection matrix when setting the camera. | |
// Store the current matrices so we can later restore them. | |
gl::pushMatrices(); | |
gl::setMatrices( mCamera ); | |
gl::scale( -1.0f, 1.0f, 1.0f ); | |
gl::translate( mTranslation ); | |
gl::rotate( mRotation ); | |
glBegin( GL_LINES ); | |
glColor3f( 1.f, 0.f, 0.f ); | |
glVertex3f( 0.f, 0.f, 0.f ); | |
glVertex3f( 10.f, 0.f, 0.f ); | |
glColor3f( 0.f, 1.f, 0.f ); | |
glVertex3f( 0.f, 0.f, 0.f ); | |
glVertex3f( 0.f, 10.f, 0.f ); | |
glColor3f( 0.f, 0.f, 1.f ); | |
glVertex3f( 0.f, 0.f, 0.f ); | |
glVertex3f( 0.f, 0.f, 10.f ); | |
glEnd(); | |
// Restore matrices. | |
gl::popMatrices(); | |
// If we want to restore OpenGL's state to the way it was | |
// at the beginning of this function, we'll have to disable | |
// the depth buffer, too. | |
gl::disableDepthWrite(); | |
gl::disableDepthRead(); | |
} | |
void CinderProjectApp::resize() | |
{ | |
// Make sure our camera keeps the correct aspect ratio. | |
mCamera.setAspectRatio( getWindowAspectRatio() ); | |
} | |
//! This line will create the entry point for, and start, the application. | |
CINDER_APP_NATIVE( CinderProjectApp, RendererGl ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment