gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:52:51 29 4
gpt4 key购买 nike

我正在尝试编写程序来显示带有模拟“电视静态”的窗口。我大部分时间都在工作,但是当我展开窗口网格线形式时。我不知道是什么原因造成的,因为这是我的第一个 OpenGL(过剩)程序。有什么建议么?提前致谢enter 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();
}

编辑:我可以使用 glRecti 删除这些行;然而,窗口越大,像素越大。

最佳答案

使用

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();
}

关于c++ - OpenGL (glut) - 提高像素放置精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5113901/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com