Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Evaluators, Curves and Surfaces - Introduction to Computer Graphics - Lecture Notes, Study notes of Computer Graphics

Evaluators, Curves and Surfaces, OneDimensional Evaluators, Evenly Spaced Coordinate Values, Evaluating a Two Dimensional Evaluator, Bezier Surface, Types of Control Points are key learning points in this lecture handout.

Typology: Study notes

2011/2012

Uploaded on 11/10/2012

taariq
taariq 🇵🇰

4.4

(15)

59 documents

1 / 11

Related documents


Partial preview of the text

Download Evaluators, Curves and Surfaces - Introduction to Computer Graphics - Lecture Notes and more Study notes Computer Graphics in PDF only on Docsity! Virtual University Computer Graphics CS602 488 Introduction to Computer Graphics Lecture 44 Evaluators, curves and Surfaces 44.1 Evaluators A Bézier curve is a vector-valued function of one variable C(u) = [X(u) Y(u) Z(u)] where u varies in some domain (say [0,1]). A Bézier surface patch is a vector-valued function of two variables S(u,v) = [X(u,v) Y(u,v) Z(u,v)] Where u and v can both vary in some domain. The range isn't necessarily three-dimensional as shown here. You might want two-dimensional output for curves on a plane or texture coordinates, or you might want four-dimensional output to specify RGBA information. Even one-dimensional output may make sense for gray levels. For each u (or u and v, in the case of a surface), the formula for C() (or S()) calculates a point on the curve (or surface). To use an evaluator, first define the function C() or S(), enable it, and then use the glEvalCoord1() or glEvalCoord2() command instead of glVertex*(). This way, the curve or surface vertices can be used like any other vertices - to form points or lines, for example. In addition, other commands automatically generate series of vertices that produce a regular mesh uniformly spaced in u (or in u and v). One- and two-dimensional evaluators are similar, but the description is somewhat simpler in one dimension, so that case is discussed first. 44.2 One-Dimensional Evaluators This section presents an example of using one-dimensional evaluators to draw a curve. It then describes the commands and equations that control evaluators. One-Dimensional Example: A Simple Bézier Curve The program shown in Example 1 draws a cubic Bézier curve using four control points, as shown in Figure 1. Virtual University Computer Graphics CS602 489 Figure 1 : Bézier Curve Example 1 : Bézier Curve with Four Control Points: #include <GL/gl.h> #include <GL/glu.h> #include <stdlib.h> #include <GL/glut.h> 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); } void display(void) { int i; glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glBegin(GL_LINE_STRIP); for (i = 0; i <= 30; i++) glEvalCoord1f((GLfloat) i/30.0); glEnd(); /* The following code displays the control points as dots. */ glPointSize(5.0); glColor3f(1.0, 1.0, 0.0); glBegin(GL_POINTS); for (i = 0; i < 4; i++) glVertex3fv(&ctrlpoints[i][0]); glEnd(); glFlush(); } void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho(-5.0, 5.0, -5.0*(GLfloat)h/(GLfloat)w, Virtual University Computer Graphics CS602 492 The command glMap1() defines a one-dimensional evaluator that uses these equations. void glMap1{fd}(GLenum target, TYPEu1, TYPEu2, GLint stride, GLint order, const TYPE*points); Defines a one-dimensional evaluator. The target parameter specifies what the control points represent, as shown in Table 1, and therefore how many values need to be supplied in points. The points can represent vertices, RGBA color data, normal vectors, or texture coordinates. For example, with GL_MAP1_COLOR_4, the evaluator generates color data along a curve in four-dimensional (RGBA) color space. You also use the parameter values listed in Table 1 to enable each defined evaluator before you invoke it. Pass the appropriate value to glEnable() or glDisable() to enable or disable the evaluator. The second two parameters for glMap1*(), u1 and u2, indicate the range for the variable u. The variable stride is the number of single- or double-precision values (as appropriate) in each block of storage. Thus, it's an offset value between the beginning of one control point and the beginning of the next. The order is the degree plus one, and it should agree with the number of control points. The points parameter points to the first coordinate of the first control point. Using the example data structure for glMap1*(), use the following for points: (GLfloat *)(&ctlpoints[0].x) Table 1 : Types of Control Points for glMap1*() Virtual University Computer Graphics CS602 493 More than one evaluator can be evaluated at a time. If you have both a GL_MAP1_VERTEX_3 and a GL_MAP1_COLOR_4 evaluator defined and enabled, for example, then calls to glEvalCoord1() generate both a position and a color. Only one of the vertex evaluators can be enabled at a time, although you might have defined both of them. Similarly, only one of the texture evaluators can be active. Other than that, however, evaluators can be used to generate any combination of vertex, normal, color, and texture-coordinate data. If more than one evaluator of the same type is defined and enabled, the one of highest dimension is used. Use glEvalCoord1*() to evaluate a defined and enabled one-dimensional map. void glEvalCoord1{fd}(TYPE u); void glEvalCoord1{fd}v(TYPE *u); Causes evaluation of the enabled one-dimensional maps. The argument u is the value (or a pointer to the value, in the vector version of the command) of the domain coordinate. For evaluated vertices, values for color, color index, normal vectors, and texture coordinates are generated by evaluation. Calls to glEvalCoord*() do not use the current values for color, color index, normal vectors, and texture coordinates. glEvalCoord*() also leaves those values unchanged. 44.4 Defining Evenly Spaced Coordinate Values in One Dimension You can use glEvalCoord1() with any values for u, but by far the most common use is with evenly spaced values, as shown previously in Example 1. To obtain evenly spaced values, define a one-dimensional grid using glMapGrid1*() and then apply it using glEvalMesh1(). void glMapGrid1{fd}(GLint n, TYPEu1, TYPEu2); Defines a grid that goes from u1 to u2 in n steps, which are evenly spaced. Virtual University Computer Graphics CS602 494 void glEvalMesh1(GLenum mode, GLint p1, GLint p2); Applies the currently defined map grid to all enabled evaluators. The mode can be either GL_POINT or GL_LINE, depending on whether you want to draw points or a connected line along the curve. The call has exactly the same effect as issuing a glEvalCoord1() for each of the steps between and including p1 and p2, where 0 <= p1, p2 <= n. Programmatically, it's equivalent to the following: glBegin(GL_POINTS); /* OR glBegin(GL_LINE_STRIP); */ for (i = p1; i <= p2; i++) glEvalCoord1(u1 + i*(u2-u1)/n); glEnd(); except that if i = 0 or i = n, then glEvalCoord1() is called with exactly u1 or u2 as its parameter. 44.5 Two-Dimensional Evaluators In two dimensions, everything is similar to the one-dimensional case, except that all the commands must take two parameters, u and v, into account. Points, colors, normals, or texture coordinates must be supplied over a surface instead of a curve. Mathematically, the definition of a Bézier surface patch is given by ∑∑ = = = n i m j ij m j n i PvBBvuS 0 0 )(),( where Pij are a set of m*n control points, and the Bi are the same Bernstein polynomials for one dimension. As before, the Pij can represent vertices, normals, colors, or texture coordinates. The procedure to use two-dimensional evaluators is similar to the procedure for one dimension. 1. Define the evaluator(s) with glMap2*(). 2. Enable them by passing the appropriate value to glEnable(). 3. Invoke them either by calling glEvalCoord2() between a glBegin() and glEnd() pair or by specifying and then applying a mesh with glMapGrid2() and glEvalMesh2(). 44.6 Defining and Evaluating a Two-Dimensional Evaluator Use glMap2*() and glEvalCoord2*() to define and then invoke a two-dimensional evaluator. void glMap2{fd}(GLenum target, TYPEu1, TYPEu2, GLint ustride, GLint uorder, TYPEv1, TYPEv2, GLint vstride, GLint vorder, TYPE points); The target parameter can have any of the values in Table 1, except that the string MAP1 is replaced with MAP2. As before, these values are also used with glEnable() to enable the corresponding evaluator. Minimum and maximum values for both u and v are provided as u1, u2, v1, and v2. The parameters ustride and vstride indicate the number of single- or double-precision values (as appropriate) between independent settings for these values, allowing users to select a subrectangle of control points out of a much larger array. For example, if the data appears in the form GLfloat ctlpoints[100][100][3]; Virtual University Computer Graphics CS602 497 or for (i = nu1; i <= nu2; i++) { /* mode == GL_LINE */ glBegin(GL_LINES); for (j = nv1; j <= nv2; j++) glEvalCoord2(u1 + i*(u2-u1)/nu, v1+j*(v2-v1)/nv); glEnd(); } for (j = nv1; j <= nv2; j++) { glBegin(GL_LINES); for (i = nu1; i <= nu2; i++) glEvalCoord2(u1 + i*(u2-u1)/nu, v1+j*(v2-v1)/nv); glEnd(); } or for (i = nu1; i < nu2; i++) { /* mode == GL_FILL */ glBegin(GL_QUAD_STRIP); for (j = nv1; j <= nv2; j++) { glEvalCoord2(u1 + i*(u2-u1)/nu, v1+j*(v2-v1)/nv); glEvalCoord2(u1 + (i+1)*(u2-u1)/nu, v1+j*(v2-v1)/nv); glEnd(); } Example 3 shows the differences necessary to draw the same Bézier surface as Example 2, but using glMapGrid2() and glEvalMesh2() to subdivide the square domain into a uniform 8x8 grid. This program also adds lighting and shading, as shown in Figure 3. Figure 3 : Lit, Shaded Bézier Surface Drawn with a Mesh Example 3 : Lit, Shaded Bézier Surface Using a Mesh: void initlights(void) { GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0}; GLfloat position[] = {0.0, 0.0, 2.0, 1.0}; GLfloat mat_diffuse[] = {0.6, 0.6, 0.6, 1.0}; GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat mat_shininess[] = {50.0}; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); Virtual University Computer Graphics CS602 498 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); glLightfv(GL_LIGHT0, GL_POSITION, position); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(85.0, 1.0, 1.0, 1.0); glEvalMesh2(GL_FILL, 0, 20, 0, 20); glPopMatrix(); glFlush(); } void init(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &ctrlpoints[0][0][0]); glEnable(GL_MAP2_VERTEX_3); glEnable(GL_AUTO_NORMAL); glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0); initlights(); }
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved