2016-12-31 113 views
0

我想要編譯頂點着色器:OpenGL不會編譯GLSL Shader。缺少擴展?

#version 430 

layout(location = 0)in vec3 vertexPosition; 
layout(location = 1)in vec3 vertexNormal; 
layout(location = 2)in vec2 vertexUV; 

out Vertex{ 
vec2 uv; 
vec4 normal; 
}vertexOut; 

void main(){ 
    gl_Position = vec4(
    vertexPosition.x, 
    vertexPosition.y, 
    vertexPosition.z, 
    1.0f); 
    vertexOut.uv = vertexUV; 
    vertexOut.normal = vec4(vertexNormal, 0.0f); 
} 

這樣

pShader.ID = glCreateShader(GL_VERTEX_SHADER); 
std::ifstream shaderFile; 
shaderFile.open(pShader.path); 

if (shaderFile.fail()) { 
    printf("!!!\nCannot open Shader File %s ! Shader compilation cancelled!", pShader.path.c_str()); 
} 
else { 
    std::string line; 
    while (getline(shaderFile, line)) { 
     pShader.content += line + '\n'; 
     ++pShader.lines; 
    } 
    const char* shaderContent = pShader.content.c_str(); 
    glShaderSource(pShader.ID, 1, &shaderContent, &pShader.lines); 
    glCompileShader(pShader.ID); 
    GLint success = 0; 
    glGetShaderiv(pShader.ID, GL_COMPILE_STATUS, &success); 
    if (success == GL_FALSE) { 
     //errror checking 
     } 

,但我得到的編譯錯誤

Vertex Shader failed to compile with the following errors: 
ERROR: 0:3: error(#132) Syntax error "layou" parse error 
ERROR: errror(#273) 1 compilation errors. No code generated 

在我的片段着色器,我也得到一個「 /「解析錯誤,但我可以找到它。

我使用glew進行輸入和上下文的擴展加載和glfw。 當我運行glewinfo.exe時,有一些擴展被標記爲丟失,但我的AMD Radeon HD 7800驅動程序是最新的。什麼是問題,我該怎麼辦?

這些都是glewinfo.exe結果:http://m.uploadedit.com/ba3s/148318971635.txt

+0

檢查你傳入['glShaderSource'](http://docs.gl/gl4/glShaderSource),尤其是你的'length'參數似乎很不正確 – PeterT

回答

3

的問題是,要傳遞一個錯誤的長度glShaderSource。最後一個參數必須包含每個字符串中的字符數。由於您可以同時傳遞多個字符串,因此這是一個包含count(第二個參數)元素的數組。

在你的榜樣正確的代碼是:

const char* code = shaderContent.c_str(); 
int length = shaderContent.size(); 
glShaderSource(pShader.ID, 1, &code, &length); 

此外,逐行讀取整個文件也行不是很有效。

+0

謝謝!那就是訣竅。 你能快速指導我以更快的方式讀取文件嗎?那麼缺少的擴展呢?這很多人失蹤了嗎? – stimulate

+1

除非您的圖形卡支持它們,否則這是正常的。 – BDL

+1

檢查這個問題關於如何一次讀取整個文件:http://stackoverflow.com/questions/116038/what-is-the-best-way-to-read-an-entire-file-into-a- stdstring-在-C – BDL