Mini Project On Bezier Curves Using Visual Studio.

You are on page 1of 22

BEZIER CURVES Page| 1

CHAPTER 1

INTRODUCTION

Bezier Curves

In mathematical field of numerical analysis, a BEZIER CURVE is a parametric curve important


in Computer Graphics and related fields. Bezier curves were widely publicized in 1962 by a
French Engineer Pierre Bezier who used them to design automobile bodies. The curves were first
developed in 1959 by Paul de Castlejau using the Castlejau’s algorithm, a numerically stable
method to evaluate Bezier Curves.
In Vector graphics, Bezier Curves are an important tool used to model smooth curves that can be
scaled indefinitely. “Paths”, as they are commonly referred to in image manipulation programs
such as InkScape, Adobe Illustrator, Adobe Photoshop, and GIMP are combinations of Bezier
Curves patched together. Paths are not bound by the limits of rasterized images and are intuitive
to modify.

Bezier curves have some interesting properties, unlike other classes of curves; they can fold over
on themselves. They can also be joined together to form smooth, continuous shapes. Figure 1
shows an example of a cubic Bezier Curve with a smooth curvature.

Fig: 1.1 Cubic Bezier Curve

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES Page| 2

Before I go with the technical details of how to write a program for the creation of Bezier Curve,
let me first give you a rough idea of how to construct a Bezier Curve graphically. To construct a
cubic Bezier curve we need four control points. Depending on the alignment of these points the
curve gets constructed. We can understand this clearly by looking at the figure below.

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES Page| 3

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES Page| 4

Fig: 1.2 Subdivision of a cubic Bezier Curve.

Here P0,P1,P2,P3 are the four control points and t=.4 is the interval at which the curve is getting
created.

CHAPTER 2

REQUIREMENTS

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES Page| 5

2.1 Hardware Requirements:


Pentium or higher processor, 16 MB or more RAM, a standard keyboard, a Microsoft
compatible mouse and a VGA monitor. If the user wants to save the created files a
secondary storage medium can be used.

2.2 Software Requirements:


 Microsoft Visual C++ with OpenGL (Express\Standard Edition).
 Windows XP or higher version.
 .Net framework 2.0 (for Visual Studio 2005).

CHAPTER 3
DESIGN

This project is developed using Microsoft Visual C++ with OpenGL as an academic project
using keyboard and mouse. This project is implemented by making use of extensive use of
library functions offered by graphic package of ‘OpenGL’. The most important of all is the

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES Page| 6

OpenGL Evaluators, without which my program wouldn’t be possible to implement. A list of


standard library functions that are used are given below. A number of user defined functions also
have been used and a summary of those functions follows this list of standard library functions.
Let me first start by explaining about the OpenGL Evaluators.

OpenGL Evaluators
• The OpenGL Evaluator function allows us to use a polynomial mapping to produce
vertices, normals, texture coordinates, and colors.
• These calculated values are then passed on to the processing pipeline as if they had been
directly specified.
• The Evaluators functions are also the basis for the NURBS(Non-Uniform Rational B-
Spline) functions which allows us to define curves and surfaces.
• These NURBS function can be used to generate non uniform spacing of points.
• Any polynomial form can be converted to Bezier form by proper generation of control
points.
• Thus NURBS function allows finer control of the space and rendering of the surface.

glMap1f(type, u_min, u_max, stride, order, point_array);


The definition of a One dimensional evaluator is as shown above. Where
• ‘u_min’ and ‘u_max’ defines the domain for parameter ‘u’.
• Stride: The number of floats or doubles between the beginning of one control point and
the beginning of the next one in the data structure referenced in points. The only
constraint is that the values for a particular control point must occupy contiguous
memory locations.
• Order: It’s the number of control points(has to be positive).
• Points: A pointer to the array of control points.

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES Page| 7

glMapGrid1f(Glint un, GLfloat u1, GLfloat u2);


• glMapGrid and glEvalMesh are used together to efficiently generate and evaluate a series
of evenly spaced map domain values. glMapGrid1 specifies the linear grid mapping
between ‘un’ integer grid coordinates to ‘u1’& ‘u2’ floating point evaluation map
coordinates.
• ‘un’: Specifies the number of partition in the grid range interval.
• ‘u1’& ‘u2’: Specifies the mapping for the integer grid domain values(i=0 to i=un).

glEvalMesh1(GLenum mode, Glint i1, Glint i2);


