-1

I have such a OpenGL-4 code (see below). I have created a buffer for vertices and wanted to initialize it with the help of for-loop in init().

It should be a circle of 30 lines (surrounded by circle later) but I can see only the first line on the screen. And I have done such things before with glVertex. But with VOA I don't really know what to do; I tried a lot but I'm really puzzled; May be It's some stupid mistake or my misunderstanding, I failed to find it. is it possible to do so with VOAs at all?

    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();
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
nath68379
  • 1
  • 1
  • Here is an answer with code for drawing a circle: http://stackoverflow.com/questions/25279009/how-to-draw-a-circle-using-vbo-in-es2-0/25321141#25321141. Not exactly the same requirements as yours, but it should give you a good idea of how to do this. – Reto Koradi Oct 19 '14 at 22:28

1 Answers1

0
numLines * 60 * sizeof(GLfloat)

That is way too big, and doesn't match the size of linearVertexBufferData at all. It should be numLines * 2 * sizeof(GLfloat) or even just sizeof(lineVertexBufferData)

glCallList(1);

This is also invalid; you never create any display lists, so it's unlikely that display list one exists. If you're using VAO's, you shouldn't need to create them anyway.

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

The second parameter means that each vertex has three components, but judging from lineVertexBufferData, it should only have two.

glDrawArrays(GL_LINES, 0, numLines * 2);

The third parameter is the number of vertices to render, not number of components. This should not be multiplied by two.

//glutSwapBuffers();

glFlush();

SwapBuffers is correct here, not glFlush (which is almost never needed).

You're also calling transform() after you draw, when it should probably be before, otherwise your transformations will be delayed a frame.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85