计算机图形学——OpenGL二维旋转,缩放,平移(课堂备份)

// ====== Computer Graphics Experiment #5 ======
// |       Two-Dimensional Transformation      |
// =============================================
//
// Requirement:
// (1) Implement functions to generate 2D transformation matrix.
// (2) Implement function to transform 2D point using 
//     transformation matrix.
// (3) Implement functions to rotate, scale and translate objects 
//     using keyboard.

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

#define PI 3.1415926535
// 2D point class
class CPoint2D
{
public:
    float x, y;
};

// 2D transformation matrix
float My2DMatrix[3][3];

// Generate translation matrix
void TranslationMatrix(float tx, float ty, float M[3][3])
// tx, ty --- Translation vector
{
        M[0][0]=1.0;M[0][1]=0.0;M[0][2]=tx;
        M[1][0]=0.0;M[1][1]=1.0;M[1][2]=ty;
        M[2][0]=0.0;M[2][1]=0.0;M[2][2]=1.0;
}

// Generate rotation matrix
void RotationMatrix(float theta, float M[3][3])
// theta --- Rotation angle in degree
{
        M[0][0]=cos(theta);M[0][1]=-sin(theta);M[0][2]=0.0;
        M[1][0]=sin(theta);M[1][1]=cos(theta);M[1][2]=0.0;
        M[2][0]=0.0;M[2][1]=0.0;M[2][2]=1.0;
}

// Generate scaling matrix
void ScalingMatrix(float sx, float sy, float M[3][3])
// sx, sy --- Scaling factors
{
    M[0][0]=sx;M[0][1]=0.0;M[0][2]=0.0;
        M[1][0]=0.0;M[1][1]=sy;M[1][2]=0.0;
        M[2][0]=0.0;M[2][1]=0.0;M[2][2]=1.0;
}


// Translate
void Translate(float tx, float ty)
// tx, ty --- Translation vector
{
    float M[3][3],temp[3][3];
    TranslationMatrix(tx, ty, M);
    temp[0][0]=M[0][0]*My2DMatrix[0][0]+M[0][1]*My2DMatrix[1][0]+M[0][2]*My2DMatrix[2][0];
    temp[0][1]=M[0][0]*My2DMatrix[0][1]+M[0][1]*My2DMatrix[1][1]+M[0][2]*My2DMatrix[2][1];
    temp[0][2]=M[0][0]*My2DMatrix[0][2]+M[0][1]*My2DMatrix[1][2]+M[0][2]*My2DMatrix[2][2];
    temp[1][0]=M[1][0]*My2DMatrix[0][0]+M[1][1]*My2DMatrix[1][0]+M[1][2]*My2DMatrix[2][0];
    temp[1][1]=M[1][0]*My2DMatrix[0][1]+M[1][1]*My2DMatrix[1][1]+M[1][2]*My2DMatrix[2][1];
    temp[1][2]=M[1][0]*My2DMatrix[0][2]+M[1][1]*My2DMatrix[1][2]+M[1][2]*My2DMatrix[2][2];
    temp[2][0]=M[2][0]*My2DMatrix[0][0]+M[2][1]*My2DMatrix[1][0]+M[2][2]*My2DMatrix[2][0];
    temp[2][1]=M[2][0]*My2DMatrix[0][1]+M[2][1]*My2DMatrix[1][1]+M[2][2]*My2DMatrix[2][1];
    temp[2][2]=M[2][0]*My2DMatrix[0][2]+M[2][1]*My2DMatrix[1][2]+M[2][2]*My2DMatrix[2][2];
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
            My2DMatrix[i][j]=temp[i][j];

}

// Rotate
void Rotate(float theta)
// theta --- Rotation angle in degree
{
    float M[3][3],temp[3][3];
    RotationMatrix(theta,M);
    temp[0][0]=M[0][0]*My2DMatrix[0][0]+M[0][1]*My2DMatrix[1][0]+M[0][2]*My2DMatrix[2][0];
    temp[0][1]=M[0][0]*My2DMatrix[0][1]+M[0][1]*My2DMatrix[1][1]+M[0][2]*My2DMatrix[2][1];
    temp[0][2]=M[0][0]*My2DMatrix[0][2]+M[0][1]*My2DMatrix[1][2]+M[0][2]*My2DMatrix[2][2];
    temp[1][0]=M[1][0]*My2DMatrix[0][0]+M[1][1]*My2DMatrix[1][0]+M[1][2]*My2DMatrix[2][0];
    temp[1][1]=M[1][0]*My2DMatrix[0][1]+M[1][1]*My2DMatrix[1][1]+M[1][2]*My2DMatrix[2][1];
    temp[1][2]=M[1][0]*My2DMatrix[0][2]+M[1][1]*My2DMatrix[1][2]+M[1][2]*My2DMatrix[2][2];
    temp[2][0]=M[2][0]*My2DMatrix[0][0]+M[2][1]*My2DMatrix[1][0]+M[2][2]*My2DMatrix[2][0];
    temp[2][1]=M[2][0]*My2DMatrix[0][1]+M[2][1]*My2DMatrix[1][1]+M[2][2]*My2DMatrix[2][1];
    temp[2][2]=M[2][0]*My2DMatrix[0][2]+M[2][1]*My2DMatrix[1][2]+M[2][2]*My2DMatrix[2][2];
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
            My2DMatrix[i][j]=temp[i][j];
}

