3

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

图像

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

1 回答 1

2

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

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

于 2012-11-01T19:26:22.417 回答