我正在尝试编写显示模拟“tv static”窗口的程序。我有它主要工作,但当我扩展窗口网格线形式。我不知道是什么导致这个,因为这是我的第一个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
删除这些行;但是窗口越大,像素越大。
答案 0 :(得分:3)
使用
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();
}
答案 1 :(得分:2)
您的屏幕尺寸为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调用类型的变化。
答案 2 :(得分:1)
当你调整窗口大小时,看起来glut的窗口大小的内部概念没有得到更新。您可能需要一个窗口调整大小处理程序。