调整过剩窗口的大小

发布于 2024-09-25 21:48:51 字数 677 浏览 4 评论 0原文

我正在一个过多的窗口中进行 2D 绘图,并且无法正确调整窗口大小。

我的 glutDisplayFunc 和 glutReshapeFunc 如下。实际上,当调整窗口大小时,绘图就会消失。如果我从 displayFunc() 中删除 glClear(),新像素不会“初始化”并且存在剪切问题。我该如何解决这个问题?

glutDisplayFunc:

void displayFunc() {
  glDisable( GL_DEPTH_TEST );
  glClear( GL_COLOR_BUFFER_BIT );
  glPointSize ( 3.0 );
  glFlush();
}

glutReshapeFunc:

void windowReshapeFunc( GLint newWidth, GLint newHeight ) {
  glViewport( 0, 0, newWidth, newHeight );
  glMatrixMode( GL_PROJECTION );
  glLoadIdentity();
  gluOrtho2D( 0, GLdouble (newWidth), 0, GLdouble (newHeight) );

  windowWidth = newWidth;
  windowHeight = newHeight;
}

I'm doing 2D drawing in a glut window, and I'm having trouble making the window resize properly.

My glutDisplayFunc and glutReshapeFunc are below. As it is, the drawing disappears when the window is resized. If I delete the glClear() from displayFunc(), new pixels don't "initialize" and there are clipping problems. How do I fix this?

glutDisplayFunc:

void displayFunc() {
  glDisable( GL_DEPTH_TEST );
  glClear( GL_COLOR_BUFFER_BIT );
  glPointSize ( 3.0 );
  glFlush();
}

glutReshapeFunc:

void windowReshapeFunc( GLint newWidth, GLint newHeight ) {
  glViewport( 0, 0, newWidth, newHeight );
  glMatrixMode( GL_PROJECTION );
  glLoadIdentity();
  gluOrtho2D( 0, GLdouble (newWidth), 0, GLdouble (newHeight) );

  windowWidth = newWidth;
  windowHeight = newHeight;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

雨落□心尘 2024-10-02 21:48:51

我会尝试在重塑函数末尾添加对 glutPostRedisplay() 的调用。

I'd try adding a call to glutPostRedisplay() around the end of your reshape function.

草莓酥 2024-10-02 21:48:51

这是我使用的基本模板...

#include <GL/glut.h>
#include <stdio.h>
#include <stdarg.h>

#define WINDOW_WIDTH  300
#define WINDOW_HEIGHT 300

// current Window width and height
int  win_width, win_height; 

void display()
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    // write your code here
    //
    //

    glutSwapBuffers();
}

void on_resize(int w, int h)
{
    win_width = w;
    win_height = h;
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-w / 2, w / 2, -h / 2, h / 2, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    display(); // refresh window.
}

void main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    glutCreateWindow("basic resize template");

    glutDisplayFunc(display);
    glutReshapeFunc(on_resize);

    glutMainLoop();
}

This is basic template that I use...

#include <GL/glut.h>
#include <stdio.h>
#include <stdarg.h>

#define WINDOW_WIDTH  300
#define WINDOW_HEIGHT 300

// current Window width and height
int  win_width, win_height; 

void display()
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    // write your code here
    //
    //

    glutSwapBuffers();
}

void on_resize(int w, int h)
{
    win_width = w;
    win_height = h;
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-w / 2, w / 2, -h / 2, h / 2, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    display(); // refresh window.
}

void main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    glutCreateWindow("basic resize template");

    glutDisplayFunc(display);
    glutReshapeFunc(on_resize);

    glutMainLoop();
}
半岛未凉 2024-10-02 21:48:51

您不能使用同一窗口调用 gluOrtho2D 两次,它会破坏图形,因此不会显示任何内容。您必须初始化窗口大小并将 Ortho 设置为屏幕大小(当前显示分辨​​率),然后相对于窗口大小进行绘制。

You can not call gluOrtho2D twice with the same window, it will break the graphics and so nothing shows up. You would have to init window size and set Ortho to the size of the screen (current display resolution), then draw relative to the size of the window.

二货你真萌 2024-10-02 21:48:51

我猜你的代码不会在显示函数中绘制场景上的所有内容,你看,如果没有发生任何事件,你必须调用一次显示,并且第一次它有你的绘图。但是,当有一个事件表明窗口大小已调整时,您的问题就会出现!尝试将您的绘图部分放入显示功能中。像这样,

void displayFunc() {
  glDisable( GL_DEPTH_TEST );
  glClear( GL_COLOR_BUFFER_BIT );
  glPointSize ( 3.0 );
  glBegin(GL_POINTS);
  {
          //Blah blah blah some points here which you expect to draw
  }
  glEnd();
  glFlush();
}

如果这没有帮助,请发布完整的代码。

I guess your code does not draw everything on scene in display func, you see, if no events occcur ever you have to call display one time and in the first time it has your drawing. But your problem rises when there is an event which says the window is resized! try putting your drawing part in display function. Like so,

void displayFunc() {
  glDisable( GL_DEPTH_TEST );
  glClear( GL_COLOR_BUFFER_BIT );
  glPointSize ( 3.0 );
  glBegin(GL_POINTS);
  {
          //Blah blah blah some points here which you expect to draw
  }
  glEnd();
  glFlush();
}

Please post the full code if this was not helpful.

天邊彩虹 2024-10-02 21:48:51

您没有在重塑函数结束时将矩阵模式设置回 GL_MODELVIEW。

void reshape(int width, int height) {
    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, GLdouble (width), 0, GLdouble (height) );
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

如果没有看到更多代码,很难说您的显示函数中是否还有其他内容。我希望这有帮助。

You're not setting the matrix mode back to GL_MODELVIEW at the end of your reshape function.

void reshape(int width, int height) {
    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, GLdouble (width), 0, GLdouble (height) );
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

It's hard to say if there's something else in your display function without seeing more code. I hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文