opengl super bible 블록 애니메이션 개선 코드

3923 단어
#include <freeglutGL/freeglut.h>

///*cube pos value*/
GLfloat rfCubeX = 0.0f;
GLfloat rfCubeY = 0.0f;
GLfloat rfCubeSize = 25.0f;
GLfloat rfCubeXStep = 1.0f;
GLfloat rfCubeYStep = 1.0f;

///*clip volume size*/
GLfloat rfClipVolumeHalfWidth = 0.0f;
GLfloat rfClipVolumeHalfHeight = 0.0f;

void fRenderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT);

	glColor3f(1.0f,0.0f,0.0f);
	glRectf(rfCubeX,rfCubeY,rfCubeX+rfCubeSize,rfCubeY-rfCubeSize);
	//glFlush();
	glutSwapBuffers();
}

void fReshapeScene(GLsizei w,GLsizei h)
{
	GLfloat rfAspectRatio;
	if(!h) {
		h = 1;
	}
	rfAspectRatio = (GLfloat)w/(GLfloat)h;

	glViewport(0,0,w,h);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	if(w <= h) {
		rfClipVolumeHalfWidth = 100.0f;
		rfClipVolumeHalfHeight = rfClipVolumeHalfWidth/rfAspectRatio;
		glOrtho(-100.0f,100.0f,-rfClipVolumeHalfHeight,rfClipVolumeHalfHeight,0.000000001f,0.0f);
	} else
	{
		rfClipVolumeHalfHeight = 100.0f;
		rfClipVolumeHalfWidth = rfClipVolumeHalfHeight*rfAspectRatio;
		glOrtho(-rfClipVolumeHalfWidth,rfClipVolumeHalfWidth,-100.0f,100.0f,0.00000001f,0.0f);
	}
	//glOrtho(-100.0f,100.0f,-100.0f/rfAspectRatio,100.0f/rfAspectRatio,1.0f,-1.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void fSetupRC(void)
{
	glClearColor(0.0f,1.0f,0.0f,1.0f);
}

void fTimerFunction(int iValue)
{
	///*turn*/
	if(rfCubeX<=-rfClipVolumeHalfWidth||rfCubeX>=rfClipVolumeHalfWidth-rfCubeSize) {
		rfCubeXStep = -rfCubeXStep;
	}
	if(rfCubeY<=-rfClipVolumeHalfHeight+rfCubeSize||rfCubeY>=rfClipVolumeHalfHeight) {
		rfCubeYStep = -rfCubeYStep;
	}

	rfCubeX += rfCubeXStep;
	rfCubeY += rfCubeYStep;

	///*horizontal overflow*/
	if(rfCubeX<-rfClipVolumeHalfWidth) {
		rfCubeX = -rfClipVolumeHalfWidth;
	} else
	if( rfCubeX>(rfClipVolumeHalfWidth-rfCubeSize)) {
		rfCubeX = rfClipVolumeHalfWidth-rfCubeSize;
	}
	///*vertical overflow*/
	if(rfCubeY>rfClipVolumeHalfHeight) {
		rfCubeY = rfClipVolumeHalfHeight;
	} else
	if( rfCubeY<(-rfClipVolumeHalfHeight+rfCubeSize) ) {
		rfCubeY = -rfClipVolumeHalfHeight+rfCubeSize;
	}

	glutPostRedisplay();
	glutTimerFunc(20,fTimerFunction,1);
}

int main(int argc,char* argv[])
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
	glutInitWindowSize(300,400);
	glutCreateWindow("Animation");
	glutDisplayFunc(fRenderScene);
	glutReshapeFunc(fReshapeScene);
	glutTimerFunc(20,fTimerFunction,1);

	fSetupRC();

	glutMainLoop();

	return (0);
}

첫째, 원문에는 두 가지 변수가 있는데 그것이 바로 window Width와 window Height이다. 나는 이름이 정확하지 않다고 생각한다. 여기서
///*clip volume size*/GLfloat rfClipVolumeHalfWidth = 0.0f; GLfloat rfClipVolumeHalfHeight = 0.0f;
즉 재단체의 반 높이와 반 너비다.
그리고 이 두 변수의 변화 추적:
if(w <= h) { rfClipVolumeHalfWidth = 100.0f; rfClipVolumeHalfHeight = rfClipVolumeHalfWidth/rfAspectRatio; glOrtho(-100.0f,100.0f,-rfClipVolumeHalfHeight,rfClipVolumeHalfHeight,0.000000001f,0.0f); } else { rfClipVolumeHalfHeight = 100.0f; rfClipVolumeHalfWidth = rfClipVolumeHalfHeight*rfAspectRatio; glOrtho(-rfClipVolumeHalfWidth,rfClipVolumeHalfWidth,-100.0f,100.0f,0.00000001f,0.0f); }
애니메이션 @ 시간:
//turn if(rfCubeX<=-rfClipVolumeHalfWidth||rfCubeX>=rfClipVolumeHalfWidth-rfCubeSize) { rfCubeXStep = -rfCubeXStep; } if(rfCubeY<=-rfClipVolumeHalfHeight+rfCubeSize||rfCubeY>=rfClipVolumeHalfHeight) { rfCubeYStep = -rfCubeYStep; } rfCubeX += rfCubeXStep; rfCubeY += rfCubeYStep; ///*horizontal overflow*/if(rfCubeX<-rfClipVolumeHalfWidth) { rfCubeX = -rfClipVolumeHalfWidth; } else if( rfCubeX>(rfClipVolumeHalfWidth-rfCubeSize)) { rfCubeX = rfClipVolumeHalfWidth-rfCubeSize; }///*vertical overflow*/if(rfCubeY>rfClipVolumeHalfHeight) { rfCubeY = rfClipVolumeHalfHeight; } else if( rfCubeY<(-rfClipVolumeHalfHeight+rfCubeSize) ) { rfCubeY = -rfClipVolumeHalfHeight+rfCubeSize; }
원문의 회전 좌표 판단이 없음=, 이렇게 하면 창의 크기 변화가 없을 때 실제 사각형은 픽셀을 넘어서게 된다.

좋은 웹페이지 즐겨찾기