Professional Documents
Culture Documents
Opengl2 PDF
Opengl2 PDF
.. , ..
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
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
. 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
-
.
, ,
, -
. , , 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, -
( ) 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.
//
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 ;
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;
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.
3.2.3.
, -
. OpenGL
,
.
glEnable(GL_NORMALIZE).
,
-
/, -
- .
-
OpenGL,
(
).
.
,
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.
,
. -
.
,
,
.
GL_FRONT ;
GL_BACK ;
GL_FRONT_AND_BACK .
mode :
GL_POINT ;
GL_LINE
;
GL_FILL
,
.
. -
glEnable (GL_CULL_FACE), -
, , -
GLUquadricObj
3.4.
, -
(display list) .
, ,
, ,
:
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 . -
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, , -
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 g l L o a d I d e n t i t y ( void )
.
, -
. 4.2. OpenGL
: OpenGL
-
. -
glVertex
.
4.2. -
- -
, .
-
-
4.2. - 59
:
0
x x
y 0 y
=M
z 0 z
1 1
M - . -
-
.
:
4.3.
OpenGL
() .
-
( ) -
.
. 4.3.
, , near far
1 1 . ,
OpenGL .
,
glVertex2.
. 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
-
,
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)
-
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 -
, -
.
5.2.
: 0.
68 5.
GL_EMISSION params
RGBA,
.
, glMaterial[ i f ]() -
-
(shininess). glMaterial[ i f ]v()
.
face , -
GL_FRONT,
GL_BACK GL_FRONT_AND_BACK.
, ,
glEnable() c GL_COLOR_MATERIAL,
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). -
: 0 ( ).
GL_SPOT_CUTOFF param
0 90
180, -
.
, .
: 180 ( ).
GL_AMBIENT params
RGBA,
.
GL_SPECULAR params -
RGBA, -
.
GL_POSITION params
, -
. w -
0.0,
(x, y, z), ,
(x, y, z, w).
5.3. 71
, .. -
.
GL_CONSTANT_ATTENUATION ,
GL_LINEAR_ATTENUATION ,
GL_QUADRATIC_ATTENUATION params
, -
.
. (.
GL_POSITION),
:
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).
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), -
.
datain dataout. , -
GL_UNSIGNED_BYTE, GL_SHORT, GL_INT ..
,
dataout.
-, ,
-
. ,
-
(mipmap).
, -
, , .
. , 2m 2n
max(m, n) + 1 , -
.
-
OpenGL
GL_LUMINANCE (
);
GL_RGB , , ;
GL_RGBA , , , (. . 7.1).
6.2.
, , -
,
, . -
, , , -
,
. -
(s, t), s t [0, 1]
(. 6.1)
. 6.1.
-
:
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, .
, -
, ,
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 ) ;
glTexParameterf (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,
( float )GL_LINEAR) ;
/* 5) . */
f r e e ( Texture ) ;
}
else
Error ( ) ;
6.3.
. : -
,
6.3. 85
void gluQuadricTexture (
GLUquadricObj * quadObject ,
GLboolean textureCoords )
textureCoords GL_TRUE,
.
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).
-
:
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) ;
, -
:
-
, .. ,
.
OpenGL -
, -
glVertex, .
7.2. - 93
7.2. -
- (accumulation buer) -
OpenGL. -
, -
. -
.
, -
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 () -
.
, .
-
:
GL_NEVER , .. -
false;
GL_ALWAYS ;
mask . ..
: ((ref AND mask) op
(svalue AND mask)).
glStencilOp
-
.
96 7.
sfail -
, :
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
. -
, .
,
, , -
,
. -
.
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. ,
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 ;
, -
, . , -
, -
, , , -
.
(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][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 = */
};
.
, ,
:
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 ;
/* */
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 -
.
:
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
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 . -
,
. -
, , :
g l B e g i n (GL_TRIANGLES) ;
glVertex3f (1 ,1 ,1);
glEnd ( ) ;
128 9.
CHECK_GL;
glColorMaterial glMaterial
, glColorMaterial ,
glMaterial.
OpenGL
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
;
(,
.).
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 g l u t P o s t R e d i s p l a y ( void )
glutReshapeFunc()
,
.
glutMouseFunc() -
, glutIdleFunc() , -
, .
, glutMotionFun(), ,
, -
. glutPassiveMotionFunc , ,
-
.
-
#i n c l u d e <GL/ g l u t . h>
/* */
glutSwapBuffers ( ) ;
};
: File New
Projects Win32 Console Application, ,
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
: 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,
-
.
-
:
: File New
Other Console Wizard, OK.
, (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.
: File New
Other Console Application, OK.
, (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 ) ;
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;
( ) , -
CWnd .
OpenGL -
OnCreate, OnPaint, OnUpdate, OnDraw
OnTimer ( , ,
OpenGL-
).
WM_ERASEBKGND, Windows
OnPaint.
OnDestroy,
OnSize.
CWnd::GetSafeHwnd.
11.5. OPENGL C MFC 151
,
OpenGL.
// 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 )
{
//
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 ( ) ;
}
//
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
OpenGL .NET
-
OpenGL Microsoft .NET.
, OpenGL -
.NET, -
, OpenGL .NET-.
--
Tao Framework.
, -
.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.
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 ( ) ;
}
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 ( ) ;
}
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 . glutMainLoop ( ) ;
}
}
}
, GL, GLU
GLUT Gl, Glu Glut, -
.
WindowsForms
OpenGL:
: File New Projects Visual
C# Windows Windows Application, -
, OK.
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() .
:
165
166 . GLU GLUT
(),
z, -
baseRadius, z = 0,
topRadius
z = height. ,
. slices stacks
, .
() -
outerRadius. innerRadius -
, -
innerRadius. slices
z, loops ,
z.
,
, -
-
y startAngle
sweepAngle. .
, -
GLUT, OpenGL
167
GLU. -
.
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 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 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.
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) ;
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);
}
glutDisplayFunc ( Display ) ;
glutReshapeFunc ( Reshape ) ;
glutKeyboardFunc ( Keyboard ) ;
glutMouseFunc ( Mouse ) ;
glutMainLoop ( ) ;
}
.2. 2: OPENGL 173
. .1. .1. -
, .
.2. 2: OpenGL
-
OpenGL , , -
. .
.
.2. OpenGL.
//
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
#ifndef _LOADBMP_H
#define _LOADBMP_H
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
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 ;
}
//
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 ;
}
{
WORD bfType ;
DWORD bfSize ;
WORD bfReserved1 ;
WORD bfReserved2 ;
DWORD bfOffBits ;
} BITMAPFILEHEADER;
.5. .
#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) ;
}
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 ) ;
}
//
// ( )
glutPostRedisplay ( ) ;
}
. .3. .5.
.5. OPENGL WIN32 195
#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.
GLRC: : ~GLRC( )
{
i f ( m_created )
.5. OPENGL WIN32 197
Destroy ( ) ;
}
i f ( IsCurrent ( ) )
return true ;
BOOL r e s = wglMakeCurrent (m_dc, m_glrc ) ;
return ( r e s != FALSE ) ;
}
int nPixelFormat = 0 ;
DWORD f l a g s ;
flags =
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
pfd . cAlphaBits = 8 ;
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 ( ) ;
}
bool GLRC : : I s C r e a t e d ( )
{
return m_created ;
}
.
201
202 .
, .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).
,
. ,
, -
, . , -
, -
, , -
,
. -
212 .
,
.
-
,
, , .
, -
, -
, .
, ,
( ,
-
2). :
,
, -
. ,
, -
. ,
. , -
, (
8.3).
( -
, ) -
, .
.3. 213
, -
, -
. :
( -
, ).
8
+2
+2
+2
+3
+4
+4
+5
. , -
.
http://www.vterrain.org
[5] . . -
OpenGL, 2 . . .- , -
, 2001.
[7] . ., . . . -
. , -, 2001.
[8] . .
, BHV, 1998.
215
216
[10] OpenGL performance optimization, Siggraph'97 course.
[15] . . . -
- -
. http://cgm.graphicon.ru/content/view/51/61/.
API, 11 , 18
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
.. , ..