转发,请保持地址:http://blog.csdn.net/stalendp/article/details/11492525
OpenGL是原理性和实践性比较强的一门技术,在学习的时候,如果能够跟着书中的例子,一边调试一边学习,效果将很好(这属于实验的一种类型吧,能够吧知识形象化,有助于学习兴趣的提高)。市面上有许多深入浅出的书籍讲的很好,比如《OpenGL SuperBible 5th Edition》、《OpenGL 4 Shanding Language Cookbook》等(前者的例子代码写的非常好,后者把OpenGL的原理讲的比较深入)。不过把这些书中的代码跑在特定的系统上,还是需要花一些代价的(比如在mac上,SLGL只支持到1.2,很多例子中的代码就需要改了;而且配置环境也是比较麻烦的)。在探寻了很久之后,发现Unity3D提供了很好的OpenGL的学习环境,并且还有一些很好的资料可以参考(强烈推荐Kenny Lammers的《Unity Shaders and Effects Cookbook》)。本文将介绍怎么在Unity3D上学习SLGL(关于OpenGL的pipeLine、VBO、PBO等其他一些,可以参考上面推荐的两本书,特别是把《OpenGL SuperBible 5th Edition》的那套工具类搞懂,就非常OK了)。这片文章源于对Minimal Shader的一个翻译和整理。
一、配置启动项(只有windows上需要)
- "C:\Program Files\Unity\Editor\Unity.exe" -force-opengl
二、创建Shader
- Shader "GLSL basic shader" { // defines the name of the shader
- SubShader { // Unity chooses the subshader that fits the GPU best
- Pass { // some shaders require multiple passes
- GLSLPROGRAM // here begins the part in Unity's GLSL
- #ifdef VERTEX // here begins the vertex shader
- void main() // all vertex shaders define a main() function
- {
- gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
- // this line transforms the predefined attribute
- // gl_Vertex of type vec4 with the predefined
- // uniform gl_ModelViewProjectionMatrix of type mat4
- // and stores the result in the predefined output
- // variable gl_Position of type vec4.
- }
- #endif // here ends the definition of the vertex shader
- #ifdef FRAGMENT // here begins the fragment shader
- void main() // all fragment shaders define a main() function
- {
- gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
- // this fragment shader just sets the output color
- // to opaque red (red = 1.0, green = 0.0, blue = 0.0,
- // alpha = 1.0)
- }
- #endif // here ends the definition of the fragment shader
- ENDGLSL // here ends the part in GLSL
- }
- }
- }
三、创建材质并绑定Shader
- Shader "GLSL basic shader" { // defines the name of the shader
- SubShader { // Unity chooses the subshader that fits the GPU best
- 。。。。。
四、把材质赋予游戏物体
关于OpenGL的资料:
Learning Modern 3D Graphics Programming
一个开发者的博客:One-minute Dungeon: Behind the scenes
游戏中法线贴图的技巧:A game of Tricks-Normal mapped Scripts