You are on page 1of 221

..

.. , ..

OPENGL



2007
681.3.07
32.973.26-018.2
34

.., .. OpenGL.
.: , 2007.  221c.

ISBN 978-5-903242-02-3

-
OpenGL.

. .. . -
OpenGL , -
OpenGL .
.
, -
/C++ -
.
, , -
OpenGL .
-
.
:
.., , .-. , -
.., .-. , -

ISBN 978-5-903242-02-3



.., ..
, 2007

. . . . . . . . . . . . . . . . . . . . . . . . . 7

. . . . . . . . . . . . . . . . . . . . . . . . . . . 11

I OpenGL 15

1. OpenGL 17

1.1. . . . . . . . . . . . . . . . . . 17

1.2. . . . . . . . . . . . . . . . . 19

1.3. . . . . . . . . . . . . . . . . . . . . . . . . 20

1.4. . . . . . . . . . . . . . . . . . . . . . . . 21

1.5. . . . . . . . . . . . . . . . . . . . . . . . 22

1.6. . . . . . . . . . . . . . . . . . 22

1.7. . . . . . . . . . . . . . . . . 23

2. OpenGL 25

2.1. . . . . . . . . . . . . . . . . 25

2.2. OpenGL . . . . . . . . . . . . . . . . . . 26

2.3. OpenGL . . . . . . . . . . . . . . . . . 28

2.4. . . . . . . . . . . . . . . . . . . . 30

2.5. . . . . . . . . . . . . . . . . . 31

2.6. . . . . . . . . . . . . . . . . 36

3
4
3. 39
3.1. . . . . . . . . . . 39
3.2. . . . . . . . . . . . . . . . . 41
3.3. glBegin / glEnd . . . . . . . . 43
3.4. . . . . . . . . . . . . . . . . . . 47
3.5. . . . . . . . . . . . . . . . . . . . . 49
3.6. . . . . . . . . . . . . . . . . 51

4. 55
4.1. . . . . . . . . . . . . . . . . . . 56
4.2. - . . . . . . . . . 58
4.3. . . . . . . . . . . . . . . . . . . . . . . . . 60
4.4. . . . . . . . . . . . . . . . . . . . . 63
4.5. . . . . . . . . . . . . . . . . 64

5. 65
5.1. . . . . . . . . . . . . . . . . . . . 65
5.2. . . . . . . . . . . . . . . 67
5.3. . . . . . . . . . . . . . 69
5.4. . . . . . . . . . . . . . . 73
5.5. . . . . . . . . . . . . . . . . 74

6. 77
6.1. . . . . . . . . . . . . . . . . . 77
6.2. . . . . . . . . . . 81
6.3. . . . . . . . . . . . . . . . 84
6.4. . . . . . . . . . . . . . . . . 87

7. 89
7.1. . . . . . 90
7.2. - . . . . . . . . . . . . . . . . . . . 93
7.3. . . . . . . . . . . . . . . . . . . . . . . 94
7.4. . . . . . . . . . . . . . . 96
7.5. . . . . . . . . . . . . . . . . 98
5

II OpenGL 99

8. OpenGL 101

8.1. . . . . . . . . . . . . . . 101
8.2. . . . . . . . . . . . . . . . . . . . 103
8.3. . . . . . . . . . . . . . . . . 109
8.4. . . . . . . . . . . . . . . . . 113

9. 115

9.1. . . . . . . . . . . . . . . 115
9.2. OpenGL . . . . . . . . . . . 120
9.3. . . . . . . . . . . . . . . . . 128

III OpenGL 131

10.OpenGL- GLUT 133

10.1. GLUT- . . . . . . . . . . . . 133


10.2. GLUT Microsoft Visual C++ 6.0 . . . . . . 137
10.3. GLUT Microsoft Visual C++ 2005 . . . . . 139
10.4. GLUT Borland C++ Builder 6 . . . . . . . 140
10.5. GLUT Borland C++ Builder 2006 . . . . . . 141

11. OpenGL MFC VCL 145

11.1. . . . . . . . . . . . . . . . . . 146
11.2. . . . . . . . . . . . . 147
11.3. (render context) . . . . . . . . 148
11.4. GLRC . . . . . . . . . . . . . . . . . . . . . . 149
11.5. OpenGL c MFC . . . . . . . . . . . 150
11.6. OpenGL c VCL . . . . . . . . . . . . 153

12.OpenGL .NET 157

12.1. GLUT Microsoft Visual C# 2005 . . . . . . 157


12.2. OpenGL WindowsForms . . . . . . 160
6
IV 163

. GLU GLUT 165

. 169
.1. 1: GLUT- . . . . . . . 169
.2. 2: OpenGL . . . . . . . 173
.3. BMP . . . . . . . . . . . . . . . . . 178
.4. 3: . . . . . . 186
.5. OpenGL Win32 . . . . . . . . 195

. 201
.1. Cornell Box . . . . . . . . . . . . . . . . . . . . . . . 201
.2. . . . . . . . . . . . . . . . . . . . 204
.3. . . . . . . . . . . . . . . 206

215

217

() -
. --
-
(Ivan Sutherland), 1963 .
. 1967 .
ACM SIGGRAPH. -
SIGGRAPH
- . 1983 .
SIGGRAPH -

.

,
1990- . -
80-

( ), 1990-
1000 USD -
.
1980- ,
1990- -
.
, ,
-

7
8
z-.

-
.
1970-
( , -
) 1980- -
(Core System, PHIGS, GKS),
1990- -
(OpenGL). -
.


. ,
1970- -
. -
ACM 1978 . . 1980- -
1990- -
(
, , -
, -
, ) [1, 2]. , -
(API) ,
OpenGL
, -
. , -
, . -
-
, , -
.

( 1999 . -
http://courses.graphicon.ru).

", "(learning-by-doing), , -
, 5-6 -
9

, . (-
.)
. -
OpenGL,
, ,
, . -
, ,
.
, -
. -
.
. . -
.

..
2007

, , -
.
, -
,
, -
, . -
,
, , -
, -
- .


80- 20- -
(), -
(, -
), -
.

,

(API  Application Programming Interface).

OpenGL
-
.

11
12
OpenGL (Open Graphics Library  -
) 1992
-
- ,
. -
IRIS GL,
Silicon Graphics Inc.

120 , -
,
-
.

OpenGL -
-
. ,
Windows, Apple. -
Mesa ( API
OpenGL)
, Linux.

OpenGL,
, -
:

. -
,
.

. , -
OpenGL, -
-
.
,
,
.
13

. OpenGL -
,

, , -
. -
-

.

-


, , , .. -
OpenGL
-
.
.
-
, . -

OpenGL.
OpenGL -

, OpenGL .

OpenGL .
OpenGL 1.2.
I

OpenGL

15
1.


OpenGL

OpenGL, -

, , -
.

1.1.
-
( ,
). , -
, :

-
, -
. :

, -
;

17
18 1. OPENGL
, .

,
. -
, x
s /c :
x(t) = st.
, -
.
, -
,
. -
 .


, -
,
. : ,
.

, -
() -
.

, , -
(, ), -
, ,
,  .
, -

(rendering). , -
.
-

.
1.2. 19

, OpenGL
.

. 1.1. OpenGL .

1.2.

.
-
. , -
, -
. ,
.
, 
. 1.2.
-
, -
. -
-
(), -
20 1. OPENGL

. 1.2. ()

.
, -
.
1.3.
,
OpenGL, .
, -
.
-
 OpenGL -
.
3.

1.3.

( ),
(motion capture), -
(  )
( , , ).
OpenGL , -

1.4. 21

. 1.3.

(, , ). -
(-
, )
.

1.4.
-
-
-
.
, , -
-
, .
OpenGL
,
22 1. OPENGL
 -
. OpenGL .
-
, -
..
OpenGL 5.

1.5.
, -
.
-
 -
(, ),
( ) ..
OpenGL
, () -
RGB (Red-Green-Blue). -
, ,
. -
, -
OpenGL. ,

OpenGL , -
.

1.6.
-
. -
-
, -
1.7. 23

(, -
).
OpenGL -
, -
[15].
( -
)
( -
).

1.7.
-
, -
-
.

.

()

( ).
-
.

,
.

,
. ..
(x, y, z) T
( ), , (xs , ys ) = T (x, y, z), (xs , ys ) 
. -
24 1. OPENGL
-
,
. -
,
.
OpenGL .
-
-
4-
. , -
-
(
).
, OpenGL -

. ,
, -
-
-,
(
OpenGL).
2.

OpenGL

2.1.
OpenGL
. -
:

-
(), -
. OpenGL
, ,
..

-
, -
.

. -
,
. ,
,
, .

25
26 2. OPENGL
OpenGL , -
, , .

-
, -
. , -
, ,
.


-
 , , .

OpenGL -
,
, ,
..

2.2. OpenGL
OpenGL . -
,
GL.
, OpenGL
.
 GL(GLU  GL Utility).
-
GL. GLU -
,
(, , , ), , -
..
OpenGL
.

2.2. OPENGL 27

. 2.1. OpenGL


-
.

GLUT (GL Utility


Toolkit). GLUT OpenGL, de facto
-
. GLUT -
OpenGL-
. GLX -
.
GLUT.

, ,
, -
. , , OpenGL,
Win32 API X Window. 2.1 -
,
Windows.
OpenGL.
28 2. OPENGL
2.3. OpenGL
OpenGL -. -
 -
, OpenGL . -
,
(,
 DLL), ( -
-
).

GL -
. -
 , , .. -
. -
, -

.

-
(vertex). ,
.
(, , ,
..), . -
.

, OpenGL
, -
.

OpenGL ,
, -
, . -
OpenGL -
, .. -
.

, OpenGL  ,
2.3. OPENGL 29

.
,
OpenGL -
.
: -
-
-
.

. 2.2. OpenGL

OpenGL -
, -
, -
.
, OpenGL -
30 2. OPENGL
,
, , ,
. -

,
. , -
.

2.4.
GL gl.h, -

#include <g l / g l . h>


GLU -
glu.h. , , -
, ,
Microsoft Visual C++ Borland C++ Builder.
, GLUT
.
OpenGL III.

( ) GL -
gl,  GL_. -
GLU GLUT -
glu (GLU_) glut (GLUT_)
, ,
. OpenGL
:

type glCommand_name [ 1 2 3 4 ] [ b s i f d ub us u i ] [ v ]
( type1 arg1 ,. . ., typeN argN )

:
2.5. 31

gl  , : -
OpenGL, GL, GLU,
GLUT, GLAUX gl, glu, glut, aux ;

Command_name  ( );

[1 2 3 4 ]  ;

[b s i f d ub us ui ]  : b  GLbyte (-
char /++), i  GLint ( int),
f  GLoat ( oat) .
gl.h;

[v ]  , -
-
.


. , glVertex2i() -
GL ,
glColor3fv()
.
-
,
C++. OpenGL
, , ,
.

2.5.
, OpenGL,
, .
() OpenGL
.
OpenGL API.
32 2. OPENGL
,
GLUT  -
Hello, World!.
,  -
. ,
OpenGL.

2.1. OpenGL.

#include <s t d l i b . h>


// GLUT
#include <g l / g l u t . h>
//
GLint Width = 512 , Height = 5 1 2 ;

//
const int CubeSize = 2 0 0 ;
//
void Display ( void )
{
int l e f t , r i g h t , top , bottom ;
l e f t = ( Width CubeSize ) / 2 ;
r i g h t = l e f t + CubeSize ;
bottom = ( Height CubeSize ) / 2 ;
top = bottom + CubeSize ;

glClearColor (0.7 , 0.7 , 0.7 , 1);


g l C l e a r (GL_COLOR_BUFFER_BIT) ;

glColor3ub ( 2 5 5 , 0 , 0 ) ;
g l B e g i n (GL_QUADS) ;
g l V e r t e x 2 f ( l e f t , bottom ) ;
g l V e r t e x 2 f ( l e f t , top ) ;
g l V e r t e x 2 f ( r i g h t , top ) ;
g l V e r t e x 2 f ( r i g h t , bottom ) ;
2.5. 33

glEnd ( ) ;

glFinish ();
}

//
void Reshape ( GLint w, GLint h )
{
Width = w;
Height = h ;

/* */
glViewport ( 0 , 0 , w, h ) ;

/* */
glMatrixMode (GL_PROJECTION) ;
glLoadIdentity ( ) ;
glOrtho ( 0 , w, 0 , h , 1.0 , 1 . 0 ) ;

glMatrixMode (GL_MODELVIEW) ;
glLoadIdentity ( ) ;
}

//
void Keyboard ( unsigned char key , int x , int y )
{
const char ESCAPE = ' \033 ' ;
i f ( key == ESCAPE )
exit (0);
}

//
void main ( int argc , char
* argv [ ] )
{
g l u t I n i t (&argc , argv ) ;
34 2. OPENGL
g l u t I n i t D i s p l a y M o d e (GLUT_RGB) ;
glutInitWindowSize ( Width , Height ) ;
glutCreateWindow ( "Red square example " ) ;

glutDisplayFunc ( Display ) ;
glutReshapeFunc ( Reshape ) ;
glutKeyboardFunc ( Keyboard ) ;

glutMainLoop ( ) ;
}

. 2.3. 2.1.

, -
,
, OpenGL GLUT.
GLUT -
c
(callback function). ,
, -

( ).
main .
2.5. 35

: , -
OpenGL, c
.

, ,
.
glutInit (&argc, argv) -
GLUT.
glutInitDisplayMode(GLUT_RGB) -
()
RGB.
glutInitWindowSize(Width, Height) -
.
, glutCreateWindow("Red square example") -
.

glutDisplayFunc ( Display ) ;
glutReshapeFunc ( Reshape ) ;
glutKeyboardFunc ( Keyboard ) ;
Display(), Reshape() Keyboard() -
, , ,
, , -
.

glutMainLoop().
, GLUT OpenGL,
OpenGL
, .
OpenGL-
API
(Win32, X Window ..),
. -
GLUT 10.
36 2. OPENGL
OpenGL -
. -
. Display,
,
.
Display():
glClearColor (0 , 0 , 0 , 1);
g l C l e a r (GL_COLOR_BUFFER_BIT) ;

glColor3ub ( 2 5 5 , 0 , 0 ) ;
g l B e g i n (GL_QUADS) ;
g l V e r t e x 2 f ( l e f t , bottom ) ;
g l V e r t e x 2 f ( l e f t , top ) ;
g l V e r t e x 2 f ( r i g h t , top ) ;
g l V e r t e x 2 f ( r i g h t , bottom ) ;
glEnd ( ) ;
,
.
-
, -
.

2.6.
1) , , -
?

2) OpenGL -
.

3) , GLUT
GLX? OpenGL?

4) () .
2.6. 37

5) OpenGL -
?

6) OpenGL, -
?

7) -
glColor4ub()? glVertex3fv() ?
3.

3.1.
, , OpenGL,
-
. -
, ,
.
OpenGL
.
OpenGL. -
GLUT, -
, glutDisplayFunc().
GLUT , -
,
(, ).
,
, .. - , -
. -

39
40 3.
. , -
glutPostRedisplay().
10.
, , , -
. ,
:

OpenGL;

