OpenGLES---点精灵


PositionSpriteShader.hpp

//varying 关键字 :vs和fs共享数据(建议vs和fs中速写一致)
const char *vs={
	"precision lowp float;"
	"uniform   mat4 _MVP;"
	"uniform   float _positionSize;"
	"attribute vec3 _position;\n"  //点大小
	"attribute vec4 _color;\n"     //外边出入的颜色
	"varying   vec4 _outColor;\n"  //共享颜色
	"void main(){\n"
	"	vec4   pos	= vec4(_position.x,_position.y,_position.z,1);\n"
	"	_outColor   = _color;\n"
	"	gl_Position = _MVP * pos;\n"
	"	gl_PointSize = _positionSize;\n"
	"}"
};
const char *fs={
	"precision lowp float;"
	"varying	vec4	  _outColor;"
	"uniform   sampler2D  _tId;"     //纹理id
	"void main(){"
	"	vec4  tColor = texture2D(_tId,gl_PointCoord);"
	"	gl_FragColor= _outColor * tColor;"
	"}"
};

shaderValue=createP(vs,fs);
_MVP      = glGetUniformLocation(shaderValue.pID,"_MVP");
_color    = glGetAttribLocation(shaderValue.pID,"_color");
_position = glGetAttribLocation(shaderValue.pID,"_position");
_positionSize=glGetUniformLocation(shaderValue.pID,"_positionSize");

_tId    = glGetUniformLocation(shaderValue.pID,"_tId");


vec2  gl_PointCoord  点精灵的二维空间坐标范围在(0.0, 0.0)到(1.0, 1.0)之间,仅用于点图元和点精灵开启的情况下


PositionSpriteWin.hpp

//清楚缓冲
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//设置视口的大小
glViewport(0,0,_width,_height);



struct Vertex{
	CELL::float3 pos;
	CELL::float2 uvpos;
	CELL::float4 rgba;
};

float	x=150,y=100,z=-1;
float   w   =   100;
float   h   =   100;
float rept=100;

Vertex rood[]={
	CELL::float3(x,y,z),	CELL::float2(0,0),			CELL::float4(1,1,1,1),
	CELL::float3(x,y+h,z),	CELL::float2(0,rept),		CELL::float4(1,1,1,1),
	CELL::float3(x+w,y,z),	CELL::float2(rept,0),		CELL::float4(1,1,1,1),
	CELL::float3(x+w,y+h,z),CELL::float2(rept,rept),	CELL::float4(1,1,1,1),
};


CELL::matrix4	project=CELL::ortho(0,(float)_width,(float)_height,0,-100,100);

shader.begin();

	/** 给Uniform变量赋值
	* @param location 变量
	* @param count 值数量(当前数据里面有几个值 1,2,3,4)
	* @param transpose 是否转置(true,false)
	* @param value 赋值数据
	*/
	glUniformMatrix4fv(shader._MVP,1,false,project.data());
	glBindTexture(GL_TEXTURE_2D,textureId);
	glUniform1i(shader._tId,0);
	glUniform1f(shader._positionSize,54);

	/** 设置顶点属性
	* @param indx 属性id
	* @param size 值数量(2,3,4) 
	* @param type 数据类型 (GL_UNSIGNED_BYTE,GL_FLOAT...)
	* @param normalized  规格化(false,true)
	* @param stride 偏移量(数据的偏移量)
	* @param ptr  数据
	*/
	glVertexAttribPointer(shader._color,4,GL_FLOAT,false,sizeof(Vertex),&rood[0].rgba);
	glVertexAttribPointer(shader._position,3,GL_FLOAT,false,sizeof(Vertex),&rood[0].pos);
	glDrawArrays(GL_POINTS,0,sizeof(rood)/sizeof(rood[0]));

shader.end();


源码地址

http://pan.baidu.com/s/1geVargZ    OpenGL_ES(6.6).zip


画了4个点(点精灵不要传uv坐标)

OpenGLES---点精灵_第1张图片




你可能感兴趣的:(OpenGLES)