2012-11-01 53 views
3

我畫我的草圖線時要在屏幕中,它似乎被添加在其不在點列表中的原點額外的點有支持OpenGL的一個問題:OpenGL的正字草圖隨着GL_LINE_STRIP

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

爲了將來的參考,即使你不能內聯一個圖像,你仍然可以鏈接到一個(像我這樣的人可能會爲你內聯)。 – Tim

回答

2

您似乎使用vertexArraySize作爲glDrawArrays的參數,這是不正確的。 glDrawArrays的數量應該是頂點的數量,而不是頂點數組的字節數。

在你的情況下,我想這將是glDrawArrays(GL_LINE_STRIP, 0, points.size());

+0

我不能相信我錯過了,顯然看起來太久了。非常感謝。 – rowanth

+0

@rowanth你應該真的考慮把這個答案標記爲正確的答案...... – Patrik