void g l C l e a r C o l o r ( clampf r , clampf g , clampf b ,


clampf a )
void g l C l e a r ( b i t f i e l d buf )
glClearColor , -
.
R,G B
[0, 1]. -
(. . 7.1). , 1.
 (0,0,0,1).
glClear , buf -
, ,
(. 7).

g l C l e a r (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
.
-
(, ..) -
.
4.
3.2. 41

, OpenGL -
, .
OpenGL.

3.2.
OpenGL (vertex) -
, , -
..
, . -
, ,
.
.
,
.

3.2.1.


-, -, ( -
).
glVertex:
void g l V e r t e x [ 2 3 4 ] [ s i f d ] ( type co ords )
void g l V e r t e x [ 2 3 4 ] [ s i f d ] v ( type * coo rds )
: x, y,
z, w. glVertex2* x y.
z 0, -
w  1. glVertex3* x, y, z
w 1. glVertex4*
.
, -
-
, OpenGL
42 3.
.
.

3.2.2.

void g l C o l o r [ 3 4 ] [ b s i f ] ( GLtype components )


void g l C o l o r [ 3 4 ] [ b s i f ] v ( GLtype components )
R, G, B ,

( -).
f (oat), -
[0,1], -
- 1.0,
. ub (unsigned byte) ,
[0,255].
, , -
, -
.
-

void glShadeModel (GLenum mode )


GL_SMOOTH -
( ), GL_FLAT  .

3.2.3.

void glNormal3 [ b s i f d ] ( type c oord s )


void glNormal3 [ b s i f d ] v ( type coor ds )
3.3. GLBEGIN / GLEND 43

, -
. OpenGL
,
.
glEnable(GL_NORMALIZE).
,
-
/, -
- .
-
OpenGL,
(
).
.
,

void glEnable (GLenum mode )


void g l D i s a b l e (GLenum mode )
-
OpenGL.
, -
.

3.3. glBegin / glEnd


.
, -
.
, . OpenGL
, -
OpenGL. -


44 3.
void g l B e g i n (GLenum mode )
void glEnd ( void )
mode ,
:

GL_POINTS  -
.

GL_LINES  -
; ,
.

GL_LINE_STRIP  -
.

GL_LINE_LOOP  -
,
, .

GL_TRIANGLES  -
; -
, .

GL_TRIANGLE_STRIP  -
.

GL_TRIANGLE_FAN  -
( -
).

GL_QUADS  -
;
, .

GL_QUAD_STRIP  n -
2n 1, 2n, 2n + 2, 2n + 1.
GL_POLYGON  -
.
3.3. GLBEGIN / GLEND 45

,
, :

GLfloat BlueCol [ 3 ] = { 0 , 0 , 1 } ;

g l B e g i n (GL_TRIANGLES) ;
g l C o l o r 3 f ( 1 . 0 , 0 . 0 , 0 . 0 ) ; /* */
glVertex3f (0.0 , 0.0 , 0 . 0 ) ;
glColor3ub ( 0 , 2 5 5 , 0 ) ; /* */
glVertex3f (1.0 , 0.0 , 0 . 0 ) ;
g l C o l o r 3 f v ( BlueCol ) ; /* */
glVertex3f (1.0 , 1.0 , 0 . 0 ) ;
glEnd ( ) ;
,
.
-
,
, GL_TRIANGLE_STRIP, GL_QUAD_STRIP,
GL_TRIAGLE_FAN.
,
. -
.
,
,
.

void glFrontFace (GLenum mode )


mode GL_CW (clockwise), -
, GL_CCW (counter-
clockwise).
-

void glPolygonMode (GLenum f a c e , Glenum mode )


46 3.
mode -
, face ,

:

GL_FRONT  ;

GL_BACK  ;

GL_FRONT_AND_BACK  .

mode :

GL_POINT  ;

GL_LINE 
;

GL_FILL 
,
.


. -
glEnable (GL_CULL_FACE), -

void g l C u l l F a c e (GLenum mode )


GL_FRONT
, GL_BACK 
( ).
-
GLU GLUT ,
, , ( GLU) , , , , -
, , , ( GLUT). -

GLU ( OpenGL -
6).
3.4. 47

, , -
GLUquadricObj

GLUquadricObj * gluNewQuadric ( void ) ;


:

void gluSphere ( GLUquadricObj * qobj ,


GLdouble radius ,
GLint s l i c e s ,
GLint s t a c k s )

void g l u C y l i n d e r ( GLUquadricObj * qobj ,


GLdouble baseRadius ,
GLdouble topRadius ,
GLdouble height ,
GLint s l i c e s ,
GLint s t a c k s )
slices z,
stacks  z.

.

3.4.

, -
(display list) .
, ,
, ,
:

void glNewList ( GLuint l i s t , GLenum mode )


void glEndList ( )
48 3.
-
, -
list . mode ,
:

GL_COMPILE  -
;

GL_COMPILE_AND_EXECUTE  -
, .

, ,

void g l C a l l L i s t ( GLuint l i s t )
list .
, -

void g l C a l l L i s t s (
G L s i z e i n , GLenum type ,
const GLvoid *lists )
n lists,
type. -
GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT,
GL_INT, GL_UNSIGNED_INT . -

void g l D e l e t e L i s t s ( GLint l i s t , G L s i z e i range )


ID
list ID list + range 1.
:

glNewList ( 1 , GL_COMPILE) ;
g l B e g i n (GL_TRIANGLES) ;
glVertex3f (1.0 f , 1.0 f , 1.0 f ) ;
glVertex3f (10.0 f , 1.0 f , 1.0 f ) ;
glVertex3f (10.0 f , 10.0 f , 1.0 f ) ;
3.5. 49

glEnd ( ) ;
glEndList ( )

...
glCallList (1);
() -
, -
. -
, ,
, .
( ) -
.

3.5.
, , -
glVertex, , -

void g l V e r t e x P o i n t e r ( GLint s i z e , GLenum type ,


G L s i z e i s t r i d e , void * ptr )
.
size (
2, 3, 4), type (
GL_SHORT, GL_INT, GL_FLOAT, GL_DOUBLE).
, -
stride
; stride , ,
. ptr -
, .
,
,

void glNormalPointer ( GLenum type , G L s i z e i s t r i d e ,


50 3.
void * p o i n t e r )
void g l C o l o r P o i n t e r ( GLint s i z e , GLenum type ,
G L s i z e i s t r i d e , void * p o i n t e r )
,
,

void g l E n a b l e C l i e n t S t a t e (GLenum array )


GL_VERTEX_ARRAY, GL_NORMAL_ARRAY,
GL_COLOR_ARRAY .
-

void g l D i s a b l e C l i e n t S t a t e (GLenum array )


array.
-

void glArrayElement ( GLint index )


OpenGL , -
index. -
glColor, glNormal, glVertex c -
. -

void glDrawArrays (GLenum mode , GLint f i r s t ,


G L s i z e i count )
count , mode,
f irst f irst +
count 1.
glArrayElement() .
, ,
-
.

3.6. 51

void glDrawElements (GLenum mode , G L s i z e i count ,


GLenum type , void * i n d i c e s )
indices  , -
type -
,
GL_UNSIGNED_BYTE,
:
GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, count
.
, -
OpenGL, ,
, .


.

3.6.
1) -

OpenGL?

2)
?

3) OpenGL?

4) ?
OpenGL.

5) OpenGL ?
?

6) OpenGL glEnable
glDisable ?
52 3.
7)
OpenGL?

8) ?
?

9)
.

10) glDrawElements().
3.6. 53

. 3.1. OpenGL.
4.

OpenGL -
: , . -
-
z: -
,  . x
, y  .


gluPerspective() , glOrtho(),
4.3.
. -

.

, OpenGL -
, -
. -
: OpenGL -
.

55
56 4.

() () ()

. 4.1. OpenGL. ()  ,
()  , ()  .

4.1.

OpenGL , -


: -,
. 4 4. -
,
, .
, -
( ),
.
-
OpenGL, -
( , glVertex.)
, , -
:

void glMatrixMode (GLenum mode )


mode,
GL_MODELVIEW, GL_PROJECTION GL_TEXTURE, -
- ,
, .
, ,
4.1. 57

.
-

void glLoadMatrix [ f d ] ( GLtype *m)


m 16 oat double
,
, ,
. : m
.

void g l L o a d I d e n t i t y ( void )
.

, -

void glPushMatrix ( void )


void glPopMatrix ( void )
-
, . -
32, -
 2.
-

void glMultMatrix [ f d ] ( GLtype *m)


m 4 4.
M , T ,
glMultMatrix -
M T .
-
,
.
58 4.
, -
,
4.2.

. 4.2. OpenGL

: OpenGL
-
. -
glVertex
.

4.2. -
- -
, .
-
-
4.2. - 59

:
0
x x
y 0 y
=M
z 0 z
1 1
M  - . -
-
.
:

void g l T r a n s l a t e [ f d ] ( GLtype x , GLtype y , GLtype z )


void g l R o t a t e [ f d ] ( GLtype angle ,
GLtype x , GLtype y , GLtype z )
void g l S c a l e [ f d ] ( GLtype x , GLtype y , GLtype z )
glTranslate , -
.
glRotate
(x,y,z).
angle ( )
glScale (
) (x,y,z) ,
.
, -
, .
, , , ,
, -
glPushMatrix,
glRotate , ,
,
glPopMatrix.
,
, -
- .

60 4.
void gluLookAt (
GLdouble eyex , GLdouble eyey , GLdouble eyez ,
GLdouble centx , GLdouble centy , GLdouble centz ,
GLdouble upx , GLdouble upy , GLdouble upz )
(eyex, eyey, eyez) ,
(centx, centy, centz) , -
, (upx, upy, upz)
y , .
, , , -
(0, 1, 0), (0, 1, 0) .
, -
, -
. , gluLookAt()
, -
- .
: OpenGL -
. , -
, , -
glTranslate() ,  glRotate().
.

4.3.
OpenGL
() .

void glOrtho ( GLdouble l e f t , GLdouble r i g h t ,


GLdouble bottom , GLdouble top ,
GLdouble near , GLdouble f a r )

void gluOrtho2D ( GLdouble l e f t , GLdouble r i g h t ,


GLdouble bottom , GLdouble top )
4.3. 61

-
( ) -
.

(lef t, bottom, znear)


(right, top, zf ar), -
. near far
-
(0, 0, 0) .

. 4.3.

, , near far
1 1 . ,
OpenGL .
,
glVertex2.

void g l u P e r s p e c t i v e ( GLdouble angley , GLdouble aspect ,


GLdouble znear , GLdouble z f a r )
-
. angley
0 180.
62 4.
x aspect, -
(
, ).

. 4.4.

zfar znear
-
. zf ar/znear, -
,

0 1.

, -

glMatrixMode(GL_PROJECTION)
glLoadIdentity().

/* */
glMatrixMode (GL_PROJECTION) ;
glLoadIdentity ( ) ;
glOrtho ( 0 , w, 0 , h , 1.0 , 1 . 0 ) ;
4.4. 63

4.4.

(clipped)
. -
:

xn xc /wc
yn = yc /wc
zn zc /wc
-
,

void glViewPort ( GLint x , GLint y ,


GLint width , GLint h e i g h t )

-
(x,y) . -
-
, (0,0) .
glViewPort(), OpenGL -
(ox,oy) -
:
ox = x + width/2
oy = y + height/2
px = width, py = height,
:

xw (px /2)xn + ox
yw = (py /2)yn + oy
zw [(f n)/2]zn + (n + f )/2
n f -
-
0 1 . -
(z-),
64 4.
.
n f

void glDepthRange ( GLclampd n , GLclampd f )


glViewPort() , -
glutReshapeFunc(),
, -
.

4.5.
1) OpenGL?

2) OpenGL.
OpenGL -
?

3) ?

4)
OpenGL.

5) glTranslate() ,
glRotate() glScale ()
gluLookAt(0, 0, 10, 10, 0, 0, 0, 1, 0)?
6) -
?

7) ? -
?
5.

-
, ,
.
, , -
, -
.
, ,
. , -
OpenGL.

5.1.
OpenGL ,
: -
, ,
. -
-
, glScale
. , -
,

65
66 5.

glEnable (GL_NORMALIZE)
-

void glLightModel [ i f ] (GLenum pname , GLenum param )


void glLightModel [ i f ] v (GLenum pname ,
const GLtype * params )
pname , -
-
:

GL_LIGHT_MODEL_LOCAL_VIEWER 
param -
. GL_FALSE, -
z -
x . GL_TRUE,
-
. , -
.

: GL_FALSE.
GL_LIGHT_MODEL_TWO_SIDE  param -
-
, .
GL_FALSE,
. GL_TRUE,
.

: GL_FALSE.
GL_LIGHT_MODE_AMBIENT  params -
, -

.

: (0.2, 0.2, 0.2,1.0).


5.2. 67

5.2.

void g l M a t e r i a l [ i f ] (GLenum f a c e , GLenum pname ,


GLtype param )
void g l M a t e r i a l [ i f ] v (GLenum f a c e , GLenum pname ,
GLtype * params )
,
, -
,
. -
param, pname:
GL_AMBIENT  params
RGBA,
(
).

: (0.2, 0.2, 0.2, 1.0).


GL_DIFFUSE  params
RGBA,
.

: (0.8, 0.8, 0.8, 1.0).


GL_SPECULAR  params -
RGBA, -
.

: (0.0, 0.0, 0.0, 1.0).


GL_SHININESS  params
0 128,
-
.

: 0.
68 5.
GL_EMISSION  params
RGBA,
.

: (0.0, 0.0, 0.0, 1.0).


GL_AMBIENT_AND_DIFFUSE 
glMaterial() pname
GL_AMBIENT GL_DIFFUSE
params.

, glMaterial[ i f ]() -
-
(shininess). glMaterial[ i f ]v()
.
face , -
GL_FRONT,
GL_BACK GL_FRONT_AND_BACK.

, ,
glEnable() c GL_COLOR_MATERIAL,

void g l C o l o r M a t e r i a l (GLenum f a c e , GLenum pname )


face , pname
. -
pname -
( ) -
glColor, -
glMaterial .
9.
:

float mat_dif [ ] = { 0 . 8 , 0 . 8 , 0 . 8 } ;
float mat_amb [ ] = { 0 . 2 , 0 . 2 , 0 . 2 } ;
float mat_spec [ ] = { 0 . 6 , 0 . 6 , 0 . 6 } ;
5.3. 69

float s h i n i n e s s = 0 . 7 * 1 2 8 ;
...
g l M a t e r i a l f v (GL_FRONT_AND_BACK,GL_AMBIENT,
mat_amb ) ;
g l M a t e r i a l f v (GL_FRONT_AND_BACK,GL_DIFFUSE,
mat_dif ) ;
g l M a t e r i a l f v (GL_FRONT_AND_BACK,GL_SPECULAR,
mat_spec ) ;
g l M a t e r i a l f (GL_FRONT, GL_SHININESS ,
shininess );

5.3.
, -
.
(, , ,
,
, . glLightModel). -

void g l L i g h t [ i f ] (GLenum l i g h t , GLenum pname ,


GLfloat param )
void g l L i g h t [ i f ] (GLenum l i g h t , GLenum pname ,
GLfloat * params )
light .

