Sign in to follow this  
Vectormycalculus

NURBS Question

Recommended Posts

Could someone explain why nothing is drawn? And the function: glNurbsCallback(theNurb, GLU_ERROR, (GLvoid) (*)()) do i have to register the tessalation callbacks?
#include <stdio.h>
#include <stdlib.h>

#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>

#ifndef CALLBACK
#define CALLBACK
#endif

GLfloat ctlPoints [4][4][3];
int showPoints = 0;

GLUnurbsObj *theNurb;

void init_surface(void)
{
     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 CALLBACK nurbsError ( GLenum errorCode )
{
     const GLubyte *estring;
     estring = gluErrorString ( errorCode );
     fprintf ( stderr, "Nurbs Error: %s\n", estring );
     exit ( 0 );
}

void InitOpenGL(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 );
    
    init_surface();
    
    theNurb = gluNewNurbsRenderer();
    gluNurbsProperty ( theNurb, GLU_SAMPLING_TOLERANCE, 25.0 );
    gluNurbsProperty ( theNurb, GLU_DISPLAY_MODE, GLU_FILL );
   // gluNurbsCallback ( theNurb, GLU_ERROR, nurbsError );
    gluNurbsProperty ( theNurb, GLU_DISPLAY_MODE, GLU_FILL );
}


void glutDisplay(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.0, 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 ( 5.0 );
          glDisable ( GL_LIGHTING );
          glColor3f ( 1.0, 1.0, 1.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 glutReshape( int width, int height )
{
     glViewport ( 0, 0, ( GLsizei ) width, ( GLsizei ) height );
     glMatrixMode ( GL_PROJECTION );
     glLoadIdentity();
     gluPerspective ( 45.0, ( GLdouble ) width / ( GLdouble ) height, 3.0, 8.0 );
     glLoadIdentity();
     glTranslatef ( 0.0, 0.0, -5.0 );
}

void glutKeyboard (unsigned char key, int x, int y)
{
     switch ( key )
     {
            case 'c':
            case 'C':
                 showPoints = !showPoints;
                 glutPostRedisplay();
                 break;
            case 27:
                 exit ( 0 );
                 break;
            default:
                 break;    
     }
}

int main( int argc, char **argv )
{

     glutInit( &argc, argv );
     glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH );

     glutInitWindowPosition( 100, 100 );
     glutInitWindowSize( 840, 680 );
     glutCreateWindow( "N-U-R-B-S" );

     glutKeyboardFunc( glutKeyboard );
     glutReshapeFunc( glutReshape );
     glutDisplayFunc( glutDisplay );

     InitOpenGL();

     glutMainLoop();
    
     return EXIT_SUCCESS;
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this