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

Source: Internet
Author: User
Tags set background

Example 12: Draw a color curve, the curve closed synthesis circle. Draw 8 points at the edge of the curve, as shown in Figure 14.

#include <windows.h>
#include <GL/glut.h>

Glunurbsobj *thenurb;

Glfloat Ctrlpoints[12][3] = {{4,0,0},{2.828,2.828,0},{0,4,0},{-2.828,2.828,0},
{ -4,0,0},{-2.828,-2.828,0},{0,-4,0},{2.828,-2.828,0},
{4,0,0},{2.828,2.828,0},{0,4,0},{2.828,2.828,0}};//Control point

Glfloat color[12][3]={{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{-1.0,1.0,0.0},
{ -1.0,0.0,0.0},{-1.0,-1.0,0.0},{0.0,-1.0,0.0},{1.0,-1.0,0.0},
{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{1.0,1.0,0.0}};

Glfloat knots[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

void Myinit (void)
{
Glclearcolor (1.0,1.0,1.0,0.0);/Set Background color
Thenurb = Glunewnurbsrenderer ();//Create a NURBs object thenurb
Glunurbsproperty (thenurb,glu_sampling_tolerance,10);
}

/* Draw Curve * *
void Mydisplay (void)
{
int i;

Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit);
glcolor3f (0.0,0.0,0.0);

Gllinewidth (3.0);

/* Draw Curve * *
Glubegincurve (THENURB);
Glunurbscurve (Thenurb,15,knots,3,&ctrlpoints[0][0],3,gl_map1_vertex_3);
Glunurbscurve (thenurb,15,knots,3,&ctrlpoints[0][0],3,gl_map1_color_4);
Gluendcurve (THENURB);

/* Draw Point * *
glcolor3f (1.0,0.0,0.0);
Glpointsize (5.0);
Glbegin (gl_points);
for (i = 0;i < 8;i++)
GLVERTEX2FV (&ctrlpoints[i][0]);
Glend ();

Glutswapbuffers ();
}

void Myreshape (Glsizei w,glsizei h)
{
Glviewport (0,0,W,H);
Glmatrixmode (gl_projection);
Glloadidentity ();

if (w <=h)
Glortho ( -10.0,10.0,-10.0* (glfloat) h/(glfloat) w,10.0* glfloat (h/) glfloat);
Else
Glortho ( -10.0* (glfloat) w/(glfloat) h,10.0* glfloat (w/) glfloat);

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 curve");

/* Draw and display * *
Myinit ();

Glutreshapefunc (Myreshape);
Glutdisplayfunc (Mydisplay);

Glutmainloop ();
return (0);
}


Glubegincurve,gluendcurve to qualify nurbs surfaces. The return value is void and the parameters are glunurbsobj* nobj, which is a pointer to a NURBs object.

void Glunurbscurve (Glunurbsobj *nobj, Glint nknots, glfloat *knot, Glint stride, Glfloat *ctlarray, GLint order,glenum ty PE) to define the shape of the curve.

Nobj a pointer to a NURBs object.

Nknots number of nodes, node points equals control points plus order.

Knot nknots Array Non-descending node value.

Stride the offset of adjacent control points.

Ctlarry A pointer to an array of control points for NURBS.

Order NURBs Curve, the order number is 1 larger than the dimension.

Type surface types.


Figure 14: NURBs curve

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.