• glEvalMesh steps through the integer domain of 1 or 2 dimensional grid whose range is
the domain of the evaluation maps specified by glMap1 and glMap2.
• ‘mode’: specifies whether to compute a one-dimensional mesh of points or lines.
Symbolic constants GL_POINT and GL_LINE are accepted.
• ‘i1’ & ‘i2’: Specify the first and last integer values for grid domain variable ‘i’.

glutInit (…);
This function defines the interaction between the windowing system and the OpenGL.
Declaration:
glutInit(&argc, argv);

glutInitDisplayMode (…);
Here we specify RGB color system and also double buffering.
Declaration:
glutInitDisplayMode(GLUT_RGB);

glutInitWindowSize(…);
This function specifies a window in the top left corner of the display.
Declaration:
glutInitWindowSize(width, height);

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES Page| 8

glutCreateWindow(“…”);
This creates an OpenGL window using the glut function where the title at the top of the window
is given by the string inside the parameter of the above function.
Declaration:
glutCreateWindow(“Use of 2D shapes and Transformaions”);

glutReshapeFunc(…);
The reshape event is generated whenever the window is resized, such as user interaction
Declaration:
glutReshapeFunc(myReshape);

glutDisplayFunc(…) ;
Graphics are sent to the screen through a function called the display call back, here the function
named ‘func’ will be called whenever the windowing system determines that OpenGL window
needs to be redisplayed.
Declaration:
glutDisplayFunc(display);

glutIdleFunc(…);
glutIdle function checks idle call back i.e it can perform a background processing task or
continuous animation when windows system events are not being received.
Declaration:
glutIdleFunc(myIdleFunc);

glutMouseFunc(…);
This function handles mouse click events.
Declaration:
glutMouseFunc(myMouse);

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES Page| 9

glutKeyboardFunc(…);
This function handles keyboard events.
Declaration:
glutKeyboardFunc(myKey);

glClearColor(…);
glColor3f(…);
In RGB color we have three attributes to set. The first is the clear color, which is set to black and
we can select the rendering color for points by setting the state of the variable to black by using
the following function call.
Declaration:
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(0.0,0.0,0.0);

void myMouse(…);
This is the mouse callback function. Within the callback function, we define the actions that we
want to take place if the specified event occurs.
Declaration:
void myMouse(int button, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
:
}

void myKey(…);

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES P a g e | 10

This is the keyboard callback function. Within the callback function, we define the actions that
we want to take place if the specified event occurs.
Declaration:
void myKey(unsigned char key, int x, int y)
{
case ‘b’: case ‘B’: drawcurves();
:
case 'Q': exit(0);
break;
}

void drawcurves(…);
The drawcurves function is the heart of the program, which is responsible for the creation of
bezier curves. In this function we use OpenGL Evaluators like glMap, glMapGrid, and
glEvalMesh together in a ‘for’ loop of the range 0 to ‘ncpts’, where ‘ncpts’ is the maximum
number of points to be plotted on the screen.
Declaration:
void drawcurves( )
{
:
}

glutPostRedisplay();

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES P a g e | 11

This ensures that the display will be drawn only once each time the program goes through the
event loop. It avoids extra or unnecessary screen drawings by setting a flag inside GLUT’s main
loop indicating that the display needs to be redrawn.

void display(void)
This function is the callback function for’ DisplayFunc’. This function is mandatory because
GLUT requires that all programs have a display callback.

void myReshape(int w,int h)


This function is used whenever the window is resized. In our program we need the required
display callback for drawing because the primitives are generated when a mouse event occurs.

CHAPTER 4
TESTING AND RESULTS

The project designed has been tested for its working, and is found to be working properly
to meet all its requirements. The project has been found to be giving correct outputs to the inputs
that were given, like the appropriate displaying of the output, and following appropriate
conditions for termination of the program etc...
The designed project has been tested for errors
and has been found to be meeting all the requirements of design with which it was started.
Thus the project is declared to be working properly.

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES P a g e | 12

Fig 4.1: Building the solution.


The above figure shows where to click to build solution.

Fig 4.2: Output of build.

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES P a g e | 13

The above figure shows the output after it has been compiled, as it contains 0 errors and at the
lower left corner we have ‘Build Succeeded’, so it is now ready for debugging.

Fig 4.3: Start debugging.

After Building the solution we need to Debug it before starting the execution. To debug the solution we
need to go to ‘Debug’ option, a drop down menu appears as shown in the figure above; now select ‘Start
Debugging’ option. At the end, the execution window opens and we can implement the Bezier Curves.

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES P a g e | 14

Fig 4.4: A four point simple Bezier Curve.

