2008. 3. 24. 17:01

[과제소스] square.c

#include <windows.h>
#include <gl/glut.h>
#include <math.h>

#define PI  3.14159

GLsizei wh = 500, ww = 500; /* initial window size */ 
GLfloat steps = 0.0f, dt = PI/30.0f;
GLfloat size = 100.0;   /* half side length of square */

GLfloat dx = 0, dy = 0;

void drawSquare(int x, int y) 
{

   y=wh-y;
   glClear(GL_COLOR_BUFFER_BIT);

   dx = size*(cos(steps) - sin(steps));
   dy = size*(sin(steps) + cos(steps));
   glBegin(GL_POLYGON);
      glVertex2f(x+dx, y+dy);
      glVertex2f(x-dy, y+dx);
      glVertex2f(x-dx, y-dy);
      glVertex2f(x+dy, y-dx);
   glEnd();
   steps += dt;
   glFlush();
}

void myReshape(GLsizei w, GLsizei h) 
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glViewport(0,0,w,h);
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();

    ww = w;    wh = h;
}



void myinit() 
{
    glClearColor (0.0, 0.0, 0.0, 1.0);
    glViewport(0, 0, ww, wh);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, (GLdouble) ww , 0.0, (GLdouble) wh , -1.0, 1.0);

    glClearColor (0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();
}

void mouse(int btn, int state, int x, int y) 
{
    if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)   exit(0);
}

void display() 
{}

int main(int argc, char** argv) 
{
    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
	
    glutInitWindowSize(900, 900);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("square");
    
    myinit ();
    
    glutReshapeFunc (myReshape);
    glutMouseFunc (mouse);
    glutMotionFunc(drawSquare);
    glutDisplayFunc(display);
    
    glutMainLoop();

}
Trackback 0 Comment 0