NURBS surface using OpenGL and glut

Hi,

I am trying to display an extruded circle using OpenGL and the glut library under Windows and Visual C++. The final surface should look like a perfectly round tube. However, what I see on the display is rather a jagged “tube”, see attached. The code, I am using for this purpose, is attached. I appreciate any hint to solve this issue.

Thank you!

Hadi

[ATTACH=CONFIG]1533[/ATTACH]


#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <GL/freeglut.h>
#include <cstdio>
#include <iostream>
using namespace std;

#define S_NUMPOINTS 9
#define S_ORDER     3  
#define S_NUMKNOTS  (S_NUMPOINTS + S_ORDER)
#define T_NUMPOINTS 2
#define T_ORDER     2
#define T_NUMKNOTS  (T_NUMPOINTS + T_ORDER)
#define SQRT2       1.414213
#define SQRT2HALF   0.707107


/* initialized local data */

GLfloat sknots[S_NUMKNOTS] =
{ 0., 0., 0., 1., 1., 2., 2., 3., 3., 4., 4., 4. };
GLfloat tknots[T_NUMKNOTS] =
{ 0.0, 0.0, 1.0, 1.0 };

GLfloat ctlpoints[S_NUMPOINTS][T_NUMPOINTS][4] =
    {
    { { 1.0, 0.0, 1.0, 1.0 }, { 1.0, 0.0, 0.0, 1.0 } },
    { { 1.0, 1.0, 1.0, SQRT2HALF },{ 1.0, 1.0, 0.0, SQRT2HALF } },
    { { 0.0, 1.0, 1.0, 1.0 },{ 0.0, 1.0, 0.0, 1.0 } },
    { { -1.0, 1.0, 1.0, SQRT2HALF },{ -1.0, 1.0, 0.0, SQRT2HALF } },
    { { -1.0, 0.0, 1.0, 1.0 },{ -1.0, 0.0, 0.0, 1.0 } },
    { { -1.0, -1.0, 1.0, SQRT2HALF },{ -1.0, -1.0, 0.0, SQRT2HALF } },
    { { 0.0, -1.0, 1.0, 1.0 },{ 0.0, -1.0, 0.0, 1.0 } },
    { { 1.0, -1.0, 1.0, SQRT2HALF },{ 1.0, -1.0, 0.0, SQRT2HALF } },
    { { 1.0, 0.0, 1.0, 1.0 },{ 1.0, 0.0, 0.0, 1.0 } },
    };

GLUnurbsObj *theNurb;

/*  Initialize material property, light source, lighting model,
*  and depth buffer.
*/
void myinit(void)
{
    GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat mat_diffuse[] = { 1.0, 0.2, 1.0, 1.0 };
    GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat mat_shininess[] = { 50.0 };

    GLfloat light0_position[] = { 1.0, 0.1, 1.0, 0.0 };
    GLfloat light1_position[] = { -1.0, 0.1, 1.0, 0.0 };

    GLfloat lmodel_ambient[] = { 0.3, 0.3, 0.3, 1.0 };

    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
    glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHT1);
    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_AUTO_NORMAL);

    theNurb = gluNewNurbsRenderer();

    gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0);
    gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glTranslatef(4., 4.5, 2.5);
    glRotatef(220.0, 1., 0., 0.);
    glRotatef(115.0, 0., 1., 0.);
    glTranslatef(-4., -4.5, -2.5);

    gluBeginSurface(theNurb);
    gluNurbsSurface(theNurb,
        S_NUMKNOTS, sknots,
        T_NUMKNOTS, tknots,
        4 * T_NUMPOINTS,
        4,
        &ctlpoints[0][0][0],
        S_ORDER, T_ORDER,
        GL_MAP2_VERTEX_4);
    gluEndSurface(theNurb);

    glPopMatrix();
    glFlush();
}

void myReshape(int w, int h)
{
    w = 400;
    h = 400;
    glViewport(100, 100, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-1.0, 1.0, -1.5, 0.5, 0.8, 10.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(7.0, 4.5, 4.0, 4.5, 4.5, 2.0, 6.0, -3.0, 2.0);
}

/*  Main Loop
*  Open window with initial window size, title bar,
*  RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{    
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutCreateWindow(argv[0]);
    myinit();
    glutReshapeFunc(myReshape);
    glutDisplayFunc(display);
    glutMainLoop();
    
    return 0;             /* ANSI C requires main to return int. */
}

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.