NeHe Tutorials OpenGL
 9. OpenGL

Moving Bitmaps In 3D Space

9- . OpenGL. , OpenGL, (blending). "" . : (bitmap) 3D, , (pixels) ( ), - , , , .

. . , , .

#include <windows.h>// Windows
#include <stdio.h>// /
#include <gl\gl.h>// OpenGL32
#include <gl\glu.h>// GLu32
#include <gl\glaux.h>// GLaux

HDChDC=NULL;// GDI
HGLRChRC=NULL;//
HWNDhWnd=NULL;//
HINSTANCEhInstance;//

boolkeys[256];// ,
boolactive=TRUE;// ( : TRUE)
boolfullscreen=TRUE;// ( : )

. twinkle tp , TRUE () FALSE (). twinkle / twinkle. tp 'T' ( ). , tp=TRUE, tp=FALSE.


BOOLtwinkle;// Twinkling Stars ( )
BOOLtp;// 'T' ?

num , , . . , . , , , (/) . , 50 num 51 - , , . num . num - , , :).

constnum=50;//

(structure). , . (, ..) - . , . , 7- stars;. , 3 , : 3- : int r,g,b . (red) (r), (green) (g), (blue) (b). , , 360 . 4- , . (floating point value) dist. . 5- angle. .

, , . , , 50 , 50 , 50 , 50 , 50 star. star stars. 8- : stars star[num]. stars. stars . . star. - [num]. num=50, star. stars. , . num.

(. . - , , :). .)

typedef struct//
{
int r, g, b;//
GLfloat dist;//
GLfloat angle;//
}
stars;// - Stars
stars star[num];// 'star' 'num',
// 'stars'

(zoom), (tilt). spin, z, .

loop , 50- , texture[1] - , . , , .

GLfloatzoom=-15.0f;//
GLfloat tilt=90.0f;//
GLfloatspin;//

GLuintloop;//
GLuinttexture[1];//

LRESULTCALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);// WndProc

. , 6, 7 8 . , star.bmp. glGenTextures(1, &texture[0]). (linear filtering).

AUX_RGBImageRec *LoadBMP(char *Filename)// bmp
{
FILE *File=NULL;//

if (!Filename)//
{
return NULL;// , NULL
}

File=fopen(Filename,"r");//

if (File)// ?
{
fclose(File);// ,
// AUX,
return auxDIBImageLoad(Filename);
}
// , NULL
return NULL;
}

( ) . Status .

int LoadGLTextures()//
{
int Status=FALSE;//

AUX_RGBImageRec *TextureImage[1];//

memset(TextureImage,0,sizeof(void *)*1);// NULL

// , ,
if (TextureImage[0]=LoadBMP("Data/Star.bmp"))
{
Status=TRUE;// TRUE

glGenTextures(1, &texture[0]);//

// (Linear Filtered)
glBindTexture(GL_TEXTURE_2D, texture[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, TextureImage[0]->sizeX,
TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
}

if (TextureImage[0])//
{
if (TextureImage[0]->data)//
{
//
free(TextureImage[0]->data);
}

free(TextureImage[0]);//
}

return Status;//
}

OpenGL , . Z- ( ) , : glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); , , . .

int InitGL(GLvoid)// OpenGL
{
if (!LoadGLTextures())//
{
return FALSE;// , FALSE
}

glEnable(GL_TEXTURE_2D);//
// ( )
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);//
glClearDepth(1.0f);// (Depth Buffer)
//
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glEnable(GL_BLEND);//

. , . , . 50 . star[1] , - star[1].angle={ }. !

for (loop=0; loop<num; loop++)//
{
star[loop].angle=0.0f;// 0

( loop) . 5.0f. , - , . loop 50 ( ), loop num 1.0f. 5 , 1.0f*5.0f 5.0f. 5.0f . , , 5.0f . zoom , , 5.0f, (- ).

, 0 255. , , 0.0f 1.0f. . , glColor4ub glColor4f. ub Unsigned Byte ( ). 0 255. , .

//
star[loop].dist=(float(loop)/num)*5.0f;
// star[loop] ().
star[loop].r=rand()%256;
// star[loop] ()
star[loop].g=rand()%256;
// star[loop] ()
star[loop].b=rand()%256;
}
return TRUE;// .

}