GL_LIGHTi, i 0 -
GL_MAX_LIGHT, -
.
pname params , -
glMaterial. pname:
GL_SPOT_EXPONENT  param -
0 128,
70 5.
. -
.

: 0 ( ).

GL_SPOT_CUTOFF  param
0 90
180, -
.
, .

: 180 ( ).

GL_AMBIENT  params
RGBA,
.

: (0.0, 0.0, 0.0, 1.0).


GL_DIFFUSE  params
RGBA,
.

: (1.0, 1.0, 1.0, 1.0) GL_LIGHT0


(0.0, 0.0, 0.0, 1.0) .

GL_SPECULAR  params -
RGBA, -
.

: (1.0, 1.0, 1.0, 1.0) GL_LIGHT0


(0.0, 0.0, 0.0, 1.0) .

GL_POSITION  params
, -
. w -
0.0,

(x, y, z), ,
(x, y, z, w).
5.3. 71

, .. -
.

: (0.0, 0.0, 1.0, 0.0).


GL_SPOT_DIRECTION  params -
, -
.

: (0.0, 0.0, 1.0, 1.0).


,
GL_SPOT_CUTOFF 180 (, , -
).

GL_CONSTANT_ATTENUATION ,

GL_LINEAR_ATTENUATION ,

GL_QUADRATIC_ATTENUATION  params
, -

.
. (.
GL_POSITION),
:

attconstant + attlinear d + attquadratic d2

d  -
; attconstant, attlinear
attquadratic , -
GL_CONSTANT_ATTENUATION,
GL_LINEAR_ATTENUATION
GL_QUADRATIC_ATTENUATION .
(1, 0, 0),
.
72 5.
-
: OpenGL -
, , .

OpenGL  , ,
-
, ..
OpenGL. , -
,
. , , -
, ,
.

: e -
glLight -
(, glLookAt()), ,
(0, 0, 0) -
, ,
.

-
-
, , .. -
.
-
glEnable(GL_LIGHTING), -
glEnable(GL_LIGHTi).
, -
,
glColor.
,
.

-
, -
.
5.4. 73

,
, -
.

5.4.
-
OpenGL  .
,
, , -
.
OpenGL -
, .. -
.
,
OpenGL.
 . 7.

glEnable(GL_FOG).

void glFog [ i f ] (enum pname , T param )


void glFog [ i f ] v (enum pname , T params )
pname :

GL_FOG_MODE  param ,
.

param :

GL_EXP  f = e(dz) ;
2
GL_EXP2  f = e(dz) ;
74 5.
GL_LINEAR  f =
e z/e s, z  , -
, .

d, e, s -
pname:
GL_FOG_DENSITY  param d
GL_FOG_START  param s
GL_FOG_END  param e.
pname,
GL_FOG_COLOR. params 
4- .
:

GLfloat FogColor [ 4 ] = { 0 . 5 , 0 . 5 , 0 . 5 , 1 } ;
glEnable (GL_FOG) ;
g l F o g i (GL_FOG_MODE,GL_LINEAR) ;
g l F o g f (GL_FOG_START, 2 0 . 0 ) ;
g l F o g f (GL_FOG_END, 1 0 0 . 0 ) ;
g l F o g f v (GL_FOG_COLOR, FogColor ) ;

5.5.
1) -
.

2) glColorMaterial?
3) , -
?

4) ?

?
5.5. 75

5) ?

6) , ,
?
6.

,
, , -
.
-
:

-
;

OpenGL;

,
;

6.1.

OpenGL.

77
78 6.
-
. -

BMP.
,
GLAUX (
glaux.lib), -
.

AUX_RGBImageRec* auxDIBImageLoad ( const char * f i l e )


le  *.bmp *.dib. -
, -
.

. -, ,
,
. -

. -
, -
. -

void gluScaleImage (GLenum format , GLint widthin ,


GL h e i g h t i n , GLenum typein ,
const void * datain ,
GLint widthout ,
GLint heightout , GLenum typeout ,
void * dataout )
format

GL_RGB GL_RGBA, -
. widthin, heightin , widhtout, heightout
, -
typein typeout , -
6.1. 79

datain dataout. , -
GL_UNSIGNED_BYTE, GL_SHORT, GL_INT ..
,
dataout.
-, ,
-
. ,
-
(mipmap).
, -
, , .

. , 2m 2n
max(m, n) + 1 , -
.
-
OpenGL

void gluBuild2DMipmaps (GLenum t a r g e t ,


GLint components ,
GLint width , GLint height ,
GLenum format , GLenum type ,
const void * data )
target GL_TEXTURE_2D. -
components
:

GL_LUMINANCE   (
);

GL_RGB  , , ;

GL_RGBA  , , , (. . 7.1).

width, height, data -


, format type -
, gluScaleImage().
80 6.

OpenGL, ,
, .
OpenGL ,
1 N, , -
target GL_TEXTURE_1D.
,
.
, OpenGL
, -
( ).

void glGenTextures ( G L s i z e i n , GLuint * t e x t u r e s )


n , -
textures. -
( -
),

void glBindTexture (GLenum t a r g e t , GLuint t e x t u r e )


target GL_TEXTURE_1D
GL_TEXTURE_2D, texture -
, -
. ,
,
glBindTexture() c -
target texture. , glBindTexture()
texture,
, -
, .

,
256256 512512 . ,
.
6.2. 81

6.2.
, , -
,
, . -
, , , -
,
. -

(s, t), s t [0, 1]
(. 6.1)

. 6.1.

-
:

void glTexParameter [ i f ] (GLenum t a r g e t , GLenum pname ,


GLenum param )
void glTexParameter [ i f ] v (GLenum t a r g e t , GLenum pname ,
Glenum * params )
target GL_TEXTURE_1D
GL_TEXTURE_2D, pname , -
, param params -
. pname:

GL_TEXTURE_MIN_FILTER  param -
82 6.
,
. GL_NEAREST -
(), GL_LINEAR  -
.

: GL_LINEAR.
GL_TEXTURE_MAG_FILTER  param -
, -
() . GL_NEAREST
(),
GL_LINEAR  .

: GL_LINEAR.
GL_TEXTURE_WRAP_S  param -
s,
[0, 1]. GL_REPEAT s -
, -
. GL_CLAMP -
(0 1), ,
.

: GL_REPEAT.
GL_TEXTURE_WRAP_T  -
, t.

GL_NEAREST -
, , ,
GL_LINEAR, .
, -
, ,

void glTexEnv [ i f ] (GLenum t a r g e t , GLenum pname ,


GLtype param )
void glTexEnv [ i f ] v (GLenum t a r g e t , GLenum pname ,
GLtype * params )
6.2. 83

target GL_TEXTURE_ENV,

pname

GL_TEXTURE_ENV_MODE,
.
param:
GL_MODULATE  -

.

GL_REPLACE 
.

-
:

/* */
#define NUM_TEXTURES 10
/* */
int TextureIDs [NUM_TEXTURES] ;
/* */
AUX_RGBImageRec * pImage ;

...
/* 1) */
glGenTextures (NUM_TEXTURES, TextureIDs ) ;

/* 2) */
glBindTexture ( TextureIDs [ i ] ) ; /* 0<=i <NUM_TEXTURES*/

/* 3) .
" 2 */
pImage=dibImageLoad ( " t e x t u r e . bmp" ) ;

i f ( Texture !=NULL)
{
/* 4) OpenGL */
84 6.
/* */
g l P i x e l S t o r e i (GL_UNPACK_ALIGNMENT, 1 ) ;

gluBuildMipmaps (GL_TEXTURE_2D,GL_RGB, pImage>sizeX ,


pImage>sizeY , GL_RGB, GL_UNSIGNED_BYTE,
pImage>data ) ;

glTexParameterf (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,
( float )GL_LINEAR) ;

glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,


( float )GL_LINEAR) ;

glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,


( float )GL_REPEAT) ;

glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,


( float )GL_REPEAT) ;

glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,


( float )GL_REPLACE) ;

/* 5) . */
f r e e ( Texture ) ;

}
else
Error ( ) ;

6.3.


. : -
,
6.3. 85

void glTexCoord [ 1 2 3 4 ] [ s i f d ] ( type coord )


void glTexCoord [ 1 2 3 4 ] [ s i f d ] v ( type * coord )
glTexCoord2*(type s, type t),
. -
-
.
-
, GLU
,
, ,
.

void gluQuadricTexture (
GLUquadricObj * quadObject ,
GLboolean textureCoords )
textureCoords GL_TRUE,
.

void glTexGen [ i f d ] (GLenum coord , GLenum pname ,


GLtype param )
void glTexGen [ i f d ] v (GLenum coord , GLenum pname ,
const GLtype * params )
coord
, GL_S, GL_T; pname -
:

GL_TEXTURE_GEN_MODE  -
. param -
:

GL_OBJECT_LINEAR 

86 6.
, pname
GL_OBJECT_PLANE (. ).
:

g = x xp + y yp + z zp + w wp

g  (s
p), x, y, z, w  -
. xp , yp , zp , wp  -
. .

GL_EYE_LINEAR  GL_OBJECT_LINEAR,
.
.. -
.

GL_SPHERE_MAP  -
. -
. -
-
.

GL_OBJECT_PLANE  , -
-
, GL_OBJECT_LINEAR.
params -
.

GL_EYE_PLANE  . -
GL_EYE_LINEAR.


glEnable
GL_TEXTURE_GEN_S GL_TEXTURE_GEN_P.
, ,
.
6.4. 87

6.4.
1) ?

2) -
?

3) -
, , -
( GL_MODULATE, GL_REPLACE)?
4)
OpenGL.

5) (mip-
mapping)?

6)
OpenGL?
7.

-
, .., OpenGL
,
, , .

,
.
OpenGL , -
. OpenGL -
, -
.

.
OpenGL
:

- ();

89
90 7.
.

, -
. -
(front) (back) .
, , -
.
.
-
.
- -
. 7.2.

(), -
, .
7.3, 8.2
8.3.

7.1. -

 ,
.. , -
.
OpenGL -
,
.
-
(blending). -
(.. -
) -
, .
 -,
7.1. 91

-. -
-
, , .. -

.
glEnable(GL_BLEND).
-
:

void glBlendFunc (enum src , enum dst )


src k1 -
, a dst -
k2 . -
: res = csrc
k1 + cdst k2 , csrc  , cdst  -
(res, k1 , k1 , csrc , cdst 
RGBA-).

src dst.
GL_SRC_ALPHA k = (As , As , As , As )
GL_SRC_ONE_MINUS_ALPHA

k = (1, 1, 1, 1) (As , As , As , As )

GL_DST_COLOR k = (Rd , Gd , Bd )
GL_ONE_MINUS_DST_COLOR

k = (1, 1, 1, 1) (Rd , Gd , Bd , d )

GL_DST_ALPHA k = (Ad , A d, A d, Ad )
GL_DST_ONE_MINUS_ALPHA

k = (1, 1, 1, 1) (Ad , Ad , Ad , Ad )
92 7.
GL_SRC_COLOR k = (Rs, Gs, Bs)
GL_ONE_MINUS_SRC_COLOR

k = (1, 1, 1, 1) (Rs , Gs , Bs , As )

: , -
. -
. 1  ; 0  -
, .. . -
:

glEnable (GL_BLEND) ;
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ;
, -
:

glColor3f (1.0 , 0.0 , 0.0 , 0.5);


g l B e g i n (GL_TRIANGLES) ;
glVertex3f (0.0 , 0.0 , 0 . 0 ) ;
glVertex3f (1.0 , 0.0 , 0 . 0 ) ;
glVertex3f (1.0 , 1.0 , 0 . 0 ) ;
glEnd ( ) ;
,
, -
:

-
, .. ,
.

OpenGL -
, -

glVertex, .
7.2. - 93

7.2. -
- (accumulation buer)  -
OpenGL. -
, -
. -
.
, -

void glReadBuffer (enum buf )


buf . buf,
GL_BACK, GL_FRONT,
. GL_BACK -
; GL_FRONT  -
. ,
.
, ( -
, OpenGL , -
, , ,
).
- .
,
. -
, ,
.
--
,

void glDrawBuffer (enum buf )


buf -
glReadBuer.
- -

94 7.
void glAccum (enum op , GLfloat value )
op -
:

GL_LOAD  , -
, value -
;

GL_ACCUM  , -

;

GL_MULT  -
value;

GL_ADD  , -
;

GL_RETURN  -
, . -
value.

, --
- glEnable.
.
-
() -
8.1.

7.3.
-
, -
, .. () . -
OpenGL (stencil
7.3. 95

buer). ,
.
, -
OpenGL () -
.
, .
-
:

void g l S t e n c i l F u n c (enum func , int r e f , u i n t mask )


void g l S t e n c i l O p (enum s f a i l , enum d p f a i l ,
enum dppass )
ref glStencilFunc -
. 0 2s 1, s
.
func .
:

GL_NEVER  , .. -
false;

GL_ALWAYS  ;

GL_LESS, GL_LEQUAL, GL_EQUAL

GL_GEQUAL, GL_GREATE, GL_NOTEQUAL 


, ref -
, , , ,
, .

mask . ..
: ((ref AND mask) op
(svalue AND mask)).
glStencilOp
-
.
96 7.
sfail -
, :

GL_KEEP, GL_ZERO, GL_REPLACE, GL_INCR

GL_DECR, GL_INVERT  -
, ,
( ref ), , -
.

dpfail -
z-, dppass
. -
, sfail.
GL_KEEP.
-
glEnable(GL_STENCIL_TEST);
-
, , ,
.

8.2 8.3.

7.4.
-
glHint (target , mode), target 
,
:

GL_FOG_HINT  -
. (-
) . -
OpenGL ,
;
7.4. 97

GL_LINE_SMOOTH_HINT  -
. mode, GL_NICEST, -
-
;

GL_PERSPECTIVE_CORRECTION_HINT 
-
. OpenGL
GL_NICEST, -
;

GL_POINT_SMOOTH_HINT  -
. mode, GL_NICEST,
;

GL_POLYGON_SMOOTH_HINT  -
.

mode :

GL_FASTEST  ;

GL_NICEST  , -
;

GL_DONT_CARE  -
.

, glHint()
-
.
OpenGL .

, glHint() -
glBegin()/glEnd().
98 7.
7.5.
1) OpenGL
?

2) glBlendFunc?
3) -
-
?

4) -? -
.

5) OpenGL
?

6) , glHint() .
7)
glHint(GL_FOG_HINT, GL_DONT_CARE)?
II

OpenGL

99
8.


OpenGL

OpenGL -
, -
-
.

8.1.
(antialiasing). -
(aliasing) -
-
( , ) .
. , -
. -
,
. ,
.

101
102 8. OPENGL
,
. , , -
. ,
-, , -
, -
.

,
, -
, , -
.

--
1/n, n 
.  -
, .

for ( i =0; i <samples_count;++ i )


// samples_count 5 10
{
ShiftCamera ( i ) ; //
RenderScene ( ) ;
i f ( i ==0)
//
glAccum (GL_LOAD, 1 / ( float ) samples_count ) ;
else
//
glAccum (GL_ACCUM, 1 / ( float ) samples_count ) ;
}
// , ,
glAccum (GL_RETURN, 1 . 0 ) ;

