4.implementation
TRANSCRIPT
-
7/30/2019 4.Implementation
1/12
Implementation Graphics Package using OpenGL
Dept of CSE, RVCE Sept-Dec 2012 14
4. Implementation:
4.1 Structures used:
There are totally nine structures used for designing our editor which is used to store various values.
The objects of these basic structures are used in various part of the design.
The following listing shows them.
4.1.1 Color :
Color is a basic structure for color parameters such as red, green, blue. The fig 4.1 shows the color
structure. These structures objects are used in other basic structures to store color values required for the
objects.
Fig 4.1 Color structure
4.1.2 GLintpoint:
GLintpoint defines a structure for a pixel. The fig. 4.2 shows the GLintpoint structure. Every tool has
an object of this structure type to store the details of each and every pixel for the corresponding tool. The
attributes of this structure are:
x : holds the x position of the pixel
y : holds the y position of the pixel
size : width of the pixel
r : radius value used in circle drawing
ptrn : pattern type for the pixelcol : holds the color of the pixel
-
7/30/2019 4.Implementation
2/12
Implementation Graphics Package using OpenGL
Dept of CSE, RVCE Sept-Dec 2012 15
end : flag tells whether this pixel is the end of the primitive drawn
pfill : flag tells whether pattern filling is enabled for the pixel.
Fig 4.2 GLintpoint structure
4.1.3 Text:
It is a basic structure for the text tool. Fig 4.3 shows the basic text structure. The text tool uses this
structure object to store the text along with the parameters listed below.
x : holds the x position of the text
y : holds the y position of the textcol : color of the text
ch : holds the text
font : specify the font
Fig 4.3 Text structure
4.1.4 Stack:
This defines the structure for a single level of stack. Fig 4.4 shows the stack structure. The main
display stack uses the objects of this structure for each level. This gives the information about the tool activeon that level and the start and end index for the tool on that level. The attributes of stack structure are:
-
7/30/2019 4.Implementation
3/12
Implementation Graphics Package using OpenGL
Dept of CSE, RVCE Sept-Dec 2012 16
strt: specifies the start index of the tool at this level.
end: specifies the end index.
tool: specifies the tool at this level.
Fig 4.4 Stack structure
4.1.5 Disp_stack :
It is a basic display stack which is used to control the display of the objects in the screen. Fig 4.5
shows the disp_stack structure. The parameters of this structures are :
top : points to the current top position.
st : It is an actual stack that holds the levels defined by the stack structure.
Fig 4.5 Disp_stack structure
4.1.6 Button :
Holds all the controls such as menus, submenus, tool buttons, pattern palette button and color buttons. Fig
4.7 shows the Button structure. The following parameters are used to draw and control the button events.
x , y : x and y position of the button [i.e. top left corner]
w , h : width and height of the button
state : state of the button at a particular instance [i.e. 0 or 1]
highlighted : used in mouse passive motion function to catch the highlighted button
-
7/30/2019 4.Implementation
4/12
Implementation Graphics Package using OpenGL
Dept of CSE, RVCE Sept-Dec 2012 17
num : button number from a particular type
type : type of the button like TOOL, MENU etc
col : color of the button
label : holds the button text
Fig 4.6 Button structure
4.2 Menus :
Menus are created using the structure called Button. The button is a basic structure for all controls in
the editor. The button structure takes Button->type value as MENU, to identify it as menu button. To create a
submenu we will pass the Button->type value as S_MENU.
The Button->num values uniquely identify the button or menu that is highlighted or clicked. The
Menus are positioned by specifying the x, y, w, h values to the button object. To display the menu we have a
function called drawButtons which is continuously called whenever the main window is redisplayed.
To display the submenus the drawButtons function in-turn calls drawSubmenus () function which
displays the submenus of the menu which has been selected. We designed the menu to look like similar to the
menus from the windows menu system.
For odd number of clicks the submenus of the selected menu will be displayed. If we select a menu and
goto another then that menu will be selected and is similar to windows menus.
Our menu system is so flexible that we can add or remove as many as we want easily. The
Button_draw () function will draw the button in proper place and it places the button text in the centre of the
-
7/30/2019 4.Implementation
5/12
Implementation Graphics Package using OpenGL
Dept of CSE, RVCE Sept-Dec 2012 18
menu by calculating the position according to the type of the button to be displayed To display a button it
switches to the required display code for that button.
Menus are highlighted whenever the mouse moves over it. If a submenu is selected then the
sel_menu will hold the button number of that menu and can be used to perform further operations. The
sel_menu can be used globally to check for the selected menu button number.
Popup menus are created using the built-in Glut function called glutCreateMenu(). The sub menu
entries are specified using the function glutAddSubMenu(). The menu entries are added using the function
glutAddMenuEntry().
4.3 Color palette :Color palette is designed using the number of color buttons. The Fig 4.7 shows the color palette. The
color button is the object of the structure button. For the color button we will specify the type of the button as
COLOR. The Button->num holds the unique color button number which is used to identify selected color.
Each colorbutton is displayed in different color using the color values specified in Color object.
Whenever a color button is pressed the draw_col object will be initialized with the color of that button. The
tool drawing function will use this draw_col object color while drawing the object.
Fig 4.7 Color palette
4.4 File options:File menu have the options like New, Open, Save, Save as and exit. The New option will create a new
blank window for drawing. If the current window is not blank then, this will call the msgwin window to
display the alert message.
The Open option shows the open dialog box which will take the filename to be opened. If the file does
not exist then, it will display error message.
The save option will display the save dialog and save as the file. If the file already exists it will display
error. The save option will not display the save dialog if the file is already opened file.
-
7/30/2019 4.Implementation
6/12
Implementation Graphics Package using OpenGL
Dept of CSE, RVCE Sept-Dec 2012 19
The save as option will display save as dialog and saves the file. If the file already exists then displays
the error message.
The exit option is used to quit the editor.
4.5 Function to draw objects using basic primitives.
4.5.1 Function to draw a Pencil
void drawPencil(int x1,int y1,int x2,int y2,GLfloat color[],GLfloat lWidth)
The above function is used to draw with a pencil on the canvas
4.5.2 Function to draw a Line
void drawLine(int x1,int y1,int x2,int y2,GLfloat color[],GLfloat lWidth)
The above function is used to draw a line on the canvas
4.5.3 Function to draw a Rectangle
void drawRectangle(int x1,int y1,int x2,int y2,GLfloat color[],GLfloat lWidth)
The above function is used to draw rectangle on the canvas
4.5.4 Function to Render a Text on the Screen
void drawKeyText()
The above function is used to render a text on the canvas
-
7/30/2019 4.Implementation
7/12
Implementation Graphics Package using OpenGL
Dept of CSE, RVCE Sept-Dec 2012 20
4.5.5 Function for Spiral Drawing
void drawSpiral(int x,int y,int x2,int y2,GLfloat color[],GLfloat lWidth)
The above function is used to draw Spiral on the canvas
4.5.6 Function to draw a Circle
void draw_pixel(GLint x,GLint y)
void plotpixels(GLint h,GLint k,GLint x,GLint y)
void drawCircle(int mx1,int my1,int mx2,int my2,GLfloat color[],GLfloat lWidth)
The above functions is used to draw Circle using two points on the canvas
4.6 Funtion to call all primitive icon functions
This section is used to call various primitive functions whenever that particular primitive is selected.
Whenever the particular primitive is selected the value is passed on to switch case .
void edit()
{
int top=0;
//if tools selected or submenu selected
if(((Toolsflag1)&&(Toolsflag2))||sMenuflag)
{
for(top=0;top
-
7/30/2019 4.Implementation
8/12
Implementation Graphics Package using OpenGL
Dept of CSE, RVCE Sept-Dec 2012 21
case CURVE:if(OBJECTS[top].flag)
drawCurve(OBJECTS[top].x1,OBJECTS[top].y1,OBJECTS[top].x2,OBJECTS[top].y2,OBJECTS[top
].x3,OBJECTS[top].y3,OBJECTS[top].color,OBJECTS[top].lw);
break;
case PENCIL:if(OBJECTS[top].flag)
drawPencil(OBJECTS[top].x1,OBJECTS[top].y1,OBJECTS[top].x2,OBJECTS[top].y2,OBJECTS[top
].color,OBJECTS[top].lw);
break;
}
4.7 Mouse Function
This function displays the mouse movement whether any button is pressed or released along with the
basic structure of the mouse called Mouse which defines the various variables.
struct Mouse{};
typedefstruct Mouse Mouse;
//global mouse struc to hold d mouse info
Mouse TheMouse={0,0,0,0,0};
// When Button is pressed
ButtonPress(&file_menu,x,y);
ButtonPress(&edit_menu,x,y);
ButtonPress(&clip_menu,x,y);
// When Button is released
ButtonRelease(&file_menu,x,y);
ButtonRelease(&edit_menu,x,y);
-
7/30/2019 4.Implementation
9/12
Implementation Graphics Package using OpenGL
Dept of CSE, RVCE Sept-Dec 2012 22
4.8 2D drawing :
2D drawings are done in screen window that is used as canvas. For each tool there is an array of
GLintpoint structure. This array will hold the details of the pixels to be drawn in favor of particular tool.
The 2D drawing canvas is controlled wholly by a display stack that holds the stack structure objects in
each level for each tool. The display stack is continuously called for each redisplay call for the canvas. The
display stack top pointer will point to the current stack top. The objects in the stack will be displayed from the
top to the bottom every time. Each display stack level will hold the details of current tool, the start and end
index of the array for that tool.
Whenever a tool is selected, the disp_stk.top is incremented and the start and end index will be
initialized to that tool index. During switching over to another tool, the current tool indices are stored in stack
top level and the top pointer is incremented for the next tool. The display stack called from top to bottom
hence, we will have the effect of objects drawn one over another.
4.9 Clipping :
There are two types of clipping implemented
Inside Clipping Outside Clipping
First we will identify the rectangular area selected by the select tool. Then perform following functions.
If (inside clipping)
Draw a rectangular patch covering the selected area.
If (outside clipping)
Draw four white rectangles from the left, right, top and bottom of the selected area excluding the
selected area.
-
7/30/2019 4.Implementation
10/12
Implementation Graphics Package using OpenGL
Dept of CSE, RVCE Sept-Dec 2012 23
4.10 Window Management:
Five routines perform tasks necessary to initialize a window.
glutInit(int *argc, char **argv) initializes GLUT and processes any command line arguments (for X,this would be options like -display and -geometry). glutInit() should be called before any other GLUT
routine.
glutInitDisplayMode(unsigned int mode) specifies whether to use an RGBA or color-index colormodel. You can also specify whether you want a single- or double-buffered window. (If youre
working in color-index mode, youll want to load certain colors into the color map; use glutSetColor()
to do this.) Finally, you can use this routine to indicate that you want the window to have an associated
depth, stencil, and/or accumulation buffer. For example, if you want a window with double buffering,
the RGBA color model, and a depth buffer, you might call glutInitDisplayMode(GLUT_DOUBLE |
GLUT_RGB | GLUT_DEPTH).
glutInitWindowPosition(int x, int y) specifies the screen location for the upper-left corner of yourwindow.
glutInitWindowSize(int width, intsize) specifies the size, in pixels, of your window.
glutCreateWindow(char *string) creates a window with an OpenGL context. It returns a uniqueidentifier for the new window. Until glutMainLoop() is called the window is not yet displayed.
4.11 The Display Callback:
glutDisplayFunc(void (*func)(void)) is the first and most important event callback function you willsee. Whenever GLUT determines the contents of the window need to be redisplayed, the callback
function registered by glutDisplayFunc() is executed. Therefore, you should put all the routines you
need to redraw the scene in the display callback function.
-
7/30/2019 4.Implementation
11/12
Implementation Graphics Package using OpenGL
Dept of CSE, RVCE Sept-Dec 2012 24
glutPostRedisplay(void), which gives glutMainLoop() a nudge to call the registered display callbackat its next opportunity. If your program changes the contents of the window, sometimes you will have
to call this function.
4.12 Running the Program
The very last thing you must do is call glutMainLoop(void). All windows that have been created are
now shown, and rendering to those windows is now effective. Event processing begins, and the registered
display callback is triggered. Once this loop is entered, it is never exited!
4.13 Drawing Three-Dimensional Objects
GLUT includes several routines for drawing these three-dimensional objects:
cone icosahedron teapot
cube octahedron tetrahedron
dodecahedron sphere torus
You can draw these objects as wireframes or as solid shaded objects with surface normals defined.
For example, the routines for a cube and a sphere are as follows:
void glutWireCube(GLdoublesize);
void glutSolidCube(GLdoublesize);
void glutWireSphere(GLdouble radius, GLintslices, GLintstacks);
void glutSolidSphere(GLdouble radius, GLintslices, GLintstacks);
All these models are drawn centered at the origin of the world coordinate system.
-
7/30/2019 4.Implementation
12/12
Implementation Graphics Package using OpenGL
Dept of CSE, RVCE Sept-Dec 2012 25
4.14 OpenGL Geometric Drawing Primitives:
Now that youve seen how to specify vertices, you still need to know how to tell OpenGL to create a
set of points, a line, or a polygon from those vertices. To do this, you bracket each set of vertices between a
call to glBegin() and a call to glEnd(). The argument passed to glBegin() determines what sort of geometric
primitive is constructed from the vertices.
Point Details
To control the size of a rendered point, use glPointSize() and supply the desired size in pixels as
theargument.
voidglLoadMatrix{fd}(const TYPE *m);Sets the sixteen values of the current matrix to those specified by m.
voidglMultMatrix{fd}(const TYPE *m);Multiplies the matrix specified by the sixteen values pointed to by m by the current matrix and stores the result
as the current matrix.
voidglLoadIdentity(void);Sets the currently modifiable matrix to the 4 4 identity matrix.
voidglMatrixMode(GLenum mode);Specifies whether the modelview, projection, or texture matrix will be modified, using the mode attribute.
Projecton matrix is used for 2-D purposes whereas Modelview is used for 3-D.