0

我正在尝试在 OpenGL 窗口中绘制一个 50x50 的网格。我绘制网格的代码是

void GLGrid::draw() {

int y=-width;
int yIncrement = width / 50;
int x=-length;
int xIncrement = length / 50;


glColor3f(0.0f,0.0f,0.0f);
for(y = -width; y < width; y+=yIncrement) {
    glBegin(GL_LINES);
        glVertex3f(-width,y,0);
        glVertex3f(width,y,0);
    glEnd();
}

for(x = -length; x < length; x+=xIncrement) {
    glBegin(GL_LINES);
        glVertex3f(-length,x,0);
        glVertex3f(length,x,0);
    glEnd();
}
}

请注意,在我执行 x=0;x < length 等操作之前,这使得该行(我看到的唯一一行)从屏幕中间开始,而不是最左边。另外,当我在整个窗口上绘制一个矩形时,我必须从负 300x300 开始,而不是 0,0。

我唯一看到的是屏幕中间的一条水平线。我认为问题是我不知道我的窗口大小实际上是多少。每当我点击时打印出来

static void mouseEvent(int button, int state, int x, int y) {
cout<<"\nMouse Event!";
cout<<"\n\tbutton:"<<button;
cout<<"\n\tstate:"<<state;
cout<<"\n\tx:"<<x;
cout<<"\n\ty:"<<y<<"\n";
}

它打印出左上角是 0,0,右下角是 300,300。所以我将我的 GLGrid 长度和宽度设置为 300。我应该将窗口长度和宽度设置为其他值吗?如果是这样,是什么?我对OpenGL很陌生,所以请原谅我的无知。为了彻底,因为我不知道是否有其他微妙的问题可能是问题,我将包含更多代码

static void initOpenGL() {
//set clear color to white
glClearColor(0.0f,0.0f,0.0f,1.0f);

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}


/*OpenGL calls*/
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

if (!init)
{
    initOpenGL();
}

renderScene();

//...more code below in this function but pretty positive its irrelevant



static void renderScene() {

    drawBackground();

    drawGrid();

}


static void drawBackground() {
//draw a white rectangle for background
    glColor3f(1.0f,1.0f,1.0f);
    glBegin(GL_QUADS);
        glVertex3f(-windowMaxX, -windowMaxY, 0);
        glVertex3f(windowMaxX, -windowMaxY, 0);
        glVertex3f(windowMaxX, windowMaxY, 0);
        glVertex3f(-windowMaxX, windowMaxY, 0);
    glEnd();
}


static void drawGrid() {
    GLGrid.draw();
}
4

1 回答 1

1

当您绘制垂直线时,您需要更改您的x值,而不是您的y值:

glBegin(GL_LINES);
    glVertex3f(x,-length,0);
    glVertex3f(x,length,0);
glEnd();

可能还有更多错误,但这是要改变的一件事。

于 2012-12-06T07:35:25.167 回答