c - OpenGL-4 : How to create a circle and circle of lines using VOA

标签 c opengl

我有这样的OpenGL-4代码(见下文)。我已经为顶点创建了一个缓冲区,并希望借助 init() 中的 for 循环来初始化它。

它应该是一个 30 行的圆圈(稍后被圆圈包围),但我在屏幕上只能看到第一行。我以前用 glVertex 做过这样的事情。但对于美国之音,我真的不知道该怎么办;我尝试了很多,但我真的很困惑;可能是一些愚蠢的错误或我的误解,我没能找到它。用 VOA 可以做到这一点吗?

    GLuint lineArrayID;
    GLuint lineVertexBuffer;
    GLuint numLines = 30;
    static GLfloat lineVertexBufferData[30][2] = {}; 
    void init() {
    draw_circle();
    glClearColor(1.0, 1.0, 1.0, 1.0);//background of the window

     GLfloat x, y;
    double angle;
    GLfloat radius = 5.0;

    angle = 2 * PI / 30;
    int j = 0;
    float i = 0;
    for (i = -PI; i < -PI; i += 0.15){
        std::cout << " +"<<std:: endl;
        x = sin(i);
        y = cos(i);

        lineVertexBufferData[j][0] = 0.0;  lineVertexBufferData[j][1] = 0.0;

        lineVertexBufferData[j][0] = x;     lineVertexBufferData[j][1] = y;

        j++;
    }

     // compile and activate the desired shader program
     shaderProgramID = loadShaders("D.vs", "D.fs");
     glUseProgram(shaderProgramID);
     // generate Buffers for our objects

    prepareLines();
    }

    void prepareLines(){


    glGenVertexArrays(1, &lineArrayID);  //gen one array object
    glBindVertexArray(lineArrayID);      //binds it

    glGenBuffers(1, &lineVertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, lineVertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, numLines * 60 * sizeof(GLfloat), lineVertexBufferData, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

    glEnableVertexAttribArray(0);
    glBindVertexArray(0);

}
static void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);


    // drawing the lines
    glBindVertexArray(lineArrayID);
glCallList(1);
    glDrawArrays(GL_LINES, 0, numLines * 2);
    glBindVertexArray(0);

    transform();
    //glRotatef(grad, 0, 0, 1);

    //glutSwapBuffers();

    glFlush();
}

最佳答案

numLines * 60 * sizeof(GLfloat)

这太大了,根本不匹配 linearVertexBufferData 的大小。它应该是 numLines * 2 * sizeof(GLfloat) 甚至只是 sizeof(lineVertexBufferData)

glCallList(1);

这也是无效的;您从未创建任何显示列表,因此显示列表不太可能存在。如果您正在使用 VAO,则无论如何都不需要创建它们。

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

第二个参数表示每个顶点有三个分量,但从lineVertexBufferData来看,应该只有两个。

glDrawArrays(GL_LINES, 0, numLines * 2);

第三个参数是要渲染的顶点数,而不是组件数。该值不应乘以二。

//glutSwapBuffers();

glFlush();

SwapBuffers 在这里是正确的,而不是 glFlush (几乎不需要)。

您还在绘制之后调用 transform() ,而它可能应该在绘制之前,否则您的转换将延迟一帧。

关于c - OpenGL-4 : How to create a circle and circle of lines using VOA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26424745/

相关文章:

c# - C# 中的 OpenGL 包装器

opengl - opengl中纹理单元和采样器制服的对应关系

opengl - 逼真的贴花

与加速度计接口(interface)时的 C 段错误

凯撒密码打印出数字而不是解密文本?在 C

c - 如何创建三维动态数组,其中维度之一在c编译时已知

opengl - 如何在 glBufferData 期间处理 GL_OUT_OF_MEMORY 错误?

C编程十六进制转字符

c - 查看 .gpg 文件内容

c++ - Qt5 & OpenGL : stdlib. h: 没有那个文件或目录