加载着色器
GLuint LoadShader (GLenum type, const char *shaderSrc){
GLuint shader;
GLint compiled;
//Create the shader object
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, &compiled);
if (!compiled) {
//Retrieve the compiler messages when compilation fails
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char *infoLog = malloc(sizeof(char) * infoLen);
glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
printf("Error compiling shader: \n%s\n",infoLog);
free(infoLog);
}
glDeleteShader(shader);
return 0;
}
return shader;
}
下一步创建和链接程序
程序对象是一个容器对象,可以将着色器与之连接,并链接一个最终的可执行程序
glGetProgramiv(GLuint program, GLenum pname, Glint *params)
pname 获取信息的参数:
> GL_LINK_STATUS 检查链接是否成功
> GL_ACTIVE_ATTRIBUTES 返回定点着色器中活动属性的数量
> GL_ACTIVE_ATTRIBUTE_MAX_LENGTH 返回最大属性名称的最大长度
> GL_ACTIVE_UNIFORMS 和 GL_ACTIVE_UNIFORM_MAX_LENGTH 分别返回活动统一变量的数量和最大统一变量名称的最大长度
> GL_ATTACHED_SHADERS 连接到程序对象的着色器数量
> GL_INFO_LOG_LENGTH查询信息日志
> GL_TRANSFORM_FEEDBACK_BUFFER_MODE 返回 GL_SEPARATE_ATTRIBS 或 GL_INTERLEAVED_ATTRIBS 表示变换反馈启用时的缓冲区模式
> GL_TRANSFORM_FEEDBACK_VARYINGS 和 GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH分别返回程序的变换反馈模式中捕捉的输出变量数量和输出变量名称的最大长度
等等...
创建程序,连接着色器并链接程序
GLint programObject = glCreateProgram();
if (programObject == 0) {
return 0;
}
glAttachShader(programObject, shader);
glAttachShader(programObject, shader);
//Link the program
glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
if (!linked) {
//Retrieve compiler error messages when linking fails
GLint infolen = 0;
glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infolen);
if (infolen > 1) {
char *infoLog = malloc(sizeof(char) * infolen);
glGetProgramInfoLog(programObject, infolen, NULL, infoLog);
printf("Error linking program:\n%s\n",infoLog);
free(infoLog);
}
glDeleteProgram(programObject);
return false;
}
//Use the programe object
glUseProgram(programObject);
建立一个统一变量缓冲区对象:
GLuint blockId, bufferId;
GLint blockSize;
GLuint bindingPoint = 1;
GLfloat lightData[] = {
1.f,0.f,0.f,0.f,0.f,0.f,0.f,1.f
};
//Retrieve the uniform block index
blockId = glGetUniformBlockIndex(program, "LightBlock");
//Associate the uniform block index with a binding point
glUniformBlockBinding(program, blockId, bindingPoint);
//Get the size of lightData; alternatively
//we can calculate it using sizeof(lightData) in this example
glGetActiveUniformBlockiv(program, blockId, GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize);
//Create and fill a buffer object
glGenBuffers(1, &bufferId);
glBindBuffer(GL_UNIFORM_BUFFER, bufferId);
glBufferData(GL_UNIFORM_BUFFER, blockSize, lightData, GL_DYNAMIC_DRAW);
//Bind the buffer object to the uniform block binding point
glBindBufferBase(GL_UNIFORM_BUFFER, bindingPoint, buffer);
未完...
网友评论