1.创建着色器
GLuint glCreateShader(GLenum type);
type: GL_VERTEX_SHADER(顶点着色器)、GL_FRAGMENT_SHADER(纹理着色器)
2.删除着色器
void glDeleteShader(GLuint shader);
shader:要删除的着色器句柄
3.着色器源码
void glShaderSource(GLuint shader, GLsizei count, const char ** string, const GLint * length);
shader:着色器对象句柄
count:着色器源码的字符数
string:指向着色器源码字符串矩阵的指针
length:长度
4.编译着色器
void glCompileShader(GLuint shader)
shader:着色器句柄
5.debug信息输出
void glGetShaderiv(GLuint shader, GLenum pname, GLint *params)
shader:着色器句柄
pname:要查询的信息 GL_COMPLE_STATUS(编译是否完成)、GL_DELETE_STATUS(着色器是否被删除)、GL_INFO_LOG_LENGTH(编译信息)、GL_SHADER_SOURCE_LENGTH(着色器源码)、GL_SHADER_TYPE(着色器类型)
p
6.获得调用日志,在使用glGetShaderiv(GL_INFO_LOG_LENGTH),分配足够的字符串来储存info log
void glGetShaderInfoLog(GLuint shader, GLsizei maxLength, GLsizei *length, GLchar *infoLog);
shader:着色器句柄
maxLength:日志缓存大小
length:
infoLog:日志内容
例:装在着色器
GLuint loadShader(GLenum type, const char *shaderSrc)
{
GLuint shader;
GLint compiled;
shader = glCreateShader(type);
if(shader == 0)
return 0;
//Load the shader source
glShaderSource(shader, 1, &shaderSrc, NULL);
//compile the shader
glCompileShader(shader);
//check the compile status
glGetShaderiv(shader, GL_COMPILE_STATUS, &compile);
if(!compiled){
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1){
char *infoLog = malloc(sizeof(char) * infoLen);
glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
//输出log
free(infoLog);
}
glDeleteShader(shader);
return 0;
}
return shader
}
7.创建源程序
GLuint glCreatePrograme();
8.删除源程序
void glDeleteProgram(GLuint program);
program:要删除的项目句柄
9.链接着色器(顶点/片段)到项目
void glAttachShader(GLuint program, GLuint shader);
program:项目句柄
shader:这色器句柄
10.分离着色器
void glDetachShader(GLuint program, GLuint shader);
program:项目句柄
shader:着色器句柄
11.链接项目
void glLinkProgram(GLuint program);
program:项目句柄
12.检查链接的结果:
void glGetProgramiv(GLuint program, GLenum pname, GLint *params);
program:项目句柄
pname: 要查询的信息
GL_ACTIVE_ATTRIBUTES
GL_ACTIVE_ATTRIBUTES_MAX_LENGTH
GL_ACTIVE_UNIFORMS
GL_ACTIVE_UNIFORMS_MAX_LENGTH
GL_ACTIVE_SHADERS
GL_DELETE_STATUS
GL_INFO_LOG_LENGTH
GL_LINK_STATUS
GL_VALIDATE_STATUS
params:保存查询结果的整形指针
13.检查项目当前的执行状态
void glValidateProgram(GLuint program);
program:项目句柄
14.开始使用program
void glUseProgram(GLuint program);
program:项目句柄
例子:创建、连接着色器、链接项目
//create the program object
programObject = glCreateProgram();
if(programObject == 0){
return 0;
}
glAttachShader(programObject, vertexShader);
glAttachShader(programObject, fragmentShader);
//link the program
glLinkProgram(programObject);
//check the link status
glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
if(!linked){
GLInt infoLen = 0;
glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1){
char * infoLog = malloc(sizeof(char) * infoLen);
glGetProgramInfoLog(programObject, infoLen, NULL, InfoLog);
//输出log
free(infoLog);
}
glDeleteProgram(programObject);
return FALSE;
}
//use program
glUseProgram(userData->programObject);
15.查找常量的细节
void glGetActiveUniform(GLuint program, GLuint index, GLsizei bufSize, GLsizei *length, GLint *size, GLenum *type, char * name);
program:项目句柄
index:被查询常量的索引
bufSize:缓存大小
length:数目
size:1
type:GL_FLOAT, GL_FLOAT_VEC2, GL_FLOAT_VEC3, GL_FLOAT_VEC4 GL_INT, GL_INT_VEC2, GL_INT_VEC3, GL_INT_VEC4, GL_BOOL GL_BOOL_VEC2, GL_BOOL_VEC3, GL_BOOL_VEC4, GL_FLOAT_MAT2, GL_FLOAT_MAT3, GL_FLOAT_MAT4, GL_SAMPLER_2D, GL_SAMPLER_CUBE
name:
16.得到常量的地址
GLint glGetUniformLocation(GLuint program, const char * name);
program:项目句柄
name:常量的名字