{目录
RectShader
RectWin 窗口
画东西的几种类型
引用Shader中的属性(自己在Shader中写的一些需要传值的类型) glGetU/A...
设置Shader数据(Shader什么类型就用什么类型设置内容) glU/V...
}
-- RectShader
#include <gles2/gl2.h>
#include <assert.h>
typedef int uniform;
typedef int attribute;
typedef int location;
class ShaderId
{
public:
ShaderId(){id=-1;};
public:
int id;
};
class ShaderVeual
{
public:
ShaderVeual(){_programId=-1;}
public:
int _programId;
ShaderId _vertex;
ShaderId _fragment;
};
class RectShader
{
public:
ShaderVeual shaderveual;
attribute _position;
uniform _color;
uniform _MVP;
public :
RectShader(){
_position=-1;
_color=-1;
_MVP=-1;
}
void shaderInit(){
const char* vs = {
"precision lowp float;"//制定float的精度(lowp低精度)
"uniform mat4 _MVP;"
"attribute vec2 _position;"
"void main()"
"{"
" vec4 pos = vec4(_position,0,1);"
" gl_Position = _MVP * pos;"
"}"
};
const char* ps = {
"precision lowp float;"
"uniform vec4 _color;"
"void main()"
"{"
" gl_FragColor = _color;"
"}"
};
shaderveual=createProgram(vs,ps);
//获取属性的位置
_position = glGetAttribLocation(shaderveual._programId, "_position");
_color = glGetUniformLocation(shaderveual._programId,"_color");
_MVP = glGetUniformLocation(shaderveual._programId,"_MVP");
}
void begin(){
// 使用程序
glUseProgram(shaderveual._programId);
//启用顶点数组
glEnableVertexAttribArray(_position);
}
void end(){
//禁用顶点数组
glDisableVertexAttribArray(_position);
// 使用完成
glUseProgram(0);
}
virtual ShaderVeual createProgram( const char* vertex,const char* fragment ){
ShaderVeual program;
bool error = false;
do{
if (vertex){
// 创建Shader (GL_VERTEX_SHADER 顶点Shader,GL_FRAGMENT_SHADER 像素Shader)
program._vertex.id = glCreateShader( GL_VERTEX_SHADER );
//制定shader的源代码
glShaderSource( program._vertex.id, 1, &vertex, 0 );
//编译Shader
glCompileShader( program._vertex.id )