[C :: OpenGL] cosin^n (Θ)의 3D 그래프

심플디 2013.08.05 21:07 조회 수 : 3099

003.png

#include <opengl.h>
#include <math.h>
#define GL_PI 3.141592

static GLfloat xRot = 0;
static GLfloat yRot = 0;
static GLfloat length001=250;
static GLfloat length002=250;

void RenderScene(void){

 GLfloat x,y,z;
 GLfloat angle001,angle002,n;
 GLfloat r,g,b;
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glPushMatrix();
 glRotatef(xRot, 1, 0, 0);
 glRotatef(yRot, 0, 1, 0);
 glBegin(GL_POINTS);
 glEnable(GL_CULL_FACE);
 glEnable(GL_DEPTH_TEST);
    r=0;
    g=0.5;
    b=1.0;
 for(n=1; n<6; n++){
     r+=0.5;
     g+=0.5;
     b+=0.5;
     if(r>1.4) r=0;
     if(g>1.4) g=0;
     if(b>1.4) b=0;
     glColor4f(r, g, b,0.5);
        for(angle001=0; angle001<=(2*GL_PI); angle001+=0.05){
            y=length001*pow(cos(angle001),n);
            x=y*tan(angle001);
            length002=x;
            for(angle002=0; angle002<=(2*GL_PI); angle002+=0.05){
                z=length002*cos(angle002);
                x=z*tan(angle002);
                if(y<0) y*=-1;
                glVertex3f(x,y,z);
                }
            }
        }
    glEnd();
    glBegin(GL_LINES);
    glColor3f(1, 1, 0);    glVertex3f(-250,0,0);    glVertex3f(250,0,0); // x
    glColor3f(0, 1, 0);    glVertex3f(0,-250,0);    glVertex3f(0,250,0); // y
    glColor3f(0, 1, 1);    glVertex3f(0,0,-250);    glVertex3f(0,0,250); // z
    glEnd();
 glPopMatrix();
 glutSwapBuffers();
 }

void SetupRC(){
 glClearColor(0, 0, 0, 1);
 glFrontFace(GL_CW);
 glShadeModel(GL_SMOOTH);

 }

void SpecialKeys(int key, int x, int y){
    GLfloat pointer=10.0;
 if(key == GLUT_KEY_UP)          xRot -= pointer;
 if(key == GLUT_KEY_DOWN)        xRot += pointer;
 if(key == GLUT_KEY_LEFT)        yRot -= pointer;
 if(key == GLUT_KEY_RIGHT)       yRot += pointer;
 if(key == GLUT_KEY_HOME)        xRot -= 45.0;
 if(key == GLUT_KEY_END)         xRot += 45.0;
 if(key == GLUT_KEY_PAGE_UP)     yRot -= 45.0;
 if(key == GLUT_KEY_PAGE_DOWN)   yRot += 45.0;
 if(key == GLUT_KEY_INSERT)      {xRot=0; yRot=0;}
 if(key > 356)                   {xRot = 0; yRot = 0;}
 if(key < -1)                    {xRot = 355; yRot = 355;}
 glutPostRedisplay();
 }

void ChangeSize(int w, int h) {
 GLfloat nRange = 300;
 if(h == 0) h = 1;
    glViewport(0, 0, w, h);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
    if (w <= h) glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
    else glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 }

int main(void){
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
 glutInitWindowSize(1250,950);
 glutInitWindowPosition(0,0);
 glutCreateWindow("Shin Dong-Seok; Lab.for manufacturing system; 000-0000-0000");
 glutReshapeFunc(ChangeSize);
 glutSpecialFunc(SpecialKeys);
 glutDisplayFunc(RenderScene);
 SetupRC();
 glutMainLoop();

 return 0;
 }