使用GL_LINE_STRIP进行OpenGL正交绘制草图

时间:2012-11-01 19:20:22

标签: opengl shader projection orthographic

当我将草绘的线条绘制到屏幕上时,我遇到了一个问题,因为它似乎是在原点上添加了一个不在点列表中的额外点:

img

(0,0)绝对不是要绘制的数组中的点,只是似乎无法推理出来。我猜可能是我的阵列尺寸太大或者其他东西但我看不到它

这是我填充数组和绘制调用的代码

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);
}

这里是我在鼠标回调中添加点的地方

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);
  }
}

最后我的简单正交顶点着色器

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);
}

1 个答案:

答案 0 :(得分:2)

您似乎使用vertexArraySize作为glDrawArrays的参数,这是不正确的。 glDrawArrays的计数应该是顶点的数量,而不是顶点数组的字节数。

在你的情况下,我猜它会是glDrawArrays( GL_LINE_STRIP, 0, points.size());