After clicking on the ‘Start Debugging’ option initially we a get a white screen which acts as a
platform to draw our curves. By using mouse we specify the location of the points and press ‘b’.
A curve as shown in the figure above gets created. We can erase the curve by pressing ‘e’. To
clear the screen we need to press ‘c’. To exit from the output window we need to press ‘q’.

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES P a g e | 15

Fig 4.5: A 16 point Bezier Curve.

CHAPTER 5
CONCLUSION

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES P a g e | 16

Through this project we have made a sincere attempt to develop an OpenGL graphics
package which meets all the necessary requirements that were set out for us in our syllabus. The
main idea behind this project is to bring together the ideas of Mathematical curves and Graphics
programming into one complex unit which can be implemented. One more objective of
implementing this project is to highlight the importance of the Bezier Curves in Graphics
Programming. Our project is not only user friendly and interactive but also gives worthy
information about the construction of Bezier Curves with minimum of technical jargon.

CHAPTER 6
FUTURE ENHANCEMENTS

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES P a g e | 17

This project is mainly concerned with the design and implementation of Cubic Bezier
Curves. However implementation of higher order Bezier Curves is not an impossible task. One
more thing that I have to mention for the future enhancement is that the increase in the number of
control points. Although I have used the help of OpenGL Evaluators in my program, it is not at
all a compulsion. With higher order curves we can even create complex drawings like the one
shown below.

Fig: 6.1 Font definition using Bezier Curves.

Or maybe you can implement a Bezier curve of degree 4 or 7. Bezier Curves of different degrees
are shown below.

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES P a g e | 18

Fig: 6.2 Bezier Curves of degrees up to seven.

Although this might get a little complicated but not at all impossible. We need to do some minor
adjustment in our algorithm and we need to write a whole new source code for a 7th order Bezier
curve.

BIBLIOGRAPHY

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES P a g e | 19

Books:
1. Edward Angel: - Interactive Computer Graphics: A Top-Down Approach with OpenGL,
5th Edition, Addison-Wesley, 2008.
2. Computer Graphics Principles & Practice: Foley, van Dam, Feiner, Huges.:- second
edition

Web Resources:
1. http://www.opengl.org
2. http://www.wikipedia.org
3. http://www.sulaco.org

APPENDIX – SOURCE CODE

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES P a g e | 20

#include "stdafx.h"
#include <GL/glut.h>

void keyboard(unsigned char key,int x,int y);

#define MAXCPTS 25

GLfloat cpts[MAXCPTS][3];
int ncpts=0;
static int width=500,height=500;

void drawcurves()
{
int i;
for(i=0;i<ncpts-3;i+=3)
{
glMap1f(GL_MAP1_VERTEX_3,0.0,1.0,3,4,cpts[i]);//sets up an evaluator for
//range 0 to 1
glMapGrid1f(30,0.0,1.0);//sets up a equally spaced grid of 30 partition betn 0&1
glEvalMesh1(GL_LINE,0,30);//renders in mode GL_LINE all enabled evaluators
//from 0 to 30
}
glFlush();
}

void display()
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
for(i=0;i<ncpts;i++)
glVertex3fv(cpts[i]);
glEnd();
glFlush();
}

void mouse(int button,int state,int x,int y)


{
float wx,wy;
if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
{
wx=(2.0*x)/(float)(width-1)-1.0;
wy=(2.0*(height-1-y))/(float)(height-1)-1.0;
if(ncpts==MAXCPTS)return;
cpts[ncpts][0]=wx;
cpts[ncpts][1]=wy;
cpts[ncpts][2]=0.0;
Dept. of CSE, EPCET 2008 - 09
BEZIER CURVES P a g e | 21

ncpts++;
glColor3f(0.0,0.0,0.0);
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex3f(wx,wy,0.0);
glEnd();
glFlush();
}
}

void keyboard(unsigned char key,int x,int y)


{
switch(key)
{
case'q':case'Q':
exit(0);
break;
case'c':case'C':
ncpts=0;
glutPostRedisplay();
break;
case'e':case'E':
glutPostRedisplay();
break;
case'b':case'B':
drawcurves();
break;
}
}
void reshape(int w,int h)
{
width=w;
height=h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,w,h);
}

int main(int argc,char **argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(width,height);
glutCreateWindow("curves");
glutDisplayFunc(display);
glutMouseFunc(mouse);

Dept. of CSE, EPCET 2008 - 09


BEZIER CURVES P a g e | 22

glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(0.0,0.0,0.0);
glPointSize(5.0);
glEnable(GL_MAP1_VERTEX_3);
glutMainLoop();
}

Dept. of CSE, EPCET 2008 - 09

You might also like