[CG] 태양계 모델링

2021. 1. 5. 21:16Hi/Computer_Graphics

 

OpenGL로 만든 태양계 모델링입니다.

아래는 전체 코드입니다.

#include <GL/glut.h>
//#include <GL/gl.h>
//#include <GL/glu.h>
static int Day = 0, Time = 0;

void MyDisplay() {
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
        glColor3f(1.0, 0.3, 0.3);
        glutWireSphere(0.2, 20, 16);//태양그리기
        glPushMatrix();//태양기준 좌표계 스택에 푸시하여 저장
            glRotatef((GLfloat)Day, 0.0, 1.0, 0.0);//회전
            glTranslatef(0.7, 0.0, 0.0);//이동
            glRotatef((GLfloat)Time, 0.0, 1.0, 0.0);//time만큼 회전(지구자전)
            glColor3f(0.5, 0.6, 0.7);
            glutWireSphere(0.1, 10, 8);//지구그리기
            glPushMatrix();//지구 좌표계 푸시
             glRotatef((GLfloat)Time, 0.0, 1.0, 0.0);//지구회전
             glTranslatef(0.2, 0.0, 0.0);//달좌표찍기
             glColor3f(0.9, 0.8, 0.2);
             glutWireSphere(0.04, 10, 8);//달그리기
             glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}

void MyTimer(int Value) {
   Day = (Day + 10) % 360;
   Time = (Time + 5) % 360;
    glutPostRedisplay();//디스플레이 콜백요구
    glutTimerFunc(35, MyTimer, 1); //동일시간반복
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("태양계+타이머");
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    glutDisplayFunc(MyDisplay);
    glutTimerFunc(25, MyTimer, 1);//키보드함수에서 타이머함수로 변경
    glutMainLoop();
    return 0;
}

glut.h, glut32.lib, glut32.dll 설치해서 import 해줘야한다.

 

728x90

'Hi > Computer_Graphics' 카테고리의 다른 글

[CG]시점변환  (0) 2021.01.05
Computer Graphics  (0) 2020.12.07