GLSL既适用于顶点着色器,也适用于片段着色器。OpenGL中使用GLSL着色器步骤
GLuint shader,program; GLuint compiled, linked; const GLchar* shaderSrc[] = { "void main(){" " gl_Position = gl_ModelViewProjectionMatrix *gl_Vertex;" "}" }; shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(shader, 1, shaderSrc, NULL); glCompileShader(shader); glGetShaderInfoLog(shader, GL_COMPILE_STATUS, &compiled); if(!compiled){ GLint length; GLchar* log; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length); log = (GLchar*)malloc(length); glGetShaderInfoLog(shader, length, &length, log); fprintf(stderr, "compile log = '%s '\n", log); free(log); } program =glCreateProgram(); glAttachShader(program, shader); glLinkProgram(program); glGetProgramiv(program, GL_LINK_STATUS, &linked); if(linker){ glUseProgram(program); }else{ GLint length; GLchar* log; glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length); log = (GLchar*)malloc(length); glGetProgramInfoLog(program, length, &length, log); fprintf(stderr, "link log = '%s ' \n", log); free(log); }
删除着色器:
void glDeleteShader(GLuint shader); //若正在被使用,标记为删除,使用完后删除//若验证通过,GL_VALIDATE_STATUS被设置为GL_TRUE, 调用glGetProgramiv()查询GL_VALIDATE_STATUS的值。
原文地址:http://blog.csdn.net/u010002704/article/details/41945975