,,,
:
OpenGL
3
..
..
2004
1.1. OpenGL.. 5
1.2. OpenGL.. 8
1.3 OpenGL . 10
1.4. OpenGL.. 11
1.4.1. . 11
1.4.2 . 12
1.4.3 . 13
1.4.4 . 13
1.4.5 . 13
1.4.6 . 14
1.4.7 .. 15
1.4.8. . 15
1.4.9 . 16
2 , OpenGL.. 17
2.1 OpenGL.. 17
2.2. .. 19
2.3 GLUT, OpenGL.. 20
2.3.1. . 20
2.3.2 . 21
2.3.3 . 22
2.3.4. .. 23
2.3.4 .. 25
2.3.5 .. 25
2.3.6 . 26
3. . 27
3.1. . 27
3.2 .. 30
OpenGL . 250 ( 200 OpenGL 50 OpenGL), , , .
OpenGL , - , . , OpenGL ; , . OpenGL . , , , , . OpenGL , .
, , , OpenGL. OpenGL (GLU OpenGL Utility Library) , NURBS- (NURBS Non-Uniform, Rational B-Spline -). GLU OpenGL. , FSG (Fahrenheit Scene Graph), OpenGL, OpenGL.
OpenGL , OpenGL- . : , , , OpenGL , , .
OpenGL, . , , , . , , , , , .
, ; , .
(, ) . , ; , , , , , . , , , , , .
1. , 1.
1. OpenGL
finclude <whateverYouNeed.h> ,
main () {
InitializeAWindowPlease();
glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0); glEnd() ; glFlush () ;
UpdateTheWindowAndCheckForEvents(); }
main() : InitializeAWindowPlease() -"" , OpenGL. OpenGL, : glCIearCoIor() , , glClear() . , , glClear(). glClearColor(). glColor3f() , . , , , .
OpenGL, , glOrtho(), , OpenGL , , . , glBegin() gl£nd(), , , . "" glVertex3f(). , , , , (, , z), , z(z 0).
, gIFlush() , , , OpenGL. -"" UpdateTheWindowAndCheckForEvents() .
, , , , OpenGL gl. , , (, , glClearColor()). , OpenGL, GL_, , (, GL__COLOR_BUFFER_BIT).
, , , , (, 3f glColor3f() glVertex3f()). , Color glColor3f() , , . , . , 3 , ; Color . f , . OpenGL .
OpenGL 8 . , , ISO OpenGL, 1.1; OpenGL. OpenGL, , ; , C++ Ada, .
|
|
OpenGL |
8- | signed char | GLbyte |
16- | short | GLshort |
32- | Int long | GLint, GLsizei |
32-
|
float | GLfloat, GLclampf |
64-
|
double | GLdouble, GLclampd |
8- | unsigned char | GLubyte, GLboolean |
16- | unsigned short | GLushort |
32- |
unsignedint unsigned long |
GLuint, GLenum, GLbitfield |
1.1
,
glVertex2i (1,3); glVertex2f (1.0, 3.0);
, , 32- , .
OpenGL v, , ( ) , . , , , , , , . , , :
glColor3f (1.0, 0.0, 0.0);
gLfloat color_array [] = {1.0, 0.0, 0.0); glColor3fv (color_array);
, OpenGL GLvoid. OpenGL, .
OpenGL . ( ), , . , . , , , , - . , OpenGL. , , , , , , , . , glEnable() glDisable() .
, . , , : glGetBooleanv(), glGetDoubIev(), glGetFloatv(), glGetlntegerv(), glGetPointerv() gIIsEnabled(). To, , , . ( glGetLight*(), glGetError() glGetPolygonStipple()). , glPushAttrib() gIPushCIientAttrib(), , glPopAttrib() glPopClienlAttrib(). , , .
OpenGL , , OpenGL. , 1.2, OpenGL, OpenGL.
, OpenGL . (, ) , , (, ) -. ( ), .
. 1.2
, , , . ( , .) , , .
, , . , . , . , , , .
" ", . (, ) 4x4. .
, . , . , , , , , , .
, , , , . ; , .
, , . (Viewport) (-). , , . .
, , , .
OpenGL, . - . , . , , . (. " " 8.)
, (, , ). .
, . , , .
OpenGL- , . , .
OpenGL . , , . , .
, . . , , , , , , , , . .
, , . .
, , . , ( ) . , , -, ( ). . , () , . , , .
OpenGL , , . OpenGL- . , . :
OpenGL (GLU OpenGL Utility Library) , OpenGL- , , ( ) . OpenGL. GLU OpenGL {OpenGL Reference Manual).
, , OpenGL. , X Window System, OpenGL OpenGL X Window System(GLX OpenGL Extension to the X Window System). GLX glX. Microsoft Windows 95/98/NT Windows OpenGL WGL. WGL wgl.
OS/2 IBM PGL (Presentation Manager) OpenGL, pgl. Apple , OpenGL, AGL, AGL agl.
. , GLX OpenGL.
OpenGL (GLUT Graphics Library UtilityToolkit) , (Mark Kilgard) , (API ApplicationProgramming Interface) . GLUT , OpenGL Programming for the X Window System (ISBN 0-201-48359-9). GLUT glut
FSG (Fahrenheit Scene Graph) - , OpenGL . FSG C++, , . FSG OpenGL.
OpenGL- gl.h . OpenGL- GLU, OpenGL, glu.h. OpenGL- :
#include <gi/gl.h> include <gl/glu.h>
Microsoft Windows , windows.h gl.h glu.h, , Microsoft Windows- gl.h glu.h, windows.h.
, OpenGL, , GLX, AGL, PGL WGL, . , GLX, , , , :
include <X11/Xlib.h> include <GL/glx.h>
Microsoft Windows WGL :
include <windows.h>
(
#include <GL/glaux.h>
OpenGL. . .)
GLUT , :
#include <GL/glut.h>
OpenGL- , , ( \32- /- . . .), :
#include <stdlib.h> #include <stdio.h>
, OpenGL , , . , . , , , , , , , . , GLUT , , .. OpenGL GLUT, , , OpenGL GLUT.
, OpenGL , (, ), GLUT , , , . , . ( , OpenGL, GLU, , , , , GLUT, , .)
GLUT OpenGL-, OpenGL. GLUT , .
GLUT , , .
glutlnit(int *argc, char **argv) GLUT ( X WindowSystem , -display -geometry). glutlnit() GLUT.
glutInitDisplayMode(unsigned int mode) , : RGBA . , . ( , , , ; , glutSetColor().) , , , , / -. , , RGBA , : g\utlnitmsv\&yMote{GLUT_DOUBLE\ GLUT^RGB \ GLUTJ)EPTH).
glutInitWindowPosition(int , int ) .
glutInitWindowSize(int width, int size) .
int glutCreateWindow(char *string) OpenGL. . : , glutMainLoopO, .
gIutDisplayFunc(void (*/nc)(void)) , . , GLUT , , , glutDisplayFunc(). , , .
, glutPostRedisplay(void), glutMainLoopO .
, , glutMainLoop(void). , , . , . , !
2 , GLUT, , 1. . , , ( ), , init(). , (, , ) , display(), GLUT.
2 OpenGL, GLUT: hello.c
#include <GL/glut.h> #include <stdlib.h>
void display(void)
/* */
glClear(GL_COLOR_BUFFER_BIT);
/* () ,
(0.25, 0.25, 0.0)
(0.75, 0.75, 0.0)*/
glColor3f(1.0, 1.0, 1.0); glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);glEnd() ;
/* He !
* OpenGL*/
glFlushO ; }
void init(void) {
/* ( ) */
glClearColor (0.0, 0.0, 0.0, 0.0);
/* */
glMatrixMode(GL_PROJECTION);
glLoadldentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); } /*
, ( RGBA).
"hello" . . . .*/
int main(int argc, char** argv) {
glutInit(Sargc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow("hello");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0; /* , ISO, , main int. */ }
, , , .
glutReshapeFunc(void (*func)(int w, int h)) , .
glutKeyboardFunc(void (*/wc)(unsigned char key, int x, int )) glutMouseFunc (void (*func)(int button, int state, int x, int )) , , .
glutMotionFunc(void (*func)(int x, int )) .
, glutIdleFunc(void (*/nc)(void)) ,
, , . . , NULL ().
GLUT :
. , :
void glutWireCube(GLdouble size);
void glutSolidCube(GLdouble size);
void glutWireSphere(GLdouble radius, GLint slices, GLint stacks);
void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks);
.
, , . , , , , , , , .
24 . , , . , , , . 24 , "" . ( 16 .) ( ) 60 76 , 120 . , 60 "", 30 , 120
, 60 . , 120 , , .
, , , . , , , , :
();
for (i = 0; i < 1000000; i++) {
();
_ (i) ;
______1_24___(); )
, , , , , 1/ 24 . , 1/24 . , , 1/24 ; , , , . , 1/24 , , , . , ; .
OpenGL , . , . , , , , , . , ; , . , "" , , . , ; .
, , , :
_____(); for (i = 0; i < 1000000; i++) {
1_();
_(i);
__() ; }
OpenGL , __() , , . , . , 60 , , , 60 (fps frames per second). , , 1/60 , .
, , 1/60 , , , . , , 1/45 , , 30 fps, "" 1/30 1/45 = 1/90 , .
, , . , , , 1/60 60 fps, 30 fps, 20 fps, 15 fps, 12 fps . . (60/1, 60/2, 60/3, 60/4, 60/5, . .). , (, , ), , 60 fps. , , 1/60 , 60 fps 30 fps, . , , 1/30 30 fps 20 fps.
, "" (1/60 , 2/60 , 3/60 . . ), - , . , . , ,
, , , , , , . , , , .
= +
. , , . , , .
: , - . , , , . , , __() .
OpenGL "__-()", , , . , X Window System , GLX:
void glXSwapBuffers(Display *dpy, Window window);
( .)
GLUT, , , :
void glutSwapBuffers(void);
3 glutSwapBuffers() , 3. , GLUT , . .
3 : double.c
#include <stdlib.h> #include <GL/glut.h>
static GLfloat spin = 0.0;
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT); } void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
glRectf(-25.0, -25.0, 25.0, 25.0);
glPopMatrix () ;
glutSwapBuffers () ; )
void spinDisplay(void)
{
spin = spin +2.0;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay(); }
void reshape (int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h) ;
glMatrixMode(GL_PROJECTION);
glLoadldentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadldentity();
void mouse(int button, int state, int x, int y) {
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutldleFunc(spinDisplay); break; case GLUT_MIDDLE_BUTTON:
if (state == GLUT_DOWN)
glutldleFunc(NULL); break; default:
break; } }
/*
.
*/
int main(int argc, char** argv) {
glutlnit(Sargc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init<);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0 ; }
, C, OpenGL.
. , - .
#include <GL/glut.h>
#include <GL/glaux.h>
#include <GL/glu.h>
int tex[1];
float xtr,ytr,ztr;
float rotx,roty,rotz;
float LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
float LightPosition[]= { 0.0f, 0.0f, 5.0f, 1.0f };//
float LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f }; //
void LoadTexs()//
{
AUX_RGBImageRec *tex1;
tex1 = auxDIBImageLoad("droy.bmp");
glGenTextures(1, &tex[0]);
glBindTexture(GL_TEXTURE_2D, tex[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, tex1->sizeX, tex1->sizeY, 0,
GL_RGB, GL_UNSIGNED_BYTE, tex1->data);
}
void init()
{
LoadTexs();//
glColor4f(1.0f,1.0f,1.0f,0.5f);
glBlendFunc(GL_SRC_ALPHA,GL_ONE); //
glEnable(GL_TEXTURE_2D);
glClearColor (0.0, 0.0, 0.0, 0.0);//
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST); //
glShadeModel (GL_SMOOTH);
glTranslatef(0.0f,0.0f,-5.0f);
glLightfv(GL_LIGHT1,GL_AMBIENT,LightAmbient); //
glLightfv(GL_LIGHT1,GL_POSITION,LightPosition);
glLightfv(GL_LIGHT1,GL_DIFFUSE,LightDiffuse);
glEnable(GL_LIGHT1);//
glEnable(GL_LIGHTING);
glEnable (GL_COLOR_MATERIAL);
glColorMaterial (GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//
glPushMatrix();//
glTranslatef(xtr,ytr,ztr);
glRotatef(rotx,1.0,0.0,0.0);
glRotatef(roty,0.0,1.0,0.0);
glRotatef(rotz,0.0,0.0,1.0);
glBegin(GL_QUADS);
glNormal3f( 0.0f, 0.0f, 1.0f);//
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); //
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); //
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); //
glNormal3f( 0.0f, 0.0f,-1.0f);//
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); //
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); //
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); //
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); //
glNormal3f( 0.0f, 1.0f, 0.0f);//
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); //
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); //
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); //
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); //
glNormal3f( 0.0f,-1.0f, 0.0f);//
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); //
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); //
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); //
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //
glNormal3f( 1.0f, 0.0f, 0.0f);//
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); //
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); //
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); //
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); //
glNormal3f(-1.0f, 0.0f, 0.0f);//
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); //
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); //
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); //
glEnd();
glPopMatrix();//
rotx+=0.2;//
roty+=0.1;// Y
rotz+=0.3;// Z
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);//
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (0.0, 1.5, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);//
}
void keyboard (unsigned char key, int x, int y)//
{
switch (key) {
case 27:
exit(0);
break;
case 'e':
glEnable(GL_BLEND);// -
break;
case 'd':
glDisable(GL_BLEND);//
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);//
glutInitWindowPosition (0, 0);//
glutCreateWindow ("DroY Cube (c) 2003 Press 'ESC' to exit");
init ();
glutFullScreen();//
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(display);
glutMainLoop();
return 0;
}
1. " . OpenGL", .
2. OpenGL. , , ,
3. .. OpenGL: .
4. . .
5. http://www.gamedev.ru OpenGL
: OpenGL
Copyright (c) 2024 Stud-Baza.ru , , , .