2011-02-25 20 views
0

我正在嘗試編寫一個程序,用於顯示一個帶有模擬「電視靜態」的窗口。我主要工作,但當我擴大窗口網格線形式。我不知道是什麼可能導致這是因爲這是我的第一個OpenGL(過剩)程序。有什麼建議麼?由於事先enter image description hereOpenGL(過剩) - 增加像素位置精度

#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

我結束了修改,以便kvark代碼填寫空間(由0.3遞增) – romejoe 2011-03-03 05:19:19

回答

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呼叫的類型的變化。

1

當您調整窗口大小時,它看起來像glut的內部窗口大小的想法沒有得到更新。您可能需要一個窗口大小調整處理程序。

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