OpenGLキーボード操作とアニメーション

4074 ワード

	         OPENGL           。             ,       。      
         (       )。               ,       ,    。    
     QUAKE!           ,  。
	                CALLBACK                ,               
       :
                 

////////////////////////////////////////////////////
#include 
#include 
#include 

#pragma comment(lib, "OpenGl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glaux.lib")

#pragma warning(disable : 4244)		// MIPS
#pragma warning(disable : 4136)		// X86
#pragma warning(disable : 4051)		// ALPHA

void myinit(void);
void CALLBACK  display(void);
void CALLBACK  reshape(GLsizei w,GLsizei h);

//     ?     4 CALLBACK  
//  display() reshape(),         
//          
void CALLBACK  left(void);//  LEFT
void CALLBACK  right(void);//  RIGHT
void CALLBACK  up(void);//  UP
void CALLBACK  down(void);//  DOWN

//      ,z_motion        
//rotate        
static int z_motion=0,rotate=0;

void myinit(void)
{
    	auxInitDisplayMode(AUX_SINGLE | AUX_RGBA);
        auxInitPosition(0,0,500,500);
        auxInitWindow("sample1");
        glClearColor(0.0,0.0,0.0,0.0);
        glClear(GL_COLOR_BUFFER_BIT);
}

void CALLBACK reshape(GLsizei w,GLsizei h)
{
	if(w<=500&&h<=500)
		glViewport(0,0,w,h);
	if(w>500&&h<=500)
		glViewport(0,0,500,h);
	if(w<=500&&h>500)
		glViewport(0,0,w,500);
	if(w>500&&h>500)
		glViewport(0,0,500,500);


	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
///*if(w<=h)
// glOrtho(-20.0,20.0,-20.0*(GLfloat)h/(GLfloat)w,
//         20.0*(GLfloat)h/(GLfloat)w,-50.0,50.0);
//else
//  glOrtho(-20.0*(GLfloat)h/(GLfloat)w,
//         20.0*(GLfloat)h/(GLfloat)w,-20.0,20.0,-50.0,50.0);
//*/
/************************************************************/
//           :    ,      
//         。 4             
//left right top bottom,             
//Z  ,     Z  ,      :
//void glFrustum(GLdouble left,GLdouble right,
//               GLdouble bottom,GLdouble top,
//               GLdouble near,GLdouble far);
/************************************************************/
	glFrustum(-20.0,20.0,-20.0,20.0,10.0,50.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void draw(void)
{
	glBegin(GL_TRIANGLE_STRIP);
	glColor3f(1.0,0.0,0.0);
	glVertex3f(15.0,0.0,0.0);
	glColor3f(0.0,1.0,0.0);
	glVertex3f(-15.0,0.0,0.0);
	glColor3f(0.0,0.0,1.0);
	glVertex3f(0.0,15.0,15.0);
//
	glColor3f(0.0,1.0,1.0);
	glVertex3f(10.0,15.0,-15.0);
//
	glColor3f(1.0,1.0,0.0);
	glVertex3f(15.0,0.0,0.0);
//
	glEnd();
}

void CALLBACK display(void)
{
	glClearColor(0.0,0.0,0.0,1.0);
	glClear(GL_COLOR_BUFFER_BIT);

//glPushMatrix();
	glLoadIdentity();

//  z_motion rotate             :
//z_motion   ,    Z  -25,  z_motion   
	glTranslatef(0.0,0.0,-25.0+z_motion);
//rotate   ,   Y (    )  5*rotate 
	glRotatef(rotate*5.0,0.0,1.0,0.0);

	draw();
//glPopMatrix();

	glFlush();
}
void CALLBACK left(void)
{
//    LEFT,rotate   +1
//       
	rotate++;
}
void CALLBACK right(void)
{
	rotate--;
}
void CALLBACK up(void)
{
	z_motion++;
}
void CALLBACK down(void)
{
z_motion--;
}
void main(void)
{
        myinit();

//          left() right() up() down()
//            
    auxKeyFunc(AUX_LEFT,left);
    auxKeyFunc(AUX_RIGHT,right);
    auxKeyFunc(AUX_UP,up);
    auxKeyFunc(AUX_DOWN,down);

    auxReshapeFunc(reshape);
    auxMainLoop(display);
}
//end of sample
////////////////////////////////////////////////////
                   ,       
                  ,