Mini Project On Bezier Curves Using Visual Studio.
Mini Project On Bezier Curves Using Visual Studio.
Mini Project On Bezier Curves Using Visual Studio.
CHAPTER 1
INTRODUCTION
Bezier Curves
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.
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.
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
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
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.
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);
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);
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(…);
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();
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.
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.
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.
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.
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’.
CHAPTER 5
CONCLUSION
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
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.
Or maybe you can implement a Bezier curve of degree 4 or 7. Bezier Curves of different degrees
are shown below.
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
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
#include "stdafx.h"
#include <GL/glut.h>
#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();
}
ncpts++;
glColor3f(0.0,0.0,0.0);
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex3f(wx,wy,0.0);
glEnd();
glFlush();
}
}
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();
}