Develop
2003.04.23 11:00
[c] OpenGL 임시로 여기 올림..
조회 수 10351 댓글 0
#include<stdlib.h>
#include<GL/glut.h>
#include<math.h>
typedef float point3[4];
point3 v[] = {
{0.0, 0.0, 1.0}, {0.0, 0.942809, -0.33333},
{-0.816497, -0.471405, -0.33333}, {0.816497, -0.471405, -0.333333}
};
static GLfloat theta[] = {0.0, 0.0, 0.0};
int n;
int mode;
void triangle(point3 a, point3 b, point3 c){
if(mode==0) glBegin(GL_LINE_LOOP);
else glBegin(GL_POLYGON);
if (mode==1) glNormal3fv(a);
if (mode==2) glNormal3fv(a);
glVertex3fv(a);
if (mode==2) glNormal3fv(b);
glVertex3fv(b);
if (mode==2) glNormal3fv(c);
glVertex3fv(c);
glEnd();
}
void normal(point3 p){
//double sqrt();
float d = 0.0;
int i;
for(i=0; i<3; i++) d += p[i] * p[i];
d = sqrt(d);
if(d>0.0) for(i=0; i<3; i++) p[i] /= d;
}
void divside_triangle(point3 a, point3 b, point3 c, int m){
point3 v1, v2, v3;
int j;
if(m>0){
for(j=0; j<3; j++) v1[j] = a[j] + b[j];
normal(v1);
for(j=0; j<3; j++) v2[j] = a[j] + c[j];
normal(v2);
for(j=0; j<3; j++) v3[j] = b[j] + c[j];
normal(v3);
divside_triangle(a, v1, v2, m-1);
divside_triangle(c, v2, v3, m-1);
divside_triangle(b, v3, v1, m-1);
divside_triangle(v1, v3, v2, m-1);
}
else triangle(a, b, c);
}
void tetrahedron(int m){
divside_triangle(v[0], v[1], v[2], m);
divside_triangle(v[3], v[2], v[1], m);
divside_triangle(v[0], v[3], v[1], m);
divside_triangle(v[0], v[2], v[3], m);
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
mode = 0;
tetrahedron(n);
mode = 1;
glTranslatef(-2.0, 0.0, 0.0);
tetrahedron(n);
mode = 2;
glTranslatef(4.0, 0.0, 0.0);
tetrahedron(n);
glFlush();
}
void myReshape(int w, int h){
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h) glOrtho(-4.0, 4.0, -4.0 * (GLfloat)h / (GLfloat)w,
4.0 * (GLfloat)h / (GLfloat)w, -10.0, 10.0);
else glOrtho(-4.0 * (GLfloat)w / (GLfloat)h,
4.0 * (GLfloat)w / (GLfloat)h, -4.0, 4.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
display();
}
void myinit(){
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_diffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_ambient[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_shininess = {100.0};
GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0};
GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0, 0.0, 0.0);
}
void main(int argc, char **argv){
//n = atoi(argv[1]);
n = 5;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("sphere");
myinit();
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMainLoop();
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
189 | Develop |
[c++] 자료구조(링크리스트,스택,큐)와 후위 표기 계산기 샘플 ㅋㅋ
4 ![]() |
hooni | 2013.04.23 | 12365 |
188 | Develop | [c++] 인라인 함수에 대한 설명 | hooni | 2013.04.23 | 7138 |
187 | Develop |
[c++] 인라인 함수 설명과 예제..
![]() |
hooni | 2013.04.23 | 6677 |
186 | Develop |
[c++] 윈도우 프로세스뷰어 분석.. ㅋㅋ
![]() |
hooni | 2013.04.23 | 8877 |
185 | Develop |
[c++] 윈도우 API 정복 예제
![]() |
hooni | 2013.04.23 | 7635 |
184 | Develop |
[c++] 웹폼(webform)전송과 http 파일 업로드 샘플
![]() |
hooni | 2013.04.23 | 33123 |
183 | Develop |
[c++] 압축프로그램(Lite Zip) 샘플
![]() |
hooni | 2013.04.23 | 8637 |
182 | Develop | [c++] 쓰레드(Thread) 객체의 사용 | hooni | 2013.04.23 | 8616 |
181 | Develop | [c++] 소켓 프로그래밍 관련 링크.. (퍼올려고 올린거) | hooni | 2013.04.23 | 7066 |
180 | Develop |
[c++] 바탕화면 테두리에 자석처럼 붙는 스냅효과..
![]() |
hooni | 2013.04.23 | 8938 |
179 | Develop | [c++] 문자열 뒤집기(문자열 거꾸로 출력) | hooni | 2013.04.23 | 17327 |
178 | Develop |
[c++] 마방진 소스코드
![]() |
hooni | 2013.04.23 | 9572 |