Skip to content

Instantly share code, notes, and snippets.

@num3ric
Created October 29, 2011 05:33
Show Gist options
  • Save num3ric/1324138 to your computer and use it in GitHub Desktop.
Save num3ric/1324138 to your computer and use it in GitHub Desktop.
Using glu NURBS inside Cinder.
#include "cinder/app/AppBasic.h"
#include "cinder/gl/gl.h"
#include "cinder/Camera.h"
#include "cinder/MayaCamUI.h"
#if defined( CINDER_MAC )
#include <OpenGL/glu.h>
#else
#include <GL/glu.h>
#endif
using namespace ci;
using namespace ci::app;
using namespace std;
GLUnurbsObj *theNurb;
GLfloat ctlpoints[4][4][3];
GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
class gluTestApp : public AppBasic {
public:
void setup();
void mouseDown( MouseEvent event );
void mouseDrag( MouseEvent event );
void resize( ResizeEvent event );
void update();
void draw();
// CAMERA
MayaCamUI mMayaCam;
float mCameraDistance;
Vec3f mLastEye, mEye, mCenter, mUp;
};
void nurbsError(GLenum errorCode)
{
const GLubyte *estring;
estring = gluErrorString(errorCode);
// fprintf (stderr, “Nurbs Error: %s\n”, estring);
exit (0);
}
void gluTestApp::setup()
{
// SETUP CAMERA
mCameraDistance = 500.0f;
mEye = Vec3f( 0.0f, 0.0f, mCameraDistance );
mCenter = Vec3f::zero();
mUp = Vec3f::yAxis();
CameraPersp cam;
cam.setEyePoint(mEye);
cam.setCenterOfInterestPoint(mCenter);
cam.setPerspective( 60.0f, getWindowAspectRatio(), 5.0f, 5000.0f );
mMayaCam.setCurrentCam( cam );
//Init lights
GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};
GLfloat position[] = {0.0, 0.0, 2.0, 1.0};
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_shininess[] = {50.0};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv( GL_FRONT, GL_DIFFUSE, Color( 1, 0, 0 ) );
theNurb = gluNewNurbsRenderer();
gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0);
gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
gluNurbsCallback(theNurb, GLU_ERROR,
(GLvoid (*)()) nurbsError);
int u, v;
for (u = 0; u < 4; u++) {
for (v = 0; v < 4; v++) {
ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5);
ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5);
if ( (u == 1 || u == 2) && (v == 1 || v == 2))
ctlpoints[u][v][2] = 3.0;
else
ctlpoints[u][v][2] = -3.0;
}
}
}
void gluTestApp::resize( ResizeEvent event )
{
CameraPersp cam = mMayaCam.getCamera();
cam.setAspectRatio( getWindowAspectRatio() );
mMayaCam.setCurrentCam( cam );
}
void gluTestApp::mouseDown( MouseEvent event )
{
mMayaCam.mouseDown(event.getPos());
}
void gluTestApp::mouseDrag( MouseEvent event )
{
mMayaCam.mouseDrag(event.getPos(), event.isLeftDown(), event.isMiddleDown(), event.isRightDown());
}
void gluTestApp::update()
{
gl::setMatrices( mMayaCam.getCamera() );
}
void gluTestApp::draw()
{
gl::clear( Color( 0, 0, 0 ) );
CameraPersp cam = mMayaCam.getCamera();
gl::setMatrices(cam);
gl::scale(Vec3f(100.0f, 100.0f, 100.0f));
gluBeginSurface(theNurb);
gluNurbsSurface(theNurb,
8, knots, 8, knots,
4 * 3, 3, &ctlpoints[0][0][0],
4, 4, GL_MAP2_VERTEX_3);
gluEndSurface(theNurb);
}
CINDER_APP_BASIC( gluTestApp, RendererGl )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment