in out

       对于 vertex shader,每个顶点都会包含一次,它的主要工作时处理关于定点的数据,然后把结果传递到管线的下个阶段。

       以前版本的GLSL,数据会通过一些内建变量,比如gl_Vertex和gl_Normal,但现在,通常时使用通用顶点属性( generic vertex attributes)来提供,通常和一个Buffer object 想关联。对于程序员来说,现在可以自由去定义一些顶点的属性集来提供输入,只要在开头的时候用in关键字来声明就可以了。

       还有一种方式就是使用uniform variables。这种变量和属性变量的区别:属性变量是指每个顶点shader调用时,都会根据属性的位置从顶点缓冲中装入该顶点的相应属性值,而uniform变量,则对每个draw调用保持不变,这意味着你在draw调用前装入该变量,然后draw中每个顶点shader执行时,都能访问该变量,而且该变量值会保持不变。它可以声明在一个或者多个shader中,如果时声明在多个shader中,变量的类型必须一致。uniform变量常用来存储一些draw执行时候的常量数据,比如光照参数、变化矩阵、纹理对象句柄等等。

       下面是基于GLSL入门的例子的一个修改,通过增加一个uniform的旋转变量,对每个顶点进行旋转一定的角度。

       首先是basic.vert:

#version 400  layout (location = 0) in  vec2 in_Position; layout (location = 1) in  vec3 in_Color; out vec3 ex_Color; uniform mat4 RotationMatrix;  void main(void) {       gl_Position = RotationMatrix * vec4(in_Position.x, in_Position.y, 0.0, 1.0);     ex_Color = in_Color; }

增加了uniform的4维矩阵变量,存储旋转矩阵。

在main.cpp中修改如下:

首先添加一下头文件,因为要用到glm库。

#include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> using glm::mat4; using glm::vec3; 

然后在renderGL中修改代码如下:

glUseProgram(programHandle);  	float angle = 30;  	mat4 rotationMatrix = glm::rotate(mat4(1.0f), angle, vec3(0.0f,0.0f,1.0f)); 	GLuint location =glGetUniformLocationg(programHandle,"RotationMatrix"); 	 	if( location >= 0 ) 	{ 		glUniformMatrix4fv(location, 1, GL_FALSE,&rotationMatrix[0][0]); 	}  	//Draw a square 	int i;  	for (i=2; i <=4; i++)     {         /* Make our background black */         glClearColor(0.0, 0.0, 0.0, 1.0);         glClear(GL_COLOR_BUFFER_BIT);         /* Invoke glDrawArrays telling that our data is a line loop and we want to draw 2-4 vertexes */         glDrawArrays(GL_TRIANGLE_FAN, 0, i);     }       // Unbind shader     glUseProgram(0);

       修改的部分首先是生成选装的矩阵,glGetUniformLocation用于检测是否存在一个变量,然后通过glUniformMatrix4fv来绑定数值,最后在绘制的时候,shader就可以调用uniform数据了。



使用uniform blocks和uniform buffer object

       UBO,顾名思义,就是一个装载Uniform变量数据的Buffer Object。就概念而言,它跟VBO之类Buffer Object差不多,反正就是显存中一块用于储存特定数据的区域了。在OpenGL端,它的创建、更新、销毁的方式都与其他Buffer Object没什么区别,我们只不过把一个或多个uniform数据交给它,以替代glUniform的方式传递数据而已。这里必须明确一点,这些数据是给到这个UBO,存储于这个UBO上,而不再是交给ShaderProgram,所以它们不会占用这个ShaderProgram自身的uniform存储空间,所以UBO是一种全新的传递数据的方式,从路径到目的地,都跟传统uniform变量的方式不一样。自然,对于这样的数据,在Shader中不能再使用上面代码中的方式来指涉了。随着UBO的引入,GLSL也引入了uniform block这种指涉工具。

       uniform block是Interface block的一种,(layout意义容后再述)在unifom关键字后直接跟随一个block name和大括号,里面是一个或多个uniform变量。一个uniform block可以指涉一个UBO的数据——我们要把block里的uniform变量与OpenGL里的数据建立关联。

      还是基于上面的例子进行修改,我们需要达到下面的效果



首先我们重新写一个basic.frag

#version 400   in  vec3 texCoord; layout(location = 0) out vec4 fragColor;  uniform blobSettings{ 	vec4 innerColor; 	vec4 outerColor; 	float radiusInner; 	float radiusOuter; };  void main(void) {     float dx = abs(texCoord.x) - 0.5;     float dy = texCoord.y -0.5;     float dist = sqrt(dx*dx + dy*dy);     fragColor = mix(innerColor, outerColor, smoothstep(radiusInner, radiusOuter, dist)); }

首先定义texCoord作为从vertex shader的输如,然后fragColor作为输出,对图形对像素进行挨个着色。

basic.vert改变不是很大,增加了一个纹理坐标。

layout (location = 0) in  vec3 inPosition; layout (location = 1) in  vec3 vertexTextCoord; out vec3 texCoord;  void main(void) {  	texCoord = vertexTextCoord;     gl_Position = vec4(inPosition, 1.0); }


main.c中需要添加一个函数用于初始化UBO,对Uniform block中的数据进行绑定。

void initUniformBlockBuffer() {     // Get the index of the uniform block     GLuint blockIndex = glGetUniformBlockIndex(programHandle, "blobSettings");      // Allocate space for the buffer     GLint blockSize;     glGetActiveUniformBlockiv(programHandle, blockIndex,                               GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize);     GLubyte * blockBuffer;     blockBuffer = (GLubyte *) malloc(blockSize);      // Query for the offsets of each block variable     const GLchar *names[] = { "innerColor", "outerColor",                               "radiusInner", "radiusOuter" };  	     GLuint indices[4];     glGetUniformIndices(programHandle, 4, names, indices);      GLint offset[4];     glGetActiveUniformsiv(programHandle, 4, indices, GL_UNIFORM_OFFSET, offset);      // Store data within the buffer at the appropriate offsets     GLfloat outerColor[] = {0.0f, 1.0f, 0.0f, 0.0f};     GLfloat innerColor[] = {1.0f, 0.0f, 0.75f, 1.0f};     GLfloat innerRadius = 0.25f, outerRadius = 0.45f;      memcpy(blockBuffer + offset[0], innerColor, 4 * sizeof(GLfloat));     memcpy(blockBuffer + offset[1], outerColor, 4 * sizeof(GLfloat));     printf("Initsa VSBO!\n");     memcpy(blockBuffer + offset[2], &innerRadius, sizeof(GLfloat));     memcpy(blockBuffer + offset[3], &outerRadius, sizeof(GLfloat));      // Create the buffer object and copy the data     GLuint uboHandle;     glGenBuffers( 1, &uboHandle );     glBindBuffer( GL_UNIFORM_BUFFER, uboHandle );     glBufferData( GL_UNIFORM_BUFFER, blockSize, blockBuffer, GL_DYNAMIC_DRAW );      // Bind the buffer object to the uniform block     glBindBufferBase( GL_UNIFORM_BUFFER, blockIndex, uboHandle ); }

shader的初始化函数也要进行一些修改:

void initShader() {  /* We're going to create a square made from lines */   const GLfloat positionData[4][3] = {       {  -1.0,  1.0, 0.0   }, /* Top point */       {  1.0,  1.0, 0.0  }, /* Right point */       {  1.0, -1.0, 0.0  }, /* Bottom point */       { -1.0,  -1.0, 0.0  } }; /* Left point */            float tcData[] = {          0.0f, 0.0f,         1.0f, 0.0f,         1.0f, 1.0f,         1.0f, 0.0f,         1.0f, 1.0f,         0.0f, 0.0f     };       /* These pointers will receive the contents of our shader source code files */     GLchar *vertexsource, *fragmentsource;       /* These are handles used to reference the shaders */     GLuint vertexshader, fragmentshader;       /* This is a handle to the shader program */     GLuint shaderprogram;           /* Allocate and assign a Vertex Array Object to our handle */     glGenVertexArrays(1, &vao);       /* Bind our Vertex Array Object as the current used object */     glBindVertexArray(vao);       /* Allocate and assign two Vertex Buffer Objects to our handle */     glGenBuffers(2, vbo);       /* Bind our first VBO as being the active buffer and storing vertex attributes (coordinates) */     glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);          glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), positionData, GL_STATIC_DRAW);            /* Specify that our coordinate data is going into attribute index 0, and contains two floats per vertex */     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);       /* Enable attribute index 0 as being used */     glEnableVertexAttribArray(0);       /* Bind our second VBO as being the active buffer and storing vertex attributes (colors) */     glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);          glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tcData, GL_STATIC_DRAW);       /* Specify that our color data is going into attribute index 1, and contains three floats per vertex */      glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);       /* Enable attribute index 1 as being used */     glEnableVertexAttribArray(1); 	 	vShader = glCreateShader( GL_VERTEX_SHADER ); 	fShader = glCreateShader( GL_FRAGMENT_SHADER ); 	printf("Here\n"); 	if(0 == vShader || 0 == fShader) 	{ 		fprintf(stderr, "Error creating vertex shader.\n"); 		quit(1); 	} 	 	GLchar* vShaderCode = textFileRead("basic.vert"); 	GLchar* fShaderCode = textFileRead("basic.frag"); 	const GLchar* vCodeArray[1] = {vShaderCode}; 	const GLchar* fCodeArray[1] = {fShaderCode}; 	glShaderSource(vShader, 1, vCodeArray, NULL); 	glShaderSource(fShader, 1, fCodeArray, NULL); 	 	glCompileShader(vShader); 	glCompileShader(fShader); 	free(vShaderCode); 	free(fShaderCode);   	GLint result; 	glGetShaderiv( vShader, GL_COMPILE_STATUS, &result ); 	if( GL_FALSE == result ) 	{ 		fprintf( stderr, "Vertex shader compilation failed!\n" ); 		GLint logLen; 		glGetShaderiv( vShader, GL_INFO_LOG_LENGTH, &logLen ); 		if( logLen > 0 ) 		{ 			char * log = (char *)malloc(logLen); 			GLsizei written; 			glGetShaderInfoLog(vShader, logLen, &written, log); 			fprintf(stderr, "Shader log:\n%s", log); 			free(log); 		} 	}  	programHandle = glCreateProgram(); 	if(0 == programHandle) 	{ 		fprintf(stderr, "Error creating programHandle.\n"); 		quit(1); 	} 	 	glAttachShader(programHandle, vShader); 	glAttachShader(programHandle, fShader); 	 	 	glBindAttribLocation(programHandle, 0, "in_Position"); 	glBindAttribLocation(programHandle, 1, "in_Color"); 	 	glLinkProgram(programHandle); }

