• Drawing Polylines Stored in a File
• Parameterizing Figures
• Menus
1
Drawing Polylines
Drawing polyline from vertices in a file
•
•
•
•
# polylines
# vertices in first polyline
Coordinates of vertices, x y, one pair per line
Repeat last 2 lines as necessary
File for dinosaur available from theWeb site
Code to draw polylines/polygons in Fig.
2.24.
2
Example “dino”
3
Suppose the file dino.dat contains a collection of polylines, in the following format (the comments are not part of the file):
21
4
169 118
174 120
179 124
178 126
5
298 86
304 92
310 104
314 114
314 119
29
32 435
10 439
. . . etc.
number of polylines in the file number of points in the first polyline first point of first polyline second point of first polyline
number of points in the second polyline first point of second polyline
4
Fig. 2.24 A Function for Drawing polylines stored in a File void drawPolyLineFile(char * fileName)
{
fstream inStream; inStream.open(fileName, ios ::in); // open the file if(inStream.fail()) return; glClear(GL_COLOR_BUFFER_BIT); // clear the screen
GLint numpolys, numLines, x ,y; inStream >> numpolys; // read the number of polylines for(int j = 0; j < numpolys; j++) // read each polyline
{
inStream >> numLines; glBegin(GL_LINE_STRIP); // draw the next polyline for (int i = 0; i < numLines; i++)
{
inStream >> x >> y; // read the next x, y pair glVertex2i(x, y);
}
glEnd();
}
glFlush(); inStream.close(); }
5
Parameterizing Figures
Parameterizing Drawings: allows making them different sizes and aspect ratios
Code for a parameterized house is in Fig.
2.27.
6
Parameterizing Figures
7
Drawing a village by calling parameterizedHouse with different parameters
8
Building a Polyline Drawer class GLintPointArray{ const int MAX_NUM = 100; public: int num;
GLintPoint pt[MAX_NUM];
};
void drawPolyLine(GlintPointArray poly, int