注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

hhfighting的博客

以责人之心责己,以恕己之心恕人

 
 
 

日志

 
 
 
 

GLSL started  

2012-11-01 01:46:01|  分类: 3D reconstructio |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
1 compile shader
GLuint shader;
shader = glCreateShader(type); //create a shader object
glShaderSource(shader, 1,"file path", &length); //set the shader source
glCompileShader(shader); //compile


/* make sure the compilation was successful */
GLint length,result;
glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
if(result == GL_FALSE) {
char *log;
/* get the shader info log */
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
log = malloc(length);
glGetShaderInfoLog(shader, length, &result, log);
/* print an error message and the info log */
fprintf(stderr, "shaderCompileFromFile(): Unable to compile %s: %s\n", filePath, log);
free(log);
}

2 shader attached to a program

program = glCreateProgram();      //create a program object
glAttachShader(program, shader); // attach the shader to the program
3 Link a program
glLinkProgram(g_program);
make sure that there were no errors  in the stage of link:
glGetProgramiv(g_program, GL_LINK_STATUS, &result);
if(result == GL_FALSE) {
GLint length;
char *log;

/* get the program info log */
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
log = malloc(length);
glGetProgramInfoLog(program, length, &result, log);

/* print an error message and the info log */
fprintf(stderr, "sceneInit(): Program linking failed: %s\n", log);
free(log);

/* delete the program */
glDeleteProgram(program);
program = 0;
}
4 using the shaders
glUseProgram(g_program);  //enable a program 
glUseProgram(0)---disable a program
5 pass some informations to shaders
g_programCameraPositionLocation = glGetUniformLocation(g_program, "cameraPosition"); 
//retrieve the locations of some uniform variables that are defined in the shaders
//this location which we obtained after linking the program object
glUniform3fvARB(g_programCameraPositionLocation, 1, g_cameraPosition); //set uniform variables

6 Notes
– The conditional branch, such as if for while, is much more expensive than in CPU. Don’t overuse it, especially in fragment shader.
– Always make sure shader is compiledcorrectly.
– Make use of default variables like gl_Vertex, gl_Normal, gl_XXMatrix to save your time.
– Check variables in question : Output it as color.
7 useful functions
? normalize(v) : unitize a vector
? dot(u,v) : dot product
? ftransform() : return transformed vertex positions ( Vertex shader only ! )
? reflect(v,n) : get reflected direction( useful for specular ! )
8 variable qualifier type
All uniform, attribute and varying types HAVE to be global. You are not allowed to specify a uniform/attribute/varying type in a function or a void.
(1) attribute
仅在vertex shader中有,是read-only。
(2) varying
 Passing data from Vertex shader to Fragment shader.在vertex shader 中是read-write,在fragment shader中是read-only。
 Data that will be interpolated during rasterization.

(3) uniform

A uniform variable is a variable that we can write to in our application and then send this value to Shader program.

uniform变量是read-only,which do not change during rendering。


9 the check list:

  • Use glActiveTexture for each texture unit
  • Use 0, 1, .. texture unit index while assigning values to uniform values.
  • Call glUseProgram() before trying to set uniform values.
  • Bind texture with texture id for each glActiveTexture.
reference:
http://joshbeam.com/articles/getting_started_with_glsl/


  评论这张
 
阅读(114)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017