渲染的时候直接画一个正方形就可以了。

glUseProgram(programHandle); glDrawArrays(GL_QUADS,0,4); glUseProgram(0); 

编译命令

g++ main.c -o main -l SDL -lGL -lGLU -lglut -lGLEW

*shader调试的一点小技巧

由于没办法在shader使用打印语句,所以shader调试起来会有点麻烦,我们可以用glGet方法来获取一些状态变量来判断shder的状态,更常用的是改变shader的代码,然后利用渲染的结果来进行调试。比如:

void main(){   float bug=0.0;   vec3 tile=texture2D(colMap, coords.st).xyz;   vec4 col=vec4(tile, 1.0);    if(something) bug=1.0;    col.x+=bug;    gl_FragColor=col; }


 

代码下载

写一个C++的shader类

首先需要升级一下系统的glew库,老版本的glew4.x的很多特性都不支持。

http://glew.sourceforge.net/下载最新的1.10版,解压cd进目录,运行:

make 

sudo make install


GLSL的基本的知识到现在已经接触得差不多了,接下来为了更方便的学习,现在把shader封装成一个class, 加入到之前的框架。

代码就不贴了,点我去下载。



参考

OpenGL/GLSL数据传递小记(3.x) - http://www.zwqxin.com/archives/shaderglsl/communication-between-opengl-glsl-2.html

OpenGL 4.0 Shading Language Cookbook