그래픽스
#include <windows.h>
#include <math.h>
#include <GL/glut.h>
#define PI 3.1415926
void display(void);
void init(void);
void main(int argc, char **argv) // void인데 어째서 선언이 들어가는지 이해안감
{
// 윈도우 창 정의
glutInit(&argc, argv); // 뭔지 모름
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // 뭔지 모름
glutInitWindowSize(600, 600);
glutInitWindowPosition(300, 300);
glutCreateWindow("This is for Homework");
// 디스플레이 펑션 실행
glutDisplayFunc(display);
init();
glutMainLoop(); // 계속 뿌려주는건가?
}
void display(void)
{
int i,j;
glClear (GL_COLOR_BUFFER_BIT); // 화면 초기화?
// n개의 점의 좌표 구하기
GLfloat theta = (2* PI) /16;
GLfloat x[16], y[16];
for(i=0; i<16; i++)
{
x[i]=1*cos(i*theta); // cos, sin 이 뭔지 모르겠음
y[i]=1*sin(i*theta);
}
glBegin(GL_LINES);
// 점 연결하기
for(i=0; i<15; i++)
{
for(j=i+1; j<16; j++)
{
glVertex2f(x[j],y[j]);
glVertex2f(x[i],y[i]);
}
}
glEnd();
glFlush();
}
void init(void)
{
//glViewport(500, 500, 300, 300); 뷰포인트에 대한 개념 모름
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION); // 뭔지 모름
glLoadIdentity(); // 뭔지 모름
gluOrtho2D(-1.5, 1.5, -1.5, 1.5); // 시점에 대해서 어렴풋이 알고있음
glMatrixMode(GL_MODELVIEW); // 시점 모드 어쩌고 밖에 모름
}
/////////////////// 단순 사각형 그리기
/*
#include <windows.h>
#include <GL/glut.h>
void display(void);
void myinit(void);
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(200, 200);
glutCreateWindow("simple OpenGL example");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glLineWidth (3);
glBegin(GL_POLYGON);
glVertex2f(0.25,0.25);
glVertex2f(0.75,0.25);
glVertex2f(0.75,0.75);
glVertex2f(0.25,0.75);
glEnd();
glFlush();
}
void myinit(void)
{
// 속성들
glClearColor(0.0, 0.0, 1.0, 0.0); // 흰 배경
glColor3f(1.0, 0.0, 0.0); // 적색으로 그림 // 관측의 설정
glViewport(1680, 1050, 1680, 1050);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 1.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
*/
///////////////////////// 가스켓
/*
void display(void);
void myinit(void);
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("simple OpenGL example");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
void myinit(void)
{
// 속성들
glClearColor(1.0, 1.0, 1.0, 1.0); // 흰 배경
glColor3f(1.0, 0.0, 0.0); // 적색으로 그림
// 관측의 설정
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 50.0, 0.0, 50.0);
glMatrixMode(GL_MODELVIEW);
}
void display(void)
{
GLfloat vertices[3][2] = {{0.0, 0.0}, {25.0, 50.0},
{50.0, 0.0}};
// 삼각형
int j, k;
int rand(); // 표준 난수 발생기
GLfloat p[2] = {7.5, 5.0}; // 임의의 점
glClear(GL_COLOR_BUFFER_BIT); // 윈도우를 지움
// 5000 개의 새로운 점을 계산하고 그림
glBegin(GL_POINTS);
for (k=0; k<50000; k++)
{
j = rand()%3; // 하나의 정점을 무작위로 선택
// 정점과 이전의 점의 중점을 계산
p[0] = (p[0]+vertices[j][0])/2.0;
p[1] = (p[1]+vertices[j][1])/2.0;
// 점을 그림
glVertex2fv(p);
}
glEnd();
glFlush();
}
*/
/*
//#include <windows.h>
//#include <stdio.h>
//#include <stdlib.h>
//#include <GL/gl.h>
//#include <GL/glu.h>
#include <GL/glut.h>
void triangle(GLfloat *a, GLfloat *b, GLfloat *c);
void tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d);
void divide_tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d, int m);
void display();
void myReshape(int w, int h);
GLfloat v[4][3]={{0.0, 0.0, 1.0}, {0.0, 0.942809, -0.33333}, {-0.816497, -0.471405, -0.333333}, {0.816497, -0.471405, -0.333333}};
GLfloat Colors[4][3] = {{0.0, 1.0, 0.0}, {1.0, 0.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, 0.0}};
int n;
void main(int argc, char **argv)
{
n=3;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(300, 200);
glutCreateWindow("3D Gasket");
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 1.0);
glutMainLoop();
}
void triangle(GLfloat *a, GLfloat *b, GLfloat *c)
{
glBegin(GL_TRIANGLES);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}
void tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d)
{
glColor3fv(Colors[0]);
triangle(a,b,c);
glColor3fv(Colors[1]);
triangle(a,c,d);
glColor3fv(Colors[2]);
triangle(a,d,b);
glColor3fv(Colors[3]);
triangle(b,d,c);
}
void divide_tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d, int m)
{
GLfloat mid[6][3];
int j;
if(m>0)
{
// 6개 중점의 중점 계산
for(j=0; j<3; j++) mid[0][j]=(a[j]+b[j])/2;
for(j=0; j<3; j++) mid[1][j]=(a[j]+c[j])/2;
for(j=0; j<3; j++) mid[2][j]=(a[j]+d[j])/2;
for(j=0; j<3; j++) mid[3][j]=(b[j]+c[j])/2;
for(j=0; j<3; j++) mid[4][j]=(c[j]+d[j])/2;
for(j=0; j<3; j++) mid[5][j]=(b[j]+d[j])/2;
// 분할하여 4개의 사면체 생성
divide_tetra(a, mid[0], mid[1], mid[2], m-1);
divide_tetra(mid[0], b, mid[3], mid[5], m-1);
divide_tetra(mid[1], mid[3], c, mid[4], m-1);
divide_tetra(mid[2], mid[4], d, mid[5], m-1);
}
else(tetra(a,b,c,d));
//순환이 끝나면 삼각형 그림
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
divide_tetra(v[0], v[1], v[2], v[3], n);
glEnd();
glFlush();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
else
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
#include <stdlib.h>
#include <GL/glut.h>
void display() {}
void mouse(int btn, int state, int x, int y);
void myinit(void);
void drawSquare(int x, int y);
void myReshape(GLsizei w, GLsizei h);
// 전역 변수
GLsizei wh = 600, ww = 600; // 초기 윈도우 크기
GLfloat size = 2.0; // 사각형 변 길이의 절반
GLfloat colors[3] = {255.0, 255.0, 255.0};
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(300,200);
glutCreateWindow("Mouse Event");
//myinit();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(drawSquare);
//glutPassiveMotionFunc(drawSquare);
glutReshapeFunc(myReshape);
glutMainLoop();
}
void mouse(int btn, int state, int x, int y)
{
//if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) drawSquare(x,y);
if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) //glClear (GL_COLOR_BUFFER_BIT);//exit(0);
{ for(int a=0; a<3; a++) colors[a]=((char)rand()%256);
}
}
void myinit(void)
{
glViewport(0, 0, ww, wh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)ww, 0.0, (GLdouble)wh);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear (GL_COLOR_BUFFER_BIT);
}
void drawSquare(int x, int y)
{
y=wh-y;
glColor3ub( colors[0], colors[1] , colors[2]);
glBegin(GL_POLYGON);
glVertex2f(x+size, y+size);
glVertex2f(x-size, y+size);
glVertex2f(x-size, y-size);
glVertex2f(x+size, y-size);
glEnd();
glFlush();
}
void myReshape(GLsizei w, GLsizei h)
{
// 클리핑 상자 조절
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// 뷰포트의 조절과 지우기
glViewport(0,0,w,h);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
// 전역변수로 저장된 새 윈도우 크기
ww=w;
wh=h;
}
#include <stdlib.h>
#include <GL/glut.h>
void display();
void mouse(int btn, int state, int x, int y);
void move(int x, int y);
void myReshape(GLsizei w, GLsizei h);
void move(int x, int y);
// 전역 변수
GLsizei wh = 600, ww = 600; // 초기 윈도우 크기
GLfloat colors[3] = {255.0, 255.0, 255.0};
GLint pointx, pointy, first=0,
pointxx, pointyy, pointxxx, pointyyy;
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(300,200);
glutCreateWindow("Rubber banding");
glEnable(GL_COLOR_LOGIC_OP);
//glDrawBuffer(GL_FRONT_AND_BACK); // 더블 버퍼 불필요?
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(move);
glutMainLoop();
}
void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
pointx=x; pointy=wh-y; // 초기 좌표 저장
glLogicOp(GL_XOR);
first=0;
}
if(btn==GLUT_LEFT_BUTTON && state==GLUT_UP)
{
glBegin(GL_LINES); // 2번째 좌표 선분 지우기
glVertex2f(pointxxx, pointyyy);
glVertex2f(pointx, pointy);
pointxx=x; pointyy=wh-y;
glLogicOp(GL_COPY);
glBegin(GL_LINES); // 최종 선분 그리기
glVertex2f(pointx, pointy);
glVertex2f(pointxx, pointyy);
}
if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
{
for(int a=0; a<3; a++) colors[a]=((char)rand()%256);
first=0;
}
}
void move(int x, int y)
{
y=wh-y; // y축에 대한 좌표 계산
glColor3ub( colors[0], colors[1] , colors[2]);
if(first==1){
glBegin(GL_LINES); // 2번째 좌표 선을 xor로 다시 그려줌으로서 지워줌
glVertex2f(pointxxx, pointyyy);
glVertex2f(pointx, pointy);
}
pointxxx=x, pointyyy=y; // 2번째 좌표 구함
glLogicOp(GL_XOR);
glBegin(GL_LINES);
glVertex2f(pointx, pointy); // 임시 선분 그리기
glVertex2f(x, y);
first=1;
glEnd();
glFlush();
}
void myReshape(GLsizei w, GLsizei h)
{
// 클리핑 상자 조절
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// 뷰포트의 조절과 지우기
glViewport(0,0,w,h);
glClear(GL_COLOR_BUFFER_BIT);
// 전역변수로 저장된 새 윈도우 크기
ww=w;
wh=h;
}
void display()
{
glLogicOp(GL_COPY);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(200,200);
glVertex2f(400,200);
glVertex2f(400,400);
glVertex2f(200,400);
glEnd();
glFlush();
}
#include <time.h>
#include <windows.h>
#include <GL/glut.h>
void display(void);
void myReshape(int w, int h);
void tick_mark(void);
void draw_plate(void);
void draw_arm(float thick, float angle, float length);
void getAngle(void);
void idle(void);
void remove(float length);
GLfloat tri[3][2] = {{ -0.1, 0.0}, {0.0, 0.02}, {0.0, -0.02}};
GLfloat angle=0, length[3]={1.0, 1.2, 1.4}, thick[3]={0.02, 0.009, 0.004};
GLfloat color[3][3] = {{1.0, 0.25, 0.25}, {0.25, 0.25, 1.0}, {0.8, 0.8, 1.0}};
GLint hour, minute, second, count;
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(200,300);
glutCreateWindow("OpenGL clock");
//glEnable(GL_COLOR_LOGIC_OP);
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMainLoop();
}
void display()
{
glutIdleFunc(idle);
draw_plate();
glFlush();
}
void draw_plate() // 시계 바탕 그리기
{
int i=0;
glColor3f(1.0, 1.0, 0.0);
while(i<60) {
glRotatef(i*6, 0.0, 0.0, 1.0); // 도형 회전
glTranslatef(1.55, 0, 0); // 거리 이동
if(i%5 == 0){
glScalef(2.0, 1.0, 1.0); // 크기 조절
tick_mark();
glScalef(0.5, 1.0, 1.0); // 다시 원점으로 복귀시키기
glTranslatef(-1.55, 0, 0);
glRotatef((i*6)*-1, 0.0, 0.0, 1.0);
}
else {
tick_mark();
glTranslatef(-1.55, 0, 0); // 다시 원점으로 복귀시키기
glRotatef((i*6)*-1, 0.0, 0.0, 1.0);
}
i++;
}
}
void tick_mark()
{
glBegin(GL_TRIANGLES);
glVertex2fv(tri[0]);
glVertex2fv(tri[1]);
glVertex2fv(tri[2]);
glEnd();
}
////////////////////////////////////////// 시분초 침 그리기
void idle()
{
count = second;
getAngle();
//if(count != second ){ // 무한루프로 초의 변화 체크
glClear(GL_COLOR_BUFFER_BIT);
draw_plate();
draw_arm(thick[2], second, length[2]);
// }
draw_arm(thick[1], minute, length[1]);
draw_arm(thick[0], hour, length[0]);
glFlush();
Sleep(1000); // 1초 단위로 화면 뿌려주기
// 대신 초가 정확하지 않으나 CPU 점유율이 낮음}
}
void draw_arm(float thick, float angle, float length)
{
glColor3f(thick*100, thick*10, thick*10);
glRotatef(angle, 0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(-(thick), length);
glVertex2f(thick, length);
glVertex2f(thick*2, -0.1*length);
glVertex2f(-(thick)*2, -0.1*length);
glEnd();
glRotatef(angle*-1, 0.0, 0.0, 1.0);
}
void getAngle()
{
long t=time(0);
int thour=(float)t/3600;
int tminute=(float)(t-3600*(int)thour)/60;
int tsecond=(float)(t-3600*(int)thour-60*(int)tminute);
hour = 360-(thour+9.0)*30.0;
minute = 360-tminute*6.0;
second = 360-tsecond*6.0;
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
if (w <= h)
gluOrtho2D (-2.0, 2.0, -2.0*(GLfloat)h/(GLfloat)w,
2.0*(GLfloat)h/(GLfloat)w);
else
gluOrtho2D (-2.0*(GLfloat)w/(GLfloat)h,
2.0*(GLfloat)w/(GLfloat)h, -2.0, 2.0);
}
/*
#include <GL/glut.h>
void display(void);
void myinit(void);
void stroke(void);
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("simple OpenGL example");
myinit();
glutDisplayFunc(display);
glutMainLoop();
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
//glLineWidth (3);
glBegin(GL_POLYGON);
glVertex2f(0.5,0.2);
glColor3f(1.0, 1.0, 0.0);
glVertex2f(0.2,0.50);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(0.5, 0.8);
glColor3f(0.0, 1.0, 1.0);
glVertex2f(0.8, 0.50);
glEnd();
stroke();
glFlush();
}
void myinit(void)
{
// 속성들
glClearColor(0.0, 0.0, 1.0, 0.0); // 흰 배경
glColor3f(1.0, 0.0, 0.0); // 적색으로 그림 // 관측의 설정
glViewport(1680, 1050, 1680, 1050);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 1.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void stroke()
{
glLineWidth(3);
glBegin(GL_LINE_STRIP);
glColor3f(0, 0, 0);
glVertex2f(0.2, 0.8);
glVertex2f(0.2, 0.2);
glVertex2f(0.8, 0.2);
glEnd();
}
*/
/*
//// 못쓰는거
#include <stdlib.h>
#include <GL/glut.h>
void display(void);
void myinit(void);
void stroke(void);
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("simple OpenGL example");
myinit();
glutDisplayFunc(display);
glutMainLoop();
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
//glLineWidth (3);
glBegin(GL_POLYGON);
glVertex2f(0.3, 0.2);
glVertex2f(0.5, 0.8);
glVertex2f(0.5, 0.35);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0.5, 0.8);
glVertex2f(0.5, 0.35);
glVertex2f(0.7, 0.2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0.2, 0.6);
glVertex2f(0.5, 0.35);
glVertex2f(0.8, 0.6);
glEnd();
stroke();
glFlush();
}
void myinit(void)
{
// 속성들
glClearColor(0.0, 0.0, 1.0, 0.0); // 흰 배경
glColor3f(1.0, 0.0, 0.0); // 적색으로 그림 // 관측의 설정
glViewport(1680, 1050, 1680, 1050);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 1.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void stroke()
{
glLineWidth(3);
glBegin(GL_LINE_STRIP);
glColor3f(0, 0, 0);
glVertex2f(0.1, 0.9);
glVertex2f(0.1, 0.1);
glVertex2f(0.9, 0.1);
glEnd();
}
*/
/*
#include <GL/glut.h>
void display(void);
void myinit(void);
void stroke(void);
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("simple OpenGL example");
myinit();
glutDisplayFunc(display);
glutMainLoop();
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
//glLineWidth (3);
glBegin(GL_POLYGON);
glVertex2f(0.4,0.27);
glColor3f(1.0, 1.0, 0.0);
glVertex2f(0.3,0.60);
glColor3f(1.0, 1.0, 0.0);
glVertex2f(0.6, 0.8);
// glColor3f(0.0, 1.0, 1.0);
//glVertex2f(0.8, 0.50);
glEnd();
stroke();
glFlush();
}
void myinit(void)
{
// 속성들
glClearColor(0.0, 0.0, 1.0, 0.0); // 흰 배경
glColor3f(1.0, 0.0, 0.0); // 적색으로 그림 // 관측의 설정
glViewport(1680, 1050, 1680, 1050);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 1.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void stroke()
{
glLineWidth(3);
glBegin(GL_LINE_STRIP);
glColor3f(0, 0, 0);
glVertex2f(0.2, 0.8);
glVertex2f(0.2, 0.2);
glVertex2f(0.7, 0.2);
glVertex2f(0.7, 0.8);
glEnd();
}
*/
#include <GL/glut.h>
void display(void);
void myinit(void);
void stroke(void);
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("simple OpenGL example");
myinit();
glutDisplayFunc(display);
glutMainLoop();
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
//glLineWidth (3);
/*
glBegin(GL_POLYGON);
glVertex2f(0.5,0.2);
glColor3f(1.0, 1.0, 0.0);
glVertex2f(0.2,0.50);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(0.5, 0.8);
glColor3f(0.0, 1.0, 1.0);
glVertex2f(0.8, 0.50);
glEnd();
*/
stroke();
glFlush();
}
void myinit(void)
{
// 속성들
glClearColor(0.0, 0.0, 1.0, 0.0); // 흰 배경
glColor3f(1.0, 0.0, 0.0); // 적색으로 그림 // 관측의 설정
glViewport(1680, 1050, 1680, 1050);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 1.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void stroke()
{
glLineWidth(3);
glBegin(GL_LINE_STRIP);
glColor3f(0, 0, 0);
glVertex2f(0.5, 0.8);
glVertex2f(0.35, 0.65);
glVertex2f(0.2, 0.65);
glVertex2f(0.35, 0.5);
glVertex2f(0.2, 0.3);
glVertex2f(0.4, 0.4);
glVertex2f(0.5, 0.2);
glVertex2f(0.65, 0.35);
glVertex2f(0.8, 0.35);
glVertex2f(0.65, 0.5);
glVertex2f(0.8, 0.65);
glVertex2f(0.65, 0.65);
glVertex2f(0.5, 0.8);
glEnd();
}
#include <stdlib.h>
#include <GL/glut.h>
GLfloat v[4][3]={{0.0, 0.0, 1.0}, {0.0, 0.942809, -0.33333},
{-0.816497, -0.471405, -0.333333}, {0.816497, -0.471405, -0.333333}};
GLfloat colors[4][3] = {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0},
{0.0, 0.0, 1.0}, {0.7, 0.3, 0.7}};
float eye[3], at[3], eye_start[3] = {0.0, 0.0, -0.391}, eye_end[3] = {0.0, 0.0, 15}; // 좌표값이 완벽하지 않음
float alpha=0.0, delta=0.003;
GLfloat theta=1;
int n;
void triangle(GLfloat *va, GLfloat *vb, GLfloat *vc)
{
glVertex3fv(va);
glVertex3fv(vb);
glVertex3fv(vc);
}
void tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d)
{
glColor3fv(colors[0]);
triangle(a, b, c);
glColor3fv(colors[1]);
triangle(a, c, d);
glColor3fv(colors[2]);
triangle(a, d, b);
glColor3fv(colors[3]);
triangle(b, d, c);
}
void divide_tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d, int m)
{
GLfloat mid[6][3];
int j;
if (m>0)
{
for (j=0; j<3; j++) mid[0][j]=(a[j]+b[j])/2;
for (j=0; j<3; j++) mid[1][j]=(a[j]+c[j])/2;
for (j=0; j<3; j++) mid[2][j]=(a[j]+d[j])/2;
for (j=0; j<3; j++) mid[3][j]=(b[j]+c[j])/2;
for (j=0; j<3; j++) mid[4][j]=(c[j]+d[j])/2;
for (j=0; j<3; j++) mid[5][j]=(b[j]+d[j])/2;
divide_tetra(a, mid[0], mid[1], mid[2], m-1);
divide_tetra(mid[0], b, mid[3], mid[5], m-1);
divide_tetra(mid[1], mid[3], c, mid[4], m-1);
divide_tetra(mid[2], mid[4], d, mid[5], m-1);
}
else (tetra(a,b,c,d));
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gluLookAt( eye[0], eye[1], eye[2],
0.0, 0.0, -100.0,
0.0, 1.0, 0.0);
glRotatef(theta, 1.0, 1.0, 1.0);
glBegin(GL_TRIANGLES);
divide_tetra(v[0], v[1], v[2], v[3], n);
glEnd();
glRotatef(theta, -1.0, -1.0, -1.0); // 카메라 위치 위해 원래각도로 복귀
glFlush();
glutSwapBuffers();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w/h, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
}
void spin(int value)
{
theta += 5;
if( theta > 360.0 ) theta -= 360.0;
glutPostRedisplay();
glutTimerFunc(30, spin, 1);
}
void approach(int value)
{
int i;
alpha=alpha+delta;
for(i=0;i<3;i++) {
eye[i]=(1.0-alpha) * eye_start[i] + alpha * (eye_end[i]);
if(alpha > 0.05 || alpha < 0.001) delta = delta *-1; // 카메라 줌인, 줌아웃 결정
}
glutTimerFunc(30, approach, 1);
}
void main( int argc, char **argv)
{
n=3;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow( "3D Gasket" );
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutTimerFunc(30, spin, 1);
glutTimerFunc(30, approach, 1);
glEnable(GL_DEPTH_TEST);
glClearColor (1.0, 1.0, 1.0, 1.0);
glutMainLoop();
}
#include <windows.h>
#include <math.h>
#include <GL/glut.h>
///////// sqrt 루트 구하는 함수
///////// pow 제곱 구하는 함수
int start_time = timeGetTime();
float l=1, g=10;
int T = 360 * sqrt(l/g);
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glClearColor(0.0, 0.0, 0.0, 1.0);
if (w <= h)
gluOrtho2D (-2.0, 2.0, -2.0*(GLfloat)h/(GLfloat)w,
2.0*(GLfloat)h/(GLfloat)w);
else
gluOrtho2D (-2.0*(GLfloat)w/(GLfloat)h,
2.0*(GLfloat)w/(GLfloat)h, -2.0, 2.0);
}
void draw_object()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2f(0.0, 0.0);
glVertex2f(0.0, -2);
glEnd();
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(-0.3, -2);
glVertex2f(0.3, -2);
glVertex2f(0.3, -2.6);
glVertex2f(-0.3, -2.6);
glEnd();
glFlush();
}
void action(int value)
{
float t = (timeGetTime() - start_time % T );
float theta = 55 * sin( sqrt((g/l)*t) );
glColor3f(1.0, 0.0, 0.0);
glPushMatrix();
glTranslatef(0.0, 1.8, 0.0);
glRotatef(theta, 0.0, 0.0, 1.0);
draw_object();
glPopMatrix();
glutTimerFunc(30, action, 1);
}
void display()
{
action(1);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(10, 10);
glutCreateWindow(" 진 자 운 동 ");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMainLoop();
}