Chapter 2 Elementary Rendering OpenGL Geometric

Chapter 2
Elementary Rendering
• Here
Here you
you will
will learn
learn how
how to
to draw
draw geometric
geometric objects
objects,,
including
including points
points,, straight lines
lines,, and
and flat
flat polygons
polygons..
•• Note
Note that
that every
every time
time you
you issue
issue aa drawing
drawing
command,
the
specified
object
is
command, the specified object is drawn,
drawn, unless
unless
you
you specify
specify otherwise.
otherwise. This
This style
style is
is called
called
immediate
-mode graphics.
immediate-mode
graphics.
•• Another
-mode graphics,
Another style
style is
is called
called retained
retained-mode
graphics,
e.g.,
you
use
display
lists
and
force
the
e.g., you use display lists and force the system
system to
to
compile
compile them
them only.
only.
1
OpenGL Geometric Primitives
Tutors\shapes.exe
• All geometric primitives are specified by
vertices
GL_LINES
GL_LINE_STRIP
GL_POINTS
GL_LINE_LOOP
GL_POLYGON
GL_TRIANGLES
GL_QUADS
GL_QUAD_STRIP
2
GL_TRIANGLE_STRIP
國立高雄應用科大機械系 C. F. Chang
GL_TRIANGLE_FAN
1
Specifying Geometric Primitives
• Primitives are specified using
glBegin(
glBegin( primType );
glEnd();
glEnd();
• primType determines how vertices are combined
GLfloat red, greed, blue;
Glfloat coords[3];
coords[3];
glBegin(
glBegin( primType );
for ( i = 0; i < nVerts;
nVerts; ++i ) {
glColor3f( red, green, blue );
glVertex3fv( coords );
}
glEnd();
glEnd();
3
Simple Example 1
Example
Example 1,
1, draws
draws an
an outlined
outlined circle:
circle:
#define
#define PI
PI 3.1415926535897;
3.1415926535897;
GLint
GLint circle_points
circle_points == 100;
100;
glBegin(GL_LINE_LOOP);
(GL_LINE_LOOP);
glBegin
glBegin(GL_LINE_LOOP);
for
for (i
(i == 0;
0; ii << circle_points;
circle_points; i++)
i++)
{{
angle
angle == 2*PI*i/circle_points;
2*PI*i/circle_points;
glVertex2f(cos
(cos(angle),
(angle), sin(angle));
glVertex2f
glVertex2f(cos(angle),
sin(angle));
}}//
// end
end for
for ii
glEnd();
();
glEnd
glEnd();
4
國立高雄應用科大機械系 C. F. Chang
2
Simple Example 2
Example 2, the following code draws a Rhombus:
...
GLfloat color_t[] = { 1.0f, 0.0f, 0.0f }; //red color
drawRhombus( color_t[] );
...
void drawRhombus( GLfloat color[] )//繪一菱形
{
glBegin( GL_QUADS );
glColor3fv( color );//顏色
glVertex2f( 0.0, 0.0 );//頂點1
glVertex2f( 1.0, 0.0 ); //頂點2
glVertex2f( 1.5, 1.118 ); //頂點3
glVertex2f( 0.5, 1.118 ); //頂點4
glEnd();
}
5
Shapes Tutorial
..\Tutors\shapes.exe
6
國立高雄應用科大機械系 C. F. Chang
3
Line Width
• void glLineWidth(GLfloat width);
• Sets the width in pixels for rendered lines;
• width must be greater than 0.0 and by default is
1.0.
• You can obtain the range of line-width that your
system offers by using
glGetFloatv(GL_LINE_WIDTH_RANGE )
7
Stippling Line
• void glLineStipple(GLint factor,
factor, GLushort pattern);
pattern);
• Sets the current stippling pattern for lines, e.g.,
• glLineStipple(1, 0x00FF);
• glEnable(GL_LINE_STIPPLE);
FF
00
FF
00
FF
00
..\Redbook Samples\LINES.EXE
8
國立高雄應用科大機械系 C. F. Chang
4
Drawing Mode for Polygons
• void glPolygonMode(GLenum face,
face, GLenum mode);
mode);
• Controls the drawing mode for a polygon's front and back faces.
• face can be GL_FRONT_AND_BACK, GL_FRONT, or GL_BACK
• mode can be GL_POINT, GL_LINE, or GL_FILL to indicate
whether the polygon should be drawn as points, outlined, or filled.
• For example, you can have the front faces filled and the back faces
outlined with :
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_LINE);
• By default, both the front and back faces are drawn filled.
• void glFrontFace(GLenum mode);
mode);
• Controls how front-facing polygons are determined
• mode can be GL_CCW (default), GL_CW
9
Face Culling
• To instruct OpenGL to discard frontfront- or backback-facing
polygons, use the following command
void glCullFace(GLenum mode);
Where mode = GL_FRONT,
GL_FRONT, GL_BACK,
GL_BACK, or GL_FRONT_AND_BACK
to indicate frontfront-facing, backback-facing, or all polygons
are to be culled (discarded).
• Then, use glEnable(GL_CULL_FACE
glEnable(GL_CULL_FACE ) to enable the
culling
• The culling can be disabled with
glDisable(GL_CULL_FACE )
1
0
國立高雄應用科大機械系 C. F. Chang
5
Stippling Polygons
1. Defines the stipple pattern for filled polygons:
void glPolygonStipple(const
glPolygonStipple(const GLubyte *mask);
mask);
where mask is a pointer to a 32 × 32 bitmap
2. Enables the stippling by the command:
glEnable(GL_POLYGON_STIPPLE);
glEnable(GL_POLYGON_STIPPLE);
..\Redbook Samples\POLYS.EXE
1
1
Define a Stippling Pattern
(from left to right, from bottom to top)
GLubyte fly[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x80, 0x01, 0xC0, 0x06, 0xC0, 0x03, 0x60,
0x04, 0x60, 0x06, 0x20, 0x04, 0x30, 0x0C, 0x20,
0x04, 0x18, 0x18, 0x20, 0x04, 0x0C, 0x30, 0x20,
0x04, 0x06, 0x60, 0x20, 0x44, 0x03, 0xC0, 0x22,
0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
0x66, 0x01, 0x80, 0x66, 0x33, 0x01, 0x80, 0xCC,
0x19, 0x81, 0x81, 0x98, 0x0C, 0xC1, 0x83, 0x30,
0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0,
0x03, 0x31, 0x8c, 0xc0, 0x03, 0x33, 0xcc, 0xc0,
0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30,
0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08,
0x10, 0x63, 0xC6, 0x08, 0x10, 0x30, 0x0c, 0x08,
0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08 };
1
03
80
01
C0
2
國立高雄應用科大機械系 C. F. Chang
6