,
, , -
,
. -
.
8.2. 103

8.2.
OpenGL
. -
, ,
OpenGL. -

, -
.

,
,
. -
. , -
.

:
,
, ,
. , -
. -
.

P  ,
.

L  , -
.

S = a(L P ) P  ,
P , a  .

, z = 0.
a = zp /(zl zp ). ,
104 8. OPENGL

xs = (xp zl zl zp )/(zl zp )
ys = (yp zl yl zp )/(zl zp )
zs = 0
:

xs = x0s /ws0
ys = ys0 /ws0
zs = 0
ws0 = z l zp
S -
:


zl 0 0 0
 0 z l 0 0
x0s ys0 0 ws0 = xs ys zs

1
xl yl

0 1
0 0 0 zl

, ,
,
S P , :
aP + bL, a b  .

:


xn
yn
G=
zn
d
, ,
P , G, -
a b, :
8.2. 105

(aP + bL)G = 0
: a(P G) + b(LG) = 0. -

a = (LG), b = (P G)
, S = (LG)P
(P G)L. ,

S = P [(LG)I GL] (8.1)

I  .
(LG)I GL
.

OpenGL.
, oorShadow
(LG)I GL.
:

/* */
RenderGeometry ( ) ;

/*
( b l e n d i n g ) */
glEnable (GL_BLEND) ;
glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA) ;
g l D i s a b l e (GL_LIGHTING ) ;
glColor4f (0.0 , 0.0 , 0.0 , 0.5);
glPushMatrix ( ) ;
/* */
glMultMatrixf ( ( GLfloat * ) floorShadow ) ;
/* */
106 8. OPENGL
RenderGeometry ( ) ;
glPopMatrix ( ) ;
glEnable (GL_LIGHTING ) ;
g l D i s a b l e (GL_BLEND) ;

oorShadow 8.1
:

/* :
p l a n e
l i g h t p o s
: m a t r i x
*/
void shadowmatrix ( GLfloat matrix [ 4 ] [ 4 ] ,
GLfloat plane [ 4 ] ,
GLfloat l i g h t p o s [ 4 ] )
{
GLfloat dot ;

dot = plane [ 0 ] * lightpos [0] +


plane [ 1 ] * lightpos [1] +
plane [ 2 ] * lightpos [2] +
plane [ 3 ] * lightpos [ 3 ] ;

matrix [ 0 ] [ 0 ] = dot lightpos [0] * plane [ 0 ] ;


matrix [ 1 ] [ 0 ] = 0. f lightpos [0] * plane [ 1 ] ;
matrix [ 2 ] [ 0 ] = 0. f lightpos [0] * plane [ 2 ] ;
matrix [ 3 ] [ 0 ] = 0. f lightpos [0] * plane [ 3 ] ;

matrix [ 0 ] [ 1 ] = 0. f lightpos [1] * plane [ 0 ] ;


matrix [ 1 ] [ 1 ] = dot lightpos [1] * plane [ 1 ] ;
matrix [ 2 ] [ 1 ] = 0. f lightpos [1] * plane [ 2 ] ;
matrix [ 3 ] [ 1 ] = 0. f lightpos [1] * plane [ 3 ] ;

matrix [ 0 ] [ 2 ] = 0. f lightpos [2] * plane [ 0 ] ;


matrix [ 1 ] [ 2 ] = 0. f lightpos [2] * plane [ 1 ] ;
matrix [ 2 ] [ 2 ] = dot lightpos [2] * plane [ 2 ] ;
matrix [ 3 ] [ 2 ] = 0. f lightpos [2] * plane [ 3 ] ;
8.2. 107

matrix [ 0 ] [ 3 ] = 0. f lightpos [3] * plane [ 0 ] ;


matrix [ 1 ] [ 3 ] = 0. f lightpos [3] * plane [ 1 ] ;
matrix [ 2 ] [ 3 ] = 0. f lightpos [3] * plane [ 2 ] ;
matrix [ 3 ] [ 3 ] = dot lightpos [3] * plane [ 3 ] ;
}
, , ,
:

, -
, . , -
, -
, , , -
.


(reblending), ..
, -
.


, ..
,
.

,
. -
-
, -
.

. -
(. . 7.2).
,  ()
-
108 8. OPENGL
. -
:

1) 0;

2) ,
1;

3) , -
; ,
2.

.
1.

/* */
g l C l e a r S t e n c i l (0 x0 )
g l C l e a r (GL_STENCIL_BUFFER_BIT) ;

/* */
glEnable (GL_STENCIL_TEST) ;
2.

/*
1 */
g l S t e n c i l F u n c (GL_ALWAYS, 0x1 , 0 x f f f f f f f f ) ;

/* */
g l S t e n c i l O p (GL_REPLACE, GL_REPLACE, GL_REPLACE) ;

/* ,
*/
RenderPlane ( ) ;
3.

/*
1 */
8.3. 109

g l S t e n c i l F u n c (GL_EQUAL, 0x1 , 0 x f f f f f f f f ) ;

/* 2 , */
g l S t e n c i l O p (GL_KEEP, GL_KEEP, GL_INCR) ;

/* */
RenderShadow ( ) ;
, -
, z-. -
, .

c ,
.
.

8.3.
-
. -
-
.
-
: -
 . ,
: 1)
2) .
-
, .
.
, , ,
, -
, -
,
110 8. OPENGL

. 8.1.

.
-
:

1) , -.

2) ,
.

3) ,
.
-.

-
.
.
. -
. ,
, OpenGL -
, :
8.3. 111

g l C l e a r (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|
GL_STENCIL_BUFFER_BIT) ;
,
.

.

glEnable (GL_STENCIL_TEST) ;
/*
1 */
g l S t e n c i l F u n c (GL_ALWAYS, 1 , 0 ) ;
g l S t e n c i l O p (GL_KEEP, GL_KEEP, GL_REPLACE) ;

RenderMirrorObject ( ) ;
:

 , -
;

(, )
1.

, -
.
. -

, -. ,
, (
):

void r e f l e c t i o n m a t r i x ( GLfloat r e f l e c t i o n _ m a t r i x [ 4 ] [ 4 ] ,
GLfloat plane_point [ 3 ] ,
G l f l o a t plane_normal [ 3 ] )
{
GLfloat * p ;
112 8. OPENGL
GLfloat * v ;
float pv ;
GLfloat * p = ( G l f l o a t * ) plane_point ;
G l f l o a t * v = ( G l f l o a t * ) plane_normal ;
float pv = p [ 0 ] * v [0]+ p [ 1 ] * v [1]+ p [ 2 ] * v [ 2 ] ;
reflection_matrix [0][0] = 1 2 * v[0] * v [0];
reflection_matrix [1][0] = 2 * v[0] * v [1];
reflection_matrix [2][0] = 2 * v[0] * v [2];
reflection_matrix [3][0] = 2 * pv * v [ 0 ] ;

reflection_matrix [0][1] = 2 * v[0] * v [1];


reflection_matrix [1][1] = 1 2 * v [ 1 ] * v [ 1 ] ;
reflection_matrix [2][1] = 2 * v[1] * v [2];
reflection_matrix [3][1] = 2 * pv * v [ 1 ] ;

reflection_matrix [0][2] = 2 * v[0] * v [2];


reflection_matrix [1][2] = 2 * v[1] * v [2];
reflection_matrix [2][2] = 1 2 * v[2] * v [2];
reflection_matrix [3][2] = 2 * pv * v [ 2 ] ;

reflection_matrix [0][3] = 0;
reflection_matrix [1][3] = 0;
reflection_matrix [2][3] = 0;
reflection_matrix [3][3] = 1;
}
,
1:

/*
1 */
g l S t e n c i l F u n c (GL_EQUAL, 0x1 , 0 x f f f f f f f f ) ;

/* */
g l S t e n c i l O p (GL_KEEP, GL_KEEP, GL_KEEP) ;
( )
8.4. 113

glPushMatrix ( ) ;
glMultMatrixf ( ( float * ) r e f l e c t i o n _ m a t r i x ) ;
RenderScene ( ) ;
glPopMatrix ( ) ;
, :

g l D i s a b l e (GL_STENCIL_TEST) ;

, , - 
..
, -
,
. -
, -
.

8.4.
-
? .

OpenGL
?

-
. ,
?
? , ?
9.

9.1.
,
, OpenGL,
-
. , -
.

9.1.1.

OpenGL
. ,
. , -
-
. , -
.
.

115
116 9.
.
(, )
. -
-
.

, ,
, -
.
OpenGL. , -
, -
OpenGL -

( ) .

9.1.2.

, OpenGL,
. -
, ,
.
, , -

OpenGL. , -
,
, OpenGL .
. , ,
. -
 , -
. :

struct c i t y
{
float l a t i t u t e , l o n g i t u d e ; /* */
char * name ; /* */
9.1. 117

int l a r g e _ f l a g ; /* 0 = , 1 = */
};
.
, ,
:

void d r a w _ c i t i e s ( int n , struct c i t y c i t y l i s t [ ] )


{
int i ;
for ( i =0; i < n ; i ++)
{
if ( c i t y l i s t [ i ] . large_flag )
glPointSize ( 4.0 ) ;
else
glPointSize ( 2.0 ) ;

g l B e g i n ( GL_POINTS ) ;
glVertex2f ( c i t y l i s t [ i ] . longitude ,
ci ty li st [ i ] . latitude );
glEnd ( ) ;
/* */
DrawText ( c i t y l i s t [ i ] . l o n g i t u d e ,
c i t y l i s t [ i ] . latitude ,
c i t y l i s t [ i ] . name ) ;
}
}
:

glPointSize ;

glBegin
glEnd ;

void d r a w _ c i t i e s ( int n , struct c i t y c i t y l i s t [ ] )


{
118 9.
int i ;
/* */
glPointSize ( 2.0 ) ;
g l B e g i n ( GL_POINTS ) ;
for ( i =0; i < n ; i ++)
{
i f ( c i t y l i s t [ i ] . l a r g e _ f l a g ==0) {
glVertex2f ( c i t y l i s t [ i ] . longitude ,
ci ty li st [ i ] . latitude );
}
}
glEnd ( ) ;
/* */
glPointSize ( 4.0 ) ;
g l B e g i n ( GL_POINTS ) ;
for ( i =0; i < n ; i ++)
{
i f ( c i t y l i s t [ i ] . l a r g e _ f l a g ==1)
{
glVertex2f ( c i t y l i s t [ i ] . longitude ,
ci ty li st [ i ] . latitude );
}
}
glEnd ( ) ;

/* */
for ( i =0; i < n ; i ++)
{
DrawText ( c i t y l i s t [ i ] . l o n g i t u d e ,
c i t y l i s t [ i ] . latitude ,
c i t y l i s t [ i ] . name ) ;
}
}

glPointSize -
glBegin glEnd.

. -
9.1. 119

, -
. :

struct c i t y _ l i s t
{
int num_cities ; /* */
float * p o s i t i o n ; /* */
char ** name ; /* */
float s i z e ; /* , */
};
.
.

glBegin/glEnd -
.
:

void d r a w _ c i t i e s ( struct c i t y _ l i s t *list )


{
int i ;
/* */
g l P o i n t S i z e ( l i s t >s i z e ) ;

g l V e r t e x P o i n t e r ( 2 , GL_FLOAT, 0 ,
l i s t >num_cities ,
l i s t >p o s i t i o n ) ;
glDrawArrays ( GL_POINTS, 0 , l i s t >num_cities ) ;
/* */
for ( i =0; i < l i s t >num_cities ; i ++)
{
DrawText ( c i t y l i s t [ i ] . l o n g i t u d e ,
ci ty li st [ i ] . latitude
c i t y l i s t [ i ] . name ) ;
}
}
120 9.
9.2. OpenGL
9.2.1. OpenGL


OpenGL.

, GL_LINES,
GL_LINE_LOOP, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN
GL_QUAD_STRIP ,
.
, OpenGL.


glVertex/glColor/glNormal
.


GL_TRIANGLE_STRIP (GL_QUAD_STRIP) -
. OpenGL , -
, glDrawElements().

glVertexPointer, glColorPointer,
glNormalPointer

void g l I n t e r l e a v e d A r r a y s ( G l i n t format ,
Glsizei stride ,
void * ptr ) ;
9.2. OPENGL 121

typedef struct tag_VERTEX_DATA


{
float c o l o r [ 4 ] ;
float normal [ 3 ] ;
float v e r t e x [ 3 ] ;
}VERTEX_DATA;
VERTEX_DATA * pData ;

g l I n t e r l e a v e d A r r a y s (GL_C4F_N3F_V3F, 0 , pData ) ;
, oat ,
oat , oat
. -
OpenGL.


-
.
glVertex, glColor, glNormal
glTexCoord.
glVertex, glColor .., -
(, glVertex3fv(v)),
,
glVertex3f(x,y,z).

,
, . -
, , GLU -
.
122 9.
, ,
.

-
. -
, ,
.

, glNormal.
, glTexCoord, ..

-
glBegin/glEnd
high-end
, -
.
glBegin/glEnd.
:

g l B e g i n (GL_TRIANGLE_STRIP) ;
for ( i =0; i < n ; i ++)
{
if ( lighting )
{
glNormal3fv ( norm [ i ] ) ;
}
glVertex3fv ( vert [ i ] ) ;
}
glEnd ( ) ;
9.2. OPENGL 123

,
lighting . ,
:

if ( lighting )
{
g l B e g i n (GL_TRIANGLE_STRIP) ;
for ( i =0; i < n ; i ++)
{
glNormal3fv ( norm [ i ] ) ;
glVertex3fv ( vert [ i ] ) ;
}
glEnd ( ) ;
}
else
{
g l B e g i n (GL_TRIANGLE_STRIP) ;
for ( i =0; i < n ; i ++)
{
glVertex3fv ( vert [ i ] ) ;
}
glEnd ( ) ;
}

9.2.2.


, glVertex, , -
, ..

(two-
sided lighting).
124 9.
.

GL_SHININESS.

.
,
.

glEnable/Disable(GL_NORMALIZE) -
.
glScale ,
. .

, GL_LINES,
GL_LINE_LOOP, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN,
GL_QUAD_STRIP OpenGL,
, -
.

9.2.3.

-
OpenGL.

, -

, , -
, .
9.2. OPENGL 125


glEnable(GL_CULL_FACE)
,
.

, -
-
. , .


, . -
,
.

9.2.4.

, -
, .

GL_UNSIGNED_BYTE -
OpenGL.

-
.

, ,

126 9.
.


,
.

,

.
.

,
glTexImage2D -
. glTexSubImage2D
glTexCopyTexSubImage2D.

9.2.5.

, , --
. -
,
.

glClear


glClear.
:
9.2. OPENGL 127

g l C l e a r (GL_COLOR_BUFFER_BIT) ;
i f ( s t e n c i l i n g ) /* ? */
{
g l C l e a r (GL_STENCIL_BUFFER_BIT) ;
}
:

if ( stenciling ) /* ? */
{
g l C l e a r (GL_COLOR_BUFFER_BIT |
STENCIL_BUFFER_BIT ) ;
}
else
{
g l C l e a r (GL_COLOR_BUFFER_BIT) ;
}

