2017-01-02 57 views
0

我試圖更新三角形的每個frame.I顏色沒有運氣,我不明白爲什麼有人可以幫助我。我已經嘗試了多次,改變我嘗試的方式每次更新數據。任何幫助,將不勝感激。Opengl着色器vec3未更新

#include <glew.h> 
#include <glfw3.h> 
#include <iostream> 
#include <fstream> 
#include <time.h> 
using namespace std; 

GLuint compileshaders() { 
    GLuint vshader, fshader, program; 

    static const GLchar * vshadersource[] = { 
     "#version 450 core    \n" 
     " layout (location=0) in vec3 position;\n" 
     " layout (location=1) in vec3 color;\n" 
     " out vec3 color_out    ;\n" 
     "void main()     \n" 
     "{        \n" 
     "color_out = color;    \n" 
     "gl_Position = vec4(position,1.0);\n" 
     "}        \n" 
    }; 

    static const GLchar * fshadersource[] = { 
     "#version 450 core    \n" 
     "in vec3 color_out;    \n" 
     "out vec4 colorO;     \n" 
     "void main()      \n" 
     "{        \n" 
     " colorO = vec4(color_out,1.0); \n" 
     "}        \n" 
    }; 

    vshader = glCreateShader(GL_VERTEX_SHADER); 
    glShaderSource(vshader, 1, vshadersource, NULL); 
    glCompileShader(vshader); 

    fshader = glCreateShader(GL_FRAGMENT_SHADER); 
    glShaderSource(fshader, 1, fshadersource, NULL); 
    glCompileShader(fshader); 

    GLint vCompiled = 0; 
    glGetShaderiv(vshader, GL_COMPILE_STATUS, &vCompiled); 
    GLint fCompiled = 0; 
    glGetShaderiv(fshader, GL_COMPILE_STATUS, &fCompiled); 

    if (vCompiled && fCompiled != GL_FALSE) { 
     program = glCreateProgram(); 
     glAttachShader(program, vshader); 
     glAttachShader(program, fshader); 
     glLinkProgram(program); 
     glDeleteShader(vshader); 
     glDeleteShader(fshader); 
     return program; 
    } 
    else { 
     cout << "Shader Error!" << endl; 
    } 

    glDeleteShader(vshader); 
    glDeleteShader(fshader); 

    return NULL; 
} 

int main(void) 
{ 
    srand(time(0)); 
    GLFWwindow* window; 
    GLuint program; 
    GLuint vertex_array_object; 
    GLuint buffers[2]; 

    if (!glfwInit()) 
     return -1; 

    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); 
    if (!window) 
    { 
     glfwTerminate(); 
     return -1; 
    } 

    glfwMakeContextCurrent(window); 

    glewInit(); 

    static const GLfloat positions[] = {0.25,-0.25,0.5,0.25,0.25,0.5,-0.25,-0.25,0.5}; 
    static GLfloat color[] = {1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0}; 

    program = compileshaders(); 
    glUseProgram(program); 
    glCreateVertexArrays(1, &vertex_array_object); 
    glCreateBuffers(2, &buffers[0]); 
    glNamedBufferStorage(buffers[0], sizeof(positions), positions, 0); 
    glVertexArrayVertexBuffer(vertex_array_object, 0, buffers[0], 0, sizeof(GLfloat) * 3); 
    glVertexArrayAttribFormat(vertex_array_object, 0, 3, GL_FLOAT, GL_FALSE, 0); 
    glVertexArrayAttribBinding(vertex_array_object, 0, 0); 
    glEnableVertexArrayAttrib(vertex_array_object, 0); 
    glNamedBufferStorage(buffers[1], sizeof(color), color, 0); 
    glVertexArrayVertexBuffer(vertex_array_object, 1, buffers[1], 0, sizeof(GLfloat) * 3); 
    glVertexArrayAttribFormat(vertex_array_object, 1, 3, GL_FLOAT, GL_FALSE, 0); 
    glVertexArrayAttribBinding(vertex_array_object, 1, 1); 
    glEnableVertexArrayAttrib(vertex_array_object, 1); 
    glBindVertexArray(vertex_array_object); 

    GLfloat clearcolor[4] = { 0.0,0.0,0.0,1.0 }; 
    glPointSize(40); 

    int width, height; 
    while (!glfwWindowShouldClose(window)) 
    { 
     glfwGetWindowSize(window, &width, &height); 
     glViewport(0, 0, width, height); 
     glClearBufferfv(GL_COLOR, 0, clearcolor); 

     for (int i = 0; i < 9; i++) { 
      color[i] = (rand() % 2); 
     } 
     glVertexAttrib3fv(1, color); 

     glDrawArrays(GL_TRIANGLES, 0, 3); 

     glfwSwapBuffers(window); 
     glfwPollEvents(); 
    } 

    glDeleteVertexArrays(1,&vertex_array_object); 
    glDeleteProgram(program); 

    glfwTerminate(); 
    return 0; 
} 

回答

1
glNamedBufferStorage(buffers[1], sizeof(color), color, 0); 

這告訴OpenGL複製當前存儲給定陣列中的在給定的buffer object數據。它不會在color和存儲的緩衝區數據之間創建一些鏈接。處理此命令後,OpenGL無法知道您正在修改color

glVertexAttrib3fv沒有做你認爲的事情。

如果要更改OpenGL緩衝區對象中的數據,則必須進行OpenGL調用以將不同的數據上載到緩衝區中。

此外,您創建了一個靜態緩衝區(使用參數爲0)。靜態緩衝區的全部要點是它的靜態:其內容在創建時刻是固定的。因此,任何上傳命令,如glNamedBufferSubData都不起作用。

因此,您需要決定如何更新緩衝區的數據。你想映射它並通過指針更改數據嗎?你想持久地圖嗎?你想用glNamedBufferSubData來更新嗎?在更新緩衝區之前是否要使緩衝區無效?有許多更新OpenGL緩衝區對象的策略。

最簡單的一個將是無效的,並使用glNamedBufferSubData,但你需要的是一個適當的使用參數:

glNamedBufferStorage(buffers[1], sizeof(color), color, GL_DYNAMIC_STORAGE_BIT); 

...

while (!glfwWindowShouldClose(window)) 
{ 
    glfwGetWindowSize(window, &width, &height); 
    glViewport(0, 0, width, height); 
    glClearBufferfv(GL_COLOR, 0, clearcolor); 

    for (int i = 0; i < 9; i++) { 
     color[i] = (rand() % 2); 
    } 

    glInvalidateBufferData(buffer[1]); 
    glNamedBufferSubData(buffer[1], 0, sizeof(color), color); 

    glDrawArrays(GL_TRIANGLES, 0, 3); 

    glfwSwapBuffers(window); 
    glfwPollEvents(); 
} 
+0

感謝您的幫助。它解決了我的問題 –