Igor Lihachev
Igor Lihachev

Reputation: 25

glDrawArrays weirdly draws array

When I try to draw circle using glDrawArrays it displays quarter of circle

The VBO, generated by code, is correct. BTW there is no such coord as 0,0,0. It appears that it draws only positive vertices, but if I multiply vertices by -1 glDrawArrays draws it on other side of screen. Here's my code:

uint un_vertexArraySize = 20;
float f_radius = 1.0f;
float f_degrees = 0;
GLfloat f_vert[un_vertexArraySize*3];
// Generate circle vertices
for (int i=0; i<un_vertexArraySize*3; i+=3)
{
    f_vert[i] = cos(f_degrees*M_PI/180)*f_radius;
    f_vert[i+1] = sin(f_degrees*M_PI/180)*f_radius;
    f_vert[i+2] = 0.0f;
    f_degrees += (360/un_vertexArraySize);
}
// Do buffer stuff
GLuint vbo;
glGenBuffers(1, &vbo);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, un_vertexArraySize*3,
             f_vert, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0); // unbind VBO
glBindVertexArray(0); // unbind VAO

// Start loop
    glClearColor(0.0,0.0,0.0,0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    shader.Use();
    glBindBuffer(GL_ARRAY_BUFFER, vao);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glDrawArrays(GL_LINE_STRIP, 0, un_vertexArraySize*3);
    glDisableVertexAttribArray(0);
    shader.UnUse();
// End loop

Frag shader:

#version 130
void main(void)
{
    gl_FragColor[0] = gl_FragCoord.x/640.0;
    gl_FragColor[1] = gl_FragCoord.y/480.0;
    gl_FragColor[2] = 0.2;
    gl_FragColor[3] = 1.0f;
}

Vert shader:

#version 130
attribute vec3 coord3d;

void main(void)
{
    vec4 temp = vec4(coord3d, 1.0f);
    gl_Position = temp;
}

I tried to google it or find on stackoverflow, but nothing.

Upvotes: 2

Views: 108

Answers (1)

Pedro David
Pedro David

Reputation: 377

I can't test this right now but at glBufferData you use un_vertexArraySize*3, that is the number of elements, but I think you missed multiplying by 4 (bytes per float) so the size is specified in bytes. That would explain why you're only drawing a quarter of the circle

Upvotes: 2

Related Questions