9.2.6.

GL

glGetError() ,
OpenGL.
, -
OpenGL . -
,
. -
, , :

#include <a s s e r t . h>


#define CHECK_GL \
a s s e r t ( glGetError ( ) != GL_NO_ERROR) ;
:

g l B e g i n (GL_TRIANGLES) ;
glVertex3f (1 ,1 ,1);
glEnd ( ) ;
128 9.
CHECK_GL;

glColorMaterial glMaterial

, glColorMaterial ,
glMaterial.

OpenGL

, OpenGL ( glEnable, glDisable,


glBindTexture ), -
,
.., .

glPolygonMode

-
glBegin GL_POINTS, GL_LINES,
,
GL_LINE_LOOP GL_LINE_STRIP
, -
.
,
OpenGL-. -
, -
.

9.3.
1) -
OpenGL-.

2) -
?
9.3. 129

3) OpenGL ?

glVertex3f (1 ,1 ,1);

float vct [ 3 ] = { 1 , 1 , 1 } ;
g l V e r t e x 3 f v ( vct ) ;
III


OpenGL

131
10.

OpenGL-
GLUT

10.1. GLUT-
-
GLUT. -

,
Windows, Linux
.
GLUT
:

133
134 10. OPENGL- GLUT
;

(,
.).

g l u t I n i t ( int * argcp , char ** argv )


argcp -
argc, main(), argv 
, , -
.
,
GLUT . :

gl u tI ni tW in do wP os it i on ( int x , int y )
glutInitWindowSize ( int width , int h e i g h t )
g l u t I n i t D i s p l a y M o d e ( unsigned int mode )

, -
, -
( | ):

GLUT_RGBA RGBA. , -
GLUT_RGBA GLUT_INDEX.
GLUT_RGB , GLUT_RGBA.
GLUT_INDEX (-
). GLUT_RGBA.
GLUT_SINGLE .
.

GLUT_DOUBLE .
GLUT_SINGLE.
10.1. GLUT- 135

GLUT_STENCIL .

GLUT_ACCUM -.

GLUT_DEPTH .

, -
.

7.
GLUT -
- . , -
, -
-
, . -
: , , ,
, (idle) -
, .
-
, . -
:

void glutDisplayFunc ( void ( * func ) ( void ) )


void glutReshapeFunc ( void ( * func ) ( int width ,
int h e i g h t ) )
void glutMouseFunc ( void ( * func ) ( int button ,
int s t a t e , int x , int y ) )
void g l u t I d l e F u n c ( void ( * func ) ( void ) )
void glutMotionFunc ( void ( * func ) ( int x , int y ) ) ;
void glutPassiveMotionFunc (
void ( * func ) ( int x , int y ) ) ;
-
. glutDisplayFunc() -
,
.
136 10. OPENGL- GLUT
, , -

void g l u t P o s t R e d i s p l a y ( void )
glutReshapeFunc()
,
.
glutMouseFunc() -
, glutIdleFunc() , -
, .
, glutMotionFun(), ,
, -
. glutPassiveMotionFunc , ,
-
.
-

void glutMainLoop ( void )


, -
GLUT. , -
, :

#i n c l u d e <GL/ g l u t . h>

void MyIdle ( void )


{
/* , ,
*/
...
};

void MyDisplay ( void )


{
/* OpenGL , */
...
10.2. GLUT MICROSOFT VISUAL C++ 6.0 137

/* */
glutSwapBuffers ( ) ;
};

void main ( int argcp , char ** argv )


{
/* GLUT */
g l u t I n i t (&argcp , argv ) ;
glutInitWindowSize ( 6 4 0 , 4 8 0 ) ;
gl u tI ni tW in do wP os it i on ( 0 , 0 ) ;
/* */
glutCreateWindow ( "My OpenGL A p p l i c a t i o n " ) ;
/* : RGBA */
g l u t I n i t D i s p l a y M o d e (GLUT_RGBA |
GLUT_DOUBLE | GLUT_DEPTH) ;
/* */
glutDisplayFunc ( MyDisplay ) ;
g l u t I d l e F u n c ( MyIdle ) ;
/* */
glutMainLoop ( ) ;
};
, -
, GLUT_DOUBLE GLUT_SINGLE,
,
glutIdleFunc().

10.2. GLUT Microsoft Visual C++ 6.0


glut.h,
glut32.lib glut32.dll MSVC\Include\Gl, MSVC\Lib,
Windows\System . -
gl.h, glu.h, opengl32.lib, glu32.lib,
opengl32.dll, glu32.dll, Visual
C++ Windows.
138 10. OPENGL- GLUT
GLAUX
 glaux.h glaux.lib.
-
:

 

: File New
Projects Win32 Console Application, ,
OK.

An empty project, Finish,


OK.


  
-
( File New Files Text File),

 
*.c *.cpp (-
Project Add To Project Files).


 
OpenGL. -
Project Settings Link Object\library
modules : opengl32.lib,
glu32.lib, glut32.lib , , glaux.lib.



Build Build program.exe,
 Build Execute program.exe.


 
, -
Project Settings Link Project Options -
subsystem:console subsystem:windows, -
/entry:mainCRTStartup.


Release
. Build Set Active Conguration
. . . -Win32 Release, -
.
10.3. GLUT MICROSOFT VISUAL C++ 2005 139

10.3. GLUT Microsoft Visual C++


2005
glut.h,
glut32.lib glut32.dll MVS8\VC\PlatformSDK\Include,
MVS8\VC\PlatformSDK\Lib, Windows\System .
gl.h,
glu.h, opengl32.lib, glu32.lib, opengl32.dll, glu32.dll, -
Visual C++ Windows.
GLAUX
 glaux.h glaux.lib.
-
:

 
 
: File New
Projects Win32 Win32 Console Application,
, OK.

Application Settings -
Console application, An empty project, Finish.


   

( File New Files Visual C++

 
C++File ), *.c *.cpp
( Project Add To Project Files).


  
OpenGL. -


Project Properties Conguration Properties
Linker Input Additional dependencies -
: opengl32.lib, glu32.lib, glut32.lib
, , glaux.lib.


 
Build program.exe, -
 Debug Start Debugging Debug Start Without
Debugging.
140 10. OPENGL- GLUT

 

 
, Project Properties
Conguration Properties Linker System


SubSystem Console Windows. -
Linker Advanced Entry Point
wmainCRTStartup ( ).


Release
. Build Conguration Manager
Active solution conguration Release,
-
.

10.4. GLUT Borland C++ Builder 6



glut.h, glut32.lib, glut32.dll CBuilder6\Include\Gl,
CBuilder6\Lib, Windows\System .
gl.h, glu.h,
opengl32.lib, glu32.lib, opengl32.dll, glu32.dll,
Borland C++ Windows.
, ( Microsoft
Visual C++) glut32.lib c Borland C++ Builder
6 , -
. ,
'implib',
Builder6\Bin.

implib glut32.lib glut32.dll


lib- dll--
.
10.5. GLUT BORLAND C++ BUILDER 2006 141

-
:

 

: File New
Other Console Wizard, OK.

Source Type  ++, Console


Application, Use VCL, Use CLX, Multi
Threaded. .




, (Project


Remove From Project) *.c
*.cpp ( Project Add To Project).



(
File Save All).



GLUT.
Project Add To Project glut32.lib



Project Build . . . , -
 Run Run.

 
Release
. Project Options
Compiler Release.

10.5. GLUT Borland C++ Builder


2006
-
glut.h, glut32.lib, glut32.dll -
Borland\BDS\4.0\Include\Gl, Borland\BDS\4.0\Lib,
142 10. OPENGL- GLUT
Windows\System .
gl.h, glu.h, opengl32.lib, glu32.lib,
opengl32.dll, glu32.dll, Borland
C++ Windows.
, ( Microsoft
Visual C++) glut32.lib c Borland C++ Builder
6 , -
. ,
'implib',
Borland\BDS\4.0\Bin. -

implib glut32.lib glut32.dll


lib- dll--
.
-
:

 

: File New
Other Console Application, OK.

Source Type  ++, Console


Application, Use VCL, Multi Threaded.
.




, (Project


Remove From Project) *.c
*.cpp ( Project Add To Project).



(
File Save All).



GLUT.
Project Add To Project glut32.lib
10.5. GLUT BORLAND C++ BUILDER 2006 143



Project Build . . . , -
 Run Run.


Release
. Project Build Congurations
Conguration name Release Build.
11.

OpenGL
MFC VCL


Windows OpenGL -
MFC (Microsoft Foundation Classes) VCL
(Visual Component Library).
Windows
, -
 OpenGL -
( ,
..).
, ,
OpenGL, . GLUT -
-
,

, GLUT. -

OpenGL.
OpenGL Windows -

145
146 11. OPENGL MFC VCL
:

1) ( );

) -
(. .11.1);

) (.11.2);

) (.11.3);

2) OpenGL ;

3) ( ).

11.1.
(device context)  -
Windows.
.
, , , -
.
 -
,
. -
, -
, .. .

.
, -
hDC

hWnd:
HWND hWnd = < >;
HDC hDC = GetDC(hWnd ) ;
ReleaseDC:
ReleaseDC (hWnd, hDC ) ;
11.2. 147

11.2.
-
(pixel format) . , -
,
. ,
OpenGL :
, , , ..
, -
PIXELFORMATDESCRIPTOR -
:

PIXELFORMATDESCRIPTOR pfd ;
// ;
ZeroMemory(&pfd , sizeof ( pfd ) ) ;
//
pfd . n S i z e = sizeof ( pfd ) ;
pfd . nVersion = 1 ;
// ,
// OpenGL
Pfd . dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
pfd . iPixelType = PFD_TYPE_RGBA;
// (8 )
pfd . c C o l o r B i t s = 2 4 ;
// 16
pfd . cDepthBits = 1 6 ;
pfd . iLayerType = PFD_MAIN_PLANE;
int iFormat = ChoosePixelFormat (hDC, &pfd ) ;
SetPixelFormat (hDC, iFormat , &pfd ) ;
,
:

int ChoosePixelFormat (
HDC hdc ,
CONST PIXELFORMATDESCRIPTOR * ppfd ) ;
148 11. OPENGL MFC VCL
-
,
, . ,
24- ,
8- ,
8- .
SetPixelFormat -
:

BOOL SetPixelFormat (
HDC hdc , int iPixelFormat ,
CONST PIXELFORMATDESCRIPTOR * ppfd ) ;

, OpenGL-
.
. , -
, :

SwapBuffers ( hDC ) ;

11.3. (render context)



OpenGL. , -
OpenGL,
OpenGL ,
.
OpenGL, , ..
hRC:
HGLRC hRC;
hRC = wglCreateContext (hDC ) ;
-
:
11.4. GLRC 149

wglMakeCurrent (hDC, hRC ) ;


OpenGL, -
SwapBuers .
-
(
):

wglDeleteContext (hRC ) ;

11.4. GLRC
..5 GLRC, -
. -
:

GLRC* m_pGLRC;
...
// 1 .
m_pGLRC = new GLRC(hWnd ) ;
bool r e s = m_pGLRC>Create ( ) ;
if (! res )
Error ( " OpenGL" ) ;
..
// 2 .
// ( WM_PAINT)
bool r e s = m_pGLRC>MakeCurrent ( ) ;
if (! res )
Error ( " " ) ;

