Programming/openGL2011. 3. 29. 23:22
여전히 glPushMatrix / glPopMatrix의 마법은 이해를 하지 못했지만
이녀석이 없으면 이상한 좌표축에서 이상하게 작동하는 것으로 보인다 -_-

아무튼 glPushMatrix 이후에는 추가된 개체를 중심으로
glRotate / glTranslatef 가 수행이 되며 push/pop이라는 용어가 보여주듯
스택에 차곡차곡 쌓이며 추가된 이후의 객체에 대해 적용이 된다.

스택에 들어가는 것과 공간의 변화를 한번 정리해보자면 다음과 같은데

어떻게 보면 Rotate와 Translate는 좌표계 자체를 변경시키며
왜곡된(?) 공간에 개체를 놓으면서 회전을 시키는 식으로 구성이 된 것으로 보인다.

#include "windows.h"
#include "GL/gl.h"
#include "GL/glu.h"
#include "GL/glut.h"

static int year = 0, day = 0;

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0, 1.0, 1.0);

	glPushMatrix();
		glutWireSphere(1.0, 20, 16);   /* draw sun */

		glRotatef((GLfloat) year, 0.0, 1.0, 0.0);
		glTranslatef(2.0, 0.0, 0.0);
		glRotatef((GLfloat) day, 0.0, 1.0, 0.0);
		glutWireSphere(0.2, 10, 8);    /* draw smaller planet */
	glPopMatrix();

	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); //GL_PROJECTION
		glLoadIdentity();
		gluLookAt(0.0, 0.0, 5.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 'd':
		  day = (day + 10) % 360;
		  glutPostRedisplay();
		  break;
	  case 'D':
		  day = (day - 10) % 360;
		  glutPostRedisplay();
		  break;
	  case 'y':
		  year = (year + 5) % 360;
		  glutPostRedisplay();
		  break;
	  case 'Y':
		  year = (year - 5) % 360;
		  glutPostRedisplay();
		  break;
	  default:
		  break;
	}
}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(500, 500); 
	glutInitWindowPosition(100, 100);
	glutCreateWindow(argv[0]);

	glClearColor(0.0, 0.0, 0.0, 0.0);
	glShadeModel(GL_FLAT); //GL_SMOOTH

	glutDisplayFunc(display); 
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutMainLoop();
	return 0;
}

void glMatrixMode(GLenum mode);
    GL_MODELVIEW  Applies subsequent matrix operations to the modelview matrix stack.
    GL_PROJECTION Applies subsequent matrix operations to the projection matrix stack.
    GL_TEXTURE    Applies subsequent matrix operations to the texture matrix stack.
    GL_COLOR      Applies subsequent matrix operations to the color matrix stack.

void glShadeModel(GLenum mode);
    GL_FLAT     Smooth shading causes the computed colors of vertices to be interpolated as the primitive is rasterized, typically assigning different colors to each resulting pixel fragment.
    GL_SMOOTH   Flat shading selects the computed color of just one vertex and assigns it to all the pixel fragments generated by rasterizing a single primitive. 

void glPushMatrix(void);
void glPopMatrix(void);

void glTranslated(GLdouble x, GLdouble y, GLdouble z);
void glTranslatef(GLfloat x,  GLfloat y,  GLfloat z);

void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks);
void glutWireSphere(GLdouble radius, GLint slices, GLint stacks);

void glRotated(GLdouble angle, GLdouble x, GLdouble y, GLdouble z);
void glRotatef(GLfloat angle,  GLfloat x,  GLfloat y,  GLfloat z);

void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);

void gluLookAt(GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ,
                        GLdouble centerX, GLdouble centerY, GLdouble centerZ,
                        GLdouble upX, GLdouble upY, GLdouble upZ);

[링크 : http://www.opengl.org/sdk/docs/man/xhtml/glTranslate.xml]             glTranslatef
[링크 : http://www.opengl.org/sdk/docs/man/xhtml/glRotate.xml]                 glRotate
[링크 : http://www.opengl.org/sdk/docs/man/xhtml/glShadeModel.xml]        glShadeModel
[링크 : http://www.opengl.org/sdk/docs/man/xhtml/glPushMatrix.xml]          glPushMatrix / glPopMatrix
[링크 : http://www.opengl.org/sdk/docs/man/xhtml/glMatrixMode.xml]         glMatrixMode

[링크 : http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml]        gluPerspective
[링크 : http://www.opengl.org/sdk/docs/man/xhtml/gluLookAt.xml]               gluLookAt

[링크 : http://www.opengl.org/documentation/specs/glut/spec3/node81.html] glutWireSphere 

'Programming > openGL' 카테고리의 다른 글

glViewport  (0) 2011.04.05
glFrustum - 절두체  (6) 2011.03.30
openGL callback function - GLUT 키보드 / 마우스 입력  (0) 2011.03.28
freeglut  (0) 2011.03.26
openGL - glbegin()  (2) 2011.03.25
Posted by 구차니