OpenGL (glut) - 提高像素放置精度

发布于 2024-10-19 16:35:05 字数 1760 浏览 2 评论 0原文

我正在尝试编写显示模拟“电视静态”窗口的程序。我大部分时间都在工作,但是当我展开窗口网格线形式时。我不知道是什么原因造成的,因为这是我的第一个 OpenGL(过剩)程序。有什么建议吗?提前致谢在此处输入图像描述

#include <GLUT/glut.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

void display(void){
    /* clear window */
    glClear(GL_COLOR_BUFFER_BIT);

    int maxy = glutGet(GLUT_WINDOW_HEIGHT);
    int maxx = glutGet(GLUT_WINDOW_WIDTH);

    glBegin(GL_POINTS);

    for (int y = 0; y <= maxy; ++y) {
        for (int x = 0; x <= maxx; ++x) {

        glColor3d(rand() / (float) RAND_MAX,rand() / (float) RAND_MAX,rand() / (float) RAND_MAX);

        glVertex2i(x, y);
    }
}
            glEnd();


/* flush GL buffers */

glFlush();

}


void init(){
/* set clear color to black */
glClearColor (0.0, 0.0, 0.0, 1.0);

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

/* set up standard orthogonal view with clipping */
/* box as cube of side 2 centered at origin */
/* This is default view and these statement could be removed */
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT), 0, 0, 1);
glDisable(GL_DEPTH_TEST);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}

int main(int argc, char** argv){
srand(time(NULL));
/* Initialize mode and open a window in upper left corner of screen */
/* Window title is name of program (arg[0]) */
glutInit(&argc,argv);

//You can try the following to set the size and position of the window

 glutInitWindowSize(500,500);
 glutInitWindowPosition(0,0);

glutCreateWindow("simple");

glutDisplayFunc(display);
init();
glutIdleFunc(display);
glutMainLoop();
}

编辑:我可以使用 glRecti 删除线条;然而,窗口越大,像素就越大。

I am trying to write program that displays a window with simulated "tv static". I have it mostly working but when I expand the window grid lines form. I have no idea what could be causing this as this is my first OpenGL (glut) program. Any suggestions? thanks in advanceenter image description here

#include <GLUT/glut.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

void display(void){
    /* clear window */
    glClear(GL_COLOR_BUFFER_BIT);

    int maxy = glutGet(GLUT_WINDOW_HEIGHT);
    int maxx = glutGet(GLUT_WINDOW_WIDTH);

    glBegin(GL_POINTS);

    for (int y = 0; y <= maxy; ++y) {
        for (int x = 0; x <= maxx; ++x) {

        glColor3d(rand() / (float) RAND_MAX,rand() / (float) RAND_MAX,rand() / (float) RAND_MAX);

        glVertex2i(x, y);
    }
}
            glEnd();


/* flush GL buffers */

glFlush();

}


void init(){
/* set clear color to black */
glClearColor (0.0, 0.0, 0.0, 1.0);

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

/* set up standard orthogonal view with clipping */
/* box as cube of side 2 centered at origin */
/* This is default view and these statement could be removed */
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT), 0, 0, 1);
glDisable(GL_DEPTH_TEST);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}

int main(int argc, char** argv){
srand(time(NULL));
/* Initialize mode and open a window in upper left corner of screen */
/* Window title is name of program (arg[0]) */
glutInit(&argc,argv);

//You can try the following to set the size and position of the window

 glutInitWindowSize(500,500);
 glutInitWindowPosition(0,0);

glutCreateWindow("simple");

glutDisplayFunc(display);
init();
glutIdleFunc(display);
glutMainLoop();
}

Edit: I can remove the lines by using glRecti; however the pixels get bigger the larger the window gets.

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

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

发布评论

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

评论(3

谁许谁一生繁华 2024-10-26 16:35:05

用于

void glutReshapeFunc(void (*func)(int width, int height));

在窗口大小更改时重置投影 glOrtho

在你的情况下,这应该可以解决问题:

void resize(int width,int height)
{
  glOrtho(0, width, height, 0, 0, 1);
}

int main(int argc, char** argv){
  //...
  glutReshapeFunc(resize);

  glutDisplayFunc(display);
  init();
  glutIdleFunc(display);
  glutMainLoop();
}

Use

void glutReshapeFunc(void (*func)(int width, int height));

to reset the projection, glOrtho, when the window size changes.

In your case this should do the trick:

void resize(int width,int height)
{
  glOrtho(0, width, height, 0, 0, 1);
}

int main(int argc, char** argv){
  //...
  glutReshapeFunc(resize);

  glutDisplayFunc(display);
  init();
  glutIdleFunc(display);
  glutMainLoop();
}
一百个冬季 2024-10-26 16:35:05

您的屏幕尺寸为 width*height 大小,但您实际上绘制的是 (width+1)*(height+1) 点。另外,您的边界像素直接绘制在边界线上,因此我不确定它们是否可见。

解决方案:

for (int y = 0; y < maxy; ++y) {
    for (int x = 0; x < maxx; ++x) {
        glColor3d(rand() / (float) RAND_MAX,rand() / (float) RAND_MAX,rand() / (float) RAND_MAX);
        glVertex2f(x+0.5f, y+0.5f);
    }
}

注意循环条件和 glVertex 调用类型的变化。

Your screen is width*height size, but you are actually drawing (width+1)*(height+1) points. Also, your boundary pixels are drawn right on the boundary lines, so I'm not sure they'll be visible.

Solution:

for (int y = 0; y < maxy; ++y) {
    for (int x = 0; x < maxx; ++x) {
        glColor3d(rand() / (float) RAND_MAX,rand() / (float) RAND_MAX,rand() / (float) RAND_MAX);
        glVertex2f(x+0.5f, y+0.5f);
    }
}

Notice the change in loop conditions and type of glVertex call.

策马西风 2024-10-26 16:35:05

当您调整窗口大小时,看起来 glut 关于窗口大小的内部想法并没有得到更新。您可能需要一个窗口调整大小处理程序。

It looks like glut's internal idea of window size isn't getting updated when you resize the window. You may need a window resize handler.

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