OpenGL - 16개의 움직이는 랜덤 티팟
설명 : 16개의 teapot이 랜던한 색과 방향으로 회전한다.
이번에는 빛을 줘서 그림자 효과도 준다.
생략된 부분은 링크된 부분을 참고하세요(소스동일)
2017/09/12 - [SW/OpenGL] - OpenGL - 사각형 회전, 색바꾸기
선언부
GLUquadricObj *p; void getaxis(int index); double getcolor(); int axi[16]; double colR[16]; double colG[16]; double colB[16]; static float theta[3]; GLdouble angle;
렌더링 함수
void RenderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0, 2.0, 2.0, 0, 0, 0, 0, 1.0, 0); //view point glEnable(GL_LIGHTING); //light on glEnable(GL_LIGHT0); //0th light GLfloat position0[4] = { 100, 100, 100, 1 }; GLfloat ambient0[4] = { 0, 0, 0.6,0 }; GLfloat diffuse0[4] = { 1.0, 1.0, 0, 1 }; GLfloat specular0[4] = { 1, 1, 1, 1 }; glLightfv(GL_LIGHT0, GL_POSITION, position0); glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0); glLightfv(GL_LIGHT0, GL_SPECULAR, specular0); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);//light attributes is ambient, diffuse glEnable(GL_COLOR_MATERIAL); //set teapots attributes GLfloat mat_specular[4] = { 1,1,1,1 }; GLfloat mat_shininess = 25.0f; glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess); double xp = -1.6; double yp = 2.4; int a = 0; int b = 0; for (int i = 0; i < 4; i++) { xp = -1.6; // teapot move 1.6 with x-axis for (int k = 0; k < 4; k++) { glPushMatrix();//push glTranslatef(xp, yp, 0.0); if (axi[a] == 0) glRotatef(angle, 1, 0, 0); else if (axi[a] == 1) glRotatef(angle, 0, 1, 0); else glRotatef(angle, 0, 0, 1); glColor3f(colR[b], colG[b], colB[b]); glutSolidTeapot(0.2); glPopMatrix();//pop a++; b++; xp = xp + 0.8; } yp = yp - 0.8; } glutSwapBuffers(); }
랜덤한 방향으로 회전하도록 x,y,z 방향을 제시해주는 함수
/// <summary> /// set rotate direction random /// </summary> /// <param name="index">The index.</param> void getaxis(int index) { if (index == 0) glRotatef(angle, 1, 0, 0); else if (index == 1) glRotatef(angle, 0, 1, 0); else glRotatef(angle, 0, 0, 1); }
주전자가 2도씩 회전함
/// <summary> /// rotate teapot 2 degree /// </summary> /// <param name="value">The value.</param> void rtimer(int value) { angle += 2; glutTimerFunc(30, rtimer, 0); glutPostRedisplay(); }
티팟에 줄 랜덤한 색을 제시
/// <summary> /// Get random colors this instance. /// </summary> /// <returns></returns> double getcolor(void) { double c; c = (double)rand() / RAND_MAX; return c; }
색과 방향 초기화
void init(void) { srand(time(NULL)); theta[0] = 0; theta[1] = 0; theta[2] = 0; for (int t = 0; t < 16; t++) { axi[t] = rand() % 3; colR[t] = (double)rand() / RAND_MAX; colG[t] = (double)rand() / RAND_MAX; colB[t] = (double)rand() / RAND_MAX; } }
메인함수
void main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(800, 800); glutCreateWindow("Ex4"); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); glutTimerFunc(30, rtimer, 1); init(); SetupRc(); glutMainLoop(); }
결과
'SW > OpenGL' 카테고리의 다른 글
OpenGL - Perspective vs Orthographic (투시투영 vs 직교투영) (0) | 2017.09.13 |
---|---|
OpenGL - 쉐이딩(Shading, line, flat smooth) (0) | 2017.09.13 |
OpenGL - 로봇 팔 만들기 (0) | 2017.09.12 |
OpenGL - 정육면체 xyz축 회전 (0) | 2017.09.12 |
OpenGL - 3점을 찍어 삼각형 그리기 (0) | 2017.09.12 |