OpenGL 4+绘制像maya / max这样的2D网格

时间:2014-03-08 17:14:39

标签: c++ opengl 3d

我正在尝试在C ++的opengl 4.4中绘制像Maya / max / modo / etc这样的网格。我发现了一个关于如何做到这一点的教程 - LINK它不适用于我的框架 - 它创建我的VBO,然后进行输入和相机处理,然后用abj_render()渲染所有内容。

这里的问题是,如果将 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)写入createVBO()函数,则仅绘制具有水平和垂直线的网格。如果我在常规点有glClear它只绘制水平或垂直线 - 而不是两者。

假设abj_Render当前位于正确位置并且我应该每个循环仅清除一次屏幕 - 在渲染函数内而不是在VBO区域中,这是正确的吗?如果是这样,该网格如何在此框架内正常工作?

//MAIN
createVBO();
SDL_mainLoop();

void SDL_mainLoop()
{
    while (!quit)
    { 
        //CAMERA + MOVEMENT CONTROLS 
        abj_render();
    }
}

void createVBO()
{
    glGetError();

    struct point
    {
        GLfloat x;
        GLfloat y;
    };

    GLint gridLines = 20;
    float gridMinus = gridLines / 2;
    point Vertices[20][20];

    for (int i = 0; i < gridLines; i++)
    {
        for (int j = 0; j < gridLines; j++)
        {
            Vertices[i][j].x = (i - gridMinus) / gridMinus;
            Vertices[i][j].y = (j - gridMinus) / gridMinus;
        }
    }

    glGenVertexArrays(1, &vaoID);
    glBindVertexArray(vaoID);

    glGenBuffers(1, &vboID);
    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);

    glEnableVertexAttribArray(0);

    programID = LoadShaders("simpleVertex.glsl", "simpleFragment.glsl");
    glUseProgram(programID);

    glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);

    //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // HAVING THIS *HERE* DRAWS GRID CORRECTLY

    glLineWidth((GLfloat)2.5f);

    if (glGetError() != GL_NO_ERROR)
        abj_handleError(" Could not create a VBO", "gl");
}


void abj_render()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /// HAVING THIS *HERE* REMOVES THE VERTICALS
    glEnable(GL_FRAMEBUFFER_SRGB);

    glBindVertexArray(vaoID);

    //GLint gridLines = 20;
    //int sizeOf(point) = 8;

    /////////// DRAW THE GRID ///////////
    for (int i = 0; i < 20; i++)
        glDrawArrays(GL_LINE_STRIP, 20 * i, 20);

    for (int i = 0; i < 20; i++)
    {
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 20 * 8, (void *)(i * 8));
        glDrawArrays(GL_LINE_STRIP, 0, 20);
    }

    SDL_GL_SwapWindow(gWindow);
}

旧教程中的旧样式是这样的:

// draw grid
glBegin(GL_LINES);
for (int i=-10;i<=10;++i)
{
    glVertex3f(i,0,-10);
    glVertex3f(i,0,10);

    glVertex3f(10,0,i);
    glVertex3f(-10,0,i);
}
glEnd();

glutSwapBuffers();

0 个答案:

没有答案