在调整窗口大小后如何保持对象的大小(让我们说一个正方形)

时间:2012-09-21 15:54:55

标签: opengl glut

  

可能重复:
  Changing projection in 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();

}

1 个答案:

答案 0 :(得分: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;
}