Resize , . , DrawGLScene , , . , .

int DrawGLScene(GLvoid)//
{
//
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//
glBindTexture(GL_TEXTURE_2D, texture[0]);

for (loop=0; loop<num; loop++)//
{
// (Model Matrix)
glLoadIdentity();
// z 'zoom'
glTranslatef(0.0f,0.0f,zoom);
// x 'tilt'
glRotatef(tilt,1.0f,0.0f,0.0f);

! :) . y. 90 , x , . : , . , -x, -z, +x, +z. 90 , , -z, -x. . -z , +z , -x , +x . ? , x z.

x. x ( +x), y, +x . 180 , +x . , x, , , .

// y
glRotatef(star[loop].angle,0.0f,1.0f,0.0f);
// x
glTranslatef(star[loop].dist,0.0f,0.0f);

. . - . , , . y 90 , . . . , , .

, , . . , . , . , , . 10 , , -10 y. y. x. -tilt. .

glRotatef(-star[loop].angle,0.0f,1.0f,0.0f);
//
glRotatef(-tilt,1.0f,0.0f,0.0f);//

twinkle TRUE, . , , (num) (loop), , 0 num-1. 10, 10- . , . , . (alpha). , (.. , .. ).

twinkle , . . twinkle , . , , twinkling . ( , ).

, . -, , , . , . .

if (twinkle)// Twinkling
{
//
glColor4ub(star[(num-loop)-1].r,star[(num-loop)-1].g,
star[(num-loop)-1].b,255);
glBegin(GL_QUADS);//
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();//
}

. , , z.

glRotatef(spin,0.0f,0.0f,1.0f);// z
//
glColor4ub(star[loop].r,star[loop].g,star[loop].b,255);
glBegin(GL_QUADS);//
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();//

. spin. . loop/num. . , . . .

spin+=0.01f;//
star[loop].angle+=float(loop)/num;//
star[loop].dist-=0.01f;//

. . , 5 . .

if (star[loop].dist<0.0f)// ?
{
star[loop].dist+=5.0f;// 5
//
star[loop].r=rand()%256;
//
star[loop].g=rand()%256;
//
star[loop].b=rand()%256;
}
}
return TRUE;//
}

. WinMain(). SwapBuffers(hDC). .

'T'. , , . twinkle FALSE, TRUE. TRUE, FALSE. 'T' tp TRUE. , 'T' .

SwapBuffers(hDC);// (Double Buffering)
if (keys['T'] && !tp)// 'T' tp FALSE
{
tp=TRUE;// tp TRUE
twinkle=!twinkle;// twinkle
}

"" ( ) 'T'. , tp=FALSE. 'T' tp FALSE, .

if (!keys['T'])// 'T'
{
tp=FALSE;// tp FALSE
}

, ' ', ' ', 'page up', 'page down'.

if (keys[VK_UP])// ?
{
tilt-=0.5f;//
}

if (keys[VK_DOWN])// ?
{
tilt+=0.5f;//
}

if (keys[VK_PRIOR])// Page Up ?
{
zoom-=0.2f;//
}

if (keys[VK_NEXT])// Page Down ?
{
zoom+=0.2f;//
}

, , .

if (keys[VK_F1])// F1 ?
{
keys[VK_F1]=FALSE;// FALSE
KillGLWindow();//
fullscreen=!fullscreen;
// Fullscreen () / Windowed ()
// OpenGL
if (!CreateGLWindow("NeHe's Textures,
Lighting & Keyboard Tutorial",640,480,16,fullscreen))
{
return 0;//
}
}
}
}

, - , ( ), , . , . , . 3D. .

Jeff Molofee (NeHe)

PMG  19 2001 (c)  Mike Samsonov