OpenGL (glut) - 提高像素放置精度
我正在尝试编写显示模拟“电视静态”窗口的程序。我大部分时间都在工作,但是当我展开窗口网格线形式时。我不知道是什么原因造成的,因为这是我的第一个 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 advance
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
用于
在窗口大小更改时重置投影
glOrtho
。在你的情况下,这应该可以解决问题:
Use
to reset the projection,
glOrtho
, when the window size changes.In your case this should do the trick:
您的屏幕尺寸为
width*height
大小,但您实际上绘制的是(width+1)*(height+1)
点。另外,您的边界像素直接绘制在边界线上,因此我不确定它们是否可见。解决方案:
注意循环条件和 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:
Notice the change in loop conditions and type of glVertex call.
当您调整窗口大小时,看起来 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.