OpenGL programming easy to get started nurbs curves and surfaces (1)

Source: Internet
Author: User

In the previous section, the general curves and surfaces are drawn, and the nurbs curves and surfaces are plotted in this section.

Example 11: This example draws two nurbs surfaces of the same shape, the difference being a wireframe, and one is composed of a solid polygon. You can see the difference after running, as shown in Figure 13.

#include <windows.h>
#include <GL/glut.h>
Glunurbsobj *THENURB1;
Glunurbsobj *thenurb2;

Glfloat Ctrlpoints[5][5][3] = {{ -3,0.5,0},{-1,1.5,0},{-2,2,0},{1,-1,0},{-5,0,0}},
{{ -3,0.5,-1},{-1,1.5,-1},{-2,2,-1},{1,-1,-1},{-5,0,-1}},
{{ -3,0.5,-2},{-1,1.5,-2},{-2,2,-2},{1,-1,-2},{-5,0,-2}},
{{ -3,0.5,-3},{-1,1.5,-3},{-2,2,-3},{1,-1,-3},{-5,0,-3}},
{{ -3,0.5,-4},{-1,1.5,-4},{-2,2,-4},{1,-1,-4},{-5,0,-4}}};//control point

Glfloat mat_diffuse[] = {1.0,0.5,0.1,1.0};
Glfloat mat_specular[] = {1.0,1.0,1.0,1.0};
Glfloat mat_shininess[] = {100.0};
Glfloat light_position[] = {0.0,-10.0,0.0,1.0};

void Myinit (void)
{
Glclearcolor (1.0,1.0,1.0,0.0);/Set Background color

/* Specify material parameters for the illumination model * *
GLMATERIALFV (Gl_front,gl_diffuse,mat_diffuse);
GLMATERIALFV (Gl_front,gl_specular,mat_specular);
GLMATERIALFV (gl_front,gl_shininess,mat_shininess);

GLLIGHTFV (gl_front,gl_position,light_position)//Set Lighting parameters
Gllightmodeli (gl_light_model_two_side,gl_true)//Set Lighting model parameters

/* Activate the light * *
Glenable (gl_lighting);
Glenable (GL_LIGHT0);

Gldepthfunc (gl_lequal);
Glenable (gl_depth_test);
Glenable (gl_lequal);
Glenable (Gl_auto_normal);
Glenable (gl_normalize);

/* Set SPECIAL effects * *
Glblendfunc (Gl_src_alpha,gl_one_minus_src_alpha);
Glhint (Gl_line_smooth_hint,gl_dont_care);
Glenable (Gl_blend);

Glfrontface (GL_CW);
Glshademodel (Gl_smooth);
Glenable (Gl_line_smooth);

THENURB1 = Glunewnurbsrenderer ();//Create a NURBs object TheNurb1
Glunurbsproperty (thenurb1,glu_sampling_tolerance,25.0);
Glunurbsproperty (Thenurb1,glu_display_mode,glu_outline_polygon);

THENURB2 = Glunewnurbsrenderer ();//Create a NURBs object TheNurb2
Glunurbsproperty (thenurb2,glu_sampling_tolerance,25.0);
Glunurbsproperty (Thenurb2,glu_display_mode,glu_fill);
}

int spin = 0;

/* Receive keyboard instruction * *
static void MyKey (unsigned char key,int x,int y)
{
Switch (key)
{
Case ' d ':
Spin = spin + 1;
Glrotatef (spin,1.0,1.0,0.0);
Glutpostredisplay ();
Break
Case 27:
Exit (0);
Default
Break
}
}

/* Draw Surface * *
void Mydisplay (void)
{
Glfloat knots[10] = {0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0};

Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit);
Glrotatef (50.0,1.0,1.0,0.0);

/* First Surface * *
Glpushmatrix ();
Gltranslatef (1.0,0.0,0.0);
Glubeginsurface (THENURB1);
/* Define surface shape * *
Glunurbssurface (Thenurb1,10,knots,10,knots,5*3,3,&ctrlpoints[0][0][0],5,5,gl_map2_vertex_3);
Gluendsurface (THENURB1);
Glpopmatrix ();

/* Second Surface * *
Glpushmatrix ();
Gltranslatef (7.0,0.0,0.0);
Glubeginsurface (THENURB2);
/* Define surface shape * *
Glunurbssurface (Thenurb2,10,knots,10,knots,5*3,3,&ctrlpoints[0][0][0],5,5,gl_map2_vertex_3);
Gluendsurface (THENURB2);
Glpopmatrix ();

Glutswapbuffers ();
}

void Myreshape (Glsizei w,glsizei h)
{
Glviewport (0,0,W,H);
Glmatrixmode (gl_projection);
Glloadidentity ();
Gluperspective (50.0, (Glfloat) w/(glfloat) h,1.0,15.0);
Glmatrixmode (Gl_modelview);
Glloadidentity ();
Gltranslatef (0.0,0.0,-9.0);
}

int main (int argc,char * * argv)
{
/* Initialize * *
Glutinit (&AMP;ARGC,ARGV);
Glutinitdisplaymode (glut_double| glut_rgb| Glut_depth);
Glutinitwindowsize (600,400);
Glutinitwindowposition (200,200);
 
/* Create window * *
Glutcreatewindow ("NURBS surface");

/* Draw and display * *
Myinit ();
Glutkeyboardfunc (MyKey);
Glutreshapefunc (Myreshape);
Glutdisplayfunc (Mydisplay);

/* Enter GLUT event handling cycle * *
Glutmainloop ();
return (0);
}


· glunurbsobj* Glnewnurbsrenderer () creates a NURBs object and returns a pointer to the object. If there is not enough memory allocated to the object, the return value is 0.

void Glunurbsproperty (glunurbsobj* nobj, Glenum property, Glfloat value) sets the NURBs attribute.

Nobj a pointer to a NURBs object.

Property you want to set.

Value sets the values of the specified property.

Glbeginsurface and Gluendsurface two functions together define the definition of a nurbs surface. The return value is void and the parameters are glunurbsobj* nobj, which is a pointer to a NURBs object.

void Glunurbssurface (Glunurbsobj *nobj, Glint Knot_count, Glfloat tknot_count, Glfloat *tknot, Glint s_stride, Glint T_st Ride, Glfloat *ctlarry, Glint sorder, Glint torder,glenum type) define NURBS surface shapes.

Nobj a pointer to a NURBs object.

Sknot_count the number of nodes in the U-direction parameterization.

Sknot the value of a non-descending node in the U-direction parameterization.

The number of nodes in the Tknot_count parameterized v direction.

Tknot a non-descending node value in the parameterized v direction.

S_stride the offset of the adjacent control points in the U direction in Ctlarry.

T_stride the offset of the adjacent control points in the V direction in Ctlarry.

An array of Ctlarrynurbs control points.

The order of NURBS on the Sorder parametric U-direction is 1 higher than that of the number of dimensions.

The order of NURBS in the Torder parametric v direction is 1 higher than that of the dimension.

Type surface types.


Figure 13: NURBs surface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.