改變gluOrtho2D()函數(shù)的參數(shù),實(shí)現(xiàn)圖形的伸縮。

注:源代碼參照《computer graphics with opengl》英文版第四版272頁。
1、關(guān)鍵是如何控制gluOrtho2D()的參數(shù)

gluOrtho2D(-size, size, -size, size);

2、改變參數(shù)

GLint flag = 0;     //to control '+' or '-'
...
if (flag == 0)
        size++;
    else if (flag == 1)
        size--;
    if (size > 1000)
        flag = 1;
    if (size < 100)
        flag = 0;

3、完整代碼

#include "pch.h"
#include <GL/glut.h>

GLfloat size = 100.0;
GLsizei size1 = 100, size2 = 100;
GLint flag = 0;     //to control '+' or '-'
class wcPt2D {
public:
    GLfloat x, y;
};

void init(void) {
    /* set color of display window to white */
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(-size, size, -size, size);

    /*set parameters for world-cooridinate clipping window*/
    glMatrixMode(GL_PROJECTION);

    /*set mode for constructiong geometric transformation matrix*/
    glMatrixMode(GL_MODELVIEW);
}

void triangle(wcPt2D *verts) {
    GLint k;

    glBegin(GL_TRIANGLES);
    for (k = 0; k < 3; k++) {
        glVertex2f(verts[k].x, verts[k].y);
        }
    glEnd();
}

void changeSize() {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (flag == 0)
        size++;
    else if (flag == 1)
        size--;
    if (size > 1000)
        flag = 1;
    if (size < 100)
        flag = 0;
    gluOrtho2D(-size, size, -size, size);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glutPostRedisplay();
}

void displayFcn(void) {
    /*define initial position for triangle*/
    wcPt2D verts[3] = {
        {-50.0,-25.0},
        {50.0,-25.0},
        {0.0,50.0}
    };

    /*clear diaplay window*/
    glClear(GL_COLOR_BUFFER_BIT);

    /*set fill color to blue*/
    glColor3f(0.0, 0.0, 1.0);

    /*set left viewport*/
    glViewport(0, 0, 300, 300);

    /*diplay triangle*/
    triangle(verts);

    /*rotate Triangle and display in right half of diaplay windows*/
    /*set fill color to red*/
    glColor3f(1.0, 0.0, 0.0);

    /*set rught viewport*/
    glViewport(300, 0, 300, 300);

    /*rotate about a z axis*/
    glRotatef(90.0, 0.0, 0.0, 1.0);

    /*display red rotated triangle */
    triangle(verts);

    glFlush();
}

void main(int argc,char ** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(50, 50);
    glutInitWindowSize(600, 300);
    glutCreateWindow("split-screen example");

    init();
    glutDisplayFunc(displayFcn);
    glutIdleFunc(changeSize);
    glutMainLoop();
}

4、實(shí)際效果


圖一
圖二
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容