2017-01-02 117 views
1

我的片段着色器不工作,它只繪製白色。 我認爲着色器在一起是不行的。我嘗試多次改變顏色並且沒有運氣。Opengl片段着色器只繪製白色

#include <glew.h> 
#include <glfw3.h> 

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" 
    "void main()     \n" 
    "{        \n" 
    "gl_Position = position;   \n" 
    "}        \n" 
}; 

static const GLchar * fshadersource[] = { 
    "#version 450 core    \n" 
    "out vec4 colorO;     \n" 
    "void main()     \n" 
    "{        \n" 
    " colorO = vec4(1.0,1.0,0.0,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); 

program = glCreateProgram(); 
glAttachShader(program, vshader); 
glAttachShader(program, fshader); 
glLinkProgram(program); 

glDeleteShader(vshader); 
glDeleteShader(fshader); 

return program; 
} 

int main(void) 
{ 
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 const GLfloat colors[] = {1.0,0.0,1.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(colors), colors, 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.4,1.0 }; 
glPointSize(40); 

while (!glfwWindowShouldClose(window)) 
{ 
    glClearBufferfv(GL_COLOR, 0, clearcolor); 

    glDrawArrays(GL_TRIANGLES, 0, 3); 

    glfwSwapBuffers(window); 
    glfwPollEvents(); 
} 

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

glfwTerminate(); 
return 0; 
} 

有人可以幫助我我仍然試圖得到opengl的掛鉤。

+2

編譯着色器時可以檢查錯誤。例如,您正在將vec3的位置分配給gl_Position,即vec4 – FamZ

+1

也可能在GL調用中出現錯誤,您可以使用glGetError()或一些glew錯誤來檢測這些錯誤。這些很可能在你的情況,因爲它似乎像你的屏幕甚至沒有清除 – FamZ

+0

謝謝,問題是設置gl_Position到position.Just一個錯誤。謝謝! –

回答

1

問題就出在你的頂點着色器,你不能直接分配給position因爲gl_Positionposition一個vec3gl_Positionvec4。 你可以這樣做:

gl_Position = vec4(position, 1.0f); 

,或者你可以定義positionvec4即使你的位置vec3position的最後一個組件將在這種情況下,自動設置爲1.0f

layout (location=0) in vec4 position; 

您可以在您撥打glCompileShader後檢測編譯錯誤,因爲您需要撥打glGetShaderivGL_COMPILE_STATUS作爲第二個參數,第三個參數是指向應存儲結果的變量的指針。這給出0GL_FALSE - 有一些編譯錯誤)或1GL_TRUE - 編譯成功)。如果出現錯誤,您可以通過調用glGetShaderInfoLog來獲取錯誤日誌,您還需要獲取信息日誌長度glGetShaderiv(以GL_INFO_LOG_LENGTH作爲第二個參數)。您可以對鏈接器錯誤執行相同的操作,但您需要調用glGetProgram*變體而不是glGetShader*變體。

0
gl_Position = position; 

需要。

gl_Position = vec4(position,1.0); 

因爲gl_Position是vec4,position是vec3。 感謝FamZ幫助我發現我的錯誤。