用于绘制网格的OpenGL窗口大小

时间:2012-12-06 07:25:53

标签: opengl grid coordinates

我试图在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&lt;长度等,但这是使线(我看到的唯一一个)从屏幕中间开始,而不是从左边开始。此外,当我在整个窗口上绘制一个矩形时,我必须以负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();
}

1 个答案:

答案 0 :(得分:1)

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

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

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