// OpenGL
g l C l e a r (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
...

//
m_pGLRC>SwapBuffers ( ) ;
150 11. OPENGL MFC VCL
// 3 .
// ( WM_DESTROY)
m_pGLRC>Destroy ( ) ;
delete m_pGLRC;

11.5. OpenGL c MFC


Windows- -
MFC, , -
OpenGL
MFC. -
MFC,
OpenGL -
:


( ) , -
CWnd .

OpenGL -
OnCreate,  OnPaint, OnUpdate, OnDraw
OnTimer ( , ,
OpenGL-
).


WM_ERASEBKGND, Windows
OnPaint.

OnDestroy,
 OnSize.


CWnd::GetSafeHwnd.
11.5. OPENGL C MFC 151

,
OpenGL.

class OpenGLWindow : public CWnd


{
public :
OpenGLWindow ( ) ;
//
...
private :
//
...
// MFC
//
afx_msg int OnCreate (LPCREATESTRUCT l p C r e a t e S t r u c t ) ;
afx_msg BOOL OnEraseBkgnd (CDC* pDC ) ;
afx_msg void OnPaint ( ) ;
afx_msg void OnDestroy ( ) ;
// Windows
DECLARE_MESSAGE_MAP( )

// OpenGL
GLRC* m_pGLRC;
};
:

//
OpenGLWindow : : OpenGLWindow ( )
: m_pGLRC(NULL)
{
}

//
BEGIN_MESSAGE_MAP(OpenGLWindow , CWnd)
// MFC
ON_WM_CREATE( )
ON_WM_PAINT( )
152 11. OPENGL MFC VCL
ON_WM_DESTROY( )
ON_WM_ERASEBKGND( )
END_MESSAGE_MAP( )

//
afx_msg int OpenGLWindow : : OnCreate (
LPCREATESTRUCT l p C r e a t e S t r u c t )
{

m_pGLRC = new GLRC( GetSafeHwnd ( ) ) ;


bool r e s = m_pGLRC>Create ( ) ;
if (! res )
return FALSE ;
return CWnd : : OnCreate ( l p C r e a t e S t r u c t ) ;
}

//
afx_msg BOOL OpenGLWindow : : OnEraseBkgnd (CDC* pDC)
{
return FALSE ;
}

//
afx_msg void OpenGLWindow : : OnPaint ( )
{
CWnd : : OnPaint ( ) ;

//
m_glRC>MakeCurrent ( ) ;

g l C l e a r (GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT) ;

// ,
< OpenGL>
11.6. OPENGL C VCL 153

m_glRC>SwapBuffers ( ) ;
}

//
afx_msg void OpenGLWindow : : OnDestroy ( )
{
m_glRC>Destroy ( ) ;
delete m_glRC ;
CWnd : : OnDestroy ( ) ;
}

11.6. OpenGL c VCL


OpenGL VCL  -
Windows-,
.
++,
OpenGL () VCL:

class OpenGLForm : public TForm


{
public :
_ _ f a s t c a l l OpenGLForm( TComponent * owner ) ;
//
...
private :
//
GLRC* m_pGLRC;
...
//
published :

//
void _ _ f a s t c a l l FormCreate ( TObject * se nder ) ;
void _ _ f a s t c a l l FormDestroy ( TObject * se nder ) ;
154 11. OPENGL MFC VCL
void _ _ f a s t c a l l FormPaint ( TObject * se nder ) ;
};

//
_ _ f a s t c a l l OpenGLForm : : OpenGLForm(
TComponent * owner )
: TForm( owner ) ,
m_pGLRC(NULL)
{
}

//
void _ _ f a s t c a l l OpenGLForm : : FormCreate (
TObject * se nder )
{
m_pGLRC = new GLRC( Handle ) ;
bool r e s = m_pGLRC>Create ( ) ;
assert ( res );
}

//
void _ _ f a s t c a l l OpenGLForm : : FormPaint (
TObject * se nder )
{
//
m_glRC>MakeCurrent ( ) ;

g l C l e a r (GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT) ;

// ,
< OpenGL>

m_glRC>SwapBuffers ( ) ;
}

//
11.6. OPENGL C VCL 155

void _ _ f a s t c a l l OpenGLForm : : FormDestroy ( )


{
m_glRC>Destroy ( ) ;
delete m_glRC ;
}
12.

OpenGL .NET

-
OpenGL Microsoft .NET.

, OpenGL -
.NET, -
, OpenGL .NET-.
 --
Tao Framework.

Tao Framwork (http://www.taoframework.com) -


.NET
Win32-, GL, GLU, GLUT, WGL.

12.1. GLUT Microsoft Visual C# 2005


OpenGL-
.NET GLUT, -
.NET- Tao.FreeGlut.

, -
.NET- Microsoft Visual C# 2005 C#
GLUT OpenGL:

157
158 12. OPENGL .NET
, GLUT (glut32.dll -
Windows\System32).

 
  
: File New
Projects Visual C# Windows Console Application, -
, OK.

Tao : Project  Add Reference 


.NET,
Tao Framework OpenGL Binding For .NET
Tao Framework FreeGLUT Binding For .NET,
, . ,
Tao Framework
Browse.

GLUT OpenGL .NET


C# Win32 C++.
, -
.2.5:

12.1. OpenGL C#.

using Tao . FreeGlut ;


using Tao . OpenGl ;
namespace gl_glut_net
{

class Program
{
private static int Width = 5 1 2 ;
private static int Height = 5 1 2 ;
public const int CubeSize = 2 0 0 ;
static void Display ( )
{
int l e f t , r i g h t , top , bottom ;
12.1. GLUT MICROSOFT VISUAL C# 2005 159

l e f t = ( Width CubeSize ) / 2 ;
r i g h t = l e f t + CubeSize ;
bottom = ( Height CubeSize ) / 2 ;
top = bottom + CubeSize ;

Gl . g l C l e a r C o l o r ( 0 . 7 f , 0 . 7 f , 0 . 7 f , 1 ) ;
Gl . g l C l e a r ( Gl .GL_COLOR_BUFFER_BIT) ;

Gl . glColor3ub ( 2 5 5 , 0 , 0 ) ;
Gl . g l B e g i n ( Gl .GL_QUADS) ;
Gl . g l V e r t e x 2 f ( l e f t , bottom ) ;
Gl . g l V e r t e x 2 f ( l e f t , top ) ;
Gl . g l V e r t e x 2 f ( r i g h t , top ) ;
Gl . g l V e r t e x 2 f ( r i g h t , bottom ) ;
Gl . glEnd ( ) ;

Gl . g l F i n i s h ( ) ;
}

static void Reshape ( int w, int h )


{
Width = w;
Height = h ;

Gl . glViewport ( 0 , 0 , w, h ) ;

Gl . glMatrixMode ( Gl .GL_PROJECTION) ;
Gl . g l L o a d I d e n t i t y ( ) ;
Gl . glOrtho ( 0 , w, 0 , h , 1.0 , 1 . 0 ) ;

Gl . glMatrixMode ( Gl .GL_MODELVIEW) ;
Gl . g l L o a d I d e n t i t y ( ) ;
}

static void Keyboard ( byte key , int x , int y )


160 12. OPENGL .NET
{
const int ESCAPE = 2 7 ;
i f ( key == ESCAPE)
Glut . glutLeaveMainLoop ( ) ;
}

static void Main ( s t r i n g [ ] a r g s )


{

Glut . g l u t I n i t ( ) ;
Glut . g l u t I n i t D i s p l a y M o d e ( Glut .GLUT_RGB) ;
Glut . glutInitWindowSize ( Width , Height ) ;
Glut . glutCreateWindow ( "Red square example " ) ;

Glut . glutDisplayFunc ( Display ) ;


Glut . glutReshapeFunc ( Reshape ) ;
Glut . glutKeyboardFunc ( Keyboard ) ;

Glut . glutMainLoop ( ) ;
}
}
}

, GL, GLU
GLUT Gl, Glu Glut, -
.

12.2. OpenGL WindowsForms


OpenGL WindowsForms , -
MFC VCL (. .11).
Tao Framework OpenGL 
Tao.Platform.Windows.SimpleOpenGlControl.
,
12.2. OPENGL WINDOWSFORMS 161

WindowsForms
OpenGL:

  
 
: File New Projects Visual
C# Windows Windows Application, -
, OK.

Tao : Project  Add Reference  .NET,


Tao Framework OpenGL Binding For .NET
Tao Framework Windows Platform API Binding For .NET,
, . , -
Tao
Framework Browse.

OpenGL,
.
Toolbox -
Choose Items..., -
SimpleOpenGLControl, ,
.

OpenGL : Toolbox
SimpleOpenGLControl
. .

OpenGL: -
InitializeComponent()
simpleOpenGlControl1.InitializeContexts().

OpenGL
Paint OpenGL ( Paint ).
IV

163
.


GLU GLUT

,
GLU GLUT.
GLU, -
quadric- -
gluNewQuadric(), gluSphere(),
gluCylinder(), gluDisk(), gluPartialDisk() .
:

void gluSphere ( GLUquadricObj * qobj , GLdouble radius ,


GLint s l i c e s , GLint s t a c k s )

radius. z
slices , z  stacks .
void g l u C y l i n d e r ( GLUquadricObj * qobj ,
GLdouble baseRadius ,
GLdouble topRadius ,
GLdouble height , GLint s l i c e s ,
GLint s t a c k s )

165
166 . GLU GLUT
(),
z, -
baseRadius, z = 0,
topRadius
z = height. ,
. slices stacks
, .

void gluDisk ( GLUquadricObj * qobj ,


GLdouble innerRadius ,
GLdouble outerRadius ,
GLint s l i c e s ,
GLint l o o p s )

() -
outerRadius. innerRadius -
, -
innerRadius. slices
z, loops  ,
z.

void g l u P a r t i a l D i s k ( GLUquadricObj * qobj ,


GLdouble innerRadius ,
GLdouble outerRadius ,
GLint s l i c e s ,
GLint loops ,
GLdouble s t a r t A n g l e ,
GLdouble sweepAngle ) ;

,
, -
-
y startAngle
sweepAngle. .

, -
GLUT, OpenGL
167

GLU. -
.

void g l u t S o l i d S p h e r e ( GLdouble radius ,


GLint s l i c e s ,
GLint s t a c k s )
void glutWireSphere ( GLdouble radius ,
GLint s l i c e s ,
GLint s t a c k s )
glutSolidSphere() , glutWireSphere() 
radius. ,
.

void g lu t So l id Cu b e ( GLdouble s i z e )
void glutWireCube ( GLdouble s i z e )

size.

void g l u t S o l i d C o n e ( GLdouble base , GLdouble height ,


GLint s l i c e s , GLint s t a c k s )
void glutWireCone ( GLdouble base , GLdouble height ,
GLint s l i c e s , GLint s t a c k s )
height
base, z. -
z = 0.
void g l u t S o l i d T o r u s ( GLdouble innerRadius ,
GLdouble outerRadius ,
GLint n s i d e s ,
GLint r i n g s )
void glutWireTorus ( GLdouble innerRadius ,
GLdouble outerRadius ,
GLint n s i d e s ,
GLint r i n g s )
z = 0.

168 . GLU GLUT
innerRadius outerRadius. nsides
, , rings 
.

void g l u t S o l i d T e t r a h e d r o n ( void )
void glutWireTetrahedron ( void )
( -
) , -
1.

void g l u t S o l i d O c t a h e d r o n ( void )
void glutWireOctahedron ( void )
, -
1.

void glutSolidDodecahedron ( void )


void glutWireDodecahedron ( void )
, -
.

void g l u t S o l i d I c o s a h e d r o n ( void )
void g lu t Wi r eI c os a he dr o n ( void )
, -
1.
-
, -

glEnable(GL_DEPTH_TEST).
.

.1. 1: GLUT-
-
GLUT- OpenGL. -

,
.
.

.1. OpenGL.

#include <s t d l i b . h>


#include <g l \ g l u t . h>
#i f d e f random
#undef random
#endif
#define random (m) ( float ) rand ( ) *m/RAND_MAX

169
170 .
//
GLint Width = 512 , Height = 5 1 2 ;
//
int Times = 1 0 0 ;
// ?
int FillFlag = 1;

long Seed = 0 ;

//
void DrawRect ( float x1 , float y1 ,
float x2 , float y2 ,
int F i l l F l a g )
{
g l B e g i n ( F i l l F l a g ? GL_QUADS : GL_LINE_LOOP) ;
g l V e r t e x 2 f ( x1 , y1 ) ;
g l V e r t e x 2 f ( x2 , y1 ) ;
g l V e r t e x 2 f ( x2 , y2 ) ;
g l V e r t e x 2 f ( x1 , y2 ) ;
glEnd ( ) ;
}

//
void Display ( void )
{
int i ;
float x1 , y1 , x2 , y2 ;
float r , g , b ;
srand ( Seed ) ;

glClearColor (0 , 0 , 0 , 1);
g l C l e a r (GL_COLOR_BUFFER_BIT) ;

for ( i = 0 ; i < Times ; i++ ) {


r = random ( 1 ) ;
g = random ( 1 ) ;
.1. 1: GLUT- 171

b = random ( 1 ) ;
glColor3f ( r , g , b );

x1 = random ( 1 ) * Width ;
y1 = random ( 1 ) * Height ;
x2 = random ( 1 ) * Width ;
y2 = random ( 1 ) * Height ;
DrawRect ( x1 , y1 , x2 , y2 , F i l l F l a g ) ;
}

glFinish ();
}

//
void Reshape ( GLint w, GLint h )
{
Width = w;
Height = h ;

glViewport ( 0 , 0 , w, h ) ;

glMatrixMode (GL_PROJECTION) ;
glLoadIdentity ( ) ;
glOrtho ( 0 , w, 0 , h , 1.0 , 1 . 0 ) ;

glMatrixMode (GL_MODELVIEW) ;
glLoadIdentity ( ) ;
}

//
void Mouse ( int button , int s t a t e ,
int x , int y )
{
i f ( s t a t e == GLUT_DOWN ) {
switch ( button ) {
case GLUT_LEFT_BUTTON:
Seed = random (RAND_MAX) ;
172 .
break ;
case GLUT_RIGHT_BUTTON:
FillFlag = ! FillFlag ;
break ;
}
glutPostRedisplay ( ) ;
}
}

//
void Keyboard ( unsigned char key , int x , int y )
{
const char ESCAPE = ' \033 ' ;
i f ( key == ESCAPE )
exit (0);
}

void main ( int argc , char * argv [ ] )


{
g l u t I n i t (&argc , argv ) ;
g l u t I n i t D i s p l a y M o d e (GLUT_RGB) ;
glutInitWindowSize ( Width , Height ) ;
glutCreateWindow ( " Rect draw example (RGB) " ) ;

glutDisplayFunc ( Display ) ;
glutReshapeFunc ( Reshape ) ;
glutKeyboardFunc ( Keyboard ) ;
glutMouseFunc ( Mouse ) ;

glutMainLoop ( ) ;
}
.2. 2: OPENGL 173

. .1. .1.  -
,  .

.2. 2: OpenGL
-
OpenGL , , -
. .
.

.2. OpenGL.

#include <s t d l i b . h>


#include <GL/ g l u t . h>
//
float mat1_dif [ ] = { 0 . 8 f , 0 . 8 f , 0 . 0 f } ;
float mat1_amb[] = { 0 . 2 f , 0 . 2 f , 0 . 2 f } ;
float mat1_spec [ ] = { 0 . 6 f , 0 . 6 f , 0 . 6 f } ;
float mat1_shininess =0.5 f * 1 2 8 ;

//
float mat2_dif [ ] = { 0 . 0 f , 0 . 0 f , 0 . 8 f } ;
float mat2_amb[] = { 0 . 2 f , 0 . 2 f , 0 . 2 f } ;
float mat2_spec [ ] = { 0 . 6 f , 0 . 6 f , 0 . 6 f } ;
174 .
float mat2_shininess =0.7 f * 1 2 8 ;
//
float mat3_dif [ ] = { 0 . 9 f , 0 . 2 f , 0 . 0 f } ;
float mat3_amb[] = { 0 . 2 f , 0 . 2 f , 0 . 2 f } ;
float mat3_spec [ ] = { 0 . 6 f , 0 . 6 f , 0 . 6 f } ;
float mat3_shininess =0.1 f * 1 2 8 ;

//
//
void i n i t ( void )
{
GLfloat light_ambient [ ] = { 0 . 0 , 0 . 0 , 0 . 0 , 1 . 0 } ;
GLfloat light_diffuse [ ] = { 1.0 , 1.0 , 1.0 , 1.0 };
GLfloat light_specular [ ] = { 1.0 , 1.0 , 1.0 , 1.0 };
GLfloat light_position [ ] = { 1.0 , 1.0 , 1.0 , 0.0 };

/* */
g l L i g h t f v (GL_LIGHT0, GL_AMBIENT,
light_ambient ) ;
g l L i g h t f v (GL_LIGHT0, GL_DIFFUSE,
light_diffuse );
g l L i g h t f v (GL_LIGHT0, GL_SPECULAR,
light_specular );
g l L i g h t f v (GL_LIGHT0, GL_POSITION,
light_position );

/* */
glEnable (GL_LIGHTING ) ;
glEnable (GL_LIGHT0 ) ;
/* z */
glEnable (GL_DEPTH_TEST) ;

//
// .
.2. 2: OPENGL 175

// .
void d i s p l a y ( void )
{
/* */
g l C l e a r (GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT) ;

glPushMatrix ( ) ;
glRotatef (20.0 , 1.0 , 0.0 , 0 . 0 ) ;

/* */
g l M a t e r i a l f v (GL_FRONT,GL_AMBIENT,
mat1_amb ) ;
g l M a t e r i a l f v (GL_FRONT,GL_DIFFUSE,
mat1_dif ) ;
g l M a t e r i a l f v (GL_FRONT,GL_SPECULAR,
mat1_spec ) ;
g l M a t e r i a l f (GL_FRONT, GL_SHININESS ,
mat1_shininess ) ;

glPushMatrix ( ) ;
g l T r a n s l a t e f ( 0.75 , 0 . 5 , 0 . 0 ) ;
glRotatef (90.0 , 1.0 , 0.0 , 0 . 0 ) ;
g l u t S o l i d T o r u s ( 0 . 2 7 5 , 0 . 8 5 , 15 , 1 5 ) ;
glPopMatrix ( ) ;

/* */
g l M a t e r i a l f v (GL_FRONT,GL_AMBIENT,
mat2_amb ) ;
g l M a t e r i a l f v (GL_FRONT,GL_DIFFUSE,
mat2_dif ) ;
g l M a t e r i a l f v (GL_FRONT,GL_SPECULAR,
mat2_spec ) ;
g l M a t e r i a l f (GL_FRONT, GL_SHININESS ,
mat2_shininess ) ;

glPushMatrix ( ) ;
176 .
g l T r a n s l a t e f ( 0.75 , 0.5 , 0 . 0 ) ;
glRotatef (270.0 , 1.0 , 0.0 , 0 . 0 ) ;
g l u t S o l i d C o n e ( 1 . 0 , 2 . 0 , 15 , 1 5 ) ;
glPopMatrix ( ) ;

/* */
g l M a t e r i a l f v (GL_FRONT,GL_AMBIENT,
mat3_amb ) ;
g l M a t e r i a l f v (GL_FRONT,GL_DIFFUSE,
mat3_dif ) ;
g l M a t e r i a l f v (GL_FRONT,GL_SPECULAR,
mat3_spec ) ;
g l M a t e r i a l f (GL_FRONT, GL_SHININESS ,
mat3_shininess ) ;

glPushMatrix ( ) ;
g l T r a n s l a t e f ( 0 . 7 5 , 0 . 0 , 1.0);
g l u t S o l i d S p h e r e ( 1 . 0 , 15 , 1 5 ) ;
glPopMatrix ( ) ;

glPopMatrix ( ) ;
/* */
glFlush ( ) ;
}

//
//
void reshape ( int w, int h )
{
//
//
glViewport ( 0 , 0 , ( G L s i z e i ) w, ( G L s i z e i ) h ) ;

//
//
glMatrixMode (GL_PROJECTION) ;
glLoadIdentity ( ) ;
.2. 2: OPENGL 177

gluPerspective (
//
40.0 ,
//
( GLfloat )w/h ,
//
1 ,100.0);
glMatrixMode (GL_MODELVIEW) ;

glLoadIdentity ( ) ;
gluLookAt (
//
0.0 f ,0.0 f ,8.0 f ,
//
0.0 f ,0.0 f ,0.0 f ,
// y
0.0 f ,1.0 f ,0.0 f ) ;

//
void keyboard ( unsigned char key , int x , int y )
{
switch ( key ) {
case 2 7 : /* e s c a p e */
exit (0);
break ;
}
}

//
// ,
//
int main ( int argc , char ** argv )
{
g l u t I n i t (&argc , argv ) ;
178 .
glutInitDisplayMode (
GLUT_SINGLE |
GLUT_RGB |
GLUT_DEPTH) ;
glutInitWindowSize ( 5 0 0 , 5 0 0 ) ;
glutCreateWindow ( argv [ 0 ] ) ;
init ();
glutReshapeFunc ( reshape ) ;
glutDisplayFunc ( d i s p l a y ) ;
glutKeyboardFunc ( keyboard ) ;
glutMainLoop ( ) ;
return 0 ;
}

. .2. .2.

.3. BMP
LoadBMP(),
-
(24 ) Windows Bitmap (BMP).
:
.3. BMP 179

int LoadBMP( const char * filen ame , IMAGE* out_img )


lename . -
out_img, -
:

typedef struct _IMAGE


{
int width ;
int h e i g h t ;
unsigned char * data ;
} IMAGE;
width height , ,
. data ,
RGB- .

.3. BMP. loadbmp.h.

#ifndef _LOADBMP_H
#define _LOADBMP_H

typedef struct _IMAGE


{
int width ;
int h e i g h t ;
unsigned char * data ;
} IMAGE;

int LoadBMP( const char * f i l e , IMAGE* out_img ) ;


#endif

.4. BMP. loadbmp.cpp.

#include "loadbmp . h"


#include <s t d i o . h>
180 .
#include <s t d l i b . h>
#include <memory . h>
// BMP
#define BMP_SIZE_FILEHEADER 14
// BMP
#define BMP_SIZE_INFOHEADER 40
#define BMP_COLOR_BITS_24 24
//

static unsigned int uInt16Number ( unsigned char buf [ 2 ] )


{
return ( buf [ 1 ] << 8) | buf [ 0 ] ;
}

static unsigned int uInt32Number ( unsigned char buf [ 4 ] )


{
unsigned numb = buf [ 3 ] ;
numb = (numb << 8) | buf [ 2 ] ;
numb = (numb << 8) | buf [ 1 ] ;
numb = (numb << 8) | buf [ 0 ] ;
return numb ;
}

int ReadFileHeader ( FILE * f , int * bitmap_pos )


{
unsigned char header [BMP_SIZE_FILEHEADER ] ;
s i z e _ t numb = 0 ;
int o f f s e t = 0 ;
i f ( f s e e k ( f , 0 , SEEK_SET) )
return 0 ;

numb = f r e a d ( header , BMP_SIZE_FILEHEADER, 1 , f ) ;


.3. BMP 181

i f (numb != 1)
return 0 ;
i f ( header [ 0 ] != 'B ' | | header [ 1 ] != 'M' )
return 0 ;
o f f s e t = uInt32Number ( header + 1 0 ) ;

numb = f r e a d ( header , 4 , 1 , f ) ;
i f (numb != 1)
return 0 ;
i f ( uInt32Number ( header ) != 40)
return 0 ;
* bitmap_pos = offset ;
return 1 ;
}

// BMP

int LoadBMP( const char * f i l e , IMAGE* out_img )


{
FILE * f ;
int bitmap_pos ;
unsigned char buf [ 4 0 ] ;
s i z e _ t numb ;
int x_res ;
int y_res ;
int n_bits ;
int compression ;
int size_image ;
int n_used_colors ;
//
f = fopen ( f i l e , " rb " ) ;
182 .
if (! f )
return 0 ;
i f ( out_img == NULL)
return 0 ;
//

i f ( ! ReadFileHeader ( f , &bitmap_pos ) )
{
fclose ( f );
return 0 ;
}

i f ( f s e e k ( f , BMP_SIZE_FILEHEADER, SEEK_SET) )
{
fclose ( f );
return 0 ;
}

numb = f r e a d ( buf , 40 , 1 , f ) ;
i f (numb != 1)
{
fclose ( f );
return 0 ;
}

x_res = ( int ) uInt32Number ( buf + 4 ) ;


y_res = ( int ) uInt32Number ( buf + 8 ) ;

n_bits = ( int ) uInt16Number ( buf + 14);


compression = ( int ) uInt32Number ( buf + 16);
size_image = ( int ) uInt32Number ( buf + 20);
n_used_colors = ( int ) uInt32Number ( buf + 32);

//
i f ( n_bits == BMP_COLOR_BITS_24)
.3. BMP 183

{
int rgb_size ;
unsigned char * rgb ;
int y ;
unsigned char * l i n e ;
int rest_4 ;
i f ( bitmap_pos !=
BMP_SIZE_FILEHEADER + BMP_SIZE_INFOHEADER)
{
fclose ( f );
return 0 ;
}

i f ( f s e e k ( f , bitmap_pos , SEEK_SET) )
{
fclose ( f );
return 0 ;
}

rgb_size = 3 * x_res ;
rest_4 = rgb_size % 4 ;
i f ( rest_4 > 0)
rgb_size += 4 rest_4 ;

out_img>width = x_res ;
out_img>h e i g h t = y_res ;

out_img>data =
( unsigned char * ) malloc ( x_res * y_res * 3 ) ;

i f ( out_img>data == NULL)
return 0 ;
rgb = ( unsigned char * ) malloc ( rgb_size ) ;

//
184 .
for ( y = 0 ; y < y_res ; y++)
{
s i z e _ t numb = 0 ;
int x = 0 ;
numb = f r e a d ( rgb , rgb_size , 1 , f ) ;
i f (numb != 1)
{
fclose ( f );
f r e e ( rgb ) ;
return 0 ;
}

numb = 0 ;
l i n e = out_img>data + x_res * 3 * y ;
for ( x = 0 ; x < x_res ; x++)
{
l i n e [ 2 ] = rgb [ numb++];
l i n e [ 1 ] = rgb [ numb++];
l i n e [0]= rgb [ numb++];
l i n e += 3 ;
}
}
fclose ( f );
f r e e ( rgb ) ;
}
else
return 0 ;
return 1 ;
}

typedef unsigned char BYTE;


typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef struct tagBITMAPFILEHEADER
.3. BMP 185

{
WORD bfType ;
DWORD bfSize ;
WORD bfReserved1 ;
WORD bfReserved2 ;
DWORD bfOffBits ;
} BITMAPFILEHEADER;

typedef struct tagBITMAPINFOHEADER {


DWORD biSize ;
long biWidth ;
long b iH e ig h t ;
WORD b i P l a n e s ;
WORD biBitCount ;
DWORD biCompression ;
DWORD b i S i z e I m a g e ;
long biXPelsPerMeter ;
long biYPelsPerMeter ;
DWORD biClrUsed ;
DWORD biClrImportant ;
} BITMAPINFOHEADER;

static void IntTo2Bytes ( int val , BYTE buf [ 2 ] )


{
buf [ 0 ] = (BYTE) v a l ;
buf [ 1 ] = (BYTE) ( v a l >> 8 ) ;
}

static void IntTo4Bytes ( int val , BYTE buf [ 4 ] )


{
buf [ 0 ] = (BYTE) v a l ;
buf [ 1 ] = (BYTE) ( v a l >> 8 ) ;
buf [ 2 ] = (BYTE) ( v a l >> 1 6 ) ;
buf [ 3 ] = (BYTE) ( v a l >> 2 4 ) ;
}
186 .
.4. 3:
-
, -
.
Microsoft Visual C++
texture.bmp
. , -

. -
LoadBMP, -
.

.5. .

#include <s t d l i b . h>


#include <math . h>
#include <GL\ g l u t . h>
#include "loadbmp . h"

#define TETR_LIST 1
GLfloat l i g h t _ c o l [ ] = { 1 , 1 , 1 } ;
float mat_diff1 [ ] = { 0 . 8 , 0 . 8 , 0 . 8 } ;
float mat_diff2 [ ] = { 0 . 0 , 0 . 0 , 0 . 9 } ;
float mat_amb[] = { 0 . 2 , 0 . 2 , 0 . 2 } ;
float mat_spec [ ] = { 0 . 6 , 0 . 6 , 0 . 6 } ;
float s h i n i n e s s =0.7 * 128 , CurAng=0, RingRad=1,
RingHeight =0.1;
GLUquadricObj * QuadrObj ;
GLuint TexId ;
GLfloat TetrVertex [ 4 ] [ 3 ] , TetrNormal [ 4 ] [ 3 ] ;

// ,
// a , b , c
void getnorm ( float a [ 3 ] , float b [ 3 ] , float c [ 3 ] ,
float * n )
.4. 3: 187

{
float mult =0;
int i , j ;
n [ 0 ] = ( b [1] a [ 1 ] ) * ( c [2] a [ 2 ] ) ( b[2] a [ 2 ] ) *
( c [1] a [ 1 ] ) ;
n [ 1 ] = ( c [0] a [ 0 ] ) * ( b [2] a [ 2 ] ) ( b[0] a [ 0 ] ) *
( c [2] a [ 2 ] ) ;
n [ 2 ] = ( b [0] a [ 0 ] ) * ( c [1] a [ 1 ] ) ( c [0] a [ 0 ] ) *
( b[1] a [ 1 ] ) ;
// :
// ( 0 , 0 , 0 )
for ( i =0; i <3; i ++) mult+=a [ i ] * n [ i ] ;
i f ( mult <0) for ( j =0; j <3; j++) n [ j ]=n [ j ] ;
}

//
void I n i t V e r t e x T e t r ( )
{
float alpha =0;
int i ;
TetrVertex [ 0 ] [ 0 ] = 0 ;
TetrVertex [ 0 ] [ 1 ] = 1 . 3 ;
TetrVertex [ 0 ] [ 2 ] = 0 ;
//
for ( i =1; i <4; i ++)
{
TetrVertex [ i ] [ 0 ] = 0 . 9 4 * cos ( alpha ) ;
TetrVertex [ i ] [ 1 ] = 0 ;
TetrVertex [ i ] [ 2 ] = 0 . 9 4 * s i n ( alpha ) ;
alpha +=120.0 * 3.14/180.0;
}
}

//
void InitNormsTetr ( )
{
getnorm ( TetrVertex [ 0 ] , TetrVertex [ 1 ] ,
188 .
TetrVertex [ 2 ] , TetrNormal [ 0 ] ) ;
getnorm ( TetrVertex [ 0 ] , TetrVertex [ 2 ] ,
TetrVertex [ 3 ] , TetrNormal [ 1 ] ) ;
getnorm ( TetrVertex [ 0 ] , TetrVertex [ 3 ] ,
TetrVertex [ 1 ] , TetrNormal [ 2 ] ) ;
getnorm ( TetrVertex [ 1 ] , TetrVertex [ 2 ] ,
TetrVertex [ 3 ] , TetrNormal [ 3 ] ) ;
}

//
void MakeTetrList ( )
{
int i ;
glNewList (TETR_LIST,GL_COMPILE) ;
//
g l B e g i n (GL_TRIANGLES) ;
for ( i =1; i <4; i ++)
{
glNormal3fv ( TetrNormal [ i 1 ] ) ;
g l V e r t e x 3 f v ( TetrVertex [ 0 ] ) ;
g l V e r t e x 3 f v ( TetrVertex [ i ] ) ;
i f ( i !=3)
g l V e r t e x 3 f v ( TetrVertex [ i + 1 ] ) ;
else
g l V e r t e x 3 f v ( TetrVertex [ 1 ] ) ;
}
glNormal3fv ( TetrNormal [ 3 ] ) ;
g l V e r t e x 3 f v ( TetrVertex [ 1 ] ) ;
g l V e r t e x 3 f v ( TetrVertex [ 2 ] ) ;
g l V e r t e x 3 f v ( TetrVertex [ 3 ] ) ;
glEnd ( ) ;
glEndList ( ) ;
}

void DrawRing ( )
{
// ( ) ,
.4. 3: 189

// z
//
// , ,
//
// z .
// z=0
g l u C y l i n d e r ( QuadrObj , RingRad , RingRad ,
RingHeight , 3 0 , 2 ) ;
}

void T e x t u r e I n i t ( )
{
char s t r F i l e [ ] = " t e x t u r e . bmp" ;
IMAGE img ;

// * . bmp
g l P i x e l S t o r e i (GL_UNPACK_ALIGNMENT, 1 ) ;
//
glGenTextures (1 ,& TexId ) ;
//
i f ( ! LoadBMP( s t r F i l e , &img ) )
return ; ;
//
glBindTexture (GL_TEXTURE_2D, TexId ) ;
//
//
gluBuild2DMipmaps (GL_TEXTURE_2D, 3 ,
img . width , img . height ,
GL_RGB,GL_UNSIGNED_BYTE, img . data ) ;

//
// q u a d r i c
gluQuadricTexture ( QuadrObj , GL_TRUE) ;

//
//
190 .
// s t
glTexParameteri (GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S, GL_REPEAT) ;
glTexParameteri (GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T, GL_REPEAT) ;

//
//
glTexParameteri (GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_NEAREST) ;
glTexParameteri (GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_NEAREST) ;

//
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
GL_MODULATE) ;
}

void I n i t ( void )
{
InitVertexTetr ( ) ;
InitNormsTetr ( ) ;
MakeTetrList ( ) ;

//
g l M a t e r i a l f v (GL_FRONT_AND_BACK, GL_AMBIENT,
mat_amb ) ;
g l M a t e r i a l f v (GL_FRONT_AND_BACK, GL_SPECULAR,
mat_spec ) ;
g l M a t e r i a l f (GL_FRONT, GL_SHININESS ,
shininess );

//
g l L i g h t f v (GL_LIGHT0, GL_DIFFUSE, l i g h t _ c o l ) ;
glEnable (GL_LIGHTING ) ;
glEnable (GL_LIGHT0 ) ;
.4. 3: 191

//
//
glEnable (GL_DEPTH_TEST) ;
//
glEnable (GL_NORMALIZE) ;
//
//
glEnable (GL_COLOR_MATERIAL) ;
g l C o l o r M a t e r i a l (GL_FRONT_AND_BACK,
GL_DIFFUSE ) ;

// q u a d r i c
//
QuadrObj=gluNewQuadric ( ) ;

//
TextureInit ( ) ;

//
glMatrixMode (GL_PROJECTION) ;
gluPerspective (89.0 , 1.0 , 0.5 , 100.0);
//
//
glMatrixMode (GL_MODELVIEW) ;
}

void DrawFigures ( void )


{
//
glEnable (GL_TEXTURE_2D) ;
//
g l C o l o r 3 f v ( mat_diff1 ) ;
//
//
glLoadIdentity ( ) ;
//
192 .
gluLookAt ( 0 . 0 , 0 . 0 , 2 . 5 ,
0.0 , 0.0 , 0.0 ,
0.0 , 1.0 , 0.0);
// ,
//
glPushMatrix ( ) ;
//
// ( ,
//
// )
g l R o t a t e f (CurAng , 1 , 1 , 0 ) ;
g l R o t a t e f ( CurAng , 1 , 0 , 0 ) ;
//
// ,
// ,
glPushMatrix ( ) ;
g l T r a n s l a t e f (0 ,0 , RingHeight / 2 ) ;
DrawRing ( ) ;
glPopMatrix ( ) ;
glPushMatrix ( ) ;
g l T r a n s l a t e f ( 0 , RingHeight / 2 , 0 ) ;
glRotatef (90 ,1 ,0 ,0);
DrawRing ( ) ;
glPopMatrix ( ) ;
glPushMatrix ( ) ;
g l T r a n s l a t e f ( RingHeight / 2 , 0 , 0 ) ;
glRotatef (90 ,0 ,1 ,0);
DrawRing ( ) ;

glPopMatrix ( ) ;
//
glPopMatrix ( ) ;
//
g l D i s a b l e (GL_TEXTURE_2D) ;

//
g l R o t a t e f ( CurAng , 1 , 0 , 0 ) ;
.4. 3: 193

g l R o t a t e f ( CurAng / 2 , 1 , 0 , 1 ) ;

// ,
// oz
g l T r a n s l a t e f ( 0 , 0.33 , 0 ) ;

//
g l C o l o r 3 f v ( mat_diff2 ) ;

//
g l C a l l L i s t (TETR_LIST ) ;
}

void Display ( void )


{
// ( )
//
g l C l e a r (GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT) ;
//
DrawFigures ( ) ;
//
glutSwapBuffers ( ) ;
}

void Redraw ( void )


{
//
CurAng+=1;

//
// ( )
glutPostRedisplay ( ) ;
}

int main ( int argc , char ** argv )


{
194 .
// GLUT
g l u t I n i t (&argc , argv ) ;
// ,
// RGB,
//
g l u t I n i t D i s p l a y M o d e (GLUT_DOUBLE |
GLUT_RGB |
GLUT_DEPTH) ;
//
glutCreateWindow ( "Example o f u s i n g OpenGL" ) ;
//
glutDisplayFunc ( Display ) ;
//
g l u t I d l e F u n c ( Redraw ) ;
// OpenGL
Init ();
//
glutMainLoop ( ) ;
return 0 ;
}

. .3. .5.
.5. OPENGL WIN32 195

.5. OpenGL Win32


.6. glrc.h.

#ifndef _GLRC_H_
#define _GLRC_H_
// OpenGL
#include <g l / g l . h>
#include <g l / gl u . h>
class GLRC
{
public :
//
GLRC( HWND wnd ) ;

//
~GLRC( ) ;

// ( )
void Destroy ( ) ;
// .
// OpenGL
bool Create ( ) ;
// ?
bool I s C r e a t e d ( ) ;
// ?
bool I s C u r r e n t ( ) const ;
//
bool MakeCurrent ( ) ;
196 .
// ,
//
void SwapBuffers ( ) ;
private :
//
bool m_created ;
// ,
HWND m_wnd;
//
HDC m_dc ;
// OpenGL
HGLRC m_glrc ;

};

#endif

.7. glrc.cpp.

#include <windows . h>


#include " g l r c . h"
#include " a s s e r t . h"
GLRC : : GLRC(HWND wnd)
: m_created ( f a l s e )
{
a s s e r t ( wnd ) ;
m_wnd = wnd ;
m_dc = : : GetDC( wnd ) ;
a s s e r t ( m_dc ) ;
}

GLRC: : ~GLRC( )
{
i f ( m_created )
.5. OPENGL WIN32 197

Destroy ( ) ;
}

void GLRC : : Destroy ( )


{
wglDeleteContext ( m_glrc ) ;
: : ReleaseDC (m_wnd, m_dc ) ;
m_created = f a l s e ;
}

bool GLRC : : MakeCurrent ( )


{
a s s e r t ( m_created ) ;

i f ( IsCurrent ( ) )
return true ;
BOOL r e s = wglMakeCurrent (m_dc, m_glrc ) ;
return ( r e s != FALSE ) ;
}

bool GLRC : : Create ( )


{
a s s e r t ( ! m_created ) ;

int nPixelFormat = 0 ;
DWORD f l a g s ;
flags =
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;

static PIXELFORMATDESCRIPTOR pfd =


{
sizeof (PIXELFORMATDESCRIPTOR) ,
1,
198 .
flags ,
PFD_TYPE_RGBA,
24 ,
0, 0, 0, 0, 0, 0,
1,
0,
0,
0, 0, 0, 0,
32 ,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};

pfd . cAlphaBits = 8 ;

nPixelFormat = ChoosePixelFormat ( m_dc, &pfd ) ;

BOOL r e s =
SetPixelFormat ( m_dc, nPixelFormat , &pfd ) ;

i f ( r e s == FALSE)
return f a l s e ;
m_glrc = wglCreateContext ( m_dc ) ;

m_created = true ;

return MakeCurrent ( ) ;
}

void GLRC : : SwapBuffers ( )


{
a s s e r t ( m_created ) ;
: : SwapBuffers (m_dc ) ;
.5. OPENGL WIN32 199

bool GLRC : : I s C u r r e n t ( ) const


{
a s s e r t ( m_created ) ;
return : : wglGetCurrentContext ( ) == m_glrc ;
}

bool GLRC : : I s C r e a t e d ( )
{
return m_created ;
}
.

.1. Cornell Box



OpenGL -
.
Cornell Box. -
,
. -
(, -
, ),
.
( , ).

, -
,
.
(http://graphics.cornell.edu)
.

201
202 .

. .1. Cornell Box.

, .1,
, -
. -
GLUT GLU.
! -
.
.

7 .


. , -
OpenGL -
,
. -
, , -
, -
.1. CORNELL BOX 203

.
(, ) -
. -

(1 ).

2- -
.


. -
8.2. 2 -
. (,
shadow volumes) .

(antialiasing) ,
8.1 - , -
2 .


1 . -
7.1.

, 15 .

( ) 7
+1
+2
+2
+2
+1

-
, , ..
204 .
.2.
-
.

. .2. .

-
. ,
.
! -
. -
.


.  , -
( ,
).
.2. 205


. , .. -
,
, -
.

, .. -
(>10)
.
,
.

(, ,
) () -
. -
.

. -
, , .. -
, ..

-
. , / -
, , ..

,
.. c
Escape. -
Screen Saver.

-
OpenGL. , , 
.
206 .
 ,
.  .

 20 . -
10 . 10 -
OpenGL
(, , .),
, ,
.

.3.
-
OpenGL ,
.

. .3. .
.3. 207

()

 -
 z = f (x, y),
(x, y) OXY
z  . -
f  , f -
T M N , x y f = T [x, y],
x y [0..M 1] [0..N 1] -
, f f
OXY x y , -
x y .
, -
T . N = M .
[x, x + 1] [y, y + 1], x y [0..N 2]
: ((x, y, T [x, y]), (x+1, y, T [x+1, y]), (x+1, y +
1, T [x+1, y +1])) ((x, y, T [x, y]), (x+1, y +1, T [x+1, y +1]), (x, y +
1, T [x, y + 1])), ,
.
T ?
 -
208 .
. -
T 2N + 1 , N  .
()
.
( -
, ) -
T, .

-
, . -
,

.

-
. , -

.
. -

.



.


. (

).

: NURBS , -
NURBS -
.
.3. 209

-
, . -
, .
, -
,
 , -
 . -
.



( ).
glColor

g l C o l o r M a t e r i a l (GL_FRONT, GL_AMBIENT_AND_DIFFUSE) ;

( ) -
, -
. ( )
-
. 5.4.

-
.  -
. 
. , , Page Up, Page
Down  , , .
210 .
GLUT' -
-
glutSpecialFunc(void (*)(int key, int x, int y)), key  -
, (. glut .h  GLUT_KEY).
glutKeyboardFunc().

(
X) -
( [0..2]).
( Y )
-
( [..]). ,
-
:

d i r e c t i o n _ z = s i n ( ) ;
d i r e c t i o n _ x = cos ( ) * cos ( ) ;
d i r e c t i o n _ y = s i n ( ) * cos ( ) ;
.
-
, -
(0, 0, 1) .
-

.
OpenGL c -
gluLookAt(). : target
position + direction.
-
, ,
.3. 211

, ,
. -
. , 
,  . , ,
timeGetTime(),
mmsystem.h, winmm.lib (-
Windows).

#include "mmsystem . h"


...
void Display ( )
{
...
int system_time_before_rendering ;
system_time_before_rendering = timeGetTime ( ) ;
RenderFrame ( ) ;
int time_spent_rendering_msec =
timeGetTime ( ) system_time_before_rendering ;
...
}
GLUT' (
timeGetTime()):
time = glutGet (GLUT_ELAPSED_TIME) ;

,
 . ,
,  -
, . , -
, -
, , -
,
. -
212 .
,
.

-
,
, , .
, -
, -
, .

, ,
( ,
-
2). :
,
, -
. ,
, -
. ,
. , -
, (
8.3).

( -
, ) -
, .

.3. 213

,  -
, -
. :
( -
, ).

8
+2
+2
+2
+3
+4
+4
+5

.  , -
.

http://www.vterrain.org

[1] , . ACM SIGGRAPH -


. , 4, 1991.

[2] Bayakovsky, Yu. Russia: Computer Graphics Education Takes


o in 1990s. Computer Graphics, 30(3), Aug. 1996.

[3] Canningham S. An Evoluing Approach to CG Courses in CS.


Graphicon'98 Conference Proceedings, MSU, Sept. 1998.

[4] Bayakovsky, Yu. Virtual Laboratory for Computer Graphics


and Machine Vision. Graphicon'99, Conference proceedings,
MSU, Sept 1999.

[5] . . -
OpenGL, 2 . . .- , -
, 2001.

[6] .. . ., BHV, 2002.

[7] . ., . . . -
. , -, 2001.

[8] . .
, BHV, 1998.

[9] Performance OpenGL: Platform Independent Techniques.


SIGGRAPH 2001 course.

215
216
[10] OpenGL performance optimization, Siggraph'97 course.

[11] Visual Introduction in OpenGL, SIGGRAPH'98.

[12] The OpenGL graphics system: a specication (version 1.1).

[13] GLUT: . Miguel Angel


Sepulveda, LinuxFocus.

[14] The OpenGL Utility Toolkit (GLUT) Programming Interface,


API version 3, specication.

[15] . . . -
- -
. http://cgm.graphicon.ru/content/view/51/61/.

API, 11 , 18

GLU, Graphics Utility Library, GL


26 glAccum, 94
GLUT, GL Utility Toolkit, 27 glArrayElement, 50
glBegin, 44
IRIS GL, 12
glBindTexture, 80
glBlendFunc, 91
OpenGL, 11
glCallList, 48
c , 30
glCallLists, 48
, 115
glClear, 40
, 12
glClearColor, 40
, 127
glColor, 42
, 101
glColorMaterial, 68
Tao Framework, 157 glColorPointer, 50
glCullFace, 46
, 20
glDeleteLists, 48
glDepthRange, 64

glDisable, 43
, 62, 90
glDisableClientState, 50
, 94
glDrawArrays, 50
, 93
glDrawBuer, 93
, 40
glDrawElements, 51
, 19 glEnable, 43

217
218
glEnableClientState, 50 glViewport, 63
glEnd, 44 GLU
glEndList, 47 gluBuild2DMipmaps, 79
glFog, 73 gluCylinder, 47, 165
glFrontFace, 45 gluDisk, 166
glGenTextures, 80 gluLookAt, 60
glHint, 96 gluNewQuadric, 47
glLight, 69 gluOrtho2D, 60
glLightModel, 66 gluPartialDisk, 166
glLoadIdentity, 57 gluPerspective, 61
glLoadMatrix, 57 gluQuadricTexture, 85
glMaterial, 67 gluScaleImage, 78
glMatrixMode, 56 gluSphere, 47, 165
glMultMatrix, 57 GLUT
glNewList, 47 gluIdleFunc, 135
glNormal, 43 glutCreateWindow, 35
glNormalPointer, 50 glutDisplayFunc, 39, 135
glOrtho, 60 glutInit, 35, 134
glPopMatrix, 57 glutInitDisplaMode, 134
glPushMatrix, 57 glutInitDisplayMode, 35
glReadBuer, 93 glutInitWindowPosition, 134
glRotate, 59 glutInitWindowSize, 35, 134
glScale, 59 glutMainLoop, 35, 136
glShadeModel, 42 glutMotionFunc, 135
glStencilFunc, 95 glutMouseFunc, 135
glStencilOp, 95 glutPassiveMotionFunc, 135
glTexCoord, 85 glutPostRedisplay, 40, 136
glTexEnv, 83 glutReshapeFunc, 64, 135
glTexGen, 85 glutSolidCone, 167
glTexParameter, 81 glutSolidCube, 167
glTranslate, 59 glutSolidDodecahedron, 168
glVertex, 31, 41 glutSolidIcosahedron, 168
glVertexPointer, 49 glutSolidOctahedron, 168
219

glutSolidSphere, 167
glutSolidTetrahedron, 168
glutSolidTorus, 167
glutWireCone, 167
glutWireCube, 167
glutWireDodecadedron, 168
glutWireIcosahedron, 168
glutWireOctahedron, 168
glutWireSphere, 167
glutWireTetrahedron, 168
glutWireTorus, 167
WGL
wglCreateContext, 148
wglDeleteContext, 149
wglMakeCurrent, 149
, 148
, 146
, 146
, 147

, 19

, 20

, 18


. . .

119992, , -2, , 1 , 2-
.

: 939-55-90
: 932-98-08
: 939-53-74
: 939-27-17

: 939-17-73

939-54-55
- http://www.cs.msu.su
e-mail: FAO@cs.msu.su
E-mail : cmc@cs.msu.ru

, -
,
:

http://www.msu.ru
OPENGL
.. , ..

You might also like