Output primitives (4) 8-2 OpenGL point drawing function, OpenGL line drawing function

4.3 OpenGL drawing point function

To describe the geometric features of a point, we simply specify a location in the world coordinate system. This coordinate position is then passed to the observation subroutine, along with other geometric descriptions already in the scene. OpenGL primitives are displayed at their default size and color unless other property values are specified. The default primitive color is white, and the default point size is equal to a single screen pixel size.
Use the following OpenCL function to specify the coordinate value of a point position: glVertex*( ): The asterisk (*) here indicates that the function requires post-level code. These post-level codes are used to specify the spatial dimensions, the data type of the coordinate value variable and the possible coordinate description in vector form. A call to gIVertex must be inserted between the glBegin function and the gEnd function. The variables of the glBegin function are used to specify the type of output primitive to be displayed, while the glEnd function has no variables. For point drawing, the variable of glBegin function is the symbolic constant GL_POINTS. Therefore, the OpenGL description of a point position is of the form
glBegin(GLPOINTS):
glVertex* ( );
glEnd():
Although the term vertex (vetex) strictly refers to a “corner” point of a polygon, the intersection of two corners, the intersection of a circle and its principal axis, or other similar coordinate positions in a geometric structure, the gvertex function in OpenGL can be used to describe the coordinates of any arbitrary point. Location. This uses a simple function to describe points, line segments and polygons, and uses more polygon patches to describe scene objects.

Coordinate positions in OpenGL can be two-dimensional, three-dimensional or four-dimensional. The suffix of glvertex is 23 or 4 indicating the dimension of its coordinate position. Four-dimensional description means homogeneous-coordinate representation, where the homogeneous parameter h (the fourth-dimensional coordinate) is the scaling factor of the Cartesian coordinate value. Homogeneous coordinate representations are useful for expressing transformation operations in matrix form, and are discussed in detail in Chapter 7. Since OpenGL treats two dimensions as a special case of three dimensions, any (xy) coordinate description is equivalent to a three-dimensional coordinate description (x, y,). In addition, OpenGL uses four-dimensional coordinates to represent vertices internally, so the above description is equivalent to four-dimensional coordinates (x, y, 0, 1).
We need to indicate what data structure to use in the numerical description of the coordinates. This is done by the second suffix of the glvertex function. The suffixes used to specify numeric data types are: i (integer) s (short) (floating point) and (double floating point). Finally, glVertex can use explicit coordinate values or individual variables that introduce coordinate positions in matrix form. If you use matrix coordinate positions, you need a third suffix code: v (“vector”).

In the example below, three equidistant points are plotted on a line with slope 2 (see Figure 4.3). Coordinates are given as pairs of integers:
glBegin (GL_POINTS):
glVertex2i (50, 100);
glVertex2i(75,150);
glVertex2i (100, 200);
glEnd();
Alternatively, we can describe the coordinate values of the previous points in matrix form:
int pointl[]=[50, 100):
int point2[][75.150]:
int point3 [ ][100, 200]:
And call the OpenGL function to draw these three points:
glBegin (GL_POINTS);
glVertex2iv (point1);
glVertex2iv (point2):
glVertex2iv (point3):
glEnd ( );

Below is another example of describing the positions of two points in a three-dimensional world coordinate system. Here the coordinates are given as explicit floating point numbers:
gBegin (GL_POINTS):
glVertex3f(-78.05, 909.72, 14.60):
glVertex3f(261.91, -5200.67, 188.33):
glEnd():
We can also define C++ classes or structures (structs) for point locations described in various dimensions. For example
class wcPt2D{

public:
GLfloat x.y:

}

With this type of definition, we can use the following statements to describe the position of a point in a two-dimensional world coordinate system:
wcPt2D pointPos:
pointPos.x=120.75:
pointPos.y-45.30:
glBegin (GL_POINTS):
8lVertex2f (pointPos,x,pointPos.y);
glEnd ( ):
We can also use the OpenGL drawing point function in the C++ process to implement the setPixel command.

4.4 OpenGL line drawing function

Graphics software packages generally provide a function that describes one or more straight line segments, where each straight line segment is defined by the coordinate positions of two endpoints. In OpenGL, just like selecting a point position, using the glVertex function to select the coordinate position of a single endpoint we can use the glBegin/glEnd pairing to introduce a list of endpoint positions. There are three OpenGL symbolic constants that can be used to specify how to connect this series of endpoint positions into a set of straight line segments. By default, each symbolic constant displays a solid white line

Use the primitive line constant GL_LINES to connect each pair of adjacent endpoints to obtain a set of straight line segments. Normally, since OpenGL only recognizes line segments as connected if they share a vertex; line segments that intersect but do not share a vertex are not recognized as connected, which results in a set of unconnected line segments unless some coordinate positions are repeated. If only one endpoint is described nothing is displayed, if the number of listed endpoints is an odd number the last endpoint is not processed. For example, if we have 5 coordinate positions, labeled p1 to p5, each represented by a two-dimensional array, then the following program can generate Figure 4.4(a):

