2

可能重复:
在 OpenGL 中更改投影

我想找到一种保持对象大小相同的方法。我有这个正方形,它从屏幕的左上角移动到右下角。当我调整输出窗口的大小时,正方形的大小也会增加。

下面是我的显示和重塑功能。

void Changesize(int w,int h)
{

    ww=w;
         wh=h;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
        glLoadIdentity() ;
    glMatrixMode(GL_MODELVIEW);

}

void display(void)
{   

    printf("i=%f & j=%f \n",i,j);
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3i (rand(), rand(), rand());
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
    glBegin(GL_POLYGON);
    glColor3i (rand(), rand(), rand());
    glVertex3f (i, j-0.125, 0.0);
    glVertex3f (i+0.125, j-0.125, 0.0);
    glVertex3f (i+0.125, j , 0.0);
    glVertex3f (i, j, 0.0);
    glEnd();
    glFlush ();
    usleep(100000);
    i=i+0.0125;
    j=j-0.0125;

    if (i>0.999999)
        {
            i=0;
            j=1;
        }
    glutSwapBuffers();
    glutPostRedisplay();

}
4

1 回答 1

1

试一试:

#include <GL/glut.h>

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glColor3ub(255,0,0);
    glPushMatrix();
        glScalef(50,50,50);
        glBegin(GL_QUADS);
            glVertex2f(-1,-1);
            glVertex2f(1,-1);
            glVertex2f(1,1);
            glVertex2f(-1,1);
        glEnd();
    glPopMatrix();

    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho( -w/2.0, w/2.0, -h/2.0, h/2.0, -1, 1);
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

    glutInitWindowSize(800,600);
    glutCreateWindow("Scale");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}
于 2012-09-21T16:05:16.623 回答