2014-10-31 48 views
0

我試圖運行我的第一個OpenGL程序。在main()函數我有無限循環:使用OpenGL在不同的程序中繪製

do { 
    glClear(GL_COLOR_BUFFER_BIT); 
    glUseProgram(programID); 

    _collection[0].draw(); 
    _collection[1].draw(); 

    glfwSwapBuffers(window); 
    glfwPollEvents(); 
} while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0) 

功能_collection []繪製()應繪製矩形:

static const GLfloat g_vertex_buffer_data[] = { 
    x, y, 0.0f,     // lewy górny 
    x, y - 0.4f, 0.0f,   // lewy dolny 
    x + 0.4f, y - 0.4f, 0.0f, // prawy dolny 
    x + 0.4f, y, 0.0f,   // lewy górny 

    x + 0.02f, y - 0.02f, 0.0f,     // lewy górny 
    x + 0.02f, y - 0.4f + 0.02f, 0.0f,   // lewy dolny 
    x + 0.4f - 0.02f, y - 0.4f + 0.02f, 0.0f, // prawy dolny 
    x + 0.4f - 0.02f, y - 0.02f, 0.0f,   // lewy górny 
}; 

static const GLfloat g_color_buffer_data[] = { 
    1.0f, 1.0f, 1.0f, // lewy górny 
    1.0f, 1.0f, 1.0f, // lewy dolny 
    1.0f, 1.0f, 1.0f, // prawy dolny 
    1.0f, 1.0f, 1.0f, // lewy górny 

    0.0f, 0.0f, 1.0f, 
    0.0f, 0.0f, 1.0f, 
    0.0f, 0.0f, 1.0f, 
    0.0f, 0.0f, 1.0f, 
}; 



GLuint vertexbuffer; 
glGenBuffers(1, &vertexbuffer); 
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); 

GLuint colorbuffer; 
glGenBuffers(1, &colorbuffer); 
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); 
glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW); 

glEnableVertexAttribArray(vertexPosition_modelspaceID); 
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 
glVertexAttribPointer(
    vertexPosition_modelspaceID, // The attribute we want to configure 
    3,     // size 
    GL_FLOAT,   // type 
    GL_FALSE,   // normalized? 
    0,     // stride 
    (void*)0   // array buffer offset 
    ); 

// przekazuję kolory wierzchołków 
glEnableVertexAttribArray(vertexColorID); 
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); 
glVertexAttribPointer(
    vertexColorID,    // The attribute we want to configure 
    3,       // size 
    GL_FLOAT,     // type 
    GL_FALSE,     // normalized? 
    0,       // stride 
    (void*)0      // array buffer offset 
    ); 

// rysuję wszystko 
glDrawArrays(GL_QUADS, 0, 8); 


glDisableVertexAttribArray(vertexPosition_modelspaceID); 
glDisableVertexAttribArray(vertexColorID); 

我的問題是:當我運行程序我見運行的唯一效果的第一個函數draw() - 這跟指數爲0。然後我改變將這些功能:

_collection[1].draw(); 
_collection[0].draw(); 

我仍然看到的第一功能的影響 - 與索引號1在這種情況下。

看起來有些東西阻塞了第二個draw()函數運行的代碼。

什麼問題?我該如何解決它?

回答

1

第二個繪製函數沒有被阻止執行。由於您的頂點和顏色信息在draw()函數的主體內定義爲靜態,因此無論您繪製哪個元素_collection,這些值都不會更改。這就是爲什麼繪製這兩個集合產生相同的結果 - 您正在同一位置繪製頂點,並使用相同的顏色。

要解決此問題,您只需要存儲一次頂點和顏色信息。您的每個收藏只應包含xy值,表明其位置。您不需要多個頂點和顏色集合,您需要在幾個不同位置繪製的單個頂點和顏色集合。

在進入主循環之前,您應該在主函數中創建頂點和顏色數組。您還應該使用glGenBuffersglBindBuffer後跟glBufferData在主循環之前告訴OpenGL關於您的主程序中的頂點和顏色數組。然後,您可以將電話撥到glGenBuffersglBufferData以外的繪圖功能。您還應該在主函數中爲頂點和顏色數組調用glVertexAttribPointer,並將它們從draw()函數中移除。

