rowanth
rowanth

Reputation: 31

OpenGL Orthographic Sketching With GL_LINE_STRIP

I am having an issue with openGL when drawing my sketched line to the screen in that it seems to be adding an extra point at the origin which is not in the point list:

img

(0,0) is definitely not in the points that are in the array to be drawn, just cant seem to reason it out. I would guess that maybe my array size is too big or something but I can't see it

Here is my code for populating the array and the draw call

void PointArray::repopulateObjectBuffer()
{
  //Rebind Appropriate VAO
  glBindVertexArray( vertex_array_object_id );

  //Recalculate Array Size
  vertexArraySize = points.size()*sizeof(glm::vec2);

  //Rebind Appropriate VBO
  glBindBuffer( GL_ARRAY_BUFFER, buffer_object_id );
  glBufferData( GL_ARRAY_BUFFER, vertexArraySize, NULL, GL_STATIC_DRAW );
  glBufferSubData( GL_ARRAY_BUFFER, 0, vertexArraySize, (const GLvoid*)(&points[0]) );

  //Set up Vertex Arrays  
  glEnableVertexAttribArray( 0 ); //SimpleShader attrib at position 0 = "vPosition"
  glVertexAttribPointer( (GLuint)0, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );

  //Unbind
  glBindBuffer(GL_ARRAY_BUFFER, 0);
  glBindVertexArray(0);
}

void PointArray::draw() {
glBindVertexArray(vertex_array_object_id);

glLineWidth(4);
glDrawArrays( GL_LINE_STRIP, 0, vertexArraySize );

glBindVertexArray(0);
}

and here is where I am adding points in the mouse callback

void mouseMovement(int x, int y) 
{
  mouseDX = x - lastX ;
  mouseDY = y - lastY ;
  lastX = x;
  lastY = y;

  if(mouse_drag)
  {
    cam->RotateByMouse(glm::vec2(mouseDX*mouseSens, mouseDY * mouseSens));
  }
  if(sketching)
  {
    sketchLine.addPoint(glm::vec2((float)x,(float)y));
    sketchLine.repopulateObjectBuffer();
    updatePickray(x,y);
  }
}

Finally my simple ortho vertex shader

in vec2 vPosition;

void main(){

const float right = 800.0;
const float bottom = 600.0;
const float left = 0.0;
const float top = 0.0;
const float far = 1.0;
const float near = -1.0;

 mat4 orthoMat = mat4(
    vec4(2.0 / (right - left),              0,                                0,                            0),
    vec4(0,                                 2.0 / (top - bottom),             0,                            0),
    vec4(0,                                 0,                               -2.0 / (far - near),           0),
    vec4(-(right + left) / (right - left), -(top + bottom) / (top - bottom), -(far + near) / (far - near),  1)
);

gl_Position = orthoMat * vec4(vPosition, 0.0, 1.0);
}

Upvotes: 3

Views: 1315

Answers (1)

Tim
Tim

Reputation: 35943

You seem to be using vertexArraySize as the parameter to glDrawArrays, which is not correct. The count of glDrawArrays should be the number of vertices, not the number of bytes of the vertex array.

In your case I guess it would be glDrawArrays( GL_LINE_STRIP, 0, points.size());.

Upvotes: 2

Related Questions