2015-07-09 155 views
3

我無法使emscripten使用openGL着色器。該項目與emscripten和gcc都編譯得很好,但是當我嘗試運行emscripten輸出時失敗。在opengl着色器中使用emscripten

我從編譯頂點着色器中得到的錯誤:

ERROR: 0:1: 'core' : invalid version directive 
ERROR: 0:3: 'layout' : syntax error 

的錯誤,我從編譯片段着色器得到:

ERROR: 0:1: 'core' : invalid version directive 
ERROR: 0:3: 'in' : storage qualifier supported in GLSL ES 3.00 only 
ERROR: 0:3: '' : No precision specified for (float) 
ERROR: 0:5: 'out' : storage qualifier supported in GLSL ES 3.00 only 
ERROR: 0:5: '' : No precision specified for (float) 

我編譯這個項目的命令:

em++ src/*.cpp -Iinclude/ -o test.html -std=c++11 -s USE_GLFW=3 -s FULL_ES3=1 

頂點着色源:

#version 330 core 

layout (location = 0) in vec3 position; 
layout (location = 1) in vec3 in_color; 

uniform mat4 model; 
uniform mat4 projection; 

out vec3 out_color; 

void main() 
{ 
    gl_Position = projection * model * vec4(position, 1.0f); 
    out_color = in_color; 
} 

片段着色器源:

#version 330 core 

in vec3 out_color; 

out vec4 color; 

void main() 
{ 
     color = vec4(out_color, 1.0); 
} 

的着色器被裝載作爲從輸出由xxd -i我在C++ 11在Linux上的工作提供字符數組。該程序工作得很好,當我運行它本地,我已經嘗試在Firefox和鉻運行emscripten輸出。

這似乎是不同版本之間的問題。有沒有辦法讓emscripten與我目前的功能一起工作,還是必須以不同的方式編寫着色器?如果我不得不重寫我的着色器,我應該如何編寫它們?

+0

嘗試從'#version'刪除'core' ;無論如何,這是默認設置。 –

+0

我刪除了'核心',擺脫了關於核心的錯誤,但其餘的錯誤依然存在。 – TheRoach

回答

5

着色器代碼必須是WebGL着色器代碼才能在瀏覽器上工作。我不認爲emscripten將着色器代碼(本例中爲GLSL 3.3)轉換爲與webGL兼容的GLSL ES 1.0。

你必須在頂點/片段着色/在使用attribute,而不是in在頂點着色器,爲varying並使用gl_FragColor作爲片段着色器的輸出變量。也不支持layout,並且變量需要精確定義。檢查WebGL備忘單here

0

在當前的emscripten中,您可以使用WebGL2和GLSL ES 3.00。你需要改變你的#version

#version 300 es 

您還需要一個默認精度添加到您的片段着色器。

如果是我我只是換我的代碼glShaderSource是像

GLint shaderSourceWrapper(GLint shader, const std::string src) { 
    #ifdef __EMSCRIPTEN__ 
    // replace '#version.*' with '#version 300 es' 
    // if it's a fragment shader add 'precision highp float' 
    // do anything else relevant like warn if there are 
    // unsupported #extension directives 
    #endif 
    glShaderSource(shader, ...) 

甚至做到這一點在JavaScript的水平like this