// Note that your vertex data isn't contingent on 'x' and 'y' positions. 
// You will use the vertex shader to move your boxes around later. 
GLfloat g_vertex_buffer_data[] = { 
    0.0f, 0, 0.0f,     // lewy górny 
    0.0f, 0.4f, 0.0f,   // lewy dolny 
    0.4f, 0.4f, 0.0f, // prawy dolny 
    0.4f, 0.0f, 0.0f,   // lewy górny 

    0.02f, 0.02f, 0.0f,     // lewy górny 
    0.02f, 0.4f + 0.02f, 0.0f,   // lewy dolny 
    0.4f - 0.02f, 0.4f + 0.02f, 0.0f, // prawy dolny 
    0.4f - 0.02f, 0.02f, 0.0f,   // lewy górny 
}; 

GLfloat g_color_buffer_data[] = { 
    1.0f, 1.0f, 1.0f, // lewy górny 
    1.0f, 1.0f, 1.0f, // lewy dolny 
    1.0f, 1.0f, 1.0f, // prawy dolny 
    1.0f, 1.0f, 1.0f, // lewy górny 

    0.0f, 0.0f, 1.0f, 
    0.0f, 0.0f, 1.0f, 
    0.0f, 0.0f, 1.0f, 
    0.0f, 0.0f, 1.0f, 
}; 

GLuint vertexbuffer; 
glGenBuffers(1, &vertexbuffer); 
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); 
glVertexAttribPointer(
    vertexPosition_modelspaceID, // The attribute we want to configure 
    3,     // size 
    GL_FLOAT,   // type 
    GL_FALSE,   // normalized? 
    0,     // stride 
    (void*)0   // array buffer offset 
    ); 

GLuint colorbuffer; 
glGenBuffers(1, &colorbuffer); 
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); 
glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW); 
glVertexAttribPointer(
    vertexColorID,    // The attribute we want to configure 
    3,       // size 
    GL_FLOAT,     // type 
    GL_FALSE,     // normalized? 
    0,       // stride 
    (void*)0      // array buffer offset 
    ); 
// All of the above information you only need to specify to openGL once, not every time you draw a frame! 

你需要改變你的着色器,使其接受來自每個集合的偏移x和y:

#version 150 
    uniform float collectionX; 
    uniform float collectionY; 
    in vec3 vertexPosition_modelspaceID; // This is the vertex attribute which the name 'vertexPosition_modelspaceID' corresponds to. 
    // Remember that your shader will also accept a color and give it to the fragment shader, include that code as well. 
    void main() 
    { 
     gl_Position = vec4(vertexPosition_modelspaceID.x + collectionX, vertexPosition_modelspaceID.y + collectionY, vertexPosition_modelspaceID.z, 1.0); 
    } 

而你需要讓你剛添加到統一變量的位置你在你的主程序着色器循環之前:

// Call these functions after you compile and link your shaders. programID should be your compiled and linked shader program. 
GLuint collectionXID = glGetUniformLocation(programID, "collectionX"); 
GLuint collectionYID = glGetUniformLocation(programID, "collectionY"); 

你繪製函數將是非常簡單的現在:

void draw() 
{ 
    glDrawArrays(GL_QUADS, 0, 8); 
} 

最後,主循環將是這個樣子:

do 
{ 
    glClear(GL_COLOR_BUFFER_BIT); 
    glUseProgram(programID); 

    glEnableVertexAttribArray(vertexPosition_modelspaceID); 
    glEnableVertexAttribArray(vertexColorID); 

    glUniform1f(collectionXID, _collection[0].x); 
    glUniform1f(collectionYID, _collection[0].y); 
    _collection[0].draw(); 

    glUniform1f(collectionXID, _collection[1].x); 
    glUniform1f(collectionYID, _collection[1].y); 
    _collection[1].draw(); 

    glfwSwapBuffers(window); 
    glDisableVertexAttribArray(vertexPosition_modelspaceID); 
    glDisableVertexAttribArray(vertexColorID); 

    glfwPollEvents(); 
} while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0) 

注意,你現在指定在其通過傳遞您的個人收藏的xy位置繪製頂點着色器程序的位置與glUniform1f函數。用變換矩陣移動頂點更爲常見,但這本身就是一個相當複雜的主題。

假設館藏有不同的xy職位,他們現在將繪製在不同的位置。

+0

因此,他應該製作頂點的副本,使它們不同? – Surt 2014-10-31 17:59:29

+0

實際上,它看起來像他想渲染相同的幾何圖形兩次,但在不同的地方。他應該發送一個轉換矩陣,執行x,y轉換到其頂點着色器,並只使用一個頂點緩衝區和顏色緩衝區。將更新我的答案。 – 2014-10-31 22:36:23

相關問題