// Scale
void Scale(float sx, float sy)
// sx, sy --- Scaling factors
{
    float M[3][3];
    ScalingMatrix(sx, sy, M);
    My2DMatrix[0][0]=M[0][0]*My2DMatrix[0][0]+M[0][1]*My2DMatrix[1][0]+M[0][2]*My2DMatrix[2][0];
    My2DMatrix[0][1]=M[0][0]*My2DMatrix[0][1]+M[0][1]*My2DMatrix[1][1]+M[0][2]*My2DMatrix[2][1];
    My2DMatrix[0][2]=M[0][0]*My2DMatrix[0][2]+M[0][1]*My2DMatrix[1][2]+M[0][2]*My2DMatrix[2][2];
    My2DMatrix[1][0]=M[1][0]*My2DMatrix[0][0]+M[1][1]*My2DMatrix[1][0]+M[1][2]*My2DMatrix[2][0];
    My2DMatrix[1][1]=M[1][0]*My2DMatrix[0][1]+M[1][1]*My2DMatrix[1][1]+M[1][2]*My2DMatrix[2][1];
    My2DMatrix[1][2]=M[1][0]*My2DMatrix[0][2]+M[1][1]*My2DMatrix[1][2]+M[1][2]*My2DMatrix[2][2];
    My2DMatrix[2][0]=M[2][0]*My2DMatrix[0][0]+M[2][1]*My2DMatrix[1][0]+M[2][2]*My2DMatrix[2][0];
    My2DMatrix[2][1]=M[2][0]*My2DMatrix[0][1]+M[2][1]*My2DMatrix[1][1]+M[2][2]*My2DMatrix[2][1];
    My2DMatrix[2][2]=M[2][0]*My2DMatrix[0][2]+M[2][1]*My2DMatrix[1][2]+M[2][2]*My2DMatrix[2][2];
}

// Transform 2D point
void Transform2DPoint(CPoint2D *p, float M[3][3], CPoint2D *q)
// p --- Input point
// M --- Transformation matrix
// q --- Output point
{
    q->x=M[0][0]*(p->x)+M[0][1]*(p->y)+M[0][2];
    q->y=M[1][0]*(p->x)+M[1][1]*(p->y)+M[1][2];
}

void IdentityMatrix(float M[3][3])
{
    M[0][0]=1.0;M[0][1]=0.0;M[0][2]=0.0;
        M[1][0]=0.0;M[1][1]=1.0;M[1][2]=0.0;
        M[2][0]=0.0;M[2][1]=0.0;M[2][2]=1.0;
}

// Initialization function
void init(void)
{
    glClearColor (0.0, 0.0, 0.5, 0.0);
    IdentityMatrix(My2DMatrix);
}

// Display callback function
void display(void)
{
    static CPoint2D MyObject[]=
    {{0.0, 63.0}, 
    {-60.0, 20.0}, {-60.0, -20.0}, 
    {60.0, -20.0}, {60.0, 20.0}};
    CPoint2D pp;
    int i;

    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 0.0);

    glBegin(GL_POLYGON);
    for (i=0; i<5; ++i)
    {
        Transform2DPoint(MyObject+i, My2DMatrix, &pp);
        glVertex2f(pp.x, pp.y);
    }
    glEnd();

    glutSwapBuffers();
}

// Reshape callback function
void reshape(int w, int h)
{
    glViewport (0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-100, 100, -100, 100);
}

// Keyboard callback function
void keyboard(unsigned char key, int x, int y)
{
    switch (key) {
        case 27: exit(0);
    }
}

// Special keyboard callback function
void special_key(int key, int x, int y)
{
    switch (key) {
        case GLUT_KEY_LEFT:
            Translate(-5.0, 0.0);glutPostRedisplay();
            break;
        case GLUT_KEY_RIGHT:
            Translate(5.0, 0.0);glutPostRedisplay();
            break;
        case GLUT_KEY_DOWN:
            Translate(0.0, -5.0);glutPostRedisplay();
            break;
        case GLUT_KEY_UP:
            Translate(0.0, 5.0);glutPostRedisplay();
            break;
    // Add your code for rotation and scaling 
        case GLUT_KEY_PAGE_UP:
            Rotate(PI/6);glutPostRedisplay();
            break;

        case GLUT_KEY_PAGE_DOWN:
            Rotate(-PI/6);glutPostRedisplay();
            break;

        case GLUT_KEY_HOME:
            Scale(0.5,0.5);glutPostRedisplay();
            break;

        case GLUT_KEY_END:
            Scale(2,2);glutPostRedisplay();
            break;

        case GLUT_KEY_INSERT:
            Rotate(PI/2);glutPostRedisplay();
            break;

    }
}

// Main program entrance
int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("Test 2D Transformation");
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutSpecialFunc(special_key);
    glutMainLoop();
    return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值