glBegin (GL LINES);
glVertex2iv(p1);
glVertex2iv (p2);
glVertex2iv (p3):
glVertex2iv(p4);
glVertex2iv(p5):
glEnd():

This way, we get a straight line segment between the first and second coordinate positions and another straight line segment between the third and fourth positions. In this case, the number of endpoints specified is an odd number, so the last coordinate position is ignored.

Use OpenGL’s primitive constant GL_LINE_STRIP to get polyline. At this time, a set of connected line segments from the first endpoint to the last endpoint is displayed. The first segment appears between the first and second endpoints; the second segment appears between the second and third endpoints; and so on, up to the last endpoint. If at least two coordinate locations are not listed, nothing is displayed. Using the 5 coordinate positions in the above example, we use the following procedure to generate Figure 4.4(b):

int point1[] = { 70,70 };
int point2[] = { 25,35 };
int point3[] = { 45,100 };
int point4[] = { 65,35 };
int point5[] = { 23,70 };

glBegin (GL_LINE_STRIP):
gVertex2iv(p1);
glVertex2iv (p2):
glVertex2iv (p3);
glVertex2iv (p4):
glVertex2iv(p5);
glEnd();

The third OpenGL primitive constant is GL_LINE_LOOP, which generates a closed polyline. The main line segment is drawn the same as using GL_LINE_STRIP, but a straight line segment is added to connect the last endpoint to the first endpoint. Figure 4.4(c) shows the display of the endpoint group using this line option:
glBegin (GL_ LINE_ LOOP):
glVertex2iv(p1);
g1Vertex2iv (p2):
glVertex2iv(p3):
glVertex2iv(P4):
glVertex2iv(p5);
glEnd():

As mentioned before, the parts of the graphics described in the world coordinate system are ultimately mapped to the coordinate system of the output device. The geometric information in the image is then scanned and converted into pixel locations. In Section 6.1 we will discuss the scan conversion algorithm that implements the penCL line drawing function.

Part of the test code:

#include "stdafx.h"
#include "windowOneLine.h"
 

void init(void) {

glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 250.0);

}

void lineSegment(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(5.0,0.0,0.0);

//Draw a line on the window
/**
glBegin(GL_LINES);
glVertex2i(180,15);
glVertex2i(10,145);
glEnd();
\t
**/

//Draw three points with a slope of 2
/**

glBegin(GL_POINTS);
glVertex2i(50,100);
glVertex2i(75,150);
glVertex2i(100,200);
glEnd();

**/

/** Draw three points with a slope of 2. Another way,
We can describe the coordinate values of the previous points in matrix form:; Finally, glVertex can use explicit coordinate values or a single variable that introduces the coordinate position in matrix form. If you use matrix coordinate positions, you need a third suffix code: v ("vector").
**/
int point1[] = { 50,100 };
int point2[] = { 75,150 };
int point3[] = { 100,200 };
glBegin(GL_POINTS);
glVertex2iv(point1);
glVertex2iv(point2);
glVertex2iv(point3);
glEnd();
**/

/**
Below is another example of describing the positions of two points in a three-dimensional world coordinate system. Here the coordinates are given as explicit floating point numbers:
**/
glBegin(GL_POINTS);
glVertex3f(78.5,909.72,14.60);
glVertex3f(61.91,52.67,183.33);
glEnd();

**/

/**
We can also define C++ classes or structures (structs) for point locations described in various dimensions.
**/
\t 
wcPt2d pointPos;
pointPos.x = 120.75;
pointPos.y = 45.30;
glBegin(GL_POINTS);
glVertex2f(pointPos.x,pointPos.y);
\t\t 
glEnd();
**/

/**
Draw a five-pointed star and use 5 endpoint coordinates to display line segments: (a) use the primitive constant GL_LINES to generate a set of unconnected line segments; (b) use GL_LINE_STRIP to generate a polyline; (c) use GLLINE_LOOP to generate a closed polyline
**/
int point1[] = { 70,70 };
int point2[] = { 25,35 };
int point3[] = { 45,100 };
int point4[] = { 65,35 };
int point5[] = { 23,70 };

//glBegin(GL_POINTS); //Five points
//glBegin(GL_LINES); //Two unrelated lines, point5 is ignored
//glBegin(GL_LINE_STRIP); //4 polylines related to 1-2, 2-3, 3-4, 4-5;
glBegin(GL_LINE_LOOP); // five-pointed star


glVertex2iv(point1);
glVertex2iv(point2);
glVertex2iv(point3);
glVertex2iv(point4);
glVertex2iv(point5);
glEnd();

glFlush();
}


void main(int argc, char** argv) {

glutInit( & amp;argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400,300);
glutInitWindowPosition(50,100);
glutCreateWindow("An Example OpenGL Program ");

init();
glutDisplayFunc(lineSegment);
glutMainLoop();





}