- Open the
attached source file: Lab01.cpp
Drawing lines
·
Modify parameters in glVertex2i() and change the line width.
·
Add glVertex2i() to draw another 2 lines.
Note that a line needs 2 vertices as the starting and ending positions. Thus, the number of vertices within the glBegin() and glEnd() is of multiple of 2.
·
Set different colors
for the lines.
Drawing triangles
·
Change the
GL_LINE_LOOP to GL_TRIANGLES.
·
As a triangle will
need 3 vertices, make sure the number of vertices within the glBegin() and glEnd() is of multiple of 3.
·
Change the
GL_TRIANGLES to GL_TRIANGLE_FAN. What
happen to your drawing?
Drawing rectangles
·
Change the
GL_TRIANGLE_FAN to GL_QUADS.
·
As a rectangle will
need 4 vertices, make sure the number of vertices within the glBegin() and glEnd() is of multiple of 4.
What will happen if it is not?
·
Change the GL_QUADS
to GL_QUAD_STRIP. What happen to your
drawing?
Drawing polygons
·
Change the GL_QUAD_STRIP
to GL_POLYGONS. Try drawing an irregular
shape.
Screen Coordinate System:
·
Modify the
parameters in the function calls to glutInitWindowSize()
and glutInitWindowPosition() until
you have a good understanding of what each parameter means.
·
Note that these
values are in pixels.
Window Coordinate System:
·
Modify the
parameters in the function call to gluOrtho2D();
until you have a good understanding of what each parameter means
·
Note that these
values are in world units.
2. Create a program to
draw the 2D objects as shown in the following diagram using GL_TRIANGLE,
GL_QUAD and GL_POLYGON.
>>..................................................................................
#include <glut.h>
// Note: this automatically includes other
//
required header files, including:
// glu.h
(GL utility functions)
// gl.h
(GL functions)
// windows.h
(Microsoft windows stuff)
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0); // set the background to white
glMatrixMode(GL_PROJECTION); // set
projection parameters
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}
void lineSegment(void)
{
glClear( GL_COLOR_BUFFER_BIT ); // Clear display
window
glColor3f(1.0f, 0.0f, 0.0f); // set the
drawing color to red
glBegin(GL_TRIANGLES); // start drawing in
'line' mode
glVertex2i( 50, 200); //
specify the lines geometry
glVertex2i( 150, 350);
glVertex2i(250, 200);
// end drawing
glEnd();
glColor3f(0.0f, 1.0f, 0.0f); // set the
drawing color to red
glBegin(GL_QUADS); // start drawing in
'line' mode
glVertex2i( 300, 200); //
specify the lines geometry
glVertex2i( 300, 350);
glVertex2i(400, 350);
glVertex2i(400, 200);
// end drawing
glEnd();
glColor3f(0.0f, 0.0f, 1.0f); // set the
drawing color to red
glBegin(GL_POLYGON); // start drawing
in 'line' mode
glVertex2i(450, 200);
glVertex2i(550, 200);// specify the lines geometry
glVertex2i(550, 300);
glVertex2i(500, 400);
glVertex2i(450, 300);
glVertex2i(450, 200);
// end drawing
glEnd();
glFlush(); // send any buffered output to
// the OpenGL rendering process
}
//-------------------------------------------------------------------
void main( int argc, char
**argv)
{
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
// Set display mode
glutInitWindowPosition(100,200); // Set top-left
display window position
glutInitWindowSize(400,200); // Set
display window width and height
glutCreateWindow("An
Example");// Create display window
init();
// Execute initialization procedure
glutDisplayFunc(lineSegment); // Send
graphics to display window
glutMainLoop(); // Display
everything and wait
}
Exercise
1.
Vertices specified, the interpretations are as
follows:
|
GL_POINTS
|
Treats each vertex as a single point. Vertex n defines point n.
N points are drawn. |
|
GL_LINES
|
Treates each pair of vertices as an independent line segment. Vertices 2n-1
and 2n define line n. N/2 lines are drawn. |
|
GL_LINE_STRIP
|
Draws a connected group of line segments from the first vertex to the
last. Vertices n and n+1 define line n. N-1 lines
drawn. |
|
GL_LINE_LOOP
|
Draws a connected group of line segments from the first vertex to the
last, then back to the first. Vertices n and n+1 define line n.
The last line, however, is defined by vertices N and 1. N lines
are drawn. |
|
GL_TRIANGLES
|
Treates each triplet of vertices as an independent triangle. Vertices 3n-2,
3n-1, and 3n define triangle n. N/3
triangles are drawn. |
|
GL_TRIANGLE_STRIP
|
Draws a connected group of triangles. One triangle is defined for each
vertex presented after the first two vertices. For odd n, vertices n,
n+1, and n+2 define triangle n. For even n,
vertices n+1, n, and n+2 define triangle n. N-2
triangles are drawn. |
|
GL_TRIANGLE_FAN
|
Draws a connected group of triangles. One triangle is defined for each
vertex presented after the first two vertices. Vertices 1, n+1,
and n+2 define triangle n. N-2 triangles are drawn. |
|
GL_QUADS
|
Treats each group of four vertices as an independent quadrilateral.
Vertices 4n-3, 4n-2, 4n-1, and 4n define
quadrilateral n. N/4 quadrilaterals are drawn. |
|
GL_QUAD_STRIP
|
Draws a connected group of quadrilaterals. One quadrilateral is defined
for each pair of vertices presented after the first pair. Vertices 2n-1,
2n, 2n+2, and 2n+1 define quadrilateral n. N/2-1
quadrilaterals are drawn. Note that the order in which vertices are used to
construct a quadrilateral from strip data is different from that used with
independent data. |
|
GL_POLYGON
|
Draws a single, convex polygon. Vertices 1 through N define
this polygon. |
Window Coordinate System
glutInitWindowPosition, glutInitWindowSize
glutInitWindowPosition and glutInitWindowSize set the initial window position and size respectively.Usage
void glutInitWindowSize(int width, int height);
void glutInitWindowPosition(int x, int y);
width
Width in pixels.
height
Height in
pixels.
x
Window X
location in pixels.
y
Window Y
location in pixels.
Description Windows created by glutCreateWindow will be requested to be created with the current initial window position and size.
The initial value of the initial window position GLUT state is -1 and -1. If either the X or Y component to the initial window position is negative, the actual window position is left to the window system to determine. The initial value of the initial window size GLUT state is 300 by 300. The initial window size components must be greater than zero.
The intent of the initial window position and size values is to provide a suggestion to the window system for a window's initial size and position. The window system is not obligated to use this information. Therefore, GLUT programs should not assume the window was created at the specified size or position. A GLUT program should use the window's reshape callback to determine the true size of the window.
World Coordinate System
gluOrtho2D
NAME
gluOrtho2D - define a 2-D orthographic
projection matrix
C
SPECIFICATION
void gluOrtho2D( GLdouble left,
GLdouble right, GLdouble bottom, GLdouble top )
PARAMETERS
|
left, right
|
|
Specify the coordinates for the left and right vertical clipping planes. |
|
bottom, top
|
|
Specify the coordinates for the bottom and top horizontal clipping planes.
|
DESCRIPTION
gluOrtho2D sets up a two-dimensional
orthographic viewing region. This is equivalent to calling glOrtho
with near = -1 and far = 1 .
2.
ليست هناك تعليقات:
إرسال تعليق