我一直沒有進入OpenGL很長一段時間,從來沒有用過OpenGLES。 對於我的項目我必須使用OpenGLES,由glGetString(GL_VERSION)
打印的版本是3.0,但因爲我正在做一個教程拼貼是完全可能的,我混合代碼不能在這個版本(即這就是爲什麼#version
標籤在着色器代碼上發表評論的原因,但它並沒有解決問題)。GLSL着色器編譯,但程序沒有鏈接
這是頂點着色器的代碼:
const GLchar * vertex_shader_source =
// "#version 100\n "
// " "
" attribute vec2 a_TexCoordinate; /* Per-vertex texture coordinate */ "
" /* information we will pass in. */ "
" attribute vec2 coord2d; "
" "
" "
" varying vec2 v_TexCoordinate; // This will be passed into the fragment "
" "
" void main(){ "
" "
" gl_Position = vec4(coord2d, 0.0, 1.0); "
" "
" // Pass through the texture coordinate. "
" v_TexCoordinate = a_TexCoordinate; "
" } ";
glShaderSource(vertex_shader_index, 1, &vertex_shader_source, nullptr);
glCompileShader(vertex_shader_index);
glGetShaderiv(vertex_shader_index, GL_COMPILE_STATUS, &compile_ok);
而這正是片段着色器的代碼:
const GLchar * fragment_shader_source =
// "#version 100\n "
// " "
"uniform sampler2D u_Texture; // The input texture. "
" "
"varying vec2 v_TexCoordinate; /* Interpolated texture */ "
" /* coordinate per fragment */ "
" "
" void main(){ "
" "
" // Output color = color of the texture at the specified UV "
" gl_FragColor = texture2D(u_Texture, v_TexCoordinate); "
" } ";
glShaderSource(fragment_shader_index, 1, &fragment_shader_source, nullptr);
glCompileShader(fragment_shader_index);
glGetShaderiv(fragment_shader_index, GL_COMPILE_STATUS, &compile_ok);
這是程序的代碼:
m_program = glCreateProgram();
glAttachShader(m_program, vertex_shader_index);
glAttachShader(m_program, fragment_shader_index);
glLinkProgram(m_program);
glGetProgramiv(m_program, GL_LINK_STATUS, &link_ok);
變量link_ok
爲爲假,而變量compile_ok
均爲爲真。從程序的鏈接擴展的錯誤說:
(0)頂點信息:C3001沒有程序規定 (0)片段信息:C3001沒有程序規定
好的,也許這可以解決!現在由於「未定義的浮點變量精度」行125,片段不編譯,但着色器短於125行......你能解釋哪個浮點是指的錯誤? – nyarlathotep108
@ nyarlathotep108如果您使用OpenGL ES,則必須指定浮點精度。在片段着色器的頂部添加像這樣的'precision mediump float;'。看看這個:http://stackoverflow.com/questions/13780609/what-does-precision-mediump-float-mean – Reigertje