I’m a Newbie to OpenGL, and the list. Trying to pick control points for nurbs surface. Not having any luck with my example below. I seem to get ‘0’ hits, and only 1 name on the stack. Anyone happen to be working with nurbs, picking, and control points and could possibly help?
I’ve looked at the edit.c ex. which uses evaluators not nurbs, but does control point selection, also the picking.c ex. for picking, and the surface.c example for drawing nurbs.
Any help or pointers for how to pick points for nurbs surface greatly appreciated.
/*
- from glut exs: surface.c, picksquare.c, and edit.c
- This program draws a NURBS surface and shows control points
*/
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
GLfloat ctlpoints[4][4][3];
int showPoints = 1;
GLUnurbsObj *theNurb;
/*
- Initializes the control points of the surface.
- The control points range from -3 to +3 in x, y, and z
/
void drawSurface(GLenum mode)
{
int u, v;
for (u = 0; u < 4; u++) {
if (mode == GL_SELECT)
glLoadName (u);
for (v = 0; v < 4; v++) {
if (mode == GL_SELECT)
glPushName (v);
ctlpoints[u][v][0] = 2.0((GLfloat)u - 1.5);
ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5);
if (mode == GL_SELECT)
glPopName ();
}
}
}
/* Initialize material property and depth buffer.
*/
void init(void)
{
GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 100.0 };
glClearColor (0.0, 0.0, 0.0, 0.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
drawSurface (GL_SELECT);
theNurb = gluNewNurbsRenderer();
gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0);
gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON);
}
void display(void)
{
GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
int i, j;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(330.0, 1.,0.,0.);
glScalef (0.5, 0.5, 0.5);
gluBeginSurface(theNurb);
gluNurbsSurface(theNurb,
8, knots, 8, knots,
4 * 3, 3, &ctlpoints[0][0][0],
4, 4, GL_MAP2_VERTEX_3);
gluEndSurface(theNurb);
if (showPoints) {
glPointSize(8.0);
glDisable(GL_LIGHTING);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
glVertex3f(ctlpoints[i][j][0],
ctlpoints[i][j][1], ctlpoints[i][j][2]);
}
}
glEnd();
glEnable(GL_LIGHTING);
}
glPopMatrix();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective (45.0, (GLdouble)w/(GLdouble)h, 3.0, 8.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -5.0);
}
/* processHits prints out the contents of the
- selection array.
*/
void processHits (GLint hits, GLuint buffer[])
{
unsigned int i, j;
GLuint ii, jj, names, *ptr;
printf (“hits = %d
“, hits);
ptr = (GLuint ) buffer;
for (i = 0; i < hits; i++) { / for each hit */
names = *ptr;
printf (” number of names for this hit = %d
“, names); ptr++;
printf(” z1 is %g;”, (float) *ptr/0x7fffffff); ptr++;
printf(" z2 is %g
“, (float) ptr/0x7fffffff); ptr++;
printf (" names are ");
for (j = 0; j < names; j++) { / for each name */
printf (”%d “, ptr);
if (j == 0) / set row and column */
ii = *ptr;
else if (j == 1)
jj = *ptr;
ptr++;
}
printf (”
");
}
}
/* pickCtlPoints() sets up selection mode, name stack,
- and projection matrix for picking.
*/
#define BUFSIZE 512
void pickCtlPoints(int button, int state, int x, int y)
{
GLuint selectBuf[BUFSIZE];
GLint hits;
GLint viewport[4];
if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN)
return;
glGetIntegerv (GL_VIEWPORT, viewport);
glSelectBuffer (BUFSIZE, selectBuf);
(void) glRenderMode (GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
/* create 5x5 pixel picking region near cursor location */
gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y),
5.0, 5.0, viewport);
gluOrtho2D (0.0, 3.0, 0.0, 3.0);
drawSurface (GL_SELECT);
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glFlush ();
hits = glRenderMode (GL_RENDER);
processHits (hits, selectBuf);
//-----------------
GLint stackSize;
glGetIntegerv(GL_NAME_STACK_DEPTH, &stackSize);
//number of names on the slection stack:
printf("STACK_DEPTH = %d
", stackSize);
//-----------------
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutMouseFunc (pickCtlPoints);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}