opengl curves & surfaces. where u varies in some domain (say [0,1]). a bezier surface patch is a...
Post on 20-Dec-2015
222 views
TRANSCRIPT
OpenGL Curves & OpenGL Curves & SurfacesSurfaces
Where u varies in some domain (say [0,1]).
A Bezier surface patch is a vector-valued function of two variables
EvaluatorsEvaluators
A Bezier curve is a vector-valued function of 1 variable
OpenGL EvaluatorOpenGL Evaluator
• Define the function C( ) or S( )
• Enable it
• Use glEvalCoord1( ) or glEvalCoord2( ) instead of glVertex*( )
• The curve or surface is used just as any other vertices are used.
Example bezcurve.cExample bezcurve.c(Note: the one I handed out is slightly different than this one.)
Control Points
Bézier Curve
bezcurve.c (part 1)bezcurve.c (part 1)
GLfloat ctrlpoints[4][3] = {
{-4.0, -4.0, 0.0},
{-2.0, 4.0, 0.0},
{2.0, -4.0, 0.0},
{4.0, 4.0, 0.0}};
void init(void){
glClearColor(0.0, 0.0, 0.0,, 0.0);
glShadeModel(GL_FLAT);
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]);
glEnable(GL_MAP1_VERTEX_3);}
glMap1{fd} ( … )glMap1{fd} ( … )• GLenum target (see next slide)
• TYPE u1 – starting value of u
• TYPE u2 – ending value of u
• GLint stride – number of single or double precision values in each block of storage.
• GLint order – degree plus 1 (should agree with the number of control points)
• const TYPE * points – pointer to control points
glMap1 target parameterglMap1 target parameter
• GL_MAP1_VERTEX_3 : x,y,z vertex coordinates
• GL_MAP1_VERTEX_4 : x,y,z,w vertex coordinates
• GL_MAP1_INDEX : color index• GL_MAP1_COLOR_4 : R,G,B,A• GL_MAP1_NORMAL : normal
coordinates• GL_MAP1_TEXTURE_COORD_1 :
s texture coordinate
• GL_MAP1_TEXTURE_COORD_2 : s,t texture coordinates
• GL_MAP1_TEXTURE_COORD_3 : s,t,r texture coordinates
• GL_MAP1_TEXTURE_COORD_4 : s,t,r,q texture coordinates
glEnable(GL_MAP1_VERTEX_3)glEnable(GL_MAP1_VERTEX_3)
• Enables the one-dimensional evaluator for three dimensional vertices.
bezcurve.c (part 2) display functionbezcurve.c (part 2) display function
void display(void) {
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0); // set color to white
glBegin(GL_LINE_STRIP);
for(i=0; i<=30; i++) // loop 31 times
glEvalCoord1f((GLfloat) i/30.0); // call evaluator
glEnd( );
bezcurve.c (part 3) bezcurve.c (part 3)
/* the following code displays the control points as dots */
glPointSize(5.0);
glColor3f(1.0, 1.0, 0.0); // set color to yellow
glBegin(GL_POINTS);
for(i=0; i<4; i++)
glVertex3fv(&ctrloints[i][0]); // Draw a point
glEnd( );
glFlush( );
Bezcurve.c (part 4)Bezcurve.c (part 4)
• A reshape function that uses glOrtho for orthographic projections and an identity matrix for model view.
• A normal main function like all other examples.
Modification for hand-outModification for hand-out
• I changed the color of the bezier curve to red and added another bezier curve with different parameters.
• Only 4 steps in the evaluator loop.
• (This new curve is drawn in white)
• Note the lack of smoothness.
• Note the same start and end points.
Additional codeAdditional code
glClear(GL_COLOR_BUFFER_BIT); // clear screenglColor3f(1.0, 1.0, 1.0); // set color to whiteglBegin(GL_LINE_STRIP); // start a line strip for(i=0; i<=4; i++); // loop FIVE times glEvalCoord1f((GLfloat)i/4.0); // calc a pointglEnd( );glColor3f(1.0, 0.0, 0.0); // set color to redglBegin(GL_LINE_STRIP); // start a line strip for(i=0; i<=30; i++); // loop 31 times glEvalCoord1f((GLfloat)i/30.0); // calc a pointglEnd( );
0 : 0.0/4.0 = 0.0
2 : 2.0/4.0 = 0.50
1 : 1.0/4.0 = 0.25
3 : 3.0/4.0 = 0.75
4 : 4.0/4.0 = 1.0
15 : 15.0/30.0=0.50
Calculated
Points
DEMOSDEMOS
• Bezier /wwwf2003/examples/class
• Curves /wwwf2003/public/curves
Another way to do even spacingAnother way to do even spacing
• void glMapGrid1{fd}(GLint n, TYPE u1, TYPE u2);
• Void glEvalMesh1(GLenum mode, GLint p1, GLint p2); (equivalent to following)
glBegin(GL_POINTS); // or glBegin(GL_LINE_STRIP);
for (i=p1; i<=p2; i++)
glEvalCoord1(u1+i*(u2-u1)/n);
glEnd();
2D Bezier Surfaces2D Bezier Surfaces
Code is bezmeshnolight.cCode is bezmeshnolight.c
bezmesh.c with lightingbezmesh.c with lighting
bezsurf.cbezsurf.c