2

次の動作を達成できませんでした。レンダリングされたシーンを歪ませたり、位置を変更したりせずに、ウィンドウのサイズを下から変更したいと考えています。

ここに画像の説明を入力

C++ OpenGL GLUT を使用しています

void resize(int w, int h)
{
    float ratio = G_WIDTH / G_HEIGHT;
    glViewport(0, 0, (h-(w/ratio))>0? w:(int)(h*ratio), (w-(h*ratio))>0? h:(int)(w/ratio));
}
4

4 に答える 4

3

GLUT シーンのサイズ変更機能では、ビューポートはおそらくデフォルトでウィンドウのサイズに設定されています。それをあなたが望む(固定)サイズに変更してみます:

それ以外の:

void windowReshapeFunc( GLint newWidth, GLint newHeight ) 
{
  glViewport( 0, 0, newWidth, newHeight );
  .. etc...
}

これを行う:

void windowReshapeFunc( GLint newWidth, GLint newHeight ) 
{
  glViewport( 0, 0, 400, 600 );
  .. etc...
}

または任意のサイズ。ウィンドウのサイズは変更されますが、これらのシーンは常に (修正された) ビューポートにレンダリングされます。

于 2012-10-17T22:00:29.603 に答える
2

直交遠近法を使用している場合は、これを使用します。

#include <GL/glut.h>

int x0, y0 = 0;
int ww, hh = 0;

void updateCamera()
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(x0, x0+ww, y0, y0+hh, -1, 1);
  glScalef(1, -1, 1);
  glTranslatef(0, -hh, 0);
}

void reshape(int w, int h)
{
    ww = w;
    hh = h;
    glViewport(0, 0, w, h);  
    updateCamera();
}

void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glBegin(GL_TRIANGLES);
    glColor3f(0.0, 0.0, 1.0); glVertex2i(0, 0);
    glColor3f(0.0, 1.0, 0.0); glVertex2i(200, 200);
    glColor3f(1.0, 0.0, 0.0); glVertex2i(20, 200);
  glEnd();
  glFlush();
  glutPostRedisplay(); 
}

int mx = 0;
int my = 0;
int dragContent = 0;

void press(int button, int state, int x, int y)
{
    mx = x;
    my = y;
    dragContent = button == GLUT_LEFT_BUTTON && state == GLUT_DOWN;
}

void move(int x, int y)
{
    if(dragContent)
    {
        x0 -= x - mx;
        y0 += y - my;
        mx = x;
        my = y;
        updateCamera();
    }
}

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutCreateWindow("Resize window without resizing content + drag content");
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutMouseFunc(press);
  glutMotionFunc(move);
  glutMainLoop();
  return 0;
}

左マウスを使用してコンテンツ (三角形) をドラッグできます。

于 2012-10-20T01:26:38.517 に答える
1
ratio = wantedWidth / wantedHeight
glViewport(0, 0, 
      (height-(width/ratio))>0? width:(int)(height*ratio),
  (width-(height*ratio))>0? height:(int)(width/ratio);

そうすることで、ウィンドウの高さ/幅の比率を常に同じに保ちながら、利用可能なウィンドウの最大サイズを使用しようとします。

編集:常に左上隅に置きます。

于 2012-10-17T22:00:07.773 に答える