【问题标题】:OpenGL Window size for drawing grid用于绘制网格的 OpenGL 窗口大小
【发布时间】:2012-12-06 22:33:04
【问题描述】:

我正在尝试在 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

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

点击时打印出来
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();
}

【问题讨论】:

  • 你用什么来获取鼠标事件?你有没有打过glMultMatrixglScaleglTranslate的电话?
  • 我只是在做 glutMouseFunc (mouseEvent);调用该函数。我不做任何多矩阵、缩放或翻译,调用..

标签: opengl grid coordinates


【解决方案1】:

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

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

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

【讨论】:

  • 嗯,好的,谢谢,这有点帮助。我现在看到一个垂直的和一个水平的:),都在中心。
  • 如果您尝试在循环中打印,您确定您的 xy 值在 -300 到 300 之间吗?
  • 是的,他们是。它们以 6 为增量。
  • 如果你让xy 介于-11 之间呢? (让他们floats
  • 呃,问题是默认绘图坐标从-11。您可以使用glOrtho 将透视图更改为更有用的东西,方法是将其称为glOrtho(0, 300, 0, 300, -1, 1);。您可能注意到的一个烦恼是鼠标位置函数只为您提供像素值。
猜你喜欢
  • 2017-09-25
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 2013-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-29
相关资源
最近更新 更多